Skip to content

Commit

Permalink
chore(upgrade): add streamCompletion helper function (credits to @sch…
Browse files Browse the repository at this point in the history
  • Loading branch information
gfortaine committed Mar 5, 2023
1 parent 3797dea commit 1a2e4a8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion config-node.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ additionalProperties:
files:
tsc-multi.mustache:
templateType: SupportingFiles
destinationFilename: tsc-multi.json
destinationFilename: tsc-multi.json
stream.mustache:
templateType: SupportingFiles
destinationFilename: stream.ts
29 changes: 29 additions & 0 deletions sdk-template-overrides/typescript-axios/stream.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Readable } from 'node:stream';

async function* chunksToLines(chunksAsync: AsyncIterable<Buffer>): AsyncIterable<string> {
let previous = "";
for await (const chunk of chunksAsync) {
const bufferChunk = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
previous += bufferChunk;
let eolIndex;
while ((eolIndex = previous.indexOf("\n")) >= 0) {
// line includes the EOL
const line = previous.slice(0, eolIndex + 1).trimEnd();
if (line === "data: [DONE]") break;
if (line.startsWith("data: ")) yield line;
previous = previous.slice(eolIndex + 1);
}
}
}

async function* linesToMessages(linesAsync: AsyncIterable<string>): AsyncIterable<string> {
for await (const line of linesAsync) {
const message = line.substring("data :".length);
yield message;
}
}

export async function* streamCompletion(stream: Readable): AsyncGenerator<string, void, undefined> {
yield* linesToMessages(chunksToLines(stream));
}

0 comments on commit 1a2e4a8

Please sign in to comment.