UNPKG

25.1 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
337declare var AbortController: {
338 prototype: AbortController;
339 new(): AbortController;
340};
341
342declare var AbortSignal: {
343 prototype: AbortSignal;
344 new(): AbortSignal;
345 // TODO: Add abort() static
346};
347//#endregion borrowed
348
349/*----------------------------------------------*
350* *
351* GLOBAL INTERFACES *
352* *
353*-----------------------------------------------*/
354declare namespace NodeJS {
355 interface InspectOptions {
356 /**
357 * If set to `true`, getters are going to be
358 * inspected as well. If set to `'get'` only getters without setter are going
359 * to be inspected. If set to `'set'` only getters having a corresponding
360 * setter are going to be inspected. This might cause side effects depending on
361 * the getter function.
362 * @default `false`
363 */
364 getters?: 'get' | 'set' | boolean;
365 showHidden?: boolean;
366 /**
367 * @default 2
368 */
369 depth?: number | null;
370 colors?: boolean;
371 customInspect?: boolean;
372 showProxy?: boolean;
373 maxArrayLength?: number | null;
374 /**
375 * Specifies the maximum number of characters to
376 * include when formatting. Set to `null` or `Infinity` to show all elements.
377 * Set to `0` or negative to show no characters.
378 * @default 10000
379 */
380 maxStringLength?: number | null;
381 breakLength?: number;
382 /**
383 * Setting this to `false` causes each object key
384 * to be displayed on a new line. It will also add new lines to text that is
385 * longer than `breakLength`. If set to a number, the most `n` inner elements
386 * are united on a single line as long as all properties fit into
387 * `breakLength`. Short array elements are also grouped together. Note that no
388 * text will be reduced below 16 characters, no matter the `breakLength` size.
389 * For more information, see the example below.
390 * @default `true`
391 */
392 compact?: boolean | number;
393 sorted?: boolean | ((a: string, b: string) => number);
394 }
395
396 interface CallSite {
397 /**
398 * Value of "this"
399 */
400 getThis(): any;
401
402 /**
403 * Type of "this" as a string.
404 * This is the name of the function stored in the constructor field of
405 * "this", if available. Otherwise the object's [[Class]] internal
406 * property.
407 */
408 getTypeName(): string | null;
409
410 /**
411 * Current function
412 */
413 getFunction(): Function | undefined;
414
415 /**
416 * Name of the current function, typically its name property.
417 * If a name property is not available an attempt will be made to try
418 * to infer a name from the function's context.
419 */
420 getFunctionName(): string | null;
421
422 /**
423 * Name of the property [of "this" or one of its prototypes] that holds
424 * the current function
425 */
426 getMethodName(): string | null;
427
428 /**
429 * Name of the script [if this function was defined in a script]
430 */
431 getFileName(): string | null;
432
433 /**
434 * Current line number [if this function was defined in a script]
435 */
436 getLineNumber(): number | null;
437
438 /**
439 * Current column number [if this function was defined in a script]
440 */
441 getColumnNumber(): number | null;
442
443 /**
444 * A call site object representing the location where eval was called
445 * [if this function was created using a call to eval]
446 */
447 getEvalOrigin(): string | undefined;
448
449 /**
450 * Is this a toplevel invocation, that is, is "this" the global object?
451 */
452 isToplevel(): boolean;
453
454 /**
455 * Does this call take place in code defined by a call to eval?
456 */
457 isEval(): boolean;
458
459 /**
460 * Is this call in native V8 code?
461 */
462 isNative(): boolean;
463
464 /**
465 * Is this a constructor call?
466 */
467 isConstructor(): boolean;
468 }
469
470 interface ErrnoException extends Error {
471 errno?: number;
472 code?: string;
473 path?: string;
474 syscall?: string;
475 stack?: string;
476 }
477
478 interface ReadableStream extends EventEmitter {
479 readable: boolean;
480 read(size?: number): string | Buffer;
481 setEncoding(encoding: BufferEncoding): this;
482 pause(): this;
483 resume(): this;
484 isPaused(): boolean;
485 pipe<T extends WritableStream>(destination: T, options?: { end?: boolean; }): T;
486 unpipe(destination?: WritableStream): this;
487 unshift(chunk: string | Uint8Array, encoding?: BufferEncoding): void;
488 wrap(oldStream: ReadableStream): this;
489 [Symbol.asyncIterator](): AsyncIterableIterator<string | Buffer>;
490 }
491
492 interface WritableStream extends EventEmitter {
493 writable: boolean;
494 write(buffer: Uint8Array | string, cb?: (err?: Error | null) => void): boolean;
495 write(str: string, encoding?: BufferEncoding, cb?: (err?: Error | null) => void): boolean;
496 end(cb?: () => void): void;
497 end(data: string | Uint8Array, cb?: () => void): void;
498 end(str: string, encoding?: BufferEncoding, cb?: () => void): void;
499 }
500
501 interface ReadWriteStream extends ReadableStream, WritableStream { }
502
503 interface Global {
504 AbortController: typeof AbortController;
505 AbortSignal: typeof AbortSignal;
506 Array: typeof Array;
507 ArrayBuffer: typeof ArrayBuffer;
508 Boolean: typeof Boolean;
509 Buffer: typeof Buffer;
510 DataView: typeof DataView;
511 Date: typeof Date;
512 Error: typeof Error;
513 EvalError: typeof EvalError;
514 Float32Array: typeof Float32Array;
515 Float64Array: typeof Float64Array;
516 Function: typeof Function;
517 Infinity: typeof Infinity;
518 Int16Array: typeof Int16Array;
519 Int32Array: typeof Int32Array;
520 Int8Array: typeof Int8Array;
521 Intl: typeof Intl;
522 JSON: typeof JSON;
523 Map: MapConstructor;
524 Math: typeof Math;
525 NaN: typeof NaN;
526 Number: typeof Number;
527 Object: typeof Object;
528 Promise: typeof Promise;
529 RangeError: typeof RangeError;
530 ReferenceError: typeof ReferenceError;
531 RegExp: typeof RegExp;
532 Set: SetConstructor;
533 String: typeof String;
534 Symbol: Function;
535 SyntaxError: typeof SyntaxError;
536 TypeError: typeof TypeError;
537 URIError: typeof URIError;
538 Uint16Array: typeof Uint16Array;
539 Uint32Array: typeof Uint32Array;
540 Uint8Array: typeof Uint8Array;
541 Uint8ClampedArray: typeof Uint8ClampedArray;
542 WeakMap: WeakMapConstructor;
543 WeakSet: WeakSetConstructor;
544 clearImmediate: (immediateId: Immediate) => void;
545 clearInterval: (intervalId: Timeout) => void;
546 clearTimeout: (timeoutId: Timeout) => void;
547 decodeURI: typeof decodeURI;
548 decodeURIComponent: typeof decodeURIComponent;
549 encodeURI: typeof encodeURI;
550 encodeURIComponent: typeof encodeURIComponent;
551 escape: (str: string) => string;
552 eval: typeof eval;
553 global: Global;
554 isFinite: typeof isFinite;
555 isNaN: typeof isNaN;
556 parseFloat: typeof parseFloat;
557 parseInt: typeof parseInt;
558 setImmediate: (callback: (...args: any[]) => void, ...args: any[]) => Immediate;
559 setInterval: (callback: (...args: any[]) => void, ms?: number, ...args: any[]) => Timeout;
560 setTimeout: (callback: (...args: any[]) => void, ms?: number, ...args: any[]) => Timeout;
561 queueMicrotask: typeof queueMicrotask;
562 undefined: typeof undefined;
563 unescape: (str: string) => string;
564 gc: () => void;
565 v8debug?: any;
566 }
567
568 interface RefCounted {
569 ref(): this;
570 unref(): this;
571 }
572
573 // compatibility with older typings
574 interface Timer extends RefCounted {
575 hasRef(): boolean;
576 refresh(): this;
577 [Symbol.toPrimitive](): number;
578 }
579
580 interface Immediate extends RefCounted {
581 hasRef(): boolean;
582 _onImmediate: Function; // to distinguish it from the Timeout class
583 }
584
585 interface Timeout extends Timer {
586 hasRef(): boolean;
587 refresh(): this;
588 [Symbol.toPrimitive](): number;
589 }
590
591 type TypedArray =
592 | Uint8Array
593 | Uint8ClampedArray
594 | Uint16Array
595 | Uint32Array
596 | Int8Array
597 | Int16Array
598 | Int32Array
599 | BigUint64Array
600 | BigInt64Array
601 | Float32Array
602 | Float64Array;
603 type ArrayBufferView = TypedArray | DataView;
604
605 interface Require {
606 (id: string): any;
607 resolve: RequireResolve;
608 cache: Dict<NodeModule>;
609 /**
610 * @deprecated
611 */
612 extensions: RequireExtensions;
613 main: Module | undefined;
614 }
615
616 interface RequireResolve {
617 (id: string, options?: { paths?: string[]; }): string;
618 paths(request: string): string[] | null;
619 }
620
621 interface RequireExtensions extends Dict<(m: Module, filename: string) => any> {
622 '.js': (m: Module, filename: string) => any;
623 '.json': (m: Module, filename: string) => any;
624 '.node': (m: Module, filename: string) => any;
625 }
626 interface Module {
627 /**
628 * `true` if the module is running during the Node.js preload
629 */
630 isPreloading: boolean;
631 exports: any;
632 require: Require;
633 id: string;
634 filename: string;
635 loaded: boolean;
636 /** @deprecated since 14.6.0 Please use `require.main` and `module.children` instead. */
637 parent: Module | null | undefined;
638 children: Module[];
639 /**
640 * @since 11.14.0
641 *
642 * The directory name of the module. This is usually the same as the path.dirname() of the module.id.
643 */
644 path: string;
645 paths: string[];
646 }
647
648 interface Dict<T> {
649 [key: string]: T | undefined;
650 }
651
652 interface ReadOnlyDict<T> {
653 readonly [key: string]: T | undefined;
654 }
655}