问题描述
我对Buffers和ReadableStreams还是很陌生,所以也许这是一个愚蠢的问题.我有一个使用ReadableStream
作为输入的库,但是我的输入只是base64格式的图像.我可以像这样转换Buffer
中的数据:
I'm fairly new to Buffers and ReadableStreams, so maybe this is a stupid question. I have a library that takes as input a ReadableStream
, but my input is just a base64 format image. I could convert the data I have in a Buffer
like so:
var img = new Buffer(img_string, 'base64');
但是我不知道如何将其转换为ReadableStream
或将获得的Buffer
转换为ReadableStream
.
But I have no idea how to convert it to a ReadableStream
or convert the Buffer
I obtained to a ReadableStream
.
有没有办法做到这一点,还是我试图实现不可能?
Is there a way to do this or am I trying to achieve the impossible?
谢谢.
推荐答案
您可以使用创建一个ReadableStream像这样的节点流缓冲区:
// Initialize stream
var myReadableStreamBuffer = new streamBuffers.ReadableStreamBuffer({
frequency: 10, // in milliseconds.
chunkSize: 2048 // in bytes.
});
// With a buffer
myReadableStreamBuffer.put(aBuffer);
// Or with a string
myReadableStreamBuffer.put("A String", "utf8");
频率不能为0,因此会引入一定的延迟.
The frequency cannot be 0 so this will introduce a certain delay.
这篇关于在Node.js中将缓冲区转换为ReadableStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!