webidl.d.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // These types are not exported, and are only used internally
  2. /**
  3. * Take in an unknown value and return one that is of type T
  4. */
  5. type Converter<T> = (object: unknown) => T
  6. type SequenceConverter<T> = (object: unknown, iterable?: IterableIterator<T>) => T[]
  7. type RecordConverter<K extends string, V> = (object: unknown) => Record<K, V>
  8. interface ConvertToIntOpts {
  9. clamp?: boolean
  10. enforceRange?: boolean
  11. }
  12. interface WebidlErrors {
  13. exception (opts: { header: string, message: string }): TypeError
  14. /**
  15. * @description Throw 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 WebidlUtil {
  32. /**
  33. * @see https://tc39.es/ecma262/#sec-ecmascript-data-types-and-values
  34. */
  35. Type (object: unknown):
  36. | 'Undefined'
  37. | 'Boolean'
  38. | 'String'
  39. | 'Symbol'
  40. | 'Number'
  41. | 'BigInt'
  42. | 'Null'
  43. | 'Object'
  44. /**
  45. * @see https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
  46. */
  47. ConvertToInt (
  48. V: unknown,
  49. bitLength: number,
  50. signedness: 'signed' | 'unsigned',
  51. opts?: ConvertToIntOpts
  52. ): number
  53. /**
  54. * @see https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
  55. */
  56. IntegerPart (N: number): number
  57. /**
  58. * Stringifies {@param V}
  59. */
  60. Stringify (V: any): string
  61. /**
  62. * Mark a value as uncloneable for Node.js.
  63. * This is only effective in some newer Node.js versions.
  64. */
  65. markAsUncloneable (V: any): void
  66. }
  67. interface WebidlConverters {
  68. /**
  69. * @see https://webidl.spec.whatwg.org/#es-DOMString
  70. */
  71. DOMString (V: unknown, prefix: string, argument: string, opts?: {
  72. legacyNullToEmptyString: boolean
  73. }): string
  74. /**
  75. * @see https://webidl.spec.whatwg.org/#es-ByteString
  76. */
  77. ByteString (V: unknown, prefix: string, argument: string): string
  78. /**
  79. * @see https://webidl.spec.whatwg.org/#es-USVString
  80. */
  81. USVString (V: unknown): string
  82. /**
  83. * @see https://webidl.spec.whatwg.org/#es-boolean
  84. */
  85. boolean (V: unknown): boolean
  86. /**
  87. * @see https://webidl.spec.whatwg.org/#es-any
  88. */
  89. any <Value>(V: Value): Value
  90. /**
  91. * @see https://webidl.spec.whatwg.org/#es-long-long
  92. */
  93. ['long long'] (V: unknown): number
  94. /**
  95. * @see https://webidl.spec.whatwg.org/#es-unsigned-long-long
  96. */
  97. ['unsigned long long'] (V: unknown): number
  98. /**
  99. * @see https://webidl.spec.whatwg.org/#es-unsigned-long
  100. */
  101. ['unsigned long'] (V: unknown): number
  102. /**
  103. * @see https://webidl.spec.whatwg.org/#es-unsigned-short
  104. */
  105. ['unsigned short'] (V: unknown, opts?: ConvertToIntOpts): number
  106. /**
  107. * @see https://webidl.spec.whatwg.org/#idl-ArrayBuffer
  108. */
  109. ArrayBuffer (V: unknown): ArrayBufferLike
  110. ArrayBuffer (V: unknown, opts: { allowShared: false }): ArrayBuffer
  111. /**
  112. * @see https://webidl.spec.whatwg.org/#es-buffer-source-types
  113. */
  114. TypedArray (
  115. V: unknown,
  116. TypedArray: NodeJS.TypedArray | ArrayBufferLike
  117. ): NodeJS.TypedArray | ArrayBufferLike
  118. TypedArray (
  119. V: unknown,
  120. TypedArray: NodeJS.TypedArray | ArrayBufferLike,
  121. opts?: { allowShared: false }
  122. ): NodeJS.TypedArray | ArrayBuffer
  123. /**
  124. * @see https://webidl.spec.whatwg.org/#es-buffer-source-types
  125. */
  126. DataView (V: unknown, opts?: { allowShared: boolean }): DataView
  127. /**
  128. * @see https://webidl.spec.whatwg.org/#BufferSource
  129. */
  130. BufferSource (
  131. V: unknown,
  132. opts?: { allowShared: boolean }
  133. ): NodeJS.TypedArray | ArrayBufferLike | DataView
  134. ['sequence<ByteString>']: SequenceConverter<string>
  135. ['sequence<sequence<ByteString>>']: SequenceConverter<string[]>
  136. ['record<ByteString, ByteString>']: RecordConverter<string, string>
  137. [Key: string]: (...args: any[]) => unknown
  138. }
  139. export interface Webidl {
  140. errors: WebidlErrors
  141. util: WebidlUtil
  142. converters: WebidlConverters
  143. /**
  144. * @description Performs a brand-check on {@param V} to ensure it is a
  145. * {@param cls} object.
  146. */
  147. brandCheck <Interface>(V: unknown, cls: Interface, opts?: { strict?: boolean }): asserts V is Interface
  148. /**
  149. * @see https://webidl.spec.whatwg.org/#es-sequence
  150. * @description Convert a value, V, to a WebIDL sequence type.
  151. */
  152. sequenceConverter <Type>(C: Converter<Type>): SequenceConverter<Type>
  153. illegalConstructor (): never
  154. /**
  155. * @see https://webidl.spec.whatwg.org/#es-to-record
  156. * @description Convert a value, V, to a WebIDL record type.
  157. */
  158. recordConverter <K extends string, V>(
  159. keyConverter: Converter<K>,
  160. valueConverter: Converter<V>
  161. ): RecordConverter<K, V>
  162. /**
  163. * Similar to {@link Webidl.brandCheck} but allows skipping the check if third party
  164. * interfaces are allowed.
  165. */
  166. interfaceConverter <Interface>(cls: Interface): (
  167. V: unknown,
  168. opts?: { strict: boolean }
  169. ) => asserts V is typeof cls
  170. // TODO(@KhafraDev): a type could likely be implemented that can infer the return type
  171. // from the converters given?
  172. /**
  173. * Converts a value, V, to a WebIDL dictionary types. Allows limiting which keys are
  174. * allowed, values allowed, optional and required keys. Auto converts the value to
  175. * a type given a converter.
  176. */
  177. dictionaryConverter (converters: {
  178. key: string,
  179. defaultValue?: () => unknown,
  180. required?: boolean,
  181. converter: (...args: unknown[]) => unknown,
  182. allowedValues?: unknown[]
  183. }[]): (V: unknown) => Record<string, unknown>
  184. /**
  185. * @see https://webidl.spec.whatwg.org/#idl-nullable-type
  186. * @description allows a type, V, to be null
  187. */
  188. nullableConverter <T>(
  189. converter: Converter<T>
  190. ): (V: unknown) => ReturnType<typeof converter> | null
  191. argumentLengthCheck (args: { length: number }, min: number, context: string): void
  192. }