【问题标题】:How to transform a highland stream into a node readable stream?如何将高地流转换为节点可读流?
【发布时间】:2017-01-16 14:48:20
【问题描述】:

我有一个highland 流串流。我想通过外部库(在我的情况下为 Amazon S3)使用它,并且对于它的 SDK,我需要一个标准的 node Readable Stream

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

【问题讨论】:

    标签: javascript stream highland.js


    【解决方案1】:

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

    但是高地流可以通过管道传输到 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 标志发出字符串或缓冲区。

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 2014-07-16
      • 2016-06-01
      • 2021-12-22
      • 2023-03-09
      • 1970-01-01
      • 2017-11-10
      • 2020-12-24
      相关资源
      最近更新 更多