UNPKG

4.29 kBTypeScriptView Raw
1import {type Readable} from 'node:stream';
2import {type Buffer} from 'node:buffer';
3
4export class MaxBufferError extends Error {
5 readonly name: 'MaxBufferError';
6 constructor();
7}
8
9// eslint-disable-next-line @typescript-eslint/ban-types
10type TextStreamItem = string | Buffer | ArrayBuffer | ArrayBufferView;
11export type AnyStream<SteamItem = TextStreamItem> = Readable | ReadableStream<SteamItem> | AsyncIterable<SteamItem>;
12
13export 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/**
25Get the given `stream` as a string.
26
27@returns The stream's contents as a promise.
28
29@example
30```
31import fs from 'node:fs';
32import getStream from 'get-stream';
33
34const stream = fs.createReadStream('unicorn.txt');
35
36console.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```
60import getStream from 'get-stream';
61
62const {body: readableStream} = await fetch('https://example.com');
63console.log(await getStream(readableStream));
64```
65
66@example
67```
68import {opendir} from 'node:fs/promises';
69import {getStreamAsArray} from 'get-stream';
70
71const asyncIterable = await opendir(directory);
72console.log(await getStreamAsArray(asyncIterable));
73```
74*/
75export default function getStream(stream: AnyStream, options?: Options): Promise<string>;
76
77/**
78Get 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```
84import {getStreamAsBuffer} from 'get-stream';
85
86const stream = fs.createReadStream('unicorn.png');
87console.log(await getStreamAsBuffer(stream));
88```
89*/
90// eslint-disable-next-line @typescript-eslint/ban-types
91export function getStreamAsBuffer(stream: AnyStream, options?: Options): Promise<Buffer>;
92
93/**
94Get 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```
100import {getStreamAsArrayBuffer} from 'get-stream';
101
102const {body: readableStream} = await fetch('https://example.com');
103console.log(await getStreamAsArrayBuffer(readableStream));
104```
105*/
106export function getStreamAsArrayBuffer(stream: AnyStream, options?: Options): Promise<ArrayBuffer>;
107
108/**
109Get 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```
115import {getStreamAsArray} from 'get-stream';
116
117const {body: readableStream} = await fetch('https://example.com');
118console.log(await getStreamAsArray(readableStream));
119```
120*/
121export function getStreamAsArray<Item>(stream: AnyStream<Item>, options?: Options): Promise<Item[]>;