| 1 | /// <reference types="node"/>
|
|---|
| 2 | import {Stream} from 'stream';
|
|---|
| 3 |
|
|---|
| 4 | declare class MaxBufferErrorClass extends Error {
|
|---|
| 5 | readonly name: 'MaxBufferError';
|
|---|
| 6 | constructor();
|
|---|
| 7 | }
|
|---|
| 8 |
|
|---|
| 9 | declare namespace getStream {
|
|---|
| 10 | interface Options {
|
|---|
| 11 | /**
|
|---|
| 12 | Maximum length of the returned string. If it exceeds this value before the stream ends, the promise will be rejected with a `MaxBufferError` error.
|
|---|
| 13 |
|
|---|
| 14 | @default Infinity
|
|---|
| 15 | */
|
|---|
| 16 | readonly maxBuffer?: number;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | interface OptionsWithEncoding<EncodingType = BufferEncoding> extends Options {
|
|---|
| 20 | /**
|
|---|
| 21 | [Encoding](https://nodejs.org/api/buffer.html#buffer_buffer) of the incoming stream.
|
|---|
| 22 |
|
|---|
| 23 | @default 'utf8'
|
|---|
| 24 | */
|
|---|
| 25 | readonly encoding?: EncodingType;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | type MaxBufferError = MaxBufferErrorClass;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | declare const getStream: {
|
|---|
| 32 | /**
|
|---|
| 33 | Get the `stream` as a string.
|
|---|
| 34 |
|
|---|
| 35 | @returns A promise that resolves when the end event fires on the stream, indicating that there is no more data to be read. The stream is switched to flowing mode.
|
|---|
| 36 |
|
|---|
| 37 | @example
|
|---|
| 38 | ```
|
|---|
| 39 | import * as fs from 'fs';
|
|---|
| 40 | import getStream = require('get-stream');
|
|---|
| 41 |
|
|---|
| 42 | (async () => {
|
|---|
| 43 | const stream = fs.createReadStream('unicorn.txt');
|
|---|
| 44 |
|
|---|
| 45 | console.log(await getStream(stream));
|
|---|
| 46 | // ,,))))))));,
|
|---|
| 47 | // __)))))))))))))),
|
|---|
| 48 | // \|/ -\(((((''''((((((((.
|
|---|
| 49 | // -*-==//////(('' . `)))))),
|
|---|
| 50 | // /|\ ))| o ;-. '((((( ,(,
|
|---|
| 51 | // ( `| / ) ;))))' ,_))^;(~
|
|---|
| 52 | // | | | ,))((((_ _____------~~~-. %,;(;(>';'~
|
|---|
| 53 | // o_); ; )))(((` ~---~ `:: \ %%~~)(v;(`('~
|
|---|
| 54 | // ; ''''```` `: `:::|\,__,%% );`'; ~
|
|---|
| 55 | // | _ ) / `:|`----' `-'
|
|---|
| 56 | // ______/\/~ | / /
|
|---|
| 57 | // /~;;.____/;;' / ___--,-( `;;;/
|
|---|
| 58 | // / // _;______;'------~~~~~ /;;/\ /
|
|---|
| 59 | // // | | / ; \;;,\
|
|---|
| 60 | // (<_ | ; /',/-----' _>
|
|---|
| 61 | // \_| ||_ //~;~~~~~~~~~
|
|---|
| 62 | // `\_| (,~~
|
|---|
| 63 | // \~\
|
|---|
| 64 | // ~~
|
|---|
| 65 | })();
|
|---|
| 66 | ```
|
|---|
| 67 | */
|
|---|
| 68 | (stream: Stream, options?: getStream.OptionsWithEncoding): Promise<string>;
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | Get the `stream` as a buffer.
|
|---|
| 72 |
|
|---|
| 73 | It honors the `maxBuffer` option as above, but it refers to byte length rather than string length.
|
|---|
| 74 | */
|
|---|
| 75 | buffer(
|
|---|
| 76 | stream: Stream,
|
|---|
| 77 | options?: getStream.Options
|
|---|
| 78 | ): Promise<Buffer>;
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | Get the `stream` as an array of values.
|
|---|
| 82 |
|
|---|
| 83 | It honors both the `maxBuffer` and `encoding` options. The behavior changes slightly based on the encoding chosen:
|
|---|
| 84 |
|
|---|
| 85 | - When `encoding` is unset, it assumes an [object mode stream](https://nodesource.com/blog/understanding-object-streams/) and collects values emitted from `stream` unmodified. In this case `maxBuffer` refers to the number of items in the array (not the sum of their sizes).
|
|---|
| 86 | - When `encoding` is set to `buffer`, it collects an array of buffers. `maxBuffer` refers to the summed byte lengths of every buffer in the array.
|
|---|
| 87 | - When `encoding` is set to anything else, it collects an array of strings. `maxBuffer` refers to the summed character lengths of every string in the array.
|
|---|
| 88 | */
|
|---|
| 89 | array<StreamObjectModeType>(
|
|---|
| 90 | stream: Stream,
|
|---|
| 91 | options?: getStream.Options
|
|---|
| 92 | ): Promise<StreamObjectModeType[]>;
|
|---|
| 93 | array(
|
|---|
| 94 | stream: Stream,
|
|---|
| 95 | options: getStream.OptionsWithEncoding<'buffer'>
|
|---|
| 96 | ): Promise<Buffer[]>;
|
|---|
| 97 | array(
|
|---|
| 98 | stream: Stream,
|
|---|
| 99 | options: getStream.OptionsWithEncoding<BufferEncoding>
|
|---|
| 100 | ): Promise<string[]>;
|
|---|
| 101 |
|
|---|
| 102 | MaxBufferError: typeof MaxBufferErrorClass;
|
|---|
| 103 | };
|
|---|
| 104 |
|
|---|
| 105 | export = getStream;
|
|---|