Carries 21 template commits: the shared IndexedDB connection/transaction
kernel and the four runtimes moved onto it, the adapter barrel boundary and
the architecture rule that enforces it, the feature adapter contribution
model, `feature-http-binding`, the split of several oversized test files,
and the release identity now derived from the package name.
Product identity is unchanged. The three hard-coded `tech-log-frontend/…`
verifier ids are gone because the template derives them from the package
name and this package is `tech-log-frontend`.
Two decisions worth naming. The platform HTTP executor is the template's
verbatim — it now takes the installed contract so a feature's types flow
through the binding — and TechLog's four gateways, which address operations
by id, reach it through one adapter at the composition root instead of being
rewritten here. And the three derived CI baselines were recomputed from the
merged `config/ci/gates.json` rather than taken from either side: 94 command
definitions, 106 references, 141 artifacts, gate shape `629e5898…`. The
method was checked by first reproducing the pre-merge values from the
pre-merge file.
Verified here: types, lint, architecture, adapter inventory, CI contract,
design system, diagnostics, remediation ledger, browser security and build
all pass; 1789 unit, 152 component, 415 TechLog, 102 integration, 19
reference-feature, 17 recipe and 7 contract tests pass. Five gates stay
red and every one was reproduced at b297d65 first: `check:i18n`,
`check:registries`, `test:runtime-schema`, `verify:documentation` and
`test:sample-removal`.
Full decision record, including what was deliberately not carried and the
five open follow-ups, in `docs/operations/template-sync-2026-09-21.md`.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
296 lines
8.9 KiB
TypeScript
296 lines
8.9 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
declaredContentLength,
|
|
decodeJsonBytes,
|
|
isEffectivelyEmpty,
|
|
isJsonMediaType,
|
|
probeForbiddenBody,
|
|
readBoundedBytes,
|
|
} from "../../src/adapters/http/bounded-body-reader.ts";
|
|
|
|
function responseWithBody(
|
|
body: ReadableStream<Uint8Array> | null,
|
|
contentLength?: string,
|
|
): Response {
|
|
return new Response(body, {
|
|
headers:
|
|
contentLength === undefined ? undefined : { "content-length": contentLength },
|
|
});
|
|
}
|
|
|
|
describe("bounded body reader", () => {
|
|
it.each([
|
|
[null, false],
|
|
["", false],
|
|
[" application/json ; charset=utf-8 ", true],
|
|
["APPLICATION/PROBLEM+JSON", true],
|
|
["text/json", false],
|
|
["application/jsonp", false],
|
|
] as const)("classifies JSON media type %j", (header, expected) => {
|
|
expect(isJsonMediaType(header)).toBe(expected);
|
|
});
|
|
|
|
it.each([
|
|
[undefined, null],
|
|
["0", 0],
|
|
["12", 12],
|
|
["-1", null],
|
|
["NaN", null],
|
|
["Infinity", null],
|
|
] as const)("parses declared content length %s", (header, expected) => {
|
|
expect(declaredContentLength(responseWithBody(null, header))).toBe(expected);
|
|
});
|
|
|
|
it("rejects an oversized declared body and tolerates cancellation failure", async () => {
|
|
const cancel = vi.fn(async () => {
|
|
throw new Error("already settled");
|
|
});
|
|
const response = {
|
|
headers: new Headers({ "content-length": "9" }),
|
|
body: { cancel },
|
|
} as unknown as Response;
|
|
|
|
await expect(readBoundedBytes(response, 8)).resolves.toEqual({
|
|
ok: false,
|
|
code: "RESPONSE_TOO_LARGE",
|
|
});
|
|
expect(cancel).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("returns empty bytes when a successful response has no body", async () => {
|
|
const result = await readBoundedBytes(responseWithBody(null), 8);
|
|
expect(result).toEqual({ ok: true, bytes: new Uint8Array(0) });
|
|
});
|
|
|
|
it("joins chunks without retaining empty chunks", async () => {
|
|
const body = new ReadableStream<Uint8Array>({
|
|
start(controller) {
|
|
controller.enqueue(new Uint8Array(0));
|
|
controller.enqueue(Uint8Array.of(1, 2));
|
|
controller.enqueue(Uint8Array.of(3));
|
|
controller.close();
|
|
},
|
|
});
|
|
|
|
await expect(readBoundedBytes(responseWithBody(body), 3)).resolves.toEqual({
|
|
ok: true,
|
|
bytes: Uint8Array.of(1, 2, 3),
|
|
});
|
|
});
|
|
|
|
it("cancels streaming input as soon as the accumulated limit is exceeded", async () => {
|
|
const cancel = vi.fn().mockRejectedValue(new Error("cancel failed"));
|
|
const reader = {
|
|
read: vi
|
|
.fn()
|
|
.mockResolvedValueOnce({ done: false, value: Uint8Array.of(1, 2) })
|
|
.mockResolvedValueOnce({ done: false, value: Uint8Array.of(3, 4) }),
|
|
cancel,
|
|
releaseLock: vi.fn(),
|
|
};
|
|
const response = {
|
|
headers: new Headers(),
|
|
body: { getReader: () => reader },
|
|
} as unknown as Response;
|
|
|
|
await expect(readBoundedBytes(response, 3)).resolves.toEqual({
|
|
ok: false,
|
|
code: "RESPONSE_TOO_LARGE",
|
|
});
|
|
expect(cancel).toHaveBeenCalledOnce();
|
|
expect(reader.releaseLock).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("maps reader failure and cancellation failure to a stream failure", async () => {
|
|
const reader = {
|
|
read: vi.fn().mockRejectedValue(new Error("stream failed")),
|
|
cancel: vi.fn().mockRejectedValue(new Error("cancel failed")),
|
|
releaseLock: vi.fn(() => {
|
|
throw new Error("already released");
|
|
}),
|
|
};
|
|
const response = {
|
|
headers: new Headers(),
|
|
body: { getReader: () => reader },
|
|
} as unknown as Response;
|
|
|
|
await expect(readBoundedBytes(response, 3)).resolves.toEqual({
|
|
ok: false,
|
|
code: "RESPONSE_STREAM_FAILURE",
|
|
});
|
|
});
|
|
|
|
it("abandons and cancels a bounded read when its lifetime aborts", async () => {
|
|
const controller = new AbortController();
|
|
const reader = {
|
|
read: vi.fn(() => new Promise<never>(() => {})),
|
|
cancel: vi.fn().mockRejectedValue(new Error("abandoned cancel failed")),
|
|
releaseLock: vi.fn(),
|
|
};
|
|
const response = {
|
|
headers: new Headers(),
|
|
body: { getReader: () => reader },
|
|
} as unknown as Response;
|
|
|
|
const outcome = readBoundedBytes(response, 3, controller.signal);
|
|
controller.abort();
|
|
|
|
await expect(outcome).resolves.toEqual({
|
|
ok: false,
|
|
code: "RESPONSE_STREAM_FAILURE",
|
|
});
|
|
expect(reader.cancel).toHaveBeenCalledOnce();
|
|
expect(reader.releaseLock).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("detects a forbidden body from declared length without reading it", async () => {
|
|
const cancel = vi.fn();
|
|
const response = {
|
|
headers: new Headers({ "content-length": "1" }),
|
|
body: { cancel },
|
|
} as unknown as Response;
|
|
|
|
await expect(probeForbiddenBody(response)).resolves.toEqual({
|
|
ok: true,
|
|
present: true,
|
|
});
|
|
expect(cancel).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("accepts an absent, completed, or zero-byte forbidden body", async () => {
|
|
await expect(probeForbiddenBody(responseWithBody(null))).resolves.toEqual({
|
|
ok: true,
|
|
present: false,
|
|
});
|
|
|
|
for (const next of [
|
|
{ done: true, value: undefined },
|
|
{ done: false, value: new Uint8Array(0) },
|
|
]) {
|
|
const reader = {
|
|
read: vi.fn().mockResolvedValue(next),
|
|
cancel: vi.fn(),
|
|
releaseLock: vi.fn(),
|
|
};
|
|
const response = {
|
|
headers: new Headers(),
|
|
body: { getReader: () => reader },
|
|
} as unknown as Response;
|
|
await expect(probeForbiddenBody(response)).resolves.toEqual({
|
|
ok: true,
|
|
present: false,
|
|
});
|
|
expect(reader.cancel).not.toHaveBeenCalled();
|
|
}
|
|
});
|
|
|
|
it("probes only one present byte and cancels the remaining body", async () => {
|
|
const reader = {
|
|
read: vi.fn().mockResolvedValue({ done: false, value: Uint8Array.of(1) }),
|
|
cancel: vi.fn().mockRejectedValue(new Error("cancel failed")),
|
|
releaseLock: vi.fn(),
|
|
};
|
|
const response = {
|
|
headers: new Headers(),
|
|
body: { getReader: () => reader },
|
|
} as unknown as Response;
|
|
|
|
await expect(probeForbiddenBody(response)).resolves.toEqual({
|
|
ok: true,
|
|
present: true,
|
|
});
|
|
expect(reader.read).toHaveBeenCalledOnce();
|
|
expect(reader.cancel).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("maps a forbidden-body probe error even when cleanup also fails", async () => {
|
|
const reader = {
|
|
read: vi.fn().mockRejectedValue(new Error("probe failed")),
|
|
cancel: vi.fn().mockRejectedValue(new Error("cancel failed")),
|
|
releaseLock: vi.fn(() => {
|
|
throw new Error("released");
|
|
}),
|
|
};
|
|
const response = {
|
|
headers: new Headers(),
|
|
body: { getReader: () => reader },
|
|
} as unknown as Response;
|
|
|
|
await expect(probeForbiddenBody(response)).resolves.toEqual({
|
|
ok: false,
|
|
code: "RESPONSE_STREAM_FAILURE",
|
|
});
|
|
});
|
|
|
|
|
|
it("abandons a pending bounded read when the operation is aborted", async () => {
|
|
const controller = new AbortController();
|
|
const reader = {
|
|
read: vi.fn(() => new Promise<never>(() => {})),
|
|
cancel: vi.fn().mockRejectedValue(new Error("cancel ignored")),
|
|
releaseLock: vi.fn(),
|
|
};
|
|
const response = {
|
|
headers: new Headers(),
|
|
body: { getReader: () => reader },
|
|
} as unknown as Response;
|
|
|
|
const pending = readBoundedBytes(response, 8, controller.signal);
|
|
controller.abort();
|
|
|
|
await expect(pending).resolves.toEqual({
|
|
ok: false,
|
|
code: "RESPONSE_STREAM_FAILURE",
|
|
});
|
|
expect(reader.cancel).toHaveBeenCalledOnce();
|
|
expect(reader.releaseLock).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("abandons a pending forbidden-body probe when the operation is aborted", async () => {
|
|
const controller = new AbortController();
|
|
const reader = {
|
|
read: vi.fn(() => new Promise<never>(() => {})),
|
|
cancel: vi.fn().mockRejectedValue(new Error("cancel ignored")),
|
|
releaseLock: vi.fn(),
|
|
};
|
|
const response = {
|
|
headers: new Headers(),
|
|
body: { getReader: () => reader },
|
|
} as unknown as Response;
|
|
|
|
const pending = probeForbiddenBody(response, controller.signal);
|
|
controller.abort();
|
|
|
|
await expect(pending).resolves.toEqual({
|
|
ok: false,
|
|
code: "RESPONSE_STREAM_FAILURE",
|
|
});
|
|
expect(reader.cancel).toHaveBeenCalledOnce();
|
|
expect(reader.releaseLock).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("decodes valid JSON and distinguishes UTF-8 from JSON failures", () => {
|
|
expect(decodeJsonBytes(new TextEncoder().encode('{"ok":true}'))).toEqual({
|
|
ok: true,
|
|
value: { ok: true },
|
|
});
|
|
expect(decodeJsonBytes(Uint8Array.of(0xc3, 0x28))).toEqual({
|
|
ok: false,
|
|
code: "UTF8_INVALID",
|
|
});
|
|
expect(decodeJsonBytes(new TextEncoder().encode("{"))).toEqual({
|
|
ok: false,
|
|
code: "JSON_INVALID",
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
[new Uint8Array(0), true],
|
|
[Uint8Array.of(0x20, 0x09, 0x0a, 0x0d), true],
|
|
[Uint8Array.of(0x20, 0x00), false],
|
|
])("classifies effective emptiness %#", (bytes, expected) => {
|
|
expect(isEffectivelyEmpty(bytes)).toBe(expected);
|
|
});
|
|
});
|