Last active
May 22, 2024 05:20
-
-
Save lambrospetrou/0c9ac9da14d8d241ae3634981ceb2871 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Option 1 | |
async function streamToString(stream: NodeJS.ReadableStream): Promise<string> { | |
const chunks: Array<any> = []; | |
for await (let chunk of stream) { | |
chunks.push(chunk) | |
} | |
const buffer = Buffer.concat(chunks); | |
return buffer.toString("utf-8") | |
} | |
// Option 2 | |
function streamToString2(stream: NodeJS.ReadableStream): Promise<string> { | |
const chunks: Array<any> = [] | |
return new Promise((resolve, reject) => { | |
stream.on('data', chunk => chunks.push(chunk)) | |
stream.on('error', reject) | |
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8'))) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Above didn't work but other version did work: