UNPKG

2.31 kBTypeScriptView Raw
1declare namespace getRawBody {
2 export type Encoding = string | true;
3
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
23 export interface RawBodyError extends Error {
24 /**
25 * The limit in bytes.
26 */
27 limit?: number;
28 /**
29 * The expected length of the stream.
30 */
31 length?: number;
32 expected?: number;
33 /**
34 * The received bytes.
35 */
36 received?: number;
37 /**
38 * The encoding.
39 */
40 encoding?: string;
41 /**
42 * The corresponding status code for the error.
43 */
44 status: number;
45 statusCode: number;
46 /**
47 * The error type.
48 */
49 type: string;
50 }
51}
52
53/**
54 * Gets the entire buffer of a stream either as a `Buffer` or a string.
55 * Validates the stream's length against an expected length and maximum
56 * limit. Ideal for parsing request bodies.
57 */
58declare function getRawBody(
59 stream: NodeJS.ReadableStream,
60 callback: (err: getRawBody.RawBodyError, body: Buffer) => void
61): void;
62
63declare function getRawBody(
64 stream: NodeJS.ReadableStream,
65 options: (getRawBody.Options & { encoding: getRawBody.Encoding }) | getRawBody.Encoding,
66 callback: (err: getRawBody.RawBodyError, body: string) => void
67): void;
68
69declare function getRawBody(
70 stream: NodeJS.ReadableStream,
71 options: getRawBody.Options,
72 callback: (err: getRawBody.RawBodyError, body: Buffer) => void
73): void;
74
75declare function getRawBody(
76 stream: NodeJS.ReadableStream,
77 options: (getRawBody.Options & { encoding: getRawBody.Encoding }) | getRawBody.Encoding
78): Promise<string>;
79
80declare function getRawBody(
81 stream: NodeJS.ReadableStream,
82 options?: getRawBody.Options
83): Promise<Buffer>;
84
85export = getRawBody;