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