feat(sse): emit id:, so an EventSource reconnect has something to resume from
`sseEncoder` wrote one field. With no `id:` on the wire the browser had nothing to put in
`Last-Event-ID`, so every automatic reconnect rebuilt the route's iterable from its start
and lost the gap in silence — a healthy-looking stream that had simply skipped events.
Half of this already worked and it was worth checking before designing anything: the
request envelope has always carried every header, so a handler could already read
`@header('last-event-id')`. It just always arrived empty, because we never gave the
browser an id to send.
`sse(data, { id, event, retry })` tags a stream item; `sseEncoder` writes the fields ahead
of `data:`. An app that never calls it sees byte-identical output. `event:` and `retry:`
come along because the envelope carries them for free and neither was reachable before.
**green-tea stores nothing.** No buffer, no retention window, no replay. The framework
carries the marker in both directions and the handler decides what the gap means, because
only the source knows: a paged log re-reads from an offset, a live sensor has no past
worth delivering. Framework-managed replay would need a stream identity that survives a
disconnect — which does not exist, since every request builds a fresh iterable — plus a
retention policy no default can get right and an in-process buffer that breaks the moment
there are two instances. This commit is also the prerequisite for any of that: whatever
replay might land later keys on these ids.
An id with a newline is rejected, not stripped. An id is exactly the value most likely to
be derived from a request — a cursor, a page token — and the SSE format is line-based, so
one newline lets a caller append arbitrary fields to somebody else's stream. Stripping
would resume from the wrong place invisibly, which is the failure this whole feature
exists to close, so it throws where the stream can report it.
`ndjsonEncoder` unwraps the payload and drops the fields: a `negotiate` route yields the
same items to both encoders, and NDJSON has no frame to carry them and no resumption
protocol to use them.
Verified non-vacuous: reverting just the two `encode` functions fails the six framing and
round-trip assertions and leaves the validation ones passing, which is the right split.
Closes #46
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+253
-4
No files matched your search
@@ -0,0 +1,151 @@
|
||||
// `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));
|
||||
|
||||
// The Node adapter turns a mid-stream throw into a bus event and a trailing `error` frame.
|
||||
const server = await broken.listen(0);
|
||||
const { port } = server.address() as import('net').AddressInfo;
|
||||
const body = await (await fetch(`http://127.0.0.1:${port}/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);
|
||||
|
||||
await broken.close();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user