How to transform a highland stream into a node readable stream?

988 Views Asked by At

I have a highland stream streaming strings. I want to consume it by an external library (in my case Amazon S3) and for its SDK I need a standard node Readable Stream.

Is there a way to transform a highland stream into a ReadStream out of the box ? Or do I have to transform it myself?

1

There are 1 best solutions below

0
On BEST ANSWER

It seems that there is no built-in way to convert the highland stream to Node Stream (as per current highland docs).

But highland stream can be piped into Node.js stream.

So you could use a standard PassThrough stream to achieve this in 2 lines of code.

PassThrough stream is basically a repeater. It's a trivial implementation of a Transform stream (both Readable and Writable).

'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
});

It will emit Strings or Buffers depending on objectMode flag.