refactor(realtime): merge snapshot and race helpers, move shared parsers to kernel (Q-6.4-8)
Realtime kept three DataSnapshot readers and about ten hand-written abort/timeout races, and two parsers had drifted copies in other adapters. Snapshot: reconnect-coordinator and bounded-poll now use result.ts captureRealtimeDataSnapshot (+ `source`, variadic hasExactSnapshotKeys). Kept the result.ts rules, the stricter ones: Object.hasOwn instead of `"value" in` (prototype pollution), enumerable fields only, keys read from one descriptor snapshot, frozen computed at snapshot time instead of a later Object.isFrozen. Poll therefore now rejects non-enumerable fields; it also accepts null-prototype records, which the other two copies already did (pure data, no inherited accessors). Characterization test added for both in bounded-poll-coordinator.test.ts. Race: one raceRealtimeTask(task, signal, deadline) in result.ts, built on kernel createAbortableOperation, replaces fetch-sse timed/cancelReader, reconnect race/drain (the drain keeps a host timer via timerDeadline, since the clock itself may hang), poll awaitTaskOrAbort/awaitWithinLease, live-poll checkpoint + two quiescence races (now one), stream-coordinator deadline/recovery-wait/close-drain, and the two WS inline races. Kept semantics: deadline armed first and wins a tie (poll lease), task still starts when the clock throws (reader cancel), collaborator rejections still reach stream-coordinator callers, scheduler install failure still fails closed as a timeout. Small change: nothing starts once the signal is already aborted (WS apply / SSE onEvent / reader.read could run just before being reported ABORTED). The mapping adds one microtask per race, so the reconnect test flush() helper gets 16 turns instead of 8. Kernel: parseRetryAfterDelay moves to platform/retry-after.ts (realtime re-exports it; http retry-policy parseRetryAfter now delegates, so it rejects "", " ", "1e3", "1.5", "0x10" and non-IMF dates it used to accept). json-member-scanner moves to platform/; web-push push-codec uses it instead of its top-level-only duplicate-key check, so nested duplicates are rejected too. INVENTORY 136 -> 137, detail tables updated. Tests: RED for http strict Retry-After, web-push nested duplicate, poll snapshot rules; new direct raceRealtimeTask tests. Gates: architecture, types app/test (+ service-worker/web-worker), lint, adapter-inventory (abort ratchet 21/21), test:unit, test:component, realtime-boundaries, integration/platform all pass. Realtime bundle 40346 -> 39433 gzip. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
54e6bce9d2
commit
ae2f006b6d
@@ -8,6 +8,7 @@ import {
|
||||
type WebPushResult,
|
||||
} from "../../contracts/web-push.ts";
|
||||
import { hasExactKeys, isRecord, validOpaqueId } from "./codec-primitives.ts";
|
||||
import { hasDuplicateJsonMembers } from "../platform/json-member-scanner.ts";
|
||||
|
||||
const REGISTRY_ID = /^[A-Z][A-Z0-9_]{0,63}$/u;
|
||||
const ISO_INSTANT =
|
||||
@@ -71,7 +72,13 @@ export function decodeWebPushHint(
|
||||
} catch {
|
||||
return webPushFailure("CONTRACT_REJECTED", "PUSH_DECODE");
|
||||
}
|
||||
if (hasDuplicateTopLevelJsonKeys(decoded)) {
|
||||
// The byte cap already bounds the scan, so the budget never trips first.
|
||||
if (
|
||||
hasDuplicateJsonMembers(decoded, {
|
||||
maxDepth: WEB_PUSH_LIMITS.decodedHintBytes,
|
||||
maxMembers: WEB_PUSH_LIMITS.decodedHintBytes,
|
||||
})
|
||||
) {
|
||||
return webPushFailure("CONTRACT_REJECTED", "PUSH_DECODE");
|
||||
}
|
||||
if (isRecord(value) && Object.hasOwn(value, "web_push")) {
|
||||
@@ -189,101 +196,3 @@ export function clickDataFromHint(
|
||||
expiresAt: hint.expiresAt,
|
||||
});
|
||||
}
|
||||
|
||||
function hasDuplicateTopLevelJsonKeys(source: string): boolean {
|
||||
let cursor = skipWhitespace(source, 0);
|
||||
if (source[cursor] !== "{") return false;
|
||||
cursor = skipWhitespace(source, cursor + 1);
|
||||
const keys = new Set<string>();
|
||||
while (cursor < source.length && source[cursor] !== "}") {
|
||||
if (source[cursor] !== "\"") return false;
|
||||
const keyEnd = jsonStringEnd(source, cursor);
|
||||
if (keyEnd === null) return false;
|
||||
let key: unknown;
|
||||
try {
|
||||
key = JSON.parse(source.slice(cursor, keyEnd));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
if (typeof key !== "string") return false;
|
||||
if (keys.has(key)) return true;
|
||||
keys.add(key);
|
||||
cursor = skipWhitespace(source, keyEnd);
|
||||
if (source[cursor] !== ":") return false;
|
||||
const valueEnd = jsonValueEnd(source, cursor + 1);
|
||||
if (valueEnd === null) return false;
|
||||
cursor = skipWhitespace(source, valueEnd);
|
||||
if (source[cursor] === ",") {
|
||||
cursor = skipWhitespace(source, cursor + 1);
|
||||
continue;
|
||||
}
|
||||
if (source[cursor] !== "}") return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function jsonStringEnd(
|
||||
source: string,
|
||||
start: number,
|
||||
): number | null {
|
||||
let escaped = false;
|
||||
for (let cursor = start + 1; cursor < source.length; cursor += 1) {
|
||||
const character = source[cursor];
|
||||
if (escaped) {
|
||||
escaped = false;
|
||||
} else if (character === "\\") {
|
||||
escaped = true;
|
||||
} else if (character === "\"") {
|
||||
return cursor + 1;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function jsonValueEnd(
|
||||
source: string,
|
||||
start: number,
|
||||
): number | null {
|
||||
let cursor = skipWhitespace(source, start);
|
||||
let depth = 0;
|
||||
let inString = false;
|
||||
let escaped = false;
|
||||
for (; cursor < source.length; cursor += 1) {
|
||||
const character = source[cursor];
|
||||
if (inString) {
|
||||
if (escaped) {
|
||||
escaped = false;
|
||||
} else if (character === "\\") {
|
||||
escaped = true;
|
||||
} else if (character === "\"") {
|
||||
inString = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (character === "\"") {
|
||||
inString = true;
|
||||
} else if (character === "{" || character === "[") {
|
||||
depth += 1;
|
||||
} else if (character === "}" || character === "]") {
|
||||
if (depth === 0) return cursor;
|
||||
depth -= 1;
|
||||
} else if (character === "," && depth === 0) {
|
||||
return cursor;
|
||||
}
|
||||
}
|
||||
return inString || depth !== 0 ? null : cursor;
|
||||
}
|
||||
|
||||
function skipWhitespace(source: string, start: number): number {
|
||||
let cursor = start;
|
||||
while (
|
||||
cursor < source.length &&
|
||||
(source[cursor] === " " ||
|
||||
source[cursor] === "\n" ||
|
||||
source[cursor] === "\r" ||
|
||||
source[cursor] === "\t")
|
||||
) {
|
||||
cursor += 1;
|
||||
}
|
||||
return cursor;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user