fix(har): preserve failed Playwright requests with unavailable timings (#11758)

This commit is contained in:
RRFZ authored and GitHub committed 2026-11-18 22:23:53 +08:00
1 parent ce9635bcb4
commit bb6e307de5
3 files changed
+138 -9

No files matched your search

@@ -0,0 +1,89 @@
import { expect, it } from "vitest";
import { historicalHar } from "../../../tests/fixtures/historicalHar.js";
import { decodeHarCapture } from "./HarCapture.js";
// Playwright 1.64.1, Chromium: a local server closed the socket before sending
// headers. The HAR exporter retained these response and timing sentinels.
const capture = () => {
const fixture = historicalHar();
const entry = fixture.log.entries[0]!;
return {
log: {
...fixture.log,
creator: { name: "Playwright", version: "1.64.1" },
entries: [
entry,
{
...entry,
response: {
...entry.response,
status: -1,
statusText: "",
content: { size: -1, mimeType: "x-unknown" },
bodySize: -1,
_failureText: "net::ERR_EMPTY_RESPONSE",
},
timings: { send: -1, wait: -1, receive: -1 },
},
],
},
};
};
it.each([
{
status: -1,
failure: "net::ERR_EMPTY_RESPONSE",
timings: { send: -1, wait: -1, receive: -1 },
},
{
status: 200,
failure: "net::ERR_CONTENT_LENGTH_MISMATCH",
timings: { send: 0, wait: 1.25, receive: -1 },
},
])(
"retains failed Playwright requests with status $status without inventing timings",
({ status, failure, timings }) => {
const fixture = capture();
Object.assign(fixture.log.entries[1]!.response, {
status,
_failureText: failure,
});
fixture.log.entries[1]!.timings = timings;
const result = decodeHarCapture(JSON.stringify(fixture), []);
expect(result.total_records).toBe(2);
expect(result.records[0]?.reported).toMatchObject({
response: { status: 200 },
timings: { send: 0, wait: 1.25, receive: 0 },
});
expect(result.records[1]?.reported).toMatchObject({
response: { status, _failureText: failure },
timings,
});
expect(result.records[1]?.limitations.join(" ")).toContain("unavailable");
expect(result.records[0]?.limitations.join(" ")).not.toContain(
"unavailable timings",
);
},
);
it.each([
"other-producer",
"other-version",
"successful-response",
"missing-failure",
"invalid-duration",
])("keeps timing validation for %s", (variant) => {
const fixture = capture();
const failed = fixture.log.entries[1]!;
if (variant === "other-producer") fixture.log.creator.name = "unknown";
if (variant === "other-version") fixture.log.creator.version = "unknown";
if (variant === "successful-response") {
failed.response.status = 200;
Reflect.deleteProperty(failed.response, "_failureText");
}
if (variant === "missing-failure")
Reflect.deleteProperty(failed.response, "_failureText");
if (variant === "invalid-duration") failed.timings.send = -2;
expect(() => decodeHarCapture(JSON.stringify(fixture), [])).toThrow();
});
+8 -1
View File
@@ -33,7 +33,9 @@ export const decodeHarCapture = (
): DecodedCapture => {
const raw = parseCaptureJson(text, "HAR");
const validation = projectHar(raw, [], "validation");
const ipv6Adaptation = createHarValidator()(validation.reported);
const adaptations = createHarValidator()(validation.reported);
const ipv6Adaptation = adaptations.bracketedIpv6;
const failedRequests = new Set(adaptations.failedRequests);
const adaptedIpv6Entries = new Set(ipv6Adaptation?.entries);
const ipv6Limitations =
ipv6Adaptation === null
@@ -82,6 +84,11 @@ export const decodeHarCapture = (
limitations: [
"HAR text without an encoding declaration is producer-decoded Unicode. Unsupported declared body encodings remain opaque; original body bytes remain unknown unless content explicitly uses valid base64. Declared sizes are preserved independently of retained bytes. mitmproxy 12.2.3 SaveHar may report null optional postData.text; that null is retained and omitted only from the upstream validation copy.",
...(adaptedIpv6Entries.has(ordinal) ? ipv6Limitations : []),
...(failedRequests.has(ordinal)
? [
"This capture declares Playwright 1.64.1 and records a failed request with unavailable timings (-1). The original failure and timing sentinels are retained; only the validation copy substitutes nonnegative timings. Producer declarations are not authenticated.",
]
: []),
...(recordsExcluded
? [
"This record's payload and sidecars are excluded because its structural ancestor property was explicitly marked sensitive.",
+41 -8
View File
@@ -48,10 +48,15 @@ interface BracketedIpv6Adaptation {
readonly entries: readonly number[];
}
interface HarProducerAdaptations {
readonly bracketedIpv6: BracketedIpv6Adaptation | null;
readonly failedRequests: readonly number[];
}
/** Validate with pinned HAR 1.2 draft-06 schemas and the owned schema corrections. */
export const createHarValidator = (): ((
value: unknown,
) => BracketedIpv6Adaptation | null) => {
) => HarProducerAdaptations) => {
const require = createRequire(import.meta.url);
const schemas = z
.record(z.string(), z.record(z.string(), z.unknown()))
@@ -124,18 +129,42 @@ const bracketedIpv6ProducerOf = (
/** Adapt only observed producer representations in the disposable validation copy. */
const allowObservedProducerRepresentations = (
value: unknown,
): BracketedIpv6Adaptation | null => {
if (!captureObject(value) || !captureObject(value.log)) return null;
): HarProducerAdaptations => {
const none = { bracketedIpv6: null, failedRequests: [] };
if (!captureObject(value) || !captureObject(value.log)) return none;
const log = value.log;
if (!captureObject(log.creator) || !Array.isArray(log.entries)) return null;
if (!captureObject(log.creator) || !Array.isArray(log.entries)) return none;
const mitmproxy =
log.creator.name === "mitmproxy" && log.creator.version === "12.2.3";
const bracketedIpv6Producer = bracketedIpv6ProducerOf(log.creator);
if (!mitmproxy && bracketedIpv6Producer === null) return null;
const playwright =
log.creator.name === "Playwright" && log.creator.version === "1.64.1";
if (!mitmproxy && !playwright && bracketedIpv6Producer === null) return none;
const failedRequests: number[] = [];
const adaptedEntries: number[] = [];
for (const [ordinal, entry] of log.entries.entries()) {
const candidate: unknown = entry;
if (!captureObject(candidate)) continue;
// Playwright leaves required durations at -1 when a request fails before
// response timing is available. Adapt only this observed sentinel in the
// disposable validation copy; the recorded values remain unchanged.
const response = candidate.response;
const timings = candidate.timings;
if (
playwright &&
captureObject(response) &&
typeof response._failureText === "string" &&
response._failureText.length > 0 &&
captureObject(timings)
) {
let adapted = false;
for (const field of ["send", "wait", "receive"])
if (timings[field] === -1) {
timings[field] = 0;
adapted = true;
}
if (adapted) failedRequests.push(ordinal);
}
const address = candidate.serverIPAddress;
if (
bracketedIpv6Producer !== null &&
@@ -153,9 +182,13 @@ const allowObservedProducerRepresentations = (
const body = request.postData;
if (captureObject(body) && body.text === null) delete body.text;
}
return bracketedIpv6Producer === null
? null
: { producer: bracketedIpv6Producer, entries: adaptedEntries };
return {
bracketedIpv6:
bracketedIpv6Producer === null
? null
: { producer: bracketedIpv6Producer, entries: adaptedEntries },
failedRequests,
};
};
const HAR_REQUEST_URL_FORMAT = "absolute-url";