-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathndjson-stream.ts
More file actions
115 lines (101 loc) · 2.91 KB
/
Copy pathndjson-stream.ts
File metadata and controls
115 lines (101 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import type { HattipHandler } from '@hattip/core'
import { createClient, createRouter } from 'rouzer'
import * as http from 'rouzer/http'
import * as ndjson from 'rouzer/ndjson'
import { z } from 'zod'
type Event = {
id: number
message: string
}
const EventFilter = z.object({
names: z.array(z.string()),
where: z.array(
z.object({
path: z.string(),
equals: z.string(),
})
),
})
export const events = http.get('events', {
response: ndjson.$type<Event>(),
})
// NDJSON responses work for POST routes with ordinary JSON body schemas too.
export const stream = http.post('events/stream', {
body: EventFilter,
response: ndjson.$type<Event>(),
})
export const routes = { events, stream }
/**
* Tiny Hattip adapter used only to keep this example self-contained. Real apps
* mount the handler with a Hattip adapter for their runtime.
*/
function createLocalFetch(handler: HattipHandler): typeof fetch {
return async (input, init) => {
const request = new Request(input, init)
const response = await handler({
request,
ip: '127.0.0.1',
platform: undefined,
env() {
return undefined
},
passThrough() {},
waitUntil(promise) {
void promise
},
})
return response ?? new Response(null, { status: 404 })
}
}
async function collect<T>(source: AsyncIterable<T>) {
const values: T[] = []
for await (const value of source) {
values.push(value)
}
return values
}
async function readFirst<T>(source: AsyncIterable<T>) {
const iterator = source[Symbol.asyncIterator]()
try {
return (await iterator.next()).value
} finally {
// Closing the client iterator cancels the response body. For Rouzer NDJSON
// routes, that cancellation reaches the server source iterator's return().
await iterator.return?.()
}
}
export async function runNdjsonStreamExample() {
const handler = createRouter({
basePath: 'api/',
plugins: [ndjson.routerPlugin],
}).use(routes, {
async *events() {
yield { id: 1, message: 'ready' }
yield { id: 2, message: 'done' }
},
async *stream({ body }) {
// The POST body was parsed and validated before the stream starts.
yield {
id: 1,
message: `${body.names[0]} for ${body.where[0]?.equals}`,
}
yield { id: 2, message: 'done' }
},
})
const client = createClient({
baseURL: 'https://example.test/api/',
routes,
plugins: [ndjson.clientPlugin],
fetch: createLocalFetch(handler),
})
const allEvents = await collect(await client.events())
// This call sends a JSON body, receives an AsyncIterable, and then stops after
// one event. Request signals can also be used to cancel long-lived streams.
const firstMatchingEvent = await readFirst(
await client.stream({
names: ['session.message'],
where: [{ path: 'id', equals: 'ses_123' }],
})
)
return { allEvents, firstMatchingEvent }
}