UNPKG

25.2 kBTypeScriptView Raw
1// Declare "static" methods in Error
2interface ErrorConstructor {
3 /** Create .stack property on a target object */
4 captureStackTrace(targetObject: object, constructorOpt?: Function): void;
5
6 /**
7 * Optional override for formatting stack traces
8 *
9 * @see https://v8.dev/docs/stack-trace-api#customizing-stack-traces
10 */
11 prepareStackTrace?: (err: Error, stackTraces: NodeJS.CallSite[]) => any;
12
13 stackTraceLimit: number;
14}
15
16// Node.js ESNEXT support
17interface String {
18 /** Removes whitespace from the left end of a string. */
19 trimLeft(): string;
20 /** Removes whitespace from the right end of a string. */
21 trimRight(): string;
22
23 /** Returns a copy with leading whitespace removed. */
24 trimStart(): string;
25 /** Returns a copy with trailing whitespace removed. */
26 trimEnd(): string;
27}
28
29interface ImportMeta {
30 url: string;
31}
32
33/*-----------------------------------------------*
34 * *
35 * GLOBAL *
36 * *
37 ------------------------------------------------*/
38
39// For backwards compability
40interface NodeRequire extends NodeJS.Require { }
41interface RequireResolve extends NodeJS.RequireResolve { }
42interface NodeModule extends NodeJS.Module { }
43
44declare var process: NodeJS.Process;
45declare var console: Console;
46
47declare var __filename: string;
48declare var __dirname: string;
49
50declare function setTimeout(callback: (...args: any[]) => void, ms?: number, ...args: any[]): NodeJS.Timeout;
51declare namespace setTimeout {
52 function __promisify__(ms: number): Promise<void>;
53 function __promisify__<T>(ms: number, value: T): Promise<T>;
54}
55declare function clearTimeout(timeoutId: NodeJS.Timeout): void;
56declare function setInterval(callback: (...args: any[]) => void, ms?: number, ...args: any[]): NodeJS.Timeout;
57declare function clearInterval(intervalId: NodeJS.Timeout): void;
58declare function setImmediate(callback: (...args: any[]) => void, ...args: any[]): NodeJS.Immediate;
59declare namespace setImmediate {
60 function __promisify__(): Promise<void>;
61 function __promisify__<T>(value: T): Promise<T>;
62}
63declare function clearImmediate(immediateId: NodeJS.Immediate): void;
64
65declare function queueMicrotask(callback: () => void): void;
66
67declare var require: NodeRequire;
68declare var module: NodeModule;
69
70// Same as module.exports
71declare var exports: any;
72
73// Buffer class
74type BufferEncoding = "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "base64url" | "latin1" | "binary" | "hex";
75
76type WithImplicitCoercion<T> = T | { valueOf(): T };
77
78/**
79 * Raw data is stored in instances of the Buffer class.
80 * A Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap. A Buffer cannot be resized.
81 * Valid string encodings: 'ascii'|'utf8'|'utf16le'|'ucs2'(alias of 'utf16le')|'base64'|'binary'(deprecated)|'hex'
82 */
83declare class Buffer extends Uint8Array {
84 /**
85 * Allocates a new buffer containing the given {str}.
86 *
87 * @param str String to store in buffer.
88 * @param encoding encoding to use, optional. Default is 'utf8'
89 * @deprecated since v10.0.0 - Use `Buffer.from(string[, encoding])` instead.
90 */
91 constructor(str: string, encoding?: BufferEncoding);
92 /**
93 * Allocates a new buffer of {size} octets.
94 *
95 * @param size count of octets to allocate.
96 * @deprecated since v10.0.0 - Use `Buffer.alloc()` instead (also see `Buffer.allocUnsafe()`).
97 */
98 constructor(size: number);
99 /**
100 * Allocates a new buffer containing the given {array} of octets.
101 *
102 * @param array The octets to store.
103 * @deprecated since v10.0.0 - Use `Buffer.from(array)` instead.
104 */
105 constructor(array: Uint8Array);
106 /**
107 * Produces a Buffer backed by the same allocated memory as
108 * the given {ArrayBuffer}/{SharedArrayBuffer}.
109 *
110 *
111 * @param arrayBuffer The ArrayBuffer with which to share memory.
112 * @deprecated since v10.0.0 - Use `Buffer.from(arrayBuffer[, byteOffset[, length]])` instead.
113 */
114 constructor(arrayBuffer: ArrayBuffer | SharedArrayBuffer);
115 /**
116 * Allocates a new buffer containing the given {array} of octets.
117 *
118 * @param array The octets to store.
119 * @deprecated since v10.0.0 - Use `Buffer.from(array)` instead.
120 */
121 constructor(array: ReadonlyArray<any>);
122 /**
123 * Copies the passed {buffer} data onto a new {Buffer} instance.
124 *
125 * @param buffer The buffer to copy.
126 * @deprecated since v10.0.0 - Use `Buffer.from(buffer)` instead.
127 */
128 constructor(buffer: Buffer);
129 /**
130 * When passed a reference to the .buffer property of a TypedArray instance,
131 * the newly created Buffer will share the same allocated memory as the TypedArray.
132 * The optional {byteOffset} and {length} arguments specify a memory range
133 * within the {arrayBuffer} that will be shared by the Buffer.
134 *
135 * @param arrayBuffer The .buffer property of any TypedArray or a new ArrayBuffer()
136 */
137 static from(arrayBuffer: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>, byteOffset?: number, length?: number): Buffer;
138 /**
139 * Creates a new Buffer using the passed {data}
140 * @param data data to create a new Buffer
141 */
142 static from(data: Uint8Array | ReadonlyArray<number>): Buffer;
143 static from(data: WithImplicitCoercion<Uint8Array | ReadonlyArray<number> | string>): Buffer;
144 /**
145 * Creates a new Buffer containing the given JavaScript string {str}.
146 * If provided, the {encoding} parameter identifies the character encoding.
147 * If not provided, {encoding} defaults to 'utf8'.
148 */
149 static from(str: WithImplicitCoercion<string> | { [Symbol.toPrimitive](hint: 'string'): string }, encoding?: BufferEncoding): Buffer;
150 /**
151 * Creates a new Buffer using the passed {data}
152 * @param values to create a new Buffer
153 */
154 static of(...items: number[]): Buffer;
155 /**
156 * Returns true if {obj} is a Buffer
157 *
158 * @param obj object to test.
159 */
160 static isBuffer(obj: any): obj is Buffer;
161 /**
162 * Returns true if {encoding} is a valid encoding argument.
163 * Valid string encodings in Node 0.12: 'ascii'|'utf8'|'utf16le'|'ucs2'(alias of 'utf16le')|'base64'|'binary'(deprecated)|'hex'
164 *
165 * @param encoding string to test.
166 */
167 static isEncoding(encoding: string): encoding is BufferEncoding;
168 /**
169 * Gives the actual byte length of a string. encoding defaults to 'utf8'.
170 * This is not the same as String.prototype.length since that returns the number of characters in a string.
171 *
172 * @param string string to test.
173 * @param encoding encoding used to evaluate (defaults to 'utf8')
174 */
175 static byteLength(
176 string: string | NodeJS.ArrayBufferView | ArrayBuffer | SharedArrayBuffer,
177 encoding?: BufferEncoding
178 ): number;
179 /**
180 * Returns a buffer which is the result of concatenating all the buffers in the list together.
181 *
182 * If the list has no items, or if the totalLength is 0, then it returns a zero-length buffer.
183 * If the list has exactly one item, then the first item of the list is returned.
184 * If the list has more than one item, then a new Buffer is created.
185 *
186 * @param list An array of Buffer objects to concatenate
187 * @param totalLength Total length of the buffers when concatenated.
188 * If totalLength is not provided, it is read from the buffers in the list. However, this adds an additional loop to the function, so it is faster to provide the length explicitly.
189 */
190 static concat(list: ReadonlyArray<Uint8Array>, totalLength?: number): Buffer;
191 /**
192 * The same as buf1.compare(buf2).
193 */
194 static compare(buf1: Uint8Array, buf2: Uint8Array): number;
195 /**
196 * Allocates a new buffer of {size} octets.
197 *
198 * @param size count of octets to allocate.
199 * @param fill if specified, buffer will be initialized by calling buf.fill(fill).
200 * If parameter is omitted, buffer will be filled with zeros.
201 * @param encoding encoding used for call to buf.fill while initalizing
202 */
203 static alloc(size: number, fill?: string | Buffer | number, encoding?: BufferEncoding): Buffer;
204 /**
205 * Allocates a new buffer of {size} octets, leaving memory not initialized, so the contents
206 * of the newly created Buffer are unknown and may contain sensitive data.
207 *
208 * @param size count of octets to allocate
209 */
210 static allocUnsafe(size: number): Buffer;
211 /**
212 * Allocates a new non-pooled buffer of {size} octets, leaving memory not initialized, so the contents
213 * of the newly created Buffer are unknown and may contain sensitive data.
214 *
215 * @param size count of octets to allocate
216 */
217 static allocUnsafeSlow(size: number): Buffer;
218 /**
219 * This is the number of bytes used to determine the size of pre-allocated, internal Buffer instances used for pooling. This value may be modified.
220 */
221 static poolSize: number;
222
223 write(string: string, encoding?: BufferEncoding): number;
224 write(string: string, offset: number, encoding?: BufferEncoding): number;
225 write(string: string, offset: number, length: number, encoding?: BufferEncoding): number;
226 toString(encoding?: BufferEncoding, start?: number, end?: number): string;
227 toJSON(): { type: 'Buffer'; data: number[] };
228 equals(otherBuffer: Uint8Array): boolean;
229 compare(
230 otherBuffer: Uint8Array,
231 targetStart?: number,
232 targetEnd?: number,
233 sourceStart?: number,
234 sourceEnd?: number
235 ): number;
236 copy(targetBuffer: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number): number;
237 /**
238 * Returns a new `Buffer` that references **the same memory as the original**, but offset and cropped by the start and end indices.
239 *
240 * This method is incompatible with `Uint8Array#slice()`, which returns a copy of the original memory.
241 *
242 * @param begin Where the new `Buffer` will start. Default: `0`.
243 * @param end Where the new `Buffer` will end (not inclusive). Default: `buf.length`.
244 */
245 slice(begin?: number, end?: number): Buffer;
246 /**
247 * Returns a new `Buffer` that references **the same memory as the original**, but offset and cropped by the start and end indices.
248 *
249 * This method is compatible with `Uint8Array#subarray()`.
250 *
251 * @param begin Where the new `Buffer` will start. Default: `0`.
252 * @param end Where the new `Buffer` will end (not inclusive). Default: `buf.length`.
253 */
254 subarray(begin?: number, end?: number): Buffer;
255 writeBigInt64BE(value: bigint, offset?: number): number;
256 writeBigInt64LE(value: bigint, offset?: number): number;
257 writeBigUInt64BE(value: bigint, offset?: number): number;
258 writeBigUInt64LE(value: bigint, offset?: number): number;
259 writeUIntLE(value: number, offset: number, byteLength: number): number;
260 writeUIntBE(value: number, offset: number, byteLength: number): number;
261 writeIntLE(value: number, offset: number, byteLength: number): number;
262 writeIntBE(value: number, offset: number, byteLength: number): number;
263 readBigUInt64BE(offset?: number): bigint;
264 readBigUInt64LE(offset?: number): bigint;
265 readBigInt64BE(offset?: number): bigint;
266 readBigInt64LE(offset?: number): bigint;
267 readUIntLE(offset: number, byteLength: number): number;
268 readUIntBE(offset: number, byteLength: number): number;
269 readIntLE(offset: number, byteLength: number): number;
270 readIntBE(offset: number, byteLength: number): number;
271 readUInt8(offset?: number): number;
272 readUInt16LE(offset?: number): number;
273 readUInt16BE(offset?: number): number;
274 readUInt32LE(offset?: number): number;
275 readUInt32BE(offset?: number): number;
276 readInt8(offset?: number): number;
277 readInt16LE(offset?: number): number;
278 readInt16BE(offset?: number): number;
279 readInt32LE(offset?: number): number;
280 readInt32BE(offset?: number): number;
281 readFloatLE(offset?: number): number;
282 readFloatBE(offset?: number): number;
283 readDoubleLE(offset?: number): number;
284 readDoubleBE(offset?: number): number;
285 reverse(): this;
286 swap16(): Buffer;
287 swap32(): Buffer;
288 swap64(): Buffer;
289 writeUInt8(value: number, offset?: number): number;
290 writeUInt16LE(value: number, offset?: number): number;
291 writeUInt16BE(value: number, offset?: number): number;
292 writeUInt32LE(value: number, offset?: number): number;
293 writeUInt32BE(value: number, offset?: number): number;
294 writeInt8(value: number, offset?: number): number;
295 writeInt16LE(value: number, offset?: number): number;
296 writeInt16BE(value: number, offset?: number): number;
297 writeInt32LE(value: number, offset?: number): number;
298 writeInt32BE(value: number, offset?: number): number;
299 writeFloatLE(value: number, offset?: number): number;
300 writeFloatBE(value: number, offset?: number): number;
301 writeDoubleLE(value: number, offset?: number): number;
302 writeDoubleBE(value: number, offset?: number): number;
303
304 fill(value: string | Uint8Array | number, offset?: number, end?: number, encoding?: BufferEncoding): this;
305
306 indexOf(value: string | number | Uint8Array, byteOffset?: number, encoding?: BufferEncoding): number;
307 lastIndexOf(value: string | number | Uint8Array, byteOffset?: number, encoding?: BufferEncoding): number;
308 entries(): IterableIterator<[number, number]>;
309 includes(value: string | number | Buffer, byteOffset?: number, encoding?: BufferEncoding): boolean;
310 keys(): IterableIterator<number>;
311 values(): IterableIterator<number>;
312}
313
314//#region borrowed
315// from https://github.com/microsoft/TypeScript/blob/38da7c600c83e7b31193a62495239a0fe478cb67/lib/lib.webworker.d.ts#L633 until moved to separate lib
316/** A controller object that allows you to abort one or more DOM requests as and when desired. */
317interface AbortController {
318 /**
319 * Returns the AbortSignal object associated with this object.
320 */
321
322 readonly signal: AbortSignal;
323 /**
324 * Invoking this method will set this object's AbortSignal's aborted flag and signal to any observers that the associated activity is to be aborted.
325 */
326 abort(): void;
327}
328
329/** A signal object that allows you to communicate with a DOM request (such as a Fetch) and abort it if required via an AbortController object. */
330interface AbortSignal {
331 /**
332 * Returns true if this AbortSignal's AbortController has signaled to abort, and false otherwise.
333 */
334 readonly aborted: boolean;
335
336 /**
337 * Invoking this method will set this object's AbortSignal's aborted flag and signal to any observers that the associated activity is to be aborted.
338 */
339 abort(): void;
340}
341
342declare var AbortController: {
343 prototype: AbortController;
344 new(): AbortController;
345};
346
347declare var AbortSignal: {
348 prototype: AbortSignal;
349 new(): AbortSignal;
350};
351//#endregion borrowed
352
353/*----------------------------------------------*
354* *
355* GLOBAL INTERFACES *
356* *
357*-----------------------------------------------*/
358declare namespace NodeJS {
359 interface InspectOptions {
360 /**
361 * If set to `true`, getters are going to be
362 * inspected as well. If set to `'get'` only getters without setter are going
363 * to be inspected. If set to `'set'` only getters having a corresponding
364 * setter are going to be inspected. This might cause side effects depending on
365 * the getter function.
366 * @default `false`
367 */
368 getters?: 'get' | 'set' | boolean;
369 showHidden?: boolean;
370 /**
371 * @default 2
372 */
373 depth?: number | null;
374 colors?: boolean;
375 customInspect?: boolean;
376 showProxy?: boolean;
377 maxArrayLength?: number | null;
378 /**
379 * Specifies the maximum number of characters to
380 * include when formatting. Set to `null` or `Infinity` to show all elements.
381 * Set to `0` or negative to show no characters.
382 * @default 10000
383 */
384 maxStringLength?: number | null;
385 breakLength?: number;
386 /**
387 * Setting this to `false` causes each object key
388 * to be displayed on a new line. It will also add new lines to text that is
389 * longer than `breakLength`. If set to a number, the most `n` inner elements
390 * are united on a single line as long as all properties fit into
391 * `breakLength`. Short array elements are also grouped together. Note that no
392 * text will be reduced below 16 characters, no matter the `breakLength` size.
393 * For more information, see the example below.
394 * @default `true`
395 */
396 compact?: boolean | number;
397 sorted?: boolean | ((a: string, b: string) => number);
398 }
399
400 interface CallSite {
401 /**
402 * Value of "this"
403 */
404 getThis(): any;
405
406 /**
407 * Type of "this" as a string.
408 * This is the name of the function stored in the constructor field of
409 * "this", if available. Otherwise the object's [[Class]] internal
410 * property.
411 */
412 getTypeName(): string | null;
413
414 /**
415 * Current function
416 */
417 getFunction(): Function | undefined;
418
419 /**
420 * Name of the current function, typically its name property.
421 * If a name property is not available an attempt will be made to try
422 * to infer a name from the function's context.
423 */
424 getFunctionName(): string | null;
425
426 /**
427 * Name of the property [of "this" or one of its prototypes] that holds
428 * the current function
429 */
430 getMethodName(): string | null;
431
432 /**
433 * Name of the script [if this function was defined in a script]
434 */
435 getFileName(): string | null;
436
437 /**
438 * Current line number [if this function was defined in a script]
439 */
440 getLineNumber(): number | null;
441
442 /**
443 * Current column number [if this function was defined in a script]
444 */
445 getColumnNumber(): number | null;
446
447 /**
448 * A call site object representing the location where eval was called
449 * [if this function was created using a call to eval]
450 */
451 getEvalOrigin(): string | undefined;
452
453 /**
454 * Is this a toplevel invocation, that is, is "this" the global object?
455 */
456 isToplevel(): boolean;
457
458 /**
459 * Does this call take place in code defined by a call to eval?
460 */
461 isEval(): boolean;
462
463 /**
464 * Is this call in native V8 code?
465 */
466 isNative(): boolean;
467
468 /**
469 * Is this a constructor call?
470 */
471 isConstructor(): boolean;
472 }
473
474 interface ErrnoException extends Error {
475 errno?: number;
476 code?: string;
477 path?: string;
478 syscall?: string;
479 stack?: string;
480 }
481
482 interface ReadableStream extends EventEmitter {
483 readable: boolean;
484 read(size?: number): string | Buffer;
485 setEncoding(encoding: BufferEncoding): this;
486 pause(): this;
487 resume(): this;
488 isPaused(): boolean;
489 pipe<T extends WritableStream>(destination: T, options?: { end?: boolean; }): T;
490 unpipe(destination?: WritableStream): this;
491 unshift(chunk: string | Uint8Array, encoding?: BufferEncoding): void;
492 wrap(oldStream: ReadableStream): this;
493 [Symbol.asyncIterator](): AsyncIterableIterator<string | Buffer>;
494 }
495
496 interface WritableStream extends EventEmitter {
497 writable: boolean;
498 write(buffer: Uint8Array | string, cb?: (err?: Error | null) => void): boolean;
499 write(str: string, encoding?: BufferEncoding, cb?: (err?: Error | null) => void): boolean;
500 end(cb?: () => void): void;
501 end(data: string | Uint8Array, cb?: () => void): void;
502 end(str: string, encoding?: BufferEncoding, cb?: () => void): void;
503 }
504
505 interface ReadWriteStream extends ReadableStream, WritableStream { }
506
507 interface Global {
508 AbortController: typeof AbortController;
509 AbortSignal: typeof AbortSignal;
510 Array: typeof Array;
511 ArrayBuffer: typeof ArrayBuffer;
512 Boolean: typeof Boolean;
513 Buffer: typeof Buffer;
514 DataView: typeof DataView;
515 Date: typeof Date;
516 Error: typeof Error;
517 EvalError: typeof EvalError;
518 Float32Array: typeof Float32Array;
519 Float64Array: typeof Float64Array;
520 Function: typeof Function;
521 Infinity: typeof Infinity;
522 Int16Array: typeof Int16Array;
523 Int32Array: typeof Int32Array;
524 Int8Array: typeof Int8Array;
525 Intl: typeof Intl;
526 JSON: typeof JSON;
527 Map: MapConstructor;
528 Math: typeof Math;
529 NaN: typeof NaN;
530 Number: typeof Number;
531 Object: typeof Object;
532 Promise: typeof Promise;
533 RangeError: typeof RangeError;
534 ReferenceError: typeof ReferenceError;
535 RegExp: typeof RegExp;
536 Set: SetConstructor;
537 String: typeof String;
538 Symbol: Function;
539 SyntaxError: typeof SyntaxError;
540 TypeError: typeof TypeError;
541 URIError: typeof URIError;
542 Uint16Array: typeof Uint16Array;
543 Uint32Array: typeof Uint32Array;
544 Uint8Array: typeof Uint8Array;
545 Uint8ClampedArray: typeof Uint8ClampedArray;
546 WeakMap: WeakMapConstructor;
547 WeakSet: WeakSetConstructor;
548 clearImmediate: (immediateId: Immediate) => void;
549 clearInterval: (intervalId: Timeout) => void;
550 clearTimeout: (timeoutId: Timeout) => void;
551 decodeURI: typeof decodeURI;
552 decodeURIComponent: typeof decodeURIComponent;
553 encodeURI: typeof encodeURI;
554 encodeURIComponent: typeof encodeURIComponent;
555 escape: (str: string) => string;
556 eval: typeof eval;
557 global: Global;
558 isFinite: typeof isFinite;
559 isNaN: typeof isNaN;
560 parseFloat: typeof parseFloat;
561 parseInt: typeof parseInt;
562 setImmediate: (callback: (...args: any[]) => void, ...args: any[]) => Immediate;
563 setInterval: (callback: (...args: any[]) => void, ms?: number, ...args: any[]) => Timeout;
564 setTimeout: (callback: (...args: any[]) => void, ms?: number, ...args: any[]) => Timeout;
565 queueMicrotask: typeof queueMicrotask;
566 undefined: typeof undefined;
567 unescape: (str: string) => string;
568 gc: () => void;
569 v8debug?: any;
570 }
571
572 interface RefCounted {
573 ref(): this;
574 unref(): this;
575 }
576
577 // compatibility with older typings
578 interface Timer extends RefCounted {
579 hasRef(): boolean;
580 refresh(): this;
581 [Symbol.toPrimitive](): number;
582 }
583
584 interface Immediate extends RefCounted {
585 hasRef(): boolean;
586 _onImmediate: Function; // to distinguish it from the Timeout class
587 }
588
589 interface Timeout extends Timer {
590 hasRef(): boolean;
591 refresh(): this;
592 [Symbol.toPrimitive](): number;
593 }
594
595 type TypedArray =
596 | Uint8Array
597 | Uint8ClampedArray
598 | Uint16Array
599 | Uint32Array
600 | Int8Array
601 | Int16Array
602 | Int32Array
603 | BigUint64Array
604 | BigInt64Array
605 | Float32Array
606 | Float64Array;
607 type ArrayBufferView = TypedArray | DataView;
608
609 interface Require {
610 (id: string): any;
611 resolve: RequireResolve;
612 cache: Dict<NodeModule>;
613 /**
614 * @deprecated
615 */
616 extensions: RequireExtensions;
617 main: Module | undefined;
618 }
619
620 interface RequireResolve {
621 (id: string, options?: { paths?: string[]; }): string;
622 paths(request: string): string[] | null;
623 }
624
625 interface RequireExtensions extends Dict<(m: Module, filename: string) => any> {
626 '.js': (m: Module, filename: string) => any;
627 '.json': (m: Module, filename: string) => any;
628 '.node': (m: Module, filename: string) => any;
629 }
630 interface Module {
631 /**
632 * `true` if the module is running during the Node.js preload
633 */
634 isPreloading: boolean;
635 exports: any;
636 require: Require;
637 id: string;
638 filename: string;
639 loaded: boolean;
640 /** @deprecated since 14.6.0 Please use `require.main` and `module.children` instead. */
641 parent: Module | null | undefined;
642 children: Module[];
643 /**
644 * @since 11.14.0
645 *
646 * The directory name of the module. This is usually the same as the path.dirname() of the module.id.
647 */
648 path: string;
649 paths: string[];
650 }
651
652 interface Dict<T> {
653 [key: string]: T | undefined;
654 }
655
656 interface ReadOnlyDict<T> {
657 readonly [key: string]: T | undefined;
658 }
659}