我有一个高地流串流。我想通过外部库(在我的情况下为 Amazon S3)使用它,并且对于它的 SDK,我需要一个标准节点 Readable Stream。
有没有办法将高地流转换为开箱即用的 ReadStream ?还是我必须自己改造?
我有一个高地流串流。我想通过外部库(在我的情况下为 Amazon S3)使用它,并且对于它的 SDK,我需要一个标准节点 Readable Stream。
有没有办法将高地流转换为开箱即用的 ReadStream ?还是我必须自己改造?
似乎没有将高地流转换为节点流的内置方法(根据当前的高地文档)。
但是高地流可以通过管道传输到 Node.js 流中。
因此,您可以使用标准的PassThrough流在 2 行代码中实现这一点。
PassThrough 流基本上是一个中继器。这是转换流(可读和可写)的简单实现。
'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
});
它将根据 objectMode 标志发出字符串或缓冲区。