如何将高地流转换为节点可读流?

时间:2017-01-16 14:48:21

标签: javascript stream highland.js

我有一个highland流流式传输字符串。我希望通过外部库(在我的案例中为Amazon S3)使用它,对于它的SDK,我需要一个标准的node Readable Stream

有没有办法将高地流转换为开箱即用的ReadStream?或者我必须自己改造它?

1 个答案:

答案 0 :(得分:2)

似乎没有内置的方法将高地流转换为节点流(根据当前的高地文档)。

但是高地流可以通过管道传输到Node.js流中。

因此,您可以使用标准的PassThrough流来实现2行代码。

PassThrough流基本上是一个转发器。它是一个简单的Transform流实现(可读和可写)。



'use strict';

const h = require('highland');
const {PassThrough, Readable} = require('stream');

let stringHighlandStream = h(['a', 'b', 'c']);

let readable = new PassThrough({objectMode: true});
stringHighlandStream.pipe(readable);

console.log(stringHighlandStream instanceof Readable); //false
console.log(readable instanceof Readable); //true

readable.on('data', function (data) {
	console.log(data); // a, b, c or <Buffer 61> ... if you omit objectMode
});
&#13;
&#13;
&#13;

它将根据objectMode标志发出字符串或缓冲区。