How to convert a string to ReadableStream (not NodeJS stream).
To convert a string to ReadableStream
function stringToStream(str) {
return new ReadableStream({
start(controller) { // https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultController
controller.enqueue(str);
controller.close();
},
})
}
const stringStream = stringToStream("some text");
// convert the string stream to byte using TextEncoderStream if you need to consume as byte like .pipeThrough(new CompressionStream())
const byteStream = stringStream.pipeThrough(new TextEncoderStream())
Or oneline solution by Blob.stream()
const byteStream = new Blob([ str ], {type: 'text/plain'}).stream();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With