1.0.15 • Published 3 years ago

writable-stream-and-promise v1.0.15

Weekly downloads
21
License
MIT
Repository
github
Last release
3 years ago

writable-stream-and-promise

npm CI Coverage Status MIT license

Pipe a stream into a Promise.

Usage

Example

const {createReadStream} = require('fs');
const {writableStreamAndPromise} = require('writable-stream-and-promise');
const {createGunzip} = require('zlib');

const processZipFile = async path => {
  const [writable, promise] = writableStreamAndPromise();
  createReadStream(path).pipe(createGunzip()).pipe(writable);
  const contents = await promise;

  // ... process contents ...

  return processedContents;
};

API

writableStreamAndPromise(options)

Returns a 2-tuple (Array): a Writable Buffer stream and a Promise. When the stream ends, the Promise resolves to a Buffer containing all the chunks written to the stream. The Promise rejects if the stream emits an error or when the stream is destroyed. Strings written to the stream will be decoded according to the given encoding or defaultEncoding. The stream does not destroy itself automatically after ending. The stream will emit a 'close' event when it is destroyed.

  • options
    • defaultEncoding The default encoding that is used when no encoding is specified when writing a String. The default is 'utf8'.
    • highWaterMark Writes are completed immediately and synchronously, so this parameter is only really relevant if the stream is corked. highWaterMark indicates the number of bytes that the stream should buffer while corked. The default is 16384 (16 kb).
  • Returns: A 2-tuple (Array of length 2), the first element a Writable stream, and the second element a Promise.

writableObjectStreamAndPromise(options)

Returns a 2-tuple (Array): a Writable object stream and a Promise. When the stream ends, the Promise resolves to an Array containing all the chunks written to the stream. The Promise rejects if the stream emits an error or when the stream is destroyed. The stream does not destroy itself automatically after ending. The stream will emit a 'close' event when it is destroyed.

  • options
    • highWaterMark Writes are completed immediately and synchronously, so this parameters is only really relevant if the stream is corked. highWaterMark indicates the number of objects that the stream should buffer while corked. The default is 16.
  • Returns: A 2-tuple (Array of length 2), the first element a Writable stream, and the second element a Promise.

Author

Raymond Lam (ray@lam-ray.com)

License

MIT

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago