webidl.d.ts 7.1 KB

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