Unverified Commit 3c75ae7c authored by Ethan Arrowood's avatar Ethan Arrowood Committed by GitHub

simplify `streamToString` method from `node-web-streams.helper.ts` (#62841)

The `encode-decode.ts` file is completely replaceable with the
[`TextDecoderStream`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream)
api, but also we can simplify the `streamToString` function too.

Working on some benchmarks now - wanted to get CI running to see if this
breaks anything though.
parent c206d89e
export function createDecodeTransformStream(decoder = new TextDecoder()) {
return new TransformStream<Uint8Array, string>({
transform(chunk, controller) {
return controller.enqueue(decoder.decode(chunk, { stream: true }))
},
flush(controller) {
return controller.enqueue(decoder.decode())
},
})
}
import { getTracer } from '../lib/trace/tracer'
import { AppRenderSpan } from '../lib/trace/constants'
import { createDecodeTransformStream } from './encode-decode'
import { DetachedPromise } from '../../lib/detached-promise'
import { scheduleImmediate, atLeastOneTask } from '../../lib/scheduler'
......@@ -71,20 +70,17 @@ export function streamFromString(str: string): ReadableStream<Uint8Array> {
export async function streamToString(
stream: ReadableStream<Uint8Array>
): Promise<string> {
let buffer = ''
await stream
// Decode the streamed chunks to turn them into strings.
.pipeThrough(createDecodeTransformStream())
.pipeTo(
new WritableStream<string>({
write(chunk) {
buffer += chunk
},
})
)
const decoder = new TextDecoder('utf-8', { fatal: true })
let string = ''
// @ts-expect-error TypeScript gets this wrong (https://nodejs.org/api/webstreams.html#async-iteration)
for await (const chunk of stream) {
string += decoder.decode(chunk, { stream: true })
}
string += decoder.decode()
return buffer
return string
}
export function createBufferedTransformStream(): TransformStream<
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment