diff --git a/types/node/child_process.d.ts b/types/node/child_process.d.ts index cff058d163..c220e1966d 100644 --- a/types/node/child_process.d.ts +++ b/types/node/child_process.d.ts @@ -404,7 +404,7 @@ declare module "child_process" { interface SpawnSyncOptions extends CommonOptions { argv0?: string; // Not specified in the docs - input?: string | NodeJS.TypedArray | DataView; + input?: string | NodeJS.ArrayBufferView; stdio?: StdioOptions; killSignal?: string | number; maxBuffer?: number; @@ -455,7 +455,7 @@ declare module "child_process" { function execSync(command: string, options?: ExecSyncOptions): Buffer; interface ExecFileSyncOptions extends CommonOptions { - input?: string | NodeJS.TypedArray | DataView; + input?: string | NodeJS.ArrayBufferView; stdio?: StdioOptions; killSignal?: string | number; maxBuffer?: number; diff --git a/types/node/crypto.d.ts b/types/node/crypto.d.ts index 5c2dc09178..a048b2064c 100644 --- a/types/node/crypto.d.ts +++ b/types/node/crypto.d.ts @@ -4,7 +4,7 @@ declare module "crypto" { interface Certificate { exportChallenge(spkac: BinaryLike): Buffer; exportPublicKey(spkac: BinaryLike): Buffer; - verifySpkac(spkac: Binary): boolean; + verifySpkac(spkac: NodeJS.ArrayBufferView): boolean; } const Certificate: { new(): Certificate; @@ -106,10 +106,18 @@ declare module "crypto" { const defaultCipherList: string; } + interface HashOptions extends stream.TransformOptions { + /** + * For XOF hash functions such as `shake256`, the + * outputLength option can be used to specify the desired output length in bytes. + */ + outputLength?: number; + } + /** @deprecated since v10.0.0 */ const fips: boolean; - function createHash(algorithm: string, options?: stream.TransformOptions): Hash; + function createHash(algorithm: string, options?: HashOptions): Hash; function createHmac(algorithm: string, key: BinaryLike, options?: stream.TransformOptions): Hmac; type Utf8AsciiLatin1Encoding = "utf8" | "ascii" | "latin1"; @@ -159,8 +167,7 @@ declare module "crypto" { type CipherCCMTypes = 'aes-128-ccm' | 'aes-192-ccm' | 'aes-256-ccm'; type CipherGCMTypes = 'aes-128-gcm' | 'aes-192-gcm' | 'aes-256-gcm'; - type Binary = NodeJS.TypedArray | DataView; - type BinaryLike = string | Binary; + type BinaryLike = string | NodeJS.ArrayBufferView; type CipherKey = BinaryLike | KeyObject; @@ -197,7 +204,7 @@ declare module "crypto" { private constructor(); update(data: BinaryLike): Buffer; update(data: string, input_encoding: Utf8AsciiBinaryEncoding): Buffer; - update(data: Binary, input_encoding: undefined, output_encoding: HexBase64BinaryEncoding): string; + update(data: NodeJS.ArrayBufferView, input_encoding: undefined, output_encoding: HexBase64BinaryEncoding): string; update(data: string, input_encoding: Utf8AsciiBinaryEncoding | undefined, output_encoding: HexBase64BinaryEncoding): string; final(): Buffer; final(output_encoding: string): string; @@ -236,23 +243,23 @@ declare module "crypto" { class Decipher extends stream.Transform { private constructor(); - update(data: Binary): Buffer; + update(data: NodeJS.ArrayBufferView): Buffer; update(data: string, input_encoding: HexBase64BinaryEncoding): Buffer; - update(data: Binary, input_encoding: undefined, output_encoding: Utf8AsciiBinaryEncoding): string; + update(data: NodeJS.ArrayBufferView, input_encoding: undefined, output_encoding: Utf8AsciiBinaryEncoding): string; update(data: string, input_encoding: HexBase64BinaryEncoding | undefined, output_encoding: Utf8AsciiBinaryEncoding): string; final(): Buffer; final(output_encoding: string): string; setAutoPadding(auto_padding?: boolean): this; - // setAuthTag(tag: Binary): this; - // setAAD(buffer: Binary): this; + // setAuthTag(tag: NodeJS.ArrayBufferView): this; + // setAAD(buffer: NodeJS.ArrayBufferView): this; } interface DecipherCCM extends Decipher { - setAuthTag(buffer: Binary): this; - setAAD(buffer: Binary, options: { plaintextLength: number }): this; + setAuthTag(buffer: NodeJS.ArrayBufferView): this; + setAAD(buffer: NodeJS.ArrayBufferView, options: { plaintextLength: number }): this; } interface DecipherGCM extends Decipher { - setAuthTag(buffer: Binary): this; - setAAD(buffer: Binary, options?: { plaintextLength: number }): this; + setAuthTag(buffer: NodeJS.ArrayBufferView): this; + setAAD(buffer: NodeJS.ArrayBufferView, options?: { plaintextLength: number }): this; } interface PrivateKeyInput { @@ -302,23 +309,23 @@ declare module "crypto" { update(data: BinaryLike): Verify; update(data: string, input_encoding: Utf8AsciiLatin1Encoding): Verify; - verify(object: Object | KeyLike, signature: Binary): boolean; + verify(object: Object | KeyLike, signature: NodeJS.ArrayBufferView): boolean; verify(object: Object | KeyLike, signature: string, signature_format?: HexBase64Latin1Encoding): boolean; // https://nodejs.org/api/crypto.html#crypto_verifier_verify_object_signature_signature_format // The signature field accepts a TypedArray type, but it is only available starting ES2017 } - function createDiffieHellman(prime_length: number, generator?: number | Binary): DiffieHellman; - function createDiffieHellman(prime: Binary): DiffieHellman; + function createDiffieHellman(prime_length: number, generator?: number | NodeJS.ArrayBufferView): DiffieHellman; + function createDiffieHellman(prime: NodeJS.ArrayBufferView): DiffieHellman; function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding): DiffieHellman; - function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding, generator: number | Binary): DiffieHellman; + function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding, generator: number | NodeJS.ArrayBufferView): DiffieHellman; function createDiffieHellman(prime: string, prime_encoding: HexBase64Latin1Encoding, generator: string, generator_encoding: HexBase64Latin1Encoding): DiffieHellman; class DiffieHellman { private constructor(); generateKeys(): Buffer; generateKeys(encoding: HexBase64Latin1Encoding): string; - computeSecret(other_public_key: Binary): Buffer; + computeSecret(other_public_key: NodeJS.ArrayBufferView): Buffer; computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding): Buffer; - computeSecret(other_public_key: Binary, output_encoding: HexBase64Latin1Encoding): string; + computeSecret(other_public_key: NodeJS.ArrayBufferView, output_encoding: HexBase64Latin1Encoding): string; computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding, output_encoding: HexBase64Latin1Encoding): string; getPrime(): Buffer; getPrime(encoding: HexBase64Latin1Encoding): string; @@ -328,9 +335,9 @@ declare module "crypto" { getPublicKey(encoding: HexBase64Latin1Encoding): string; getPrivateKey(): Buffer; getPrivateKey(encoding: HexBase64Latin1Encoding): string; - setPublicKey(public_key: Binary): void; + setPublicKey(public_key: NodeJS.ArrayBufferView): void; setPublicKey(public_key: string, encoding: string): void; - setPrivateKey(private_key: Binary): void; + setPrivateKey(private_key: NodeJS.ArrayBufferView): void; setPrivateKey(private_key: string, encoding: string): void; verifyError: number; } @@ -350,10 +357,10 @@ declare module "crypto" { function pseudoRandomBytes(size: number): Buffer; function pseudoRandomBytes(size: number, callback: (err: Error | null, buf: Buffer) => void): void; - function randomFillSync(buffer: T, offset?: number, size?: number): T; - function randomFill(buffer: T, callback: (err: Error | null, buf: T) => void): void; - function randomFill(buffer: T, offset: number, callback: (err: Error | null, buf: T) => void): void; - function randomFill(buffer: T, offset: number, size: number, callback: (err: Error | null, buf: T) => void): void; + function randomFillSync(buffer: T, offset?: number, size?: number): T; + function randomFill(buffer: T, callback: (err: Error | null, buf: T) => void): void; + function randomFill(buffer: T, offset: number, callback: (err: Error | null, buf: T) => void): void; + function randomFill(buffer: T, offset: number, size: number, callback: (err: Error | null, buf: T) => void): void; interface ScryptOptions { N?: number; @@ -382,12 +389,15 @@ declare module "crypto" { interface RsaPrivateKey { key: KeyLike; passphrase?: string; + /** + * @default 'sha1' + */ + oaepHash?: string; + oaepLabel?: NodeJS.TypedArray; padding?: number; } - function publicEncrypt(public_key: RsaPublicKey | KeyLike, buffer: Binary): Buffer; - function privateDecrypt(private_key: RsaPrivateKey | KeyLike, buffer: Binary): Buffer; - function privateEncrypt(private_key: RsaPrivateKey | KeyLike, buffer: Binary): Buffer; - function publicDecrypt(public_key: RsaPublicKey | KeyLike, buffer: Binary): Buffer; + function publicEncrypt(key: RsaPublicKey | RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer; + function publicDecrypt(key: RsaPublicKey | RsaPrivateKey | KeyLike, buffer: NodeJS.ArrayBufferView): Buffer; function getCiphers(): string[]; function getCurves(): string[]; function getHashes(): string[]; @@ -402,19 +412,19 @@ declare module "crypto" { ): Buffer | string; generateKeys(): Buffer; generateKeys(encoding: HexBase64Latin1Encoding, format?: ECDHKeyFormat): string; - computeSecret(other_public_key: Binary): Buffer; + computeSecret(other_public_key: NodeJS.ArrayBufferView): Buffer; computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding): Buffer; - computeSecret(other_public_key: Binary, output_encoding: HexBase64Latin1Encoding): string; + computeSecret(other_public_key: NodeJS.ArrayBufferView, output_encoding: HexBase64Latin1Encoding): string; computeSecret(other_public_key: string, input_encoding: HexBase64Latin1Encoding, output_encoding: HexBase64Latin1Encoding): string; getPrivateKey(): Buffer; getPrivateKey(encoding: HexBase64Latin1Encoding): string; getPublicKey(): Buffer; getPublicKey(encoding: HexBase64Latin1Encoding, format?: ECDHKeyFormat): string; - setPrivateKey(private_key: Binary): void; + setPrivateKey(private_key: NodeJS.ArrayBufferView): void; setPrivateKey(private_key: string, encoding: HexBase64Latin1Encoding): void; } function createECDH(curve_name: string): ECDH; - function timingSafeEqual(a: Binary, b: Binary): boolean; + function timingSafeEqual(a: NodeJS.ArrayBufferView, b: NodeJS.ArrayBufferView): boolean; /** @deprecated since v10.0.0 */ const DEFAULT_ENCODING: string; @@ -585,7 +595,7 @@ declare module "crypto" { * If `key` is not a [`KeyObject`][], this function behaves as if `key` had been * passed to [`crypto.createPrivateKey()`][]. */ - function sign(algorithm: string | null | undefined, data: Binary, key: KeyLike | SignPrivateKeyInput): Buffer; + function sign(algorithm: string | null | undefined, data: NodeJS.ArrayBufferView, key: KeyLike | SignPrivateKeyInput): Buffer; interface VerifyKeyWithOptions extends KeyObject, SigningOptions { } @@ -598,5 +608,5 @@ declare module "crypto" { * If `key` is not a [`KeyObject`][], this function behaves as if `key` had been * passed to [`crypto.createPublicKey()`][]. */ - function verify(algorithm: string | null | undefined, data: Binary, key: KeyLike | VerifyKeyWithOptions, signature: Binary): Buffer; + function verify(algorithm: string | null | undefined, data: NodeJS.ArrayBufferView, key: KeyLike | VerifyKeyWithOptions, signature: NodeJS.ArrayBufferView): Buffer; } diff --git a/types/node/events.d.ts b/types/node/events.d.ts index f86102101c..03f5b905ed 100644 --- a/types/node/events.d.ts +++ b/types/node/events.d.ts @@ -1,8 +1,17 @@ declare module "events" { class internal extends NodeJS.EventEmitter { } + interface NodeEventTarget { + once(event: string | symbol, listener: (...args: any[]) => void): this; + } + + interface DOMEventTarget { + addEventListener(event: string, listener: (...args: any[]) => void, opts?: { once: boolean }): any; + } + namespace internal { - function once(emitter: EventEmitter, event: string | symbol): Promise; + function once(emitter: NodeEventTarget, event: string | symbol): Promise; + function once(emitter: DOMEventTarget, event: string): Promise; class EventEmitter extends internal { /** @deprecated since v4.0.0 */ static listenerCount(emitter: EventEmitter, event: string | symbol): number; diff --git a/types/node/fs.d.ts b/types/node/fs.d.ts index bc015cec09..3747c461ea 100644 --- a/types/node/fs.d.ts +++ b/types/node/fs.d.ts @@ -8,8 +8,9 @@ declare module "fs" { */ type PathLike = string | Buffer | URL; - type BinaryData = DataView | NodeJS.TypedArray; - class Stats { + type NoParamCallback = (err: NodeJS.ErrnoException | null) => void; + + interface StatsBase { isFile(): boolean; isDirectory(): boolean; isBlockDevice(): boolean; @@ -17,6 +18,7 @@ declare module "fs" { isSymbolicLink(): boolean; isFIFO(): boolean; isSocket(): boolean; + dev: number; ino: number; mode: number; @@ -37,6 +39,12 @@ declare module "fs" { birthtime: Date; } + interface Stats extends StatsBase { + } + + class Stats { + } + class Dirent { isFile(): boolean; isDirectory(): boolean; @@ -146,7 +154,7 @@ declare module "fs" { * @param newPath A path to a file. If a URL is provided, it must use the `file:` protocol. * URL support is _experimental_. */ - function rename(oldPath: PathLike, newPath: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void; + function rename(oldPath: PathLike, newPath: PathLike, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace rename { @@ -174,14 +182,14 @@ declare module "fs" { * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * @param len If not specified, defaults to `0`. */ - function truncate(path: PathLike, len: number | undefined | null, callback: (err: NodeJS.ErrnoException | null) => void): void; + function truncate(path: PathLike, len: number | undefined | null, callback: NoParamCallback): void; /** * Asynchronous truncate(2) - Truncate a file to a specified length. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * URL support is _experimental_. */ - function truncate(path: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void; + function truncate(path: PathLike, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace truncate { @@ -205,13 +213,13 @@ declare module "fs" { * @param fd A file descriptor. * @param len If not specified, defaults to `0`. */ - function ftruncate(fd: number, len: number | undefined | null, callback: (err: NodeJS.ErrnoException | null) => void): void; + function ftruncate(fd: number, len: number | undefined | null, callback: NoParamCallback): void; /** * Asynchronous ftruncate(2) - Truncate a file to a specified length. * @param fd A file descriptor. */ - function ftruncate(fd: number, callback: (err: NodeJS.ErrnoException | null) => void): void; + function ftruncate(fd: number, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace ftruncate { @@ -234,7 +242,7 @@ declare module "fs" { * Asynchronous chown(2) - Change ownership of a file. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. */ - function chown(path: PathLike, uid: number, gid: number, callback: (err: NodeJS.ErrnoException | null) => void): void; + function chown(path: PathLike, uid: number, gid: number, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace chown { @@ -255,7 +263,7 @@ declare module "fs" { * Asynchronous fchown(2) - Change ownership of a file. * @param fd A file descriptor. */ - function fchown(fd: number, uid: number, gid: number, callback: (err: NodeJS.ErrnoException | null) => void): void; + function fchown(fd: number, uid: number, gid: number, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace fchown { @@ -276,7 +284,7 @@ declare module "fs" { * Asynchronous lchown(2) - Change ownership of a file. Does not dereference symbolic links. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. */ - function lchown(path: PathLike, uid: number, gid: number, callback: (err: NodeJS.ErrnoException | null) => void): void; + function lchown(path: PathLike, uid: number, gid: number, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace lchown { @@ -298,7 +306,7 @@ declare module "fs" { * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * @param mode A file mode. If a string is passed, it is parsed as an octal integer. */ - function chmod(path: PathLike, mode: string | number, callback: (err: NodeJS.ErrnoException | null) => void): void; + function chmod(path: PathLike, mode: string | number, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace chmod { @@ -322,7 +330,7 @@ declare module "fs" { * @param fd A file descriptor. * @param mode A file mode. If a string is passed, it is parsed as an octal integer. */ - function fchmod(fd: number, mode: string | number, callback: (err: NodeJS.ErrnoException | null) => void): void; + function fchmod(fd: number, mode: string | number, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace fchmod { @@ -346,7 +354,7 @@ declare module "fs" { * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. * @param mode A file mode. If a string is passed, it is parsed as an octal integer. */ - function lchmod(path: PathLike, mode: string | number, callback: (err: NodeJS.ErrnoException | null) => void): void; + function lchmod(path: PathLike, mode: string | number, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace lchmod { @@ -433,7 +441,7 @@ declare module "fs" { * @param existingPath A path to a file. If a URL is provided, it must use the `file:` protocol. * @param newPath A path to a file. If a URL is provided, it must use the `file:` protocol. */ - function link(existingPath: PathLike, newPath: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void; + function link(existingPath: PathLike, newPath: PathLike, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace link { @@ -459,14 +467,14 @@ declare module "fs" { * @param type May be set to `'dir'`, `'file'`, or `'junction'` (default is `'file'`) and is only available on Windows (ignored on other platforms). * When using `'junction'`, the `target` argument will automatically be normalized to an absolute path. */ - function symlink(target: PathLike, path: PathLike, type: symlink.Type | undefined | null, callback: (err: NodeJS.ErrnoException | null) => void): void; + function symlink(target: PathLike, path: PathLike, type: symlink.Type | undefined | null, callback: NoParamCallback): void; /** * Asynchronous symlink(2) - Create a new symbolic link to an existing file. * @param target A path to an existing file. If a URL is provided, it must use the `file:` protocol. * @param path A path to the new symlink. If a URL is provided, it must use the `file:` protocol. */ - function symlink(target: PathLike, path: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void; + function symlink(target: PathLike, path: PathLike, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace symlink { @@ -662,7 +670,7 @@ declare module "fs" { * Asynchronous unlink(2) - delete a name and possibly the file it refers to. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. */ - function unlink(path: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void; + function unlink(path: PathLike, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace unlink { @@ -679,11 +687,42 @@ declare module "fs" { */ function unlinkSync(path: PathLike): void; + interface RmDirOptions { + /** + * If `true`, perform a recursive directory removal. In + * recursive mode, errors are not reported if `path` does not exist, and + * operations are retried on failure. + * @experimental + * @default false + */ + recursive?: boolean; + } + + interface RmDirAsyncOptions extends RmDirOptions { + /** + * If an `EMFILE` error is encountered, Node.js will + * retry the operation with a linear backoff of 1ms longer on each try until the + * timeout duration passes this limit. This option is ignored if the `recursive` + * option is not `true`. + * @default 1000 + */ + emfileWait?: number; + /** + * If an `EBUSY`, `ENOTEMPTY`, or `EPERM` error is + * encountered, Node.js will retry the operation with a linear backoff wait of + * 100ms longer on each try. This option represents the number of retries. This + * option is ignored if the `recursive` option is not `true`. + * @default 3 + */ + maxBusyTries?: number; + } + /** * Asynchronous rmdir(2) - delete a directory. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. */ - function rmdir(path: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void; + function rmdir(path: PathLike, callback: NoParamCallback): void; + function rmdir(path: PathLike, options: RmDirAsyncOptions, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace rmdir { @@ -691,14 +730,14 @@ declare module "fs" { * Asynchronous rmdir(2) - delete a directory. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. */ - function __promisify__(path: PathLike): Promise; + function __promisify__(path: PathLike, options?: RmDirAsyncOptions): Promise; } /** * Synchronous rmdir(2) - delete a directory. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. */ - function rmdirSync(path: PathLike): void; + function rmdirSync(path: PathLike, options?: RmDirOptions): void; export interface MakeDirectoryOptions { /** @@ -719,13 +758,13 @@ declare module "fs" { * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`. */ - function mkdir(path: PathLike, options: number | string | MakeDirectoryOptions | undefined | null, callback: (err: NodeJS.ErrnoException | null) => void): void; + function mkdir(path: PathLike, options: number | string | MakeDirectoryOptions | undefined | null, callback: NoParamCallback): void; /** * Asynchronous mkdir(2) - create a directory with a mode of `0o777`. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol. */ - function mkdir(path: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void; + function mkdir(path: PathLike, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace mkdir { @@ -923,7 +962,7 @@ declare module "fs" { * Asynchronous close(2) - close a file descriptor. * @param fd A file descriptor. */ - function close(fd: number, callback: (err: NodeJS.ErrnoException | null) => void): void; + function close(fd: number, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace close { @@ -976,7 +1015,7 @@ declare module "fs" { * @param atime The last access time. If a string is provided, it will be coerced to number. * @param mtime The last modified time. If a string is provided, it will be coerced to number. */ - function utimes(path: PathLike, atime: string | number | Date, mtime: string | number | Date, callback: (err: NodeJS.ErrnoException | null) => void): void; + function utimes(path: PathLike, atime: string | number | Date, mtime: string | number | Date, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace utimes { @@ -1003,7 +1042,7 @@ declare module "fs" { * @param atime The last access time. If a string is provided, it will be coerced to number. * @param mtime The last modified time. If a string is provided, it will be coerced to number. */ - function futimes(fd: number, atime: string | number | Date, mtime: string | number | Date, callback: (err: NodeJS.ErrnoException | null) => void): void; + function futimes(fd: number, atime: string | number | Date, mtime: string | number | Date, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace futimes { @@ -1028,7 +1067,7 @@ declare module "fs" { * Asynchronous fsync(2) - synchronize a file's in-core state with the underlying storage device. * @param fd A file descriptor. */ - function fsync(fd: number, callback: (err: NodeJS.ErrnoException | null) => void): void; + function fsync(fd: number, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace fsync { @@ -1052,7 +1091,7 @@ declare module "fs" { * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`. * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position. */ - function write( + function write( fd: number, buffer: TBuffer, offset: number | undefined | null, @@ -1067,7 +1106,7 @@ declare module "fs" { * @param offset The part of the buffer to be written. If not supplied, defaults to `0`. * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`. */ - function write( + function write( fd: number, buffer: TBuffer, offset: number | undefined | null, @@ -1080,7 +1119,7 @@ declare module "fs" { * @param fd A file descriptor. * @param offset The part of the buffer to be written. If not supplied, defaults to `0`. */ - function write( + function write( fd: number, buffer: TBuffer, offset: number | undefined | null, @@ -1091,7 +1130,7 @@ declare module "fs" { * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor. * @param fd A file descriptor. */ - function write(fd: number, buffer: TBuffer, callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void): void; + function write(fd: number, buffer: TBuffer, callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void): void; /** * Asynchronously writes `string` to the file referenced by the supplied file descriptor. @@ -1132,7 +1171,7 @@ declare module "fs" { * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`. * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position. */ - function __promisify__( + function __promisify__( fd: number, buffer?: TBuffer, offset?: number, @@ -1157,7 +1196,7 @@ declare module "fs" { * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`. * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position. */ - function writeSync(fd: number, buffer: BinaryData, offset?: number | null, length?: number | null, position?: number | null): number; + function writeSync(fd: number, buffer: NodeJS.ArrayBufferView, offset?: number | null, length?: number | null, position?: number | null): number; /** * Synchronously writes `string` to the file referenced by the supplied file descriptor, returning the number of bytes written. @@ -1176,7 +1215,7 @@ declare module "fs" { * @param length The number of bytes to read. * @param position The offset from the beginning of the file from which data should be read. If `null`, data will be read from the current position. */ - function read( + function read( fd: number, buffer: TBuffer, offset: number, @@ -1194,7 +1233,13 @@ declare module "fs" { * @param length The number of bytes to read. * @param position The offset from the beginning of the file from which data should be read. If `null`, data will be read from the current position. */ - function __promisify__(fd: number, buffer: TBuffer, offset: number, length: number, position: number | null): Promise<{ bytesRead: number, buffer: TBuffer }>; + function __promisify__( + fd: number, + buffer: TBuffer, + offset: number, + length: number, + position: number | null + ): Promise<{ bytesRead: number, buffer: TBuffer }>; } /** @@ -1205,7 +1250,7 @@ declare module "fs" { * @param length The number of bytes to read. * @param position The offset from the beginning of the file from which data should be read. If `null`, data will be read from the current position. */ - function readSync(fd: number, buffer: BinaryData, offset: number, length: number, position: number | null): number; + function readSync(fd: number, buffer: NodeJS.ArrayBufferView, offset: number, length: number, position: number | null): number; /** * Asynchronously reads the entire contents of a file. @@ -1322,7 +1367,7 @@ declare module "fs" { * If `mode` is a string, it is parsed as an octal integer. * If `flag` is not supplied, the default of `'w'` is used. */ - function writeFile(path: PathLike | number, data: any, options: WriteFileOptions, callback: (err: NodeJS.ErrnoException | null) => void): void; + function writeFile(path: PathLike | number, data: any, options: WriteFileOptions, callback: NoParamCallback): void; /** * Asynchronously writes data to a file, replacing the file if it already exists. @@ -1331,7 +1376,7 @@ declare module "fs" { * If a file descriptor is provided, the underlying file will _not_ be closed automatically. * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string. */ - function writeFile(path: PathLike | number, data: any, callback: (err: NodeJS.ErrnoException | null) => void): void; + function writeFile(path: PathLike | number, data: any, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace writeFile { @@ -1376,7 +1421,7 @@ declare module "fs" { * If `mode` is a string, it is parsed as an octal integer. * If `flag` is not supplied, the default of `'a'` is used. */ - function appendFile(file: PathLike | number, data: any, options: WriteFileOptions, callback: (err: NodeJS.ErrnoException | null) => void): void; + function appendFile(file: PathLike | number, data: any, options: WriteFileOptions, callback: NoParamCallback): void; /** * Asynchronously append data to a file, creating the file if it does not exist. @@ -1385,7 +1430,7 @@ declare module "fs" { * If a file descriptor is provided, the underlying file will _not_ be closed automatically. * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string. */ - function appendFile(file: PathLike | number, data: any, callback: (err: NodeJS.ErrnoException | null) => void): void; + function appendFile(file: PathLike | number, data: any, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace appendFile { @@ -1663,6 +1708,13 @@ declare module "fs" { /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by others. */ const S_IXOTH: number; + + /** + * When set, a memory file mapping is used to access the file. This flag + * is available on Windows operating systems only. On other operating systems, + * this flag is ignored. + */ + const UV_FS_O_FILEMAP: number; } /** @@ -1670,14 +1722,14 @@ declare module "fs" { * @param path A path to a file or directory. If a URL is provided, it must use the `file:` protocol. * URL support is _experimental_. */ - function access(path: PathLike, mode: number | undefined, callback: (err: NodeJS.ErrnoException | null) => void): void; + function access(path: PathLike, mode: number | undefined, callback: NoParamCallback): void; /** * Asynchronously tests a user's permissions for the file specified by path. * @param path A path to a file or directory. If a URL is provided, it must use the `file:` protocol. * URL support is _experimental_. */ - function access(path: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void; + function access(path: PathLike, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace access { @@ -1707,6 +1759,10 @@ declare module "fs" { fd?: number; mode?: number; autoClose?: boolean; + /** + * @default false + */ + emitClose?: boolean; start?: number; end?: number; highWaterMark?: number; @@ -1731,7 +1787,7 @@ declare module "fs" { * Asynchronous fdatasync(2) - synchronize a file's in-core state with storage device. * @param fd A file descriptor. */ - function fdatasync(fd: number, callback: (err: NodeJS.ErrnoException | null) => void): void; + function fdatasync(fd: number, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace fdatasync { @@ -1757,7 +1813,7 @@ declare module "fs" { * @param src A path to the source file. * @param dest A path to the destination file. */ - function copyFile(src: PathLike, dest: PathLike, callback: (err: NodeJS.ErrnoException | null) => void): void; + function copyFile(src: PathLike, dest: PathLike, callback: NoParamCallback): void; /** * Asynchronously copies src to dest. By default, dest is overwritten if it already exists. * No arguments other than a possible exception are given to the callback function. @@ -1768,7 +1824,7 @@ declare module "fs" { * @param dest A path to the destination file. * @param flags An integer that specifies the behavior of the copy operation. The only supported flag is fs.constants.COPYFILE_EXCL, which causes the copy operation to fail if dest already exists. */ - function copyFile(src: PathLike, dest: PathLike, flags: number, callback: (err: NodeJS.ErrnoException | null) => void): void; + function copyFile(src: PathLike, dest: PathLike, flags: number, callback: NoParamCallback): void; // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime. namespace copyFile { @@ -1799,6 +1855,39 @@ declare module "fs" { */ function copyFileSync(src: PathLike, dest: PathLike, flags?: number): void; + /** + * Write an array of ArrayBufferViews to the file specified by fd using writev(). + * position is the offset from the beginning of the file where this data should be written. + * It is unsafe to use fs.writev() multiple times on the same file without waiting for the callback. For this scenario, use fs.createWriteStream(). + * On Linux, positional writes don't work when the file is opened in append mode. + * The kernel ignores the position argument and always appends the data to the end of the file. + */ + function writev( + fd: number, + buffers: NodeJS.ArrayBufferView[], + cb: (err: NodeJS.ErrnoException | null, bytesWritten: number, buffers: NodeJS.ArrayBufferView[]) => void + ): void; + function writev( + fd: number, + buffers: NodeJS.ArrayBufferView[], + position: number, + cb: (err: NodeJS.ErrnoException | null, bytesWritten: number, buffers: NodeJS.ArrayBufferView[]) => void + ): void; + + interface WriteVResult { + bytesWritten: number; + buffers: NodeJS.ArrayBufferView[]; + } + + namespace writev { + function __promisify__(fd: number, buffers: NodeJS.ArrayBufferView[], position?: number): Promise; + } + + /** + * See `writev`. + */ + function writevSync(fd: number, buffers: NodeJS.ArrayBufferView[], position?: number): number; + namespace promises { interface FileHandle { /** @@ -1925,6 +2014,11 @@ declare module "fs" { */ writeFile(data: any, options?: { encoding?: string | null, mode?: string | number, flag?: string | number } | string | null): Promise; + /** + * See `fs.writev` promisified version. + */ + writev(buffers: NodeJS.ArrayBufferView[], position?: number): Promise; + /** * Asynchronous close(2) - close a `FileHandle`. */ diff --git a/types/node/globals.d.ts b/types/node/globals.d.ts index 9df7727759..ed43a033af 100644 --- a/types/node/globals.d.ts +++ b/types/node/globals.d.ts @@ -341,7 +341,7 @@ declare class Buffer extends Uint8Array { * @param encoding encoding used to evaluate (defaults to 'utf8') */ static byteLength( - string: string | NodeJS.TypedArray | DataView | ArrayBuffer | SharedArrayBuffer, + string: string | NodeJS.ArrayBufferView | ArrayBuffer | SharedArrayBuffer, encoding?: BufferEncoding ): number; /** @@ -651,9 +651,7 @@ declare namespace NodeJS { interface ReadWriteStream extends ReadableStream, WritableStream { } - interface Events extends EventEmitter { } - - interface Domain extends Events { + interface Domain extends EventEmitter { run(fn: (...args: any[]) => T, ...args: any[]): T; add(emitter: EventEmitter | Timer): void; remove(emitter: EventEmitter | Timer): void; @@ -1152,4 +1150,5 @@ declare namespace NodeJS { } type TypedArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array; + type ArrayBufferView = TypedArray | DataView; } diff --git a/types/node/http2.d.ts b/types/node/http2.d.ts index 184e10f34c..7b40e5cf62 100644 --- a/types/node/http2.d.ts +++ b/types/node/http2.d.ts @@ -277,12 +277,12 @@ declare module "http2" { destroy(error?: Error, code?: number): void; readonly destroyed: boolean; readonly encrypted?: boolean; - goaway(code?: number, lastStreamID?: number, opaqueData?: Buffer | DataView | NodeJS.TypedArray): void; + goaway(code?: number, lastStreamID?: number, opaqueData?: NodeJS.ArrayBufferView): void; readonly localSettings: Settings; readonly originSet?: string[]; readonly pendingSettingsAck: boolean; ping(callback: (err: Error | null, duration: number, payload: Buffer) => void): boolean; - ping(payload: Buffer | DataView | NodeJS.TypedArray , callback: (err: Error | null, duration: number, payload: Buffer) => void): boolean; + ping(payload: NodeJS.ArrayBufferView, callback: (err: Error | null, duration: number, payload: Buffer) => void): boolean; ref(): void; readonly remoteSettings: Settings; setTimeout(msecs: number, callback?: () => void): void; diff --git a/types/node/index.d.ts b/types/node/index.d.ts index 6141ecda6e..5733689551 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for non-npm package Node.js 12.7 +// Type definitions for non-npm package Node.js 12.11 // Project: http://nodejs.org/ // Definitions by: Microsoft TypeScript // DefinitelyTyped @@ -67,8 +67,8 @@ interface WeakSetConstructor { } interface Set {} interface Map {} interface ReadonlySet {} -interface IteratorResult { } interface Iterable { } +interface IteratorResult { } interface AsyncIterable { } interface Iterator { next(value?: any): IteratorResult; diff --git a/types/node/net.d.ts b/types/node/net.d.ts index c8bccb7b30..1e4f971876 100644 --- a/types/node/net.d.ts +++ b/types/node/net.d.ts @@ -18,7 +18,26 @@ declare module "net" { writable?: boolean; } - interface TcpSocketConnectOpts { + interface OnReadOpts { + buffer: Uint8Array | (() => Uint8Array); + /** + * This function is called for every chunk of incoming data. + * Two arguments are passed to it: the number of bytes written to buffer and a reference to buffer. + * Return false from this function to implicitly pause() the socket. + */ + callback(bytesWritten: number, buf: Uint8Array): boolean; + } + + interface ConnectOpts { + /** + * If specified, incoming data is stored in a single buffer and passed to the supplied callback when data arrives on the socket. + * Note: this will cause the streaming functionality to not provide any data, however events like 'error', 'end', and 'close' will + * still be emitted as normal and methods like pause() and resume() will also behave as expected. + */ + onread?: OnReadOpts; + } + + interface TcpSocketConnectOpts extends ConnectOpts { port: number; host?: string; localAddress?: string; @@ -28,7 +47,7 @@ declare module "net" { lookup?: LookupFunction; } - interface IpcSocketConnectOpts { + interface IpcSocketConnectOpts extends ConnectOpts { path: string; } diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index e8a65c0def..5f1642424f 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -12,302 +12,6 @@ import * as inspector from "inspector"; import * as trace_events from "trace_events"; import Module = require("module"); -//////////////////////////////////////////////////// -/// File system tests : http://nodejs.org/api/fs.html -//////////////////////////////////////////////////// - -{ - { - fs.writeFile("thebible.txt", - "Do unto others as you would have them do unto you.", - assert.ifError); - - fs.write(1234, "test", () => { }); - - fs.writeFile("Harry Potter", - "\"You be wizzing, Harry,\" jived Dumbledore.", - { - encoding: "ascii" - }, - assert.ifError); - - fs.writeFile("testfile", "content", "utf8", assert.ifError); - - fs.writeFileSync("testfile", "content", "utf8"); - fs.writeFileSync("testfile", "content", { encoding: "utf8" }); - fs.writeFileSync("testfile", new DataView(new ArrayBuffer(1)), { encoding: "utf8" }); - } - - { - fs.appendFile("testfile", "foobar", "utf8", assert.ifError); - fs.appendFile("testfile", "foobar", { encoding: "utf8" }, assert.ifError); - fs.appendFileSync("testfile", "foobar", "utf8"); - fs.appendFileSync("testfile", "foobar", { encoding: "utf8" }); - } - - { - let content: string; - let buffer: Buffer; - let stringOrBuffer: string | Buffer; - const nullEncoding: string | null = null; - const stringEncoding: string | null = 'utf8'; - - content = fs.readFileSync('testfile', 'utf8'); - content = fs.readFileSync('testfile', { encoding: 'utf8' }); - stringOrBuffer = fs.readFileSync('testfile', stringEncoding); - stringOrBuffer = fs.readFileSync('testfile', { encoding: stringEncoding }); - - buffer = fs.readFileSync('testfile'); - buffer = fs.readFileSync('testfile', null); - buffer = fs.readFileSync('testfile', { encoding: null }); - stringOrBuffer = fs.readFileSync('testfile', nullEncoding); - stringOrBuffer = fs.readFileSync('testfile', { encoding: nullEncoding }); - - buffer = fs.readFileSync('testfile', { flag: 'r' }); - - fs.readFile('testfile', 'utf8', (err, data) => content = data); - fs.readFile('testfile', { encoding: 'utf8' }, (err, data) => content = data); - fs.readFile('testfile', stringEncoding, (err, data) => stringOrBuffer = data); - fs.readFile('testfile', { encoding: stringEncoding }, (err, data) => stringOrBuffer = data); - - fs.readFile('testfile', (err, data) => buffer = data); - fs.readFile('testfile', null, (err, data) => buffer = data); - fs.readFile('testfile', { encoding: null }, (err, data) => buffer = data); - fs.readFile('testfile', nullEncoding, (err, data) => stringOrBuffer = data); - fs.readFile('testfile', { encoding: nullEncoding }, (err, data) => stringOrBuffer = data); - - fs.readFile('testfile', { flag: 'r' }, (err, data) => buffer = data); - } - - { - fs.read(1, new DataView(new ArrayBuffer(1)), 0, 1, 0, (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: DataView) => {}); - } - - { - fs.readSync(1, new DataView(new ArrayBuffer(1)), 0, 1, 0); - } - - { - let errno: number; - fs.readFile('testfile', (err, data) => { - if (err && err.errno) { - errno = err.errno; - } - }); - } - - { - let listS: string[]; - listS = fs.readdirSync('path'); - listS = fs.readdirSync('path', { encoding: 'utf8' }); - listS = fs.readdirSync('path', { encoding: null }); - listS = fs.readdirSync('path', { encoding: undefined }) as string[]; - listS = fs.readdirSync('path', 'utf8'); - listS = fs.readdirSync('path', null); - listS = fs.readdirSync('path', undefined); - const listDir: fs.Dirent[] = fs.readdirSync('path', { withFileTypes: true }); - const listDir2: Buffer[] = fs.readdirSync('path', { withFileTypes: false, encoding: 'buffer' }); - const listDir3: fs.Dirent[] = fs.readdirSync('path', { encoding: 'utf8', withFileTypes: true }); - - let listB: Buffer[]; - listB = fs.readdirSync('path', { encoding: 'buffer' }); - listB = fs.readdirSync("path", 'buffer'); - - const enc = 'buffer'; - fs.readdirSync('path', { encoding: enc }); - fs.readdirSync('path', { }); - - fs.readdir('path', { withFileTypes: true }, (err: NodeJS.ErrnoException | null, files: fs.Dirent[]) => {}); - } - - async function testPromisify() { - const rd = util.promisify(fs.readdir); - let listS: string[]; - listS = await rd('path'); - listS = await rd('path', 'utf8'); - listS = await rd('path', null); - listS = await rd('path', undefined); - listS = await rd('path', { encoding: 'utf8' }); - listS = await rd('path', { encoding: null }); - listS = await rd('path', { encoding: null, withFileTypes: false }); - listS = await rd('path', { encoding: 'utf8', withFileTypes: false }); - const listDir: fs.Dirent[] = await rd('path', { withFileTypes: true }); - const listDir2: Buffer[] = await rd('path', { withFileTypes: false, encoding: 'buffer' }); - const listDir3: fs.Dirent[] = await rd('path', { encoding: 'utf8', withFileTypes: true }); - } - - { - fs.mkdtemp('/tmp/foo-', (err, folder) => { - console.log(folder); - // Prints: /tmp/foo-itXde2 - }); - } - - { - let tempDir: string; - tempDir = fs.mkdtempSync('/tmp/foo-'); - } - - { - fs.watch('/tmp/foo-', (event, filename) => { - console.log(event, filename); - }); - - fs.watch('/tmp/foo-', 'utf8', (event, filename) => { - console.log(event, filename); - }); - - fs.watch('/tmp/foo-', { - recursive: true, - persistent: true, - encoding: 'utf8' - }, (event, filename) => { - console.log(event, filename); - }); - } - - { - fs.access('/path/to/folder', (err) => { }); - - fs.access(Buffer.from(''), (err) => { }); - - fs.access('/path/to/folder', fs.constants.F_OK | fs.constants.R_OK, (err) => { }); - - fs.access(Buffer.from(''), fs.constants.F_OK | fs.constants.R_OK, (err) => { }); - - fs.accessSync('/path/to/folder'); - - fs.accessSync(Buffer.from('')); - - fs.accessSync('path/to/folder', fs.constants.W_OK | fs.constants.X_OK); - - fs.accessSync(Buffer.from(''), fs.constants.W_OK | fs.constants.X_OK); - } - - { - let s = '123'; - let b: Buffer; - fs.readlink('/path/to/folder', (err, linkString) => s = linkString); - fs.readlink('/path/to/folder', undefined, (err, linkString) => s = linkString); - fs.readlink('/path/to/folder', 'utf8', (err, linkString) => s = linkString); - fs.readlink('/path/to/folder', 'buffer', (err, linkString) => b = linkString); - fs.readlink('/path/to/folder', s, (err, linkString) => typeof linkString === 'string' ? s = linkString : b = linkString); - fs.readlink('/path/to/folder', {}, (err, linkString) => s = linkString); - fs.readlink('/path/to/folder', { encoding: undefined }, (err, linkString) => s = linkString); - fs.readlink('/path/to/folder', { encoding: 'utf8' }, (err, linkString) => s = linkString); - fs.readlink('/path/to/folder', { encoding: 'buffer' }, (err, linkString) => b = linkString); - fs.readlink('/path/to/folder', { encoding: s }, (err, linkString) => typeof linkString === "string" ? s = linkString : b = linkString); - - s = fs.readlinkSync('/path/to/folder'); - s = fs.readlinkSync('/path/to/folder', undefined); - s = fs.readlinkSync('/path/to/folder', 'utf8'); - b = fs.readlinkSync('/path/to/folder', 'buffer'); - const v1 = fs.readlinkSync('/path/to/folder', s); - typeof v1 === "string" ? s = v1 : b = v1; - - s = fs.readlinkSync('/path/to/folder', {}); - s = fs.readlinkSync('/path/to/folder', { encoding: undefined }); - s = fs.readlinkSync('/path/to/folder', { encoding: 'utf8' }); - b = fs.readlinkSync('/path/to/folder', { encoding: 'buffer' }); - const v2 = fs.readlinkSync('/path/to/folder', { encoding: s }); - typeof v2 === "string" ? s = v2 : b = v2; - } - - { - let s = '123'; - let b: Buffer; - fs.realpath('/path/to/folder', (err, resolvedPath) => s = resolvedPath); - fs.realpath('/path/to/folder', undefined, (err, resolvedPath) => s = resolvedPath); - fs.realpath('/path/to/folder', 'utf8', (err, resolvedPath) => s = resolvedPath); - fs.realpath('/path/to/folder', 'buffer', (err, resolvedPath) => b = resolvedPath); - fs.realpath('/path/to/folder', s, (err, resolvedPath) => typeof resolvedPath === 'string' ? s = resolvedPath : b = resolvedPath); - fs.realpath('/path/to/folder', {}, (err, resolvedPath) => s = resolvedPath); - fs.realpath('/path/to/folder', { encoding: undefined }, (err, resolvedPath) => s = resolvedPath); - fs.realpath('/path/to/folder', { encoding: 'utf8' }, (err, resolvedPath) => s = resolvedPath); - fs.realpath('/path/to/folder', { encoding: 'buffer' }, (err, resolvedPath) => b = resolvedPath); - fs.realpath('/path/to/folder', { encoding: s }, (err, resolvedPath) => typeof resolvedPath === "string" ? s = resolvedPath : b = resolvedPath); - - s = fs.realpathSync('/path/to/folder'); - s = fs.realpathSync('/path/to/folder', undefined); - s = fs.realpathSync('/path/to/folder', 'utf8'); - b = fs.realpathSync('/path/to/folder', 'buffer'); - const v1 = fs.realpathSync('/path/to/folder', s); - typeof v1 === "string" ? s = v1 : b = v1; - - s = fs.realpathSync('/path/to/folder', {}); - s = fs.realpathSync('/path/to/folder', { encoding: undefined }); - s = fs.realpathSync('/path/to/folder', { encoding: 'utf8' }); - b = fs.realpathSync('/path/to/folder', { encoding: 'buffer' }); - const v2 = fs.realpathSync('/path/to/folder', { encoding: s }); - typeof v2 === "string" ? s = v2 : b = v2; - - // native - fs.realpath.native('/path/to/folder', (err, resolvedPath) => s = resolvedPath); - fs.realpath.native('/path/to/folder', undefined, (err, resolvedPath) => s = resolvedPath); - fs.realpath.native('/path/to/folder', 'utf8', (err, resolvedPath) => s = resolvedPath); - fs.realpath.native('/path/to/folder', 'buffer', (err, resolvedPath) => b = resolvedPath); - fs.realpath.native('/path/to/folder', s, (err, resolvedPath) => typeof resolvedPath === 'string' ? s = resolvedPath : b = resolvedPath); - fs.realpath.native('/path/to/folder', {}, (err, resolvedPath) => s = resolvedPath); - fs.realpath.native('/path/to/folder', { encoding: undefined }, (err, resolvedPath) => s = resolvedPath); - fs.realpath.native('/path/to/folder', { encoding: 'utf8' }, (err, resolvedPath) => s = resolvedPath); - fs.realpath.native('/path/to/folder', { encoding: 'buffer' }, (err, resolvedPath) => b = resolvedPath); - fs.realpath.native('/path/to/folder', { encoding: s }, (err, resolvedPath) => typeof resolvedPath === "string" ? s = resolvedPath : b = resolvedPath); - - s = fs.realpathSync.native('/path/to/folder'); - s = fs.realpathSync.native('/path/to/folder', undefined); - s = fs.realpathSync.native('/path/to/folder', 'utf8'); - b = fs.realpathSync.native('/path/to/folder', 'buffer'); - const v3 = fs.realpathSync.native('/path/to/folder', s); - typeof v3 === "string" ? s = v3 : b = v3; - - s = fs.realpathSync.native('/path/to/folder', {}); - s = fs.realpathSync.native('/path/to/folder', { encoding: undefined }); - s = fs.realpathSync.native('/path/to/folder', { encoding: 'utf8' }); - b = fs.realpathSync.native('/path/to/folder', { encoding: 'buffer' }); - const v4 = fs.realpathSync.native('/path/to/folder', { encoding: s }); - typeof v4 === "string" ? s = v4 : b = v4; - } - - { - fs.copyFile('/path/to/src', '/path/to/dest', (err) => console.error(err)); - fs.copyFile('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_EXCL, (err) => console.error(err)); - fs.copyFile('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_FICLONE, (err) => console.error(err)); - fs.copyFile('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_FICLONE_FORCE, (err) => console.error(err)); - - fs.copyFileSync('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_EXCL); - fs.copyFileSync('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_FICLONE); - fs.copyFileSync('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_FICLONE_FORCE); - - const cf = util.promisify(fs.copyFile); - cf('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_EXCL).then(console.log); - } - - { - fs.mkdir('some/test/path', { - recursive: true, - mode: 0o777, - }, () => { - }); - - fs.mkdirSync('some/test/path', { - recursive: true, - mode: 0o777, - }); - } - - { - let names: Promise; - let buffers: Promise; - let namesOrBuffers: Promise; - let entries: Promise; - - names = fs.promises.readdir('/path/to/dir', { encoding: 'utf8', withFileTypes: false }); - buffers = fs.promises.readdir('/path/to/dir', { encoding: 'buffer', withFileTypes: false }); - namesOrBuffers = fs.promises.readdir('/path/to/dir', { encoding: 'SOME OTHER', withFileTypes: false }); - entries = fs.promises.readdir('/path/to/dir', { encoding: 'utf8', withFileTypes: true }); - } -} - //////////////////////////////////////////////////// /// Url tests : http://nodejs.org/api/url.html //////////////////////////////////////////////////// diff --git a/types/node/test/crypto.ts b/types/node/test/crypto.ts index 39c639482c..28051e3924 100644 --- a/types/node/test/crypto.ts +++ b/types/node/test/crypto.ts @@ -4,7 +4,8 @@ import { promisify } from 'util'; { // crypto_hash_string_test - const hashResult: string = crypto.createHash('md5').update('world').digest('hex'); + let hashResult: string = crypto.createHash('md5').update('world').digest('hex'); + hashResult = crypto.createHash('shake256', { outputLength: 16 }).update('world').digest('hex'); } { @@ -645,3 +646,11 @@ import { promisify } from 'util'; const sig: Buffer = crypto.sign('md5', Buffer.from(''), 'mykey'); const correct: Buffer = crypto.verify('md5', sig, 'mykey', sig); } + +{ + const buf: Buffer = crypto.publicEncrypt({ + key: 'test', + oaepHash: 'sha1', + oaepLabel: Buffer.from('asd'), + }, Buffer.from([])); +} diff --git a/types/node/test/events.ts b/types/node/test/events.ts index 6005b8b15b..f73c3f0efe 100644 --- a/types/node/test/events.ts +++ b/types/node/test/events.ts @@ -74,3 +74,11 @@ const any: any = 1; { new events(); } + +{ + events.once({ + addEventListener(name: string, listener: (res: number) => void, opts: { once: boolean }) { + setTimeout(() => listener(123), 100); + } + }, 'name'); +} diff --git a/types/node/test/fs.ts b/types/node/test/fs.ts new file mode 100644 index 0000000000..ea3fb7d10a --- /dev/null +++ b/types/node/test/fs.ts @@ -0,0 +1,299 @@ +import * as fs from 'fs'; +import * as assert from 'assert'; +import * as util from 'util'; + +{ + fs.writeFile("thebible.txt", + "Do unto others as you would have them do unto you.", + assert.ifError); + + fs.write(1234, "test", () => { }); + + fs.writeFile("Harry Potter", + "\"You be wizzing, Harry,\" jived Dumbledore.", + { + encoding: "ascii" + }, + assert.ifError); + + fs.writeFile("testfile", "content", "utf8", assert.ifError); + + fs.writeFileSync("testfile", "content", "utf8"); + fs.writeFileSync("testfile", "content", { encoding: "utf8" }); + fs.writeFileSync("testfile", new DataView(new ArrayBuffer(1)), { encoding: "utf8" }); +} + +{ + fs.appendFile("testfile", "foobar", "utf8", assert.ifError); + fs.appendFile("testfile", "foobar", { encoding: "utf8" }, assert.ifError); + fs.appendFileSync("testfile", "foobar", "utf8"); + fs.appendFileSync("testfile", "foobar", { encoding: "utf8" }); +} + +{ + let content: string; + let buffer: Buffer; + let stringOrBuffer: string | Buffer; + const nullEncoding: string | null = null; + const stringEncoding: string | null = 'utf8'; + + content = fs.readFileSync('testfile', 'utf8'); + content = fs.readFileSync('testfile', { encoding: 'utf8' }); + stringOrBuffer = fs.readFileSync('testfile', stringEncoding); + stringOrBuffer = fs.readFileSync('testfile', { encoding: stringEncoding }); + + buffer = fs.readFileSync('testfile'); + buffer = fs.readFileSync('testfile', null); + buffer = fs.readFileSync('testfile', { encoding: null }); + stringOrBuffer = fs.readFileSync('testfile', nullEncoding); + stringOrBuffer = fs.readFileSync('testfile', { encoding: nullEncoding }); + + buffer = fs.readFileSync('testfile', { flag: 'r' }); + + fs.readFile('testfile', 'utf8', (err, data) => content = data); + fs.readFile('testfile', { encoding: 'utf8' }, (err, data) => content = data); + fs.readFile('testfile', stringEncoding, (err, data) => stringOrBuffer = data); + fs.readFile('testfile', { encoding: stringEncoding }, (err, data) => stringOrBuffer = data); + + fs.readFile('testfile', (err, data) => buffer = data); + fs.readFile('testfile', null, (err, data) => buffer = data); + fs.readFile('testfile', { encoding: null }, (err, data) => buffer = data); + fs.readFile('testfile', nullEncoding, (err, data) => stringOrBuffer = data); + fs.readFile('testfile', { encoding: nullEncoding }, (err, data) => stringOrBuffer = data); + + fs.readFile('testfile', { flag: 'r' }, (err, data) => buffer = data); +} + +{ + fs.read(1, new DataView(new ArrayBuffer(1)), 0, 1, 0, (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: DataView) => {}); +} + +{ + fs.readSync(1, new DataView(new ArrayBuffer(1)), 0, 1, 0); +} + +{ + let errno: number; + fs.readFile('testfile', (err, data) => { + if (err && err.errno) { + errno = err.errno; + } + }); +} + +{ + let listS: string[]; + listS = fs.readdirSync('path'); + listS = fs.readdirSync('path', { encoding: 'utf8' }); + listS = fs.readdirSync('path', { encoding: null }); + listS = fs.readdirSync('path', { encoding: undefined }) as string[]; + listS = fs.readdirSync('path', 'utf8'); + listS = fs.readdirSync('path', null); + listS = fs.readdirSync('path', undefined); + const listDir: fs.Dirent[] = fs.readdirSync('path', { withFileTypes: true }); + const listDir2: Buffer[] = fs.readdirSync('path', { withFileTypes: false, encoding: 'buffer' }); + const listDir3: fs.Dirent[] = fs.readdirSync('path', { encoding: 'utf8', withFileTypes: true }); + + let listB: Buffer[]; + listB = fs.readdirSync('path', { encoding: 'buffer' }); + listB = fs.readdirSync("path", 'buffer'); + + const enc = 'buffer'; + fs.readdirSync('path', { encoding: enc }); + fs.readdirSync('path', { }); + + fs.readdir('path', { withFileTypes: true }, (err: NodeJS.ErrnoException | null, files: fs.Dirent[]) => {}); +} + +async function testPromisify() { + const rd = util.promisify(fs.readdir); + let listS: string[]; + listS = await rd('path'); + listS = await rd('path', 'utf8'); + listS = await rd('path', null); + listS = await rd('path', undefined); + listS = await rd('path', { encoding: 'utf8' }); + listS = await rd('path', { encoding: null }); + listS = await rd('path', { encoding: null, withFileTypes: false }); + listS = await rd('path', { encoding: 'utf8', withFileTypes: false }); + const listDir: fs.Dirent[] = await rd('path', { withFileTypes: true }); + const listDir2: Buffer[] = await rd('path', { withFileTypes: false, encoding: 'buffer' }); + const listDir3: fs.Dirent[] = await rd('path', { encoding: 'utf8', withFileTypes: true }); +} + +{ + fs.mkdtemp('/tmp/foo-', (err, folder) => { + console.log(folder); + // Prints: /tmp/foo-itXde2 + }); +} + +{ + let tempDir: string; + tempDir = fs.mkdtempSync('/tmp/foo-'); +} + +{ + fs.watch('/tmp/foo-', (event, filename) => { + console.log(event, filename); + }); + + fs.watch('/tmp/foo-', 'utf8', (event, filename) => { + console.log(event, filename); + }); + + fs.watch('/tmp/foo-', { + recursive: true, + persistent: true, + encoding: 'utf8' + }, (event, filename) => { + console.log(event, filename); + }); +} + +{ + fs.access('/path/to/folder', (err) => { }); + + fs.access(Buffer.from(''), (err) => { }); + + fs.access('/path/to/folder', fs.constants.F_OK | fs.constants.R_OK, (err) => { }); + + fs.access(Buffer.from(''), fs.constants.F_OK | fs.constants.R_OK, (err) => { }); + + fs.accessSync('/path/to/folder'); + + fs.accessSync(Buffer.from('')); + + fs.accessSync('path/to/folder', fs.constants.W_OK | fs.constants.X_OK); + + fs.accessSync(Buffer.from(''), fs.constants.W_OK | fs.constants.X_OK); +} + +{ + let s = '123'; + let b: Buffer; + fs.readlink('/path/to/folder', (err, linkString) => s = linkString); + fs.readlink('/path/to/folder', undefined, (err, linkString) => s = linkString); + fs.readlink('/path/to/folder', 'utf8', (err, linkString) => s = linkString); + fs.readlink('/path/to/folder', 'buffer', (err, linkString) => b = linkString); + fs.readlink('/path/to/folder', s, (err, linkString) => typeof linkString === 'string' ? s = linkString : b = linkString); + fs.readlink('/path/to/folder', {}, (err, linkString) => s = linkString); + fs.readlink('/path/to/folder', { encoding: undefined }, (err, linkString) => s = linkString); + fs.readlink('/path/to/folder', { encoding: 'utf8' }, (err, linkString) => s = linkString); + fs.readlink('/path/to/folder', { encoding: 'buffer' }, (err, linkString) => b = linkString); + fs.readlink('/path/to/folder', { encoding: s }, (err, linkString) => typeof linkString === "string" ? s = linkString : b = linkString); + + s = fs.readlinkSync('/path/to/folder'); + s = fs.readlinkSync('/path/to/folder', undefined); + s = fs.readlinkSync('/path/to/folder', 'utf8'); + b = fs.readlinkSync('/path/to/folder', 'buffer'); + const v1 = fs.readlinkSync('/path/to/folder', s); + typeof v1 === "string" ? s = v1 : b = v1; + + s = fs.readlinkSync('/path/to/folder', {}); + s = fs.readlinkSync('/path/to/folder', { encoding: undefined }); + s = fs.readlinkSync('/path/to/folder', { encoding: 'utf8' }); + b = fs.readlinkSync('/path/to/folder', { encoding: 'buffer' }); + const v2 = fs.readlinkSync('/path/to/folder', { encoding: s }); + typeof v2 === "string" ? s = v2 : b = v2; +} + +{ + let s = '123'; + let b: Buffer; + fs.realpath('/path/to/folder', (err, resolvedPath) => s = resolvedPath); + fs.realpath('/path/to/folder', undefined, (err, resolvedPath) => s = resolvedPath); + fs.realpath('/path/to/folder', 'utf8', (err, resolvedPath) => s = resolvedPath); + fs.realpath('/path/to/folder', 'buffer', (err, resolvedPath) => b = resolvedPath); + fs.realpath('/path/to/folder', s, (err, resolvedPath) => typeof resolvedPath === 'string' ? s = resolvedPath : b = resolvedPath); + fs.realpath('/path/to/folder', {}, (err, resolvedPath) => s = resolvedPath); + fs.realpath('/path/to/folder', { encoding: undefined }, (err, resolvedPath) => s = resolvedPath); + fs.realpath('/path/to/folder', { encoding: 'utf8' }, (err, resolvedPath) => s = resolvedPath); + fs.realpath('/path/to/folder', { encoding: 'buffer' }, (err, resolvedPath) => b = resolvedPath); + fs.realpath('/path/to/folder', { encoding: s }, (err, resolvedPath) => typeof resolvedPath === "string" ? s = resolvedPath : b = resolvedPath); + + s = fs.realpathSync('/path/to/folder'); + s = fs.realpathSync('/path/to/folder', undefined); + s = fs.realpathSync('/path/to/folder', 'utf8'); + b = fs.realpathSync('/path/to/folder', 'buffer'); + const v1 = fs.realpathSync('/path/to/folder', s); + typeof v1 === "string" ? s = v1 : b = v1; + + s = fs.realpathSync('/path/to/folder', {}); + s = fs.realpathSync('/path/to/folder', { encoding: undefined }); + s = fs.realpathSync('/path/to/folder', { encoding: 'utf8' }); + b = fs.realpathSync('/path/to/folder', { encoding: 'buffer' }); + const v2 = fs.realpathSync('/path/to/folder', { encoding: s }); + typeof v2 === "string" ? s = v2 : b = v2; + + // native + fs.realpath.native('/path/to/folder', (err, resolvedPath) => s = resolvedPath); + fs.realpath.native('/path/to/folder', undefined, (err, resolvedPath) => s = resolvedPath); + fs.realpath.native('/path/to/folder', 'utf8', (err, resolvedPath) => s = resolvedPath); + fs.realpath.native('/path/to/folder', 'buffer', (err, resolvedPath) => b = resolvedPath); + fs.realpath.native('/path/to/folder', s, (err, resolvedPath) => typeof resolvedPath === 'string' ? s = resolvedPath : b = resolvedPath); + fs.realpath.native('/path/to/folder', {}, (err, resolvedPath) => s = resolvedPath); + fs.realpath.native('/path/to/folder', { encoding: undefined }, (err, resolvedPath) => s = resolvedPath); + fs.realpath.native('/path/to/folder', { encoding: 'utf8' }, (err, resolvedPath) => s = resolvedPath); + fs.realpath.native('/path/to/folder', { encoding: 'buffer' }, (err, resolvedPath) => b = resolvedPath); + fs.realpath.native('/path/to/folder', { encoding: s }, (err, resolvedPath) => typeof resolvedPath === "string" ? s = resolvedPath : b = resolvedPath); + + s = fs.realpathSync.native('/path/to/folder'); + s = fs.realpathSync.native('/path/to/folder', undefined); + s = fs.realpathSync.native('/path/to/folder', 'utf8'); + b = fs.realpathSync.native('/path/to/folder', 'buffer'); + const v3 = fs.realpathSync.native('/path/to/folder', s); + typeof v3 === "string" ? s = v3 : b = v3; + + s = fs.realpathSync.native('/path/to/folder', {}); + s = fs.realpathSync.native('/path/to/folder', { encoding: undefined }); + s = fs.realpathSync.native('/path/to/folder', { encoding: 'utf8' }); + b = fs.realpathSync.native('/path/to/folder', { encoding: 'buffer' }); + const v4 = fs.realpathSync.native('/path/to/folder', { encoding: s }); + typeof v4 === "string" ? s = v4 : b = v4; +} + +{ + fs.copyFile('/path/to/src', '/path/to/dest', (err) => console.error(err)); + fs.copyFile('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_EXCL, (err) => console.error(err)); + fs.copyFile('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_FICLONE, (err) => console.error(err)); + fs.copyFile('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_FICLONE_FORCE, (err) => console.error(err)); + + fs.copyFileSync('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_EXCL); + fs.copyFileSync('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_FICLONE); + fs.copyFileSync('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_FICLONE_FORCE); + + const cf = util.promisify(fs.copyFile); + cf('/path/to/src', '/path/to/dest', fs.constants.COPYFILE_EXCL).then(console.log); +} + +{ + fs.mkdir('some/test/path', { + recursive: true, + mode: 0o777, + }, () => { + }); + + fs.mkdirSync('some/test/path', { + recursive: true, + mode: 0o777, + }); +} + +{ + let names: Promise; + let buffers: Promise; + let namesOrBuffers: Promise; + let entries: Promise; + + names = fs.promises.readdir('/path/to/dir', { encoding: 'utf8', withFileTypes: false }); + buffers = fs.promises.readdir('/path/to/dir', { encoding: 'buffer', withFileTypes: false }); + namesOrBuffers = fs.promises.readdir('/path/to/dir', { encoding: 'SOME OTHER', withFileTypes: false }); + entries = fs.promises.readdir('/path/to/dir', { encoding: 'utf8', withFileTypes: true }); +} + +{ + fs.writev(1, [Buffer.from('123')], (err: NodeJS.ErrnoException | null, bytesWritten: number, buffers: NodeJS.ArrayBufferView[]) => { + }); + const bytesWritten = fs.writevSync(1, [Buffer.from('123')]); +} diff --git a/types/node/test/util.ts b/types/node/test/util.ts index eb454194bc..4c34279af6 100644 --- a/types/node/test/util.ts +++ b/types/node/test/util.ts @@ -162,6 +162,8 @@ import { readFile } from 'fs'; const teEncoding: string = te.encoding; const teEncodeRes: Uint8Array = te.encode("TextEncoder"); + const encIntoRes: util.EncodeIntoResult = te.encodeInto('asdf', new Uint8Array(16)); + // util.types let b: boolean; b = util.types.isBigInt64Array(15); diff --git a/types/node/ts3.2/fs.d.ts b/types/node/ts3.2/fs.d.ts new file mode 100644 index 0000000000..0a9eae0761 --- /dev/null +++ b/types/node/ts3.2/fs.d.ts @@ -0,0 +1,33 @@ +// tslint:disable-next-line:no-bad-reference +/// + +declare module 'fs' { + interface BigIntStats extends StatsBase { + } + + class BigIntStats { + atimeNs: BigInt; + mtimeNs: BigInt; + ctimeNs: BigInt; + birthtimeNs: BigInt; + } + + interface BigIntOptions { + bigint: true; + } + + interface StatOptions { + bigint: boolean; + } + + function stat(path: PathLike, options: BigIntOptions, callback: (err: NodeJS.ErrnoException | null, stats: BigIntStats) => void): void; + function stat(path: PathLike, options: StatOptions, callback: (err: NodeJS.ErrnoException | null, stats: Stats | BigIntStats) => void): void; + + namespace stat { + function __promisify__(path: PathLike, options: BigIntOptions): Promise; + function __promisify__(path: PathLike, options: StatOptions): Promise; + } + + function statSync(path: PathLike, options: BigIntOptions): BigIntStats; + function statSync(path: PathLike, options: StatOptions): Stats | BigIntStats; +} diff --git a/types/node/ts3.2/index.d.ts b/types/node/ts3.2/index.d.ts index ce7709bc7c..ee076935ef 100644 --- a/types/node/ts3.2/index.d.ts +++ b/types/node/ts3.2/index.d.ts @@ -16,5 +16,6 @@ /// // TypeScript 3.2-specific augmentations: +/// /// /// diff --git a/types/node/ts3.2/node-tests.ts b/types/node/ts3.2/node-tests.ts index a95a4b55df..bf87f3a0b5 100644 --- a/types/node/ts3.2/node-tests.ts +++ b/types/node/ts3.2/node-tests.ts @@ -25,6 +25,7 @@ import '../worker_threads'; import '../zlib'; import { types } from 'util'; +import { BigIntStats, statSync, Stats } from 'fs'; ////////////////////////////////////////////////////////// /// Global Tests : https://nodejs.org/api/global.html /// @@ -50,6 +51,12 @@ import { types } from 'util'; } } +// FS Tests +{ + const bigStats: BigIntStats = statSync('.', { bigint: true }); + const anyStats: Stats | BigIntStats = statSync('.', { bigint: Math.random() > 0.5 }); +} + // Global Tests { diff --git a/types/node/tsconfig.json b/types/node/tsconfig.json index f04d5defaf..599d5f37aa 100644 --- a/types/node/tsconfig.json +++ b/types/node/tsconfig.json @@ -51,6 +51,7 @@ "test/crypto.ts", "test/dgram.ts", "test/events.ts", + "test/fs.ts", "test/global.ts", "test/http.ts", "test/http2.ts", diff --git a/types/node/util.d.ts b/types/node/util.d.ts index 0e7a1c6eed..95131cb28d 100644 --- a/types/node/util.d.ts +++ b/types/node/util.d.ts @@ -153,13 +153,26 @@ declare module "util" { options?: { fatal?: boolean; ignoreBOM?: boolean } ); decode( - input?: NodeJS.TypedArray | DataView | ArrayBuffer | null, + input?: NodeJS.ArrayBufferView | ArrayBuffer | null, options?: { stream?: boolean } ): string; } + interface EncodeIntoResult { + /** + * The read Unicode code units of input. + */ + + read: number; + /** + * The written UTF-8 bytes of output. + */ + written: number; + } + class TextEncoder { readonly encoding: string; encode(input?: string): Uint8Array; + encodeInto(input: string, output: Uint8Array): EncodeIntoResult; } } diff --git a/types/node/v8.d.ts b/types/node/v8.d.ts index fdecdb625b..2e2706e206 100644 --- a/types/node/v8.d.ts +++ b/types/node/v8.d.ts @@ -26,6 +26,18 @@ declare module "v8" { number_of_detached_contexts: number; } + interface HeapCodeStatistics { + code_and_metadata_size: number; + bytecode_and_metadata_size: number; + external_script_source_size: number; + } + + /** + * Returns an integer representing a "version tag" derived from the V8 version, command line flags and detected CPU features. + * This is useful for determining whether a vm.Script cachedData buffer is compatible with this instance of V8. + */ + function cachedDataVersionTag(): number; + function getHeapStatistics(): HeapInfo; function getHeapSpaceStatistics(): HeapSpaceInfo[]; function setFlagsFromString(flags: string): void; @@ -49,4 +61,137 @@ declare module "v8" { * the main Node.js thread or the id of a worker thread. */ function writeHeapSnapshot(fileName?: string): string; + + function getHeapCodeStatistics(): HeapCodeStatistics; + + /** + * @experimental + */ + class Serializer { + /** + * Writes out a header, which includes the serialization format version. + */ + writeHeader(): void; + + /** + * Serializes a JavaScript value and adds the serialized representation to the internal buffer. + * This throws an error if value cannot be serialized. + */ + writeValue(val: any): boolean; + + /** + * Returns the stored internal buffer. + * This serializer should not be used once the buffer is released. + * Calling this method results in undefined behavior if a previous write has failed. + */ + releaseBuffer(): Buffer; + + /** + * Marks an ArrayBuffer as having its contents transferred out of band.\ + * Pass the corresponding ArrayBuffer in the deserializing context to deserializer.transferArrayBuffer(). + */ + transferArrayBuffer(id: number, arrayBuffer: ArrayBuffer): void; + + /** + * Write a raw 32-bit unsigned integer. + */ + writeUint32(value: number): void; + + /** + * Write a raw 64-bit unsigned integer, split into high and low 32-bit parts. + */ + writeUint64(hi: number, lo: number): void; + + /** + * Write a JS number value. + */ + writeDouble(value: number): void; + + /** + * Write raw bytes into the serializer’s internal buffer. + * The deserializer will require a way to compute the length of the buffer. + */ + writeRawBytes(buffer: NodeJS.TypedArray): void; + } + + /** + * A subclass of `Serializer` that serializes `TypedArray` (in particular `Buffer`) and `DataView` objects as host objects, + * and only stores the part of their underlying `ArrayBuffers` that they are referring to. + * @experimental + */ + class DefaultSerializer extends Serializer { + } + + /** + * @experimental + */ + class Deserializer { + constructor(data: NodeJS.TypedArray); + /** + * Reads and validates a header (including the format version). + * May, for example, reject an invalid or unsupported wire format. + * In that case, an Error is thrown. + */ + readHeader(): boolean; + + /** + * Deserializes a JavaScript value from the buffer and returns it. + */ + readValue(): any; + + /** + * Marks an ArrayBuffer as having its contents transferred out of band. + * Pass the corresponding `ArrayBuffer` in the serializing context to serializer.transferArrayBuffer() + * (or return the id from serializer._getSharedArrayBufferId() in the case of SharedArrayBuffers). + */ + transferArrayBuffer(id: number, arrayBuffer: ArrayBuffer): void; + + /** + * Reads the underlying wire format version. + * Likely mostly to be useful to legacy code reading old wire format versions. + * May not be called before .readHeader(). + */ + getWireFormatVersion(): number; + + /** + * Read a raw 32-bit unsigned integer and return it. + */ + readUint32(): number; + + /** + * Read a raw 64-bit unsigned integer and return it as an array [hi, lo] with two 32-bit unsigned integer entries. + */ + readUint64(): [number, number]; + + /** + * Read a JS number value. + */ + readDouble(): number; + + /** + * Read raw bytes from the deserializer’s internal buffer. + * The length parameter must correspond to the length of the buffer that was passed to serializer.writeRawBytes(). + */ + readRawBytes(length: number): Buffer; + } + + /** + * A subclass of `Serializer` that serializes `TypedArray` (in particular `Buffer`) and `DataView` objects as host objects, + * and only stores the part of their underlying `ArrayBuffers` that they are referring to. + * @experimental + */ + class DefaultDeserializer extends Deserializer { + } + + /** + * Uses a `DefaultSerializer` to serialize value into a buffer. + * @experimental + */ + function serialize(value: any): Buffer; + + /** + * Uses a `DefaultDeserializer` with default options to read a JS value from a buffer. + * @experimental + */ + function deserialize(data: NodeJS.TypedArray): any; } diff --git a/types/node/v9/tsconfig.json b/types/node/v9/tsconfig.json index 8752b2560b..8aacc7d666 100644 --- a/types/node/v9/tsconfig.json +++ b/types/node/v9/tsconfig.json @@ -26,4 +26,4 @@ "noEmit": true, "forceConsistentCasingInFileNames": true } -} \ No newline at end of file +} diff --git a/types/node/zlib.d.ts b/types/node/zlib.d.ts index b45721a146..a03e900c10 100644 --- a/types/node/zlib.d.ts +++ b/types/node/zlib.d.ts @@ -18,7 +18,7 @@ declare module "zlib" { level?: number; // compression only memLevel?: number; // compression only strategy?: number; // compression only - dictionary?: NodeJS.TypedArray | DataView | ArrayBuffer; // deflate/inflate only, empty dictionary by default + dictionary?: NodeJS.ArrayBufferView | ArrayBuffer; // deflate/inflate only, empty dictionary by default } interface BrotliOptions { @@ -79,7 +79,7 @@ declare module "zlib" { function createInflateRaw(options?: ZlibOptions): InflateRaw; function createUnzip(options?: ZlibOptions): Unzip; - type InputType = string | DataView | ArrayBuffer | NodeJS.TypedArray; + type InputType = string | ArrayBuffer | NodeJS.ArrayBufferView; type CompressCallback = (error: Error | null, result: Buffer) => void; diff --git a/types/readable-stream/tsconfig.json b/types/readable-stream/tsconfig.json index 0c769a8761..5113653857 100644 --- a/types/readable-stream/tsconfig.json +++ b/types/readable-stream/tsconfig.json @@ -20,4 +20,4 @@ "index.d.ts", "readable-stream-tests.ts" ] -} \ No newline at end of file +}