JOHN SILLVESTOR commited on
Commit
c779bf8
·
verified ·
1 Parent(s): 121567d

Light-speed start: 1MB first window + RAM head/tail, not full-file 206

Browse files
Files changed (1) hide show
  1. server.js +46 -6
server.js CHANGED
@@ -16,10 +16,11 @@ const HASH_LEN = 6;
16
  const TOKEN_TTL = 900;
17
  const MIN_ALIGN = 4096;
18
  const MB = 1024 * 1024;
19
- const GET_SIZES = [524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096];
20
- const FIRST_GET = 524288;
21
- const HOT_HEAD = 524288;
22
  const HOT_TAIL = 1048576;
 
23
  const BOT_MAX = 24;
24
  const PIPE_MS = 45000;
25
  const PIPE_MAX_MS = 30 * 60 * 1000;
@@ -715,9 +716,10 @@ async function fillDuration(bot, rec, msgId) {
715
 
716
  function parseRange(h, size) {
717
  if (!size) return { start: 0, end: 0, partial: false };
718
- if (!h) return { start: 0, end: size - 1, partial: false };
 
719
  const m = /^bytes=(\d*)-(\d*)$/.exec(String(h).trim());
720
- if (!m) return { start: 0, end: size - 1, partial: false };
721
  let start;
722
  let end;
723
  if (m[1] === "") {
@@ -726,7 +728,13 @@ function parseRange(h, size) {
726
  end = size - 1;
727
  } else {
728
  start = Number(m[1]);
729
- end = m[2] === "" ? size - 1 : Number(m[2]);
 
 
 
 
 
 
730
  }
731
  if (!Number.isFinite(start) || start < 0) start = 0;
732
  if (!Number.isFinite(end) || end >= size) end = size - 1;
@@ -1043,6 +1051,38 @@ async function handleStream(req, res, url, msgId) {
1043
  res.writeHead(503);
1044
  return res.end();
1045
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1046
  let rec;
1047
  try {
1048
  rec = await fileFromMsg(bot, msgId);
 
16
  const TOKEN_TTL = 900;
17
  const MIN_ALIGN = 4096;
18
  const MB = 1024 * 1024;
19
+ const GET_SIZES = [1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096];
20
+ const FIRST_GET = 1048576;
21
+ const HOT_HEAD = 1048576;
22
  const HOT_TAIL = 1048576;
23
+ const STREAM_WINDOW = 2 * MB;
24
  const BOT_MAX = 24;
25
  const PIPE_MS = 45000;
26
  const PIPE_MAX_MS = 30 * 60 * 1000;
 
716
 
717
  function parseRange(h, size) {
718
  if (!size) return { start: 0, end: 0, partial: false };
719
+ const firstEnd = Math.min(size - 1, HOT_HEAD - 1);
720
+ if (!h) return { start: 0, end: firstEnd, partial: firstEnd < size - 1 };
721
  const m = /^bytes=(\d*)-(\d*)$/.exec(String(h).trim());
722
+ if (!m) return { start: 0, end: firstEnd, partial: firstEnd < size - 1 };
723
  let start;
724
  let end;
725
  if (m[1] === "") {
 
728
  end = size - 1;
729
  } else {
730
  start = Number(m[1]);
731
+ if (m[2] === "") {
732
+ if (start === 0) end = firstEnd;
733
+ else if (start >= Math.max(0, size - HOT_TAIL)) end = size - 1;
734
+ else end = Math.min(size - 1, start + STREAM_WINDOW - 1);
735
+ } else {
736
+ end = Number(m[2]);
737
+ }
738
  }
739
  if (!Number.isFinite(start) || start < 0) start = 0;
740
  if (!Number.isFinite(end) || end >= size) end = size - 1;
 
1051
  res.writeHead(503);
1052
  return res.end();
1053
  }
1054
+ const sizeHint = Number(ticket.s) || 0;
1055
+ const mimeHint = String(ticket.n || "video/mp4");
1056
+ const typeHint = mimeHint.indexOf("/") > 0 ? mimeHint : "video/mp4";
1057
+ if (sizeHint > 0) {
1058
+ const rngHot = parseRange(req.headers.range, sizeHint);
1059
+ const needHot = rngHot.end - rngHot.start + 1;
1060
+ const rangedHot = !!req.headers.range;
1061
+ const headersHot = {
1062
+ "Content-Type": typeHint,
1063
+ "Accept-Ranges": "bytes",
1064
+ "Cache-Control": "private, no-store, no-transform",
1065
+ "Content-Disposition": "inline",
1066
+ "Content-Encoding": "identity",
1067
+ "ETag": '"' + msgId + "-" + sizeHint + "-" + (ticket.h || "x") + '"',
1068
+ "X-Content-Type-Options": "nosniff",
1069
+ "X-Accel-Buffering": "no",
1070
+ "Content-Length": String(needHot)
1071
+ };
1072
+ if (rangedHot) {
1073
+ headersHot["Content-Range"] = "bytes " + rngHot.start + "-" + rngHot.end + "/" + sizeHint;
1074
+ }
1075
+ if (req.method === "HEAD") {
1076
+ res.writeHead(rangedHot ? 206 : 200, headersHot);
1077
+ return res.end();
1078
+ }
1079
+ const hit = hotPrefix(msgId, rngHot.start, rngHot.end);
1080
+ if (hit && hit.length >= needHot) {
1081
+ res.writeHead(rangedHot ? 206 : 200, headersHot);
1082
+ res.end(hit.subarray(0, needHot));
1083
+ return;
1084
+ }
1085
+ }
1086
  let rec;
1087
  try {
1088
  rec = await fileFromMsg(bot, msgId);