Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions types/node/buffer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,34 @@ declare module 'buffer' {
*/
text(): Promise<string>;
}

/**
* Decodes a string of Base64-encoded data into bytes, and encodes those bytes into a string using Latin-1 (ISO-8859-1).
*
* This function is only provided for compatibility with legacy web platform APIs
* and should never be used in new code, because they use strings to represent
* binary data and predate the introduction of typed arrays in JavaScript.
* For code running using Node.js APIs, converting between base64-encoded strings
* and binary data should be performed using `Buffer.from(str, 'base64')` and
* `buf.toString('base64')`.
*
* @deprecated dom compatibility
*/
export function atob(input: string): string;

/**
* Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes into a string using Base64.
*
* This function is only provided for compatibility with legacy web platform APIs
* and should never be used in new code, because they use strings to represent
* binary data and predate the introduction of typed arrays in JavaScript.
* For code running using Node.js APIs, converting between base64-encoded strings
* and binary data should be performed using `Buffer.from(str, 'base64')` and
* `buf.toString('base64')`.
*
* @deprecated dom compatibility
*/
export function btoa(input: string): string;
}

declare module 'node:buffer' {
Expand Down
21 changes: 12 additions & 9 deletions types/node/child_process.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ declare module 'child_process' {
* @default 'SIGTERM'
*/
killSignal?: NodeJS.Signals | number;

/**
* In milliseconds the maximum amount of time the process is allowed to run.
*/
timeout?: number;
}

interface ProcessEnvOptions {
Expand Down Expand Up @@ -483,14 +488,17 @@ declare module 'child_process' {
function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns<Buffer>;
function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptions): SpawnSyncReturns<Buffer>;

interface ExecSyncOptions extends CommonOptions {
input?: string | Uint8Array;
interface CommonExecOptions extends ProcessEnvOptions {
input?: string | NodeJS.ArrayBufferView;
stdio?: StdioOptions;
shell?: string;
killSignal?: NodeJS.Signals | number;
maxBuffer?: number;
encoding?: BufferEncoding | 'buffer' | null;
}

interface ExecSyncOptions extends CommonExecOptions {
shell?: string;
}
interface ExecSyncOptionsWithStringEncoding extends ExecSyncOptions {
encoding: BufferEncoding;
}
Expand All @@ -502,12 +510,7 @@ declare module 'child_process' {
function execSync(command: string, options?: ExecSyncOptionsWithBufferEncoding): Buffer;
function execSync(command: string, options?: ExecSyncOptions): Buffer;

interface ExecFileSyncOptions extends CommonOptions {
input?: string | NodeJS.ArrayBufferView;
stdio?: StdioOptions;
killSignal?: NodeJS.Signals | number;
maxBuffer?: number;
encoding?: BufferEncoding;
interface ExecFileSyncOptions extends CommonExecOptions {
shell?: boolean | string;
}
interface ExecFileSyncOptionsWithStringEncoding extends ExecFileSyncOptions {
Expand Down
17 changes: 13 additions & 4 deletions types/node/crypto.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,9 @@ declare module 'crypto' {
cipher?: string;
passphrase?: string | Buffer;
}

interface JwkKeyExportOptions {
format: 'jwk';
}

interface JsonWebKey {
crv?: string;
d?: string;
Expand All @@ -215,6 +213,7 @@ declare module 'crypto' {
qi?: string;
x?: string;
y?: string;
[key: string]: unknown;
}

interface AsymmetricKeyDetails {
Expand All @@ -236,6 +235,10 @@ declare module 'crypto' {
namedCurve?: string;
}

interface JwkKeyExportOptions {
format: 'jwk';
}

class KeyObject {
private constructor();
asymmetricKeyType?: KeyType;
Expand All @@ -253,6 +256,7 @@ declare module 'crypto' {
asymmetricKeyDetails?: AsymmetricKeyDetails;
export(options: KeyExportOptions<'pem'>): string | Buffer;
export(options?: KeyExportOptions<'der'>): Buffer;
export(options?: JwkKeyExportOptions): JsonWebKey;
symmetricKeySize?: number;
type: KeyObjectType;
}
Expand Down Expand Up @@ -378,8 +382,13 @@ declare module 'crypto' {

function generateKey(type: 'hmac' | 'aes', options: {length: number}, callback: (err: Error | null, key: KeyObject) => void): void;

function createPrivateKey(key: PrivateKeyInput | string | Buffer): KeyObject;
function createPublicKey(key: PublicKeyInput | string | Buffer | KeyObject): KeyObject;
interface JsonWebKeyInput {
key: JsonWebKey;
format: 'jwk';
}

function createPrivateKey(key: PrivateKeyInput | string | Buffer | JsonWebKeyInput): KeyObject;
function createPublicKey(key: PublicKeyInput | string | Buffer | KeyObject | JsonWebKeyInput): KeyObject;
function createSecretKey(key: NodeJS.ArrayBufferView): KeyObject;

function createSign(algorithm: string, options?: stream.WritableOptions): Signer;
Expand Down
7 changes: 6 additions & 1 deletion types/node/fs/promises.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare module 'fs/promises' {
import { Abortable } from 'events';
import { Stream } from 'stream';
import {
Stats,
BigIntStats,
Expand Down Expand Up @@ -512,7 +513,11 @@ declare module 'fs/promises' {
* 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 | FileHandle, data: string | Uint8Array, options?: BaseEncodingOptions & { mode?: Mode, flag?: OpenMode } & Abortable | BufferEncoding | null): Promise<void>;
function writeFile(
path: PathLike | FileHandle,
data: string | NodeJS.ArrayBufferView | Iterable<string | NodeJS.ArrayBufferView> | AsyncIterable<string | NodeJS.ArrayBufferView> | Stream,
options?: BaseEncodingOptions & { mode?: Mode, flag?: OpenMode } & Abortable | BufferEncoding | null
): Promise<void>;

/**
* Asynchronously append data to a file, creating the file if it does not exist.
Expand Down
5 changes: 5 additions & 0 deletions types/node/http.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ declare module 'http' {
setTimeout(timeout: number, callback?: () => void): this;
setNoDelay(noDelay?: boolean): void;
setSocketKeepAlive(enable?: boolean, initialDelay?: number): void;
/**
* Returns an array containing the unique names of the current outgoing raw headers.
* Header names are returned with their exact casing being set.
*/
getRawHeaderNames(): string[];

addListener(event: 'abort', listener: () => void): this;
addListener(event: 'connect', listener: (response: IncomingMessage, socket: Socket, head: Buffer) => void): this;
Expand Down
2 changes: 1 addition & 1 deletion types/node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Type definitions for non-npm package Node.js 15.12
// Type definitions for non-npm package Node.js 15.14
// Project: http://nodejs.org/
// Definitions by: Microsoft TypeScript <https://github.com/Microsoft>
// DefinitelyTyped <https://github.com/DefinitelyTyped>
Expand Down
38 changes: 38 additions & 0 deletions types/node/net.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ declare module 'net' {
* @param type Either 'ipv4' or 'ipv6'. Default: 'ipv4'.
*/
addAddress(address: string, type?: IPVersion): void;
addAddress(address: SocketAddress): void;

/**
* Adds a rule to block a range of IP addresses from start (inclusive) to end (inclusive).
Expand All @@ -282,6 +283,7 @@ declare module 'net' {
* @param type Either 'ipv4' or 'ipv6'. Default: 'ipv4'.
*/
addRange(start: string, end: string, type?: IPVersion): void;
addRange(start: SocketAddress, end: SocketAddress): void;

/**
* Adds a rule to block a range of IP addresses specified as a subnet mask.
Expand All @@ -291,6 +293,7 @@ declare module 'net' {
* For IPv4, this must be a value between 0 and 32. For IPv6, this must be between 0 and 128.
* @param type Either 'ipv4' or 'ipv6'. Default: 'ipv4'.
*/
addSubnet(net: SocketAddress, prefix: number): void;
addSubnet(net: string, prefix: number, type?: IPVersion): void;

/**
Expand All @@ -299,6 +302,7 @@ declare module 'net' {
* @param address The IP address to check
* @param type Either 'ipv4' or 'ipv6'. Default: 'ipv4'.
*/
check(address: SocketAddress): boolean;
check(address: string, type?: IPVersion): boolean;
}

Expand All @@ -323,4 +327,38 @@ declare module 'net' {
function isIP(input: string): number;
function isIPv4(input: string): boolean;
function isIPv6(input: string): boolean;

interface SocketAddressInitOptions {
/**
* The network address as either an IPv4 or IPv6 string.
* @default 127.0.0.1
*/
address?: string;
/**
* @default `'ipv4'`
*/
family?: IPVersion;
/**
* An IPv6 flow-label used only if `family` is `'ipv6'`.
* @default 0
*/
flowlabel?: number;
/**
* An IP port.
* @default 0
*/
port?: number;
}

// TODO: Mark as clonable if `kClone` symbol is set in node.
/**
* Immutable socket address.
*/
class SocketAddress {
constructor(options: SocketAddressInitOptions);
readonly address: string;
readonly family: IPVersion;
readonly port: number;
readonly flowlabel: number;
}
}
4 changes: 4 additions & 0 deletions types/node/test/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,7 @@ async () => {
blob.slice(1, 2); // $ExpectType Blob
blob.slice(1, 2, 'other'); // $ExpectType Blob
};

{
atob(btoa('test')); // $ExpectType string
}
5 changes: 3 additions & 2 deletions types/node/test/child_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Writable, Readable, Pipe } from 'stream';
childProcess.exec("echo test");
childProcess.exec("echo test", { windowsHide: true });
childProcess.spawn("echo");
childProcess.spawn("echo", { windowsHide: true, signal: new AbortSignal(), killSignal: "SIGABRT" });
childProcess.spawn("echo", { windowsHide: true, signal: new AbortSignal(), killSignal: "SIGABRT", timeout: 123 });
childProcess.spawn("echo", ["test"], { windowsHide: true });
childProcess.spawn("echo", ["test"], { windowsHide: true, argv0: "echo-test" });
childProcess.spawn("echo", ["test"], { stdio: [0xdeadbeef, "inherit", undefined, "pipe"] });
Expand Down Expand Up @@ -55,7 +55,8 @@ import { Writable, Readable, Pipe } from 'stream';
execPath: '',
execArgv: ['asda'],
signal: new AbortSignal(),
killSignal: "SIGABRT"
killSignal: "SIGABRT",
timeout: 123,
});
const ipc: Pipe = forked.channel!;
const hasRef: boolean = ipc.hasRef();
Expand Down
84 changes: 84 additions & 0 deletions types/node/test/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@ import { promisify } from 'util';
}

{
const callback = (error: Error | null, signature: Buffer): void => {};
const key = crypto.createPrivateKey('pkey');
crypto.sign('sha256', Buffer.from('asd'), {
key: Buffer.from('keylike'),
Expand All @@ -942,13 +943,22 @@ import { promisify } from 'util';
key,
dsaEncoding: 'der',
});
crypto.sign('sha256', Buffer.from('asd'), {
key,
dsaEncoding: 'der',
}, callback);
promisify(crypto.sign)('sha256', Buffer.from('asd'), {
key,
dsaEncoding: 'der',
}).then((signature: Buffer) => {});
crypto.createSign('sha256').update(Buffer.from('asd')).sign({
key,
dsaEncoding: 'der',
});
}

{
const callback = (error: Error | null, result: boolean): void => {};
const key = crypto.createPublicKey('pkey');
crypto.verify(
'sha256',
Expand Down Expand Up @@ -978,6 +988,25 @@ import { promisify } from 'util';
},
Buffer.from('sig'),
);
crypto.verify(
'sha256',
Buffer.from('asd'),
{
key,
dsaEncoding: 'der',
},
Buffer.from('sig'),
callback
);
promisify(crypto.verify)(
'sha256',
Buffer.from('asd'),
{
key,
dsaEncoding: 'der',
},
Buffer.from('sig'),
).then((result: boolean) => {});
crypto.createVerify('sha256').update(Buffer.from('asd')).verify(
{
key,
Expand Down Expand Up @@ -1106,6 +1135,61 @@ import { promisify } from 'util';
crypto.checkPrimeSync(123n, { checks: 123 }); // $ExpectType boolean
}

{
crypto.generateKeyPair('ec', { namedCurve: 'P-256' }, (err, publicKey, privateKey) => {
for (const keyObject of [publicKey, privateKey]) {
if (keyObject.asymmetricKeyDetails) {
if (keyObject.asymmetricKeyDetails.modulusLength) {
const modulusLength: number = keyObject.asymmetricKeyDetails.modulusLength;
}
if (keyObject.asymmetricKeyDetails.publicExponent) {
const publicExponent: bigint = keyObject.asymmetricKeyDetails.publicExponent;
}
if (keyObject.asymmetricKeyDetails.divisorLength) {
const divisorLength: number = keyObject.asymmetricKeyDetails.divisorLength;
}
if (keyObject.asymmetricKeyDetails.namedCurve) {
const namedCurve: string = keyObject.asymmetricKeyDetails.namedCurve;
}
}
}
});
const secretKeyObject = crypto.createSecretKey(Buffer.from('secret'));
crypto.generateKeyPair('ec', { namedCurve: 'P-256' }, (err, publicKey, privateKey) => {
for (const keyObject of [publicKey, privateKey, secretKeyObject]) {
const jwk = keyObject.export({ format: 'jwk' });
jwk.crv;
jwk.d;
jwk.dp;
jwk.dq;
jwk.e;
jwk.k;
jwk.kty;
jwk.n;
jwk.p;
jwk.q;
jwk.qi;
jwk.x;
jwk.y;
crypto.createPublicKey({ key: jwk, format: 'jwk' });
crypto.createPrivateKey({ key: jwk, format: 'jwk' });
}
});
}

{
const jwk = {
alg: 'ES256',
crv: 'P-256',
kty: 'EC',
x: 'ySK38C1jBdLwDsNWKzzBHqKYEE5Cgv-qjWvorUXk9fw',
y: '_LeQBw07cf5t57Iavn4j-BqJsAD1dpoz8gokd3sBsOo',
key_ops: ['sign'],
};
crypto.createPublicKey({ key: jwk, format: 'jwk' });
crypto.createPrivateKey({ key: jwk, format: 'jwk' });
}

{
crypto.generateKeyPair('ec', { namedCurve: 'P-256' }, (err, publicKey, privateKey) => {
for (const keyObject of [publicKey, privateKey]) {
Expand Down
Loading