mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-10-30 17:27:09 +00:00
test(engine): count what the mixer read from a cloud file, not what the server buffered (#6315)
This commit is contained in:
@@ -12,15 +12,16 @@ import { processCompositionAudio } from "./audioMixer.js";
|
||||
const HAS_FFMPEG = spawnSync(getFfmpegBinary(), ["-version"], { encoding: "utf-8" }).status === 0;
|
||||
const BYTES_PER_SECOND = 48_000 * 2 * 2;
|
||||
|
||||
// Writes the body in pieces, counting the bytes served.
|
||||
// Writes body[start, end) in pieces, counting the bytes served.
|
||||
async function streamBody(
|
||||
res: ServerResponse,
|
||||
body: Buffer,
|
||||
start: number,
|
||||
end: number,
|
||||
served?: { bytes: number },
|
||||
): Promise<void> {
|
||||
for (let at = start; at < body.length && !res.destroyed; at += 65_536) {
|
||||
const piece = body.subarray(at, at + 65_536);
|
||||
for (let at = start; at < end && !res.destroyed; at += 65_536) {
|
||||
const piece = body.subarray(at, Math.min(at + 65_536, end));
|
||||
if (served) served.bytes += piece.length;
|
||||
if (!res.write(piece)) await once(res, "drain").catch(() => undefined);
|
||||
}
|
||||
@@ -29,7 +30,10 @@ async function streamBody(
|
||||
|
||||
// A range server like S3's (ffmpeg asks for `bytes=<start>-`). With `cut`, the first read starting
|
||||
// at `cut.from` or later (past the probes' header reads) stops at `cut.at`, and every later request
|
||||
// is refused, as for a link that just expired.
|
||||
// is refused, as for a link that just expired. With `served`, each answer is capped at SERVED_RANGE_BYTES:
|
||||
// an open-ended answer is written into the kernel's socket buffers long before the client closes it, so
|
||||
// the count would measure buffering, which varies with load, not what the client read.
|
||||
const SERVED_RANGE_BYTES = 1_048_576;
|
||||
function serve(
|
||||
body: Buffer,
|
||||
cut?: { from: number; at: number },
|
||||
@@ -39,14 +43,15 @@ function serve(
|
||||
const server = createServer(async (req, res) => {
|
||||
if (refusing) return void res.writeHead(403).end("Request has expired");
|
||||
const start = Number(/bytes=(\d+)-/.exec(req.headers.range ?? "")?.[1] ?? 0);
|
||||
const end = served ? Math.min(body.length, start + SERVED_RANGE_BYTES) : body.length;
|
||||
res.writeHead(206, {
|
||||
"Accept-Ranges": "bytes",
|
||||
"Content-Length": body.length - start,
|
||||
"Content-Range": `bytes ${start}-${body.length - 1}/${body.length}`,
|
||||
"Content-Length": end - start,
|
||||
"Content-Range": `bytes ${start}-${end - 1}/${body.length}`,
|
||||
"Content-Type": "audio/wav",
|
||||
});
|
||||
const cutHere = cut !== undefined && start >= cut.from && start < cut.at;
|
||||
if (!cutHere) return streamBody(res, body, start, served);
|
||||
if (!cutHere) return streamBody(res, body, start, end, served);
|
||||
refusing = true;
|
||||
res.write(body.subarray(start, cut.at), () => res.destroy());
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user