index.d.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { Readable } from 'stream';
  2. declare namespace getRawBody {
  3. export type Encoding = string | true;
  4. export interface Options {
  5. /**
  6. * The expected length of the stream.
  7. */
  8. length?: number | string | null;
  9. /**
  10. * The byte limit of the body. This is the number of bytes or any string
  11. * format supported by `bytes`, for example `1000`, `'500kb'` or `'3mb'`.
  12. */
  13. limit?: number | string | null;
  14. /**
  15. * The encoding to use to decode the body into a string. By default, a
  16. * `Buffer` instance will be returned when no encoding is specified. Most
  17. * likely, you want `utf-8`, so setting encoding to `true` will decode as
  18. * `utf-8`. You can use any type of encoding supported by `iconv-lite`.
  19. */
  20. encoding?: Encoding | null;
  21. }
  22. export interface RawBodyError extends Error {
  23. /**
  24. * The limit in bytes.
  25. */
  26. limit?: number;
  27. /**
  28. * The expected length of the stream.
  29. */
  30. length?: number;
  31. expected?: number;
  32. /**
  33. * The received bytes.
  34. */
  35. received?: number;
  36. /**
  37. * The encoding.
  38. */
  39. encoding?: string;
  40. /**
  41. * The corresponding status code for the error.
  42. */
  43. status: number;
  44. statusCode: number;
  45. /**
  46. * The error type.
  47. */
  48. type: string;
  49. }
  50. }
  51. /**
  52. * Gets the entire buffer of a stream either as a `Buffer` or a string.
  53. * Validates the stream's length against an expected length and maximum
  54. * limit. Ideal for parsing request bodies.
  55. */
  56. declare function getRawBody(
  57. stream: Readable,
  58. callback: (err: getRawBody.RawBodyError, body: Buffer) => void
  59. ): void;
  60. declare function getRawBody(
  61. stream: Readable,
  62. options: (getRawBody.Options & { encoding: getRawBody.Encoding }) | getRawBody.Encoding,
  63. callback: (err: getRawBody.RawBodyError, body: string) => void
  64. ): void;
  65. declare function getRawBody(
  66. stream: Readable,
  67. options: getRawBody.Options,
  68. callback: (err: getRawBody.RawBodyError, body: Buffer) => void
  69. ): void;
  70. declare function getRawBody(
  71. stream: Readable,
  72. options: (getRawBody.Options & { encoding: getRawBody.Encoding }) | getRawBody.Encoding
  73. ): Promise<string>;
  74. declare function getRawBody(
  75. stream: Readable,
  76. options?: getRawBody.Options
  77. ): Promise<Buffer>;
  78. export = getRawBody;