mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 17:44:27 +00:00
* perf(browser): fetch source maps with bounded lookahead and in-order admission fetchWebSourceMaps awaited each map's full fetch-decode cycle before starting the next, so bundles with dozens of external maps paid serial round trips. Responses now start with a lookahead of 4 (approved origins only, never past the operation deadline), while every budget (URL bytes, retained bytes, decoded records), deadline check and decode still happens strictly in request order inside fetchOne, so item order, inclusion under output limits and limitations are unchanged. A started prefetch that is never admitted is cancelled in teardown, and abort/deadline paths route through fetchOne so retained-cleanup ownership (SourceMapPendingFetch) is preserved. Closes #14109. Verified: WebSourceMapFetcher tests 31 passed (30 existing unchanged plus a new out-of-order-completion proof asserting overlap >1 and request-order admission), check:fast. * fix(browser): abort and bind cleanup for unadmitted source-map prefetches Teardown now aborts the operation controller before removing deadline/caller controls, and every started-but-unadmitted prefetch is settled and bound to the returned result: pending-fetch cleanup becomes the retained failure (or a secondary failure on an existing error), and unread prefetched responses are cancelled with cleanup failures tracked. The lookahead test now gates six requests to prove the four-fetch cap. * fix(browser): pipeline source-map fetches without speculative or reordered requests Start each request's fetch after its budget reservation and deadline checks pass, and start the next fetch only when the current body is read and the next failure reservation still fits. Redirect chains and host-observable request order stay sequential; the next fetch overlaps only the current decode. Sibling binding now ignores teardown-abort artifacts and binds only genuinely incomplete cleanup. Boundary suites (49 tests) and unit suite (31) pass unchanged in their budget and redirect assertions. * fix(browser): retain sibling source-map cleanup on teardown Cancellation that stops admission at the first map left later lookahead fetches owning unread responses. Teardown skipped stored failures and recorded the rest only as secondary notes, so the returned error could report cleanup complete while those resources were still owned. Await every started prefetch and merge its live cleanup through the existing cleanup-composition helpers, preserving remaining resources. * fix(browser): keep caller cancellation through source-map cleanup Unused-prefetch teardown chose success before its cleanup wait and after the caller abort listener was removed. Recheck the caller signal after that wait so a cancel during a successful release stays a cancellation, while an existing failure keeps its cleanup owners. Cover unused-prefetch release, mixed redirect and prefetch cleanup, and cancellation during release with structured cleanup facts. --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
1385 lines
43 KiB
TypeScript
1385 lines
43 KiB
TypeScript
import { createServer } from "node:http";
|
|
import { once } from "node:events";
|
|
|
|
import { expect, it, vi } from "vitest";
|
|
|
|
import { fetchWebSourceMaps } from "./WebSourceMapFetcher.js";
|
|
import { analyzeWebBundleInputSchema } from "../domain/webBundleAnalysis.js";
|
|
import {
|
|
AnalysisError,
|
|
primaryAnalysisError,
|
|
} from "../domain/analysisErrorBase.js";
|
|
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
|
import { fetchWebSourceMapsOk } from "../../tests/fixtures/fetchWebSourceMapsOk.js";
|
|
import {
|
|
phaseArrival,
|
|
phaseGate,
|
|
} from "../../tests/support/lifecycleHarness.js";
|
|
import { testWaitDeadline } from "../../tests/support/testWait.js";
|
|
|
|
const input = analyzeWebBundleInputSchema.parse({
|
|
cdp_endpoint: "http://127.0.0.1:9222",
|
|
allowed_origins: ["http://127.0.0.1:9222"],
|
|
target_id: "page-1",
|
|
fetch_source_maps: true,
|
|
});
|
|
const request = {
|
|
scriptKey: `scr_${"1".repeat(64)}`,
|
|
declaredUrl: "https://app.example.test/app.js.map",
|
|
fetchUrl: "https://app.example.test/app.js.map",
|
|
};
|
|
|
|
const approvedInput = {
|
|
...input,
|
|
allowed_origins: [new URL(request.fetchUrl).origin],
|
|
};
|
|
|
|
it("pipelines the next fetch against decode but admits in request order", async () => {
|
|
const mapText = (name: string) =>
|
|
JSON.stringify({
|
|
version: 3,
|
|
sources: [`${name}.js`],
|
|
names: [],
|
|
mappings: "AAAA",
|
|
});
|
|
let active = 0;
|
|
let maxActive = 0;
|
|
const started: number[] = [];
|
|
let releaseFirst!: () => void;
|
|
const firstGate = new Promise<void>((resolve) => {
|
|
releaseFirst = resolve;
|
|
});
|
|
const requests = [0, 1, 2, 3, 4, 5].map((index) => ({
|
|
...request,
|
|
scriptKey: `scr_${String(index + 1).repeat(64)}`,
|
|
declaredUrl: `https://app.example.test/map${index}.js.map`,
|
|
fetchUrl: `https://app.example.test/map${index}.js.map`,
|
|
}));
|
|
const pending = fetchWebSourceMaps(requests, approvedInput, undefined, {
|
|
fetch: async (url) => {
|
|
const index = Number(/map(\d)/.exec(String(url))?.[1] ?? 0);
|
|
started.push(index);
|
|
active += 1;
|
|
maxActive = Math.max(maxActive, active);
|
|
// Later requests finish first; admission must still be in request order.
|
|
if (index === 0) await firstGate;
|
|
else
|
|
await new Promise((resolve) =>
|
|
setTimeout(resolve, Math.max(0, 2 - index) * 5),
|
|
);
|
|
active -= 1;
|
|
return new Response(mapText(`map${index}`), { status: 200 });
|
|
},
|
|
});
|
|
// No speculative fetch starts while map0 is still being fetched.
|
|
await expect.poll(() => started.length).toBe(1);
|
|
expect(started).toEqual([0]);
|
|
releaseFirst();
|
|
const result = await pending;
|
|
if (!result.ok) throw result.error;
|
|
// Host fetches stay sequential (one chain at a time, in request order);
|
|
// the overlap is the next fetch against the current decode.
|
|
expect(maxActive).toBe(1);
|
|
expect(started).toEqual([0, 1, 2, 3, 4, 5]);
|
|
expect(result.value.items.map((item) => item.status)).toEqual([
|
|
"included",
|
|
"included",
|
|
"included",
|
|
"included",
|
|
"included",
|
|
"included",
|
|
]);
|
|
expect(
|
|
result.value.items.map((item) =>
|
|
item.status === "included"
|
|
? item.original_sources.map(({ source }) => source)
|
|
: [],
|
|
),
|
|
).toEqual([
|
|
["https://app.example.test/map0.js"],
|
|
["https://app.example.test/map1.js"],
|
|
["https://app.example.test/map2.js"],
|
|
["https://app.example.test/map3.js"],
|
|
["https://app.example.test/map4.js"],
|
|
["https://app.example.test/map5.js"],
|
|
]);
|
|
});
|
|
|
|
const nestedCauses = (error: unknown): unknown[] => {
|
|
if (error instanceof AggregateError)
|
|
return [error, ...error.errors.flatMap(nestedCauses)];
|
|
if (!(error instanceof AnalysisError)) return [error];
|
|
return [
|
|
error.cause,
|
|
...nestedCauses(error.cause),
|
|
...error.secondaryFailures.flatMap((failure) => nestedCauses(failure)),
|
|
];
|
|
};
|
|
|
|
const lookaheadRequests = [0, 1].map((index) => ({
|
|
...request,
|
|
scriptKey: `scr_${String(index + 1).repeat(64)}`,
|
|
declaredUrl: `https://app.example.test/map${index}.js.map`,
|
|
fetchUrl: `https://app.example.test/map${index}.js.map`,
|
|
}));
|
|
|
|
const smallMap = JSON.stringify({
|
|
version: 3,
|
|
sources: ["map0.js"],
|
|
names: [],
|
|
mappings: "AAAA",
|
|
});
|
|
|
|
it("releases an unused prefetch when cancellation stops admission at the first map", async () => {
|
|
const controller = new AbortController();
|
|
const reason = new Error("caller cancelled during first map");
|
|
const requests = lookaheadRequests;
|
|
const result = await fetchWebSourceMaps(
|
|
requests,
|
|
approvedInput,
|
|
controller.signal,
|
|
{
|
|
fetch: async (url) => {
|
|
const index = Number(/map(\d)/.exec(String(url))?.[1] ?? 0);
|
|
if (index === 1) controller.abort(reason);
|
|
return new Response(index === 0 ? smallMap : "", { status: 200 });
|
|
},
|
|
},
|
|
);
|
|
if (result.ok) throw new Error("Expected cancellation");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
cleanupIncomplete: false,
|
|
cleanupResources: [],
|
|
});
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "cancelled",
|
|
category: "cancelled",
|
|
details: {
|
|
cleanup: "complete",
|
|
partial_observation: {
|
|
kind: "web-source-map-fetch",
|
|
source_maps: { requested: 2, processed: 0 },
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("keeps an unused prefetch owned until its cancellation releases", async () => {
|
|
const controller = new AbortController();
|
|
const reason = new Error("caller cancelled during first map");
|
|
const cleanup = phaseGate();
|
|
const requests = lookaheadRequests;
|
|
const siblingUrl = requests[1]!.fetchUrl;
|
|
const pending = fetchWebSourceMaps(
|
|
requests,
|
|
approvedInput,
|
|
controller.signal,
|
|
{
|
|
fetch: async (url) => {
|
|
const index = Number(/map(\d)/.exec(String(url))?.[1] ?? 0);
|
|
if (index === 0) return new Response(smallMap, { status: 200 });
|
|
controller.abort(reason);
|
|
return new Response(
|
|
new ReadableStream<Uint8Array>({
|
|
cancel: () => cleanup.hold(),
|
|
}),
|
|
{ status: 200 },
|
|
);
|
|
},
|
|
},
|
|
);
|
|
try {
|
|
const result = await pending;
|
|
if (result.ok) throw new Error("Expected cancellation");
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual([siblingUrl]);
|
|
expect(primaryAnalysisError(result.error).cause).toBe(reason);
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
category: "cancelled",
|
|
details: {
|
|
cleanup: "incomplete",
|
|
resources: [siblingUrl],
|
|
},
|
|
});
|
|
cleanup.release();
|
|
await expect.poll(() => result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
expect(primaryAnalysisError(result.error).cause).toBe(reason);
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "cancelled",
|
|
category: "cancelled",
|
|
details: { cleanup: "complete" },
|
|
});
|
|
} finally {
|
|
cleanup.release();
|
|
await pending;
|
|
}
|
|
});
|
|
|
|
it("keeps an included map when an unadmitted sibling cleanup fails", async () => {
|
|
const physical = new Error("sibling response cancellation failed");
|
|
const requests = lookaheadRequests;
|
|
const siblingUrl = requests[1]!.fetchUrl;
|
|
// Large enough to include this map and to start the next fetch, and too
|
|
// small to admit that fetch once the included text is charged.
|
|
const fatMap = JSON.stringify({
|
|
version: 3,
|
|
sources: ["map0.js"],
|
|
sourcesContent: ["x".repeat(15000)],
|
|
names: [],
|
|
mappings: "AAAA",
|
|
});
|
|
const result = await fetchWebSourceMaps(
|
|
requests,
|
|
{ ...approvedInput, source_map_limits: { output_bytes: 32400 } },
|
|
undefined,
|
|
{
|
|
fetch: async (url) => {
|
|
const index = Number(/map(\d)/.exec(String(url))?.[1] ?? 0);
|
|
if (index === 0) return new Response(fatMap, { status: 200 });
|
|
return new Response(
|
|
new ReadableStream<Uint8Array>({
|
|
cancel() {
|
|
throw physical;
|
|
},
|
|
}),
|
|
{ status: 200 },
|
|
);
|
|
},
|
|
},
|
|
);
|
|
if (result.ok) throw new Error("Expected sibling cleanup to stay owned");
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual([siblingUrl]);
|
|
expect(nestedCauses(result.error)).toContain(physical);
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
cleanup: "incomplete",
|
|
resources: [siblingUrl],
|
|
partial_observation: {
|
|
kind: "web-source-map-fetch",
|
|
source_maps: {
|
|
status: "partial",
|
|
requested: 2,
|
|
processed: 1,
|
|
items: [{ status: "included" }],
|
|
},
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("returns cancellation when the caller aborts during unused response release", async () => {
|
|
const controller = new AbortController();
|
|
const reason = new Error("caller cancelled during unused response release");
|
|
const requests = lookaheadRequests;
|
|
const fatMap = JSON.stringify({
|
|
version: 3,
|
|
sources: ["map0.js"],
|
|
sourcesContent: ["x".repeat(15000)],
|
|
names: [],
|
|
mappings: "AAAA",
|
|
});
|
|
const result = await fetchWebSourceMaps(
|
|
requests,
|
|
{ ...approvedInput, source_map_limits: { output_bytes: 32400 } },
|
|
controller.signal,
|
|
{
|
|
fetch: async (url) => {
|
|
const index = Number(/map(\d)/.exec(String(url))?.[1] ?? 0);
|
|
if (index === 0) return new Response(fatMap, { status: 200 });
|
|
return new Response(
|
|
new ReadableStream<Uint8Array>({
|
|
cancel() {
|
|
controller.abort(reason);
|
|
},
|
|
}),
|
|
{ status: 200 },
|
|
);
|
|
},
|
|
},
|
|
);
|
|
if (result.ok) throw new Error("Expected cancellation");
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
expect(primaryAnalysisError(result.error).cause).toBe(reason);
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "cancelled",
|
|
category: "cancelled",
|
|
details: {
|
|
cleanup: "complete",
|
|
partial_observation: {
|
|
kind: "web-source-map-fetch",
|
|
source_maps: {
|
|
status: "partial",
|
|
requested: 2,
|
|
processed: 1,
|
|
items: [{ status: "included" }],
|
|
},
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("keeps only the failed owner when redirect cleanup succeeds and an unused prefetch fails", async () => {
|
|
const physical = new Error("unused prefetch cancellation failed");
|
|
const requests = lookaheadRequests;
|
|
const siblingUrl = requests[1]!.fetchUrl;
|
|
const redirectUrl = "https://app.example.test/map0-final.js.map";
|
|
let redirectReleases = 0;
|
|
const fatMap = JSON.stringify({
|
|
version: 3,
|
|
sources: ["map0.js"],
|
|
sourcesContent: ["x".repeat(15000)],
|
|
names: [],
|
|
mappings: "AAAA",
|
|
});
|
|
const result = await fetchWebSourceMaps(
|
|
requests,
|
|
{ ...approvedInput, source_map_limits: { output_bytes: 32400 } },
|
|
undefined,
|
|
{
|
|
fetch: async (url) => {
|
|
const href = String(url);
|
|
if (href === requests[0]!.fetchUrl)
|
|
return new Response(
|
|
new ReadableStream<Uint8Array>({
|
|
cancel() {
|
|
redirectReleases += 1;
|
|
},
|
|
}),
|
|
{ status: 302, headers: { location: redirectUrl } },
|
|
);
|
|
if (href === redirectUrl) return new Response(fatMap, { status: 200 });
|
|
return new Response(
|
|
new ReadableStream<Uint8Array>({
|
|
cancel() {
|
|
throw physical;
|
|
},
|
|
}),
|
|
{ status: 200 },
|
|
);
|
|
},
|
|
},
|
|
);
|
|
if (result.ok) throw new Error("Expected the failed prefetch to stay owned");
|
|
expect(redirectReleases).toBe(1);
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual([siblingUrl]);
|
|
expect(result.error.cleanupResources).not.toContain(redirectUrl);
|
|
expect(nestedCauses(result.error)).toContain(physical);
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
cleanup: "incomplete",
|
|
resources: [siblingUrl],
|
|
partial_observation: {
|
|
kind: "web-source-map-fetch",
|
|
source_maps: {
|
|
status: "partial",
|
|
requested: 2,
|
|
processed: 1,
|
|
items: [{ status: "included", effective_url: redirectUrl }],
|
|
},
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("merges sibling cleanup when cancellation ends admission at the first map", async () => {
|
|
const controller = new AbortController();
|
|
const reason = new Error("caller cancelled during first map");
|
|
const physical = new Error("sibling response cancellation failed");
|
|
const requests = [0, 1].map((index) => ({
|
|
...request,
|
|
scriptKey: `scr_${String(index + 1).repeat(64)}`,
|
|
declaredUrl: `https://app.example.test/map${index}.js.map`,
|
|
fetchUrl: `https://app.example.test/map${index}.js.map`,
|
|
}));
|
|
const siblingUrl = requests[1]!.fetchUrl;
|
|
const mapText = JSON.stringify({
|
|
version: 3,
|
|
sources: ["map0.js"],
|
|
names: [],
|
|
mappings: "AAAA",
|
|
});
|
|
const result = await fetchWebSourceMaps(
|
|
requests,
|
|
approvedInput,
|
|
controller.signal,
|
|
{
|
|
fetch: async (url) => {
|
|
const index = Number(/map(\d)/.exec(String(url))?.[1] ?? 0);
|
|
if (index === 0) return new Response(mapText, { status: 200 });
|
|
controller.abort(reason);
|
|
return new Response(
|
|
new ReadableStream<Uint8Array>({
|
|
cancel() {
|
|
throw physical;
|
|
},
|
|
}),
|
|
{ status: 200 },
|
|
);
|
|
},
|
|
},
|
|
);
|
|
if (result.ok) throw new Error("Expected cancellation");
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual([siblingUrl]);
|
|
expect(primaryAnalysisError(result.error).cause).toBe(reason);
|
|
expect(nestedCauses(result.error)).toContain(physical);
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
category: "cancelled",
|
|
details: {
|
|
cleanup: "incomplete",
|
|
resources: [siblingUrl],
|
|
partial_observation: {
|
|
kind: "web-source-map-fetch",
|
|
source_maps: { requested: 2, processed: 0 },
|
|
},
|
|
diagnostics: {
|
|
primary_error: {
|
|
code: "cancelled",
|
|
details: { operation: "analyze_web_bundle", cleanup: "complete" },
|
|
},
|
|
cleanup_error: {
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
resources: [siblingUrl],
|
|
cleanup: "incomplete",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("checks cancellation before returning an empty source-map selection", async () => {
|
|
const controller = new AbortController();
|
|
const host = {
|
|
fetch: async () => {
|
|
throw new Error("No source-map request should be made");
|
|
},
|
|
};
|
|
expect(await fetchWebSourceMaps([], input, controller.signal, host)).toEqual({
|
|
ok: true,
|
|
value: { status: "not_requested", requested: 0, processed: 0, items: [] },
|
|
});
|
|
|
|
const reason = new Error("cancelled before fetching");
|
|
controller.abort(reason);
|
|
const result = await fetchWebSourceMaps([], input, controller.signal, host);
|
|
if (result.ok) throw new Error("Expected cancellation");
|
|
expect(result.error._tag).toBe("AnalysisCancelledError");
|
|
expect(result.error.cause).toBe(reason);
|
|
});
|
|
|
|
it("cancels a response body that arrives after its fetch host aborts synchronously", async () => {
|
|
const controller = new AbortController();
|
|
const reason = new Error("fetch host aborted during admission");
|
|
const fetching = phaseGate<void, Response>();
|
|
let cancellationReason: unknown;
|
|
const response = new Response(
|
|
new ReadableStream<Uint8Array>({
|
|
cancel(cause) {
|
|
cancellationReason = cause;
|
|
},
|
|
}),
|
|
);
|
|
try {
|
|
const result = await fetchWebSourceMaps(
|
|
[request],
|
|
approvedInput,
|
|
controller.signal,
|
|
{
|
|
fetch: () => {
|
|
controller.abort(reason);
|
|
return fetching.hold();
|
|
},
|
|
},
|
|
);
|
|
if (result.ok) throw new Error("Expected cancellation");
|
|
expect(result.error.cause).toBe(reason);
|
|
fetching.release(response);
|
|
await expect.poll(() => cancellationReason).toBe(reason);
|
|
await expect.poll(() => result.error.cleanupIncomplete).toBe(false);
|
|
} finally {
|
|
fetching.release(response);
|
|
}
|
|
});
|
|
|
|
it("retains cleanup when a response arrives after fetch cancellation", async () => {
|
|
const controller = new AbortController();
|
|
const reason = new Error("caller cancelled during fetch");
|
|
const fetching = phaseGate<void, Response>();
|
|
const [body, sibling] = new ReadableStream<Uint8Array>({
|
|
start(stream) {
|
|
stream.enqueue(new Uint8Array([1]));
|
|
},
|
|
}).tee();
|
|
const response = new Response(body);
|
|
try {
|
|
const pending = fetchWebSourceMaps(
|
|
[request],
|
|
approvedInput,
|
|
controller.signal,
|
|
{
|
|
fetch: () => fetching.hold(),
|
|
},
|
|
);
|
|
await fetching.reached(pending, "source-map fetch");
|
|
controller.abort(reason);
|
|
const result = await pending;
|
|
if (result.ok) throw new Error("Expected cancellation");
|
|
expect(result.error.cause).toBe(reason);
|
|
fetching.release(response);
|
|
await expect
|
|
.poll(() => body.locked || !result.error.cleanupIncomplete)
|
|
.toBe(true);
|
|
expect(body.locked).toBe(true);
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual([request.fetchUrl]);
|
|
await sibling.cancel();
|
|
await expect.poll(() => result.error.cleanupIncomplete).toBe(false);
|
|
expect(response.body?.locked).toBe(false);
|
|
} finally {
|
|
controller.abort(reason);
|
|
fetching.release(response);
|
|
await Promise.all([
|
|
sibling.cancel(),
|
|
body.locked ? undefined : body.cancel(),
|
|
]);
|
|
}
|
|
});
|
|
|
|
it.each([false, true])(
|
|
"distinguishes a late response cleanup failure (failed=%s)",
|
|
async (failed) => {
|
|
const controller = new AbortController();
|
|
const reason = new Error("caller cancelled during fetch");
|
|
const physical = new Error("late response cancellation failed");
|
|
const fetching = phaseGate<void, Response>();
|
|
const response = new Response(
|
|
failed
|
|
? new ReadableStream<Uint8Array>({
|
|
cancel() {
|
|
throw physical;
|
|
},
|
|
})
|
|
: new ReadableStream<Uint8Array>({
|
|
start(stream) {
|
|
stream.error(reason);
|
|
},
|
|
}),
|
|
);
|
|
try {
|
|
const pending = fetchWebSourceMaps(
|
|
[request],
|
|
approvedInput,
|
|
controller.signal,
|
|
{
|
|
fetch: () => fetching.hold(),
|
|
},
|
|
);
|
|
await fetching.reached(pending, "source-map fetch");
|
|
controller.abort(reason);
|
|
const result = await pending;
|
|
if (result.ok) throw new Error("Expected cancellation");
|
|
expect(result.error.cause).toBe(reason);
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
fetching.release(response);
|
|
if (failed) {
|
|
await expect
|
|
.poll(
|
|
() =>
|
|
result.error.secondaryFailures[0]?.secondaryFailures[0]?.cause,
|
|
)
|
|
.toBe(physical);
|
|
expect(result.error.secondaryFailures).toHaveLength(1);
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual([request.fetchUrl]);
|
|
} else {
|
|
await expect.poll(() => result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
}
|
|
} finally {
|
|
controller.abort(reason);
|
|
fetching.release(response);
|
|
}
|
|
},
|
|
);
|
|
|
|
it("retains a fetch host that ignores an item deadline until it settles", async () => {
|
|
let rejectFetch!: (cause: unknown) => void;
|
|
const fetching = new Promise<Response>((_resolve, reject) => {
|
|
rejectFetch = reject;
|
|
});
|
|
const pending = fetchWebSourceMaps(
|
|
[request],
|
|
{
|
|
...input,
|
|
allowed_origins: [new URL(request.fetchUrl).origin],
|
|
source_map_fetch_timeout_ms: 50,
|
|
},
|
|
undefined,
|
|
{ fetch: async () => fetching },
|
|
);
|
|
const result = await pending;
|
|
if (result.ok) throw new Error("Expected pending-fetch cleanup");
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual([request.fetchUrl]);
|
|
rejectFetch(result.error.cause);
|
|
await expect.poll(() => result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
});
|
|
|
|
it("preserves a physical fetch failure when the caller cancels before delivery", async () => {
|
|
const controller = new AbortController();
|
|
const physical = new Error("physical fetch failure");
|
|
const reason = { kind: "late caller cancellation" };
|
|
const result = await fetchWebSourceMaps(
|
|
[request],
|
|
approvedInput,
|
|
controller.signal,
|
|
{
|
|
fetch: () => {
|
|
const rejected = Promise.reject(physical);
|
|
void rejected.catch(() => {
|
|
queueMicrotask(() => controller.abort(reason));
|
|
});
|
|
return rejected;
|
|
},
|
|
},
|
|
);
|
|
if (result.ok) throw new Error("Expected physical fetch failure");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "ProviderAdapterError",
|
|
cause: physical,
|
|
});
|
|
expect(
|
|
result.error.secondaryFailures.some((failure) => failure.cause === reason),
|
|
).toBe(true);
|
|
});
|
|
|
|
it("retains a fetch rejection that arrives after its host aborts synchronously", async () => {
|
|
const controller = new AbortController();
|
|
const reason = new Error("fetch host aborted during admission");
|
|
const lateFailure = new Error("late fetch failure");
|
|
const fetching = phaseGate<void, Response>();
|
|
try {
|
|
const result = await fetchWebSourceMaps(
|
|
[request],
|
|
approvedInput,
|
|
controller.signal,
|
|
{
|
|
fetch: () => {
|
|
controller.abort(reason);
|
|
return fetching.hold();
|
|
},
|
|
},
|
|
);
|
|
if (result.ok) throw new Error("Expected cancellation");
|
|
expect(result.error.cause).toBe(reason);
|
|
expect(result.error.cleanupResources).toEqual([request.fetchUrl]);
|
|
fetching.reject(lateFailure);
|
|
await expect.poll(() => result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.secondaryFailures.map(({ cause }) => cause)).toEqual([
|
|
lateFailure,
|
|
]);
|
|
} finally {
|
|
fetching.release(new Response(null));
|
|
}
|
|
});
|
|
|
|
it.each([
|
|
{
|
|
sourceRoot: "https://cdn.example/src/",
|
|
expected: "https://cdn.example/src/a.js",
|
|
},
|
|
{
|
|
sourceRoot: "https://cdn.example/src//",
|
|
expected: "https://cdn.example/src//a.js",
|
|
},
|
|
{ sourceRoot: "webpack://", expected: "webpack://a.js" },
|
|
{ sourceRoot: "webpack:///", expected: "webpack:///a.js" },
|
|
{ sourceRoot: "", expected: "https://app.example.test/maps/a.js" },
|
|
{ sourceRoot: null, expected: "https://app.example.test/maps/a.js" },
|
|
])(
|
|
"retains the admitted ECMA-426 source identity for root $sourceRoot",
|
|
async ({ sourceRoot, expected }) => {
|
|
const text = JSON.stringify({
|
|
version: 3,
|
|
sourceRoot,
|
|
sources: ["a.js", null],
|
|
names: [],
|
|
mappings: "AAAA;ACAA",
|
|
});
|
|
const fetchInput = analyzeWebBundleInputSchema.parse({
|
|
cdp_endpoint: "http://127.0.0.1:9222",
|
|
target_id: "page-1",
|
|
fetch_source_maps: true,
|
|
allowed_origins: [new URL(request.fetchUrl).origin],
|
|
});
|
|
const selectedRequest = {
|
|
...request,
|
|
declaredUrl: "https://app.example.test/maps/app.js.map",
|
|
fetchUrl: "https://app.example.test/maps/app.js.map",
|
|
};
|
|
const result = await fetchWebSourceMapsOk(
|
|
[selectedRequest],
|
|
fetchInput,
|
|
undefined,
|
|
{
|
|
fetch: async () => new Response(text, { status: 200 }),
|
|
},
|
|
);
|
|
expect(result.items[0]).toMatchObject({
|
|
status: "included",
|
|
original_sources: [{ source: expected }, { source: null }],
|
|
mappings: [{ source: expected }, { source: null }],
|
|
});
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
{ trigger: "caller-abort", rejects: false },
|
|
{ trigger: "caller-abort", rejects: true },
|
|
{ trigger: "response-limit", rejects: false },
|
|
{ trigger: "response-limit", rejects: true },
|
|
])(
|
|
"retains original cancellation after $trigger (rejects: $rejects)",
|
|
async ({ trigger, rejects }) => {
|
|
let started!: () => void;
|
|
const reading = new Promise<void>((resolve) => {
|
|
started = resolve;
|
|
});
|
|
let cancelled!: () => void;
|
|
const cancelling = new Promise<void>((resolve) => {
|
|
cancelled = resolve;
|
|
});
|
|
let finish!: () => void;
|
|
const held = new Promise<void>((resolve) => {
|
|
finish = resolve;
|
|
});
|
|
let calls = 0;
|
|
const cleanupFailure = new Error("response cancellation failed");
|
|
const body = new ReadableStream<Uint8Array>(
|
|
{
|
|
pull(controller) {
|
|
started();
|
|
if (trigger === "response-limit")
|
|
controller.enqueue(new Uint8Array(2));
|
|
},
|
|
async cancel() {
|
|
calls += 1;
|
|
cancelled();
|
|
await held;
|
|
if (rejects) throw cleanupFailure;
|
|
},
|
|
},
|
|
{ highWaterMark: 0 },
|
|
);
|
|
const controller = new AbortController();
|
|
const reason = { requested: "stop source-map response" };
|
|
const pending = fetchWebSourceMaps(
|
|
[request],
|
|
{
|
|
...input,
|
|
allowed_origins: [new URL(request.fetchUrl).origin],
|
|
source_map_limits: { fetch_bytes: 1 },
|
|
},
|
|
controller.signal,
|
|
{ fetch: async () => new Response(body) },
|
|
);
|
|
try {
|
|
await reading;
|
|
if (trigger === "caller-abort") controller.abort(reason);
|
|
await cancelling;
|
|
if (trigger === "response-limit") controller.abort(reason);
|
|
const result = await pending;
|
|
if (result.ok) throw new Error("Expected caller cancellation");
|
|
expect(result.error).toMatchObject({
|
|
_tag:
|
|
trigger === "caller-abort"
|
|
? "AnalysisCancelledError"
|
|
: "ProviderAdapterError",
|
|
cleanupIncomplete: true,
|
|
cleanupResources: [request.fetchUrl],
|
|
});
|
|
expect(result.error.hasCleanupOwner).toBe(true);
|
|
expect(result.error.cleanupRetry).toBeUndefined();
|
|
expect(result.error.cleanup?.reason).toContain("pending");
|
|
finish();
|
|
await expect
|
|
.poll(() =>
|
|
rejects
|
|
? result.error.cleanup?.reason.includes(
|
|
"response cancellation failed",
|
|
)
|
|
: !result.error.cleanupIncomplete,
|
|
)
|
|
.toBe(true);
|
|
if (trigger === "caller-abort") expect(result.error.cause).toBe(reason);
|
|
else {
|
|
expect(result.error.cause).toMatchObject({
|
|
name: "ParseFailureError",
|
|
failure: {
|
|
kind: "limit",
|
|
format: "source-map",
|
|
limit: { name: "fetch_bytes", maximum: 1 },
|
|
},
|
|
});
|
|
expect(
|
|
result.error.secondaryFailures.some(
|
|
(failure) => failure.cause === reason,
|
|
),
|
|
).toBe(true);
|
|
}
|
|
expect(result.error.cleanupIncomplete).toBe(rejects);
|
|
if (rejects)
|
|
expect(
|
|
result.error.secondaryFailures.some(
|
|
(failure) => failure.cause === cleanupFailure,
|
|
),
|
|
).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual(
|
|
rejects ? [request.fetchUrl] : [],
|
|
);
|
|
expect(calls).toBe(1);
|
|
} finally {
|
|
finish();
|
|
controller.abort(reason);
|
|
await pending;
|
|
}
|
|
},
|
|
);
|
|
|
|
it("keeps same-URL response cleanup owners independent across mixed outcomes", async () => {
|
|
const cleanupFailure = new Error("second response cancellation failed");
|
|
const operations = [false, true].map((rejects) => {
|
|
const reading = phaseArrival();
|
|
const cleanup = phaseGate();
|
|
let calls = 0;
|
|
const body = new ReadableStream<Uint8Array>(
|
|
{
|
|
pull() {
|
|
reading.arrive();
|
|
},
|
|
async cancel() {
|
|
calls += 1;
|
|
await cleanup.hold();
|
|
if (rejects) throw cleanupFailure;
|
|
},
|
|
},
|
|
{ highWaterMark: 0 },
|
|
);
|
|
const controller = new AbortController();
|
|
const reason = { rejects };
|
|
const pending = fetchWebSourceMaps(
|
|
[request],
|
|
approvedInput,
|
|
controller.signal,
|
|
{ fetch: async () => new Response(body) },
|
|
);
|
|
return {
|
|
reading,
|
|
cleanup,
|
|
controller,
|
|
reason,
|
|
pending,
|
|
calls: () => calls,
|
|
body,
|
|
};
|
|
});
|
|
try {
|
|
await Promise.all(
|
|
operations.map(({ reading, pending }) =>
|
|
reading.reached(pending, "same-URL source-map body read"),
|
|
),
|
|
);
|
|
for (const operation of operations)
|
|
operation.controller.abort(operation.reason);
|
|
const errors = await Promise.all(
|
|
operations.map(async ({ pending }) => {
|
|
const result = await pending;
|
|
if (result.ok) throw new Error("Expected cancellation");
|
|
return result.error;
|
|
}),
|
|
);
|
|
expect(errors.map((error) => error.cleanupResources)).toEqual([
|
|
[request.fetchUrl],
|
|
[request.fetchUrl],
|
|
]);
|
|
operations[0]!.cleanup.release();
|
|
await expect.poll(() => errors[0]!.cleanupIncomplete).toBe(false);
|
|
expect(errors[1]!.cleanupIncomplete).toBe(true);
|
|
expect(errors[1]!.cleanupResources).toEqual([request.fetchUrl]);
|
|
operations[1]!.cleanup.release();
|
|
await expect.poll(() => errors[1]!.secondaryFailures.length).toBe(1);
|
|
expect(errors[1]!.secondaryFailures[0]?.cause).toBe(cleanupFailure);
|
|
expect(errors.map((error) => error.cleanupIncomplete)).toEqual([
|
|
false,
|
|
true,
|
|
]);
|
|
expect(errors.map((error) => error.cause)).toEqual(
|
|
operations.map(({ reason }) => reason),
|
|
);
|
|
expect(operations.map(({ calls }) => calls())).toEqual([1, 1]);
|
|
expect(operations.map(({ body }) => body.locked)).toEqual([false, false]);
|
|
} finally {
|
|
for (const operation of operations) {
|
|
operation.cleanup.release();
|
|
operation.controller.abort(operation.reason);
|
|
}
|
|
await Promise.all(operations.map(({ pending }) => pending));
|
|
}
|
|
});
|
|
|
|
it.each([null, false])(
|
|
"preserves a falsy caller reason and releases a native HTTP body: %s",
|
|
async (reason) => {
|
|
const server = createServer((_request, response) => {
|
|
response.writeHead(200, { "content-type": "application/json" });
|
|
response.write('{"version":3,');
|
|
});
|
|
server.listen(0, "127.0.0.1");
|
|
await once(server, "listening");
|
|
const address = server.address();
|
|
if (address === null || typeof address === "string")
|
|
throw new Error("Expected TCP address");
|
|
const url = `http://127.0.0.1:${address.port}/map`;
|
|
const controller = new AbortController();
|
|
let delivered!: () => void;
|
|
const responseReceived = new Promise<void>((resolve) => {
|
|
delivered = resolve;
|
|
});
|
|
try {
|
|
const pending = fetchWebSourceMaps(
|
|
[{ ...request, declaredUrl: url, fetchUrl: url }],
|
|
{
|
|
...input,
|
|
allowed_origins: [new URL(url).origin],
|
|
source_map_fetch_timeout_ms: 10_000,
|
|
},
|
|
controller.signal,
|
|
{
|
|
fetch: async (...args) => {
|
|
const response = await fetch(...args);
|
|
delivered();
|
|
return response;
|
|
},
|
|
},
|
|
);
|
|
await responseReceived;
|
|
await new Promise<void>((resolve) => setImmediate(resolve));
|
|
controller.abort(reason);
|
|
const result = await pending;
|
|
if (result.ok) throw new Error("Expected caller cancellation");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
});
|
|
await expect.poll(() => result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
} finally {
|
|
server.closeAllConnections();
|
|
await new Promise<void>((resolve, reject) =>
|
|
server.close((error) => (error ? reject(error) : resolve())),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
|
|
it("does not mistake an independent body AbortError for a falsy caller cancellation", async () => {
|
|
const controller = new AbortController();
|
|
const physical = new DOMException("independent body failure", "AbortError");
|
|
const body = new ReadableStream<Uint8Array>(
|
|
{
|
|
pull(stream) {
|
|
stream.error(physical);
|
|
queueMicrotask(() => controller.abort(false));
|
|
},
|
|
},
|
|
{ highWaterMark: 0 },
|
|
);
|
|
const result = await fetchWebSourceMaps(
|
|
[request],
|
|
approvedInput,
|
|
controller.signal,
|
|
{ fetch: async () => new Response(body) },
|
|
);
|
|
if (result.ok) throw new Error("Expected the independent body failure");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "ProviderAdapterError",
|
|
cause: physical,
|
|
});
|
|
expect(
|
|
result.error.secondaryFailures.some((failure) => failure.cause === false),
|
|
).toBe(true);
|
|
});
|
|
|
|
it("keeps actual stalled HTTP headers and body deadlines local to their maps", async () => {
|
|
const headers = phaseArrival();
|
|
const body = phaseArrival();
|
|
const server = createServer((request, response) => {
|
|
if (request.url === "/headers") headers.arrive();
|
|
else if (request.url === "/body")
|
|
response
|
|
.writeHead(200, { "content-type": "application/json" })
|
|
.write('{"version":3,');
|
|
});
|
|
const listening = once(server, "listening");
|
|
server.listen(0, "127.0.0.1");
|
|
await listening;
|
|
const address = server.address();
|
|
if (address === null || typeof address === "string")
|
|
throw new Error("Expected TCP address");
|
|
const origin = `http://127.0.0.1:${address.port}`;
|
|
const deadline = 13_700;
|
|
const controller = new AbortController();
|
|
const signal = AbortSignal.any([
|
|
controller.signal,
|
|
AbortSignal.timeout(testWaitDeadline()),
|
|
]);
|
|
const capture = (stage: "headers" | "body") => {
|
|
const url = `${origin}/${stage}`;
|
|
return fetchWebSourceMaps(
|
|
[{ ...request, declaredUrl: url, fetchUrl: url }],
|
|
{
|
|
...input,
|
|
allowed_origins: [origin],
|
|
source_map_fetch_timeout_ms: deadline,
|
|
},
|
|
signal,
|
|
{
|
|
fetch: async (url, options) => {
|
|
const response = await fetch(url, options);
|
|
if (stage === "body") body.arrive();
|
|
return response;
|
|
},
|
|
},
|
|
);
|
|
};
|
|
|
|
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] });
|
|
const waitingForHeaders = capture("headers");
|
|
const waitingForBody = capture("body");
|
|
try {
|
|
await headers.reached(waitingForHeaders, "request before response headers");
|
|
await body.reached(
|
|
waitingForBody,
|
|
"headers before remaining response body",
|
|
);
|
|
await vi.advanceTimersByTimeAsync(deadline);
|
|
const limitation = expect.stringContaining(
|
|
`${deadline} ms source_map_fetch_timeout_ms deadline`,
|
|
);
|
|
for (const [pending, effectiveUrl] of [
|
|
[waitingForHeaders, null],
|
|
[waitingForBody, `${origin}/body`],
|
|
] as const)
|
|
expect(await pending).toMatchObject({
|
|
ok: true,
|
|
value: {
|
|
items: [
|
|
{ status: "fetch_failed", effective_url: effectiveUrl, limitation },
|
|
],
|
|
},
|
|
});
|
|
} finally {
|
|
controller.abort();
|
|
try {
|
|
await new Promise<void>((resolve, reject) => {
|
|
server.close((error) => (error ? reject(error) : resolve()));
|
|
server.closeAllConnections();
|
|
});
|
|
} finally {
|
|
await Promise.allSettled([waitingForHeaders, waitingForBody]);
|
|
vi.useRealTimers();
|
|
}
|
|
}
|
|
});
|
|
|
|
it("retains redirect and HTTP error response locks until their tee siblings release", async () => {
|
|
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
|
const deadline = 100;
|
|
const attempts = [302, 503].map((status) => {
|
|
const [body, sibling] = new ReadableStream<Uint8Array>().tee();
|
|
const pending = fetchWebSourceMaps(
|
|
[request],
|
|
{ ...approvedInput, source_map_fetch_timeout_ms: deadline },
|
|
undefined,
|
|
{
|
|
fetch: async (url) => {
|
|
if (String(url) !== request.fetchUrl)
|
|
throw new Error("Redirect followed before releasing its body");
|
|
return new Response(body, {
|
|
status,
|
|
...(status === 302 ? { headers: { location: "/next" } } : {}),
|
|
});
|
|
},
|
|
},
|
|
);
|
|
return { status, body, sibling, pending };
|
|
});
|
|
try {
|
|
await vi.advanceTimersByTimeAsync(deadline);
|
|
for (const { status, body, sibling, pending } of attempts) {
|
|
const result = await Promise.race([pending, null]);
|
|
if (result === null || result.ok)
|
|
throw new Error(
|
|
`Expected retained cleanup for HTTP ${status} at the deadline`,
|
|
);
|
|
expect(result.error).toMatchObject({
|
|
cleanupIncomplete: true,
|
|
cleanupResources: [request.fetchUrl],
|
|
});
|
|
expect(body.locked).toBe(true);
|
|
await sibling.cancel();
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(body.locked).toBe(false);
|
|
}
|
|
} finally {
|
|
try {
|
|
await Promise.all(
|
|
attempts.map(async ({ body, sibling, pending }) => {
|
|
await Promise.allSettled([body.cancel(), sibling.cancel()]);
|
|
await pending;
|
|
}),
|
|
);
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
}
|
|
});
|
|
|
|
it("cancels an admitted response unread when the deadline expires before the timer fires", async () => {
|
|
const timeoutMs = 50;
|
|
let now = 0;
|
|
const clock = vi.spyOn(performance, "now").mockImplementation(() => now);
|
|
const firstBodyAction = phaseArrival<"read" | "cancel">();
|
|
const cancellation = phaseGate();
|
|
let operationSignal: AbortSignal | null | undefined;
|
|
const body = new ReadableStream<Uint8Array>(
|
|
{
|
|
pull() {
|
|
firstBodyAction.arrive("read");
|
|
},
|
|
cancel() {
|
|
firstBodyAction.arrive("cancel");
|
|
return cancellation.hold();
|
|
},
|
|
},
|
|
{ highWaterMark: 0 },
|
|
);
|
|
const pending = fetchWebSourceMaps(
|
|
[request],
|
|
{ ...approvedInput, source_map_fetch_timeout_ms: timeoutMs },
|
|
undefined,
|
|
{
|
|
fetch: async (_url, options) => {
|
|
operationSignal = options?.signal;
|
|
now = timeoutMs + 1;
|
|
return new Response(body);
|
|
},
|
|
},
|
|
);
|
|
try {
|
|
expect(
|
|
await firstBodyAction.reached(pending, "first response body action"),
|
|
).toBe("cancel");
|
|
expect(operationSignal?.aborted).toBe(false);
|
|
const result = await pending;
|
|
if (result.ok) throw new Error("Expected owned response cleanup");
|
|
expect(result.error).toMatchObject({
|
|
cleanupIncomplete: true,
|
|
cleanupResources: [request.fetchUrl],
|
|
cause: {
|
|
message: expect.stringContaining("50 ms source_map_fetch_timeout_ms"),
|
|
},
|
|
});
|
|
expect(body.locked).toBe(true);
|
|
cancellation.release();
|
|
await expect.poll(() => result.error.cleanupIncomplete).toBe(false);
|
|
expect(body.locked).toBe(false);
|
|
} finally {
|
|
clock.mockRestore();
|
|
cancellation.release();
|
|
await pending;
|
|
}
|
|
});
|
|
|
|
it("retains an oversized redirect identity's pending response cleanup", async () => {
|
|
const [body, sibling] = new ReadableStream<Uint8Array>({
|
|
start(controller) {
|
|
controller.enqueue(new Uint8Array([1]));
|
|
},
|
|
}).tee();
|
|
const finalUrl = new URL(`/${"a".repeat(6000)}`, request.fetchUrl).href;
|
|
const pending = fetchWebSourceMaps(
|
|
[request],
|
|
{
|
|
...input,
|
|
allowed_origins: [new URL(request.fetchUrl).origin],
|
|
source_map_limits: { output_bytes: 4000 },
|
|
source_map_fetch_timeout_ms: 100,
|
|
},
|
|
undefined,
|
|
{
|
|
fetch: async (url) =>
|
|
String(url) === request.fetchUrl
|
|
? new Response(null, { status: 302, headers: { location: finalUrl } })
|
|
: new Response(body),
|
|
},
|
|
);
|
|
try {
|
|
const result = await pending;
|
|
if (result.ok) throw new Error("Expected output capacity refusal");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "AnalysisResourceConstraintError",
|
|
cleanupIncomplete: true,
|
|
cleanupResources: [finalUrl],
|
|
});
|
|
expect(result.error.hasCleanupOwner).toBe(true);
|
|
await sibling.cancel();
|
|
await expect.poll(() => result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
} finally {
|
|
await sibling.cancel();
|
|
await pending;
|
|
}
|
|
});
|
|
|
|
it("preserves the physical reader failure when stream cancellation also rejects", async () => {
|
|
const failure = new Error("response transport failed");
|
|
const body = new ReadableStream<Uint8Array>(
|
|
{
|
|
pull(controller) {
|
|
controller.error(failure);
|
|
},
|
|
},
|
|
{ highWaterMark: 0 },
|
|
);
|
|
const result = await fetchWebSourceMaps(
|
|
[request],
|
|
{
|
|
...input,
|
|
allowed_origins: [new URL(request.fetchUrl).origin],
|
|
},
|
|
undefined,
|
|
{ fetch: async () => new Response(body) },
|
|
);
|
|
if (result.ok) throw new Error("Expected the physical read failure");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "ProviderAdapterError",
|
|
cause: failure,
|
|
cleanupIncomplete: true,
|
|
cleanupResources: [request.fetchUrl],
|
|
});
|
|
await expect
|
|
.poll(() => result.error.secondaryFailures[0]?.cause)
|
|
.toBe(failure);
|
|
expect(result.error.cleanup?.reason).toContain("response transport failed");
|
|
expect(result.error.cleanupRetry).toBeUndefined();
|
|
expect(body.locked).toBe(false);
|
|
});
|
|
|
|
it("recovers a fetched map by raising its exact structural admission on the next call", async () => {
|
|
const text = JSON.stringify({
|
|
version: 3,
|
|
sources: ["a.ts"],
|
|
sourcesContent: ["export const a = 1;"],
|
|
names: [],
|
|
mappings: "AAAA",
|
|
});
|
|
const selected = {
|
|
...input,
|
|
allowed_origins: [new URL(request.fetchUrl).origin],
|
|
};
|
|
const host = { fetch: async () => new Response(text) };
|
|
const refused = await fetchWebSourceMapsOk(
|
|
[request],
|
|
{ ...selected, source_map_limits: { decoded_records: 4 } },
|
|
undefined,
|
|
host,
|
|
);
|
|
expect(refused.items[0]).toMatchObject({
|
|
status: "fetch_failed",
|
|
limitation: expect.stringContaining(
|
|
"source_map_limits.decoded_records is 4",
|
|
),
|
|
});
|
|
const recovered = await fetchWebSourceMapsOk(
|
|
[request],
|
|
{ ...selected, source_map_limits: { decoded_records: 5 } },
|
|
undefined,
|
|
host,
|
|
);
|
|
expect(recovered.items[0]).toMatchObject({
|
|
status: "included",
|
|
original_sources: [{ source: "https://app.example.test/a.ts" }],
|
|
mappings: [{ generated_line: 1, generated_column: 0 }],
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
{
|
|
limits: { decoded_records: 4 },
|
|
directory: "maps",
|
|
limit: "decoded_records",
|
|
},
|
|
{
|
|
limits: { fetch_bytes: 1500 },
|
|
directory: "m".repeat(1000),
|
|
limit: "fetch_bytes",
|
|
},
|
|
])(
|
|
"charges $limit for retained map interpretations and releases discarded ones",
|
|
async ({ limits, directory, limit }) => {
|
|
const requests = ["1", "2", "3"].map((key) => ({
|
|
...request,
|
|
scriptKey: `scr_${key.repeat(64)}`,
|
|
fetchUrl: `https://app.example.test/${directory}/app.js.map`,
|
|
}));
|
|
const responses = ["ACAA", "AAAA", "AAAA"].map((mappings) =>
|
|
JSON.stringify({ version: 3, sources: ["a.js"], names: [], mappings }),
|
|
);
|
|
const result = await fetchWebSourceMapsOk(
|
|
requests,
|
|
{
|
|
...input,
|
|
allowed_origins: ["https://app.example.test"],
|
|
source_map_limits: limits,
|
|
},
|
|
undefined,
|
|
{ fetch: async () => new Response(responses.shift()) },
|
|
);
|
|
|
|
expect(result).toMatchObject({
|
|
status: "partial",
|
|
requested: 3,
|
|
processed: 3,
|
|
items: [
|
|
{
|
|
status: "invalid",
|
|
response_identity: { representation: "fetch-response-body-bytes" },
|
|
},
|
|
{
|
|
status: "included",
|
|
mappings: [{ source: `https://app.example.test/${directory}/a.js` }],
|
|
},
|
|
{
|
|
status: "fetch_failed",
|
|
limitation: expect.stringContaining(`source_map_limits.${limit} is`),
|
|
},
|
|
],
|
|
});
|
|
},
|
|
);
|