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