@silver886/mcp-proxy 0.2.3 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/host/agent.js +69 -22
- package/dist/proxy/core/constants.d.ts +2 -0
- package/dist/proxy/core/constants.js +8 -1
- package/dist/proxy/discovery/client.js +24 -11
- package/dist/proxy/pairing/controller.d.ts +3 -0
- package/dist/proxy/pairing/controller.js +56 -4
- package/dist/proxy/pairing/http.js +11 -0
- package/dist/proxy/runtime/forwarder.js +22 -0
- package/dist/proxy/runtime/handlers.d.ts +2 -2
- package/dist/proxy/runtime/handlers.js +10 -5
- package/dist/proxy/runtime/sse.js +6 -0
- package/dist/proxy/runtime/upstream-bridge.js +9 -3
- package/dist/proxy/server.js +10 -0
- package/dist/shared/protocol.d.ts +7 -0
- package/dist/shared/protocol.js +44 -0
- package/package.json +17 -3
- package/scripts/fetch.mjs +87 -0
- package/static/setup.js +86 -25
- package/node_modules/cloudflared/LICENSE +0 -21
- package/node_modules/cloudflared/README.md +0 -156
- package/node_modules/cloudflared/lib/cloudflared.js +0 -10
- package/node_modules/cloudflared/lib/constants.js +0 -58
- package/node_modules/cloudflared/lib/error.js +0 -32
- package/node_modules/cloudflared/lib/handler.js +0 -117
- package/node_modules/cloudflared/lib/index.js +0 -126
- package/node_modules/cloudflared/lib/install.js +0 -155
- package/node_modules/cloudflared/lib/lib.d.ts +0 -236
- package/node_modules/cloudflared/lib/lib.js +0 -45
- package/node_modules/cloudflared/lib/regex.js +0 -52
- package/node_modules/cloudflared/lib/service.js +0 -229
- package/node_modules/cloudflared/lib/tunnel.js +0 -164
- package/node_modules/cloudflared/lib/types.js +0 -16
- package/node_modules/cloudflared/package.json +0 -59
- package/node_modules/cloudflared/scripts/postinstall.mjs +0 -10
- package/node_modules/eventsource-parser/LICENSE +0 -21
- package/node_modules/eventsource-parser/README.md +0 -126
- package/node_modules/eventsource-parser/dist/index.cjs +0 -166
- package/node_modules/eventsource-parser/dist/index.cjs.map +0 -1
- package/node_modules/eventsource-parser/dist/index.d.cts +0 -146
- package/node_modules/eventsource-parser/dist/index.d.ts +0 -146
- package/node_modules/eventsource-parser/dist/index.js +0 -166
- package/node_modules/eventsource-parser/dist/index.js.map +0 -1
- package/node_modules/eventsource-parser/dist/stream.cjs +0 -28
- package/node_modules/eventsource-parser/dist/stream.cjs.map +0 -1
- package/node_modules/eventsource-parser/dist/stream.d.cts +0 -121
- package/node_modules/eventsource-parser/dist/stream.d.ts +0 -121
- package/node_modules/eventsource-parser/dist/stream.js +0 -29
- package/node_modules/eventsource-parser/dist/stream.js.map +0 -1
- package/node_modules/eventsource-parser/package.json +0 -92
- package/node_modules/eventsource-parser/src/errors.ts +0 -44
- package/node_modules/eventsource-parser/src/index.ts +0 -3
- package/node_modules/eventsource-parser/src/parse.ts +0 -395
- package/node_modules/eventsource-parser/src/stream.ts +0 -88
- package/node_modules/eventsource-parser/src/types.ts +0 -97
- package/node_modules/eventsource-parser/stream.js +0 -2
|
@@ -1,395 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* EventSource/Server-Sent Events parser
|
|
3
|
-
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
4
|
-
*/
|
|
5
|
-
import {ParseError} from './errors.ts'
|
|
6
|
-
import type {EventSourceParser, ParserCallbacks} from './types.ts'
|
|
7
|
-
|
|
8
|
-
// ASCII codes used in the hot parsing paths.
|
|
9
|
-
const LF = 10
|
|
10
|
-
const CR = 13
|
|
11
|
-
const SPACE = 32
|
|
12
|
-
|
|
13
|
-
// oxlint-disable-next-line no-unused-vars
|
|
14
|
-
function noop(_arg: unknown) {
|
|
15
|
-
// intentional noop
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Creates a new EventSource parser.
|
|
20
|
-
*
|
|
21
|
-
* @param callbacks - Callbacks to invoke on different parsing events:
|
|
22
|
-
* - `onEvent` when a new event is parsed
|
|
23
|
-
* - `onError` when an error occurs
|
|
24
|
-
* - `onRetry` when a new reconnection interval has been sent from the server
|
|
25
|
-
* - `onComment` when a comment is encountered in the stream
|
|
26
|
-
*
|
|
27
|
-
* @returns A new EventSource parser, with `parse` and `reset` methods.
|
|
28
|
-
* @public
|
|
29
|
-
*/
|
|
30
|
-
export function createParser(callbacks: ParserCallbacks): EventSourceParser {
|
|
31
|
-
if (typeof callbacks === 'function') {
|
|
32
|
-
throw new TypeError(
|
|
33
|
-
'`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?',
|
|
34
|
-
)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const {onEvent = noop, onError = noop, onRetry = noop, onComment} = callbacks
|
|
38
|
-
|
|
39
|
-
// Trailing bytes from prior `feed()` calls that did not yet form a complete line.
|
|
40
|
-
// Stored as an array of fragments and only joined when a line terminator arrives.
|
|
41
|
-
// Concatenating per-feed (`prefix + chunk`) is O(N²) when a single SSE line spans
|
|
42
|
-
// many chunks (e.g. a large `data:` payload streamed in tiny slices, or an MCP-style
|
|
43
|
-
// server that emits one giant content block). Buffering as fragments + joining once
|
|
44
|
-
// makes the same workload linear.
|
|
45
|
-
const pendingFragments: string[] = []
|
|
46
|
-
|
|
47
|
-
let isFirstChunk = true
|
|
48
|
-
let id: string | undefined
|
|
49
|
-
let data = ''
|
|
50
|
-
let dataLines = 0
|
|
51
|
-
let eventType: string | undefined
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Feeds a chunk of the SSE stream to the parser. Any trailing bytes that do
|
|
55
|
-
* not yet form a complete line are held back and prepended to the next chunk,
|
|
56
|
-
* so callers can pass arbitrary slices of the stream without worrying about
|
|
57
|
-
* line boundaries.
|
|
58
|
-
*
|
|
59
|
-
* Per the SSE spec, a UTF-8 BOM (0xEF 0xBB 0xBF) at the start of the very
|
|
60
|
-
* first chunk is stripped before parsing.
|
|
61
|
-
*
|
|
62
|
-
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream
|
|
63
|
-
*/
|
|
64
|
-
function feed(chunk: string) {
|
|
65
|
-
if (isFirstChunk) {
|
|
66
|
-
isFirstChunk = false
|
|
67
|
-
// Match and strip UTF-8 BOM from the start of the stream, if present.
|
|
68
|
-
// (Per the spec, this is only valid at the very start of the stream)
|
|
69
|
-
if (
|
|
70
|
-
chunk.charCodeAt(0) === 0xef &&
|
|
71
|
-
chunk.charCodeAt(1) === 0xbb &&
|
|
72
|
-
chunk.charCodeAt(2) === 0xbf
|
|
73
|
-
) {
|
|
74
|
-
chunk = chunk.slice(3)
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Hot path: no buffered prefix from a prior partial line. Hand the chunk
|
|
79
|
-
// straight to `processLines`, exactly like the original implementation.
|
|
80
|
-
// Zero new work in the common case (every chunk ends with `\n\n`).
|
|
81
|
-
if (pendingFragments.length === 0) {
|
|
82
|
-
const trailing = processLines(chunk)
|
|
83
|
-
if (trailing !== '') pendingFragments.push(trailing)
|
|
84
|
-
return
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// We have a buffered prefix. If this chunk also has no terminator, append
|
|
88
|
-
// to the buffer without concatenating — that's the O(N²) trap we're
|
|
89
|
-
// avoiding (large single `data:` payload split across many tiny chunks).
|
|
90
|
-
if (chunk.indexOf('\n') === -1 && chunk.indexOf('\r') === -1) {
|
|
91
|
-
pendingFragments.push(chunk)
|
|
92
|
-
return
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Terminator arrived. Join the accumulated fragments + this chunk once,
|
|
96
|
-
// process, and buffer any new trailing partial line.
|
|
97
|
-
pendingFragments.push(chunk)
|
|
98
|
-
const input = pendingFragments.join('')
|
|
99
|
-
pendingFragments.length = 0
|
|
100
|
-
const trailing = processLines(input)
|
|
101
|
-
if (trailing !== '') pendingFragments.push(trailing)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Splits `chunk` into SSE lines and dispatches each to the appropriate handler.
|
|
106
|
-
* Returns any trailing bytes that did not terminate with a line break, so the
|
|
107
|
-
* caller can prepend them to the next chunk.
|
|
108
|
-
*
|
|
109
|
-
* The SSE spec permits three line terminators: `\n`, `\r`, and `\r\n`. Real-world
|
|
110
|
-
* streams almost always use plain `\n`, so we take a fast path when no `\r` is
|
|
111
|
-
* present in the chunk. The slow path is spec-correct but does more work per line.
|
|
112
|
-
*/
|
|
113
|
-
function processLines(chunk: string): string {
|
|
114
|
-
let searchIndex = 0
|
|
115
|
-
|
|
116
|
-
// Fast path: LF-only chunk (the common case for typical SSE servers).
|
|
117
|
-
// We can scan forward with a single `indexOf('\n')` per line and inline
|
|
118
|
-
// the hot-path branches for `data:` and `event:` without the CR bookkeeping
|
|
119
|
-
// the slow path needs.
|
|
120
|
-
if (chunk.indexOf('\r') === -1) {
|
|
121
|
-
let lfIndex = chunk.indexOf('\n', searchIndex)
|
|
122
|
-
while (lfIndex !== -1) {
|
|
123
|
-
// Blank line: end-of-event marker. Dispatch the accumulated event (if any)
|
|
124
|
-
// and reset the buffered fields. This is hoisted out of `parseLine` because
|
|
125
|
-
// it's the single most common line shape after `data:` lines.
|
|
126
|
-
if (searchIndex === lfIndex) {
|
|
127
|
-
if (dataLines > 0) {
|
|
128
|
-
onEvent({id, event: eventType, data})
|
|
129
|
-
}
|
|
130
|
-
id = undefined
|
|
131
|
-
data = ''
|
|
132
|
-
dataLines = 0
|
|
133
|
-
eventType = undefined
|
|
134
|
-
searchIndex = lfIndex + 1
|
|
135
|
-
lfIndex = chunk.indexOf('\n', searchIndex)
|
|
136
|
-
continue
|
|
137
|
-
}
|
|
138
|
-
const firstCharCode = chunk.charCodeAt(searchIndex)
|
|
139
|
-
if (isDataPrefix(chunk, searchIndex, firstCharCode)) {
|
|
140
|
-
// `data:` line — append the value to the event's data buffer.
|
|
141
|
-
// 'data:'.length === 5, 'data: '.length === 6
|
|
142
|
-
const valueStart =
|
|
143
|
-
chunk.charCodeAt(searchIndex + 5) === SPACE ? searchIndex + 6 : searchIndex + 5
|
|
144
|
-
const value = chunk.slice(valueStart, lfIndex)
|
|
145
|
-
// Fast path within a fast path: if this is the first data line AND the
|
|
146
|
-
// next char is another LF (i.e. `data:foo\n\n`), dispatch immediately
|
|
147
|
-
// without ever writing to the `data` buffer. This is the shape of a
|
|
148
|
-
// typical single-line SSE event (ChatGPT-style streams, etc.) and is
|
|
149
|
-
// hot enough to be worth the duplication.
|
|
150
|
-
if (dataLines === 0 && chunk.charCodeAt(lfIndex + 1) === LF) {
|
|
151
|
-
onEvent({id, event: eventType, data: value})
|
|
152
|
-
id = undefined
|
|
153
|
-
data = ''
|
|
154
|
-
eventType = undefined
|
|
155
|
-
searchIndex = lfIndex + 2
|
|
156
|
-
lfIndex = chunk.indexOf('\n', searchIndex)
|
|
157
|
-
continue
|
|
158
|
-
}
|
|
159
|
-
// Multi-line data: concatenate with newline separator per spec.
|
|
160
|
-
data = dataLines === 0 ? value : `${data}\n${value}`
|
|
161
|
-
dataLines++
|
|
162
|
-
} else if (isEventPrefix(chunk, searchIndex, firstCharCode)) {
|
|
163
|
-
// `event:` line — set the event type for the next dispatch. Per spec,
|
|
164
|
-
// an empty value resets `event type` to its default (undefined here).
|
|
165
|
-
// 'event:'.length === 6, 'event: '.length === 7
|
|
166
|
-
eventType =
|
|
167
|
-
chunk.slice(
|
|
168
|
-
chunk.charCodeAt(searchIndex + 6) === SPACE ? searchIndex + 7 : searchIndex + 6,
|
|
169
|
-
lfIndex,
|
|
170
|
-
) || undefined
|
|
171
|
-
} else {
|
|
172
|
-
// Everything else: `id:`, `retry:`, comment lines (`:` prefix), unknown
|
|
173
|
-
// fields, or malformed lines. These are rarer and go through the full
|
|
174
|
-
// per-line parser, which handles the SSE field grammar in detail.
|
|
175
|
-
parseLine(chunk, searchIndex, lfIndex)
|
|
176
|
-
}
|
|
177
|
-
searchIndex = lfIndex + 1
|
|
178
|
-
lfIndex = chunk.indexOf('\n', searchIndex)
|
|
179
|
-
}
|
|
180
|
-
return chunk.slice(searchIndex)
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// Slow path: the chunk contains at least one `\r`, so lines may be terminated
|
|
184
|
-
// by `\r`, `\n`, or `\r\n`. We locate the next terminator by looking at both
|
|
185
|
-
// the nearest `\r` and `\n` and picking whichever comes first.
|
|
186
|
-
while (searchIndex < chunk.length) {
|
|
187
|
-
const crIndex = chunk.indexOf('\r', searchIndex)
|
|
188
|
-
const lfIndex = chunk.indexOf('\n', searchIndex)
|
|
189
|
-
|
|
190
|
-
let lineEnd = -1
|
|
191
|
-
if (crIndex !== -1 && lfIndex !== -1) {
|
|
192
|
-
lineEnd = crIndex < lfIndex ? crIndex : lfIndex
|
|
193
|
-
} else if (crIndex !== -1) {
|
|
194
|
-
// A trailing `\r` at the very end of the chunk is ambiguous: it could be
|
|
195
|
-
// a bare-CR terminator, or the first half of a `\r\n` whose `\n` arrives
|
|
196
|
-
// in the next chunk. Defer until we see more input.
|
|
197
|
-
if (crIndex === chunk.length - 1) {
|
|
198
|
-
lineEnd = -1
|
|
199
|
-
} else {
|
|
200
|
-
lineEnd = crIndex
|
|
201
|
-
}
|
|
202
|
-
} else if (lfIndex !== -1) {
|
|
203
|
-
lineEnd = lfIndex
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
if (lineEnd === -1) {
|
|
207
|
-
break
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
parseLine(chunk, searchIndex, lineEnd)
|
|
211
|
-
searchIndex = lineEnd + 1
|
|
212
|
-
// If we just consumed a `\r` and the next char is `\n`, skip it so the
|
|
213
|
-
// pair is treated as a single terminator rather than an empty line.
|
|
214
|
-
if (chunk.charCodeAt(searchIndex - 1) === CR && chunk.charCodeAt(searchIndex) === LF) {
|
|
215
|
-
searchIndex++
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return chunk.slice(searchIndex)
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function parseLine(chunk: string, start: number, end: number) {
|
|
223
|
-
if (start === end) {
|
|
224
|
-
dispatchEvent()
|
|
225
|
-
return
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const firstCharCode = chunk.charCodeAt(start)
|
|
229
|
-
|
|
230
|
-
if (isDataPrefix(chunk, start, firstCharCode)) {
|
|
231
|
-
// 'data:'.length === 5, 'data: '.length === 6
|
|
232
|
-
const valueStart = chunk.charCodeAt(start + 5) === SPACE ? start + 6 : start + 5
|
|
233
|
-
const value = chunk.slice(valueStart, end)
|
|
234
|
-
data = dataLines === 0 ? value : `${data}\n${value}`
|
|
235
|
-
dataLines++
|
|
236
|
-
return
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (isEventPrefix(chunk, start, firstCharCode)) {
|
|
240
|
-
// 'event:'.length === 6, 'event: '.length === 7
|
|
241
|
-
eventType =
|
|
242
|
-
chunk.slice(chunk.charCodeAt(start + 6) === SPACE ? start + 7 : start + 6, end) || undefined
|
|
243
|
-
return
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// Fast path for "id:" — 'i' = 105, 'd' = 100, ':' = 58
|
|
247
|
-
if (
|
|
248
|
-
firstCharCode === 105 &&
|
|
249
|
-
chunk.charCodeAt(start + 1) === 100 &&
|
|
250
|
-
chunk.charCodeAt(start + 2) === 58
|
|
251
|
-
) {
|
|
252
|
-
// 'id:'.length === 3, 'id: '.length === 4
|
|
253
|
-
const value = chunk.slice(chunk.charCodeAt(start + 3) === SPACE ? start + 4 : start + 3, end)
|
|
254
|
-
id = value.includes('\0') ? undefined : value
|
|
255
|
-
return
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
// Comment line — ':' = 58
|
|
259
|
-
if (firstCharCode === 58) {
|
|
260
|
-
if (onComment) {
|
|
261
|
-
const line = chunk.slice(start, end)
|
|
262
|
-
// skip ':' (+1), or ': ' (+2) when a space follows
|
|
263
|
-
onComment(line.slice(chunk.charCodeAt(start + 1) === SPACE ? 2 : 1))
|
|
264
|
-
}
|
|
265
|
-
return
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
const line = chunk.slice(start, end)
|
|
269
|
-
const fieldSeparatorIndex = line.indexOf(':')
|
|
270
|
-
if (fieldSeparatorIndex === -1) {
|
|
271
|
-
processField(line, '', line)
|
|
272
|
-
return
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const field = line.slice(0, fieldSeparatorIndex)
|
|
276
|
-
// skip ':' (+1), or ': ' (+2) when a space follows
|
|
277
|
-
const offset = line.charCodeAt(fieldSeparatorIndex + 1) === SPACE ? 2 : 1
|
|
278
|
-
const value = line.slice(fieldSeparatorIndex + offset)
|
|
279
|
-
processField(field, value, line)
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
function processField(field: string, value: string, line: string) {
|
|
283
|
-
// Field names must be compared literally, with no case folding performed.
|
|
284
|
-
switch (field) {
|
|
285
|
-
case 'event':
|
|
286
|
-
// Set the `event type` buffer to field value
|
|
287
|
-
eventType = value || undefined
|
|
288
|
-
break
|
|
289
|
-
case 'data':
|
|
290
|
-
data = dataLines === 0 ? value : `${data}\n${value}`
|
|
291
|
-
dataLines++
|
|
292
|
-
break
|
|
293
|
-
case 'id':
|
|
294
|
-
// If the field value does not contain U+0000 NULL, then set the `ID` buffer to
|
|
295
|
-
// the field value. Otherwise, ignore the field.
|
|
296
|
-
id = value.includes('\0') ? undefined : value
|
|
297
|
-
break
|
|
298
|
-
case 'retry':
|
|
299
|
-
// If the field value consists of only ASCII digits, then interpret the field value as an
|
|
300
|
-
// integer in base ten, and set the event stream's reconnection time to that integer.
|
|
301
|
-
// Otherwise, ignore the field.
|
|
302
|
-
if (/^\d+$/.test(value)) {
|
|
303
|
-
onRetry(parseInt(value, 10))
|
|
304
|
-
} else {
|
|
305
|
-
onError(
|
|
306
|
-
new ParseError(`Invalid \`retry\` value: "${value}"`, {
|
|
307
|
-
type: 'invalid-retry',
|
|
308
|
-
value,
|
|
309
|
-
line,
|
|
310
|
-
}),
|
|
311
|
-
)
|
|
312
|
-
}
|
|
313
|
-
break
|
|
314
|
-
default:
|
|
315
|
-
// Otherwise, the field is ignored.
|
|
316
|
-
onError(
|
|
317
|
-
new ParseError(
|
|
318
|
-
`Unknown field "${field.length > 20 ? `${field.slice(0, 20)}…` : field}"`,
|
|
319
|
-
{type: 'unknown-field', field, value, line},
|
|
320
|
-
),
|
|
321
|
-
)
|
|
322
|
-
break
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
function dispatchEvent() {
|
|
327
|
-
if (dataLines > 0) {
|
|
328
|
-
onEvent({
|
|
329
|
-
id,
|
|
330
|
-
event: eventType,
|
|
331
|
-
data,
|
|
332
|
-
})
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
id = undefined
|
|
336
|
-
data = ''
|
|
337
|
-
dataLines = 0
|
|
338
|
-
eventType = undefined
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
function reset(options: {consume?: boolean} = {}) {
|
|
342
|
-
if (options.consume && pendingFragments.length > 0) {
|
|
343
|
-
const incompleteLine = pendingFragments.join('')
|
|
344
|
-
parseLine(incompleteLine, 0, incompleteLine.length)
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
isFirstChunk = true
|
|
348
|
-
id = undefined
|
|
349
|
-
data = ''
|
|
350
|
-
dataLines = 0
|
|
351
|
-
eventType = undefined
|
|
352
|
-
pendingFragments.length = 0
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
return {feed, reset}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
/**
|
|
359
|
-
* Checks if `chunk` starts with the literal `data:` at index `i`.
|
|
360
|
-
*
|
|
361
|
-
* Equivalent to `chunk.startsWith('data:', i)`, but benchmarks show this
|
|
362
|
-
* hand-unrolled char-code comparison is ~20% faster on common event types.
|
|
363
|
-
* The caller passes `firstCharCode` (the code at `i`) so it can be reused
|
|
364
|
-
* across prefix checks.
|
|
365
|
-
*
|
|
366
|
-
* ASCII: 'd' = 100, 'a' = 97, 't' = 116, 'a' = 97, ':' = 58
|
|
367
|
-
*/
|
|
368
|
-
function isDataPrefix(chunk: string, i: number, firstCharCode: number): boolean {
|
|
369
|
-
return (
|
|
370
|
-
firstCharCode === 100 &&
|
|
371
|
-
chunk.charCodeAt(i + 1) === 97 &&
|
|
372
|
-
chunk.charCodeAt(i + 2) === 116 &&
|
|
373
|
-
chunk.charCodeAt(i + 3) === 97 &&
|
|
374
|
-
chunk.charCodeAt(i + 4) === 58
|
|
375
|
-
)
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/**
|
|
379
|
-
* Checks if `chunk` starts with the literal `event:` at index `i`.
|
|
380
|
-
*
|
|
381
|
-
* See {@link isDataPrefix} for why this is hand-unrolled rather than using
|
|
382
|
-
* `String.prototype.startsWith`.
|
|
383
|
-
*
|
|
384
|
-
* ASCII: 'e' = 101, 'v' = 118, 'e' = 101, 'n' = 110, 't' = 116, ':' = 58
|
|
385
|
-
*/
|
|
386
|
-
function isEventPrefix(chunk: string, i: number, firstCharCode: number): boolean {
|
|
387
|
-
return (
|
|
388
|
-
firstCharCode === 101 &&
|
|
389
|
-
chunk.charCodeAt(i + 1) === 118 &&
|
|
390
|
-
chunk.charCodeAt(i + 2) === 101 &&
|
|
391
|
-
chunk.charCodeAt(i + 3) === 110 &&
|
|
392
|
-
chunk.charCodeAt(i + 4) === 116 &&
|
|
393
|
-
chunk.charCodeAt(i + 5) === 58
|
|
394
|
-
)
|
|
395
|
-
}
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import {createParser} from './parse.ts'
|
|
2
|
-
import type {EventSourceMessage, EventSourceParser} from './types.ts'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Options for the EventSourceParserStream.
|
|
6
|
-
*
|
|
7
|
-
* @public
|
|
8
|
-
*/
|
|
9
|
-
export interface StreamOptions {
|
|
10
|
-
/**
|
|
11
|
-
* Behavior when a parsing error occurs.
|
|
12
|
-
*
|
|
13
|
-
* - A custom function can be provided to handle the error.
|
|
14
|
-
* - `'terminate'` will error the stream and stop parsing.
|
|
15
|
-
* - Any other value will ignore the error and continue parsing.
|
|
16
|
-
*
|
|
17
|
-
* @defaultValue `undefined`
|
|
18
|
-
*/
|
|
19
|
-
onError?: ('terminate' | ((error: Error) => void)) | undefined
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Callback for when a reconnection interval is sent from the server.
|
|
23
|
-
*
|
|
24
|
-
* @param retry - The number of milliseconds to wait before reconnecting.
|
|
25
|
-
*/
|
|
26
|
-
onRetry?: ((retry: number) => void) | undefined
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Callback for when a comment is encountered in the stream.
|
|
30
|
-
*
|
|
31
|
-
* @param comment - The comment encountered in the stream.
|
|
32
|
-
*/
|
|
33
|
-
onComment?: ((comment: string) => void) | undefined
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* A TransformStream that ingests a stream of strings and produces a stream of `EventSourceMessage`.
|
|
38
|
-
*
|
|
39
|
-
* @example Basic usage
|
|
40
|
-
* ```
|
|
41
|
-
* const eventStream =
|
|
42
|
-
* response.body
|
|
43
|
-
* .pipeThrough(new TextDecoderStream())
|
|
44
|
-
* .pipeThrough(new EventSourceParserStream())
|
|
45
|
-
* ```
|
|
46
|
-
*
|
|
47
|
-
* @example Terminate stream on parsing errors
|
|
48
|
-
* ```
|
|
49
|
-
* const eventStream =
|
|
50
|
-
* response.body
|
|
51
|
-
* .pipeThrough(new TextDecoderStream())
|
|
52
|
-
* .pipeThrough(new EventSourceParserStream({terminateOnError: true}))
|
|
53
|
-
* ```
|
|
54
|
-
*
|
|
55
|
-
* @public
|
|
56
|
-
*/
|
|
57
|
-
export class EventSourceParserStream extends TransformStream<string, EventSourceMessage> {
|
|
58
|
-
constructor({onError, onRetry, onComment}: StreamOptions = {}) {
|
|
59
|
-
let parser!: EventSourceParser
|
|
60
|
-
|
|
61
|
-
super({
|
|
62
|
-
start(controller) {
|
|
63
|
-
parser = createParser({
|
|
64
|
-
onEvent: (event) => {
|
|
65
|
-
controller.enqueue(event)
|
|
66
|
-
},
|
|
67
|
-
onError(error) {
|
|
68
|
-
if (onError === 'terminate') {
|
|
69
|
-
controller.error(error)
|
|
70
|
-
} else if (typeof onError === 'function') {
|
|
71
|
-
onError(error)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Ignore by default
|
|
75
|
-
},
|
|
76
|
-
onRetry,
|
|
77
|
-
onComment,
|
|
78
|
-
})
|
|
79
|
-
},
|
|
80
|
-
transform(chunk) {
|
|
81
|
-
parser.feed(chunk)
|
|
82
|
-
},
|
|
83
|
-
})
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export {type ErrorType, ParseError} from './errors.ts'
|
|
88
|
-
export type {EventSourceMessage} from './types.ts'
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import type {ParseError} from './errors.ts'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* EventSource parser instance.
|
|
5
|
-
*
|
|
6
|
-
* Needs to be reset between reconnections/when switching data source, using the `reset()` method.
|
|
7
|
-
*
|
|
8
|
-
* @public
|
|
9
|
-
*/
|
|
10
|
-
export interface EventSourceParser {
|
|
11
|
-
/**
|
|
12
|
-
* Feeds the parser another chunk. The method _does not_ return a parsed message.
|
|
13
|
-
* Instead, callbacks passed when creating the parser will be triggered once we see enough data
|
|
14
|
-
* for a valid/invalid parsing step (see {@link ParserCallbacks}).
|
|
15
|
-
*
|
|
16
|
-
* @param chunk - The chunk to parse. Can be a partial, eg in the case of streaming messages.
|
|
17
|
-
* @public
|
|
18
|
-
*/
|
|
19
|
-
feed(chunk: string): void
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Resets the parser state. This is required when you have a new stream of messages -
|
|
23
|
-
* for instance in the case of a client being disconnected and reconnecting.
|
|
24
|
-
*
|
|
25
|
-
* Previously received, incomplete data will NOT be parsed unless you pass `consume: true`,
|
|
26
|
-
* which tells the parser to attempt to consume any incomplete data as if it ended with a newline
|
|
27
|
-
* character. This is useful for cases when a server sends a non-EventSource message that you
|
|
28
|
-
* want to be able to react to in an `onError` callback.
|
|
29
|
-
*
|
|
30
|
-
* @public
|
|
31
|
-
*/
|
|
32
|
-
reset(options?: {consume?: boolean}): void
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* A parsed EventSource message event
|
|
37
|
-
*
|
|
38
|
-
* @public
|
|
39
|
-
*/
|
|
40
|
-
export interface EventSourceMessage {
|
|
41
|
-
/**
|
|
42
|
-
* The event type sent from the server. Note that this differs from the browser `EventSource`
|
|
43
|
-
* implementation in that browsers will default this to `message`, whereas this parser will
|
|
44
|
-
* leave this as `undefined` if not explicitly declared.
|
|
45
|
-
*/
|
|
46
|
-
event?: string | undefined
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* ID of the message, if any was provided by the server. Can be used by clients to keep the
|
|
50
|
-
* last received message ID in sync when reconnecting.
|
|
51
|
-
*/
|
|
52
|
-
id?: string | undefined
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* The data received for this message
|
|
56
|
-
*/
|
|
57
|
-
data: string
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Callbacks that can be passed to the parser to handle different types of parsed messages
|
|
62
|
-
* and errors.
|
|
63
|
-
*
|
|
64
|
-
* @public
|
|
65
|
-
*/
|
|
66
|
-
export interface ParserCallbacks {
|
|
67
|
-
/**
|
|
68
|
-
* Callback for when a new event/message is parsed from the stream.
|
|
69
|
-
* This is the main callback that clients will use to handle incoming messages.
|
|
70
|
-
*
|
|
71
|
-
* @param event - The parsed event/message
|
|
72
|
-
*/
|
|
73
|
-
onEvent?: ((event: EventSourceMessage) => void) | undefined
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Callback for when the server sends a new reconnection interval through the `retry` field.
|
|
77
|
-
*
|
|
78
|
-
* @param retry - The number of milliseconds to wait before reconnecting.
|
|
79
|
-
*/
|
|
80
|
-
onRetry?: ((retry: number) => void) | undefined
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Callback for when a comment is encountered in the stream.
|
|
84
|
-
*
|
|
85
|
-
* @param comment - The comment encountered in the stream.
|
|
86
|
-
*/
|
|
87
|
-
onComment?: ((comment: string) => void) | undefined
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Callback for when an error occurs during parsing. This is a catch-all for any errors
|
|
91
|
-
* that occur during parsing, and can be used to handle them in a custom way. Most clients
|
|
92
|
-
* tend to silently ignore any errors and instead retry, but it can be helpful to log/debug.
|
|
93
|
-
*
|
|
94
|
-
* @param error - The error that occurred during parsing
|
|
95
|
-
*/
|
|
96
|
-
onError?: ((error: ParseError) => void) | undefined
|
|
97
|
-
}
|