chore: sync the frontend template from ebea300 to af3d8e6
Carries 359 template commits, tag v0.2.0 plus one documentation commit: the generator-built feature index with its app, bounded-context and feature declarations, the src/platform kernel and scale tooling, the 2026-09-24 src review fixes, and the 2026-09-25 maturity review round with its CHANGELOG [보안]/[계약] rows. 691 of the 824 template paths are applied, 85 of them merged with product changes; 127 are not carried. Product identity is unchanged: name, version 0.1.0, routes, design and the TechLog domain. Three decisions worth naming. TechLog stays composed directly, as the last two syncs recorded; its directory carries a NOT_INSTALLED marker that says so, and every installed-* composer takes the template's projection with TechLog added on top. The reference sample is no longer installed: the template now installs a feature only through feature-module.ts and context-routes.ts, which load the screens this product deleted, so its three operations, six schemas and one topic leave the registries, recorded as registry change evidence. And AppRouter mounts the platform and TechLog routes only; the template's bounded-context route gateway waits for a generator-installed feature. Required fixes: readSubject on the BFF session owner (without it every HTTP Studio session would close as integration-failed), zod through the facade in nine TechLog files, noUnusedLocals, and public/config.json written out to match the local profile. Derived baselines were recomputed after reproducing both sides' values: CI contract 101 commands / 106 references / 141 artifacts, gate shape c9adcf85; registry approval 2dc6c3a3; the runtime config schema baseline equals the schema at8210d48. Verified here: types, lint, architecture, adapter inventory, CI contract, i18n, design system, diagnostics, browser security, dev release manifest, TechLog contract and build pass; 220 component, 155 integration, 15 contract, 418 TechLog, 43 reference-feature, 18 recipe and 46 release-coherence tests pass, and 2381 of 2383 unit tests. Reproduced at8210d48first and unchanged: check:registries (byte-identical), test:runtime-schema, verify:documentation. Brought by the template and escalated rather than suppressed: check:request-fanout on TechLog's cursor pagination, check:remediation-ledger (fails upstream too), the shared-chunk hash test, and the registry-dependent sample removal and second-feature tests. Playwright suites were not run. Full record, the five open questions and the follow-ups in docs/operations/template-sync-2026-09-29.md. (cherry picked from commit b2170cf265dea45dc9cc05d3c61e627e97f31cdb)
This commit is contained in:
@@ -139,6 +139,65 @@ async function flush(): Promise<void> {
|
||||
}
|
||||
|
||||
describe("bounded poll coordinator", () => {
|
||||
it("observes lease start, each attempt and the stop reason", async () => {
|
||||
const clock = new ManualClock();
|
||||
const results: BoundedPollAttemptResult<string>[] = [
|
||||
{ ok: true, value: { kind: "UNCHANGED", responseBytes: 0 } },
|
||||
{
|
||||
ok: false,
|
||||
error: {
|
||||
kind: "RATE_LIMITED",
|
||||
operation: "POLL",
|
||||
retryable: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
ok: true,
|
||||
value: {
|
||||
kind: "VALUE",
|
||||
value: "done",
|
||||
responseBytes: 8,
|
||||
state: "COMPLETED",
|
||||
},
|
||||
},
|
||||
];
|
||||
const observations: unknown[] = [];
|
||||
const coordinator = createBoundedPollCoordinator({
|
||||
policy,
|
||||
operation,
|
||||
execute: async () => results.shift()!,
|
||||
environment: new MutableEnvironment(),
|
||||
clock,
|
||||
random: () => 0.5,
|
||||
observe: (observation) => {
|
||||
observations.push(observation);
|
||||
throw new Error("sink failure must not change the lease");
|
||||
},
|
||||
});
|
||||
|
||||
let settled = false;
|
||||
const running = coordinator.run();
|
||||
void running.then(() => {
|
||||
settled = true;
|
||||
});
|
||||
for (let step = 0; step < 50 && !settled; step += 1) {
|
||||
clock.advance(1_000);
|
||||
await flush();
|
||||
}
|
||||
|
||||
await expect(running).resolves.toMatchObject({
|
||||
ok: true,
|
||||
value: { kind: "TERMINAL", attempts: 3 },
|
||||
});
|
||||
expect(observations).toEqual([
|
||||
{ kind: "LEASE_STARTED" },
|
||||
{ kind: "ATTEMPT_FINISHED", outcome: "UNCHANGED" },
|
||||
{ kind: "ATTEMPT_FINISHED", outcome: "RATE_LIMITED" },
|
||||
{ kind: "ATTEMPT_FINISHED", outcome: "VALUE" },
|
||||
{ kind: "LEASE_STOPPED", outcome: "TERMINAL" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("chains completed attempts without overlap and stops at a terminal state", async () => {
|
||||
const clock = new ManualClock();
|
||||
const environment = new MutableEnvironment();
|
||||
@@ -562,17 +621,27 @@ describe("bounded poll coordinator", () => {
|
||||
});
|
||||
|
||||
it.each(["RATE_LIMITED", "PROVIDER_UNAVAILABLE"] as const)(
|
||||
"does not retry %s without a bounded server hint",
|
||||
"retries retryable %s without a server hint on policy backoff",
|
||||
async (kind) => {
|
||||
const clock = new ManualClock();
|
||||
const execute = vi.fn().mockResolvedValue({
|
||||
ok: false,
|
||||
error: {
|
||||
kind,
|
||||
operation: "POLL",
|
||||
retryable: true,
|
||||
},
|
||||
});
|
||||
const execute = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
error: {
|
||||
kind,
|
||||
operation: "POLL",
|
||||
retryable: true,
|
||||
},
|
||||
})
|
||||
.mockResolvedValue({
|
||||
ok: false,
|
||||
error: {
|
||||
kind,
|
||||
operation: "POLL",
|
||||
retryable: false,
|
||||
},
|
||||
});
|
||||
const coordinator = createBoundedPollCoordinator<string>({
|
||||
policy,
|
||||
operation,
|
||||
@@ -585,6 +654,10 @@ describe("bounded poll coordinator", () => {
|
||||
const pending = coordinator.run();
|
||||
clock.advance(5_000);
|
||||
await flush();
|
||||
expect(execute).toHaveBeenCalledTimes(1);
|
||||
clock.advance(5_000);
|
||||
await flush();
|
||||
|
||||
await expect(pending).resolves.toEqual({
|
||||
ok: false,
|
||||
error: {
|
||||
@@ -593,7 +666,7 @@ describe("bounded poll coordinator", () => {
|
||||
retryable: false,
|
||||
},
|
||||
});
|
||||
expect(execute).toHaveBeenCalledTimes(1);
|
||||
expect(execute).toHaveBeenCalledTimes(2);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -821,6 +894,78 @@ describe("bounded poll coordinator", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("ends a run whose executor throws with a retryable OFFLINE", async () => {
|
||||
const clock = new ManualClock();
|
||||
const coordinator = createBoundedPollCoordinator<string>({
|
||||
policy,
|
||||
operation,
|
||||
environment: new MutableEnvironment(),
|
||||
clock,
|
||||
execute: async () => {
|
||||
throw new TypeError("Failed to fetch");
|
||||
},
|
||||
});
|
||||
const running = coordinator.run();
|
||||
clock.advance(5_000);
|
||||
await expect(running).resolves.toEqual({
|
||||
ok: false,
|
||||
error: { kind: "OFFLINE", operation: "POLL", retryable: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("reads attempt results with the shared realtime snapshot rules", async () => {
|
||||
const runWith = async (result: unknown) => {
|
||||
const clock = new ManualClock();
|
||||
const coordinator = createBoundedPollCoordinator<string>({
|
||||
policy,
|
||||
operation,
|
||||
environment: new MutableEnvironment(),
|
||||
clock,
|
||||
execute: async () => result as BoundedPollAttemptResult<string>,
|
||||
});
|
||||
const running = coordinator.run();
|
||||
clock.advance(5_000);
|
||||
return running;
|
||||
};
|
||||
const terminalValue = () => ({
|
||||
kind: "VALUE",
|
||||
value: "done",
|
||||
responseBytes: 1,
|
||||
state: "COMPLETED",
|
||||
});
|
||||
|
||||
// A hidden (non-enumerable) field is not plain data: rejected.
|
||||
await expect(
|
||||
runWith(
|
||||
Object.defineProperties(
|
||||
{},
|
||||
{
|
||||
ok: { enumerable: false, value: true },
|
||||
value: { enumerable: true, value: terminalValue() },
|
||||
},
|
||||
),
|
||||
),
|
||||
).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "PROTOCOL_MISMATCH" },
|
||||
});
|
||||
// A null-prototype record is plain data: accepted.
|
||||
await expect(
|
||||
runWith(
|
||||
Object.assign(Object.create(null) as object, {
|
||||
ok: true,
|
||||
value: Object.assign(
|
||||
Object.create(null) as object,
|
||||
terminalValue(),
|
||||
),
|
||||
}),
|
||||
),
|
||||
).resolves.toMatchObject({
|
||||
ok: true,
|
||||
value: { kind: "TERMINAL", value: "done" },
|
||||
});
|
||||
});
|
||||
|
||||
it("fails closed on response ceilings, concurrent runs and close", async () => {
|
||||
const clock = new ManualClock();
|
||||
const coordinator = createBoundedPollCoordinator<string>({
|
||||
|
||||
@@ -10,8 +10,10 @@ import {
|
||||
} from "../../../src/contracts/realtime-events.ts";
|
||||
import {
|
||||
isValidatedRealtimeEventDto,
|
||||
realtimeDecodeFailureStream,
|
||||
} from "../../../src/adapters/realtime/event-codec.ts";
|
||||
import {
|
||||
STREAM_ID,
|
||||
TEST_LIMITS,
|
||||
createTestRealtimeCodec,
|
||||
createTestRealtimeRegistry,
|
||||
@@ -59,28 +61,31 @@ describe("REALTIME_EVENT_V1 codec", () => {
|
||||
ok: false,
|
||||
error: { kind: "MALFORMED_EVENT", operation: "DECODE" },
|
||||
});
|
||||
expect(
|
||||
codec.decode(
|
||||
realtimeEventJson({ streamId: "UNKNOWN_STREAM" }),
|
||||
),
|
||||
).toMatchObject({
|
||||
const unknownStream = codec.decode(
|
||||
realtimeEventJson({ streamId: "UNKNOWN_STREAM" }),
|
||||
);
|
||||
expect(unknownStream).toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "MALFORMED_EVENT" },
|
||||
});
|
||||
expect(
|
||||
codec.decode(
|
||||
realtimeEventJson({ eventType: "UNKNOWN_EVENT" }),
|
||||
),
|
||||
).toMatchObject({
|
||||
expect(realtimeDecodeFailureStream(unknownStream)).toBeNull();
|
||||
// A type added by a newer backend is a version mismatch, not corruption.
|
||||
const unknownType = codec.decode(
|
||||
realtimeEventJson({ eventType: "UNKNOWN_EVENT" }),
|
||||
);
|
||||
expect(unknownType).toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "MALFORMED_EVENT" },
|
||||
error: { kind: "PROTOCOL_MISMATCH", operation: "REGISTRY" },
|
||||
});
|
||||
expect(
|
||||
codec.decode(realtimeEventJson({ payload: { value: 42 } })),
|
||||
).toMatchObject({
|
||||
expect(realtimeDecodeFailureStream(unknownType)).toBe(STREAM_ID);
|
||||
const badPayload = codec.decode(
|
||||
realtimeEventJson({ payload: { value: 42 } }),
|
||||
);
|
||||
expect(badPayload).toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "MALFORMED_EVENT" },
|
||||
});
|
||||
expect(realtimeDecodeFailureStream(badPayload)).toBe(STREAM_ID);
|
||||
|
||||
const duplicateEnvelopeMember =
|
||||
`{"eventId":"shadowed",${JSON.stringify(
|
||||
@@ -227,6 +232,24 @@ describe("REALTIME_EVENT_V1 codec", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps a fixed-size semantic fingerprint however large the payload", () => {
|
||||
const codec = createTestRealtimeCodec();
|
||||
const small = codec.decode(realtimeEventJson());
|
||||
const large = codec.decode(
|
||||
realtimeEventJson({ payload: { value: "x".repeat(3_000) } }),
|
||||
);
|
||||
|
||||
expect(small.ok && large.ok).toBe(true);
|
||||
if (!small.ok || !large.ok) return;
|
||||
expect(large.value.fingerprintBytes).toBe(
|
||||
small.value.fingerprintBytes,
|
||||
);
|
||||
expect(large.value.fingerprintBytes).toBeLessThanOrEqual(32);
|
||||
expect(large.value.semanticFingerprint).not.toBe(
|
||||
small.value.semanticFingerprint,
|
||||
);
|
||||
});
|
||||
|
||||
it("uses bounded uint64 sequence and header-safe cursor helpers", () => {
|
||||
expect(isCanonicalRealtimeSequence("0")).toBe(true);
|
||||
expect(isCanonicalRealtimeSequence(REALTIME_MAX_SEQUENCE)).toBe(true);
|
||||
|
||||
@@ -18,9 +18,20 @@ import {
|
||||
realtimeFailure,
|
||||
realtimeSuccess,
|
||||
} from "../../../src/adapters/realtime/result.ts";
|
||||
import type {
|
||||
RealtimeStreamCoordinator,
|
||||
import {
|
||||
createRealtimeStreamCoordinator,
|
||||
type RealtimeStreamCoordinator,
|
||||
} from "../../../src/adapters/realtime/stream-coordinator.ts";
|
||||
import {
|
||||
createFetchSseConnection,
|
||||
} from "../../../src/adapters/realtime/sse/fetch-sse-connection.ts";
|
||||
import {
|
||||
STREAM_ID,
|
||||
TEST_MAPPERS,
|
||||
createTestRealtimeCodec,
|
||||
createTestRealtimeRegistry,
|
||||
realtimeEventJson,
|
||||
} from "./fixture.ts";
|
||||
|
||||
function setup(
|
||||
recoveryMode:
|
||||
@@ -209,3 +220,182 @@ describe("realtime transport event consumer", () => {
|
||||
expect(runtime.accept).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("SSE transport, consumer and stream coordinator together", () => {
|
||||
it("keeps reading after a SNAPSHOT_ONLY recovery that needs no transport barrier", async () => {
|
||||
const registry = createTestRealtimeRegistry({
|
||||
recovery: {
|
||||
mode: "SNAPSHOT_ONLY",
|
||||
snapshotOperationId: "GET_REFERENCE_REALTIME_SNAPSHOT",
|
||||
checkpointCodecId: "ReferenceRealtimeCheckpoint",
|
||||
barrier: "NONE",
|
||||
},
|
||||
delivery: "INVALIDATION_HINT",
|
||||
stateBearing: false,
|
||||
});
|
||||
const snapshots = ["0", "3"];
|
||||
const recoveries: string[] = [];
|
||||
const apply = vi.fn(async () => realtimeSuccess(undefined));
|
||||
const coordinator = createRealtimeStreamCoordinator({
|
||||
registry,
|
||||
mappers: TEST_MAPPERS,
|
||||
authority: {
|
||||
effects: { apply },
|
||||
recovery: {
|
||||
async recover(request) {
|
||||
recoveries.push(request.reason);
|
||||
const sequence = snapshots.shift() ?? "0";
|
||||
return realtimeSuccess({
|
||||
kind: "SNAPSHOT_RESET",
|
||||
checkpoint: {
|
||||
recoveryMode: "SNAPSHOT_ONLY",
|
||||
streamEpoch: "stream-epoch-0001",
|
||||
lastAppliedSequence: sequence,
|
||||
resumeCursor: null,
|
||||
snapshotRevision: `snapshot-revision-${sequence}`,
|
||||
},
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
scope: {
|
||||
generation: 7,
|
||||
scopeBinding: "scope-binding-0001",
|
||||
isCurrent: () => true,
|
||||
},
|
||||
now: () => 10_000,
|
||||
});
|
||||
const consumer = createRealtimeEventConsumer({
|
||||
codec: createTestRealtimeCodec(registry),
|
||||
coordinator,
|
||||
});
|
||||
await expect(
|
||||
coordinator.recover(STREAM_ID, "INITIALIZE"),
|
||||
).resolves.toMatchObject({ ok: true });
|
||||
|
||||
const frame = (sequence: string) =>
|
||||
`data: ${realtimeEventJson({
|
||||
eventId: `event-0000000${sequence}`,
|
||||
sequence,
|
||||
recoveryMode: "SNAPSHOT_ONLY",
|
||||
resumeCursor: null,
|
||||
})}\n\n`;
|
||||
const body = [frame("1"), frame("3"), frame("4")].join("");
|
||||
const connection = createFetchSseConnection({
|
||||
endpoint: "https://app.example.test/events",
|
||||
applicationOrigin: "https://app.example.test",
|
||||
recoveryMode: "SNAPSHOT_ONLY",
|
||||
fetcher: async () =>
|
||||
new Response(body, {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "text/event-stream" },
|
||||
}),
|
||||
});
|
||||
|
||||
// Sequence 3 is a gap: the coordinator recovers to snapshot 3, and the
|
||||
// stream must keep reading so sequence 4 still applies.
|
||||
const closed = await connection.read({
|
||||
resumeCursor: null,
|
||||
onEvent: (event, signal) =>
|
||||
consumer.consumeForTransport(
|
||||
event.data,
|
||||
{ kind: "SSE_NO_CURSOR" },
|
||||
signal,
|
||||
),
|
||||
});
|
||||
|
||||
expect(closed).toMatchObject({ ok: true, value: { kind: "EOF" } });
|
||||
expect(recoveries).toEqual(["INITIALIZE", "SEQUENCE_GAP"]);
|
||||
expect(apply).toHaveBeenCalledTimes(2);
|
||||
expect(coordinator.getResumeState(STREAM_ID)).toMatchObject({
|
||||
lastAppliedSequence: "4",
|
||||
});
|
||||
expect(coordinator.inspect(STREAM_ID).awaitingTransportBarrier).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
it("ends on an unregistered event type as a registry mismatch and leaves freshness UNKNOWN", async () => {
|
||||
const registry = createTestRealtimeRegistry({
|
||||
recovery: {
|
||||
mode: "SNAPSHOT_ONLY",
|
||||
snapshotOperationId: "GET_REFERENCE_REALTIME_SNAPSHOT",
|
||||
checkpointCodecId: "ReferenceRealtimeCheckpoint",
|
||||
barrier: "NONE",
|
||||
},
|
||||
delivery: "INVALIDATION_HINT",
|
||||
stateBearing: false,
|
||||
});
|
||||
const apply = vi.fn(async () => realtimeSuccess(undefined));
|
||||
const coordinator = createRealtimeStreamCoordinator({
|
||||
registry,
|
||||
mappers: TEST_MAPPERS,
|
||||
authority: {
|
||||
effects: { apply },
|
||||
recovery: {
|
||||
recover: async () =>
|
||||
realtimeSuccess({
|
||||
kind: "SNAPSHOT_RESET",
|
||||
checkpoint: {
|
||||
recoveryMode: "SNAPSHOT_ONLY",
|
||||
streamEpoch: "stream-epoch-0001",
|
||||
lastAppliedSequence: "0",
|
||||
resumeCursor: null,
|
||||
snapshotRevision: "snapshot-revision-0",
|
||||
},
|
||||
}),
|
||||
},
|
||||
},
|
||||
scope: {
|
||||
generation: 7,
|
||||
scopeBinding: "scope-binding-0001",
|
||||
isCurrent: () => true,
|
||||
},
|
||||
now: () => 10_000,
|
||||
});
|
||||
const consumer = createRealtimeEventConsumer({
|
||||
codec: createTestRealtimeCodec(registry),
|
||||
coordinator,
|
||||
});
|
||||
await expect(
|
||||
coordinator.recover(STREAM_ID, "INITIALIZE"),
|
||||
).resolves.toMatchObject({ ok: true });
|
||||
|
||||
// A newer backend added an event type this bundle does not know.
|
||||
const frame = (sequence: string, eventType?: string) =>
|
||||
`data: ${realtimeEventJson({
|
||||
eventId: `event-0000000${sequence}`,
|
||||
sequence,
|
||||
recoveryMode: "SNAPSHOT_ONLY",
|
||||
resumeCursor: null,
|
||||
...(eventType === undefined ? {} : { eventType }),
|
||||
})}\n\n`;
|
||||
const body = frame("1") + frame("2", "ADDED_BY_NEWER_BACKEND");
|
||||
const connection = createFetchSseConnection({
|
||||
endpoint: "https://app.example.test/events",
|
||||
applicationOrigin: "https://app.example.test",
|
||||
recoveryMode: "SNAPSHOT_ONLY",
|
||||
fetcher: async () =>
|
||||
new Response(body, {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "text/event-stream" },
|
||||
}),
|
||||
});
|
||||
|
||||
const closed = await connection.read({
|
||||
resumeCursor: null,
|
||||
onEvent: (event, signal) =>
|
||||
consumer.consumeForTransport(
|
||||
event.data,
|
||||
{ kind: "SSE_NO_CURSOR" },
|
||||
signal,
|
||||
),
|
||||
});
|
||||
|
||||
expect(apply).toHaveBeenCalledOnce();
|
||||
expect(closed).toEqual(
|
||||
realtimeFailure("PROTOCOL_MISMATCH", "REGISTRY", false),
|
||||
);
|
||||
expect(coordinator.inspect(STREAM_ID).freshness).toBe("UNKNOWN");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
realtimeTransportRecoveryCommitted,
|
||||
type RealtimeRecoveryCheckpoint,
|
||||
} from "../../../src/application/ports/realtime/event-authority.ts";
|
||||
import type { ClockPort } from "../../../src/application/ports/clock-port.ts";
|
||||
import type { RealtimeResult } from "../../../src/application/ports/realtime/shared.ts";
|
||||
import type {
|
||||
StreamRegistrationId,
|
||||
@@ -56,6 +57,66 @@ function eventStream(
|
||||
);
|
||||
}
|
||||
|
||||
type Sleeper = { dueAt: number; resolve(): void };
|
||||
|
||||
class ManualClock implements ClockPort {
|
||||
current = 0;
|
||||
private sleepers: Sleeper[] = [];
|
||||
|
||||
now(): number {
|
||||
return this.current;
|
||||
}
|
||||
|
||||
sleep(milliseconds: number, signal?: AbortSignal): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (signal?.aborted) {
|
||||
reject(new Error("Aborted."));
|
||||
return;
|
||||
}
|
||||
const sleeper = { dueAt: this.current + milliseconds, resolve };
|
||||
signal?.addEventListener(
|
||||
"abort",
|
||||
() => {
|
||||
this.sleepers = this.sleepers.filter((entry) => entry !== sleeper);
|
||||
reject(new Error("Aborted."));
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
this.sleepers.push(sleeper);
|
||||
});
|
||||
}
|
||||
|
||||
advance(milliseconds: number): void {
|
||||
this.current += milliseconds;
|
||||
const ready = this.sleepers.filter(
|
||||
(sleeper) => sleeper.dueAt <= this.current,
|
||||
);
|
||||
this.sleepers = this.sleepers.filter(
|
||||
(sleeper) => sleeper.dueAt > this.current,
|
||||
);
|
||||
for (const sleeper of ready) sleeper.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
async function settle(): Promise<void> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
|
||||
function openStream() {
|
||||
let push: ((text: string) => void) | undefined;
|
||||
const cancel = vi.fn();
|
||||
const response = new Response(
|
||||
new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
push = (text) => controller.enqueue(encoder.encode(text));
|
||||
},
|
||||
cancel,
|
||||
}),
|
||||
{ headers: { "Content-Type": "text/event-stream" } },
|
||||
);
|
||||
return { response, cancel, push: (text: string) => push?.(text) };
|
||||
}
|
||||
|
||||
function connection(
|
||||
fetcher: typeof fetch,
|
||||
recoveryMode: "CURSOR" | "SESSION_REBUILD" | "SNAPSHOT_ONLY" =
|
||||
@@ -526,6 +587,123 @@ describe("fetch SSE connection", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("dispatches a single chunk holding more events than the per-chunk item cap", async () => {
|
||||
const chunk = Array.from(
|
||||
{ length: 300 },
|
||||
(_, index) =>
|
||||
`id: cursor-${index}\ndata: ${"x".repeat(300)}\n\n`,
|
||||
).join("");
|
||||
const runtime = connection(
|
||||
(async () => eventStream([chunk])) as typeof fetch,
|
||||
);
|
||||
const received: string[] = [];
|
||||
await expect(
|
||||
runtime.read({
|
||||
resumeCursor: null,
|
||||
onEvent: (event) => {
|
||||
received.push(event.id ?? "");
|
||||
return realtimeSuccess(SSE_CONTINUE);
|
||||
},
|
||||
}),
|
||||
).resolves.toMatchObject({ ok: true, value: { kind: "EOF" } });
|
||||
expect(received).toHaveLength(300);
|
||||
expect(received[299]).toBe("cursor-299");
|
||||
});
|
||||
|
||||
it("dispatches every normal event from a 1MB reader chunk", async () => {
|
||||
// The browser, not the server, picks the chunk size.
|
||||
const chunk = Array.from(
|
||||
{ length: 2_000 },
|
||||
(_, index) =>
|
||||
`id: cursor-${index}\ndata: ${"é".repeat(250)}\n\n`,
|
||||
).join("");
|
||||
expect(encoder.encode(chunk).byteLength).toBeGreaterThan(1_000_000);
|
||||
const runtime = connection(
|
||||
(async () => eventStream([chunk])) as typeof fetch,
|
||||
);
|
||||
const received: string[] = [];
|
||||
await expect(
|
||||
runtime.read({
|
||||
resumeCursor: null,
|
||||
onEvent: (event) => {
|
||||
received.push(event.id ?? "");
|
||||
return realtimeSuccess(SSE_CONTINUE);
|
||||
},
|
||||
}),
|
||||
).resolves.toMatchObject({ ok: true, value: { kind: "EOF" } });
|
||||
expect(received).toHaveLength(2_000);
|
||||
expect(received[1_999]).toBe("cursor-1999");
|
||||
});
|
||||
|
||||
it("ends a silent stream with a retryable IDLE_TIMEOUT and cancels the reader", async () => {
|
||||
const clock = new ManualClock();
|
||||
const stream = openStream();
|
||||
const runtime = createFetchSseConnection({
|
||||
endpoint: "https://app.example.test/events",
|
||||
applicationOrigin: "https://app.example.test",
|
||||
recoveryMode: "CURSOR",
|
||||
fetcher: (async () => stream.response) as typeof fetch,
|
||||
clock,
|
||||
idleTimeoutMs: 1_000,
|
||||
});
|
||||
let settled = false;
|
||||
const pending = runtime.read({
|
||||
resumeCursor: null,
|
||||
onEvent: continueEvent,
|
||||
});
|
||||
void pending.then(() => {
|
||||
settled = true;
|
||||
});
|
||||
await settle();
|
||||
|
||||
clock.advance(999);
|
||||
await settle();
|
||||
expect(settled).toBe(false);
|
||||
|
||||
clock.advance(1);
|
||||
await expect(pending).resolves.toEqual(
|
||||
realtimeFailure("IDLE_TIMEOUT", "RECEIVE", true),
|
||||
);
|
||||
expect(stream.cancel).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("restarts the idle deadline on every comment", async () => {
|
||||
const clock = new ManualClock();
|
||||
const stream = openStream();
|
||||
const comments = vi.fn();
|
||||
const runtime = createFetchSseConnection({
|
||||
endpoint: "https://app.example.test/events",
|
||||
applicationOrigin: "https://app.example.test",
|
||||
recoveryMode: "CURSOR",
|
||||
fetcher: (async () => stream.response) as typeof fetch,
|
||||
clock,
|
||||
idleTimeoutMs: 1_000,
|
||||
});
|
||||
let settled = false;
|
||||
const pending = runtime.read({
|
||||
resumeCursor: null,
|
||||
onEvent: continueEvent,
|
||||
onComment: comments,
|
||||
});
|
||||
void pending.then(() => {
|
||||
settled = true;
|
||||
});
|
||||
await settle();
|
||||
|
||||
for (let beat = 0; beat < 3; beat += 1) {
|
||||
clock.advance(900);
|
||||
stream.push(": heartbeat\n");
|
||||
await settle();
|
||||
}
|
||||
expect(comments).toHaveBeenCalledTimes(3);
|
||||
expect(settled).toBe(false);
|
||||
|
||||
clock.advance(1_000);
|
||||
await expect(pending).resolves.toEqual(
|
||||
realtimeFailure("IDLE_TIMEOUT", "RECEIVE", true),
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects arbitrary endpoints and mode-incoherent cursors before fetch", async () => {
|
||||
expect(() =>
|
||||
createFetchSseConnection({
|
||||
|
||||
@@ -426,6 +426,41 @@ describe("live/poll authoritative writer handoff", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("bounds the probe by what is still buffered, not by all it ever accepted", async () => {
|
||||
const firstLive = deferred<RealtimeResult<void>>();
|
||||
const order: string[] = [];
|
||||
const harness = createHarness({
|
||||
initial: "POLL",
|
||||
limits: { ...limits, maxProbeBufferedEvents: 2 },
|
||||
apply: vi.fn(async ({ value }) => {
|
||||
order.push(value);
|
||||
if (value === "live-1") await firstLive.promise;
|
||||
return realtimeSuccess(undefined);
|
||||
}),
|
||||
});
|
||||
const live = expectProbe(harness.coordinator.beginLiveProbe());
|
||||
await live.write("live-1", 3);
|
||||
await live.write("live-2", 3);
|
||||
|
||||
const activation = live.activate();
|
||||
// live-1 left the buffer for the drain, so one slot is free again.
|
||||
await vi.waitFor(() => expect(order).toEqual(["live-1"]));
|
||||
await expect(live.write("live-3", 3)).resolves.toMatchObject({
|
||||
ok: true,
|
||||
value: { kind: "BUFFERED" },
|
||||
});
|
||||
await expect(live.write("live-4", 3)).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "QUEUE_OVERFLOW" },
|
||||
});
|
||||
|
||||
// Overflow during activation still fails closed.
|
||||
firstLive.resolve(realtimeSuccess(undefined));
|
||||
await expect(activation).resolves.toMatchObject({ ok: false });
|
||||
expect(harness.coordinator.inspect().state).toBe("CLOSED");
|
||||
expect(order).toEqual(["live-1"]);
|
||||
});
|
||||
|
||||
it("drops an overflowing probe without displacing poll and never reuses its generation", async () => {
|
||||
const harness = createHarness({
|
||||
initial: "POLL",
|
||||
|
||||
@@ -2,11 +2,11 @@ import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import type { ClockPort } from "../../../src/application/ports/clock-port.ts";
|
||||
import type {
|
||||
RealtimeFailureKind,
|
||||
RealtimeResult,
|
||||
} from "../../../src/application/ports/realtime/shared.ts";
|
||||
import type {
|
||||
RealtimeRecoveryCheckpoint,
|
||||
import {
|
||||
REALTIME_TRANSPORT_CONTINUE,
|
||||
type RealtimeRecoveryCheckpoint,
|
||||
} from "../../../src/application/ports/realtime/event-authority.ts";
|
||||
import {
|
||||
createRealtimeReconnectCoordinator,
|
||||
@@ -27,6 +27,19 @@ import {
|
||||
realtimeFailure,
|
||||
realtimeSuccess,
|
||||
} from "../../../src/adapters/realtime/result.ts";
|
||||
import {
|
||||
createFetchSseConnection,
|
||||
readSseForReconnect,
|
||||
SSE_CONTINUE,
|
||||
} from "../../../src/adapters/realtime/sse/fetch-sse-connection.ts";
|
||||
import {
|
||||
createWebSocketConnection,
|
||||
type WebSocketClosedReceipt,
|
||||
type WebSocketFacade,
|
||||
} from "../../../src/adapters/realtime/websocket/websocket-connection.ts";
|
||||
import {
|
||||
REALTIME_WEBSOCKET_PROTOCOL,
|
||||
} from "../../../src/adapters/realtime/websocket/websocket-protocol.ts";
|
||||
|
||||
type Sleeper = Readonly<{
|
||||
dueAt: number;
|
||||
@@ -221,7 +234,8 @@ function deferred<Value>(): Readonly<{
|
||||
}
|
||||
|
||||
async function flush(): Promise<void> {
|
||||
for (let turn = 0; turn < 8; turn += 1) {
|
||||
// Enough turns for a retry loop iteration: sleep, attempt and close races.
|
||||
for (let turn = 0; turn < 16; turn += 1) {
|
||||
await Promise.resolve();
|
||||
}
|
||||
}
|
||||
@@ -309,62 +323,78 @@ describe("realtime reconnect coordinator", () => {
|
||||
"RATE_LIMITED",
|
||||
"PROVIDER_UNAVAILABLE",
|
||||
] as const)(
|
||||
"does not reconnect an external %s attempt failure without a server hint",
|
||||
"reconnects a retryable external %s attempt failure without a server hint",
|
||||
async (kind) => {
|
||||
const clock = new ManualClock();
|
||||
const terminal = realtimeFailure(
|
||||
kind,
|
||||
"CONNECT",
|
||||
true,
|
||||
);
|
||||
const attempt = vi.fn(() => failed(terminal));
|
||||
const retryable = realtimeFailure(kind, "CONNECT", true);
|
||||
const terminal = realtimeFailure(kind, "CONNECT", false);
|
||||
let attempts = 0;
|
||||
const coordinator = createRealtimeReconnectCoordinator({
|
||||
policy: policy(),
|
||||
environment: new MutableOnlineEnvironment(true),
|
||||
clock,
|
||||
random: () => 0.5,
|
||||
confirmTransportBarrier: confirmationSucceeded,
|
||||
attempt,
|
||||
attempt: () =>
|
||||
failed(++attempts === 1 ? retryable : terminal),
|
||||
});
|
||||
|
||||
const result = await coordinator.run();
|
||||
const running = coordinator.run();
|
||||
await flush();
|
||||
expect(attempts).toBe(1);
|
||||
expect(clock.sleepCalls).toEqual([50]);
|
||||
|
||||
clock.advance(50);
|
||||
const result = await running;
|
||||
|
||||
expect(attempts).toBe(2);
|
||||
expect(result).toBe(terminal);
|
||||
expect(attempt).toHaveBeenCalledOnce();
|
||||
expect(clock.sleepCalls).toEqual([]);
|
||||
},
|
||||
);
|
||||
|
||||
it("does not reconnect an external close failure that requires but omits a server hint", async () => {
|
||||
it("reconnects a retryable external close failure without a server hint", async () => {
|
||||
const clock = new ManualClock();
|
||||
const terminal = realtimeFailure(
|
||||
const retryable = realtimeFailure(
|
||||
"PROVIDER_UNAVAILABLE",
|
||||
"RECEIVE",
|
||||
true,
|
||||
);
|
||||
const attempt = vi.fn(() =>
|
||||
opened(
|
||||
Object.freeze({
|
||||
classifyClosed: (
|
||||
receipt: RealtimeReconnectOutcome<void>,
|
||||
) => receipt,
|
||||
close: vi.fn(),
|
||||
waitClosed: () => failed(terminal),
|
||||
}),
|
||||
),
|
||||
const terminal = realtimeFailure(
|
||||
"PROVIDER_UNAVAILABLE",
|
||||
"RECEIVE",
|
||||
false,
|
||||
);
|
||||
let attempts = 0;
|
||||
const coordinator = createRealtimeReconnectCoordinator({
|
||||
policy: policy(),
|
||||
environment: new MutableOnlineEnvironment(true),
|
||||
clock,
|
||||
random: () => 0.5,
|
||||
confirmTransportBarrier: confirmationSucceeded,
|
||||
attempt,
|
||||
attempt: () => {
|
||||
const closedWith = ++attempts === 1 ? retryable : terminal;
|
||||
return opened(
|
||||
Object.freeze({
|
||||
classifyClosed: (
|
||||
receipt: RealtimeReconnectOutcome<void>,
|
||||
) => receipt,
|
||||
close: vi.fn(),
|
||||
waitClosed: () => failed(closedWith),
|
||||
}),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const result = await coordinator.run();
|
||||
const running = coordinator.run();
|
||||
await flush();
|
||||
expect(attempts).toBe(1);
|
||||
expect(clock.sleepCalls).toEqual([50]);
|
||||
|
||||
clock.advance(50);
|
||||
const result = await running;
|
||||
|
||||
expect(attempts).toBe(2);
|
||||
expect(result).toBe(terminal);
|
||||
expect(attempt).toHaveBeenCalledOnce();
|
||||
expect(clock.sleepCalls).toEqual([]);
|
||||
});
|
||||
|
||||
it("waits for an explicit online signal without a retry timer", async () => {
|
||||
@@ -404,8 +434,180 @@ describe("realtime reconnect coordinator", () => {
|
||||
expect(result).toBe(terminal);
|
||||
});
|
||||
|
||||
it("resets a spent retry only after policy eligibility", async () => {
|
||||
const runScenario = async (markValid: boolean) => {
|
||||
it("does not spend the elapsed budget while waiting offline", async () => {
|
||||
const clock = new ManualClock();
|
||||
const environment = new MutableOnlineEnvironment(false);
|
||||
const terminal = realtimeFailure(
|
||||
"FORBIDDEN",
|
||||
"CONNECT",
|
||||
false,
|
||||
);
|
||||
const attempt = vi.fn(() => failed(terminal));
|
||||
const coordinator = createRealtimeReconnectCoordinator({
|
||||
policy: policy(),
|
||||
environment,
|
||||
clock,
|
||||
random: () => 0.5,
|
||||
confirmTransportBarrier: confirmationSucceeded,
|
||||
attempt,
|
||||
});
|
||||
|
||||
const running = coordinator.run();
|
||||
await flush();
|
||||
// Offline for longer than maxElapsedMs before the first attempt.
|
||||
clock.advance(6_000);
|
||||
environment.emit(true);
|
||||
await flushUntil(() => clock.sleepCalls.length === 1);
|
||||
expect(clock.sleepCalls).toEqual([50]);
|
||||
|
||||
clock.advance(50);
|
||||
const result = await running;
|
||||
expect(attempt).toHaveBeenCalledTimes(1);
|
||||
expect(result).toBe(terminal);
|
||||
});
|
||||
|
||||
it.each(["an offline blip", "a waitClosed throw"] as const)(
|
||||
"reconnects after %s ends a healthy session older than the elapsed budget",
|
||||
async (ending) => {
|
||||
const clock = new ManualClock();
|
||||
const environment = new MutableOnlineEnvironment(true);
|
||||
const closing = deferred<RealtimeReconnectOutcome<void>>();
|
||||
const terminal = realtimeFailure(
|
||||
"FORBIDDEN",
|
||||
"CONNECT",
|
||||
false,
|
||||
);
|
||||
let attempts = 0;
|
||||
const coordinator = createRealtimeReconnectCoordinator({
|
||||
policy: policy(),
|
||||
environment,
|
||||
clock,
|
||||
random: () => 0.5,
|
||||
confirmTransportBarrier: confirmationSucceeded,
|
||||
attempt: (context: RealtimeReconnectAttemptContext) => {
|
||||
attempts += 1;
|
||||
if (attempts > 1) return failed(terminal);
|
||||
context.markValidHeartbeatOrEvent();
|
||||
return opened<RealtimeReconnectOutcome<void>>(
|
||||
Object.freeze({
|
||||
classifyClosed: (
|
||||
receipt: RealtimeReconnectOutcome<void>,
|
||||
) => receipt,
|
||||
close: () =>
|
||||
closing.resolve(
|
||||
failed(
|
||||
realtimeFailure("OFFLINE", "RECEIVE", true),
|
||||
),
|
||||
),
|
||||
waitClosed: () =>
|
||||
ending === "an offline blip"
|
||||
? closing.promise
|
||||
: closing.promise.then(() => {
|
||||
throw new Error("transport failed");
|
||||
}),
|
||||
}),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const running = coordinator.run();
|
||||
await flushUntil(() => attempts === 1);
|
||||
await flush();
|
||||
// Healthy for twice maxElapsedMs, then a one second outage.
|
||||
clock.advance(10_000);
|
||||
if (ending === "an offline blip") {
|
||||
environment.emit(false);
|
||||
await flush();
|
||||
clock.advance(1_000);
|
||||
environment.emit(true);
|
||||
} else {
|
||||
closing.resolve(closedSuccessfully());
|
||||
}
|
||||
await flushUntil(() => clock.sleepCalls.length === 1);
|
||||
expect(clock.sleepCalls).toEqual([50]);
|
||||
|
||||
clock.advance(50);
|
||||
const result = await running;
|
||||
expect(attempts).toBe(2);
|
||||
expect(result).toBe(terminal);
|
||||
},
|
||||
);
|
||||
|
||||
it("observes attempts, retries, the session and why the run ended", async () => {
|
||||
const clock = new ManualClock();
|
||||
const terminal = realtimeFailure("FORBIDDEN", "RECEIVE", false);
|
||||
const observations: unknown[] = [];
|
||||
let attempts = 0;
|
||||
const coordinator = createRealtimeReconnectCoordinator({
|
||||
policy: policy(),
|
||||
environment: new MutableOnlineEnvironment(true),
|
||||
clock,
|
||||
random: () => 0.5,
|
||||
confirmTransportBarrier: confirmationSucceeded,
|
||||
observe: (observation) => {
|
||||
observations.push(observation);
|
||||
throw new Error("sink failure must not change the run");
|
||||
},
|
||||
attempt: () =>
|
||||
++attempts === 1
|
||||
? failed(realtimeFailure("CONNECT_TIMEOUT", "CONNECT", true))
|
||||
: opened(
|
||||
Object.freeze({
|
||||
classifyClosed: (
|
||||
receipt: RealtimeReconnectOutcome<void>,
|
||||
) => receipt,
|
||||
close: vi.fn(),
|
||||
waitClosed: () => failed(terminal),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
const running = coordinator.run();
|
||||
await flushUntil(() => clock.sleepCalls.length === 1);
|
||||
clock.advance(50);
|
||||
await expect(running).resolves.toBe(terminal);
|
||||
expect(observations).toEqual([
|
||||
{ kind: "ATTEMPT_STARTED" },
|
||||
{
|
||||
kind: "RETRY_SCHEDULED",
|
||||
failureKind: "CONNECT_TIMEOUT",
|
||||
delayBucket: "UNDER_1S",
|
||||
},
|
||||
{ kind: "ATTEMPT_STARTED" },
|
||||
{ kind: "SESSION_OPENED" },
|
||||
{ kind: "SESSION_CLOSED" },
|
||||
{ kind: "RUN_ENDED", reason: "TERMINAL", failureKind: "FORBIDDEN" },
|
||||
]);
|
||||
|
||||
// A second run that spends its retries ends as EXHAUSTED, not TERMINAL.
|
||||
const exhausted = realtimeFailure("PROVIDER_UNAVAILABLE", "CONNECT", true);
|
||||
const ends: unknown[] = [];
|
||||
const spending = createRealtimeReconnectCoordinator({
|
||||
policy: policy({ maxAttempts: 1 }),
|
||||
environment: new MutableOnlineEnvironment(true),
|
||||
clock,
|
||||
random: () => 0,
|
||||
confirmTransportBarrier: confirmationSucceeded,
|
||||
observe: (observation) => {
|
||||
if (observation.kind === "RUN_ENDED") ends.push(observation);
|
||||
},
|
||||
attempt: () => failed(exhausted),
|
||||
});
|
||||
const spent = spending.run();
|
||||
await flushUntil(() => clock.sleepCalls.length === 2);
|
||||
clock.advance(0);
|
||||
await expect(spent).resolves.toBe(exhausted);
|
||||
expect(ends).toEqual([
|
||||
{
|
||||
kind: "RUN_ENDED",
|
||||
reason: "EXHAUSTED",
|
||||
failureKind: "PROVIDER_UNAVAILABLE",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("resets a spent retry only after a valid signal and a stable open", async () => {
|
||||
const runScenario = async (markValid: boolean, openMs: number) => {
|
||||
const clock = new ManualClock();
|
||||
const firstFailure = realtimeFailure(
|
||||
"CONNECT_TIMEOUT",
|
||||
@@ -423,6 +625,7 @@ describe("realtime reconnect coordinator", () => {
|
||||
false,
|
||||
);
|
||||
const closeSession = vi.fn();
|
||||
const closing = deferred<RealtimeReconnectOutcome<void>>();
|
||||
let attempts = 0;
|
||||
const coordinator = createRealtimeReconnectCoordinator({
|
||||
policy: policy({ maxAttempts: 1 }),
|
||||
@@ -445,8 +648,7 @@ describe("realtime reconnect coordinator", () => {
|
||||
receipt: RealtimeReconnectOutcome<void>,
|
||||
) => receipt,
|
||||
close: closeSession,
|
||||
waitClosed: () =>
|
||||
failed(sessionFailure, 0),
|
||||
waitClosed: () => closing.promise,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -457,12 +659,12 @@ describe("realtime reconnect coordinator", () => {
|
||||
const running = coordinator.run();
|
||||
await flush();
|
||||
clock.advance(0);
|
||||
await flushUntil(() => attempts === 2);
|
||||
await flush();
|
||||
if (markValid) {
|
||||
await flushUntil(() => clock.sleepCalls.length === 2);
|
||||
expect(clock.sleepCalls).toEqual([0, 0]);
|
||||
clock.advance(0);
|
||||
}
|
||||
clock.advance(openMs);
|
||||
closing.resolve(failed(sessionFailure, 0));
|
||||
await flushUntil(() => clock.sleepCalls.length === 2);
|
||||
clock.advance(0);
|
||||
const result = await running;
|
||||
return {
|
||||
attempts,
|
||||
@@ -473,12 +675,20 @@ describe("realtime reconnect coordinator", () => {
|
||||
};
|
||||
};
|
||||
|
||||
const unstable = await runScenario(false);
|
||||
expect(unstable.attempts).toBe(2);
|
||||
expect(unstable.result).toBe(unstable.sessionFailure);
|
||||
expect(unstable.closeSession).toHaveBeenCalledOnce();
|
||||
// A heartbeat on a short-lived connection, or a quiet long one, keeps
|
||||
// the spent budget; only both together reset it.
|
||||
for (const [markValid, openMs] of [
|
||||
[false, 0],
|
||||
[true, 499],
|
||||
[false, 500],
|
||||
] as const) {
|
||||
const spent = await runScenario(markValid, openMs);
|
||||
expect(spent.attempts).toBe(2);
|
||||
expect(spent.result).toBe(spent.sessionFailure);
|
||||
expect(spent.closeSession).toHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
const eligible = await runScenario(true);
|
||||
const eligible = await runScenario(true, 500);
|
||||
expect(eligible.attempts).toBe(3);
|
||||
expect(eligible.result).toBe(eligible.terminal);
|
||||
expect(eligible.closeSession).toHaveBeenCalledOnce();
|
||||
@@ -1455,3 +1665,356 @@ describe("realtime reconnect coordinator", () => {
|
||||
expect(coordinator.getState()).toBe("IDLE");
|
||||
});
|
||||
});
|
||||
|
||||
class FakeWebSocket implements WebSocketFacade {
|
||||
protocol: string = REALTIME_WEBSOCKET_PROTOCOL;
|
||||
readyState = 0;
|
||||
bufferedAmount = 0;
|
||||
readonly listeners = new Map<
|
||||
string,
|
||||
Set<(event: unknown) => void>
|
||||
>();
|
||||
readonly sent: string[] = [];
|
||||
|
||||
send(data: string): void {
|
||||
if (this.readyState !== 1) throw new Error("not open");
|
||||
this.sent.push(data);
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.readyState = 3;
|
||||
}
|
||||
|
||||
addEventListener(
|
||||
type: string,
|
||||
listener: (event: unknown) => void,
|
||||
): void {
|
||||
const listeners = this.listeners.get(type) ?? new Set();
|
||||
listeners.add(listener);
|
||||
this.listeners.set(type, listeners);
|
||||
}
|
||||
|
||||
removeEventListener(
|
||||
type: string,
|
||||
listener: (event: unknown) => void,
|
||||
): void {
|
||||
this.listeners.get(type)?.delete(listener);
|
||||
}
|
||||
|
||||
accept(): void {
|
||||
this.readyState = 1;
|
||||
this.emit("open", {});
|
||||
this.emit("message", {
|
||||
data: JSON.stringify({
|
||||
type: "WELCOME",
|
||||
protocol: REALTIME_WEBSOCKET_PROTOCOL,
|
||||
connectionId: "connection.0001",
|
||||
heartbeatMs: 5_000,
|
||||
heartbeatAckTimeoutMs: 1_000,
|
||||
limits: {
|
||||
maxFrameBytes: 64 * 1_024,
|
||||
maxSubscriptions: 32,
|
||||
maxInboundQueueCount: 256,
|
||||
maxInboundQueueBytes: 4 * 1_024 * 1_024,
|
||||
maxOutboundQueueCount: 128,
|
||||
maxOutboundQueueBytes: 256 * 1_024,
|
||||
maxBufferedAmountBytes: 256 * 1_024,
|
||||
maxEventsPerSecond: 256,
|
||||
},
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
nativeClose(code: number): void {
|
||||
this.readyState = 3;
|
||||
this.emit("close", { code, reason: "" });
|
||||
}
|
||||
|
||||
ackHeartbeats(): void {
|
||||
for (const raw of this.sent.splice(0)) {
|
||||
const frame = JSON.parse(raw) as { type: string; nonce: string };
|
||||
if (frame.type !== "HEARTBEAT") continue;
|
||||
this.emit("message", {
|
||||
data: JSON.stringify({
|
||||
type: "HEARTBEAT_ACK",
|
||||
protocol: REALTIME_WEBSOCKET_PROTOCOL,
|
||||
nonce: frame.nonce,
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private emit(type: string, event: unknown): void {
|
||||
for (const listener of [...(this.listeners.get(type) ?? [])]) {
|
||||
listener(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe("transport failures through the reconnect coordinator", () => {
|
||||
it.each([
|
||||
["NETWORK_LOST", 1_006],
|
||||
["RESTART", 1_012],
|
||||
] as const)(
|
||||
"reconnects a WebSocket %s close that carries no server hint",
|
||||
async (_category, code) => {
|
||||
const clock = new ManualClock();
|
||||
const sockets: FakeWebSocket[] = [];
|
||||
const coordinator = createRealtimeReconnectCoordinator<
|
||||
WebSocketClosedReceipt
|
||||
>({
|
||||
policy: policy(),
|
||||
environment: new MutableOnlineEnvironment(true),
|
||||
clock,
|
||||
random: () => 0.5,
|
||||
confirmTransportBarrier: confirmationSucceeded,
|
||||
attempt: async (context) => {
|
||||
const connection = createWebSocketConnection({
|
||||
origin: "https://app.example.test",
|
||||
endpointPath: "/_realtime/v1",
|
||||
clock,
|
||||
createNonce: () => "nonce.0001",
|
||||
createSocket: () => {
|
||||
const socket = new FakeWebSocket();
|
||||
sockets.push(socket);
|
||||
return socket;
|
||||
},
|
||||
onEvent: async () =>
|
||||
realtimeSuccess(REALTIME_TRANSPORT_CONTINUE),
|
||||
onRecoveryRequired: () => undefined,
|
||||
});
|
||||
const connected = await connection.connect(context.signal);
|
||||
if (!connected.ok) return failed(connected);
|
||||
return opened(
|
||||
Object.freeze({
|
||||
waitClosed: () => connection.waitClosed(),
|
||||
classifyClosed: (receipt: WebSocketClosedReceipt) =>
|
||||
Object.freeze({
|
||||
result: Object.freeze({
|
||||
ok: false as const,
|
||||
error: receipt.error,
|
||||
}),
|
||||
}),
|
||||
close: () => connection.close(),
|
||||
}),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const running = coordinator.run();
|
||||
await flushUntil(() => sockets.length === 1);
|
||||
sockets[0]!.accept();
|
||||
await flush();
|
||||
sockets[0]!.nativeClose(code);
|
||||
await flushUntil(() => clock.sleepCalls.includes(50));
|
||||
clock.advance(50);
|
||||
await flushUntil(() => sockets.length === 2);
|
||||
expect(sockets).toHaveLength(2);
|
||||
|
||||
// OVERLOADED carries no hint either and stays terminal.
|
||||
sockets[1]!.accept();
|
||||
await flush();
|
||||
sockets[1]!.nativeClose(1_013);
|
||||
await expect(running).resolves.toEqual({
|
||||
ok: false,
|
||||
error: {
|
||||
kind: "PROVIDER_UNAVAILABLE",
|
||||
operation: "RECEIVE",
|
||||
retryable: false,
|
||||
},
|
||||
});
|
||||
expect(sockets).toHaveLength(2);
|
||||
},
|
||||
);
|
||||
|
||||
it("reconnects a quiet WebSocket whose heartbeats kept it healthy past the elapsed budget", async () => {
|
||||
const clock = new ManualClock();
|
||||
const sockets: FakeWebSocket[] = [];
|
||||
let nonce = 0;
|
||||
const coordinator = createRealtimeReconnectCoordinator<
|
||||
WebSocketClosedReceipt
|
||||
>({
|
||||
policy: policy(),
|
||||
environment: new MutableOnlineEnvironment(true),
|
||||
clock,
|
||||
random: () => 0.5,
|
||||
confirmTransportBarrier: confirmationSucceeded,
|
||||
attempt: async (context) => {
|
||||
const connection = createWebSocketConnection({
|
||||
origin: "https://app.example.test",
|
||||
endpointPath: "/_realtime/v1",
|
||||
clock,
|
||||
createNonce: () => `nonce.${++nonce}`,
|
||||
createSocket: () => {
|
||||
const socket = new FakeWebSocket();
|
||||
sockets.push(socket);
|
||||
return socket;
|
||||
},
|
||||
onEvent: async () =>
|
||||
realtimeSuccess(REALTIME_TRANSPORT_CONTINUE),
|
||||
onRecoveryRequired: () => undefined,
|
||||
observe: (observation) => {
|
||||
if (observation.kind === "HEARTBEAT_ACK") {
|
||||
context.markValidHeartbeatOrEvent();
|
||||
}
|
||||
},
|
||||
});
|
||||
const connected = await connection.connect(context.signal);
|
||||
if (!connected.ok) return failed(connected);
|
||||
return opened(
|
||||
Object.freeze({
|
||||
waitClosed: () => connection.waitClosed(),
|
||||
classifyClosed: (receipt: WebSocketClosedReceipt) =>
|
||||
Object.freeze({
|
||||
result: Object.freeze({
|
||||
ok: false as const,
|
||||
error: receipt.error,
|
||||
}),
|
||||
}),
|
||||
close: () => connection.close(),
|
||||
}),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const running = coordinator.run();
|
||||
await flushUntil(() => sockets.length === 1);
|
||||
sockets[0]!.accept();
|
||||
await flush();
|
||||
// No events, only acknowledged heartbeats, for longer than maxElapsedMs.
|
||||
for (let beat = 0; beat < 2; beat += 1) {
|
||||
clock.advance(5_000);
|
||||
await flush();
|
||||
sockets[0]!.ackHeartbeats();
|
||||
clock.advance(1_000);
|
||||
await flush();
|
||||
}
|
||||
sockets[0]!.nativeClose(1_012);
|
||||
await flushUntil(() => clock.sleepCalls.includes(50));
|
||||
clock.advance(50);
|
||||
await flushUntil(() => sockets.length === 2);
|
||||
expect(sockets).toHaveLength(2);
|
||||
|
||||
coordinator.close();
|
||||
await expect(running).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "CLOSED" },
|
||||
});
|
||||
});
|
||||
|
||||
it.each([502, 504])(
|
||||
"reconnects SSE HTTP %i that carries no server hint",
|
||||
async (status) => {
|
||||
const clock = new ManualClock();
|
||||
const fetcher = vi
|
||||
.fn<typeof fetch>()
|
||||
.mockResolvedValueOnce(new Response(null, { status }))
|
||||
.mockResolvedValueOnce(new Response(null, { status: 403 }));
|
||||
const coordinator = createRealtimeReconnectCoordinator({
|
||||
policy: policy(),
|
||||
environment: new MutableOnlineEnvironment(true),
|
||||
clock,
|
||||
random: () => 0.5,
|
||||
confirmTransportBarrier: confirmationSucceeded,
|
||||
attempt: async (context) => {
|
||||
const read = await createFetchSseConnection({
|
||||
endpoint: "https://app.example.test/events",
|
||||
applicationOrigin: "https://app.example.test",
|
||||
recoveryMode: "CURSOR",
|
||||
fetcher,
|
||||
clock,
|
||||
}).read({
|
||||
resumeCursor: null,
|
||||
signal: context.signal,
|
||||
onEvent: () => realtimeSuccess(SSE_CONTINUE),
|
||||
});
|
||||
// Only connect failures are exercised here.
|
||||
return failed(
|
||||
read.ok
|
||||
? realtimeFailure("PROTOCOL_MISMATCH", "CONNECT", false)
|
||||
: read,
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const running = coordinator.run();
|
||||
await flushUntil(() => clock.sleepCalls.includes(50));
|
||||
clock.advance(50);
|
||||
|
||||
await expect(running).resolves.toEqual({
|
||||
ok: false,
|
||||
error: {
|
||||
kind: "FORBIDDEN",
|
||||
operation: "CONNECT",
|
||||
retryable: false,
|
||||
},
|
||||
});
|
||||
expect(fetcher).toHaveBeenCalledTimes(2);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([429, 503])(
|
||||
"waits at least the SSE %i Retry-After through the reconnect bridge",
|
||||
async (status) => {
|
||||
const clock = new ManualClock();
|
||||
const fetcher = vi
|
||||
.fn<typeof fetch>()
|
||||
.mockResolvedValueOnce(
|
||||
new Response(null, {
|
||||
status,
|
||||
headers: { "retry-after": "3" },
|
||||
}),
|
||||
)
|
||||
.mockResolvedValueOnce(
|
||||
new Response(null, {
|
||||
status,
|
||||
headers: { "retry-after": "10" },
|
||||
}),
|
||||
);
|
||||
const coordinator = createRealtimeReconnectCoordinator({
|
||||
policy: policy({ maxDelayMs: 5_000, maxElapsedMs: 60_000 }),
|
||||
environment: new MutableOnlineEnvironment(true),
|
||||
clock,
|
||||
random: () => 0.5,
|
||||
confirmTransportBarrier: confirmationSucceeded,
|
||||
attempt: async (context) => {
|
||||
const read = await readSseForReconnect(
|
||||
createFetchSseConnection({
|
||||
endpoint: "https://app.example.test/events",
|
||||
applicationOrigin: "https://app.example.test",
|
||||
recoveryMode: "CURSOR",
|
||||
fetcher,
|
||||
clock,
|
||||
}),
|
||||
{
|
||||
resumeCursor: null,
|
||||
signal: context.signal,
|
||||
onEvent: () => realtimeSuccess(SSE_CONTINUE),
|
||||
},
|
||||
);
|
||||
// Only connect failures are exercised here.
|
||||
if (read.result.ok) {
|
||||
return failed(
|
||||
realtimeFailure("PROTOCOL_MISMATCH", "CONNECT", false),
|
||||
);
|
||||
}
|
||||
return read as RealtimeReconnectOutcome<never>;
|
||||
},
|
||||
});
|
||||
|
||||
// 10_000 is each read's connect timeout; 3_000 is the reconnect wait.
|
||||
const running = coordinator.run();
|
||||
await flushUntil(() => clock.sleepCalls.length === 2);
|
||||
expect(clock.sleepCalls).toEqual([10_000, 3_000]);
|
||||
clock.advance(3_000);
|
||||
|
||||
// A hint above the policy's maxDelayMs still ends the run.
|
||||
await expect(running).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { operation: "CONNECT", retryable: true },
|
||||
});
|
||||
expect(fetcher).toHaveBeenCalledTimes(2);
|
||||
expect(clock.sleepCalls).toEqual([10_000, 3_000, 10_000]);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -80,31 +80,19 @@ describe("realtime reconnect policy", () => {
|
||||
expect(parseRetryAfterDelay("-1", 0)).toBeNull();
|
||||
});
|
||||
|
||||
it("resets attempts only after stable open or a valid signal", () => {
|
||||
expect(
|
||||
it("resets attempts only after a valid signal on a stable open", () => {
|
||||
const eligible = (nowMs: number, observed: boolean) =>
|
||||
isReconnectAttemptResetEligible({
|
||||
policy,
|
||||
openedAtMs: 10,
|
||||
nowMs: 29_000,
|
||||
observedValidHeartbeatOrEvent: false,
|
||||
}),
|
||||
).toBe(false);
|
||||
expect(
|
||||
isReconnectAttemptResetEligible({
|
||||
policy,
|
||||
openedAtMs: 10,
|
||||
nowMs: 30_010,
|
||||
observedValidHeartbeatOrEvent: false,
|
||||
}),
|
||||
).toBe(true);
|
||||
expect(
|
||||
isReconnectAttemptResetEligible({
|
||||
policy,
|
||||
openedAtMs: 10,
|
||||
nowMs: 11,
|
||||
observedValidHeartbeatOrEvent: true,
|
||||
}),
|
||||
).toBe(true);
|
||||
nowMs,
|
||||
observedValidHeartbeatOrEvent: observed,
|
||||
});
|
||||
expect(eligible(29_000, false)).toBe(false);
|
||||
expect(eligible(30_010, false)).toBe(false);
|
||||
expect(eligible(11, true)).toBe(false);
|
||||
expect(eligible(30_009, true)).toBe(false);
|
||||
expect(eligible(30_010, true)).toBe(true);
|
||||
expect(reconnectBudgetRemaining(policy, 1_000, 2_000)).toBe(
|
||||
299_000,
|
||||
);
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
isRealtimeResult,
|
||||
raceRealtimeTask,
|
||||
realtimeFailure,
|
||||
realtimeSuccess,
|
||||
snapshotRealtimeResult,
|
||||
timerDeadline,
|
||||
} from "../../../src/adapters/realtime/result.ts";
|
||||
|
||||
function isString(value: unknown): value is string {
|
||||
@@ -166,3 +168,78 @@ describe("realtime result boundary", () => {
|
||||
expect(snapshotRealtimeResult(source, isString)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("realtime abort/deadline race", () => {
|
||||
const never = () => new Promise<never>(() => undefined);
|
||||
|
||||
it("settles with the value, a rejection or a synchronous throw", async () => {
|
||||
await expect(raceRealtimeTask(() => 7)).resolves.toEqual({
|
||||
kind: "VALUE",
|
||||
value: 7,
|
||||
});
|
||||
await expect(
|
||||
raceRealtimeTask(() => Promise.reject(new Error("no"))),
|
||||
).resolves.toEqual({ kind: "REJECTED" });
|
||||
await expect(
|
||||
raceRealtimeTask(() => {
|
||||
throw new Error("sync");
|
||||
}),
|
||||
).resolves.toEqual({ kind: "REJECTED" });
|
||||
});
|
||||
|
||||
it("aborts without starting, or mid-flight with cleanup", async () => {
|
||||
const task = vi.fn(never);
|
||||
await expect(
|
||||
raceRealtimeTask(task, AbortSignal.abort()),
|
||||
).resolves.toEqual({ kind: "ABORTED" });
|
||||
expect(task).not.toHaveBeenCalled();
|
||||
|
||||
const caller = new AbortController();
|
||||
const remove = vi.spyOn(caller.signal, "removeEventListener");
|
||||
let lease: AbortSignal | undefined;
|
||||
const running = raceRealtimeTask((signal) => {
|
||||
lease = signal;
|
||||
return never();
|
||||
}, caller.signal);
|
||||
caller.abort();
|
||||
await expect(running).resolves.toEqual({ kind: "ABORTED" });
|
||||
expect(lease?.aborted).toBe(true);
|
||||
expect(remove).toHaveBeenCalledWith("abort", expect.any(Function));
|
||||
});
|
||||
|
||||
it("times out on the deadline and reports a broken deadline", async () => {
|
||||
let deadlineSignal: AbortSignal | undefined;
|
||||
await expect(
|
||||
raceRealtimeTask(never, undefined, (signal) => {
|
||||
deadlineSignal = signal;
|
||||
return Promise.resolve();
|
||||
}),
|
||||
).resolves.toEqual({ kind: "TIMEOUT" });
|
||||
expect(deadlineSignal?.aborted).toBe(true);
|
||||
|
||||
await expect(
|
||||
raceRealtimeTask(never, undefined, () => Promise.reject(new Error())),
|
||||
).resolves.toEqual({ kind: "CLOCK_FAILED" });
|
||||
|
||||
// A deadline that throws still lets the task start (e.g. a cancel).
|
||||
const task = vi.fn(never);
|
||||
await expect(
|
||||
raceRealtimeTask(task, undefined, () => {
|
||||
throw new Error("clock");
|
||||
}),
|
||||
).resolves.toEqual({ kind: "CLOCK_FAILED" });
|
||||
expect(task).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("clears a host-timer deadline when the task wins", async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
await expect(
|
||||
raceRealtimeTask(() => "done", undefined, timerDeadline(1_000)),
|
||||
).resolves.toEqual({ kind: "VALUE", value: "done" });
|
||||
expect(vi.getTimerCount()).toBe(0);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,364 @@
|
||||
// The round-2 SSE parser (decode each chunk, then re-encode every segment to
|
||||
// count its bytes), kept only as the oracle for the differential test.
|
||||
import type { RealtimeResult } from "../../../src/application/ports/realtime/shared.ts";
|
||||
import {
|
||||
realtimeFailure,
|
||||
realtimeSuccess,
|
||||
} from "../../../src/adapters/realtime/result.ts";
|
||||
|
||||
export const SSE_PARSER_CEILINGS = Object.freeze({
|
||||
maxLineBytes: 64 * 1_024,
|
||||
maxEventBytes: 64 * 1_024,
|
||||
maxIncompleteBufferBytes: 128 * 1_024,
|
||||
maxChunkBytes: 256 * 1_024,
|
||||
maxItemsPerChunk: 256,
|
||||
maxRetryMs: 60_000,
|
||||
});
|
||||
|
||||
const LINE_BREAK = /[\r\n]/gu;
|
||||
const NO_ITEMS: readonly SseParserItem[] = Object.freeze([]);
|
||||
const COMMENT_ITEMS: readonly SseParserItem[] = Object.freeze([
|
||||
Object.freeze({ kind: "COMMENT" as const }),
|
||||
]);
|
||||
|
||||
export type SseParserLimits = Readonly<{
|
||||
maxLineBytes: number;
|
||||
maxEventBytes: number;
|
||||
maxIncompleteBufferBytes: number;
|
||||
maxChunkBytes: number;
|
||||
maxItemsPerChunk: number;
|
||||
maxRetryMs: number;
|
||||
}>;
|
||||
|
||||
export type ParsedSseEvent = Readonly<{
|
||||
kind: "EVENT";
|
||||
eventType: string;
|
||||
data: string;
|
||||
/**
|
||||
* Standard SSE last-event-ID state. Consumers that require cursor-after-
|
||||
* effect must additionally require `hasExplicitId` and commit independently.
|
||||
*/
|
||||
id: string | null;
|
||||
hasExplicitId: boolean;
|
||||
}>;
|
||||
|
||||
export type SseParserItem =
|
||||
| ParsedSseEvent
|
||||
| Readonly<{ kind: "COMMENT" }>
|
||||
| Readonly<{ kind: "RETRY"; retryMs: number }>;
|
||||
|
||||
export type SseParserFinish = Readonly<{
|
||||
items: readonly SseParserItem[];
|
||||
incompleteEventDiscarded: boolean;
|
||||
}>;
|
||||
|
||||
export type IncrementalSseParser = Readonly<{
|
||||
/**
|
||||
* Returns at most `maxItemsPerChunk` items. Text past that cap is kept and
|
||||
* must be taken with `drain()` until it returns no items; pushing or
|
||||
* finishing before then fails with QUEUE_OVERFLOW.
|
||||
*/
|
||||
push(chunk: Uint8Array): RealtimeResult<readonly SseParserItem[]>;
|
||||
drain(): RealtimeResult<readonly SseParserItem[]>;
|
||||
finish(): RealtimeResult<SseParserFinish>;
|
||||
}>;
|
||||
|
||||
export function createIncrementalSseParser(
|
||||
limits: Partial<SseParserLimits> = {},
|
||||
): IncrementalSseParser {
|
||||
const resolved = resolveLimits(limits);
|
||||
// ignoreBOM: false makes the decoder strip the one leading BOM the
|
||||
// stream grammar allows; a second one is ordinary text.
|
||||
const decoder = new TextDecoder("utf-8", {
|
||||
fatal: true,
|
||||
ignoreBOM: false,
|
||||
});
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
let state: "OPEN" | "FAILED" | "FINISHED" = "OPEN";
|
||||
let pendingCarriageReturn = false;
|
||||
let line = "";
|
||||
let lineBytes = 0;
|
||||
let undrained = "";
|
||||
let blockBytes = 0;
|
||||
let dataLines: string[] = [];
|
||||
let eventType = "";
|
||||
let lastEventId: string | null = null;
|
||||
let hasExplicitId = false;
|
||||
|
||||
function push(
|
||||
chunk: Uint8Array,
|
||||
): RealtimeResult<readonly SseParserItem[]> {
|
||||
if (state !== "OPEN") {
|
||||
return realtimeFailure("CLOSED", "DECODE");
|
||||
}
|
||||
if (!(chunk instanceof Uint8Array)) {
|
||||
return fail("MALFORMED_EVENT");
|
||||
}
|
||||
if (chunk.byteLength > resolved.maxChunkBytes) {
|
||||
return fail("EVENT_TOO_LARGE");
|
||||
}
|
||||
if (undrained.length > 0) return fail("QUEUE_OVERFLOW");
|
||||
let text: string;
|
||||
try {
|
||||
text = decoder.decode(chunk, { stream: true });
|
||||
} catch {
|
||||
return fail("MALFORMED_EVENT");
|
||||
}
|
||||
return consumeText(text);
|
||||
}
|
||||
|
||||
function drain(): RealtimeResult<readonly SseParserItem[]> {
|
||||
if (state !== "OPEN") {
|
||||
return realtimeFailure("CLOSED", "DECODE");
|
||||
}
|
||||
const text = undrained;
|
||||
undrained = "";
|
||||
return consumeText(text);
|
||||
}
|
||||
|
||||
function finish(): RealtimeResult<SseParserFinish> {
|
||||
if (state !== "OPEN") {
|
||||
return realtimeFailure("CLOSED", "DECODE");
|
||||
}
|
||||
if (undrained.length > 0) return fail("QUEUE_OVERFLOW");
|
||||
let tail: string;
|
||||
try {
|
||||
tail = decoder.decode();
|
||||
} catch {
|
||||
return fail("MALFORMED_EVENT");
|
||||
}
|
||||
const consumed = consumeText(tail);
|
||||
if (!consumed.ok) return consumed;
|
||||
const items = [...consumed.value];
|
||||
if (pendingCarriageReturn) {
|
||||
pendingCarriageReturn = false;
|
||||
const processed = processLine(1);
|
||||
if (!processed.ok) return processed;
|
||||
items.push(...processed.value);
|
||||
}
|
||||
if (
|
||||
undrained.length > 0 ||
|
||||
items.length > resolved.maxItemsPerChunk
|
||||
) {
|
||||
return fail("QUEUE_OVERFLOW");
|
||||
}
|
||||
const incompleteEventDiscarded =
|
||||
lineBytes > 0 ||
|
||||
blockBytes > 0 ||
|
||||
dataLines.length > 0 ||
|
||||
eventType.length > 0 ||
|
||||
hasExplicitId;
|
||||
clearBlock();
|
||||
line = "";
|
||||
lineBytes = 0;
|
||||
state = "FINISHED";
|
||||
return success(
|
||||
Object.freeze({
|
||||
items: Object.freeze(items),
|
||||
incompleteEventDiscarded,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Works a line at a time: find the next CR/LF, append the text before it
|
||||
* and count its UTF-8 bytes once. Byte counts only grow within a line, so
|
||||
* checking the limits per segment fails exactly where a per-character check
|
||||
* would. Stops at `maxItemsPerChunk` items and keeps the rest for `drain()`.
|
||||
*/
|
||||
function consumeText(
|
||||
text: string,
|
||||
): RealtimeResult<readonly SseParserItem[]> {
|
||||
const items: SseParserItem[] = [];
|
||||
let index = 0;
|
||||
|
||||
while (index < text.length) {
|
||||
if (items.length >= resolved.maxItemsPerChunk) {
|
||||
undrained = text.slice(index);
|
||||
break;
|
||||
}
|
||||
if (pendingCarriageReturn) {
|
||||
pendingCarriageReturn = false;
|
||||
const lineFeed = text.charCodeAt(index) === 0x0a;
|
||||
const processed = processLine(lineFeed ? 2 : 1);
|
||||
if (!processed.ok) return processed;
|
||||
items.push(...processed.value);
|
||||
if (lineFeed) index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
LINE_BREAK.lastIndex = index;
|
||||
const lineBreak = LINE_BREAK.exec(text)?.index ?? text.length;
|
||||
if (lineBreak > index) {
|
||||
const segment = text.slice(index, lineBreak);
|
||||
line += segment;
|
||||
lineBytes += encoder.encode(segment).byteLength;
|
||||
if (
|
||||
lineBytes > resolved.maxLineBytes ||
|
||||
lineBytes + blockBytes > resolved.maxIncompleteBufferBytes
|
||||
) {
|
||||
return fail("EVENT_TOO_LARGE");
|
||||
}
|
||||
}
|
||||
if (lineBreak === text.length) break;
|
||||
|
||||
index = lineBreak + 1;
|
||||
if (text.charCodeAt(lineBreak) === 0x0d) {
|
||||
pendingCarriageReturn = true;
|
||||
continue;
|
||||
}
|
||||
const processed = processLine(1);
|
||||
if (!processed.ok) return processed;
|
||||
items.push(...processed.value);
|
||||
}
|
||||
return success(Object.freeze(items));
|
||||
}
|
||||
|
||||
function processLine(
|
||||
terminatorBytes: number,
|
||||
): RealtimeResult<readonly SseParserItem[]> {
|
||||
const currentLine = line;
|
||||
const currentLineBytes = lineBytes;
|
||||
line = "";
|
||||
lineBytes = 0;
|
||||
|
||||
if (currentLine.length === 0) {
|
||||
const items: SseParserItem[] = [];
|
||||
if (dataLines.length > 0) {
|
||||
items.push(
|
||||
Object.freeze({
|
||||
kind: "EVENT",
|
||||
eventType: eventType.length > 0 ? eventType : "message",
|
||||
data: dataLines.join("\n"),
|
||||
id: lastEventId,
|
||||
hasExplicitId,
|
||||
}),
|
||||
);
|
||||
}
|
||||
clearBlock();
|
||||
return success(Object.freeze(items));
|
||||
}
|
||||
|
||||
if (currentLine.startsWith(":")) return success(COMMENT_ITEMS);
|
||||
|
||||
blockBytes += currentLineBytes + terminatorBytes;
|
||||
if (blockBytes > resolved.maxEventBytes) {
|
||||
return fail("EVENT_TOO_LARGE");
|
||||
}
|
||||
if (blockBytes > resolved.maxIncompleteBufferBytes) {
|
||||
return fail("EVENT_TOO_LARGE");
|
||||
}
|
||||
|
||||
const separator = currentLine.indexOf(":");
|
||||
const field =
|
||||
separator === -1
|
||||
? currentLine
|
||||
: currentLine.slice(0, separator);
|
||||
let value =
|
||||
separator === -1 ? "" : currentLine.slice(separator + 1);
|
||||
if (value.startsWith(" ")) value = value.slice(1);
|
||||
|
||||
if (field === "data") {
|
||||
dataLines.push(value);
|
||||
return success(NO_ITEMS);
|
||||
}
|
||||
if (field === "event") {
|
||||
eventType = value;
|
||||
return success(NO_ITEMS);
|
||||
}
|
||||
if (field === "id") {
|
||||
if (!value.includes("\0")) {
|
||||
lastEventId = value;
|
||||
hasExplicitId = true;
|
||||
}
|
||||
return success(NO_ITEMS);
|
||||
}
|
||||
if (field === "retry" && /^\d+$/u.test(value)) {
|
||||
const retryMs = Number(value);
|
||||
if (
|
||||
Number.isSafeInteger(retryMs) &&
|
||||
retryMs <= resolved.maxRetryMs
|
||||
) {
|
||||
return success(
|
||||
Object.freeze([
|
||||
Object.freeze({ kind: "RETRY" as const, retryMs }),
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
||||
return success(NO_ITEMS);
|
||||
}
|
||||
|
||||
function clearBlock(): void {
|
||||
blockBytes = 0;
|
||||
dataLines = [];
|
||||
eventType = "";
|
||||
hasExplicitId = false;
|
||||
}
|
||||
|
||||
function fail(
|
||||
kind:
|
||||
| "EVENT_TOO_LARGE"
|
||||
| "MALFORMED_EVENT"
|
||||
| "QUEUE_OVERFLOW",
|
||||
): RealtimeResult<never> {
|
||||
state = "FAILED";
|
||||
line = "";
|
||||
lineBytes = 0;
|
||||
undrained = "";
|
||||
clearBlock();
|
||||
return realtimeFailure(kind, "DECODE");
|
||||
}
|
||||
|
||||
return Object.freeze({ push, drain, finish });
|
||||
}
|
||||
|
||||
function resolveLimits(
|
||||
input: Partial<SseParserLimits>,
|
||||
): SseParserLimits {
|
||||
const limits = {
|
||||
maxLineBytes:
|
||||
input.maxLineBytes ?? SSE_PARSER_CEILINGS.maxLineBytes,
|
||||
maxEventBytes:
|
||||
input.maxEventBytes ?? SSE_PARSER_CEILINGS.maxEventBytes,
|
||||
maxIncompleteBufferBytes:
|
||||
input.maxIncompleteBufferBytes ??
|
||||
SSE_PARSER_CEILINGS.maxIncompleteBufferBytes,
|
||||
maxChunkBytes:
|
||||
input.maxChunkBytes ?? SSE_PARSER_CEILINGS.maxChunkBytes,
|
||||
maxItemsPerChunk:
|
||||
input.maxItemsPerChunk ??
|
||||
SSE_PARSER_CEILINGS.maxItemsPerChunk,
|
||||
maxRetryMs: input.maxRetryMs ?? SSE_PARSER_CEILINGS.maxRetryMs,
|
||||
};
|
||||
if (
|
||||
!positiveInteger(limits.maxLineBytes) ||
|
||||
limits.maxLineBytes > SSE_PARSER_CEILINGS.maxLineBytes ||
|
||||
!positiveInteger(limits.maxEventBytes) ||
|
||||
limits.maxEventBytes > SSE_PARSER_CEILINGS.maxEventBytes ||
|
||||
!positiveInteger(limits.maxIncompleteBufferBytes) ||
|
||||
limits.maxIncompleteBufferBytes >
|
||||
SSE_PARSER_CEILINGS.maxIncompleteBufferBytes ||
|
||||
limits.maxIncompleteBufferBytes < limits.maxEventBytes ||
|
||||
!positiveInteger(limits.maxChunkBytes) ||
|
||||
limits.maxChunkBytes > SSE_PARSER_CEILINGS.maxChunkBytes ||
|
||||
limits.maxChunkBytes < limits.maxEventBytes ||
|
||||
!positiveInteger(limits.maxItemsPerChunk) ||
|
||||
limits.maxItemsPerChunk >
|
||||
SSE_PARSER_CEILINGS.maxItemsPerChunk ||
|
||||
!positiveInteger(limits.maxRetryMs) ||
|
||||
limits.maxRetryMs > SSE_PARSER_CEILINGS.maxRetryMs
|
||||
) {
|
||||
throw new TypeError("Invalid SSE parser limits.");
|
||||
}
|
||||
return Object.freeze(limits);
|
||||
}
|
||||
|
||||
function success<Value>(value: Value): RealtimeResult<Value> {
|
||||
return realtimeSuccess(value);
|
||||
}
|
||||
|
||||
function positiveInteger(value: number): boolean {
|
||||
return Number.isSafeInteger(value) && value > 0;
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
createIncrementalSseParser,
|
||||
type SseParserItem,
|
||||
} from "../../../src/adapters/realtime/sse/sse-parser.ts";
|
||||
import { createIncrementalSseParser as createReferenceSseParser } from "./sse-parser-reference.ts";
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
@@ -46,6 +47,29 @@ describe("incremental SSE parser", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("strips exactly one leading BOM", () => {
|
||||
const parser = createIncrementalSseParser();
|
||||
// The second BOM belongs to the field name, so the line is ignored.
|
||||
expect(
|
||||
pushText(parser, "\uFEFF\uFEFFdata: x\n\ndata: y\n\n"),
|
||||
).toEqual([
|
||||
{
|
||||
kind: "EVENT",
|
||||
eventType: "message",
|
||||
data: "y",
|
||||
id: null,
|
||||
hasExplicitId: false,
|
||||
},
|
||||
]);
|
||||
|
||||
// Also when the first BOM is split across reads. Node's streaming
|
||||
// TextDecoder (ignoreBOM: false) drops both BOMs then; the parser must not.
|
||||
const split = createIncrementalSseParser();
|
||||
const bytes = encoder.encode("data: x\n\n");
|
||||
expect(split.push(bytes.subarray(0, 1))).toEqual({ ok: true, value: [] });
|
||||
expect(split.push(bytes.subarray(1))).toEqual({ ok: true, value: [] });
|
||||
});
|
||||
|
||||
it("preserves standard inherited ID state while marking direct IDs", () => {
|
||||
const parser = createIncrementalSseParser();
|
||||
const items = pushText(
|
||||
@@ -159,18 +183,6 @@ describe("incremental SSE parser", () => {
|
||||
},
|
||||
});
|
||||
|
||||
const excessiveBatch = createIncrementalSseParser({
|
||||
maxItemsPerChunk: 2,
|
||||
});
|
||||
expect(excessiveBatch.push(encoder.encode(":\n:\n:\n"))).toEqual({
|
||||
ok: false,
|
||||
error: {
|
||||
kind: "QUEUE_OVERFLOW",
|
||||
operation: "DECODE",
|
||||
retryable: false,
|
||||
},
|
||||
});
|
||||
|
||||
const oversizedChunk = createIncrementalSseParser({
|
||||
maxChunkBytes: 65_536,
|
||||
});
|
||||
@@ -185,4 +197,312 @@ describe("incremental SSE parser", () => {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("hands items past the per-chunk cap to drain() instead of failing", () => {
|
||||
const parser = createIncrementalSseParser({ maxItemsPerChunk: 1 });
|
||||
const comment = { kind: "COMMENT" };
|
||||
expect(pushText(parser, ":\r:\r\n:\ndata: a\r")).toEqual([comment]);
|
||||
// The retained text resumes between CR and LF.
|
||||
expect(parser.drain()).toEqual({ ok: true, value: [comment] });
|
||||
expect(parser.drain()).toEqual({ ok: true, value: [comment] });
|
||||
expect(parser.drain()).toEqual({ ok: true, value: [] });
|
||||
expect(parser.drain()).toEqual({ ok: true, value: [] });
|
||||
expect(pushText(parser, "\n\n")).toEqual([
|
||||
{
|
||||
kind: "EVENT",
|
||||
eventType: "message",
|
||||
data: "a",
|
||||
id: null,
|
||||
hasExplicitId: false,
|
||||
},
|
||||
]);
|
||||
|
||||
// A lone CR followed by another complete line still honours the cap.
|
||||
const loneCarriageReturn = createIncrementalSseParser({
|
||||
maxItemsPerChunk: 1,
|
||||
});
|
||||
expect(pushText(loneCarriageReturn, ":\r:\n")).toEqual([comment]);
|
||||
expect(loneCarriageReturn.drain()).toEqual({
|
||||
ok: true,
|
||||
value: [comment],
|
||||
});
|
||||
|
||||
// Undrained items are a caller bug: fail closed instead of buffering.
|
||||
const undrained = createIncrementalSseParser({ maxItemsPerChunk: 1 });
|
||||
expect(pushText(undrained, ":\n:\n")).toEqual([comment]);
|
||||
const overflow = {
|
||||
ok: false,
|
||||
error: {
|
||||
kind: "QUEUE_OVERFLOW",
|
||||
operation: "DECODE",
|
||||
retryable: false,
|
||||
},
|
||||
};
|
||||
expect(undrained.push(encoder.encode(":\n"))).toEqual(overflow);
|
||||
const unfinished = createIncrementalSseParser({ maxItemsPerChunk: 1 });
|
||||
expect(pushText(unfinished, ":\n:\n")).toEqual([comment]);
|
||||
expect(unfinished.finish()).toEqual(overflow);
|
||||
});
|
||||
|
||||
it("parses the same items whether bytes arrive at once or one by one", () => {
|
||||
const stream = encoder.encode(
|
||||
"event: a\r\ndata: 한글\rdata: 😀 é\r\n\r\n" +
|
||||
": note\rid: c-1\ndata: xy\r\rretry: 10\n" +
|
||||
"data: last\n\n",
|
||||
);
|
||||
const whole = createIncrementalSseParser();
|
||||
const wholeResult = whole.push(stream);
|
||||
if (!wholeResult.ok) throw new Error(wholeResult.error.kind);
|
||||
|
||||
const split = createIncrementalSseParser();
|
||||
const splitItems: SseParserItem[] = [];
|
||||
for (let index = 0; index < stream.length; index += 1) {
|
||||
const pushed = split.push(stream.subarray(index, index + 1));
|
||||
if (!pushed.ok) throw new Error(pushed.error.kind);
|
||||
splitItems.push(...pushed.value);
|
||||
}
|
||||
|
||||
const expected: SseParserItem[] = [
|
||||
{
|
||||
kind: "EVENT",
|
||||
eventType: "a",
|
||||
data: "한글\n😀 é",
|
||||
id: null,
|
||||
hasExplicitId: false,
|
||||
},
|
||||
{ kind: "COMMENT" },
|
||||
{
|
||||
kind: "EVENT",
|
||||
eventType: "message",
|
||||
data: "xy",
|
||||
id: "c-1",
|
||||
hasExplicitId: true,
|
||||
},
|
||||
{ kind: "RETRY", retryMs: 10 },
|
||||
{
|
||||
kind: "EVENT",
|
||||
eventType: "message",
|
||||
data: "last",
|
||||
id: "c-1",
|
||||
hasExplicitId: false,
|
||||
},
|
||||
];
|
||||
expect(wholeResult.value).toEqual(expected);
|
||||
expect(splitItems).toEqual(expected);
|
||||
});
|
||||
|
||||
it("dispatches a trailing CR-terminated blank line only at finish", () => {
|
||||
const parser = createIncrementalSseParser();
|
||||
expect(pushText(parser, "data: a\rdata: b\r\r")).toEqual([]);
|
||||
expect(parser.finish()).toEqual({
|
||||
ok: true,
|
||||
value: {
|
||||
items: [
|
||||
{
|
||||
kind: "EVENT",
|
||||
eventType: "message",
|
||||
data: "a\nb",
|
||||
id: null,
|
||||
hasExplicitId: false,
|
||||
},
|
||||
],
|
||||
incompleteEventDiscarded: false,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("counts UTF-8 bytes, not UTF-16 units, against line and event limits", () => {
|
||||
const limits = {
|
||||
maxLineBytes: 9,
|
||||
maxEventBytes: 11,
|
||||
maxIncompleteBufferBytes: 11,
|
||||
};
|
||||
const tooLarge = {
|
||||
ok: false,
|
||||
error: {
|
||||
kind: "EVENT_TOO_LARGE",
|
||||
operation: "DECODE",
|
||||
retryable: false,
|
||||
},
|
||||
};
|
||||
|
||||
// "data:😀" is 7 UTF-16 units but 9 bytes; the CRLF makes the event 11.
|
||||
expect(
|
||||
createIncrementalSseParser(limits).push(
|
||||
encoder.encode("data:😀\r\n\r\n"),
|
||||
),
|
||||
).toMatchObject({ ok: true, value: [{ data: "😀" }] });
|
||||
expect(
|
||||
createIncrementalSseParser(limits).push(
|
||||
encoder.encode("data:😀a"),
|
||||
),
|
||||
).toEqual(tooLarge);
|
||||
expect(
|
||||
createIncrementalSseParser(limits).push(
|
||||
encoder.encode("data:한\r\ndata:\r\n"),
|
||||
),
|
||||
).toEqual(tooLarge);
|
||||
|
||||
// The same limit holds when the multibyte character is split across reads.
|
||||
const split = createIncrementalSseParser(limits);
|
||||
const bytes = encoder.encode("data:한한");
|
||||
expect(split.push(bytes.subarray(0, 6))).toEqual({
|
||||
ok: true,
|
||||
value: [],
|
||||
});
|
||||
expect(split.push(bytes.subarray(6))).toEqual(tooLarge);
|
||||
});
|
||||
|
||||
it("accepts a maximum-size line across many reads and rejects one byte more", () => {
|
||||
const maxLineBytes = 32 * 1_024;
|
||||
const payload = "é".repeat((maxLineBytes - 6) / 2);
|
||||
const exact = encoder.encode(`data: ${payload}\n\n`);
|
||||
// 1,000-byte reads split some two-byte "é" characters.
|
||||
const parser = createIncrementalSseParser({ maxLineBytes });
|
||||
const items: SseParserItem[] = [];
|
||||
for (let index = 0; index < exact.length; index += 1_000) {
|
||||
const pushed = parser.push(exact.subarray(index, index + 1_000));
|
||||
if (!pushed.ok) throw new Error(pushed.error.kind);
|
||||
items.push(...pushed.value);
|
||||
}
|
||||
expect(items).toEqual([
|
||||
{
|
||||
kind: "EVENT",
|
||||
eventType: "message",
|
||||
data: payload,
|
||||
id: null,
|
||||
hasExplicitId: false,
|
||||
},
|
||||
]);
|
||||
|
||||
const over = createIncrementalSseParser({ maxLineBytes });
|
||||
const overBytes = encoder.encode(`data: ${payload}a`);
|
||||
expect(over.push(overBytes.subarray(0, 20_000))).toEqual({
|
||||
ok: true,
|
||||
value: [],
|
||||
});
|
||||
expect(over.push(overBytes.subarray(20_000))).toEqual({
|
||||
ok: false,
|
||||
error: {
|
||||
kind: "EVENT_TOO_LARGE",
|
||||
operation: "DECODE",
|
||||
retryable: false,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("parses a full-size chunk of many events", () => {
|
||||
const events = Array.from({ length: 200 }, (_, index) =>
|
||||
JSON.stringify({ index, value: "v".repeat(1_200) }),
|
||||
);
|
||||
const chunk = encoder.encode(
|
||||
events
|
||||
.map((data, index) => `id: c-${index}\r\ndata: ${data}\r\n\r\n`)
|
||||
.join(""),
|
||||
);
|
||||
expect(chunk.byteLength).toBeGreaterThan(240 * 1_024);
|
||||
expect(chunk.byteLength).toBeLessThanOrEqual(256 * 1_024);
|
||||
|
||||
const pushed = createIncrementalSseParser().push(chunk);
|
||||
if (!pushed.ok) throw new Error(pushed.error.kind);
|
||||
expect(pushed.value).toHaveLength(200);
|
||||
expect(pushed.value[199]).toEqual({
|
||||
kind: "EVENT",
|
||||
eventType: "message",
|
||||
data: events[199],
|
||||
id: "c-199",
|
||||
hasExplicitId: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("byte-level SSE parser against the round-2 text parser", () => {
|
||||
const asciiPieces = [
|
||||
"data", "data:", "data: ", "event: ", "id: ", "id:", "retry: ",
|
||||
":", "x", " ", "abc", "12", "\0", "\r", "\n", "\r\n", "\n\n", "\r\r",
|
||||
];
|
||||
const multibytePieces = [...asciiPieces, "é", "한글", "😀", ""];
|
||||
|
||||
type Create = typeof createIncrementalSseParser;
|
||||
|
||||
// mulberry32: a small seeded PRNG, so a failing seed can be replayed.
|
||||
function randomSource(seed: number): () => number {
|
||||
let state = seed;
|
||||
return () => {
|
||||
state = (state + 0x6d2b79f5) | 0;
|
||||
let value = Math.imul(state ^ (state >>> 15), 1 | state);
|
||||
value ^= value + Math.imul(value ^ (value >>> 7), 61 | value);
|
||||
return ((value ^ (value >>> 14)) >>> 0) / 4_294_967_296;
|
||||
};
|
||||
}
|
||||
|
||||
function run(
|
||||
create: Create,
|
||||
limits: Parameters<Create>[0],
|
||||
chunks: readonly Uint8Array[],
|
||||
skipDrain: readonly boolean[],
|
||||
): unknown[] {
|
||||
const parser = create(limits);
|
||||
const log: unknown[] = [];
|
||||
for (const [index, chunk] of chunks.entries()) {
|
||||
let result = parser.push(chunk);
|
||||
log.push(result);
|
||||
while (!skipDrain[index] && result.ok && result.value.length > 0) {
|
||||
result = parser.drain();
|
||||
log.push(result);
|
||||
}
|
||||
if (!result.ok) return log;
|
||||
}
|
||||
log.push(parser.finish());
|
||||
return log;
|
||||
}
|
||||
|
||||
it.each([
|
||||
["ASCII", asciiPieces],
|
||||
["multibyte", multibytePieces],
|
||||
])("matches on random %s streams and read splits", (_, pieces) => {
|
||||
for (let seed = 1; seed <= 1_500; seed += 1) {
|
||||
const next = randomSource(seed);
|
||||
const pick = (max: number) => Math.floor(next() * max);
|
||||
let text = next() < 0.3 ? "" : "";
|
||||
for (let count = 1 + pick(40); count > 0; count -= 1) {
|
||||
text += pieces[pick(pieces.length)];
|
||||
}
|
||||
// Node's streaming TextDecoder, which the reference relies on, drops a
|
||||
// U+FEFF right after the first character when that character is split
|
||||
// across reads (see the BOM test above). Keep that input out.
|
||||
const first = String.fromCodePoint(text.codePointAt(0) ?? 0x20);
|
||||
while (text.startsWith("", first.length)) {
|
||||
text = first + text.slice(first.length + 1);
|
||||
}
|
||||
let bytes = encoder.encode(text);
|
||||
// Cutting the stream can end it inside a character.
|
||||
if (next() < 0.2) bytes = bytes.subarray(0, pick(bytes.length + 1));
|
||||
|
||||
const chunks: Uint8Array[] = [];
|
||||
const maxRead = 1 + pick(next() < 0.5 ? 6 : bytes.length + 1);
|
||||
for (let index = 0; index < bytes.length; ) {
|
||||
const size = 1 + pick(maxRead);
|
||||
chunks.push(bytes.subarray(index, index + size));
|
||||
index += size;
|
||||
}
|
||||
const skipDrain = chunks.map(() => next() < 0.05);
|
||||
|
||||
const maxEventBytes = 4 + pick(60);
|
||||
const limits =
|
||||
next() < 0.3
|
||||
? {}
|
||||
: {
|
||||
maxLineBytes: 4 + pick(40),
|
||||
maxEventBytes,
|
||||
maxIncompleteBufferBytes: maxEventBytes + pick(20),
|
||||
maxItemsPerChunk: 1 + pick(4),
|
||||
};
|
||||
|
||||
expect(
|
||||
run(createIncrementalSseParser, limits, chunks, skipDrain),
|
||||
`seed ${seed}`,
|
||||
).toEqual(run(createReferenceSseParser, limits, chunks, skipDrain));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -707,7 +707,7 @@ describe("transport-independent realtime stream coordinator", () => {
|
||||
expect(harness.coordinator.inspect(STREAM_ID).freshness).toBe("STALE");
|
||||
});
|
||||
|
||||
it("recovers instead of evicting and continuing after dedupe capacity", async () => {
|
||||
it("evicts the oldest dedupe entry and keeps applying at capacity", async () => {
|
||||
const harness = createHarness({
|
||||
registry: createTestRealtimeRegistry({
|
||||
limits: {
|
||||
@@ -718,24 +718,58 @@ describe("transport-independent realtime stream coordinator", () => {
|
||||
});
|
||||
await harness.initialize();
|
||||
await harness.coordinator.accept(harness.event());
|
||||
const second = {
|
||||
eventId: "event-00000002",
|
||||
sequence: "2",
|
||||
resumeCursor: "cursor-00000002",
|
||||
};
|
||||
|
||||
await expect(
|
||||
harness.coordinator.accept(
|
||||
harness.event({
|
||||
eventId: "event-00000002",
|
||||
sequence: "2",
|
||||
resumeCursor: "cursor-00000002",
|
||||
}),
|
||||
),
|
||||
harness.coordinator.accept(harness.event(second)),
|
||||
).resolves.toMatchObject({
|
||||
ok: true,
|
||||
value: {
|
||||
outcome: "RECOVERED",
|
||||
reason: "DEDUPE_OVERFLOW",
|
||||
},
|
||||
value: { outcome: "APPLIED" },
|
||||
});
|
||||
expect(harness.effects).toHaveBeenCalledTimes(1);
|
||||
expect(harness.coordinator.inspect(STREAM_ID).dedupeEntries).toBe(0);
|
||||
expect(harness.effects).toHaveBeenCalledTimes(2);
|
||||
expect(harness.recoveryReasons).toEqual(["INITIALIZE"]);
|
||||
expect(harness.coordinator.inspect(STREAM_ID).dedupeEntries).toBe(1);
|
||||
// The newest entry still catches its duplicate; the evicted one is stale.
|
||||
await expect(
|
||||
harness.coordinator.accept(harness.event(second)),
|
||||
).resolves.toMatchObject({
|
||||
ok: true,
|
||||
value: { outcome: "DROPPED", reason: "DUPLICATE_EVENT" },
|
||||
});
|
||||
await expect(
|
||||
harness.coordinator.accept(harness.event()),
|
||||
).resolves.toMatchObject({
|
||||
ok: true,
|
||||
value: { outcome: "DROPPED", reason: "STALE_EVENT" },
|
||||
});
|
||||
});
|
||||
|
||||
it("expires dedupe entries by TTL oldest first", async () => {
|
||||
let now = 10_000;
|
||||
const harness = createHarness({ now: () => now });
|
||||
await harness.initialize();
|
||||
await harness.coordinator.accept(harness.event());
|
||||
now += TEST_LIMITS.dedupeTtlMs / 2;
|
||||
await harness.coordinator.accept(
|
||||
harness.event({
|
||||
eventId: "event-00000002",
|
||||
sequence: "2",
|
||||
resumeCursor: "cursor-00000002",
|
||||
}),
|
||||
);
|
||||
now += TEST_LIMITS.dedupeTtlMs / 2;
|
||||
await harness.coordinator.accept(
|
||||
harness.event({
|
||||
eventId: "event-00000003",
|
||||
sequence: "3",
|
||||
resumeCursor: "cursor-00000003",
|
||||
}),
|
||||
);
|
||||
expect(harness.coordinator.inspect(STREAM_ID).dedupeEntries).toBe(2);
|
||||
});
|
||||
|
||||
it("rejects a regressing same-epoch recovery checkpoint atomically", async () => {
|
||||
@@ -1053,6 +1087,58 @@ describe("realtime physical task ownership", () => {
|
||||
|
||||
firstApply.resolve(realtimeSuccess(undefined));
|
||||
});
|
||||
|
||||
it("fails a recovery whose effect quiescence ends in DRAINING", async () => {
|
||||
const wedged = deferred<RealtimeResult<void>>();
|
||||
let recoveries = 0;
|
||||
const harness = createHarness({
|
||||
apply: async () => wedged.promise,
|
||||
recover: async () => {
|
||||
recoveries += 1;
|
||||
return realtimeSuccess(snapshotCommit("0"));
|
||||
},
|
||||
taskLimits: { effectTimeoutMs: 5, drainTimeoutMs: 5 },
|
||||
});
|
||||
await harness.initialize();
|
||||
recoveries = 0;
|
||||
|
||||
const applying = harness.coordinator.accept(
|
||||
harness.event({
|
||||
eventId: "event-00000001",
|
||||
sequence: "1",
|
||||
resumeCursor: "cursor-00000001",
|
||||
}),
|
||||
);
|
||||
for (let flush = 0; flush < 10; flush += 1) await Promise.resolve();
|
||||
expect(harness.effects).toHaveBeenCalledOnce();
|
||||
expect(harness.coordinator.lifecycle(STREAM_ID)).toBe("OPEN");
|
||||
// The effect ignores abort, so it outlives its deadline and the stream
|
||||
// enters DRAINING while this recovery waits for quiescence.
|
||||
const recovering = harness.coordinator.recover(
|
||||
STREAM_ID,
|
||||
"SEQUENCE_GAP",
|
||||
);
|
||||
|
||||
await expect(applying).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "IDLE_TIMEOUT", operation: "APPLY" },
|
||||
});
|
||||
await expect(recovering).resolves.toMatchObject({
|
||||
ok: false,
|
||||
error: { kind: "CLOSED", operation: "RECOVER" },
|
||||
});
|
||||
expect(recoveries).toBe(0);
|
||||
expect(harness.coordinator.inspect(STREAM_ID).hasResumeState).toBe(
|
||||
false,
|
||||
);
|
||||
|
||||
// The late effect settles; the stream still needs a real recovery.
|
||||
wedged.resolve(realtimeSuccess(undefined));
|
||||
await vi.waitFor(() =>
|
||||
expect(harness.coordinator.lifecycle(STREAM_ID)).toBe("OPEN"),
|
||||
);
|
||||
expect(harness.coordinator.inspect(STREAM_ID).freshness).toBe("STALE");
|
||||
});
|
||||
});
|
||||
|
||||
type HarnessOptions = Readonly<{
|
||||
@@ -1070,6 +1156,7 @@ type HarnessOptions = Readonly<{
|
||||
}>;
|
||||
scheduleTimeout?: (callback: () => void, delayMs: number) => unknown;
|
||||
clearScheduledTimeout?: (handle: unknown) => void;
|
||||
now?: () => number;
|
||||
}>;
|
||||
|
||||
function createHarness(options: HarnessOptions = {}) {
|
||||
@@ -1113,7 +1200,7 @@ function createHarness(options: HarnessOptions = {}) {
|
||||
scopeBinding: "scope-binding-0001",
|
||||
isCurrent: () => current,
|
||||
},
|
||||
now: () => 10_000,
|
||||
now: options.now ?? (() => 10_000),
|
||||
observe: options.observe,
|
||||
...(options.taskLimits ? { taskLimits: options.taskLimits } : {}),
|
||||
...(options.scheduleTimeout
|
||||
|
||||
Reference in New Issue
Block a user