UNPKG

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