@silver886/mcp-proxy 0.2.2 → 0.2.4
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/package.json +15 -2
- package/scripts/fetch.mjs +87 -0
- package/node_modules/cloudflared/LICENSE +0 -21
- package/node_modules/cloudflared/README.md +0 -156
- package/node_modules/cloudflared/bin/cloudflared +0 -0
- 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,166 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
-
class ParseError extends Error {
|
|
4
|
-
constructor(message, options) {
|
|
5
|
-
super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
const LF = 10, CR = 13, SPACE = 32;
|
|
9
|
-
function noop(_arg) {
|
|
10
|
-
}
|
|
11
|
-
function createParser(callbacks) {
|
|
12
|
-
if (typeof callbacks == "function")
|
|
13
|
-
throw new TypeError(
|
|
14
|
-
"`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?"
|
|
15
|
-
);
|
|
16
|
-
const { onEvent = noop, onError = noop, onRetry = noop, onComment } = callbacks, pendingFragments = [];
|
|
17
|
-
let isFirstChunk = !0, id, data = "", dataLines = 0, eventType;
|
|
18
|
-
function feed(chunk) {
|
|
19
|
-
if (isFirstChunk && (isFirstChunk = !1, chunk.charCodeAt(0) === 239 && chunk.charCodeAt(1) === 187 && chunk.charCodeAt(2) === 191 && (chunk = chunk.slice(3))), pendingFragments.length === 0) {
|
|
20
|
-
const trailing2 = processLines(chunk);
|
|
21
|
-
trailing2 !== "" && pendingFragments.push(trailing2);
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
if (chunk.indexOf(`
|
|
25
|
-
`) === -1 && chunk.indexOf("\r") === -1) {
|
|
26
|
-
pendingFragments.push(chunk);
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
pendingFragments.push(chunk);
|
|
30
|
-
const input = pendingFragments.join("");
|
|
31
|
-
pendingFragments.length = 0;
|
|
32
|
-
const trailing = processLines(input);
|
|
33
|
-
trailing !== "" && pendingFragments.push(trailing);
|
|
34
|
-
}
|
|
35
|
-
function processLines(chunk) {
|
|
36
|
-
let searchIndex = 0;
|
|
37
|
-
if (chunk.indexOf("\r") === -1) {
|
|
38
|
-
let lfIndex = chunk.indexOf(`
|
|
39
|
-
`, searchIndex);
|
|
40
|
-
for (; lfIndex !== -1; ) {
|
|
41
|
-
if (searchIndex === lfIndex) {
|
|
42
|
-
dataLines > 0 && onEvent({ id, event: eventType, data }), id = void 0, data = "", dataLines = 0, eventType = void 0, searchIndex = lfIndex + 1, lfIndex = chunk.indexOf(`
|
|
43
|
-
`, searchIndex);
|
|
44
|
-
continue;
|
|
45
|
-
}
|
|
46
|
-
const firstCharCode = chunk.charCodeAt(searchIndex);
|
|
47
|
-
if (isDataPrefix(chunk, searchIndex, firstCharCode)) {
|
|
48
|
-
const valueStart = chunk.charCodeAt(searchIndex + 5) === SPACE ? searchIndex + 6 : searchIndex + 5, value = chunk.slice(valueStart, lfIndex);
|
|
49
|
-
if (dataLines === 0 && chunk.charCodeAt(lfIndex + 1) === LF) {
|
|
50
|
-
onEvent({ id, event: eventType, data: value }), id = void 0, data = "", eventType = void 0, searchIndex = lfIndex + 2, lfIndex = chunk.indexOf(`
|
|
51
|
-
`, searchIndex);
|
|
52
|
-
continue;
|
|
53
|
-
}
|
|
54
|
-
data = dataLines === 0 ? value : `${data}
|
|
55
|
-
${value}`, dataLines++;
|
|
56
|
-
} else isEventPrefix(chunk, searchIndex, firstCharCode) ? eventType = chunk.slice(
|
|
57
|
-
chunk.charCodeAt(searchIndex + 6) === SPACE ? searchIndex + 7 : searchIndex + 6,
|
|
58
|
-
lfIndex
|
|
59
|
-
) || void 0 : parseLine(chunk, searchIndex, lfIndex);
|
|
60
|
-
searchIndex = lfIndex + 1, lfIndex = chunk.indexOf(`
|
|
61
|
-
`, searchIndex);
|
|
62
|
-
}
|
|
63
|
-
return chunk.slice(searchIndex);
|
|
64
|
-
}
|
|
65
|
-
for (; searchIndex < chunk.length; ) {
|
|
66
|
-
const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
|
|
67
|
-
`, searchIndex);
|
|
68
|
-
let lineEnd = -1;
|
|
69
|
-
if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = crIndex < lfIndex ? crIndex : lfIndex : crIndex !== -1 ? crIndex === chunk.length - 1 ? lineEnd = -1 : lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1)
|
|
70
|
-
break;
|
|
71
|
-
parseLine(chunk, searchIndex, lineEnd), searchIndex = lineEnd + 1, chunk.charCodeAt(searchIndex - 1) === CR && chunk.charCodeAt(searchIndex) === LF && searchIndex++;
|
|
72
|
-
}
|
|
73
|
-
return chunk.slice(searchIndex);
|
|
74
|
-
}
|
|
75
|
-
function parseLine(chunk, start, end) {
|
|
76
|
-
if (start === end) {
|
|
77
|
-
dispatchEvent();
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
const firstCharCode = chunk.charCodeAt(start);
|
|
81
|
-
if (isDataPrefix(chunk, start, firstCharCode)) {
|
|
82
|
-
const valueStart = chunk.charCodeAt(start + 5) === SPACE ? start + 6 : start + 5, value2 = chunk.slice(valueStart, end);
|
|
83
|
-
data = dataLines === 0 ? value2 : `${data}
|
|
84
|
-
${value2}`, dataLines++;
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
if (isEventPrefix(chunk, start, firstCharCode)) {
|
|
88
|
-
eventType = chunk.slice(chunk.charCodeAt(start + 6) === SPACE ? start + 7 : start + 6, end) || void 0;
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
if (firstCharCode === 105 && chunk.charCodeAt(start + 1) === 100 && chunk.charCodeAt(start + 2) === 58) {
|
|
92
|
-
const value2 = chunk.slice(chunk.charCodeAt(start + 3) === SPACE ? start + 4 : start + 3, end);
|
|
93
|
-
id = value2.includes("\0") ? void 0 : value2;
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
if (firstCharCode === 58) {
|
|
97
|
-
if (onComment) {
|
|
98
|
-
const line2 = chunk.slice(start, end);
|
|
99
|
-
onComment(line2.slice(chunk.charCodeAt(start + 1) === SPACE ? 2 : 1));
|
|
100
|
-
}
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
const line = chunk.slice(start, end), fieldSeparatorIndex = line.indexOf(":");
|
|
104
|
-
if (fieldSeparatorIndex === -1) {
|
|
105
|
-
processField(line, "", line);
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
const field = line.slice(0, fieldSeparatorIndex), offset = line.charCodeAt(fieldSeparatorIndex + 1) === SPACE ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
|
|
109
|
-
processField(field, value, line);
|
|
110
|
-
}
|
|
111
|
-
function processField(field, value, line) {
|
|
112
|
-
switch (field) {
|
|
113
|
-
case "event":
|
|
114
|
-
eventType = value || void 0;
|
|
115
|
-
break;
|
|
116
|
-
case "data":
|
|
117
|
-
data = dataLines === 0 ? value : `${data}
|
|
118
|
-
${value}`, dataLines++;
|
|
119
|
-
break;
|
|
120
|
-
case "id":
|
|
121
|
-
id = value.includes("\0") ? void 0 : value;
|
|
122
|
-
break;
|
|
123
|
-
case "retry":
|
|
124
|
-
/^\d+$/.test(value) ? onRetry(parseInt(value, 10)) : onError(
|
|
125
|
-
new ParseError(`Invalid \`retry\` value: "${value}"`, {
|
|
126
|
-
type: "invalid-retry",
|
|
127
|
-
value,
|
|
128
|
-
line
|
|
129
|
-
})
|
|
130
|
-
);
|
|
131
|
-
break;
|
|
132
|
-
default:
|
|
133
|
-
onError(
|
|
134
|
-
new ParseError(
|
|
135
|
-
`Unknown field "${field.length > 20 ? `${field.slice(0, 20)}\u2026` : field}"`,
|
|
136
|
-
{ type: "unknown-field", field, value, line }
|
|
137
|
-
)
|
|
138
|
-
);
|
|
139
|
-
break;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
function dispatchEvent() {
|
|
143
|
-
dataLines > 0 && onEvent({
|
|
144
|
-
id,
|
|
145
|
-
event: eventType,
|
|
146
|
-
data
|
|
147
|
-
}), id = void 0, data = "", dataLines = 0, eventType = void 0;
|
|
148
|
-
}
|
|
149
|
-
function reset(options = {}) {
|
|
150
|
-
if (options.consume && pendingFragments.length > 0) {
|
|
151
|
-
const incompleteLine = pendingFragments.join("");
|
|
152
|
-
parseLine(incompleteLine, 0, incompleteLine.length);
|
|
153
|
-
}
|
|
154
|
-
isFirstChunk = !0, id = void 0, data = "", dataLines = 0, eventType = void 0, pendingFragments.length = 0;
|
|
155
|
-
}
|
|
156
|
-
return { feed, reset };
|
|
157
|
-
}
|
|
158
|
-
function isDataPrefix(chunk, i, firstCharCode) {
|
|
159
|
-
return firstCharCode === 100 && chunk.charCodeAt(i + 1) === 97 && chunk.charCodeAt(i + 2) === 116 && chunk.charCodeAt(i + 3) === 97 && chunk.charCodeAt(i + 4) === 58;
|
|
160
|
-
}
|
|
161
|
-
function isEventPrefix(chunk, i, firstCharCode) {
|
|
162
|
-
return firstCharCode === 101 && chunk.charCodeAt(i + 1) === 118 && chunk.charCodeAt(i + 2) === 101 && chunk.charCodeAt(i + 3) === 110 && chunk.charCodeAt(i + 4) === 116 && chunk.charCodeAt(i + 5) === 58;
|
|
163
|
-
}
|
|
164
|
-
exports.ParseError = ParseError;
|
|
165
|
-
exports.createParser = createParser;
|
|
166
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/errors.ts","../src/parse.ts"],"sourcesContent":["/**\n * The type of error that occurred.\n * @public\n */\nexport type ErrorType = 'invalid-retry' | 'unknown-field'\n\n/**\n * Error thrown when encountering an issue during parsing.\n *\n * @public\n */\nexport class ParseError extends Error {\n /**\n * The type of error that occurred.\n */\n type: ErrorType\n\n /**\n * In the case of an unknown field encountered in the stream, this will be the field name.\n */\n field?: string | undefined\n\n /**\n * In the case of an unknown field encountered in the stream, this will be the value of the field.\n */\n value?: string | undefined\n\n /**\n * The line that caused the error, if available.\n */\n line?: string | undefined\n\n constructor(\n message: string,\n options: {type: ErrorType; field?: string; value?: string; line?: string},\n ) {\n super(message)\n this.name = 'ParseError'\n this.type = options.type\n this.field = options.field\n this.value = options.value\n this.line = options.line\n }\n}\n","/**\n * EventSource/Server-Sent Events parser\n * @see https://html.spec.whatwg.org/multipage/server-sent-events.html\n */\nimport {ParseError} from './errors.ts'\nimport type {EventSourceParser, ParserCallbacks} from './types.ts'\n\n// ASCII codes used in the hot parsing paths.\nconst LF = 10\nconst CR = 13\nconst SPACE = 32\n\n// oxlint-disable-next-line no-unused-vars\nfunction noop(_arg: unknown) {\n // intentional noop\n}\n\n/**\n * Creates a new EventSource parser.\n *\n * @param callbacks - Callbacks to invoke on different parsing events:\n * - `onEvent` when a new event is parsed\n * - `onError` when an error occurs\n * - `onRetry` when a new reconnection interval has been sent from the server\n * - `onComment` when a comment is encountered in the stream\n *\n * @returns A new EventSource parser, with `parse` and `reset` methods.\n * @public\n */\nexport function createParser(callbacks: ParserCallbacks): EventSourceParser {\n if (typeof callbacks === 'function') {\n throw new TypeError(\n '`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?',\n )\n }\n\n const {onEvent = noop, onError = noop, onRetry = noop, onComment} = callbacks\n\n // Trailing bytes from prior `feed()` calls that did not yet form a complete line.\n // Stored as an array of fragments and only joined when a line terminator arrives.\n // Concatenating per-feed (`prefix + chunk`) is O(N²) when a single SSE line spans\n // many chunks (e.g. a large `data:` payload streamed in tiny slices, or an MCP-style\n // server that emits one giant content block). Buffering as fragments + joining once\n // makes the same workload linear.\n const pendingFragments: string[] = []\n\n let isFirstChunk = true\n let id: string | undefined\n let data = ''\n let dataLines = 0\n let eventType: string | undefined\n\n /**\n * Feeds a chunk of the SSE stream to the parser. Any trailing bytes that do\n * not yet form a complete line are held back and prepended to the next chunk,\n * so callers can pass arbitrary slices of the stream without worrying about\n * line boundaries.\n *\n * Per the SSE spec, a UTF-8 BOM (0xEF 0xBB 0xBF) at the start of the very\n * first chunk is stripped before parsing.\n *\n * @see https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream\n */\n function feed(chunk: string) {\n if (isFirstChunk) {\n isFirstChunk = false\n // Match and strip UTF-8 BOM from the start of the stream, if present.\n // (Per the spec, this is only valid at the very start of the stream)\n if (\n chunk.charCodeAt(0) === 0xef &&\n chunk.charCodeAt(1) === 0xbb &&\n chunk.charCodeAt(2) === 0xbf\n ) {\n chunk = chunk.slice(3)\n }\n }\n\n // Hot path: no buffered prefix from a prior partial line. Hand the chunk\n // straight to `processLines`, exactly like the original implementation.\n // Zero new work in the common case (every chunk ends with `\\n\\n`).\n if (pendingFragments.length === 0) {\n const trailing = processLines(chunk)\n if (trailing !== '') pendingFragments.push(trailing)\n return\n }\n\n // We have a buffered prefix. If this chunk also has no terminator, append\n // to the buffer without concatenating — that's the O(N²) trap we're\n // avoiding (large single `data:` payload split across many tiny chunks).\n if (chunk.indexOf('\\n') === -1 && chunk.indexOf('\\r') === -1) {\n pendingFragments.push(chunk)\n return\n }\n\n // Terminator arrived. Join the accumulated fragments + this chunk once,\n // process, and buffer any new trailing partial line.\n pendingFragments.push(chunk)\n const input = pendingFragments.join('')\n pendingFragments.length = 0\n const trailing = processLines(input)\n if (trailing !== '') pendingFragments.push(trailing)\n }\n\n /**\n * Splits `chunk` into SSE lines and dispatches each to the appropriate handler.\n * Returns any trailing bytes that did not terminate with a line break, so the\n * caller can prepend them to the next chunk.\n *\n * The SSE spec permits three line terminators: `\\n`, `\\r`, and `\\r\\n`. Real-world\n * streams almost always use plain `\\n`, so we take a fast path when no `\\r` is\n * present in the chunk. The slow path is spec-correct but does more work per line.\n */\n function processLines(chunk: string): string {\n let searchIndex = 0\n\n // Fast path: LF-only chunk (the common case for typical SSE servers).\n // We can scan forward with a single `indexOf('\\n')` per line and inline\n // the hot-path branches for `data:` and `event:` without the CR bookkeeping\n // the slow path needs.\n if (chunk.indexOf('\\r') === -1) {\n let lfIndex = chunk.indexOf('\\n', searchIndex)\n while (lfIndex !== -1) {\n // Blank line: end-of-event marker. Dispatch the accumulated event (if any)\n // and reset the buffered fields. This is hoisted out of `parseLine` because\n // it's the single most common line shape after `data:` lines.\n if (searchIndex === lfIndex) {\n if (dataLines > 0) {\n onEvent({id, event: eventType, data})\n }\n id = undefined\n data = ''\n dataLines = 0\n eventType = undefined\n searchIndex = lfIndex + 1\n lfIndex = chunk.indexOf('\\n', searchIndex)\n continue\n }\n const firstCharCode = chunk.charCodeAt(searchIndex)\n if (isDataPrefix(chunk, searchIndex, firstCharCode)) {\n // `data:` line — append the value to the event's data buffer.\n // 'data:'.length === 5, 'data: '.length === 6\n const valueStart =\n chunk.charCodeAt(searchIndex + 5) === SPACE ? searchIndex + 6 : searchIndex + 5\n const value = chunk.slice(valueStart, lfIndex)\n // Fast path within a fast path: if this is the first data line AND the\n // next char is another LF (i.e. `data:foo\\n\\n`), dispatch immediately\n // without ever writing to the `data` buffer. This is the shape of a\n // typical single-line SSE event (ChatGPT-style streams, etc.) and is\n // hot enough to be worth the duplication.\n if (dataLines === 0 && chunk.charCodeAt(lfIndex + 1) === LF) {\n onEvent({id, event: eventType, data: value})\n id = undefined\n data = ''\n eventType = undefined\n searchIndex = lfIndex + 2\n lfIndex = chunk.indexOf('\\n', searchIndex)\n continue\n }\n // Multi-line data: concatenate with newline separator per spec.\n data = dataLines === 0 ? value : `${data}\\n${value}`\n dataLines++\n } else if (isEventPrefix(chunk, searchIndex, firstCharCode)) {\n // `event:` line — set the event type for the next dispatch. Per spec,\n // an empty value resets `event type` to its default (undefined here).\n // 'event:'.length === 6, 'event: '.length === 7\n eventType =\n chunk.slice(\n chunk.charCodeAt(searchIndex + 6) === SPACE ? searchIndex + 7 : searchIndex + 6,\n lfIndex,\n ) || undefined\n } else {\n // Everything else: `id:`, `retry:`, comment lines (`:` prefix), unknown\n // fields, or malformed lines. These are rarer and go through the full\n // per-line parser, which handles the SSE field grammar in detail.\n parseLine(chunk, searchIndex, lfIndex)\n }\n searchIndex = lfIndex + 1\n lfIndex = chunk.indexOf('\\n', searchIndex)\n }\n return chunk.slice(searchIndex)\n }\n\n // Slow path: the chunk contains at least one `\\r`, so lines may be terminated\n // by `\\r`, `\\n`, or `\\r\\n`. We locate the next terminator by looking at both\n // the nearest `\\r` and `\\n` and picking whichever comes first.\n while (searchIndex < chunk.length) {\n const crIndex = chunk.indexOf('\\r', searchIndex)\n const lfIndex = chunk.indexOf('\\n', searchIndex)\n\n let lineEnd = -1\n if (crIndex !== -1 && lfIndex !== -1) {\n lineEnd = crIndex < lfIndex ? crIndex : lfIndex\n } else if (crIndex !== -1) {\n // A trailing `\\r` at the very end of the chunk is ambiguous: it could be\n // a bare-CR terminator, or the first half of a `\\r\\n` whose `\\n` arrives\n // in the next chunk. Defer until we see more input.\n if (crIndex === chunk.length - 1) {\n lineEnd = -1\n } else {\n lineEnd = crIndex\n }\n } else if (lfIndex !== -1) {\n lineEnd = lfIndex\n }\n\n if (lineEnd === -1) {\n break\n }\n\n parseLine(chunk, searchIndex, lineEnd)\n searchIndex = lineEnd + 1\n // If we just consumed a `\\r` and the next char is `\\n`, skip it so the\n // pair is treated as a single terminator rather than an empty line.\n if (chunk.charCodeAt(searchIndex - 1) === CR && chunk.charCodeAt(searchIndex) === LF) {\n searchIndex++\n }\n }\n\n return chunk.slice(searchIndex)\n }\n\n function parseLine(chunk: string, start: number, end: number) {\n if (start === end) {\n dispatchEvent()\n return\n }\n\n const firstCharCode = chunk.charCodeAt(start)\n\n if (isDataPrefix(chunk, start, firstCharCode)) {\n // 'data:'.length === 5, 'data: '.length === 6\n const valueStart = chunk.charCodeAt(start + 5) === SPACE ? start + 6 : start + 5\n const value = chunk.slice(valueStart, end)\n data = dataLines === 0 ? value : `${data}\\n${value}`\n dataLines++\n return\n }\n\n if (isEventPrefix(chunk, start, firstCharCode)) {\n // 'event:'.length === 6, 'event: '.length === 7\n eventType =\n chunk.slice(chunk.charCodeAt(start + 6) === SPACE ? start + 7 : start + 6, end) || undefined\n return\n }\n\n // Fast path for \"id:\" — 'i' = 105, 'd' = 100, ':' = 58\n if (\n firstCharCode === 105 &&\n chunk.charCodeAt(start + 1) === 100 &&\n chunk.charCodeAt(start + 2) === 58\n ) {\n // 'id:'.length === 3, 'id: '.length === 4\n const value = chunk.slice(chunk.charCodeAt(start + 3) === SPACE ? start + 4 : start + 3, end)\n id = value.includes('\\0') ? undefined : value\n return\n }\n\n // Comment line — ':' = 58\n if (firstCharCode === 58) {\n if (onComment) {\n const line = chunk.slice(start, end)\n // skip ':' (+1), or ': ' (+2) when a space follows\n onComment(line.slice(chunk.charCodeAt(start + 1) === SPACE ? 2 : 1))\n }\n return\n }\n\n const line = chunk.slice(start, end)\n const fieldSeparatorIndex = line.indexOf(':')\n if (fieldSeparatorIndex === -1) {\n processField(line, '', line)\n return\n }\n\n const field = line.slice(0, fieldSeparatorIndex)\n // skip ':' (+1), or ': ' (+2) when a space follows\n const offset = line.charCodeAt(fieldSeparatorIndex + 1) === SPACE ? 2 : 1\n const value = line.slice(fieldSeparatorIndex + offset)\n processField(field, value, line)\n }\n\n function processField(field: string, value: string, line: string) {\n // Field names must be compared literally, with no case folding performed.\n switch (field) {\n case 'event':\n // Set the `event type` buffer to field value\n eventType = value || undefined\n break\n case 'data':\n data = dataLines === 0 ? value : `${data}\\n${value}`\n dataLines++\n break\n case 'id':\n // If the field value does not contain U+0000 NULL, then set the `ID` buffer to\n // the field value. Otherwise, ignore the field.\n id = value.includes('\\0') ? undefined : value\n break\n case 'retry':\n // If the field value consists of only ASCII digits, then interpret the field value as an\n // integer in base ten, and set the event stream's reconnection time to that integer.\n // Otherwise, ignore the field.\n if (/^\\d+$/.test(value)) {\n onRetry(parseInt(value, 10))\n } else {\n onError(\n new ParseError(`Invalid \\`retry\\` value: \"${value}\"`, {\n type: 'invalid-retry',\n value,\n line,\n }),\n )\n }\n break\n default:\n // Otherwise, the field is ignored.\n onError(\n new ParseError(\n `Unknown field \"${field.length > 20 ? `${field.slice(0, 20)}…` : field}\"`,\n {type: 'unknown-field', field, value, line},\n ),\n )\n break\n }\n }\n\n function dispatchEvent() {\n if (dataLines > 0) {\n onEvent({\n id,\n event: eventType,\n data,\n })\n }\n\n id = undefined\n data = ''\n dataLines = 0\n eventType = undefined\n }\n\n function reset(options: {consume?: boolean} = {}) {\n if (options.consume && pendingFragments.length > 0) {\n const incompleteLine = pendingFragments.join('')\n parseLine(incompleteLine, 0, incompleteLine.length)\n }\n\n isFirstChunk = true\n id = undefined\n data = ''\n dataLines = 0\n eventType = undefined\n pendingFragments.length = 0\n }\n\n return {feed, reset}\n}\n\n/**\n * Checks if `chunk` starts with the literal `data:` at index `i`.\n *\n * Equivalent to `chunk.startsWith('data:', i)`, but benchmarks show this\n * hand-unrolled char-code comparison is ~20% faster on common event types.\n * The caller passes `firstCharCode` (the code at `i`) so it can be reused\n * across prefix checks.\n *\n * ASCII: 'd' = 100, 'a' = 97, 't' = 116, 'a' = 97, ':' = 58\n */\nfunction isDataPrefix(chunk: string, i: number, firstCharCode: number): boolean {\n return (\n firstCharCode === 100 &&\n chunk.charCodeAt(i + 1) === 97 &&\n chunk.charCodeAt(i + 2) === 116 &&\n chunk.charCodeAt(i + 3) === 97 &&\n chunk.charCodeAt(i + 4) === 58\n )\n}\n\n/**\n * Checks if `chunk` starts with the literal `event:` at index `i`.\n *\n * See {@link isDataPrefix} for why this is hand-unrolled rather than using\n * `String.prototype.startsWith`.\n *\n * ASCII: 'e' = 101, 'v' = 118, 'e' = 101, 'n' = 110, 't' = 116, ':' = 58\n */\nfunction isEventPrefix(chunk: string, i: number, firstCharCode: number): boolean {\n return (\n firstCharCode === 101 &&\n chunk.charCodeAt(i + 1) === 118 &&\n chunk.charCodeAt(i + 2) === 101 &&\n chunk.charCodeAt(i + 3) === 110 &&\n chunk.charCodeAt(i + 4) === 116 &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O;AAAA,MACP;AAAA,IAAA,CACD,GAGH,KAAK,QACL,OAAO,IACP,YAAY,GACZ,YAAY;AAAA,EACd;AAEA,WAAS,MAAM,UAA+B,IAAI;AAChD,QAAI,QAAQ,WAAW,iBAAiB,SAAS,GAAG;AAClD,YAAM,iBAAiB,iBAAiB,KAAK,EAAE;AAC/C,gBAAU,gBAAgB,GAAG,eAAe,MAAM;AAAA,IACpD;AAEA,mBAAe,IACf,KAAK,QACL,OAAO,IACP,YAAY,GACZ,YAAY,QACZ,iBAAiB,SAAS;AAAA,EAC5B;AAEA,SAAO,EAAC,MAAM,MAAA;AAChB;AAYA,SAAS,aAAa,OAAe,GAAW,eAAgC;AAC9E,SACE,kBAAkB,OAClB,MAAM,WAAW,IAAI,CAAC,MAAM,MAC5B,MAAM,WAAW,IAAI,CAAC,MAAM,OAC5B,MAAM,WAAW,IAAI,CAAC,MAAM,MAC5B,MAAM,WAAW,IAAI,CAAC,MAAM;AAEhC;AAUA,SAAS,cAAc,OAAe,GAAW,eAAgC;AAC/E,SACE,kBAAkB,OAClB,MAAM,WAAW,IAAI,CAAC,MAAM,OAC5B,MAAM,WAAW,IAAI,CAAC,MAAM,OAC5B,MAAM,WAAW,IAAI,CAAC,MAAM,OAC5B,MAAM,WAAW,IAAI,CAAC,MAAM,OAC5B,MAAM,WAAW,IAAI,CAAC,MAAM;AAEhC;;;"}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates a new EventSource parser.
|
|
3
|
-
*
|
|
4
|
-
* @param callbacks - Callbacks to invoke on different parsing events:
|
|
5
|
-
* - `onEvent` when a new event is parsed
|
|
6
|
-
* - `onError` when an error occurs
|
|
7
|
-
* - `onRetry` when a new reconnection interval has been sent from the server
|
|
8
|
-
* - `onComment` when a comment is encountered in the stream
|
|
9
|
-
*
|
|
10
|
-
* @returns A new EventSource parser, with `parse` and `reset` methods.
|
|
11
|
-
* @public
|
|
12
|
-
*/
|
|
13
|
-
export declare function createParser(
|
|
14
|
-
callbacks: ParserCallbacks,
|
|
15
|
-
): EventSourceParser;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* The type of error that occurred.
|
|
19
|
-
* @public
|
|
20
|
-
*/
|
|
21
|
-
export declare type ErrorType = "invalid-retry" | "unknown-field";
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* A parsed EventSource message event
|
|
25
|
-
*
|
|
26
|
-
* @public
|
|
27
|
-
*/
|
|
28
|
-
export declare interface EventSourceMessage {
|
|
29
|
-
/**
|
|
30
|
-
* The event type sent from the server. Note that this differs from the browser `EventSource`
|
|
31
|
-
* implementation in that browsers will default this to `message`, whereas this parser will
|
|
32
|
-
* leave this as `undefined` if not explicitly declared.
|
|
33
|
-
*/
|
|
34
|
-
event?: string | undefined;
|
|
35
|
-
/**
|
|
36
|
-
* ID of the message, if any was provided by the server. Can be used by clients to keep the
|
|
37
|
-
* last received message ID in sync when reconnecting.
|
|
38
|
-
*/
|
|
39
|
-
id?: string | undefined;
|
|
40
|
-
/**
|
|
41
|
-
* The data received for this message
|
|
42
|
-
*/
|
|
43
|
-
data: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* EventSource parser instance.
|
|
48
|
-
*
|
|
49
|
-
* Needs to be reset between reconnections/when switching data source, using the `reset()` method.
|
|
50
|
-
*
|
|
51
|
-
* @public
|
|
52
|
-
*/
|
|
53
|
-
export declare interface EventSourceParser {
|
|
54
|
-
/**
|
|
55
|
-
* Feeds the parser another chunk. The method _does not_ return a parsed message.
|
|
56
|
-
* Instead, callbacks passed when creating the parser will be triggered once we see enough data
|
|
57
|
-
* for a valid/invalid parsing step (see {@link ParserCallbacks}).
|
|
58
|
-
*
|
|
59
|
-
* @param chunk - The chunk to parse. Can be a partial, eg in the case of streaming messages.
|
|
60
|
-
* @public
|
|
61
|
-
*/
|
|
62
|
-
feed(chunk: string): void;
|
|
63
|
-
/**
|
|
64
|
-
* Resets the parser state. This is required when you have a new stream of messages -
|
|
65
|
-
* for instance in the case of a client being disconnected and reconnecting.
|
|
66
|
-
*
|
|
67
|
-
* Previously received, incomplete data will NOT be parsed unless you pass `consume: true`,
|
|
68
|
-
* which tells the parser to attempt to consume any incomplete data as if it ended with a newline
|
|
69
|
-
* character. This is useful for cases when a server sends a non-EventSource message that you
|
|
70
|
-
* want to be able to react to in an `onError` callback.
|
|
71
|
-
*
|
|
72
|
-
* @public
|
|
73
|
-
*/
|
|
74
|
-
reset(options?: { consume?: boolean }): void;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Error thrown when encountering an issue during parsing.
|
|
79
|
-
*
|
|
80
|
-
* @public
|
|
81
|
-
*/
|
|
82
|
-
export declare class ParseError extends Error {
|
|
83
|
-
/**
|
|
84
|
-
* The type of error that occurred.
|
|
85
|
-
*/
|
|
86
|
-
type: ErrorType;
|
|
87
|
-
/**
|
|
88
|
-
* In the case of an unknown field encountered in the stream, this will be the field name.
|
|
89
|
-
*/
|
|
90
|
-
field?: string | undefined;
|
|
91
|
-
/**
|
|
92
|
-
* In the case of an unknown field encountered in the stream, this will be the value of the field.
|
|
93
|
-
*/
|
|
94
|
-
value?: string | undefined;
|
|
95
|
-
/**
|
|
96
|
-
* The line that caused the error, if available.
|
|
97
|
-
*/
|
|
98
|
-
line?: string | undefined;
|
|
99
|
-
constructor(
|
|
100
|
-
message: string,
|
|
101
|
-
options: {
|
|
102
|
-
type: ErrorType;
|
|
103
|
-
field?: string;
|
|
104
|
-
value?: string;
|
|
105
|
-
line?: string;
|
|
106
|
-
},
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Callbacks that can be passed to the parser to handle different types of parsed messages
|
|
112
|
-
* and errors.
|
|
113
|
-
*
|
|
114
|
-
* @public
|
|
115
|
-
*/
|
|
116
|
-
export declare interface ParserCallbacks {
|
|
117
|
-
/**
|
|
118
|
-
* Callback for when a new event/message is parsed from the stream.
|
|
119
|
-
* This is the main callback that clients will use to handle incoming messages.
|
|
120
|
-
*
|
|
121
|
-
* @param event - The parsed event/message
|
|
122
|
-
*/
|
|
123
|
-
onEvent?: ((event: EventSourceMessage) => void) | undefined;
|
|
124
|
-
/**
|
|
125
|
-
* Callback for when the server sends a new reconnection interval through the `retry` field.
|
|
126
|
-
*
|
|
127
|
-
* @param retry - The number of milliseconds to wait before reconnecting.
|
|
128
|
-
*/
|
|
129
|
-
onRetry?: ((retry: number) => void) | undefined;
|
|
130
|
-
/**
|
|
131
|
-
* Callback for when a comment is encountered in the stream.
|
|
132
|
-
*
|
|
133
|
-
* @param comment - The comment encountered in the stream.
|
|
134
|
-
*/
|
|
135
|
-
onComment?: ((comment: string) => void) | undefined;
|
|
136
|
-
/**
|
|
137
|
-
* Callback for when an error occurs during parsing. This is a catch-all for any errors
|
|
138
|
-
* that occur during parsing, and can be used to handle them in a custom way. Most clients
|
|
139
|
-
* tend to silently ignore any errors and instead retry, but it can be helpful to log/debug.
|
|
140
|
-
*
|
|
141
|
-
* @param error - The error that occurred during parsing
|
|
142
|
-
*/
|
|
143
|
-
onError?: ((error: ParseError) => void) | undefined;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export {};
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates a new EventSource parser.
|
|
3
|
-
*
|
|
4
|
-
* @param callbacks - Callbacks to invoke on different parsing events:
|
|
5
|
-
* - `onEvent` when a new event is parsed
|
|
6
|
-
* - `onError` when an error occurs
|
|
7
|
-
* - `onRetry` when a new reconnection interval has been sent from the server
|
|
8
|
-
* - `onComment` when a comment is encountered in the stream
|
|
9
|
-
*
|
|
10
|
-
* @returns A new EventSource parser, with `parse` and `reset` methods.
|
|
11
|
-
* @public
|
|
12
|
-
*/
|
|
13
|
-
export declare function createParser(
|
|
14
|
-
callbacks: ParserCallbacks,
|
|
15
|
-
): EventSourceParser;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* The type of error that occurred.
|
|
19
|
-
* @public
|
|
20
|
-
*/
|
|
21
|
-
export declare type ErrorType = "invalid-retry" | "unknown-field";
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* A parsed EventSource message event
|
|
25
|
-
*
|
|
26
|
-
* @public
|
|
27
|
-
*/
|
|
28
|
-
export declare interface EventSourceMessage {
|
|
29
|
-
/**
|
|
30
|
-
* The event type sent from the server. Note that this differs from the browser `EventSource`
|
|
31
|
-
* implementation in that browsers will default this to `message`, whereas this parser will
|
|
32
|
-
* leave this as `undefined` if not explicitly declared.
|
|
33
|
-
*/
|
|
34
|
-
event?: string | undefined;
|
|
35
|
-
/**
|
|
36
|
-
* ID of the message, if any was provided by the server. Can be used by clients to keep the
|
|
37
|
-
* last received message ID in sync when reconnecting.
|
|
38
|
-
*/
|
|
39
|
-
id?: string | undefined;
|
|
40
|
-
/**
|
|
41
|
-
* The data received for this message
|
|
42
|
-
*/
|
|
43
|
-
data: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* EventSource parser instance.
|
|
48
|
-
*
|
|
49
|
-
* Needs to be reset between reconnections/when switching data source, using the `reset()` method.
|
|
50
|
-
*
|
|
51
|
-
* @public
|
|
52
|
-
*/
|
|
53
|
-
export declare interface EventSourceParser {
|
|
54
|
-
/**
|
|
55
|
-
* Feeds the parser another chunk. The method _does not_ return a parsed message.
|
|
56
|
-
* Instead, callbacks passed when creating the parser will be triggered once we see enough data
|
|
57
|
-
* for a valid/invalid parsing step (see {@link ParserCallbacks}).
|
|
58
|
-
*
|
|
59
|
-
* @param chunk - The chunk to parse. Can be a partial, eg in the case of streaming messages.
|
|
60
|
-
* @public
|
|
61
|
-
*/
|
|
62
|
-
feed(chunk: string): void;
|
|
63
|
-
/**
|
|
64
|
-
* Resets the parser state. This is required when you have a new stream of messages -
|
|
65
|
-
* for instance in the case of a client being disconnected and reconnecting.
|
|
66
|
-
*
|
|
67
|
-
* Previously received, incomplete data will NOT be parsed unless you pass `consume: true`,
|
|
68
|
-
* which tells the parser to attempt to consume any incomplete data as if it ended with a newline
|
|
69
|
-
* character. This is useful for cases when a server sends a non-EventSource message that you
|
|
70
|
-
* want to be able to react to in an `onError` callback.
|
|
71
|
-
*
|
|
72
|
-
* @public
|
|
73
|
-
*/
|
|
74
|
-
reset(options?: { consume?: boolean }): void;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Error thrown when encountering an issue during parsing.
|
|
79
|
-
*
|
|
80
|
-
* @public
|
|
81
|
-
*/
|
|
82
|
-
export declare class ParseError extends Error {
|
|
83
|
-
/**
|
|
84
|
-
* The type of error that occurred.
|
|
85
|
-
*/
|
|
86
|
-
type: ErrorType;
|
|
87
|
-
/**
|
|
88
|
-
* In the case of an unknown field encountered in the stream, this will be the field name.
|
|
89
|
-
*/
|
|
90
|
-
field?: string | undefined;
|
|
91
|
-
/**
|
|
92
|
-
* In the case of an unknown field encountered in the stream, this will be the value of the field.
|
|
93
|
-
*/
|
|
94
|
-
value?: string | undefined;
|
|
95
|
-
/**
|
|
96
|
-
* The line that caused the error, if available.
|
|
97
|
-
*/
|
|
98
|
-
line?: string | undefined;
|
|
99
|
-
constructor(
|
|
100
|
-
message: string,
|
|
101
|
-
options: {
|
|
102
|
-
type: ErrorType;
|
|
103
|
-
field?: string;
|
|
104
|
-
value?: string;
|
|
105
|
-
line?: string;
|
|
106
|
-
},
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Callbacks that can be passed to the parser to handle different types of parsed messages
|
|
112
|
-
* and errors.
|
|
113
|
-
*
|
|
114
|
-
* @public
|
|
115
|
-
*/
|
|
116
|
-
export declare interface ParserCallbacks {
|
|
117
|
-
/**
|
|
118
|
-
* Callback for when a new event/message is parsed from the stream.
|
|
119
|
-
* This is the main callback that clients will use to handle incoming messages.
|
|
120
|
-
*
|
|
121
|
-
* @param event - The parsed event/message
|
|
122
|
-
*/
|
|
123
|
-
onEvent?: ((event: EventSourceMessage) => void) | undefined;
|
|
124
|
-
/**
|
|
125
|
-
* Callback for when the server sends a new reconnection interval through the `retry` field.
|
|
126
|
-
*
|
|
127
|
-
* @param retry - The number of milliseconds to wait before reconnecting.
|
|
128
|
-
*/
|
|
129
|
-
onRetry?: ((retry: number) => void) | undefined;
|
|
130
|
-
/**
|
|
131
|
-
* Callback for when a comment is encountered in the stream.
|
|
132
|
-
*
|
|
133
|
-
* @param comment - The comment encountered in the stream.
|
|
134
|
-
*/
|
|
135
|
-
onComment?: ((comment: string) => void) | undefined;
|
|
136
|
-
/**
|
|
137
|
-
* Callback for when an error occurs during parsing. This is a catch-all for any errors
|
|
138
|
-
* that occur during parsing, and can be used to handle them in a custom way. Most clients
|
|
139
|
-
* tend to silently ignore any errors and instead retry, but it can be helpful to log/debug.
|
|
140
|
-
*
|
|
141
|
-
* @param error - The error that occurred during parsing
|
|
142
|
-
*/
|
|
143
|
-
onError?: ((error: ParseError) => void) | undefined;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export {};
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
class ParseError extends Error {
|
|
2
|
-
constructor(message, options) {
|
|
3
|
-
super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
|
|
4
|
-
}
|
|
5
|
-
}
|
|
6
|
-
const LF = 10, CR = 13, SPACE = 32;
|
|
7
|
-
function noop(_arg) {
|
|
8
|
-
}
|
|
9
|
-
function createParser(callbacks) {
|
|
10
|
-
if (typeof callbacks == "function")
|
|
11
|
-
throw new TypeError(
|
|
12
|
-
"`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?"
|
|
13
|
-
);
|
|
14
|
-
const { onEvent = noop, onError = noop, onRetry = noop, onComment } = callbacks, pendingFragments = [];
|
|
15
|
-
let isFirstChunk = !0, id, data = "", dataLines = 0, eventType;
|
|
16
|
-
function feed(chunk) {
|
|
17
|
-
if (isFirstChunk && (isFirstChunk = !1, chunk.charCodeAt(0) === 239 && chunk.charCodeAt(1) === 187 && chunk.charCodeAt(2) === 191 && (chunk = chunk.slice(3))), pendingFragments.length === 0) {
|
|
18
|
-
const trailing2 = processLines(chunk);
|
|
19
|
-
trailing2 !== "" && pendingFragments.push(trailing2);
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
if (chunk.indexOf(`
|
|
23
|
-
`) === -1 && chunk.indexOf("\r") === -1) {
|
|
24
|
-
pendingFragments.push(chunk);
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
pendingFragments.push(chunk);
|
|
28
|
-
const input = pendingFragments.join("");
|
|
29
|
-
pendingFragments.length = 0;
|
|
30
|
-
const trailing = processLines(input);
|
|
31
|
-
trailing !== "" && pendingFragments.push(trailing);
|
|
32
|
-
}
|
|
33
|
-
function processLines(chunk) {
|
|
34
|
-
let searchIndex = 0;
|
|
35
|
-
if (chunk.indexOf("\r") === -1) {
|
|
36
|
-
let lfIndex = chunk.indexOf(`
|
|
37
|
-
`, searchIndex);
|
|
38
|
-
for (; lfIndex !== -1; ) {
|
|
39
|
-
if (searchIndex === lfIndex) {
|
|
40
|
-
dataLines > 0 && onEvent({ id, event: eventType, data }), id = void 0, data = "", dataLines = 0, eventType = void 0, searchIndex = lfIndex + 1, lfIndex = chunk.indexOf(`
|
|
41
|
-
`, searchIndex);
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
const firstCharCode = chunk.charCodeAt(searchIndex);
|
|
45
|
-
if (isDataPrefix(chunk, searchIndex, firstCharCode)) {
|
|
46
|
-
const valueStart = chunk.charCodeAt(searchIndex + 5) === SPACE ? searchIndex + 6 : searchIndex + 5, value = chunk.slice(valueStart, lfIndex);
|
|
47
|
-
if (dataLines === 0 && chunk.charCodeAt(lfIndex + 1) === LF) {
|
|
48
|
-
onEvent({ id, event: eventType, data: value }), id = void 0, data = "", eventType = void 0, searchIndex = lfIndex + 2, lfIndex = chunk.indexOf(`
|
|
49
|
-
`, searchIndex);
|
|
50
|
-
continue;
|
|
51
|
-
}
|
|
52
|
-
data = dataLines === 0 ? value : `${data}
|
|
53
|
-
${value}`, dataLines++;
|
|
54
|
-
} else isEventPrefix(chunk, searchIndex, firstCharCode) ? eventType = chunk.slice(
|
|
55
|
-
chunk.charCodeAt(searchIndex + 6) === SPACE ? searchIndex + 7 : searchIndex + 6,
|
|
56
|
-
lfIndex
|
|
57
|
-
) || void 0 : parseLine(chunk, searchIndex, lfIndex);
|
|
58
|
-
searchIndex = lfIndex + 1, lfIndex = chunk.indexOf(`
|
|
59
|
-
`, searchIndex);
|
|
60
|
-
}
|
|
61
|
-
return chunk.slice(searchIndex);
|
|
62
|
-
}
|
|
63
|
-
for (; searchIndex < chunk.length; ) {
|
|
64
|
-
const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
|
|
65
|
-
`, searchIndex);
|
|
66
|
-
let lineEnd = -1;
|
|
67
|
-
if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = crIndex < lfIndex ? crIndex : lfIndex : crIndex !== -1 ? crIndex === chunk.length - 1 ? lineEnd = -1 : lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1)
|
|
68
|
-
break;
|
|
69
|
-
parseLine(chunk, searchIndex, lineEnd), searchIndex = lineEnd + 1, chunk.charCodeAt(searchIndex - 1) === CR && chunk.charCodeAt(searchIndex) === LF && searchIndex++;
|
|
70
|
-
}
|
|
71
|
-
return chunk.slice(searchIndex);
|
|
72
|
-
}
|
|
73
|
-
function parseLine(chunk, start, end) {
|
|
74
|
-
if (start === end) {
|
|
75
|
-
dispatchEvent();
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
const firstCharCode = chunk.charCodeAt(start);
|
|
79
|
-
if (isDataPrefix(chunk, start, firstCharCode)) {
|
|
80
|
-
const valueStart = chunk.charCodeAt(start + 5) === SPACE ? start + 6 : start + 5, value2 = chunk.slice(valueStart, end);
|
|
81
|
-
data = dataLines === 0 ? value2 : `${data}
|
|
82
|
-
${value2}`, dataLines++;
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
if (isEventPrefix(chunk, start, firstCharCode)) {
|
|
86
|
-
eventType = chunk.slice(chunk.charCodeAt(start + 6) === SPACE ? start + 7 : start + 6, end) || void 0;
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
if (firstCharCode === 105 && chunk.charCodeAt(start + 1) === 100 && chunk.charCodeAt(start + 2) === 58) {
|
|
90
|
-
const value2 = chunk.slice(chunk.charCodeAt(start + 3) === SPACE ? start + 4 : start + 3, end);
|
|
91
|
-
id = value2.includes("\0") ? void 0 : value2;
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
if (firstCharCode === 58) {
|
|
95
|
-
if (onComment) {
|
|
96
|
-
const line2 = chunk.slice(start, end);
|
|
97
|
-
onComment(line2.slice(chunk.charCodeAt(start + 1) === SPACE ? 2 : 1));
|
|
98
|
-
}
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const line = chunk.slice(start, end), fieldSeparatorIndex = line.indexOf(":");
|
|
102
|
-
if (fieldSeparatorIndex === -1) {
|
|
103
|
-
processField(line, "", line);
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
const field = line.slice(0, fieldSeparatorIndex), offset = line.charCodeAt(fieldSeparatorIndex + 1) === SPACE ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
|
|
107
|
-
processField(field, value, line);
|
|
108
|
-
}
|
|
109
|
-
function processField(field, value, line) {
|
|
110
|
-
switch (field) {
|
|
111
|
-
case "event":
|
|
112
|
-
eventType = value || void 0;
|
|
113
|
-
break;
|
|
114
|
-
case "data":
|
|
115
|
-
data = dataLines === 0 ? value : `${data}
|
|
116
|
-
${value}`, dataLines++;
|
|
117
|
-
break;
|
|
118
|
-
case "id":
|
|
119
|
-
id = value.includes("\0") ? void 0 : value;
|
|
120
|
-
break;
|
|
121
|
-
case "retry":
|
|
122
|
-
/^\d+$/.test(value) ? onRetry(parseInt(value, 10)) : onError(
|
|
123
|
-
new ParseError(`Invalid \`retry\` value: "${value}"`, {
|
|
124
|
-
type: "invalid-retry",
|
|
125
|
-
value,
|
|
126
|
-
line
|
|
127
|
-
})
|
|
128
|
-
);
|
|
129
|
-
break;
|
|
130
|
-
default:
|
|
131
|
-
onError(
|
|
132
|
-
new ParseError(
|
|
133
|
-
`Unknown field "${field.length > 20 ? `${field.slice(0, 20)}\u2026` : field}"`,
|
|
134
|
-
{ type: "unknown-field", field, value, line }
|
|
135
|
-
)
|
|
136
|
-
);
|
|
137
|
-
break;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
function dispatchEvent() {
|
|
141
|
-
dataLines > 0 && onEvent({
|
|
142
|
-
id,
|
|
143
|
-
event: eventType,
|
|
144
|
-
data
|
|
145
|
-
}), id = void 0, data = "", dataLines = 0, eventType = void 0;
|
|
146
|
-
}
|
|
147
|
-
function reset(options = {}) {
|
|
148
|
-
if (options.consume && pendingFragments.length > 0) {
|
|
149
|
-
const incompleteLine = pendingFragments.join("");
|
|
150
|
-
parseLine(incompleteLine, 0, incompleteLine.length);
|
|
151
|
-
}
|
|
152
|
-
isFirstChunk = !0, id = void 0, data = "", dataLines = 0, eventType = void 0, pendingFragments.length = 0;
|
|
153
|
-
}
|
|
154
|
-
return { feed, reset };
|
|
155
|
-
}
|
|
156
|
-
function isDataPrefix(chunk, i, firstCharCode) {
|
|
157
|
-
return firstCharCode === 100 && chunk.charCodeAt(i + 1) === 97 && chunk.charCodeAt(i + 2) === 116 && chunk.charCodeAt(i + 3) === 97 && chunk.charCodeAt(i + 4) === 58;
|
|
158
|
-
}
|
|
159
|
-
function isEventPrefix(chunk, i, firstCharCode) {
|
|
160
|
-
return firstCharCode === 101 && chunk.charCodeAt(i + 1) === 118 && chunk.charCodeAt(i + 2) === 101 && chunk.charCodeAt(i + 3) === 110 && chunk.charCodeAt(i + 4) === 116 && chunk.charCodeAt(i + 5) === 58;
|
|
161
|
-
}
|
|
162
|
-
export {
|
|
163
|
-
ParseError,
|
|
164
|
-
createParser
|
|
165
|
-
};
|
|
166
|
-
//# sourceMappingURL=index.js.map
|