webidl.d.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // These types are not exported, and are only used internally
  2. import * as undici from './index'
  3. /**
  4. * Take in an unknown value and return one that is of type T
  5. */
  6. type Converter<T> = (object: unknown) => T
  7. type SequenceConverter<T> = (object: unknown, iterable?: IterableIterator<T>) => T[]
  8. type RecordConverter<K extends string, V> = (object: unknown) => Record<K, V>
  9. interface WebidlErrors {
  10. /**
  11. * @description Instantiate an error
  12. */
  13. exception (opts: { header: string, message: string }): TypeError
  14. /**
  15. * @description Instantiate an error when conversion from one type to another has failed
  16. */
  17. conversionFailed (opts: {
  18. prefix: string
  19. argument: string
  20. types: string[]
  21. }): TypeError
  22. /**
  23. * @description Throw an error when an invalid argument is provided
  24. */
  25. invalidArgument (opts: {
  26. prefix: string
  27. value: string
  28. type: string
  29. }): TypeError
  30. }
  31. interface WebIDLTypes {
  32. UNDEFINED: 1,
  33. BOOLEAN: 2,
  34. STRING: 3,
  35. SYMBOL: 4,
  36. NUMBER: 5,
  37. BIGINT: 6,
  38. NULL: 7
  39. OBJECT: 8
  40. }
  41. interface WebidlUtil {
  42. /**
  43. * @see https://tc39.es/ecma262/#sec-ecmascript-data-types-and-values
  44. */
  45. Type (object: unknown): WebIDLTypes[keyof WebIDLTypes]
  46. TypeValueToString (o: unknown):
  47. | 'Undefined'
  48. | 'Boolean'
  49. | 'String'
  50. | 'Symbol'
  51. | 'Number'
  52. | 'BigInt'
  53. | 'Null'
  54. | 'Object'
  55. Types: WebIDLTypes
  56. /**
  57. * @see https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
  58. */
  59. ConvertToInt (
  60. V: unknown,
  61. bitLength: number,
  62. signedness: 'signed' | 'unsigned',
  63. flags?: number
  64. ): number
  65. /**
  66. * @see https://webidl.spec.whatwg.org/#abstract-opdef-integerpart
  67. */
  68. IntegerPart (N: number): number
  69. /**
  70. * Stringifies {@param V}
  71. */
  72. Stringify (V: any): string
  73. MakeTypeAssertion <I>(I: I): (arg: any) => arg is I
  74. /**
  75. * Mark a value as uncloneable for Node.js.
  76. * This is only effective in some newer Node.js versions.
  77. */
  78. markAsUncloneable (V: any): void
  79. IsResizableArrayBuffer (V: ArrayBufferLike): boolean
  80. HasFlag (flag: number, attributes: number): boolean
  81. }
  82. interface WebidlConverters {
  83. /**
  84. * @see https://webidl.spec.whatwg.org/#es-DOMString
  85. */
  86. DOMString (V: unknown, prefix: string, argument: string, flags?: number): string
  87. /**
  88. * @see https://webidl.spec.whatwg.org/#es-ByteString
  89. */
  90. ByteString (V: unknown, prefix: string, argument: string): string
  91. /**
  92. * @see https://webidl.spec.whatwg.org/#es-USVString
  93. */
  94. USVString (V: unknown): string
  95. /**
  96. * @see https://webidl.spec.whatwg.org/#es-boolean
  97. */
  98. boolean (V: unknown): boolean
  99. /**
  100. * @see https://webidl.spec.whatwg.org/#es-any
  101. */
  102. any <Value>(V: Value): Value
  103. /**
  104. * @see https://webidl.spec.whatwg.org/#es-long-long
  105. */
  106. ['long long'] (V: unknown): number
  107. /**
  108. * @see https://webidl.spec.whatwg.org/#es-unsigned-long-long
  109. */
  110. ['unsigned long long'] (V: unknown): number
  111. /**
  112. * @see https://webidl.spec.whatwg.org/#es-unsigned-long
  113. */
  114. ['unsigned long'] (V: unknown): number
  115. /**
  116. * @see https://webidl.spec.whatwg.org/#es-unsigned-short
  117. */
  118. ['unsigned short'] (V: unknown, flags?: number): number
  119. /**
  120. * @see https://webidl.spec.whatwg.org/#idl-ArrayBuffer
  121. */
  122. ArrayBuffer (
  123. V: unknown,
  124. prefix: string,
  125. argument: string,
  126. options?: { allowResizable: boolean }
  127. ): ArrayBuffer
  128. /**
  129. * @see https://webidl.spec.whatwg.org/#idl-SharedArrayBuffer
  130. */
  131. SharedArrayBuffer (
  132. V: unknown,
  133. prefix: string,
  134. argument: string,
  135. options?: { allowResizable: boolean }
  136. ): SharedArrayBuffer
  137. /**
  138. * @see https://webidl.spec.whatwg.org/#es-buffer-source-types
  139. */
  140. TypedArray (
  141. V: unknown,
  142. T: new () => NodeJS.TypedArray,
  143. prefix: string,
  144. argument: string,
  145. flags?: number
  146. ): NodeJS.TypedArray
  147. /**
  148. * @see https://webidl.spec.whatwg.org/#es-buffer-source-types
  149. */
  150. DataView (
  151. V: unknown,
  152. prefix: string,
  153. argument: string,
  154. flags?: number
  155. ): DataView
  156. /**
  157. * @see https://webidl.spec.whatwg.org/#es-buffer-source-types
  158. */
  159. ArrayBufferView (
  160. V: unknown,
  161. prefix: string,
  162. argument: string,
  163. flags?: number
  164. ): NodeJS.ArrayBufferView
  165. /**
  166. * @see https://webidl.spec.whatwg.org/#BufferSource
  167. */
  168. BufferSource (
  169. V: unknown,
  170. prefix: string,
  171. argument: string,
  172. flags?: number
  173. ): ArrayBuffer | NodeJS.ArrayBufferView
  174. /**
  175. * @see https://webidl.spec.whatwg.org/#AllowSharedBufferSource
  176. */
  177. AllowSharedBufferSource (
  178. V: unknown,
  179. prefix: string,
  180. argument: string,
  181. flags?: number
  182. ): ArrayBuffer | SharedArrayBuffer | NodeJS.ArrayBufferView
  183. ['sequence<ByteString>']: SequenceConverter<string>
  184. ['sequence<sequence<ByteString>>']: SequenceConverter<string[]>
  185. ['record<ByteString, ByteString>']: RecordConverter<string, string>
  186. /**
  187. * @see https://fetch.spec.whatwg.org/#requestinfo
  188. */
  189. RequestInfo (V: unknown): undici.Request | string
  190. /**
  191. * @see https://fetch.spec.whatwg.org/#requestinit
  192. */
  193. RequestInit (V: unknown): undici.RequestInit
  194. /**
  195. * @see https://html.spec.whatwg.org/multipage/webappapis.html#eventhandlernonnull
  196. */
  197. EventHandlerNonNull (V: unknown): Function | null
  198. WebSocketStreamWrite (V: unknown): ArrayBuffer | NodeJS.TypedArray | string
  199. [Key: string]: (...args: any[]) => unknown
  200. }
  201. type WebidlIsFunction<T> = (arg: any) => arg is T
  202. interface WebidlIs {
  203. Request: WebidlIsFunction<undici.Request>
  204. Response: WebidlIsFunction<undici.Response>
  205. ReadableStream: WebidlIsFunction<ReadableStream>
  206. Blob: WebidlIsFunction<Blob>
  207. URLSearchParams: WebidlIsFunction<URLSearchParams>
  208. File: WebidlIsFunction<File>
  209. FormData: WebidlIsFunction<undici.FormData>
  210. URL: WebidlIsFunction<URL>
  211. WebSocketError: WebidlIsFunction<undici.WebSocketError>
  212. AbortSignal: WebidlIsFunction<AbortSignal>
  213. MessagePort: WebidlIsFunction<MessagePort>
  214. USVString: WebidlIsFunction<string>
  215. /**
  216. * @see https://webidl.spec.whatwg.org/#BufferSource
  217. */
  218. BufferSource: WebidlIsFunction<ArrayBuffer | NodeJS.TypedArray>
  219. }
  220. export interface Webidl {
  221. errors: WebidlErrors
  222. util: WebidlUtil
  223. converters: WebidlConverters
  224. is: WebidlIs
  225. attributes: WebIDLExtendedAttributes
  226. /**
  227. * @description Performs a brand-check on {@param V} to ensure it is a
  228. * {@param cls} object.
  229. */
  230. brandCheck <Interface extends new () => unknown>(V: unknown, cls: Interface): asserts V is Interface
  231. brandCheckMultiple <Interfaces extends (new () => unknown)[]> (list: Interfaces): (V: any) => asserts V is Interfaces[number]
  232. /**
  233. * @see https://webidl.spec.whatwg.org/#es-sequence
  234. * @description Convert a value, V, to a WebIDL sequence type.
  235. */
  236. sequenceConverter <Type>(C: Converter<Type>): SequenceConverter<Type>
  237. illegalConstructor (): never
  238. /**
  239. * @see https://webidl.spec.whatwg.org/#es-to-record
  240. * @description Convert a value, V, to a WebIDL record type.
  241. */
  242. recordConverter <K extends string, V>(
  243. keyConverter: Converter<K>,
  244. valueConverter: Converter<V>
  245. ): RecordConverter<K, V>
  246. /**
  247. * Similar to {@link Webidl.brandCheck} but allows skipping the check if third party
  248. * interfaces are allowed.
  249. */
  250. interfaceConverter <Interface>(typeCheck: WebidlIsFunction<Interface>, name: string): (
  251. V: unknown,
  252. prefix: string,
  253. argument: string
  254. ) => asserts V is Interface
  255. // TODO(@KhafraDev): a type could likely be implemented that can infer the return type
  256. // from the converters given?
  257. /**
  258. * Converts a value, V, to a WebIDL dictionary types. Allows limiting which keys are
  259. * allowed, values allowed, optional and required keys. Auto converts the value to
  260. * a type given a converter.
  261. */
  262. dictionaryConverter (converters: {
  263. key: string,
  264. defaultValue?: () => unknown,
  265. required?: boolean,
  266. converter: (...args: unknown[]) => unknown,
  267. allowedValues?: unknown[]
  268. }[]): (V: unknown) => Record<string, unknown>
  269. /**
  270. * @see https://webidl.spec.whatwg.org/#idl-nullable-type
  271. * @description allows a type, V, to be null
  272. */
  273. nullableConverter <T>(
  274. converter: Converter<T>
  275. ): (V: unknown) => ReturnType<typeof converter> | null
  276. argumentLengthCheck (args: { length: number }, min: number, context: string): void
  277. }
  278. interface WebIDLExtendedAttributes {
  279. /** https://webidl.spec.whatwg.org/#Clamp */
  280. Clamp: number
  281. /** https://webidl.spec.whatwg.org/#EnforceRange */
  282. EnforceRange: number
  283. /** https://webidl.spec.whatwg.org/#AllowShared */
  284. AllowShared: number
  285. /** https://webidl.spec.whatwg.org/#AllowResizable */
  286. AllowResizable: number
  287. /** https://webidl.spec.whatwg.org/#LegacyNullToEmptyString */
  288. LegacyNullToEmptyString: number
  289. }