main.d.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Definitions by: Jacob Baskin <https://github.com/jacobbaskin>
  2. // BendingBender <https://github.com/BendingBender>
  3. // Igor Savin <https://github.com/kibertoad>
  4. /// <reference types="node" />
  5. import * as http from 'http';
  6. import { Readable, Writable } from 'stream';
  7. export { Dicer } from "../deps/dicer/lib/dicer";
  8. export const Busboy: BusboyConstructor;
  9. export default Busboy;
  10. export interface BusboyConfig {
  11. /**
  12. * These are the HTTP headers of the incoming request, which are used by individual parsers.
  13. */
  14. headers: BusboyHeaders;
  15. /**
  16. * `highWaterMark` to use for this Busboy instance.
  17. * @default WritableStream default.
  18. */
  19. highWaterMark?: number | undefined;
  20. /**
  21. * highWaterMark to use for file streams.
  22. * @default ReadableStream default.
  23. */
  24. fileHwm?: number | undefined;
  25. /**
  26. * Default character set to use when one isn't defined.
  27. * @default 'utf8'
  28. */
  29. defCharset?: string | undefined;
  30. /**
  31. * Detect if a Part is a file.
  32. *
  33. * By default a file is detected if contentType
  34. * is application/octet-stream or fileName is not
  35. * undefined.
  36. *
  37. * Modify this to handle e.g. Blobs.
  38. */
  39. isPartAFile?: (fieldName: string | undefined, contentType: string | undefined, fileName: string | undefined) => boolean;
  40. /**
  41. * If paths in the multipart 'filename' field shall be preserved.
  42. * @default false
  43. */
  44. preservePath?: boolean | undefined;
  45. /**
  46. * Various limits on incoming data.
  47. */
  48. limits?:
  49. | {
  50. /**
  51. * Max field name size (in bytes)
  52. * @default 100 bytes
  53. */
  54. fieldNameSize?: number | undefined;
  55. /**
  56. * Max field value size (in bytes)
  57. * @default 1MB
  58. */
  59. fieldSize?: number | undefined;
  60. /**
  61. * Max number of non-file fields
  62. * @default Infinity
  63. */
  64. fields?: number | undefined;
  65. /**
  66. * For multipart forms, the max file size (in bytes)
  67. * @default Infinity
  68. */
  69. fileSize?: number | undefined;
  70. /**
  71. * For multipart forms, the max number of file fields
  72. * @default Infinity
  73. */
  74. files?: number | undefined;
  75. /**
  76. * For multipart forms, the max number of parts (fields + files)
  77. * @default Infinity
  78. */
  79. parts?: number | undefined;
  80. /**
  81. * For multipart forms, the max number of header key=>value pairs to parse
  82. * @default 2000
  83. */
  84. headerPairs?: number | undefined;
  85. /**
  86. * For multipart forms, the max size of a header part
  87. * @default 81920
  88. */
  89. headerSize?: number | undefined;
  90. }
  91. | undefined;
  92. }
  93. export type BusboyHeaders = { 'content-type': string } & http.IncomingHttpHeaders;
  94. export interface BusboyFileStream extends
  95. Readable {
  96. truncated: boolean;
  97. /**
  98. * The number of bytes that have been read so far.
  99. */
  100. bytesRead: number;
  101. }
  102. export interface Busboy extends Writable {
  103. addListener<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
  104. addListener(event: string | symbol, listener: (...args: any[]) => void): this;
  105. on<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
  106. on(event: string | symbol, listener: (...args: any[]) => void): this;
  107. once<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
  108. once(event: string | symbol, listener: (...args: any[]) => void): this;
  109. removeListener<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
  110. removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
  111. off<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
  112. off(event: string | symbol, listener: (...args: any[]) => void): this;
  113. prependListener<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
  114. prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
  115. prependOnceListener<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
  116. prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
  117. }
  118. export interface BusboyEvents {
  119. /**
  120. * Emitted for each new file form field found.
  121. *
  122. * * Note: if you listen for this event, you should always handle the `stream` no matter if you care about the
  123. * file contents or not (e.g. you can simply just do `stream.resume();` if you want to discard the contents),
  124. * otherwise the 'finish' event will never fire on the Busboy instance. However, if you don't care about **any**
  125. * incoming files, you can simply not listen for the 'file' event at all and any/all files will be automatically
  126. * and safely discarded (these discarded files do still count towards `files` and `parts` limits).
  127. * * If a configured file size limit was reached, `stream` will both have a boolean property `truncated`
  128. * (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens.
  129. *
  130. * @param listener.transferEncoding Contains the 'Content-Transfer-Encoding' value for the file stream.
  131. * @param listener.mimeType Contains the 'Content-Type' value for the file stream.
  132. */
  133. file: (
  134. fieldname: string,
  135. stream: BusboyFileStream,
  136. filename: string,
  137. transferEncoding: string,
  138. mimeType: string,
  139. ) => void;
  140. /**
  141. * Emitted for each new non-file field found.
  142. */
  143. field: (
  144. fieldname: string,
  145. value: string,
  146. fieldnameTruncated: boolean,
  147. valueTruncated: boolean,
  148. transferEncoding: string,
  149. mimeType: string,
  150. ) => void;
  151. finish: () => void;
  152. /**
  153. * Emitted when specified `parts` limit has been reached. No more 'file' or 'field' events will be emitted.
  154. */
  155. partsLimit: () => void;
  156. /**
  157. * Emitted when specified `files` limit has been reached. No more 'file' events will be emitted.
  158. */
  159. filesLimit: () => void;
  160. /**
  161. * Emitted when specified `fields` limit has been reached. No more 'field' events will be emitted.
  162. */
  163. fieldsLimit: () => void;
  164. error: (error: unknown) => void;
  165. }
  166. export interface BusboyConstructor {
  167. (options: BusboyConfig): Busboy;
  168. new(options: BusboyConfig): Busboy;
  169. }