在 Node.js 中将 Buffer 转换为 ReadableStream [英] Converting a Buffer into a ReadableStream in Node.js

查看:42
本文介绍了在 Node.js 中将 Buffer 转换为 ReadableStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 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 中将 Buffer 转换为 ReadableStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆