index.d.ts 4.2 KB

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