-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathproduce.js
174 lines (158 loc) · 4.32 KB
/
produce.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { writable } from 'svelte/store'
import { ok } from './ok'
import { error } from './error'
/**
*
* @param {import('./types.internal').CreateEmitterPayload} payload
* @returns {import('./types.external').Emitter}
*/
function createEmitter({ controller, context }) {
let id = 1
const encoder = new TextEncoder()
return function emit(eventName, data) {
if (!context.connected) {
return error('Client disconnected from the stream.')
}
const typeOfEventName = typeof eventName
const typeOfData = typeof data
if (typeOfEventName !== 'string') {
return error(
`Event name must of type \`string\`, received \`${typeOfEventName}\`.`,
)
}
if (typeOfData !== 'string') {
return error(
`Event data must of type \`string\`, received \`${typeOfData}\`.`,
)
}
if (eventName.includes('\n')) {
return error(
`Event name must not contain new line characters, received "${eventName}".`,
)
}
try {
controller.enqueue(encoder.encode(`id: ${id}\nevent: ${eventName}\n`))
const chunks = data.split('\n')
for (const chunk of chunks) {
controller.enqueue(
encoder.encode(`data: ${encodeURIComponent(chunk)}\n`),
)
}
controller.enqueue(encoder.encode('\n'))
id++
return ok()
} catch (e) {
return error(e)
}
}
}
/**
*
* @param {import('./types.internal').ProduceStreamPayload} payload
* @returns
*/
function produceStream({ start, lock, context, stop, ping = 30_000 }) {
return new ReadableStream({
async start(controller) {
context.connected = true
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this
const emit = createEmitter({ controller, context })
const started = start({ source: self, emit, lock })
const interval = setInterval(function run() {
const { error } = emit('ping', '')
if (error) {
lock.set(false)
}
}, ping)
/**
*
* @param {{$lock:boolean}} payload
* @returns {Promise<boolean>}
*/
async function stopLocal({ $lock }) {
if ($lock || !context.connected) {
return false
}
clearInterval(interval)
try {
controller.close()
} catch {
// Do nothing.
// This means the client has already disconnected without notice.
}
context.connected = false
if (stop) {
await stop(self)
}
const cancelInline = await started
if (cancelInline) {
await cancelInline(self)
}
return true
}
const unsubscribe = lock.subscribe(async function run($lock) {
if (await stopLocal({ $lock })) {
unsubscribe()
}
})
},
cancel() {
lock.set(false)
},
})
}
/**
*
* @returns {import('./types.internal').StreamContext}
*/
function createContext() {
return { connected: false }
}
/**
* Create one stream and emit multiple server sent events.
* @param {import('./types.external').Start} start The stream has started, you can start emitting events.
* > ## Example
* > ```js
* > export function POST() {
* > return produce(function start({ emit }) {
* > const notifications = [
* > { title: 'title-1', body: 'lorem...' },
* > { title: 'title-2', body: 'lorem...' },
* > { title: 'title-3', body: 'lorem...' },
* > ]
* >
* > for (const notification of notifications) {
* > const { error } = emit('notification', JSON.stringify(notification))
* > if (error) {
* > // Make sure to check for errors,
* > // otherwise your stream will keep producing data
* > // and you'll create a memory leak.
* > return
* > }
* > }
* > })
* > }
* > ```
* @param {import('./types.external').ProducePayload} options
*/
export function produce(start, { stop, headers, ping = 30_000 } = {}) {
const context = createContext()
const lock = writable(true)
const producedStream = produceStream({
start,
lock,
ping,
stop,
context,
})
return new Response(producedStream, {
//@ts-ignore
headers: {
'Cache-Control': 'no-store',
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
...headers,
},
})
}