Converting a Buffer into a ReadableStream in Node.js

后端 未结 8 1054
借酒劲吻你
借酒劲吻你 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).

    0 讨论(0)
  • 2020-11-27 16:21

    something like this...

    import { Readable } from 'stream'
    
    const buffer = new Buffer(img_string, 'base64')
    const readable = new Readable()
    readable._read = () => {} // _read is required but you can noop it
    readable.push(buffer)
    readable.push(null)
    
    readable.pipe(consumer) // consume the stream
    

    In the general course, a readable stream's _read function should collect data from the underlying source and push it incrementally ensuring you don't harvest a huge source into memory before it's needed.

    In this case though you already have the source in memory, so _read is not required.

    Pushing the whole buffer just wraps it in the readable stream api.

    0 讨论(0)
  • 2020-11-27 16:25

    You can create a ReadableStream using Node Stream Buffers like so:

    // Initialize stream
    var myReadableStreamBuffer = new streamBuffers.ReadableStreamBuffer({
      frequency: 10,      // in milliseconds.
      chunkSize: 2048     // in bytes.
    }); 
    
    // With a buffer
    myReadableStreamBuffer.put(aBuffer);
    
    // Or with a string
    myReadableStreamBuffer.put("A String", "utf8");
    

    The frequency cannot be 0 so this will introduce a certain delay.

    0 讨论(0)
  • 2020-11-27 16:28

    Try this:

    const Duplex = require('stream').Duplex;  // core NodeJS API
    function bufferToStream(buffer) {  
      let stream = new Duplex();
      stream.push(buffer);
      stream.push(null);
      return stream;
    }
    

    Source: Brian Mancini -> http://derpturkey.com/buffer-to-stream-in-node/

    0 讨论(0)
  • 2020-11-27 16:32

    Node Stream Buffer is obviously designed for use in testing; the inability to avoid a delay makes it a poor choice for production use.

    Gabriel Llamas suggests streamifier in this answer: How to wrap a buffer as a stream2 Readable stream?

    0 讨论(0)
  • 2020-11-27 16:33

    This is my simple code for this.

    import { Readable } from 'stream';
    
    const newStream = new Readable({
                        read() {
                          this.push(someBuffer);
                        },
                      })
    
    0 讨论(0)
提交回复
热议问题