Converting a Buffer into a ReadableStream in Node.js

后端 未结 8 1242
借酒劲吻你
借酒劲吻你 2020-11-27 16:09

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

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 16:18

    You don't need to add a whole npm lib for a single file. i refactored it to typescript:

    import { Readable, ReadableOptions } from "stream";
    
    export class MultiStream extends Readable {
      _object: any;
      constructor(object: any, options: ReadableOptions) {
        super(object instanceof Buffer || typeof object === "string" ? options : { objectMode: true });
        this._object = object;
      }
      _read = () => {
        this.push(this._object);
        this._object = null;
      };
    }
    

    based on node-streamifier (the best option as said above).

提交回复
热议问题