Skip to content

Commit 7cf5549

Browse files
committed
chore(upgrade): add streamCompletion helper function (credits to @schnerd & @rauschma)
1 parent 5137f34 commit 7cf5549

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

config-node.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ additionalProperties:
66
files:
77
tsc-multi.mustache:
88
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 numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

0 commit comments

Comments
 (0)