UNPKG

4.5 kBTypeScriptView Raw
1type ParserType =
2 | 'REQUEST'
3 | 'RESPONSE'
4
5type RequestMethod =
6 | 'DELETE'
7 | 'GET'
8 | 'HEAD'
9 | 'POST'
10 | 'PUT'
11 | 'CONNECT'
12 | 'OPTIONS'
13 | 'TRACE'
14 | 'COPY'
15 | 'LOCK'
16 | 'MKCOL'
17 | 'MOVE'
18 | 'PROPFIND'
19 | 'PROPPATCH'
20 | 'SEARCH'
21 | 'UNLOCK'
22 | 'BIND'
23 | 'REBIND'
24 | 'UNBIND'
25 | 'ACL'
26 | 'REPORT'
27 | 'MKACTIVITY'
28 | 'CHECKOUT'
29 | 'MERGE'
30 | 'M-SEARCH'
31 | 'NOTIFY'
32 | 'SUBSCRIBE'
33 | 'UNSUBSCRIBE'
34 | 'PATCH'
35 | 'PURGE'
36 | 'MKCALENDAR'
37 | 'LINK'
38 | 'UNLINK'
39 | string
40
41type StateHeaderKey =
42 | 'REQUEST_LINE'
43 | 'RESPONSE_LINE'
44 | 'HEADER'
45
46type StateFinishAllowedKey =
47 | 'REQUEST_LINE'
48 | 'RESPONSE_LINE'
49 | 'BODY_RAW'
50
51type HeaderObject = Array<string>
52type noop<T = void> = ()=> T
53
54type HeaderInfo<HEADER = HeaderObject> = {
55 versionMajor: number
56 versionMinor: number
57 headers: HEADER
58 method: number
59 url: string
60 statusCode: number
61 statusMessage: string
62 upgrade: boolean
63 shouldKeepAlive: boolean
64}
65export type OnHeadersCompleteParser<HEADER = HeaderObject, Mode_0_12 extends boolean = true> = Mode_0_12 extends true
66 ? (info: HeaderInfo<HEADER>)=> number | void
67 : (
68 versionMajor: number,
69 versionMinor: number,
70 headers: HEADER,
71 method: number,
72 url: string,
73 statusCode: number,
74 statusMessage: string,
75 upgrade: boolean,
76 shouldKeepAlive: boolean,
77 )=> number | void
78export type OnBodyParser = (chunk: Buffer, offset: number, length: number)=> void
79// Only called in the slow case where slow means
80// that the request headers were either fragmented
81// across multiple TCP packets or too large to be
82// processed in a single run. This method is also
83// called to process trailing HTTP headers.
84export type OnHeadersParser = (headers: string[], url: string)=> void
85
86declare class HTTPParserJS {
87 initialize(type: ParserType, async_resource?: unknown): void
88
89 // Some handler stubs, needed for compatibility
90 [HTTPParser.kOnHeaders]: OnHeadersParser
91 [HTTPParser.kOnHeadersComplete]: OnHeadersCompleteParser
92 [HTTPParser.kOnBody]: OnBodyParser
93 [HTTPParser.kOnMessageComplete]: noop
94
95 reinitialize: HTTPParserConstructor
96 close: noop
97 pause: noop
98 resume: noop
99 free: noop
100 private _compatMode0_11: false | boolean
101 getAsyncId: noop<0>
102
103 execute(chunk: Buffer, start?: number, length?: number): number | Error
104 finish(): void | Error
105
106 // These three methods are used for an internal speed optimization, and it also
107 // works if theses are noops. Basically consume() asks us to read the bytes
108 // ourselves, but if we don't do it we get them through execute().
109 consume: noop
110 unconsume: noop
111 getCurrentBuffer: noop
112
113 /**
114 * For correct error handling - see HTTPParser#execute
115 * @example this.userCall()(userFunction('arg'));
116 */
117 userCall<T = unknown>(): (ret?: T)=> T
118 private nextRequest: noop
119 private consumeLine: noop<string|void>
120 parseHeader(line: string, headers: string[]): void
121 private REQUEST_LINE: noop
122 private RESPONSE_LINE: noop
123 shouldKeepAlive(): boolean
124 /**
125 * For older versions of node (v6.x and older?), that return `skipBody=1` or `skipBody=true`, need this `return true;` if it's an upgrade request.
126 */
127 private HEADER(): void | boolean
128 private BODY_CHUNKHEAD(): void
129 private BODY_CHUNK(): void
130 private BODY_CHUNKEMPTYLINE(): void
131 private BODY_CHUNKTRAILERS(): void
132 private BODY_RAW(): void
133 private BODY_SIZED(): void
134
135 get onHeaders(): OnHeadersParser
136 set onHeaders(to: OnHeadersParser)
137
138 get onHeadersComplete(): OnHeadersCompleteParser
139 set onHeadersComplete(to: OnHeadersCompleteParser)
140
141 get onBody(): OnBodyParser
142 set onBody(to: OnBodyParser)
143
144 get onMessageComplete(): noop
145 set onMessageComplete(to: noop)
146}
147
148interface HTTPParserConstructor extends Function {
149 new(type?: ParserType): HTTPParserJS
150 (type?: ParserType): void
151
152 readonly prototype: HTTPParserJS
153
154 readonly REQUEST: 'REQUEST'
155 readonly RESPONSE: 'RESPONSE'
156 readonly methods: RequestMethod[]
157
158 encoding: 'ascii'|string
159 /**
160 * maxHeaderSize (in bytes) is configurable, but 80kb by default;
161 * @default 80 * 1024 = 80kb
162 */
163 maxHeaderSize: 81920|number
164
165 // Note: *not* starting with kOnHeaders=0 line the Node parser, because any
166 // newly added constants (kOnTimeout in Node v12.19.0) will overwrite 0!
167 readonly kOnHeaders: 1
168 readonly kOnHeadersComplete: 2
169 readonly kOnBody: 3
170 readonly kOnMessageComplete: 4
171
172 kOnExecute(): void
173}
174export const HTTPParser: HTTPParserConstructor
175export const methods: RequestMethod[]