如何将字符串转换为 ReadableStream(不是 NodeJS 流)。
将字符串转换为 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())
Blob.stream()
进行在线解决方案
const byteStream = new Blob([ str ], {type: 'text/plain'}).stream();