JOHN SILLVESTOR commited on
Commit
9dbbe1d
·
verified ·
1 Parent(s): bfac50f

universal stream ranges for all browsers

Browse files
Files changed (1) hide show
  1. server.js +44 -36
server.js CHANGED
@@ -155,6 +155,17 @@ function destOk(req, allow) {
155
  return allow.indexOf(d) >= 0;
156
  }
157
 
 
 
 
 
 
 
 
 
 
 
 
158
  function streamLock() {
159
  return {
160
  "X-Content-Type-Options": "nosniff",
@@ -761,6 +772,22 @@ function parseRange(h, size) {
761
  return { start, end, partial: start > 0 || end < size - 1 };
762
  }
763
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
764
  function pipeBudget(start, end) {
765
  const n = Math.max(1, end - start + 1);
766
  return Math.min(PIPE_MAX_MS, Math.max(PIPE_MS, Math.ceil(n / (128 * 1024)) * 1000));
@@ -1030,7 +1057,7 @@ async function handleGenerate(req, res, url) {
1030
  }
1031
 
1032
  async function handleStream(req, res, url, msgId) {
1033
- if (!destOk(req, ["video", "audio", "empty"])) {
1034
  res.writeHead(403);
1035
  return res.end();
1036
  }
@@ -1081,29 +1108,23 @@ async function handleStream(req, res, url, msgId) {
1081
  if (sizeHint > 0) {
1082
  const rngHot = parseRange(req.headers.range, sizeHint);
1083
  const needHot = rngHot.end - rngHot.start + 1;
1084
- const rangedHot = !!req.headers.range;
1085
- const headersHot = Object.assign(streamLock(), {
1086
- "Content-Type": typeHint,
1087
- "Accept-Ranges": "bytes",
1088
- "Cache-Control": "private, no-store, no-transform",
1089
- "Content-Disposition": "inline",
1090
- "Content-Encoding": "identity",
1091
- "ETag": '"' + msgId + "-" + sizeHint + "-" + (ticket.h || "x") + '"',
1092
- "X-Accel-Buffering": "no",
1093
- "Content-Length": String(needHot)
1094
- });
1095
- if (rangedHot) {
1096
- headersHot["Content-Range"] = "bytes " + rngHot.start + "-" + rngHot.end + "/" + sizeHint;
1097
- }
1098
  if (req.method === "HEAD") {
1099
- res.writeHead(rangedHot ? 206 : 200, headersHot);
1100
  return res.end();
1101
  }
1102
  const hit = hotPrefix(msgId, rngHot.start, rngHot.end);
1103
- if (hit && hit.length >= needHot) {
1104
- res.writeHead(rangedHot ? 206 : 200, headersHot);
1105
- res.end(hit.subarray(0, needHot));
1106
- return;
 
 
 
 
 
 
 
1107
  }
1108
  }
1109
  let rec;
@@ -1127,25 +1148,12 @@ async function handleStream(req, res, url, msgId) {
1127
  }
1128
  const size = rec.size;
1129
  const rng = parseRange(req.headers.range, size);
1130
- const ranged = !!req.headers.range;
1131
- const headers = Object.assign(streamLock(), {
1132
- "Content-Type": httpVideoType(rec.mime, rec.name),
1133
- "Accept-Ranges": "bytes",
1134
- "Cache-Control": "private, no-store, no-transform",
1135
- "Content-Disposition": "inline",
1136
- "Content-Encoding": "identity",
1137
- "ETag": '"' + msgId + "-" + size + "-" + (rec.hash || "x") + '"',
1138
- "X-Accel-Buffering": "no"
1139
- });
1140
- headers["Content-Length"] = String(rng.end - rng.start + 1);
1141
- if (ranged) {
1142
- headers["Content-Range"] = "bytes " + rng.start + "-" + rng.end + "/" + size;
1143
- }
1144
  if (req.method === "HEAD") {
1145
- res.writeHead(ranged ? 206 : 200, headers);
1146
  return res.end();
1147
  }
1148
- const status = ranged ? 206 : 200;
1149
  try {
1150
  await pipeRange(bot, rec, rng.start, rng.end, req, res, status, headers, msgId);
1151
  } catch (e) {
 
155
  return allow.indexOf(d) >= 0;
156
  }
157
 
158
+ function streamDestOk(req) {
159
+ const d = String(req.headers["sec-fetch-dest"] || "").toLowerCase();
160
+ if (!d) return true;
161
+ const block = [
162
+ "document", "iframe", "frame", "fencedframe", "embed", "object",
163
+ "script", "style", "manifest", "worker", "sharedworker", "serviceworker",
164
+ "report", "xslt", "image", "font", "json"
165
+ ];
166
+ return block.indexOf(d) < 0;
167
+ }
168
+
169
  function streamLock() {
170
  return {
171
  "X-Content-Type-Options": "nosniff",
 
772
  return { start, end, partial: start > 0 || end < size - 1 };
773
  }
774
 
775
+ function mediaHeaders(type, msgId, size, hash, rng) {
776
+ const headers = Object.assign(streamLock(), {
777
+ "Content-Type": type,
778
+ "Accept-Ranges": "bytes",
779
+ "Cache-Control": "private, no-store, no-transform",
780
+ "Content-Disposition": "inline",
781
+ "ETag": '"' + msgId + "-" + size + "-" + (hash || "x") + '"',
782
+ "X-Accel-Buffering": "no",
783
+ "Content-Length": String(rng.end - rng.start + 1),
784
+ "Content-Range": "bytes " + rng.start + "-" + rng.end + "/" + size
785
+ });
786
+ if (rng.partial) headers.Connection = "close";
787
+ return headers;
788
+ }
789
+
790
+
791
  function pipeBudget(start, end) {
792
  const n = Math.max(1, end - start + 1);
793
  return Math.min(PIPE_MAX_MS, Math.max(PIPE_MS, Math.ceil(n / (128 * 1024)) * 1000));
 
1057
  }
1058
 
1059
  async function handleStream(req, res, url, msgId) {
1060
+ if (!streamDestOk(req)) {
1061
  res.writeHead(403);
1062
  return res.end();
1063
  }
 
1108
  if (sizeHint > 0) {
1109
  const rngHot = parseRange(req.headers.range, sizeHint);
1110
  const needHot = rngHot.end - rngHot.start + 1;
1111
+ const headersHot = mediaHeaders(typeHint, msgId, sizeHint, ticket.h, rngHot);
 
 
 
 
 
 
 
 
 
 
 
 
 
1112
  if (req.method === "HEAD") {
1113
+ res.writeHead(206, headersHot);
1114
  return res.end();
1115
  }
1116
  const hit = hotPrefix(msgId, rngHot.start, rngHot.end);
1117
+ if (hit && hit.length) {
1118
+ let send = 0;
1119
+ if (hit.length >= needHot) send = needHot;
1120
+ else if (needHot <= 2) send = 0;
1121
+ else if (hit.length >= 4096) send = hit.length;
1122
+ if (send > 0) {
1123
+ const sent = Object.assign({}, rngHot, { end: rngHot.start + send - 1, partial: rngHot.start > 0 || rngHot.start + send < sizeHint });
1124
+ res.writeHead(206, mediaHeaders(typeHint, msgId, sizeHint, ticket.h, sent));
1125
+ res.end(hit.subarray(0, send));
1126
+ return;
1127
+ }
1128
  }
1129
  }
1130
  let rec;
 
1148
  }
1149
  const size = rec.size;
1150
  const rng = parseRange(req.headers.range, size);
1151
+ const headers = mediaHeaders(httpVideoType(rec.mime, rec.name), msgId, size, rec.hash, rng);
 
 
 
 
 
 
 
 
 
 
 
 
 
1152
  if (req.method === "HEAD") {
1153
+ res.writeHead(206, headers);
1154
  return res.end();
1155
  }
1156
+ const status = 206;
1157
  try {
1158
  await pipeRange(bot, rec, rng.start, rng.end, req, res, status, headers, msgId);
1159
  } catch (e) {