Skip to content
VaelJS Blog

node:https

Node-compatible HTTPS (HTTP/1.1 over TLS) client and server APIs (node:https).

Available in vjs only (not available in vjs-lite). Enabled by default unless --no-node-compat is passed.

import ... from "node:https"
Since v0.0.0-alpha.2 vjs (V8)
Cargo feature: node-compat (enabled by default in vjs; absent in vjs-lite; disable with --no-node-compat)
import { createServer } from "node:https";
const server = createServer({ key: "...", cert: "..." }, (_req, res) => {
res.end("secure ok");
});
server.listen(0, "127.0.0.1");
functionSince v0.0.0-alpha.2vjs only
export function createServer(
  options: ServerOptions,
  requestListener?: RequestListener,
): Server;
export function createServer(requestListener?: RequestListener): Server;

Create a new HTTPS Server.

Overload 1 — createServer(options, requestListener?)
NameTypeDescription
optionsServerOptionsTLS certificates and HTTP server configuration.
requestListener optionalRequestListenerOptional "request" event listener.
Server — New HTTPS Server.
Overload 2 — createServer(requestListener?)

Create a new HTTPS Server with an optional "request" listener.

NameTypeDescription
requestListener optionalRequestListenerOptional "request" event listener.
Server — New HTTPS Server.
functionSince v0.0.0-alpha.2vjs only
export function request(
  options: RequestOptions | string | import("node:url").URL,
  callback?: (res: import("node:http").IncomingMessage) => void,
): import("node:http").ClientRequest;
export function request(
  url: string | import("node:url").URL,
  options: RequestOptions,
  callback?: (res: import("node:http").IncomingMessage) => void,
): import("node:http").ClientRequest;

Initiate an HTTPS request using options.

Overload 1 — request(options, callback?)
NameTypeDescription
optionsRequestOptions | string | import("node:url").URLRequest options or URL string.
callback optional(res: import("node:http").IncomingMessage) => voidOptional "response" listener.
import("node:http").ClientRequest — Outgoing ClientRequest.
Overload 2 — request(url, options, callback?)

Initiate an HTTPS request to url with explicit options.

NameTypeDescription
urlstring | import("node:url").URLTarget URL string or URL.
optionsRequestOptionsRequest options merged over url.
callback optional(res: import("node:http").IncomingMessage) => voidOptional "response" listener.
import("node:http").ClientRequest — Outgoing ClientRequest.
functionSince v0.0.0-alpha.2vjs only
export function get(
  options: RequestOptions | string | import("node:url").URL,
  callback?: (res: import("node:http").IncomingMessage) => void,
): import("node:http").ClientRequest;
export function get(
  url: string | import("node:url").URL,
  options: RequestOptions,
  callback?: (res: import("node:http").IncomingMessage) => void,
): import("node:http").ClientRequest;

Convenience wrapper around request() that forces method "GET" and automatically calls .end().

Overload 1 — get(options, callback?)
NameTypeDescription
optionsRequestOptions | string | import("node:url").URLRequest options or URL string.
callback optional(res: import("node:http").IncomingMessage) => voidOptional "response" listener.
import("node:http").ClientRequest — Outgoing ClientRequest.
Overload 2 — get(url, options, callback?)

Convenience "GET" request to url with explicit options.

NameTypeDescription
urlstring | import("node:url").URLTarget URL string or URL.
optionsRequestOptionsRequest options merged over url.
callback optional(res: import("node:http").IncomingMessage) => voidOptional "response" listener.
import("node:http").ClientRequest — Outgoing ClientRequest.
classSince v0.0.0-alpha.2vjs only
export class Agent {
  maxSockets: number;
  maxTotalSockets: number;
  maxFreeSockets: number;
  readonly sockets: Record<string, unknown[]>;
  readonly freeSockets: Record<string, unknown[]>;
  readonly requests: Record<string, import("node:http").ClientRequest[]>;
  constructor(options?: AgentOptions);
  destroy(): void;
  getName(options?: RequestOptions): string;
}

HTTPS connection-pooling agent managing TLS sockets.

Constructor
constructor(options?: AgentOptions);

Construct a new HTTPS Agent.

Parameters
NameTypeDescription
options optionalAgentOptionsOptional pooling and TLS configuration.
Properties
PropertyTypeDescriptionSince
maxSockets numberMaximum concurrent sockets per host key.v0.0.0-alpha.2
maxTotalSockets numberMaximum total concurrent sockets across all hosts.v0.0.0-alpha.2
maxFreeSockets numberMaximum idle keep-alive sockets retained per host key.v0.0.0-alpha.2
sockets readonlyRecord<string, unknown[]>Active sockets keyed by destination string.v0.0.0-alpha.2
freeSockets readonlyRecord<string, unknown[]>Idle keep-alive sockets keyed by destination string.v0.0.0-alpha.2
requests readonlyRecord<string, import("node:http").ClientRequest[]>Queued pending requests keyed by destination string.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
destroy(): void;

Destroy all sockets currently managed by this agent.

Returns
void
methodSince v0.0.0-alpha.2
getName(options?: RequestOptions): string;

Return the canonical pool key string for options.

Parameters
NameTypeDescription
options optionalRequestOptions—
Returns
string
classSince v0.0.0-alpha.2vjs only
export class Server {
  readonly listening: boolean;
  headersTimeout: number;
  keepAliveTimeout: number;
  maxHeadersCount: number | null;
  requestTimeout: number;
  timeout: number;
  constructor(requestListener?: RequestListener);
  constructor(options: ServerOptions, requestListener?: RequestListener);
  listen(port?: number, hostname?: string, listeningListener?: () => void): this;
  listen(port?: number, listeningListener?: () => void): this;
  listen(
      options: { port?: number; host?: string; backlog?: number; path?: string },
      listeningListener?: () => void,
    ): this;
  close(callback?: (err?: Error) => void): this;
  closeAllConnections(): void;
  closeIdleConnections(): void;
  setTimeout(msecs?: number, callback?: () => void): this;
  setSecureContext(options: import("node:tls").SecureContextOptions): void;
  addContext(hostname: string, context: import("node:tls").SecureContextOptions): void;
  address(): { address: string; family: string; port: number } | string | null;
  ref(): this;
  unref(): this;
  on(eventName: string | symbol, listener: (...args: any[]) => void): this;
  once(eventName: string | symbol, listener: (...args: any[]) => void): this;
  off(eventName: string | symbol, listener: (...args: any[]) => void): this;
  emit(eventName: string | symbol, ...args: any[]): boolean;
  [Symbol.asyncDispose](): Promise<void>;
}

HTTPS server accepting TLS connections and dispatching HTTP/1.1 requests.

Constructor
constructor(requestListener?: RequestListener);
constructor(options: ServerOptions, requestListener?: RequestListener);

Construct an HTTPS Server with an optional "request" listener.

Overload 1 — new Server(requestListener?)
NameTypeDescription
requestListener optionalRequestListenerCallback invoked for each HTTPS request.
Overload 2 — new Server(options, requestListener?)

Construct an HTTPS Server with TLS options and optional listener.

NameTypeDescription
optionsServerOptionsTLS certificates and HTTP server configuration.
requestListener optionalRequestListenerCallback invoked for each HTTPS request.
Properties
PropertyTypeDescriptionSince
listening readonlybooleantrue while the server is listening for connections.v0.0.0-alpha.2
headersTimeout numberMaximum milliseconds to wait for incoming headers.v0.0.0-alpha.2
keepAliveTimeout numberKeep-alive timeout in milliseconds.v0.0.0-alpha.2
maxHeadersCount number | nullMaximum number of headers parsed per request.v0.0.0-alpha.2
requestTimeout numberRequest timeout in milliseconds.v0.0.0-alpha.2
timeout numberInactivity socket timeout in milliseconds.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
listen(port?: number, hostname?: string, listeningListener?: () => void): this;
listen(port?: number, listeningListener?: () => void): this;
listen(
  options: { port?: number; host?: string; backlog?: number; path?: string },
  listeningListener?: () => void,
): this;

Start listening for HTTPS connections on port.

Overload 1 — listen(port?, hostname?, listeningListener?)
NameTypeDescription
port optionalnumber—
hostname optionalstring—
listeningListener optional() => void—
this
Overload 2 — listen(port?, listeningListener?)

Start listening on port with optional listener.

NameTypeDescription
port optionalnumber—
listeningListener optional() => void—
this
Overload 3 — listen(options, listeningListener?)

Start listening using explicit options.

NameTypeDescription
options{ port?: number; host?: string; backlog?: number; path?: string }—
listeningListener optional() => void—
this
methodSince v0.0.0-alpha.2
close(callback?: (err?: Error) => void): this;

Stop accepting new connections and close once active requests finish.

Parameters
NameTypeDescription
callback optional(err?: Error) => void—
Returns
this
methodSince v0.0.0-alpha.2
closeAllConnections(): void;

Immediately close all connected sockets.

Returns
void
methodSince v0.0.0-alpha.2
closeIdleConnections(): void;

Close all idle keep-alive sockets.

Returns
void
methodSince v0.0.0-alpha.2
setTimeout(msecs?: number, callback?: () => void): this;

Set default socket inactivity timeout.

Parameters
NameTypeDescription
msecs optionalnumber—
callback optional() => void—
Returns
this
methodSince v0.0.0-alpha.2
setSecureContext(options: import("node:tls").SecureContextOptions): void;

Replace the server’s default TLS credentials and options.

Parameters
NameTypeDescription
optionsimport("node:tls").SecureContextOptions—
Returns
void
methodSince v0.0.0-alpha.2
addContext(hostname: string, context: import("node:tls").SecureContextOptions): void;

Register an SNI hostname-specific SecureContextOptions.

Parameters
NameTypeDescription
hostnamestring—
contextimport("node:tls").SecureContextOptions—
Returns
void
methodSince v0.0.0-alpha.2
address(): { address: string; family: string; port: number } | string | null;

Return bound local address metadata or null.

Returns
{ address: string; family: string; port: number } | string | null
methodSince v0.0.0-alpha.2
ref(): this;

Keep the event loop active while listening.

Returns
this
methodSince v0.0.0-alpha.2
unref(): this;

Allow the event loop to exit if the server is the only active handle.

Returns
this
methodSince v0.0.0-alpha.2
on(eventName: string | symbol, listener: (...args: any[]) => void): this;

Register an event listener.

Parameters
NameTypeDescription
eventNamestring | symbol—
listener(...args: any[]) => void—
Returns
this
methodSince v0.0.0-alpha.2
once(eventName: string | symbol, listener: (...args: any[]) => void): this;

Register a one-time event listener.

Parameters
NameTypeDescription
eventNamestring | symbol—
listener(...args: any[]) => void—
Returns
this
methodSince v0.0.0-alpha.2
off(eventName: string | symbol, listener: (...args: any[]) => void): this;

Remove an event listener.

Parameters
NameTypeDescription
eventNamestring | symbol—
listener(...args: any[]) => void—
Returns
this
methodSince v0.0.0-alpha.2
emit(eventName: string | symbol, ...args: any[]): boolean;

Synchronously emit an event.

Parameters
NameTypeDescription
eventNamestring | symbol—
...argsany[]—
Returns
boolean
methodSince v0.0.0-alpha.2
[Symbol.asyncDispose](): Promise<void>;

Close the server asynchronously when disposed via await using.

Returns
Promise<void>
interfaceSince v0.0.0-alpha.2vjs only
export interface AbortSignalLike {
  readonly aborted: boolean;
  readonly reason?: unknown;
  addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
  removeEventListener?(type: string, listener: (...args: any[]) => void): void;
}

Minimal abort-signal contract accepted by HTTPS request options.

Properties
PropertyTypeDescriptionSince
aborted readonlybooleanWhether the signal has already been aborted.v0.0.0-alpha.2
reason readonly optionalunknownOptional abort reason value.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;

Register an "abort" listener.

Parameters
NameTypeDescription
typestring—
listener(...args: any[]) => void—
options optional{ once?: boolean }—
Returns
void
methodSince v0.0.0-alpha.2
removeEventListener?(type: string, listener: (...args: any[]) => void): void;

Unregister an "abort" listener.

Parameters
NameTypeDescription
typestring—
listener(...args: any[]) => void—
Returns
void
interfaceSince v0.0.0-alpha.2vjs only
export interface RequestOptions {
  protocol?: string | null;
  host?: string | null;
  hostname?: string | null;
  family?: number;
  port?: number | string | null;
  localAddress?: string;
  method?: string;
  path?: string | null;
  headers?: import("node:http").OutgoingHttpHeaders;
  auth?: string | null;
  agent?: Agent | boolean;
  timeout?: number;
  signal?: AbortSignalLike;
  ca?: string | Uint8Array | Array<string | Uint8Array>;
  cert?: string | Uint8Array | Array<string | Uint8Array>;
  key?: string | Uint8Array | Array<string | Uint8Array>;
  passphrase?: string;
  pfx?: string | Uint8Array | Array<string | Uint8Array>;
  ciphers?: string;
  rejectUnauthorized?: boolean;
  servername?: string;
  minVersion?: import("node:tls").SecureVersion;
  maxVersion?: import("node:tls").SecureVersion;
  checkServerIdentity?: typeof import("node:tls").checkServerIdentity;
}

Options accepted by https.request(), https.get(), and https.createServer().

Properties
PropertyTypeDescriptionSince
protocol optionalstring | nullURL protocol scheme ("https:").v0.0.0-alpha.2
host optionalstring | nullTarget host or IP address.v0.0.0-alpha.2
hostname optionalstring | nullTarget hostname (preferred over host).v0.0.0-alpha.2
family optionalnumberIP address family (4 or 6).v0.0.0-alpha.2
port optionalnumber | string | nullTarget TCP port (default 443).v0.0.0-alpha.2
localAddress optionalstringLocal interface IP address to bind.v0.0.0-alpha.2
method optionalstringHTTP method ("GET", "POST", etc.).v0.0.0-alpha.2
path optionalstring | nullRequest path and query string (default "/").v0.0.0-alpha.2
headers optionalimport("node:http").OutgoingHttpHeadersOutgoing HTTP headers.v0.0.0-alpha.2
auth optionalstring | nullBasic authentication "user:password" string.v0.0.0-alpha.2
agent optionalAgent | booleanConnection-pooling Agent, or false to bypass pooling.v0.0.0-alpha.2
timeout optionalnumberSocket inactivity timeout in milliseconds.v0.0.0-alpha.2
signal optionalAbortSignalLikeOptional abort signal cancelling the in-flight request.v0.0.0-alpha.2
ca optionalstring | Uint8Array | Array<string | Uint8Array>Trusted CA certificates (PEM string or bytes).v0.0.0-alpha.2
cert optionalstring | Uint8Array | Array<string | Uint8Array>Client certificate chain in PEM format.v0.0.0-alpha.2
key optionalstring | Uint8Array | Array<string | Uint8Array>Client private key in PEM format.v0.0.0-alpha.2
passphrase optionalstringPassphrase protecting private keys.v0.0.0-alpha.2
pfx optionalstring | Uint8Array | Array<string | Uint8Array>PKCS#12 certificate/key bundle.v0.0.0-alpha.2
ciphers optionalstringCipher suite specification string.v0.0.0-alpha.2
rejectUnauthorized optionalbooleanReject invalid server certificates (default true).v0.0.0-alpha.2
servername optionalstringSNI server name for TLS handshake and verification.v0.0.0-alpha.2
minVersion optionalimport("node:tls").SecureVersionMinimum allowed TLS protocol version.v0.0.0-alpha.2
maxVersion optionalimport("node:tls").SecureVersionMaximum allowed TLS protocol version.v0.0.0-alpha.2
checkServerIdentity optionaltypeof import("node:tls").checkServerIdentityCustom hostname/certificate verification callback.v0.0.0-alpha.2
typeSince v0.0.0-alpha.2vjs only
export type RequestListener = (
  req: import("node:http").IncomingMessage,
  res: import("node:http").ServerResponse,
) => void;

HTTPS request listener callback signature.

typeSince v0.0.0-alpha.2vjs only
export type AgentOptions = import("node:http").AgentOptions & {
  /** Trusted CA certificates (PEM string or bytes). @since 0.0.0-alpha.2 */
  ca?: string | Uint8Array | Array<string | Uint8Array>;
  /** Client certificate chain in PEM format. @since 0.0.0-alpha.2 */
  cert?: string | Uint8Array | Array<string | Uint8Array>;
  /** Client private key in PEM format. @since 0.0.0-alpha.2 */
  key?: string | Uint8Array | Array<string | Uint8Array>;
  /** Passphrase for encrypted private keys or PKCS#12 bundles. @since 0.0.0-alpha.2 */
  passphrase?: string;
  /** PKCS#12 certificate/key bundle. @since 0.0.0-alpha.2 */
  pfx?: string | Uint8Array | Array<string | Uint8Array>;
  /** Cipher suite specification string. @since 0.0.0-alpha.2 */
  ciphers?: string;
  /** Reject peers whose certificate fails trust verification (default `true`). @since 0.0.0-alpha.2 */
  rejectUnauthorized?: boolean;
  /** SNI server name override. @since 0.0.0-alpha.2 */
  servername?: string;
  /** Maximum number of TLS sessions cached by this agent. @since 0.0.0-alpha.2 */
  maxCachedSessions?: number;
  /** Minimum allowed TLS protocol version. @since 0.0.0-alpha.2 */
  minVersion?: import("node:tls").SecureVersion;
  /** Maximum allowed TLS protocol version. @since 0.0.0-alpha.2 */
  maxVersion?: import("node:tls").SecureVersion;
};

Options accepted by new https.Agent(options).

typeSince v0.0.0-alpha.2vjs only
export type ServerOptions = import("node:http").ServerOptions &
  import("node:tls").TlsOptions;

Options accepted by https.createServer() and new https.Server().

constSince v0.0.0-alpha.2vjs only
export const globalAgent: Agent;

Default global HTTPS Agent used when no custom agent is supplied.

defaultSince v0.0.0-alpha.2vjs only
const _default: {
  /** `Agent` constructor. @since 0.0.0-alpha.2 */
  Agent: typeof Agent;
  /** `Server` constructor. @since 0.0.0-alpha.2 */
  Server: typeof Server;
  /** Create an HTTPS server. @since 0.0.0-alpha.2 */
  createServer: typeof createServer;
  /** Issue an HTTPS GET request. @since 0.0.0-alpha.2 */
  get: typeof get;
  /** Default global HTTPS `Agent`. @since 0.0.0-alpha.2 */
  readonly globalAgent: Agent;
  /** Issue an HTTPS request. @since 0.0.0-alpha.2 */
  request: typeof request;
};
export default _default;

Default export object providing the node:https API.