webidl.d.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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) => 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. interface WebidlConverters {
  59. /**
  60. * @see https://webidl.spec.whatwg.org/#es-DOMString
  61. */
  62. DOMString (V: unknown, opts?: {
  63. legacyNullToEmptyString: boolean
  64. }): string
  65. /**
  66. * @see https://webidl.spec.whatwg.org/#es-ByteString
  67. */
  68. ByteString (V: unknown): string
  69. /**
  70. * @see https://webidl.spec.whatwg.org/#es-USVString
  71. */
  72. USVString (V: unknown): string
  73. /**
  74. * @see https://webidl.spec.whatwg.org/#es-boolean
  75. */
  76. boolean (V: unknown): boolean
  77. /**
  78. * @see https://webidl.spec.whatwg.org/#es-any
  79. */
  80. any <Value>(V: Value): Value
  81. /**
  82. * @see https://webidl.spec.whatwg.org/#es-long-long
  83. */
  84. ['long long'] (V: unknown): number
  85. /**
  86. * @see https://webidl.spec.whatwg.org/#es-unsigned-long-long
  87. */
  88. ['unsigned long long'] (V: unknown): number
  89. /**
  90. * @see https://webidl.spec.whatwg.org/#es-unsigned-long
  91. */
  92. ['unsigned long'] (V: unknown): number
  93. /**
  94. * @see https://webidl.spec.whatwg.org/#es-unsigned-short
  95. */
  96. ['unsigned short'] (V: unknown, opts?: ConvertToIntOpts): number
  97. /**
  98. * @see https://webidl.spec.whatwg.org/#idl-ArrayBuffer
  99. */
  100. ArrayBuffer (V: unknown): ArrayBufferLike
  101. ArrayBuffer (V: unknown, opts: { allowShared: false }): ArrayBuffer
  102. /**
  103. * @see https://webidl.spec.whatwg.org/#es-buffer-source-types
  104. */
  105. TypedArray (
  106. V: unknown,
  107. TypedArray: NodeJS.TypedArray | ArrayBufferLike
  108. ): NodeJS.TypedArray | ArrayBufferLike
  109. TypedArray (
  110. V: unknown,
  111. TypedArray: NodeJS.TypedArray | ArrayBufferLike,
  112. opts?: { allowShared: false }
  113. ): NodeJS.TypedArray | ArrayBuffer
  114. /**
  115. * @see https://webidl.spec.whatwg.org/#es-buffer-source-types
  116. */
  117. DataView (V: unknown, opts?: { allowShared: boolean }): DataView
  118. /**
  119. * @see https://webidl.spec.whatwg.org/#BufferSource
  120. */
  121. BufferSource (
  122. V: unknown,
  123. opts?: { allowShared: boolean }
  124. ): NodeJS.TypedArray | ArrayBufferLike | DataView
  125. ['sequence<ByteString>']: SequenceConverter<string>
  126. ['sequence<sequence<ByteString>>']: SequenceConverter<string[]>
  127. ['record<ByteString, ByteString>']: RecordConverter<string, string>
  128. [Key: string]: (...args: any[]) => unknown
  129. }
  130. export interface Webidl {
  131. errors: WebidlErrors
  132. util: WebidlUtil
  133. converters: WebidlConverters
  134. /**
  135. * @description Performs a brand-check on {@param V} to ensure it is a
  136. * {@param cls} object.
  137. */
  138. brandCheck <Interface>(V: unknown, cls: Interface, opts?: { strict?: boolean }): asserts V is Interface
  139. /**
  140. * @see https://webidl.spec.whatwg.org/#es-sequence
  141. * @description Convert a value, V, to a WebIDL sequence type.
  142. */
  143. sequenceConverter <Type>(C: Converter<Type>): SequenceConverter<Type>
  144. illegalConstructor (): never
  145. /**
  146. * @see https://webidl.spec.whatwg.org/#es-to-record
  147. * @description Convert a value, V, to a WebIDL record type.
  148. */
  149. recordConverter <K extends string, V>(
  150. keyConverter: Converter<K>,
  151. valueConverter: Converter<V>
  152. ): RecordConverter<K, V>
  153. /**
  154. * Similar to {@link Webidl.brandCheck} but allows skipping the check if third party
  155. * interfaces are allowed.
  156. */
  157. interfaceConverter <Interface>(cls: Interface): (
  158. V: unknown,
  159. opts?: { strict: boolean }
  160. ) => asserts V is typeof cls
  161. // TODO(@KhafraDev): a type could likely be implemented that can infer the return type
  162. // from the converters given?
  163. /**
  164. * Converts a value, V, to a WebIDL dictionary types. Allows limiting which keys are
  165. * allowed, values allowed, optional and required keys. Auto converts the value to
  166. * a type given a converter.
  167. */
  168. dictionaryConverter (converters: {
  169. key: string,
  170. defaultValue?: unknown,
  171. required?: boolean,
  172. converter: (...args: unknown[]) => unknown,
  173. allowedValues?: unknown[]
  174. }[]): (V: unknown) => Record<string, unknown>
  175. /**
  176. * @see https://webidl.spec.whatwg.org/#idl-nullable-type
  177. * @description allows a type, V, to be null
  178. */
  179. nullableConverter <T>(
  180. converter: Converter<T>
  181. ): (V: unknown) => ReturnType<typeof converter> | null
  182. argumentLengthCheck (args: { length: number }, min: number, context: {
  183. header: string
  184. message?: string
  185. }): void
  186. }