Skip to main content
Http2Session - http2 - Node documentation
interface Http2Session
extends EventEmitter

Usage in Deno

import { type Http2Session } from "node:http2";
<div class="alert alert-warning"><div><svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z" fill="none" /> <path d="M12 9v4" /> <path d="M10.363 3.591l-8.106 13.534a1.914 1.914 0 0 0 1.636 2.871h16.214a1.914 1.914 0 0 0 1.636 -2.87l-8.106 -13.536a1.914 1.914 0 0 0 -3.274 0z" /> <path d="M12 16h.01" /> </svg> Deno compatibility</div><div><p> The following methods are non-functional stubs: <ul> <li>setLocalWindowSize</li> <li>ping</li> <li>localSettings</li> <li>remoteSettings</li> <li>settings</li> <li>ref</li> <li>unref</li> </ul> </p> </div></div>

Properties

readonly
optional
alpnProtocol: string | undefined

Value will be undefined if the Http2Session is not yet connected to a socket, h2c if the Http2Session is not connected to a TLSSocket, or will return the value of the connected TLSSocket's own alpnProtocol property.

readonly
closed: boolean

Will be true if this Http2Session instance has been closed, otherwise false.

readonly
connecting: boolean

Will be true if this Http2Session instance is still connecting, will be set to false before emitting connect event and/or calling the http2.connect callback.

readonly
destroyed: boolean

Will be true if this Http2Session instance has been destroyed and must no longer be used, otherwise false.

readonly
optional
encrypted: boolean | undefined

Value is undefined if the Http2Session session socket has not yet been connected, true if the Http2Session is connected with a TLSSocket, and false if the Http2Session is connected to any other kind of socket or stream.

A prototype-less object describing the current local settings of this Http2Session. The local settings are local to thisHttp2Session instance.

readonly
optional
originSet: string[] | undefined

If the Http2Session is connected to a TLSSocket, the originSet property will return an Array of origins for which the Http2Session may be considered authoritative.

The originSet property is only available when using a secure TLS connection.

readonly
pendingSettingsAck: boolean

Indicates whether the Http2Session is currently waiting for acknowledgment of a sent SETTINGS frame. Will be true after calling the http2session.settings() method. Will be false once all sent SETTINGS frames have been acknowledged.

A prototype-less object describing the current remote settings of thisHttp2Session. The remote settings are set by the connected HTTP/2 peer.

readonly
socket: net.Socket | tls.TLSSocket

Returns a Proxy object that acts as a net.Socket (or tls.TLSSocket) but limits available methods to ones safe to use with HTTP/2.

destroy, emit, end, pause, read, resume, and write will throw an error with code ERR_HTTP2_NO_SOCKET_MANIPULATION. See Http2Session and Sockets for more information.

setTimeout method will be called on this Http2Session.

All other interactions will be routed directly to the socket.

Provides miscellaneous information about the current state of theHttp2Session.

An object describing the current status of this Http2Session.

readonly
type: number

The http2session.type will be equal to http2.constants.NGHTTP2_SESSION_SERVER if this Http2Session instance is a server, and http2.constants.NGHTTP2_SESSION_CLIENT if the instance is a client.

Methods

close(callback?: () => void): void

Gracefully closes the Http2Session, allowing any existing streams to complete on their own and preventing new Http2Stream instances from being created. Once closed, http2session.destroy()might be called if there are no open Http2Stream instances.

If specified, the callback function is registered as a handler for the'close' event.

destroy(
error?: Error,
code?: number,
): void

Immediately terminates the Http2Session and the associated net.Socket or tls.TLSSocket.

Once destroyed, the Http2Session will emit the 'close' event. If error is not undefined, an 'error' event will be emitted immediately before the 'close' event.

If there are any remaining open Http2Streams associated with the Http2Session, those will also be destroyed.

goaway(
code?: number,
lastStreamID?: number,
opaqueData?: ArrayBufferView,
): void

Transmits a GOAWAY frame to the connected peer without shutting down theHttp2Session.

ping(callback: (
err: Error | null,
duration: number,
payload: Buffer,
) => void
): boolean

Sends a PING frame to the connected HTTP/2 peer. A callback function must be provided. The method will return true if the PING was sent, false otherwise.

The maximum number of outstanding (unacknowledged) pings is determined by the maxOutstandingPings configuration option. The default maximum is 10.

If provided, the payload must be a Buffer, TypedArray, or DataView containing 8 bytes of data that will be transmitted with the PING and returned with the ping acknowledgment.

The callback will be invoked with three arguments: an error argument that will be null if the PING was successfully acknowledged, a duration argument that reports the number of milliseconds elapsed since the ping was sent and the acknowledgment was received, and a Buffer containing the 8-byte PING payload.

session.ping(Buffer.from('abcdefgh'), (err, duration, payload) => {
  if (!err) {
    console.log(`Ping acknowledged in ${duration} milliseconds`);
    console.log(`With payload '${payload.toString()}'`);
  }
});

If the payload argument is not specified, the default payload will be the 64-bit timestamp (little endian) marking the start of the PING duration.

ping(
payload: ArrayBufferView,
callback: (
err: Error | null,
duration: number,
payload: Buffer,
) => void
,
): boolean
ref(): void

Calls ref() on this Http2Session instance's underlying net.Socket.

setLocalWindowSize(windowSize: number): void

Sets the local endpoint's window size. The windowSize is the total window size to set, not the delta.

import http2 from 'node:http2';

const server = http2.createServer();
const expectedWindowSize = 2 ** 20;
server.on('connect', (session) => {

  // Set local window size to be 2 ** 20
  session.setLocalWindowSize(expectedWindowSize);
});
setTimeout(
msecs: number,
callback?: () => void,
): void

Used to set a callback function that is called when there is no activity on the Http2Session after msecs milliseconds. The given callback is registered as a listener on the 'timeout' event.

settings(
settings: Settings,
callback?: (
err: Error | null,
settings: Settings,
duration: number,
) => void
,
): void

Updates the current local settings for this Http2Session and sends a new SETTINGS frame to the connected HTTP/2 peer.

Once called, the http2session.pendingSettingsAck property will be true while the session is waiting for the remote peer to acknowledge the new settings.

The new settings will not become effective until the SETTINGS acknowledgment is received and the 'localSettings' event is emitted. It is possible to send multiple SETTINGS frames while acknowledgment is still pending.

unref(): void

Calls unref() on this Http2Sessioninstance's underlying net.Socket.

addListener(
event: "close",
listener: () => void,
): this
addListener(
event: "error",
listener: (err: Error) => void,
): this
addListener(
event: "frameError",
listener: (
frameType: number,
errorCode: number,
streamID: number,
) => void
,
): this
addListener(
event: "goaway",
listener: (
errorCode: number,
lastStreamID: number,
opaqueData?: Buffer,
) => void
,
): this
addListener(
event: "localSettings",
listener: (settings: Settings) => void,
): this
addListener(
event: "ping",
listener: () => void,
): this
addListener(
event: "remoteSettings",
listener: (settings: Settings) => void,
): this
addListener(
event: "timeout",
listener: () => void,
): this
addListener(
event: string | symbol,
listener: (...args: any[]) => void,
): this
emit(event: "close"): boolean
emit(
event: "error",
err: Error,
): boolean
emit(
event: "frameError",
frameType: number,
errorCode: number,
streamID: number,
): boolean
emit(
event: "goaway",
errorCode: number,
lastStreamID: number,
opaqueData?: Buffer,
): boolean
emit(
event: "localSettings",
settings: Settings,
): boolean
emit(event: "ping"): boolean
emit(
event: "remoteSettings",
settings: Settings,
): boolean
emit(event: "timeout"): boolean
emit(
event: string | symbol,
...args: any[],
): boolean
on(
event: "close",
listener: () => void,
): this
on(
event: "error",
listener: (err: Error) => void,
): this
on(
event: "frameError",
listener: (
frameType: number,
errorCode: number,
streamID: number,
) => void
,
): this
on(
event: "goaway",
listener: (
errorCode: number,
lastStreamID: number,
opaqueData?: Buffer,
) => void
,
): this
on(
event: "localSettings",
listener: (settings: Settings) => void,
): this
on(
event: "ping",
listener: () => void,
): this
on(
event: "remoteSettings",
listener: (settings: Settings) => void,
): this
on(
event: "timeout",
listener: () => void,
): this
on(
event: string | symbol,
listener: (...args: any[]) => void,
): this
once(
event: "close",
listener: () => void,
): this
once(
event: "error",
listener: (err: Error) => void,
): this
once(
event: "frameError",
listener: (
frameType: number,
errorCode: number,
streamID: number,
) => void
,
): this
once(
event: "goaway",
listener: (
errorCode: number,
lastStreamID: number,
opaqueData?: Buffer,
) => void
,
): this
once(
event: "localSettings",
listener: (settings: Settings) => void,
): this
once(
event: "ping",
listener: () => void,
): this
once(
event: "remoteSettings",
listener: (settings: Settings) => void,
): this
once(
event: "timeout",
listener: () => void,
): this
once(
event: string | symbol,
listener: (...args: any[]) => void,
): this
prependListener(
event: "close",
listener: () => void,
): this
prependListener(
event: "error",
listener: (err: Error) => void,
): this
prependListener(
event: "frameError",
listener: (
frameType: number,
errorCode: number,
streamID: number,
) => void
,
): this
prependListener(
event: "goaway",
listener: (
errorCode: number,
lastStreamID: number,
opaqueData?: Buffer,
) => void
,
): this
prependListener(
event: "localSettings",
listener: (settings: Settings) => void,
): this
prependListener(
event: "ping",
listener: () => void,
): this
prependListener(
event: "remoteSettings",
listener: (settings: Settings) => void,
): this
prependListener(
event: "timeout",
listener: () => void,
): this
prependListener(
event: string | symbol,
listener: (...args: any[]) => void,
): this
prependOnceListener(
event: "close",
listener: () => void,
): this
prependOnceListener(
event: "error",
listener: (err: Error) => void,
): this
prependOnceListener(
event: "frameError",
listener: (
frameType: number,
errorCode: number,
streamID: number,
) => void
,
): this
prependOnceListener(
event: "goaway",
listener: (
errorCode: number,
lastStreamID: number,
opaqueData?: Buffer,
) => void
,
): this
prependOnceListener(
event: "localSettings",
listener: (settings: Settings) => void,
): this
prependOnceListener(
event: "ping",
listener: () => void,
): this
prependOnceListener(
event: "remoteSettings",
listener: (settings: Settings) => void,
): this
prependOnceListener(
event: "timeout",
listener: () => void,
): this
prependOnceListener(
event: string | symbol,
listener: (...args: any[]) => void,
): this