@azure/core-sse 2.2.1-alpha.20250620.3 → 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.
@@ -3,7 +3,6 @@
3
3
  // Licensed under the MIT License.
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.createSseStream = createSseStream;
6
- const tslib_1 = require("tslib");
7
6
  const utils_js_1 = require("./utils.js");
8
7
  var ControlChars;
9
8
  (function (ControlChars) {
@@ -31,132 +30,102 @@ function createMessage() {
31
30
  retry: undefined,
32
31
  };
33
32
  }
34
- function toLine(chunkIter) {
35
- return tslib_1.__asyncGenerator(this, arguments, function* toLine_1() {
36
- var _a, e_1, _b, _c;
37
- let buf;
38
- let bufIdx = 0;
39
- let fieldLen = -1;
40
- let discardTrailingNewline = false;
41
- try {
42
- for (var _d = true, chunkIter_1 = tslib_1.__asyncValues(chunkIter), chunkIter_1_1; chunkIter_1_1 = yield tslib_1.__await(chunkIter_1.next()), _a = chunkIter_1_1.done, !_a; _d = true) {
43
- _c = chunkIter_1_1.value;
44
- _d = false;
45
- const chunk = _c;
46
- if (buf === undefined) {
47
- buf = chunk;
48
- bufIdx = 0;
49
- fieldLen = -1;
50
- }
51
- else {
52
- buf = concatBuffer(buf, chunk);
33
+ async function* toLine(chunkIter) {
34
+ let buf;
35
+ let bufIdx = 0;
36
+ let fieldLen = -1;
37
+ let discardTrailingNewline = false;
38
+ for await (const chunk of chunkIter) {
39
+ if (buf === undefined) {
40
+ buf = chunk;
41
+ bufIdx = 0;
42
+ fieldLen = -1;
43
+ }
44
+ else {
45
+ buf = concatBuffer(buf, chunk);
46
+ }
47
+ const bufLen = buf.length;
48
+ let start = 0;
49
+ while (bufIdx < bufLen) {
50
+ if (discardTrailingNewline) {
51
+ if (buf[bufIdx] === ControlChars.NewLine) {
52
+ start = ++bufIdx;
53
53
  }
54
- const bufLen = buf.length;
55
- let start = 0;
56
- while (bufIdx < bufLen) {
57
- if (discardTrailingNewline) {
58
- if (buf[bufIdx] === ControlChars.NewLine) {
59
- start = ++bufIdx;
60
- }
61
- discardTrailingNewline = false;
62
- }
63
- let end = -1;
64
- for (; bufIdx < bufLen && end === -1; ++bufIdx) {
65
- switch (buf[bufIdx]) {
66
- case ControlChars.Colon:
67
- if (fieldLen === -1) {
68
- fieldLen = bufIdx - start;
69
- }
70
- break;
71
- case ControlChars.CarriageReturn:
72
- // We need to discard the trailing newline if any but can't do
73
- // that now because we need to dispatch the current line first.
74
- discardTrailingNewline = true;
75
- end = bufIdx;
76
- break;
77
- case ControlChars.NewLine:
78
- end = bufIdx;
79
- break;
54
+ discardTrailingNewline = false;
55
+ }
56
+ let end = -1;
57
+ for (; bufIdx < bufLen && end === -1; ++bufIdx) {
58
+ switch (buf[bufIdx]) {
59
+ case ControlChars.Colon:
60
+ if (fieldLen === -1) {
61
+ fieldLen = bufIdx - start;
80
62
  }
81
- }
82
- if (end === -1) {
83
- // We reached the end of the buffer but the line hasn't ended.
84
- // Wait for the next chunk and then continue parsing:
85
63
  break;
86
- }
87
- yield yield tslib_1.__await({ line: buf.subarray(start, end), fieldLen });
88
- start = bufIdx; // we're now on the next line
89
- fieldLen = -1;
90
- }
91
- if (start === bufLen) {
92
- buf = undefined;
93
- }
94
- else if (start !== 0) {
95
- // discard already processed lines
96
- buf = buf.subarray(start);
97
- bufIdx -= start;
64
+ case ControlChars.CarriageReturn:
65
+ // We need to discard the trailing newline if any but can't do
66
+ // that now because we need to dispatch the current line first.
67
+ discardTrailingNewline = true;
68
+ end = bufIdx;
69
+ break;
70
+ case ControlChars.NewLine:
71
+ end = bufIdx;
72
+ break;
98
73
  }
99
74
  }
100
- }
101
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
102
- finally {
103
- try {
104
- if (!_d && !_a && (_b = chunkIter_1.return)) yield tslib_1.__await(_b.call(chunkIter_1));
75
+ if (end === -1) {
76
+ // We reached the end of the buffer but the line hasn't ended.
77
+ // Wait for the next chunk and then continue parsing:
78
+ break;
105
79
  }
106
- finally { if (e_1) throw e_1.error; }
80
+ yield { line: buf.subarray(start, end), fieldLen };
81
+ start = bufIdx; // we're now on the next line
82
+ fieldLen = -1;
83
+ }
84
+ if (start === bufLen) {
85
+ buf = undefined;
86
+ }
87
+ else if (start !== 0) {
88
+ // discard already processed lines
89
+ buf = buf.subarray(start);
90
+ bufIdx -= start;
107
91
  }
108
- });
92
+ }
109
93
  }
110
- function toMessage(lineIter) {
111
- return tslib_1.__asyncGenerator(this, arguments, function* toMessage_1() {
112
- var _a, e_2, _b, _c;
113
- let message = createMessage();
114
- const decoder = new TextDecoder();
115
- try {
116
- for (var _d = true, lineIter_1 = tslib_1.__asyncValues(lineIter), lineIter_1_1; lineIter_1_1 = yield tslib_1.__await(lineIter_1.next()), _a = lineIter_1_1.done, !_a; _d = true) {
117
- _c = lineIter_1_1.value;
118
- _d = false;
119
- const { line, fieldLen } = _c;
120
- if (line.length === 0 && message.data !== undefined) {
121
- // empty line denotes end of message. Yield and start a new message:
122
- yield yield tslib_1.__await(message);
123
- message = createMessage();
124
- }
125
- else if (fieldLen > 0) {
126
- // exclude comments and lines with no values
127
- // line is of format "<field>:<value>" or "<field>: <value>"
128
- // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
129
- const field = decoder.decode(line.subarray(0, fieldLen));
130
- const valueOffset = fieldLen + (line[fieldLen + 1] === ControlChars.Space ? 2 : 1);
131
- const value = decoder.decode(line.subarray(valueOffset));
132
- switch (field) {
133
- case "data":
134
- message.data = message.data ? message.data + "\n" + value : value;
135
- break;
136
- case "event":
137
- message.event = value;
138
- break;
139
- case "id":
140
- message.id = value;
141
- break;
142
- case "retry": {
143
- const retry = parseInt(value, 10);
144
- if (!isNaN(retry)) {
145
- message.retry = retry;
146
- }
147
- break;
148
- }
94
+ async function* toMessage(lineIter) {
95
+ let message = createMessage();
96
+ const decoder = new TextDecoder();
97
+ for await (const { line, fieldLen } of lineIter) {
98
+ if (line.length === 0 && message.data !== undefined) {
99
+ // empty line denotes end of message. Yield and start a new message:
100
+ yield message;
101
+ message = createMessage();
102
+ }
103
+ else if (fieldLen > 0) {
104
+ // exclude comments and lines with no values
105
+ // line is of format "<field>:<value>" or "<field>: <value>"
106
+ // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
107
+ const field = decoder.decode(line.subarray(0, fieldLen));
108
+ const valueOffset = fieldLen + (line[fieldLen + 1] === ControlChars.Space ? 2 : 1);
109
+ const value = decoder.decode(line.subarray(valueOffset));
110
+ switch (field) {
111
+ case "data":
112
+ message.data = message.data ? message.data + "\n" + value : value;
113
+ break;
114
+ case "event":
115
+ message.event = value;
116
+ break;
117
+ case "id":
118
+ message.id = value;
119
+ break;
120
+ case "retry": {
121
+ const retry = parseInt(value, 10);
122
+ if (!isNaN(retry)) {
123
+ message.retry = retry;
149
124
  }
125
+ break;
150
126
  }
151
127
  }
152
128
  }
153
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
154
- finally {
155
- try {
156
- if (!_d && !_a && (_b = lineIter_1.return)) yield tslib_1.__await(_b.call(lineIter_1));
157
- }
158
- finally { if (e_2) throw e_2.error; }
159
- }
160
- });
129
+ }
161
130
  }
162
131
  //# sourceMappingURL=sse.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"sse.js","sourceRoot":"","sources":["../../src/sse.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;AAoClC,0CAMC;;AAjCD,yCAA+D;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,SAAgB,eAAe,CAC7B,WAAgF;IAEhF,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAA,8BAAmB,EAAC,WAAW,CAAC,CAAC;IAC9D,MAAM,SAAS,GAAG,SAAS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I,GAAG,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,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;;AAoClC,0CAMC;AAjCD,yCAA+D;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,SAAgB,eAAe,CAC7B,WAAgF;IAEhF,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAA,8BAAmB,EAAC,WAAW,CAAC,CAAC;IAC9D,MAAM,SAAS,GAAG,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC9C,OAAO,IAAA,uBAAY,EAAC,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,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;oBACxB,CAAC;oBACD,MAAM;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"]}
@@ -1,11 +1,11 @@
1
- // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
- // It should be published with your NPM package. It should not be tracked by Git.
3
- {
4
- "tsdocVersion": "0.12",
5
- "toolPackages": [
6
- {
7
- "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.52.8"
9
- }
10
- ]
11
- }
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.52.8"
9
+ }
10
+ ]
11
+ }
@@ -4,7 +4,6 @@
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.createStream = createStream;
6
6
  exports.ensureAsyncIterable = ensureAsyncIterable;
7
- const tslib_1 = require("tslib");
8
7
  function createStream(asyncIter, cancel) {
9
8
  const stream = iteratorToStream(asyncIter, cancel);
10
9
  /** TODO: remove these polyfills once all supported runtimes support them */
@@ -16,8 +15,7 @@ function polyfillStream(stream, dispose) {
16
15
  return stream;
17
16
  }
18
17
  function makeAsyncDisposable(webStream, dispose) {
19
- var _a;
20
- (_a = Symbol.asyncDispose) !== null && _a !== void 0 ? _a : (Symbol.asyncDispose = Symbol("Symbol.asyncDispose"));
18
+ Symbol.asyncDispose ??= Symbol("Symbol.asyncDispose");
21
19
  if (!webStream[Symbol.asyncDispose]) {
22
20
  webStream[Symbol.asyncDispose] = () => dispose();
23
21
  }
@@ -72,23 +70,21 @@ function isReadableStream(body) {
72
70
  typeof body.getReader === "function" &&
73
71
  typeof body.tee === "function");
74
72
  }
75
- function toAsyncIterable(stream) {
76
- return tslib_1.__asyncGenerator(this, arguments, function* toAsyncIterable_1() {
77
- const reader = stream.getReader();
78
- try {
79
- while (true) {
80
- const { value, done } = yield tslib_1.__await(reader.read());
81
- if (done) {
82
- return yield tslib_1.__await(void 0);
83
- }
84
- yield yield tslib_1.__await(value);
73
+ async function* toAsyncIterable(stream) {
74
+ const reader = stream.getReader();
75
+ try {
76
+ while (true) {
77
+ const { value, done } = await reader.read();
78
+ if (done) {
79
+ return;
85
80
  }
81
+ yield value;
86
82
  }
87
- finally {
88
- const cancelPromise = reader.cancel();
89
- reader.releaseLock();
90
- yield tslib_1.__await(cancelPromise);
91
- }
92
- });
83
+ }
84
+ finally {
85
+ const cancelPromise = reader.cancel();
86
+ reader.releaseLock();
87
+ await cancelPromise;
88
+ }
93
89
  }
94
90
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;AAKlC,oCAOC;AAkDD,kDAyBC;;AAlFD,SAAgB,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,OAAO,CAAC,CAAC;IACrC,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,mBAAmB,CAC1B,SAAc,EACd,OAAgC;;IAEhC,MAAC,MAAM,CAAC,YAAoB,oCAA3B,MAAM,CAAC,YAAoB,GAAK,MAAM,CAAC,qBAAqB,CAAC,EAAC;IAC/D,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC;QACpC,SAAS,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG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AAC,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,SAAgB,eAAe,CAAI,MAAyB;;QAC1D,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,EAAE,CAAC;QAClC,IAAI,CAAC;YACH,OAAO,IAAI,EAAE,CAAC;gBACZ,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,sBAAM,MAAM,CAAC,IAAI,EAAE,CAAA,CAAC;gBAC5C,IAAI,IAAI,EAAE,CAAC;oBACT,qCAAO;gBACT,CAAC;gBACD,4BAAM,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,sBAAM,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,oCAOC;AAkDD,kDAyBC;AAlFD,SAAgB,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,OAAO,CAAC,CAAC;IACrC,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,mBAAmB,CAC1B,SAAc,EACd,OAAgC;IAE/B,MAAM,CAAC,YAAoB,KAAK,MAAM,CAAC,qBAAqB,CAAC,CAAC;IAC/D,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC;QACpC,SAAS,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC;IACnD,CAAC;AACH,CAAC;AAED,SAAS,iBAAiB,CACxB,SAAc;IAEd,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC;QACrC,SAAS,CAAC,MAAM,CAAC,aAAa,CAAC,GAAG,GAAG,EAAE,CAAC,eAAe,CAAC,SAAS,CAAC,CAAC;IACrE,CAAC;IAED,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,CAAC;QACtB,SAAS,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC,eAAe,CAAC,SAAS,CAAC,CAAC;IACtD,CAAC;AACH,CAAC;AAED,SAAS,g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AC,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"]}