1 | import {type Readable} from 'node:stream';
|
2 | import {type Buffer} from 'node:buffer';
|
3 |
|
4 | export class MaxBufferError extends Error {
|
5 | readonly name: 'MaxBufferError';
|
6 | constructor();
|
7 | }
|
8 |
|
9 | // eslint-disable-next-line @typescript-eslint/ban-types
|
10 | type TextStreamItem = string | Buffer | ArrayBuffer | ArrayBufferView;
|
11 | export type AnyStream<SteamItem = TextStreamItem> = Readable | ReadableStream<SteamItem> | AsyncIterable<SteamItem>;
|
12 |
|
13 | export type Options = {
|
14 | /**
|
15 | Maximum length of the stream. If exceeded, the promise will be rejected with a `MaxBufferError`.
|
16 |
|
17 | Depending on the [method](#api), the length is measured with [`string.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length), [`buffer.length`](https://nodejs.org/api/buffer.html#buflength), [`arrayBuffer.byteLength`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/byteLength) or [`array.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length).
|
18 |
|
19 | @default Infinity
|
20 | */
|
21 | readonly maxBuffer?: number;
|
22 | };
|
23 |
|
24 | /**
|
25 | Get the given `stream` as a string.
|
26 |
|
27 | @returns The stream's contents as a promise.
|
28 |
|
29 | @example
|
30 | ```
|
31 | import fs from 'node:fs';
|
32 | import getStream from 'get-stream';
|
33 |
|
34 | const stream = fs.createReadStream('unicorn.txt');
|
35 |
|
36 | console.log(await getStream(stream));
|
37 | // ,,))))))));,
|
38 | // __)))))))))))))),
|
39 | // \|/ -\(((((''''((((((((.
|
40 | // -*-==//////(('' . `)))))),
|
41 | // /|\ ))| o ;-. '((((( ,(,
|
42 | // ( `| / ) ;))))' ,_))^;(~
|
43 | // | | | ,))((((_ _____------~~~-. %,;(;(>';'~
|
44 | // o_); ; )))(((` ~---~ `:: \ %%~~)(v;(`('~
|
45 | // ; ''''```` `: `:::|\,__,%% );`'; ~
|
46 | // | _ ) / `:|`----' `-'
|
47 | // ______/\/~ | / /
|
48 | // /~;;.____/;;' / ___--,-( `;;;/
|
49 | // / // _;______;'------~~~~~ /;;/\ /
|
50 | // // | | / ; \;;,\
|
51 | // (<_ | ; /',/-----' _>
|
52 | // \_| ||_ //~;~~~~~~~~~
|
53 | // `\_| (,~~
|
54 | // \~\
|
55 | // ~~
|
56 | ```
|
57 |
|
58 | @example
|
59 | ```
|
60 | import getStream from 'get-stream';
|
61 |
|
62 | const {body: readableStream} = await fetch('https://example.com');
|
63 | console.log(await getStream(readableStream));
|
64 | ```
|
65 |
|
66 | @example
|
67 | ```
|
68 | import {opendir} from 'node:fs/promises';
|
69 | import {getStreamAsArray} from 'get-stream';
|
70 |
|
71 | const asyncIterable = await opendir(directory);
|
72 | console.log(await getStreamAsArray(asyncIterable));
|
73 | ```
|
74 | */
|
75 | export default function getStream(stream: AnyStream, options?: Options): Promise<string>;
|
76 |
|
77 | /**
|
78 | Get the given `stream` as a Node.js [`Buffer`](https://nodejs.org/api/buffer.html#class-buffer).
|
79 |
|
80 | @returns The stream's contents as a promise.
|
81 |
|
82 | @example
|
83 | ```
|
84 | import {getStreamAsBuffer} from 'get-stream';
|
85 |
|
86 | const stream = fs.createReadStream('unicorn.png');
|
87 | console.log(await getStreamAsBuffer(stream));
|
88 | ```
|
89 | */
|
90 | // eslint-disable-next-line @typescript-eslint/ban-types
|
91 | export function getStreamAsBuffer(stream: AnyStream, options?: Options): Promise<Buffer>;
|
92 |
|
93 | /**
|
94 | Get the given `stream` as an [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer).
|
95 |
|
96 | @returns The stream's contents as a promise.
|
97 |
|
98 | @example
|
99 | ```
|
100 | import {getStreamAsArrayBuffer} from 'get-stream';
|
101 |
|
102 | const {body: readableStream} = await fetch('https://example.com');
|
103 | console.log(await getStreamAsArrayBuffer(readableStream));
|
104 | ```
|
105 | */
|
106 | export function getStreamAsArrayBuffer(stream: AnyStream, options?: Options): Promise<ArrayBuffer>;
|
107 |
|
108 | /**
|
109 | Get the given `stream` as an array. Unlike [other methods](#api), this supports [streams of objects](https://nodejs.org/api/stream.html#object-mode).
|
110 |
|
111 | @returns The stream's contents as a promise.
|
112 |
|
113 | @example
|
114 | ```
|
115 | import {getStreamAsArray} from 'get-stream';
|
116 |
|
117 | const {body: readableStream} = await fetch('https://example.com');
|
118 | console.log(await getStreamAsArray(readableStream));
|
119 | ```
|
120 | */
|
121 | export function getStreamAsArray<Item>(stream: AnyStream<Item>, options?: Options): Promise<Item[]>;
|