File tree 2 files changed +33
-1
lines changed
sdk-template-overrides/typescript-axios
2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -6,4 +6,7 @@ additionalProperties:
6
6
files :
7
7
tsc-multi.mustache :
8
8
templateType : SupportingFiles
9
- destinationFilename : tsc-multi.json
9
+ destinationFilename : tsc-multi.json
10
+ stream.mustache :
11
+ templateType : SupportingFiles
12
+ destinationFilename : stream.ts
Original file line number Diff line number Diff line change
1
+ import { Readable } from 'node:stream';
2
+
3
+ async function* chunksToLines(chunksAsync: AsyncIterable<Buffer >): AsyncIterable<string > {
4
+ let previous = " " ;
5
+ for await (const chunk of chunksAsync) {
6
+ const bufferChunk = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
7
+ previous += bufferChunk;
8
+ let eolIndex;
9
+ while ((eolIndex = previous.indexOf(" \n " )) >= 0) {
10
+ // line includes the EOL
11
+ const line = previous.slice(0, eolIndex + 1).trimEnd();
12
+ if (line === " data: [DONE]" ) break ;
13
+ if (line.startsWith(" data: " )) yield line;
14
+ previous = previous.slice(eolIndex + 1);
15
+ }
16
+ }
17
+ }
18
+
19
+ async function* linesToMessages(linesAsync: AsyncIterable<string >): AsyncIterable<string > {
20
+ for await (const line of linesAsync) {
21
+ const message = line.substring(" data :" .length);
22
+
23
+ yield message;
24
+ }
25
+ }
26
+
27
+ export async function* streamCompletion(stream: Readable): AsyncGenerator<string , void, undefined > {
28
+ yield* linesToMessages(chunksToLines(stream));
29
+ }
You can’t perform that action at this time.
0 commit comments