mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
release: v1.32.0 — hardening and scalability (#580)
* release: prepare v1.32.0 * test: stabilize water capture re-entry
This commit is contained in:
1 parent
b01822be48
commit
62eb431d66
372 files changed
+25178
-8561
No files matched your search
@@ -1,8 +1,13 @@
|
||||
import { Buffer } from "node:buffer";
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { detectOcrMimeType, OCR_MAX_BYTES } from "../ocr-upload";
|
||||
import {
|
||||
BodyTooLargeError,
|
||||
detectOcrMimeType,
|
||||
OCR_MAX_BYTES,
|
||||
readBoundedBody,
|
||||
} from "../ocr-upload";
|
||||
|
||||
/** Pad a magic-byte header out to the 12-byte minimum the sniffer needs. */
|
||||
function withMagic(bytes: number[]): Buffer {
|
||||
@@ -47,3 +52,114 @@ describe("detectOcrMimeType", () => {
|
||||
expect(OCR_MAX_BYTES).toBe(12 * 1024 * 1024);
|
||||
});
|
||||
});
|
||||
|
||||
describe("readBoundedBody", () => {
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("cancels immediately when a chunk crosses the byte cap", async () => {
|
||||
let pulls = 0;
|
||||
const cancel = vi.fn();
|
||||
const stream = new ReadableStream<Uint8Array>(
|
||||
{
|
||||
pull(controller) {
|
||||
pulls += 1;
|
||||
if (pulls === 1) {
|
||||
controller.enqueue(new Uint8Array(5));
|
||||
return;
|
||||
}
|
||||
controller.error(new Error("reader continued after overflow"));
|
||||
},
|
||||
cancel,
|
||||
},
|
||||
{ highWaterMark: 0 },
|
||||
);
|
||||
|
||||
await expect(readBoundedBody(stream, 4)).rejects.toBeInstanceOf(
|
||||
BodyTooLargeError,
|
||||
);
|
||||
expect(cancel).toHaveBeenCalledOnce();
|
||||
expect(pulls).toBe(1);
|
||||
});
|
||||
|
||||
it("cancels a non-ending read when its AbortSignal fires", async () => {
|
||||
let controller: ReadableStreamDefaultController<Uint8Array>;
|
||||
const cancel = vi.fn();
|
||||
const stream = new ReadableStream<Uint8Array>({
|
||||
start(value) {
|
||||
controller = value;
|
||||
},
|
||||
cancel,
|
||||
});
|
||||
const abort = new AbortController();
|
||||
|
||||
const result = readBoundedBody(stream, 16, { signal: abort.signal });
|
||||
abort.abort();
|
||||
try {
|
||||
controller!.close();
|
||||
} catch {
|
||||
// A correctly cancelled stream is already closed.
|
||||
}
|
||||
|
||||
await expect(result).rejects.toMatchObject({ name: "AbortError" });
|
||||
expect(cancel).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("cancels a slow read at an absolute deadline", async () => {
|
||||
vi.useFakeTimers();
|
||||
const cancel = vi.fn();
|
||||
let controller: ReadableStreamDefaultController<Uint8Array>;
|
||||
const stream = new ReadableStream<Uint8Array>({
|
||||
start(value) {
|
||||
controller = value;
|
||||
setTimeout(() => {
|
||||
try {
|
||||
controller.close();
|
||||
} catch {
|
||||
// A deadline cancellation closes it first.
|
||||
}
|
||||
}, 200);
|
||||
},
|
||||
cancel,
|
||||
});
|
||||
|
||||
const result = readBoundedBody(stream, 16, {
|
||||
deadline: Date.now() + 100,
|
||||
});
|
||||
const assertion = expect(result).rejects.toMatchObject({
|
||||
name: "AbortError",
|
||||
});
|
||||
await vi.advanceTimersByTimeAsync(200);
|
||||
await assertion;
|
||||
expect(cancel).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("combines every independently aligned chunk", async () => {
|
||||
const first = new Uint8Array([1, 2]);
|
||||
const second = new Uint8Array([3, 4]);
|
||||
const stream = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(first);
|
||||
controller.enqueue(second);
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
|
||||
await expect(readBoundedBody(stream, 4)).resolves.toEqual(
|
||||
new Uint8Array([1, 2, 3, 4]),
|
||||
);
|
||||
});
|
||||
|
||||
it("reuses an aligned single chunk instead of copying the whole body", async () => {
|
||||
const chunk = new Uint8Array([1, 2, 3, 4]);
|
||||
const stream = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(chunk);
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
|
||||
await expect(readBoundedBody(stream, 4)).resolves.toBe(chunk);
|
||||
});
|
||||
});
|
||||
+71
-12
@@ -28,42 +28,101 @@ export class BodyTooLargeError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export interface BoundedBodyReadOptions {
|
||||
/** Cancels the body read when the request itself is aborted. */
|
||||
signal?: AbortSignal;
|
||||
/** Absolute Unix timestamp in milliseconds after which the read is cancelled. */
|
||||
deadline?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a request body stream into a single buffer while counting bytes,
|
||||
* throwing `BodyTooLargeError` the moment the running total passes `maxBytes`.
|
||||
* Reads the body exactly once (no clone, no tee) so a cap trip frees the
|
||||
* allocation immediately. Keeps draining the stream to its natural close
|
||||
* rather than cancelling mid-transfer (an abrupt cancel races the undici
|
||||
* producer and surfaces as an unhandled rejection). Verbatim port of the
|
||||
* avatar route's reader.
|
||||
* Read a request body stream into a single buffer while counting bytes.
|
||||
*
|
||||
* Overflow, request abort, and deadline expiry cancel the reader immediately,
|
||||
* so a producer cannot keep the request alive after the result is known. The
|
||||
* body is read exactly once (no clone or tee). An aligned single chunk is
|
||||
* returned directly; multi-chunk bodies incur only the unavoidable final
|
||||
* contiguous allocation.
|
||||
*/
|
||||
export async function readBoundedBody(
|
||||
body: ReadableStream<Uint8Array> | null,
|
||||
maxBytes: number,
|
||||
options: BoundedBodyReadOptions = {},
|
||||
): Promise<Uint8Array<ArrayBuffer>> {
|
||||
if (!body) return new Uint8Array(0);
|
||||
const reader = body.getReader();
|
||||
const chunks: Uint8Array[] = [];
|
||||
let retained = 0;
|
||||
let overflow = false;
|
||||
let aborted = false;
|
||||
let abortReason: unknown;
|
||||
|
||||
const cancel = (reason: unknown) => {
|
||||
if (aborted) return;
|
||||
aborted = true;
|
||||
abortReason = reason;
|
||||
void reader.cancel(reason).catch(() => {
|
||||
// The read result is already settled; cancellation failure is immaterial.
|
||||
});
|
||||
};
|
||||
const abortFromSignal = () => {
|
||||
cancel(
|
||||
options.signal?.reason ??
|
||||
new DOMException("Request body read aborted", "AbortError"),
|
||||
);
|
||||
};
|
||||
|
||||
options.signal?.addEventListener("abort", abortFromSignal, { once: true });
|
||||
if (options.signal?.aborted) abortFromSignal();
|
||||
|
||||
const deadlineDelay =
|
||||
options.deadline === undefined
|
||||
? undefined
|
||||
: Math.max(0, options.deadline - Date.now());
|
||||
const deadlineTimer =
|
||||
deadlineDelay === undefined
|
||||
? undefined
|
||||
: setTimeout(() => {
|
||||
cancel(
|
||||
new DOMException(
|
||||
"Request body read deadline exceeded",
|
||||
"AbortError",
|
||||
),
|
||||
);
|
||||
}, deadlineDelay);
|
||||
|
||||
try {
|
||||
for (;;) {
|
||||
if (aborted) throw abortReason;
|
||||
const { done, value } = await reader.read();
|
||||
if (aborted) throw abortReason;
|
||||
if (done) break;
|
||||
if (overflow) continue;
|
||||
if (retained + value.byteLength > maxBytes) {
|
||||
chunks.length = 0;
|
||||
retained = 0;
|
||||
overflow = true;
|
||||
continue;
|
||||
const error = new BodyTooLargeError();
|
||||
cancel(error);
|
||||
throw error;
|
||||
}
|
||||
chunks.push(value);
|
||||
retained += value.byteLength;
|
||||
}
|
||||
} finally {
|
||||
clearTimeout(deadlineTimer);
|
||||
options.signal?.removeEventListener("abort", abortFromSignal);
|
||||
reader.releaseLock();
|
||||
}
|
||||
if (overflow) throw new BodyTooLargeError();
|
||||
|
||||
const onlyChunk = chunks[0];
|
||||
if (
|
||||
chunks.length === 1 &&
|
||||
onlyChunk !== undefined &&
|
||||
onlyChunk.buffer instanceof ArrayBuffer &&
|
||||
onlyChunk.byteOffset === 0 &&
|
||||
onlyChunk.byteLength === onlyChunk.buffer.byteLength
|
||||
) {
|
||||
return onlyChunk as Uint8Array<ArrayBuffer>;
|
||||
}
|
||||
|
||||
const out = new Uint8Array(retained);
|
||||
let offset = 0;
|
||||
for (const chunk of chunks) {
|
||||
|
||||
Reference in new issue
Block a user