http-parser.d.ts 4.4 KB

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