File size: 4,504 Bytes
5fae594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
type ParserType =
  | 'REQUEST'
  | 'RESPONSE'

type RequestMethod =
  | 'DELETE'
  | 'GET'
  | 'HEAD'
  | 'POST'
  | 'PUT'
  | 'CONNECT'
  | 'OPTIONS'
  | 'TRACE'
  | 'COPY'
  | 'LOCK'
  | 'MKCOL'
  | 'MOVE'
  | 'PROPFIND'
  | 'PROPPATCH'
  | 'SEARCH'
  | 'UNLOCK'
  | 'BIND'
  | 'REBIND'
  | 'UNBIND'
  | 'ACL'
  | 'REPORT'
  | 'MKACTIVITY'
  | 'CHECKOUT'
  | 'MERGE'
  | 'M-SEARCH'
  | 'NOTIFY'
  | 'SUBSCRIBE'
  | 'UNSUBSCRIBE'
  | 'PATCH'
  | 'PURGE'
  | 'MKCALENDAR'
  | 'LINK'
  | 'UNLINK'
  | string

type StateHeaderKey =
  | 'REQUEST_LINE'
  | 'RESPONSE_LINE'
  | 'HEADER'

type StateFinishAllowedKey =
  | 'REQUEST_LINE'
  | 'RESPONSE_LINE'
  | 'BODY_RAW'

type HeaderObject = Array<string>
type noop<T = void> = ()=> T

type HeaderInfo<HEADER = HeaderObject> = {
  versionMajor: number
  versionMinor: number
  headers: HEADER
  method: number
  url: string
  statusCode: number
  statusMessage: string
  upgrade: boolean
  shouldKeepAlive: boolean
}
export type OnHeadersCompleteParser<HEADER = HeaderObject, Mode_0_12 extends boolean = true> = Mode_0_12 extends true
  ? (info: HeaderInfo<HEADER>)=> number | void
  : (
    versionMajor: number,
    versionMinor: number,
    headers: HEADER,
    method: number,
    url: string,
    statusCode: number,
    statusMessage: string,
    upgrade: boolean,
    shouldKeepAlive: boolean,
  )=> number | void
export type OnBodyParser = (chunk: Buffer, offset: number, length: number)=> void
// Only called in the slow case where slow means
// that the request headers were either fragmented
// across multiple TCP packets or too large to be
// processed in a single run. This method is also
// called to process trailing HTTP headers.
export type OnHeadersParser = (headers: string[], url: string)=> void

declare class HTTPParserJS {
  initialize(type: ParserType, async_resource?: unknown): void

  // Some handler stubs, needed for compatibility
  [HTTPParser.kOnHeaders]: OnHeadersParser
  [HTTPParser.kOnHeadersComplete]: OnHeadersCompleteParser
  [HTTPParser.kOnBody]: OnBodyParser
  [HTTPParser.kOnMessageComplete]: noop

  reinitialize: HTTPParserConstructor
  close: noop
  pause: noop
  resume: noop
  free: noop
  private _compatMode0_11: false | boolean
  getAsyncId: noop<0>

  execute(chunk: Buffer, start?: number, length?: number): number | Error
  finish(): void | Error

  // These three methods are used for an internal speed optimization, and it also
  // works if theses are noops. Basically consume() asks us to read the bytes
  // ourselves, but if we don't do it we get them through execute().
  consume: noop
  unconsume: noop
  getCurrentBuffer: noop

  /**
   * For correct error handling - see HTTPParser#execute
   * @example this.userCall()(userFunction('arg'));
   */
  userCall<T = unknown>(): (ret?: T)=> T
  private nextRequest: noop
  private consumeLine: noop<string|void>
  parseHeader(line: string, headers: string[]): void
  private REQUEST_LINE: noop
  private RESPONSE_LINE: noop
  shouldKeepAlive(): boolean
  /**
   * 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.
   */
  private HEADER(): void | boolean
  private BODY_CHUNKHEAD(): void
  private BODY_CHUNK(): void
  private BODY_CHUNKEMPTYLINE(): void
  private BODY_CHUNKTRAILERS(): void
  private BODY_RAW(): void
  private BODY_SIZED(): void

  get onHeaders(): OnHeadersParser
  set onHeaders(to: OnHeadersParser)

  get onHeadersComplete(): OnHeadersCompleteParser
  set onHeadersComplete(to: OnHeadersCompleteParser)

  get onBody(): OnBodyParser
  set onBody(to: OnBodyParser)

  get onMessageComplete(): noop
  set onMessageComplete(to: noop)
}

interface HTTPParserConstructor extends Function {
  new(type?: ParserType): HTTPParserJS
  (type?: ParserType): void

  readonly prototype: HTTPParserJS

  readonly REQUEST: 'REQUEST'
  readonly RESPONSE: 'RESPONSE'
  readonly methods: RequestMethod[]

  encoding: 'ascii'|string
  /**
   * maxHeaderSize (in bytes) is configurable, but 80kb by default;
   * @default 80 * 1024 = 80kb
   */
  maxHeaderSize: 81920|number

  // Note: *not* starting with kOnHeaders=0 line the Node parser, because any
  // newly added constants (kOnTimeout in Node v12.19.0) will overwrite 0!
  readonly kOnHeaders: 1
  readonly kOnHeadersComplete: 2
  readonly kOnBody: 3
  readonly kOnMessageComplete: 4

  kOnExecute(): void
}
export const HTTPParser: HTTPParserConstructor
export const methods: RequestMethod[]