Files
gerbeur/api/lib/serve-file.ts
khannurien 0d4e344d77 v3: comments say what the code can't — the documentation pass wanted JSDoc on every export and every field of an exported interface, which filled the code with restatements ("The dump.", "Whether it is loading.", "The props of X.", "Renders the X.") and module docs that told a reader nothing ("The hot and new feeds, and the props every main feed of the index takes."); some were wrong as well as vague. Every comment in api/, shared/ and src/ is rewritten under one rule, now stated in the README's "Comments" section: every module and export still has a JSDoc, and it gives context (who uses it and where), usage (what a caller must know: a precondition, what it leaves to the caller, a side effect) or its place in the chain (where its input comes from, where its output goes); a field, parameter, private function or local gets one only when it adds to its parent's, and context about a whole type goes on the type; a type's doc is phrased from the value's side ("Returned to Y by X when Z", "Passed to Y by X"); full sentences, as short as they can be, no label-colon-list; // goes wherever the code isn't obvious and says why, and the section banners and narration go. Every claim was checked against the code, which turned up some forty comments that were wrong: DumpCard's claimed it was only used by the hot and new feeds, the WebSocket welcome claimed to be a socket's first message (presence comes first), UpdateUserRequest said null removes a description (it keeps it), deletedDumpIds leaves out that it also holds dumps made private, THEME_COLOR claimed to mirror index.html's, and the like. The routers' JSDoc now says where they are mounted and what becomes of a thrown APIException. doc-lint.ts lets a field without a doc through; a module or export without one still fails. No code changes; 204 backend and 628 frontend tests pass, and doc:lint, deno check, deno lint and tsc are clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 14:31:14 +00:00

169 lines
4.7 KiB
TypeScript

/**
* Sends files from disk with ETags and byte ranges, streamed a chunk at a
* time. The files and thumbnails routes call it, and lib/upload.ts for the
* other uploads.
*
* @module
*/
import type { Context } from "@oak/oak";
const CHUNK_SIZE = 64 * 1024;
/**
* Streams `length` bytes of an open file, from wherever its cursor stands,
* and closes it once done or cancelled. The response never holds more than a
* chunk of the file in memory — a seek in a 50 MB track reads what it plays.
*/
function fileStream(
file: Deno.FsFile,
length: number,
): ReadableStream<Uint8Array> {
let remaining = length;
let closed = false;
const close = () => {
if (closed) return;
closed = true;
file.close();
};
return new ReadableStream({
async pull(controller) {
if (remaining <= 0) {
close();
controller.close();
return;
}
const buf = new Uint8Array(Math.min(CHUNK_SIZE, remaining));
let n: number | null;
try {
n = await file.read(buf);
} catch (err) {
close();
controller.error(err);
return;
}
if (n === null) {
close();
controller.close();
return;
}
remaining -= n;
controller.enqueue(buf.subarray(0, n));
if (remaining <= 0) {
close();
controller.close();
}
},
cancel() {
close();
},
});
}
/** Checks whether an If-None-Match header names `etag` (weak comparison). */
function matchesEtag(header: string | null, etag: string): boolean {
if (!header) return false;
if (header.trim() === "*") return true;
const strip = (tag: string) => tag.trim().replace(/^W\//, "");
return header.split(",").some((tag) => strip(tag) === strip(etag));
}
/**
* Passed to {@link serveFile} by its caller, which knows the file's type and
* how long it may be cached.
*/
export interface ServeFileOptions {
mime: string;
cacheControl: string;
/**
* Answers a single `Range: bytes=…` with a 206. The files route sets it, so
* that audio and video can seek.
*/
ranges?: boolean;
}
/**
* Sends a file from disk with an ETag, answering 304 when the client already
* has it, and single byte ranges when `ranges` is set. It returns false, having
* sent nothing, when the file doesn't exist, and leaves the 404 to the caller.
*/
export async function serveFile(
ctx: Context,
path: string,
{ mime, cacheControl, ranges = false }: ServeFileOptions,
): Promise<boolean> {
let file: Deno.FsFile;
let stat: Deno.FileInfo;
try {
file = await Deno.open(path, { read: true });
stat = await file.stat();
} catch {
return false;
}
const total = stat.size;
const mtime = stat.mtime?.getTime() ?? 0;
const etag = `W/"${total.toString(36)}-${mtime.toString(36)}"`;
const headers = ctx.response.headers;
headers.set("Content-Type", mime);
headers.set("Cache-Control", cacheControl);
headers.set("ETag", etag);
if (stat.mtime) headers.set("Last-Modified", stat.mtime.toUTCString());
if (ranges) headers.set("Accept-Ranges", "bytes");
if (matchesEtag(ctx.request.headers.get("If-None-Match"), etag)) {
file.close();
ctx.response.status = 304;
return true;
}
// Only a single byte range is served as one. Anything else — several
// ranges, another unit — is a header RFC 9110 lets us ignore, so the whole
// file goes out with a 200 rather than a 416 for ranges that were fine.
const match = ranges
? ctx.request.headers.get("Range")?.match(/^bytes=(\d*)-(\d*)$/)
: null;
if (!match) {
// Explicit: a caller may have tried another file first and left a 404.
ctx.response.status = 200;
headers.set("Content-Length", String(total));
ctx.response.body = fileStream(file, total);
return true;
}
// "bytes=a-b", "bytes=a-" (to the end) or "bytes=-n" (the last n bytes).
let start: number;
let end: number;
if (match[1]) {
start = parseInt(match[1], 10);
end = match[2] ? Math.min(parseInt(match[2], 10), total - 1) : total - 1;
} else {
const suffix = parseInt(match[2], 10);
start = Math.max(total - suffix, 0);
end = total - 1;
// "bytes=-" and "bytes=-0" name no bytes at all.
if (!(suffix > 0)) start = total;
}
if (start > end || start >= total) {
file.close();
ctx.response.status = 416;
headers.set("Content-Range", `bytes */${total}`);
return true;
}
const length = end - start + 1;
try {
await file.seek(start, Deno.SeekMode.Start);
} catch (err) {
file.close();
throw err;
}
ctx.response.status = 206;
headers.set("Content-Range", `bytes ${start}-${end}/${total}`);
headers.set("Content-Length", String(length));
ctx.response.body = fileStream(file, length);
return true;
}