@azure/core-sse 2.3.0 → 2.3.1-alpha.20250721.2

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/dist/esm/sse.js CHANGED
@@ -1,6 +1,5 @@
1
1
  // Copyright (c) Microsoft Corporation.
2
2
  // Licensed under the MIT License.
3
- import { __asyncGenerator, __asyncValues, __await } from "tslib";
4
3
  import { createStream, ensureAsyncIterable } from "./utils.js";
5
4
  var ControlChars;
6
5
  (function (ControlChars) {
@@ -28,132 +27,102 @@ function createMessage() {
28
27
  retry: undefined,
29
28
  };
30
29
  }
31
- function toLine(chunkIter) {
32
- return __asyncGenerator(this, arguments, function* toLine_1() {
33
- var _a, e_1, _b, _c;
34
- let buf;
35
- let bufIdx = 0;
36
- let fieldLen = -1;
37
- let discardTrailingNewline = false;
38
- try {
39
- for (var _d = true, chunkIter_1 = __asyncValues(chunkIter), chunkIter_1_1; chunkIter_1_1 = yield __await(chunkIter_1.next()), _a = chunkIter_1_1.done, !_a; _d = true) {
40
- _c = chunkIter_1_1.value;
41
- _d = false;
42
- const chunk = _c;
43
- if (buf === undefined) {
44
- buf = chunk;
45
- bufIdx = 0;
46
- fieldLen = -1;
47
- }
48
- else {
49
- buf = concatBuffer(buf, chunk);
30
+ async function* toLine(chunkIter) {
31
+ let buf;
32
+ let bufIdx = 0;
33
+ let fieldLen = -1;
34
+ let discardTrailingNewline = false;
35
+ for await (const chunk of chunkIter) {
36
+ if (buf === undefined) {
37
+ buf = chunk;
38
+ bufIdx = 0;
39
+ fieldLen = -1;
40
+ }
41
+ else {
42
+ buf = concatBuffer(buf, chunk);
43
+ }
44
+ const bufLen = buf.length;
45
+ let start = 0;
46
+ while (bufIdx < bufLen) {
47
+ if (discardTrailingNewline) {
48
+ if (buf[bufIdx] === ControlChars.NewLine) {
49
+ start = ++bufIdx;
50
50
  }
51
- const bufLen = buf.length;
52
- let start = 0;
53
- while (bufIdx < bufLen) {
54
- if (discardTrailingNewline) {
55
- if (buf[bufIdx] === ControlChars.NewLine) {
56
- start = ++bufIdx;
57
- }
58
- discardTrailingNewline = false;
59
- }
60
- let end = -1;
61
- for (; bufIdx < bufLen && end === -1; ++bufIdx) {
62
- switch (buf[bufIdx]) {
63
- case ControlChars.Colon:
64
- if (fieldLen === -1) {
65
- fieldLen = bufIdx - start;
66
- }
67
- break;
68
- case ControlChars.CarriageReturn:
69
- // We need to discard the trailing newline if any but can't do
70
- // that now because we need to dispatch the current line first.
71
- discardTrailingNewline = true;
72
- end = bufIdx;
73
- break;
74
- case ControlChars.NewLine:
75
- end = bufIdx;
76
- break;
51
+ discardTrailingNewline = false;
52
+ }
53
+ let end = -1;
54
+ for (; bufIdx < bufLen && end === -1; ++bufIdx) {
55
+ switch (buf[bufIdx]) {
56
+ case ControlChars.Colon:
57
+ if (fieldLen === -1) {
58
+ fieldLen = bufIdx - start;
77
59
  }
78
- }
79
- if (end === -1) {
80
- // We reached the end of the buffer but the line hasn't ended.
81
- // Wait for the next chunk and then continue parsing:
82
60
  break;
83
- }
84
- yield yield __await({ line: buf.subarray(start, end), fieldLen });
85
- start = bufIdx; // we're now on the next line
86
- fieldLen = -1;
87
- }
88
- if (start === bufLen) {
89
- buf = undefined;
90
- }
91
- else if (start !== 0) {
92
- // discard already processed lines
93
- buf = buf.subarray(start);
94
- bufIdx -= start;
61
+ case ControlChars.CarriageReturn:
62
+ // We need to discard the trailing newline if any but can't do
63
+ // that now because we need to dispatch the current line first.
64
+ discardTrailingNewline = true;
65
+ end = bufIdx;
66
+ break;
67
+ case ControlChars.NewLine:
68
+ end = bufIdx;
69
+ break;
95
70
  }
96
71
  }
97
- }
98
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
99
- finally {
100
- try {
101
- if (!_d && !_a && (_b = chunkIter_1.return)) yield __await(_b.call(chunkIter_1));
72
+ if (end === -1) {
73
+ // We reached the end of the buffer but the line hasn't ended.
74
+ // Wait for the next chunk and then continue parsing:
75
+ break;
102
76
  }
103
- finally { if (e_1) throw e_1.error; }
77
+ yield { line: buf.subarray(start, end), fieldLen };
78
+ start = bufIdx; // we're now on the next line
79
+ fieldLen = -1;
80
+ }
81
+ if (start === bufLen) {
82
+ buf = undefined;
83
+ }
84
+ else if (start !== 0) {
85
+ // discard already processed lines
86
+ buf = buf.subarray(start);
87
+ bufIdx -= start;
104
88
  }
105
- });
89
+ }
106
90
  }
107
- function toMessage(lineIter) {
108
- return __asyncGenerator(this, arguments, function* toMessage_1() {
109
- var _a, e_2, _b, _c;
110
- let message = createMessage();
111
- const decoder = new TextDecoder();
112
- try {
113
- for (var _d = true, lineIter_1 = __asyncValues(lineIter), lineIter_1_1; lineIter_1_1 = yield __await(lineIter_1.next()), _a = lineIter_1_1.done, !_a; _d = true) {
114
- _c = lineIter_1_1.value;
115
- _d = false;
116
- const { line, fieldLen } = _c;
117
- if (line.length === 0 && message.data !== undefined) {
118
- // empty line denotes end of message. Yield and start a new message:
119
- yield yield __await(message);
120
- message = createMessage();
121
- }
122
- else if (fieldLen > 0) {
123
- // exclude comments and lines with no values
124
- // line is of format "<field>:<value>" or "<field>: <value>"
125
- // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
126
- const field = decoder.decode(line.subarray(0, fieldLen));
127
- const valueOffset = fieldLen + (line[fieldLen + 1] === ControlChars.Space ? 2 : 1);
128
- const value = decoder.decode(line.subarray(valueOffset));
129
- switch (field) {
130
- case "data":
131
- message.data = message.data ? message.data + "\n" + value : value;
132
- break;
133
- case "event":
134
- message.event = value;
135
- break;
136
- case "id":
137
- message.id = value;
138
- break;
139
- case "retry": {
140
- const retry = parseInt(value, 10);
141
- if (!isNaN(retry)) {
142
- message.retry = retry;
143
- }
144
- break;
145
- }
91
+ async function* toMessage(lineIter) {
92
+ let message = createMessage();
93
+ const decoder = new TextDecoder();
94
+ for await (const { line, fieldLen } of lineIter) {
95
+ if (line.length === 0 && message.data !== undefined) {
96
+ // empty line denotes end of message. Yield and start a new message:
97
+ yield message;
98
+ message = createMessage();
99
+ }
100
+ else if (fieldLen > 0) {
101
+ // exclude comments and lines with no values
102
+ // line is of format "<field>:<value>" or "<field>: <value>"
103
+ // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
104
+ const field = decoder.decode(line.subarray(0, fieldLen));
105
+ const valueOffset = fieldLen + (line[fieldLen + 1] === ControlChars.Space ? 2 : 1);
106
+ const value = decoder.decode(line.subarray(valueOffset));
107
+ switch (field) {
108
+ case "data":
109
+ message.data = message.data ? message.data + "\n" + value : value;
110
+ break;
111
+ case "event":
112
+ message.event = value;
113
+ break;
114
+ case "id":
115
+ message.id = value;
116
+ break;
117
+ case "retry": {
118
+ const retry = parseInt(value, 10);
119
+ if (!isNaN(retry)) {
120
+ message.retry = retry;
146
121
  }
122
+ break;
147
123
  }
148
124
  }
149
125
  }
150
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
151
- finally {
152
- try {
153
- if (!_d && !_a && (_b = lineIter_1.return)) yield __await(_b.call(lineIter_1));
154
- }
155
- finally { if (e_2) throw e_2.error; }
156
- }
157
- });
126
+ }
158
127
  }
159
128
  //# sourceMappingURL=sse.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"sse.js","sourceRoot":"","sources":["../../src/sse.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;;AASlC,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAE/D,IAAK,YAKJ;AALD,WAAK,YAAY;IACf,sDAAY,CAAA;IACZ,oEAAmB,CAAA;IACnB,kDAAU,CAAA;IACV,kDAAU,CAAA;AACZ,CAAC,EALI,YAAY,KAAZ,YAAY,QAKhB;AAoBD,MAAM,UAAU,eAAe,CAC7B,WAAgF;IAEhF,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,mBAAmB,CAAC,WAAW,CAAC,CAAC;IAC9D,MAAM,SAAS,GAAG,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,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K,CAAC;4BAClE,MAAM;wBACR,KAAK,OAAO;4BACV,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC;4BACtB,MAAM;wBACR,KAAK,IAAI;4BACP,OAAO,CAAC,EAAE,GAAG,KAAK,CAAC;4BACnB,MAAM;wBACR,KAAK,OAAO,CAAC,CAAC,CAAC;4BACb,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;4BAClC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;gCAClB,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC;4BACxB,CAAC;4BACD,MAAM;wBACR,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;;;;;;;;;IACH,CAAC;CAAA","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { IncomingMessage } from \"node:http\";\nimport type {\n EventMessage,\n EventMessageStream,\n NodeJSReadableStream,\n PartialSome,\n} from \"./models.js\";\nimport { createStream, ensureAsyncIterable } from \"./utils.js\";\n\nenum ControlChars {\n NewLine = 10,\n CarriageReturn = 13,\n Space = 32,\n Colon = 58,\n}\n\n/**\n * Processes a response stream into a stream of events.\n * @param chunkStream - A stream of Uint8Array chunks\n * @returns A stream of EventMessage objects\n */\nexport function createSseStream(chunkStream: ReadableStream<Uint8Array>): EventMessageStream;\n/**\n * Processes a response stream into a stream of events.\n * @param chunkStream - A NodeJS HTTP response\n * @returns A stream of EventMessage objects\n */\nexport function createSseStream(chunkStream: IncomingMessage): EventMessageStream;\n/**\n * Processes a response stream into a stream of events.\n * @param chunkStream - A NodeJS Readable stream\n * @returns A stream of EventMessage objects\n */\nexport function createSseStream(chunkStream: NodeJSReadableStream): EventMessageStream;\nexport function createSseStream(\n chunkStream: IncomingMessage | NodeJSReadableStream | ReadableStream<Uint8Array>,\n): EventMessageStream {\n const { cancel, iterable } = ensureAsyncIterable(chunkStream);\n const asyncIter = toMessage(toLine(iterable));\n return createStream(asyncIter, cancel);\n}\n\nfunction concatBuffer(a: Uint8Array, b: Uint8Array): Uint8Array {\n const res = new Uint8Array(a.length + b.length);\n res.set(a);\n res.set(b, a.length);\n return res;\n}\n\nfunction createMessage(): PartialSome<EventMessage, \"data\"> {\n return {\n data: undefined,\n event: \"\",\n id: \"\",\n retry: undefined,\n };\n}\n\nasync function* toLine(\n chunkIter: AsyncIterable<Uint8Array>,\n): AsyncIterable<{ line: Uint8Array; fieldLen: number }> {\n let buf: Uint8Array | undefined;\n let bufIdx = 0;\n let fieldLen = -1;\n let discardTrailingNewline = false;\n for await (const chunk of chunkIter) {\n if (buf === undefined) {\n buf = chunk;\n bufIdx = 0;\n fieldLen = -1;\n } else {\n buf = concatBuffer(buf, chunk);\n }\n const bufLen = buf.length;\n let start = 0;\n while (bufIdx < bufLen) {\n if (discardTrailingNewline) {\n if (buf[bufIdx] === ControlChars.NewLine) {\n start = ++bufIdx;\n }\n discardTrailingNewline = false;\n }\n let end = -1;\n for (; bufIdx < bufLen && end === -1; ++bufIdx) {\n switch (buf[bufIdx]) {\n case ControlChars.Colon:\n if (fieldLen === -1) {\n fieldLen = bufIdx - start;\n }\n break;\n case ControlChars.CarriageReturn:\n // We need to discard the trailing newline if any but can't do\n // that now because we need to dispatch the current line first.\n discardTrailingNewline = true;\n end = bufIdx;\n break;\n case ControlChars.NewLine:\n end = bufIdx;\n break;\n }\n }\n if (end === -1) {\n // We reached the end of the buffer but the line hasn't ended.\n // Wait for the next chunk and then continue parsing:\n break;\n }\n yield { line: buf.subarray(start, end), fieldLen };\n start = bufIdx; // we're now on the next line\n fieldLen = -1;\n }\n if (start === bufLen) {\n buf = undefined;\n } else if (start !== 0) {\n // discard already processed lines\n buf = buf.subarray(start);\n bufIdx -= start;\n }\n }\n}\n\nasync function* toMessage(\n lineIter: AsyncIterable<{ line: Uint8Array; fieldLen: number }>,\n): AsyncIterableIterator<EventMessage> {\n let message = createMessage();\n const decoder = new TextDecoder();\n for await (const { line, fieldLen } of lineIter) {\n if (line.length === 0 && message.data !== undefined) {\n // empty line denotes end of message. Yield and start a new message:\n yield message as EventMessage;\n message = createMessage();\n } else if (fieldLen > 0) {\n // exclude comments and lines with no values\n // line is of format \"<field>:<value>\" or \"<field>: <value>\"\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\n const field = decoder.decode(line.subarray(0, fieldLen));\n const valueOffset = fieldLen + (line[fieldLen + 1] === ControlChars.Space ? 2 : 1);\n const value = decoder.decode(line.subarray(valueOffset));\n\n switch (field) {\n case \"data\":\n message.data = message.data ? message.data + \"\\n\" + value : value;\n break;\n case \"event\":\n message.event = value;\n break;\n case \"id\":\n message.id = value;\n break;\n case \"retry\": {\n const retry = parseInt(value, 10);\n if (!isNaN(retry)) {\n message.retry = retry;\n }\n break;\n }\n }\n }\n }\n}\n"]}
1
+ {"version":3,"file":"sse.js","sourceRoot":"","sources":["../../src/sse.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AASlC,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAE/D,IAAK,YAKJ;AALD,WAAK,YAAY;IACf,sDAAY,CAAA;IACZ,oEAAmB,CAAA;IACnB,kDAAU,CAAA;IACV,kDAAU,CAAA;AACZ,CAAC,EALI,YAAY,KAAZ,YAAY,QAKhB;AAoBD,MAAM,UAAU,eAAe,CAC7B,WAAgF;IAEhF,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,mBAAmB,CAAC,WAAW,CAAC,CAAC;IAC9D,MAAM,SAAS,GAAG,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC9C,OAAO,YAAY,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;AACzC,CAAC;AAED,SAAS,YAAY,CAAC,CAAa,EAAE,CAAa;IAChD,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;I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;gBACR,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { IncomingMessage } from \"node:http\";\nimport type {\n EventMessage,\n EventMessageStream,\n NodeJSReadableStream,\n PartialSome,\n} from \"./models.js\";\nimport { createStream, ensureAsyncIterable } from \"./utils.js\";\n\nenum ControlChars {\n NewLine = 10,\n CarriageReturn = 13,\n Space = 32,\n Colon = 58,\n}\n\n/**\n * Processes a response stream into a stream of events.\n * @param chunkStream - A stream of Uint8Array chunks\n * @returns A stream of EventMessage objects\n */\nexport function createSseStream(chunkStream: ReadableStream<Uint8Array>): EventMessageStream;\n/**\n * Processes a response stream into a stream of events.\n * @param chunkStream - A NodeJS HTTP response\n * @returns A stream of EventMessage objects\n */\nexport function createSseStream(chunkStream: IncomingMessage): EventMessageStream;\n/**\n * Processes a response stream into a stream of events.\n * @param chunkStream - A NodeJS Readable stream\n * @returns A stream of EventMessage objects\n */\nexport function createSseStream(chunkStream: NodeJSReadableStream): EventMessageStream;\nexport function createSseStream(\n chunkStream: IncomingMessage | NodeJSReadableStream | ReadableStream<Uint8Array>,\n): EventMessageStream {\n const { cancel, iterable } = ensureAsyncIterable(chunkStream);\n const asyncIter = toMessage(toLine(iterable));\n return createStream(asyncIter, cancel);\n}\n\nfunction concatBuffer(a: Uint8Array, b: Uint8Array): Uint8Array {\n const res = new Uint8Array(a.length + b.length);\n res.set(a);\n res.set(b, a.length);\n return res;\n}\n\nfunction createMessage(): PartialSome<EventMessage, \"data\"> {\n return {\n data: undefined,\n event: \"\",\n id: \"\",\n retry: undefined,\n };\n}\n\nasync function* toLine(\n chunkIter: AsyncIterable<Uint8Array>,\n): AsyncIterable<{ line: Uint8Array; fieldLen: number }> {\n let buf: Uint8Array | undefined;\n let bufIdx = 0;\n let fieldLen = -1;\n let discardTrailingNewline = false;\n for await (const chunk of chunkIter) {\n if (buf === undefined) {\n buf = chunk;\n bufIdx = 0;\n fieldLen = -1;\n } else {\n buf = concatBuffer(buf, chunk);\n }\n const bufLen = buf.length;\n let start = 0;\n while (bufIdx < bufLen) {\n if (discardTrailingNewline) {\n if (buf[bufIdx] === ControlChars.NewLine) {\n start = ++bufIdx;\n }\n discardTrailingNewline = false;\n }\n let end = -1;\n for (; bufIdx < bufLen && end === -1; ++bufIdx) {\n switch (buf[bufIdx]) {\n case ControlChars.Colon:\n if (fieldLen === -1) {\n fieldLen = bufIdx - start;\n }\n break;\n case ControlChars.CarriageReturn:\n // We need to discard the trailing newline if any but can't do\n // that now because we need to dispatch the current line first.\n discardTrailingNewline = true;\n end = bufIdx;\n break;\n case ControlChars.NewLine:\n end = bufIdx;\n break;\n }\n }\n if (end === -1) {\n // We reached the end of the buffer but the line hasn't ended.\n // Wait for the next chunk and then continue parsing:\n break;\n }\n yield { line: buf.subarray(start, end), fieldLen };\n start = bufIdx; // we're now on the next line\n fieldLen = -1;\n }\n if (start === bufLen) {\n buf = undefined;\n } else if (start !== 0) {\n // discard already processed lines\n buf = buf.subarray(start);\n bufIdx -= start;\n }\n }\n}\n\nasync function* toMessage(\n lineIter: AsyncIterable<{ line: Uint8Array; fieldLen: number }>,\n): AsyncIterableIterator<EventMessage> {\n let message = createMessage();\n const decoder = new TextDecoder();\n for await (const { line, fieldLen } of lineIter) {\n if (line.length === 0 && message.data !== undefined) {\n // empty line denotes end of message. Yield and start a new message:\n yield message as EventMessage;\n message = createMessage();\n } else if (fieldLen > 0) {\n // exclude comments and lines with no values\n // line is of format \"<field>:<value>\" or \"<field>: <value>\"\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\n const field = decoder.decode(line.subarray(0, fieldLen));\n const valueOffset = fieldLen + (line[fieldLen + 1] === ControlChars.Space ? 2 : 1);\n const value = decoder.decode(line.subarray(valueOffset));\n\n switch (field) {\n case \"data\":\n message.data = message.data ? message.data + \"\\n\" + value : value;\n break;\n case \"event\":\n message.event = value;\n break;\n case \"id\":\n message.id = value;\n break;\n case \"retry\": {\n const retry = parseInt(value, 10);\n if (!isNaN(retry)) {\n message.retry = retry;\n }\n break;\n }\n }\n }\n }\n}\n"]}
package/dist/esm/utils.js CHANGED
@@ -1,6 +1,5 @@
1
1
  // Copyright (c) Microsoft Corporation.
2
2
  // Licensed under the MIT License.
3
- import { __asyncGenerator, __await } from "tslib";
4
3
  export function createStream(asyncIter, cancel) {
5
4
  const stream = iteratorToStream(asyncIter, cancel);
6
5
  /** TODO: remove these polyfills once all supported runtimes support them */
@@ -12,8 +11,7 @@ function polyfillStream(stream, dispose) {
12
11
  return stream;
13
12
  }
14
13
  function makeAsyncDisposable(webStream, dispose) {
15
- var _a;
16
- (_a = Symbol.asyncDispose) !== null && _a !== void 0 ? _a : (Symbol.asyncDispose = Symbol("Symbol.asyncDispose"));
14
+ Symbol.asyncDispose ??= Symbol("Symbol.asyncDispose");
17
15
  if (!webStream[Symbol.asyncDispose]) {
18
16
  webStream[Symbol.asyncDispose] = () => dispose();
19
17
  }
@@ -68,23 +66,21 @@ function isReadableStream(body) {
68
66
  typeof body.getReader === "function" &&
69
67
  typeof body.tee === "function");
70
68
  }
71
- function toAsyncIterable(stream) {
72
- return __asyncGenerator(this, arguments, function* toAsyncIterable_1() {
73
- const reader = stream.getReader();
74
- try {
75
- while (true) {
76
- const { value, done } = yield __await(reader.read());
77
- if (done) {
78
- return yield __await(void 0);
79
- }
80
- yield yield __await(value);
69
+ async function* toAsyncIterable(stream) {
70
+ const reader = stream.getReader();
71
+ try {
72
+ while (true) {
73
+ const { value, done } = await reader.read();
74
+ if (done) {
75
+ return;
81
76
  }
77
+ yield value;
82
78
  }
83
- finally {
84
- const cancelPromise = reader.cancel();
85
- reader.releaseLock();
86
- yield __await(cancelPromise);
87
- }
88
- });
79
+ }
80
+ finally {
81
+ const cancelPromise = reader.cancel();
82
+ reader.releaseLock();
83
+ await cancelPromise;
84
+ }
89
85
  }
90
86
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
-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,EAAE,IAAI,EAAE,GAAG,cAAM,MAAM,CAAC,IAAI,EAAE,CAAA,CAAC;gBAC5C,IAAI,IAAI,EAAE,CAAC;oBACT,6BAAO;gBACT,CAAC;gBACD,oBAAM,KAAK,CAAA,CAAC;YACd,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;YACtC,MAAM,CAAC,WAAW,EAAE,CAAC;YACrB,cAAM,aAAa,CAAA,CAAC;QACtB,CAAC;IACH,CAAC;CAAA","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { IncomingMessage } from \"node:http\";\nimport type { NodeJSReadableStream } from \"./models.js\";\n\nexport function createStream<T>(\n asyncIter: AsyncIterableIterator<T>,\n cancel: () => PromiseLike<void>,\n): ReadableStream<T> & AsyncDisposable & AsyncIterable<T> {\n const stream = iteratorToStream(asyncIter, cancel);\n /** TODO: remove these polyfills once all supported runtimes support them */\n return polyfillStream(stream, cancel);\n}\n\nfunction polyfillStream<T>(\n stream: ReadableStream<T>,\n dispose: () => PromiseLike<void>,\n): ReadableStream<T> & AsyncIterable<T> & AsyncDisposable {\n makeAsyncIterable<T>(stream);\n makeAsyncDisposable(stream, dispose);\n return stream;\n}\n\nfunction makeAsyncDisposable<T>(\n webStream: any,\n dispose: () => PromiseLike<void>,\n): asserts webStream is ReadableStream<T> & AsyncDisposable {\n (Symbol.asyncDispose as any) ??= Symbol(\"Symbol.asyncDispose\");\n if (!webStream[Symbol.asyncDispose]) {\n webStream[Symbol.asyncDispose] = () => dispose();\n }\n}\n\nfunction makeAsyncIterable<T>(\n webStream: any,\n): asserts webStream is ReadableStream<T> & AsyncIterable<T> {\n if (!webStream[Symbol.asyncIterator]) {\n webStream[Symbol.asyncIterator] = () => toAsyncIterable(webStream);\n }\n\n if (!webStream.values) {\n webStream.values = () => toAsyncIterable(webStream);\n }\n}\n\nfunction iteratorToStream<T>(\n iterator: AsyncIterableIterator<T>,\n cancel: () => PromiseLike<void>,\n): ReadableStream<T> {\n return new ReadableStream({\n async pull(controller) {\n const { value, done } = await iterator.next();\n if (done) {\n controller.close();\n } else {\n controller.enqueue(value);\n }\n },\n cancel,\n });\n}\n\nexport function ensureAsyncIterable(\n stream: IncomingMessage | NodeJSReadableStream | ReadableStream<Uint8Array>,\n): {\n cancel(): Promise<void>;\n iterable: AsyncIterable<Uint8Array>;\n} {\n if (isReadableStream(stream)) {\n makeAsyncIterable<Uint8Array>(stream);\n return {\n cancel: () => stream.cancel(),\n iterable: stream,\n };\n } else {\n return {\n cancel: async () => {\n // socket could be null if the connection is already closed\n if (\"socket\" in stream && stream.socket) {\n stream.socket.end();\n } else {\n stream.destroy();\n }\n },\n iterable: stream as AsyncIterable<Uint8Array>,\n };\n }\n}\n\nfunction isReadableStream(body: unknown): body is ReadableStream {\n return Boolean(\n body &&\n typeof (body as ReadableStream).getReader === \"function\" &&\n typeof (body as ReadableStream).tee === \"function\",\n );\n}\n\nasync function* toAsyncIterable<T>(stream: ReadableStream<T>): AsyncIterableIterator<T> {\n const reader = stream.getReader();\n try {\n while (true) {\n const { value, done } = await reader.read();\n if (done) {\n return;\n }\n yield value;\n }\n } finally {\n const cancelPromise = reader.cancel();\n reader.releaseLock();\n await cancelPromise;\n }\n}\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAKlC,MAAM,UAAU,YAAY,CAC1B,SAAmC,EACnC,MAA+B;IAE/B,MAAM,MAAM,GAAG,gBAAgB,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IACnD,4EAA4E;IAC5E,OAAO,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AACxC,CAAC;AAED,SAAS,cAAc,CACrB,MAAyB,EACzB,OAAgC;IAEhC,iBAAiB,CAAI,MAAM,CAAC,CAAC;IAC7B,mBAAmB,CAAC,MAAM,EAAE,OAA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YACL,MAAM,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE;YAC7B,QAAQ,EAAE,MAAM;SACjB,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,OAAO;YACL,MAAM,EAAE,KAAK,IAAI,EAAE;gBACjB,2DAA2D;gBAC3D,IAAI,QAAQ,IAAI,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;oBACxC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;gBACtB,CAAC;qBAAM,CAAC;oBACN,MAAM,CAAC,OAAO,EAAE,CAAC;gBACnB,CAAC;YACH,CAAC;YACD,QAAQ,EAAE,MAAmC;SAC9C,CAAC;IACJ,CAAC;AACH,CAAC;AAED,SAAS,gBAAgB,CAAC,IAAa;IACrC,OAAO,OAAO,CACZ,IAAI;QACF,OAAQ,IAAuB,CAAC,SAAS,KAAK,UAAU;QACxD,OAAQ,IAAuB,CAAC,GAAG,KAAK,UAAU,CACrD,CAAC;AACJ,CAAC;AAED,KAAK,SAAS,CAAC,CAAC,eAAe,CAAI,MAAyB;IAC1D,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,EAAE,CAAC;IAClC,IAAI,CAAC;QACH,OAAO,IAAI,EAAE,CAAC;YACZ,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;YAC5C,IAAI,IAAI,EAAE,CAAC;gBACT,OAAO;YACT,CAAC;YACD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;YAAS,CAAC;QACT,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;QACtC,MAAM,CAAC,WAAW,EAAE,CAAC;QACrB,MAAM,aAAa,CAAC;IACtB,CAAC;AACH,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { IncomingMessage } from \"node:http\";\nimport type { NodeJSReadableStream } from \"./models.js\";\n\nexport function createStream<T>(\n asyncIter: AsyncIterableIterator<T>,\n cancel: () => PromiseLike<void>,\n): ReadableStream<T> & AsyncDisposable & AsyncIterable<T> {\n const stream = iteratorToStream(asyncIter, cancel);\n /** TODO: remove these polyfills once all supported runtimes support them */\n return polyfillStream(stream, cancel);\n}\n\nfunction polyfillStream<T>(\n stream: ReadableStream<T>,\n dispose: () => PromiseLike<void>,\n): ReadableStream<T> & AsyncIterable<T> & AsyncDisposable {\n makeAsyncIterable<T>(stream);\n makeAsyncDisposable(stream, dispose);\n return stream;\n}\n\nfunction makeAsyncDisposable<T>(\n webStream: any,\n dispose: () => PromiseLike<void>,\n): asserts webStream is ReadableStream<T> & AsyncDisposable {\n (Symbol.asyncDispose as any) ??= Symbol(\"Symbol.asyncDispose\");\n if (!webStream[Symbol.asyncDispose]) {\n webStream[Symbol.asyncDispose] = () => dispose();\n }\n}\n\nfunction makeAsyncIterable<T>(\n webStream: any,\n): asserts webStream is ReadableStream<T> & AsyncIterable<T> {\n if (!webStream[Symbol.asyncIterator]) {\n webStream[Symbol.asyncIterator] = () => toAsyncIterable(webStream);\n }\n\n if (!webStream.values) {\n webStream.values = () => toAsyncIterable(webStream);\n }\n}\n\nfunction iteratorToStream<T>(\n iterator: AsyncIterableIterator<T>,\n cancel: () => PromiseLike<void>,\n): ReadableStream<T> {\n return new ReadableStream({\n async pull(controller) {\n const { value, done } = await iterator.next();\n if (done) {\n controller.close();\n } else {\n controller.enqueue(value);\n }\n },\n cancel,\n });\n}\n\nexport function ensureAsyncIterable(\n stream: IncomingMessage | NodeJSReadableStream | ReadableStream<Uint8Array>,\n): {\n cancel(): Promise<void>;\n iterable: AsyncIterable<Uint8Array>;\n} {\n if (isReadableStream(stream)) {\n makeAsyncIterable<Uint8Array>(stream);\n return {\n cancel: () => stream.cancel(),\n iterable: stream,\n };\n } else {\n return {\n cancel: async () => {\n // socket could be null if the connection is already closed\n if (\"socket\" in stream && stream.socket) {\n stream.socket.end();\n } else {\n stream.destroy();\n }\n },\n iterable: stream as AsyncIterable<Uint8Array>,\n };\n }\n}\n\nfunction isReadableStream(body: unknown): body is ReadableStream {\n return Boolean(\n body &&\n typeof (body as ReadableStream).getReader === \"function\" &&\n typeof (body as ReadableStream).tee === \"function\",\n );\n}\n\nasync function* toAsyncIterable<T>(stream: ReadableStream<T>): AsyncIterableIterator<T> {\n const reader = stream.getReader();\n try {\n while (true) {\n const { value, done } = await reader.read();\n if (done) {\n return;\n }\n yield value;\n }\n } finally {\n const cancelPromise = reader.cancel();\n reader.releaseLock();\n await cancelPromise;\n }\n}\n"]}
@@ -15,8 +15,7 @@ export interface EventMessage {
15
15
  /**
16
16
  * A stream of event messages
17
17
  */
18
- export interface EventMessageStream extends ReadableStream<EventMessage>, AsyncDisposable, AsyncIterable<EventMessage> {
19
- }
18
+ export type EventMessageStream = ReadableStream<EventMessage> & AsyncDisposable & AsyncIterable<EventMessage>;
20
19
  export type PartialSome<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
21
20
  /**
22
21
  * A Node.js Readable stream that also has a `destroy` method.
@@ -1 +1 @@
1
- {"version":3,"file":"models.js","sourceRoot":"","sources":["../../src/models.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\n/**\n * Represents a message sent in an event stream\n * https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format\n */\nexport interface EventMessage {\n /** The event ID to set the EventSource object's last event ID value. */\n id: string;\n /** A string identifying the type of event described. */\n event: string;\n /** The event data */\n data: string;\n /** The reconnection interval (in milliseconds) to wait before retrying the connection */\n retry?: number;\n}\n\n/**\n * A stream of event messages\n */\nexport interface EventMessageStream\n extends ReadableStream<EventMessage>,\n AsyncDisposable,\n AsyncIterable<EventMessage> {}\n\nexport type PartialSome<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;\n\n/**\n * A Node.js Readable stream that also has a `destroy` method.\n */\nexport interface NodeJSReadableStream extends NodeJS.ReadableStream {\n /**\n * Destroy the stream. Optionally emit an 'error' event, and emit a\n * 'close' event (unless emitClose is set to false). After this call,\n * internal resources will be released.\n */\n destroy(error?: Error): void;\n}\n"]}
1
+ {"version":3,"file":"models.js","sourceRoot":"","sources":["../../src/models.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\n/**\n * Represents a message sent in an event stream\n * https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format\n */\nexport interface EventMessage {\n /** The event ID to set the EventSource object's last event ID value. */\n id: string;\n /** A string identifying the type of event described. */\n event: string;\n /** The event data */\n data: string;\n /** The reconnection interval (in milliseconds) to wait before retrying the connection */\n retry?: number;\n}\n\n/**\n * A stream of event messages\n */\nexport type EventMessageStream = ReadableStream<EventMessage> &\n AsyncDisposable &\n AsyncIterable<EventMessage>;\n\nexport type PartialSome<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;\n\n/**\n * A Node.js Readable stream that also has a `destroy` method.\n */\nexport interface NodeJSReadableStream extends NodeJS.ReadableStream {\n /**\n * Destroy the stream. Optionally emit an 'error' event, and emit a\n * 'close' event (unless emitClose is set to false). After this call,\n * internal resources will be released.\n */\n destroy(error?: Error): void;\n}\n"]}
@@ -1,6 +1,5 @@
1
1
  // Copyright (c) Microsoft Corporation.
2
2
  // Licensed under the MIT License.
3
- import { __asyncGenerator, __asyncValues, __await } from "tslib";
4
3
  import { createStream, ensureAsyncIterable } from "./utils.js";
5
4
  var ControlChars;
6
5
  (function (ControlChars) {
@@ -28,132 +27,102 @@ function createMessage() {
28
27
  retry: undefined,
29
28
  };
30
29
  }
31
- function toLine(chunkIter) {
32
- return __asyncGenerator(this, arguments, function* toLine_1() {
33
- var _a, e_1, _b, _c;
34
- let buf;
35
- let bufIdx = 0;
36
- let fieldLen = -1;
37
- let discardTrailingNewline = false;
38
- try {
39
- for (var _d = true, chunkIter_1 = __asyncValues(chunkIter), chunkIter_1_1; chunkIter_1_1 = yield __await(chunkIter_1.next()), _a = chunkIter_1_1.done, !_a; _d = true) {
40
- _c = chunkIter_1_1.value;
41
- _d = false;
42
- const chunk = _c;
43
- if (buf === undefined) {
44
- buf = chunk;
45
- bufIdx = 0;
46
- fieldLen = -1;
47
- }
48
- else {
49
- buf = concatBuffer(buf, chunk);
30
+ async function* toLine(chunkIter) {
31
+ let buf;
32
+ let bufIdx = 0;
33
+ let fieldLen = -1;
34
+ let discardTrailingNewline = false;
35
+ for await (const chunk of chunkIter) {
36
+ if (buf === undefined) {
37
+ buf = chunk;
38
+ bufIdx = 0;
39
+ fieldLen = -1;
40
+ }
41
+ else {
42
+ buf = concatBuffer(buf, chunk);
43
+ }
44
+ const bufLen = buf.length;
45
+ let start = 0;
46
+ while (bufIdx < bufLen) {
47
+ if (discardTrailingNewline) {
48
+ if (buf[bufIdx] === ControlChars.NewLine) {
49
+ start = ++bufIdx;
50
50
  }
51
- const bufLen = buf.length;
52
- let start = 0;
53
- while (bufIdx < bufLen) {
54
- if (discardTrailingNewline) {
55
- if (buf[bufIdx] === ControlChars.NewLine) {
56
- start = ++bufIdx;
57
- }
58
- discardTrailingNewline = false;
59
- }
60
- let end = -1;
61
- for (; bufIdx < bufLen && end === -1; ++bufIdx) {
62
- switch (buf[bufIdx]) {
63
- case ControlChars.Colon:
64
- if (fieldLen === -1) {
65
- fieldLen = bufIdx - start;
66
- }
67
- break;
68
- case ControlChars.CarriageReturn:
69
- // We need to discard the trailing newline if any but can't do
70
- // that now because we need to dispatch the current line first.
71
- discardTrailingNewline = true;
72
- end = bufIdx;
73
- break;
74
- case ControlChars.NewLine:
75
- end = bufIdx;
76
- break;
51
+ discardTrailingNewline = false;
52
+ }
53
+ let end = -1;
54
+ for (; bufIdx < bufLen && end === -1; ++bufIdx) {
55
+ switch (buf[bufIdx]) {
56
+ case ControlChars.Colon:
57
+ if (fieldLen === -1) {
58
+ fieldLen = bufIdx - start;
77
59
  }
78
- }
79
- if (end === -1) {
80
- // We reached the end of the buffer but the line hasn't ended.
81
- // Wait for the next chunk and then continue parsing:
82
60
  break;
83
- }
84
- yield yield __await({ line: buf.subarray(start, end), fieldLen });
85
- start = bufIdx; // we're now on the next line
86
- fieldLen = -1;
87
- }
88
- if (start === bufLen) {
89
- buf = undefined;
90
- }
91
- else if (start !== 0) {
92
- // discard already processed lines
93
- buf = buf.subarray(start);
94
- bufIdx -= start;
61
+ case ControlChars.CarriageReturn:
62
+ // We need to discard the trailing newline if any but can't do
63
+ // that now because we need to dispatch the current line first.
64
+ discardTrailingNewline = true;
65
+ end = bufIdx;
66
+ break;
67
+ case ControlChars.NewLine:
68
+ end = bufIdx;
69
+ break;
95
70
  }
96
71
  }
97
- }
98
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
99
- finally {
100
- try {
101
- if (!_d && !_a && (_b = chunkIter_1.return)) yield __await(_b.call(chunkIter_1));
72
+ if (end === -1) {
73
+ // We reached the end of the buffer but the line hasn't ended.
74
+ // Wait for the next chunk and then continue parsing:
75
+ break;
102
76
  }
103
- finally { if (e_1) throw e_1.error; }
77
+ yield { line: buf.subarray(start, end), fieldLen };
78
+ start = bufIdx; // we're now on the next line
79
+ fieldLen = -1;
80
+ }
81
+ if (start === bufLen) {
82
+ buf = undefined;
83
+ }
84
+ else if (start !== 0) {
85
+ // discard already processed lines
86
+ buf = buf.subarray(start);
87
+ bufIdx -= start;
104
88
  }
105
- });
89
+ }
106
90
  }
107
- function toMessage(lineIter) {
108
- return __asyncGenerator(this, arguments, function* toMessage_1() {
109
- var _a, e_2, _b, _c;
110
- let message = createMessage();
111
- const decoder = new TextDecoder();
112
- try {
113
- for (var _d = true, lineIter_1 = __asyncValues(lineIter), lineIter_1_1; lineIter_1_1 = yield __await(lineIter_1.next()), _a = lineIter_1_1.done, !_a; _d = true) {
114
- _c = lineIter_1_1.value;
115
- _d = false;
116
- const { line, fieldLen } = _c;
117
- if (line.length === 0 && message.data !== undefined) {
118
- // empty line denotes end of message. Yield and start a new message:
119
- yield yield __await(message);
120
- message = createMessage();
121
- }
122
- else if (fieldLen > 0) {
123
- // exclude comments and lines with no values
124
- // line is of format "<field>:<value>" or "<field>: <value>"
125
- // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
126
- const field = decoder.decode(line.subarray(0, fieldLen));
127
- const valueOffset = fieldLen + (line[fieldLen + 1] === ControlChars.Space ? 2 : 1);
128
- const value = decoder.decode(line.subarray(valueOffset));
129
- switch (field) {
130
- case "data":
131
- message.data = message.data ? message.data + "\n" + value : value;
132
- break;
133
- case "event":
134
- message.event = value;
135
- break;
136
- case "id":
137
- message.id = value;
138
- break;
139
- case "retry": {
140
- const retry = parseInt(value, 10);
141
- if (!isNaN(retry)) {
142
- message.retry = retry;
143
- }
144
- break;
145
- }
91
+ async function* toMessage(lineIter) {
92
+ let message = createMessage();
93
+ const decoder = new TextDecoder();
94
+ for await (const { line, fieldLen } of lineIter) {
95
+ if (line.length === 0 && message.data !== undefined) {
96
+ // empty line denotes end of message. Yield and start a new message:
97
+ yield message;
98
+ message = createMessage();
99
+ }
100
+ else if (fieldLen > 0) {
101
+ // exclude comments and lines with no values
102
+ // line is of format "<field>:<value>" or "<field>: <value>"
103
+ // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
104
+ const field = decoder.decode(line.subarray(0, fieldLen));
105
+ const valueOffset = fieldLen + (line[fieldLen + 1] === ControlChars.Space ? 2 : 1);
106
+ const value = decoder.decode(line.subarray(valueOffset));
107
+ switch (field) {
108
+ case "data":
109
+ message.data = message.data ? message.data + "\n" + value : value;
110
+ break;
111
+ case "event":
112
+ message.event = value;
113
+ break;
114
+ case "id":
115
+ message.id = value;
116
+ break;
117
+ case "retry": {
118
+ const retry = parseInt(value, 10);
119
+ if (!isNaN(retry)) {
120
+ message.retry = retry;
146
121
  }
122
+ break;
147
123
  }
148
124
  }
149
125
  }
150
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
151
- finally {
152
- try {
153
- if (!_d && !_a && (_b = lineIter_1.return)) yield __await(_b.call(lineIter_1));
154
- }
155
- finally { if (e_2) throw e_2.error; }
156
- }
157
- });
126
+ }
158
127
  }
159
128
  //# sourceMappingURL=sse.js.map