Files
core/test/sse-events.test.ts
T
chrnx 7d39aebfab
CI / dco (pull_request) Successful in 6s
CI / runtimes (bun) (pull_request) Successful in 15s
CI / build (pull_request) Successful in 28s
CI / runtimes (edge) (pull_request) Successful in 12s
CI / runtimes (deno) (pull_request) Successful in 20s
fix(stream): report a stream's lifecycle on every adapter, not only Node
`pipeStream` emitted `stream:open`/`:close`/`:error` and wrote the encoder's error frame.
`asReadableStream` — the path every Fetch runtime takes, so Deno, Bun, workerd and
`app.fetch()` on Node — emitted nothing and called `controller.error(err)`.

Both halves of that were silent. A consumer counting `stream:error` saw zero on three of
the four runtimes while the streams were failing normally, and the client got a truncated
body with no explanation, which from the outside is indistinguishable from a clean end of
stream. The observability guide documents these three events without qualification; on
three runtimes they did not exist.

The error path now frames rather than breaks. Enqueue `encodeError`'s frame, then close
cleanly — the Node behaviour, and the better one: an SSE consumer that received an `error`
event knows what happened, where a transport-level failure looks exactly like the
connection dropping. `controller.error` is kept for an encoder with no error frame to
offer, because then breaking the stream is the only signal left.

`stream:close` is latched. A `cancel` can land after the body already ended, and the three
exits — done, error, cancel — must not each emit their own.

The test drives both adapters from one route table rather than asserting them in separate
blocks, since a table that describes one and then the other is how the two drifted in the
first place. Verified non-vacuous: reverting `src/http/web.ts` fails all four fetch rows
and leaves the three Node ones green, which is the split that says the test is measuring
the adapter and not the framework.

The `sse()` injection test from the previous commit went back to `app.fetch`. It had been
written against `listen()` only because the fetch path swallowed the error — that was a
workaround for this bug, and it is no longer needed.

Closes #83

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-09-01 11:52:42 -05:00

147 lines
6.0 KiB
TypeScript

// `id:` on the wire, and the `Last-Event-ID` round-trip it exists to enable.
//
// `EventSource` reconnects on its own — that is the reason to pick SSE over a raw WebSocket. Until
// an `id:` was written the browser had nothing to send back, so every reconnect restarted the
// iterable from its beginning and lost the gap in silence. green-tea carries the marker both ways
// and stores nothing: what the gap means is the handler's call, because only the source knows
// whether it is replayable.
import 'reflect-metadata';
import { describe, expect, it } from 'vitest';
import { ndjsonEncoder, sse, sseEncoder } from '../src/encoders';
import { Module, Route, Sse } from '../src/metadata';
import { header } from '../src/params';
import { createApp } from '../src/app';
const frame = (item: unknown): string => String(sseEncoder.encode(item));
describe('sse() framing', () => {
it('writes id: before data:', () => {
expect(frame(sse({ t: 1 }, { id: '7' }))).toBe('id: 7\ndata: {"t":1}\n\n');
});
it('writes every field it was given, fields before data', () => {
expect(frame(sse({ t: 1 }, { id: '7', event: 'tick', retry: 5000 }))).toBe(
'retry: 5000\nevent: tick\nid: 7\ndata: {"t":1}\n\n',
);
});
it('leaves a plain item exactly as it was', () => {
// The whole point of the marker: an app that never calls sse() sees no change on the wire.
expect(frame({ t: 1 })).toBe('data: {"t":1}\n\n');
});
it('keeps an empty id, which is how the spec resets the client s last event id', () => {
expect(frame(sse({ t: 1 }, { id: '' }))).toBe('id: \ndata: {"t":1}\n\n');
});
it('cannot be talked into extra fields by a newline in the payload', () => {
// JSON.stringify escapes the newline, so the data line stays single by construction.
expect(frame({ note: 'a\nid: 99' })).toBe('data: {"note":"a\\nid: 99"}\n\n');
});
});
describe('sse() rejects a field that would break the frame', () => {
// An id is exactly the value most likely to come from a request — a cursor, a page token — so
// this is a boundary, not a formatting nicety. Stripping would resume from the wrong place
// silently, which is the bug this feature exists to close.
for (const [label, id] of [
['newline', 'a\nid: 99'],
['carriage return', 'a\rid: 99'],
['NUL', 'a\0b'],
] as const) {
it(`throws on a ${label} in id`, () => {
expect(() => sse({ t: 1 }, { id })).toThrow(/may not contain a newline or a NUL/);
});
}
it('throws on a newline in event', () => {
expect(() => sse({ t: 1 }, { event: 'a\ndata: fake' })).toThrow(/'event' may not contain/);
});
it('throws on a fractional retry, which a browser would ignore', () => {
expect(() => sse({ t: 1 }, { retry: 1.5 })).toThrow(/whole number of milliseconds/);
});
});
describe('ndjson', () => {
it('unwraps the payload and drops the SSE fields', () => {
// A `negotiate` route yields the same items to both encoders. NDJSON has no frame to carry
// id/event/retry and no resumption protocol to use them, so the client gets what it asked for.
expect(String(ndjsonEncoder.encode(sse({ t: 1 }, { id: '7' })))).toBe('{"t":1}\n');
});
it('leaves a plain item alone', () => {
expect(String(ndjsonEncoder.encode({ t: 1 }))).toBe('{"t":1}\n');
});
});
describe('the Last-Event-ID round trip', () => {
const app = (): ReturnType<typeof createApp> => {
@Route('/')
class Ctl {
// Reading the header needs nothing new — the envelope has always carried it. What was
// missing was the `id:` that gives the browser something to put in it.
@Sse('/feed') feed(@header('last-event-id') from: string) {
const start = from ? Number(from) + 1 : 1;
return (async function* () {
for (let n = start; n < start + 2; n++) yield sse({ n }, { id: String(n) });
})();
}
}
@Module({ mountpoint: '/', controllers: [Ctl] })
class M {}
return createApp({ modules: [M] });
};
it('serves ids, and resumes from the one the client sends back', async () => {
const feed = app();
const first = await (await feed.fetch(new Request('http://x/feed'))).text();
expect(first).toBe('id: 1\ndata: {"n":1}\n\nid: 2\ndata: {"n":2}\n\n');
// What EventSource does by itself on reconnect, spelled out.
const resumed = await (
await feed.fetch(new Request('http://x/feed', { headers: { 'last-event-id': '2' } }))
).text();
expect(resumed).toBe('id: 3\ndata: {"n":3}\n\nid: 4\ndata: {"n":4}\n\n');
});
it('serves the same bytes over the Node adapter', async () => {
// A different write path — `pipeStream` to a socket rather than a web ReadableStream — so
// passing on `fetch` proves nothing about the runtime most apps deploy on.
const feed = app();
const server = await feed.listen(0);
const { port } = server.address() as import('net').AddressInfo;
const res = await fetch(`http://127.0.0.1:${port}/feed`, { headers: { 'last-event-id': '9' } });
expect(res.headers.get('content-type')).toBe('text/event-stream');
expect(await res.text()).toBe('id: 10\ndata: {"n":10}\n\nid: 11\ndata: {"n":11}\n\n');
await feed.close();
});
it('refuses an id that would inject fields, rather than writing it', async () => {
@Route('/')
class Ctl {
@Sse('/bad') bad() {
return (async function* () {
yield sse({ t: 1 }, { id: 'cursor\ndata: injected' });
})();
}
}
@Module({ mountpoint: '/', controllers: [Ctl] })
class M {}
const broken = createApp({ modules: [M] });
const errors: unknown[] = [];
broken.bus.on('stream:error', (e) => errors.push(e.error));
const body = await (await broken.fetch(new Request('http://x/bad'))).text();
// Not `toContain`: the rejection quotes the offending value, so the text appears inside the
// error payload — escaped, on one line. The claim is that no *field* was injected.
expect(body.split('\n')).not.toContain('data: injected');
expect(body).toContain('event: error');
expect(errors).toHaveLength(1);
});
});