deno_node 0.182.0

Node compatibility for Deno
Documentation
// Copyright 2018-2026 the Deno authors. MIT license.

use deno_core::op2;
use libnghttp2 as ffi;
use serde::Serialize;

pub const STREAM_OPTION_EMPTY_PAYLOAD: i32 = 0x1;
pub const STREAM_OPTION_GET_TRAILERS: i32 = 0x2;
pub const MAX_ADDITIONAL_SETTINGS: usize = 10;

#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum PaddingStrategy {
  None = 0,
  Aligned = 1,
  Max = 2,
  Callback = 3,
}

#[repr(usize)]
#[derive(Debug, Clone, Copy)]
pub enum SettingsIndex {
  HeaderTableSize = 0,
  EnablePush = 1,
  InitialWindowSize = 2,
  MaxFrameSize = 3,
  MaxConcurrentStreams = 4,
  MaxHeaderListSize = 5,
  EnableConnectProtocol = 6,
  Count = 7,
}

#[repr(usize)]
#[derive(Debug, Clone, Copy)]
#[allow(dead_code, reason = "variants used for repr(usize) mapping")]
pub enum SessionStateIndex {
  EffectiveLocalWindowSize = 0,
  EffectiveRecvDataLength = 1,
  NextStreamId = 2,
  LocalWindowSize = 3,
  LastProcStreamId = 4,
  RemoteWindowSize = 5,
  OutboundQueueSize = 6,
  HdDeflateDynamicTableSize = 7,
  HdInflateDynamicTableSize = 8,
  Count = 9,
}

#[repr(usize)]
#[derive(Debug, Clone, Copy)]
#[allow(dead_code, reason = "variants used for repr(usize) mapping")]
pub enum StreamStateIndex {
  State = 0,
  Weight = 1,
  SumDependencyWeight = 2,
  LocalClose = 3,
  RemoteClose = 4,
  LocalWindowSize = 5,
  Count = 6,
}

#[repr(usize)]
#[derive(Debug, Clone, Copy)]
#[allow(dead_code, reason = "variants used for repr(usize) mapping")]
pub enum OptionsIndex {
  MaxDeflateDynamicTableSize = 0,
  MaxReservedRemoteStreams = 1,
  MaxSendHeaderBlockLength = 2,
  PeerMaxConcurrentStreams = 3,
  PaddingStrategy = 4,
  MaxHeaderListPairs = 5,
  MaxOutstandingPings = 6,
  MaxOutstandingSettings = 7,
  MaxSessionMemory = 8,
  MaxSettings = 9,
  StreamResetRate = 10,
  StreamResetBurst = 11,
  StrictHttpFieldWhitespaceValidation = 12,
  Flags = 13,
}

#[derive(Debug, Clone, Copy)]
#[repr(i32)]
pub enum SessionType {
  Server = 0,
  Client = 1,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub struct Http2Constants {
  nghttp2_hcat_request: u32,
  nghttp2_hcat_response: u32,
  nghttp2_hcat_push_response: u32,
  nghttp2_hcat_headers: u32,
  nghttp2_nv_flag_none: u32,
  nghttp2_nv_flag_no_index: u32,
  nghttp2_err_deferred: i32,
  nghttp2_err_stream_id_not_available: i32,
  nghttp2_err_invalid_argument: i32,
  nghttp2_err_stream_closed: i32,
  nghttp2_err_nomem: i32,
  stream_option_empty_payload: i32,
  stream_option_get_trailers: i32,

  http2_header_status: &'static str,
  http2_header_method: &'static str,
  http2_header_authority: &'static str,
  http2_header_scheme: &'static str,
  http2_header_path: &'static str,
  http2_header_protocol: &'static str,
  http2_header_access_control_allow_credentials: &'static str,
  http2_header_access_control_max_age: &'static str,
  http2_header_access_control_request_method: &'static str,
  http2_header_age: &'static str,
  http2_header_authorization: &'static str,
  http2_header_content_encoding: &'static str,
  http2_header_content_language: &'static str,
  http2_header_content_length: &'static str,
  http2_header_content_location: &'static str,
  http2_header_content_md5: &'static str,
  http2_header_content_range: &'static str,
  http2_header_content_type: &'static str,
  http2_header_cookie: &'static str,
  http2_header_date: &'static str,
  http2_header_dnt: &'static str,
  http2_header_etag: &'static str,
  http2_header_expires: &'static str,
  http2_header_from: &'static str,
  http2_header_host: &'static str,
  http2_header_if_match: &'static str,
  http2_header_if_none_match: &'static str,
  http2_header_if_modified_since: &'static str,
  http2_header_if_range: &'static str,
  http2_header_if_unmodified_since: &'static str,
  http2_header_last_modified: &'static str,
  http2_header_location: &'static str,
  http2_header_max_forwards: &'static str,
  http2_header_proxy_authorization: &'static str,
  http2_header_range: &'static str,
  http2_header_referer: &'static str,
  http2_header_retry_after: &'static str,
  http2_header_set_cookie: &'static str,
  http2_header_tk: &'static str,
  http2_header_upgrade_insecure_requests: &'static str,
  http2_header_user_agent: &'static str,
  http2_header_x_content_type_options: &'static str,
  http2_header_connection: &'static str,
  http2_header_upgrade: &'static str,
  http2_header_http2_settings: &'static str,
  http2_header_te: &'static str,
  http2_header_transfer_encoding: &'static str,
  http2_header_keep_alive: &'static str,
  http2_header_proxy_connection: &'static str,
  http2_header_accept: &'static str,
  http2_header_accept_encoding: &'static str,
  http2_header_accept_language: &'static str,
  http2_header_accept_ranges: &'static str,

  http2_method_connect: &'static str,
  http2_method_delete: &'static str,
  http2_method_get: &'static str,
  http2_method_head: &'static str,

  http_status_continue: u32,
  http_status_switching_protocols: u32,
  http_status_early_hints: u32,
  http_status_ok: u32,
  http_status_no_content: u32,
  http_status_reset_content: u32,
  http_status_not_modified: u32,
  http_status_method_not_allowed: u32,
  http_status_expectation_failed: u32,
  http_status_misdirected_request: u32,

  nghttp2_err_frame_size_error: u32,
  nghttp2_session_server: u32,
  nghttp2_session_client: u32,
  nghttp2_stream_state_idle: u32,
  nghttp2_stream_state_open: u32,
  nghttp2_stream_state_reserved_local: u32,
  nghttp2_stream_state_reserved_remote: u32,
  nghttp2_stream_state_half_closed_local: u32,
  nghttp2_stream_state_half_closed_remote: u32,
  nghttp2_stream_state_closed: u32,
  nghttp2_flag_none: u32,
  nghttp2_flag_end_stream: u32,
  nghttp2_flag_end_headers: u32,
  nghttp2_flag_ack: u32,
  nghttp2_flag_padded: u32,
  nghttp2_flag_priority: u32,
  default_settings_header_table_size: u32,
  default_settings_enable_push: u32,
  default_settings_max_concurrent_streams: u32,
  default_settings_initial_window_size: u32,
  default_settings_max_frame_size: u32,
  default_settings_max_header_list_size: u32,
  default_settings_enable_connect_protocol: u32,
  max_max_frame_size: u32,
  min_max_frame_size: u32,
  max_initial_window_size: u32,
  nghttp2_settings_header_table_size: u32,
  nghttp2_settings_enable_push: u32,
  nghttp2_settings_max_concurrent_streams: u32,
  nghttp2_settings_initial_window_size: u32,
  nghttp2_settings_max_frame_size: u32,
  nghttp2_settings_max_header_list_size: u32,
  nghttp2_settings_enable_connect_protocol: u32,
  padding_strategy_none: u32,
  padding_strategy_aligned: u32,
  padding_strategy_max: u32,
  padding_strategy_callback: u32,

  nghttp2_no_error: u32,
  nghttp2_protocol_error: u32,
  nghttp2_internal_error: u32,
  nghttp2_flow_control_error: u32,
  nghttp2_settings_timeout: u32,
  nghttp2_stream_closed: u32,
  nghttp2_frame_size_error: u32,
  nghttp2_refused_stream: u32,
  nghttp2_cancel: u32,
  nghttp2_compression_error: u32,
  nghttp2_connect_error: u32,
  nghttp2_enhance_your_calm: u32,
  nghttp2_inadequate_security: u32,
  nghttp2_http_1_1_required: u32,

  header_table_size: u32,
  enable_push: u32,
  max_concurrent_streams: u32,
  initial_window_size: u32,
  max_frame_size: u32,
  max_header_list_size: u32,
  enable_connect_protocol: u32,

  nghttp2_default_weight: u32,
}

#[op2]
#[serde]
#[allow(clippy::unnecessary_cast, reason = "platform specific")]
pub fn op_http2_constants() -> Http2Constants {
  Http2Constants {
    nghttp2_hcat_request: ffi::NGHTTP2_HCAT_REQUEST as u32,
    nghttp2_hcat_response: ffi::NGHTTP2_HCAT_RESPONSE as u32,
    nghttp2_hcat_push_response: ffi::NGHTTP2_HCAT_PUSH_RESPONSE as u32,
    nghttp2_hcat_headers: ffi::NGHTTP2_HCAT_HEADERS as u32,
    nghttp2_nv_flag_none: ffi::NGHTTP2_NV_FLAG_NONE as u32,
    nghttp2_nv_flag_no_index: ffi::NGHTTP2_NV_FLAG_NO_INDEX as u32,
    nghttp2_err_deferred: ffi::NGHTTP2_ERR_DEFERRED as i32,
    nghttp2_err_stream_id_not_available:
      ffi::NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE as i32,
    nghttp2_err_invalid_argument: ffi::NGHTTP2_ERR_INVALID_ARGUMENT as i32,
    nghttp2_err_stream_closed: ffi::NGHTTP2_ERR_STREAM_CLOSED as i32,
    nghttp2_err_nomem: ffi::NGHTTP2_ERR_NOMEM as i32,
    stream_option_empty_payload: STREAM_OPTION_EMPTY_PAYLOAD,
    stream_option_get_trailers: STREAM_OPTION_GET_TRAILERS,

    http2_header_status: ":status",
    http2_header_method: ":method",
    http2_header_authority: ":authority",
    http2_header_scheme: ":scheme",
    http2_header_path: ":path",
    http2_header_protocol: ":protocol",
    http2_header_access_control_allow_credentials: "access-control-allow-credentials",
    http2_header_access_control_max_age: "access-control-max-age",
    http2_header_access_control_request_method: "access-control-request-method",
    http2_header_age: "age",
    http2_header_authorization: "authorization",
    http2_header_content_encoding: "content-encoding",
    http2_header_content_language: "content-language",
    http2_header_content_length: "content-length",
    http2_header_content_location: "content-location",
    http2_header_content_md5: "content-md5",
    http2_header_content_range: "content-range",
    http2_header_content_type: "content-type",
    http2_header_cookie: "cookie",
    http2_header_date: "date",
    http2_header_dnt: "dnt",
    http2_header_etag: "etag",
    http2_header_expires: "expires",
    http2_header_from: "from",
    http2_header_host: "host",
    http2_header_if_match: "if-match",
    http2_header_if_none_match: "if-none-match",
    http2_header_if_modified_since: "if-modified-since",
    http2_header_if_range: "if-range",
    http2_header_if_unmodified_since: "if-unmodified-since",
    http2_header_last_modified: "last-modified",
    http2_header_location: "location",
    http2_header_max_forwards: "max-forwards",
    http2_header_proxy_authorization: "proxy-authorization",
    http2_header_range: "range",
    http2_header_referer: "referer",
    http2_header_retry_after: "retry-after",
    http2_header_set_cookie: "set-cookie",
    http2_header_tk: "tk",
    http2_header_upgrade_insecure_requests: "upgrade-insecure-requests",
    http2_header_user_agent: "user-agent",
    http2_header_x_content_type_options: "x-content-type-options",
    http2_header_connection: "connection",
    http2_header_upgrade: "upgrade",
    http2_header_http2_settings: "http2-settings",
    http2_header_te: "te",
    http2_header_transfer_encoding: "transfer-encoding",
    http2_header_keep_alive: "keep-alive",
    http2_header_proxy_connection: "proxy-connection",
    http2_header_accept: "accept",
    http2_header_accept_encoding: "accept-encoding",
    http2_header_accept_language: "accept-language",
    http2_header_accept_ranges: "accept-ranges",

    http2_method_connect: "CONNECT",
    http2_method_delete: "DELETE",
    http2_method_get: "GET",
    http2_method_head: "HEAD",

    http_status_continue: 100,
    http_status_switching_protocols: 101,
    http_status_early_hints: 103,
    http_status_ok: 200,
    http_status_no_content: 204,
    http_status_reset_content: 205,
    http_status_not_modified: 304,
    http_status_method_not_allowed: 405,
    http_status_expectation_failed: 417,
    http_status_misdirected_request: 421,

    nghttp2_err_frame_size_error: ffi::NGHTTP2_ERR_FRAME_SIZE_ERROR as u32,
    nghttp2_session_server: 0,
    nghttp2_session_client: 1,
    nghttp2_stream_state_idle: ffi::NGHTTP2_STREAM_STATE_IDLE as u32,
    nghttp2_stream_state_open: ffi::NGHTTP2_STREAM_STATE_OPEN as u32,
    nghttp2_stream_state_reserved_local:
      ffi::NGHTTP2_STREAM_STATE_RESERVED_LOCAL as u32,
    nghttp2_stream_state_reserved_remote:
      ffi::NGHTTP2_STREAM_STATE_RESERVED_REMOTE as u32,
    nghttp2_stream_state_half_closed_local:
      ffi::NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL as u32,
    nghttp2_stream_state_half_closed_remote:
      ffi::NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE as u32,
    nghttp2_stream_state_closed: ffi::NGHTTP2_STREAM_STATE_CLOSED as u32,
    nghttp2_flag_none: ffi::NGHTTP2_FLAG_NONE as u32,
    nghttp2_flag_end_stream: ffi::NGHTTP2_FLAG_END_STREAM as u32,
    nghttp2_flag_end_headers: ffi::NGHTTP2_FLAG_END_HEADERS as u32,
    nghttp2_flag_ack: ffi::NGHTTP2_FLAG_ACK as u32,
    nghttp2_flag_padded: ffi::NGHTTP2_FLAG_PADDED as u32,
    nghttp2_flag_priority: ffi::NGHTTP2_FLAG_PRIORITY as u32,
    default_settings_header_table_size: 4096,
    default_settings_enable_push: 1,
    default_settings_max_concurrent_streams: 0xffffffff,
    default_settings_initial_window_size: 65535,
    default_settings_max_frame_size: 16384,
    default_settings_max_header_list_size: 0xffffffff,
    default_settings_enable_connect_protocol: 0,
    max_max_frame_size: 16777215,
    min_max_frame_size: 16384,
    max_initial_window_size: 2147483647,
    nghttp2_settings_header_table_size: ffi::NGHTTP2_SETTINGS_HEADER_TABLE_SIZE
      as u32,
    nghttp2_settings_enable_push: ffi::NGHTTP2_SETTINGS_ENABLE_PUSH as u32,
    nghttp2_settings_max_concurrent_streams:
      ffi::NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS as u32,
    nghttp2_settings_initial_window_size:
      ffi::NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE as u32,
    nghttp2_settings_max_frame_size: ffi::NGHTTP2_SETTINGS_MAX_FRAME_SIZE
      as u32,
    nghttp2_settings_max_header_list_size:
      ffi::NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE as u32,
    nghttp2_settings_enable_connect_protocol: 8,
    padding_strategy_none: 0,
    padding_strategy_aligned: 1,
    padding_strategy_max: 2,
    padding_strategy_callback: 3,

    nghttp2_no_error: ffi::NGHTTP2_NO_ERROR as u32,
    nghttp2_protocol_error: ffi::NGHTTP2_PROTOCOL_ERROR as u32,
    nghttp2_internal_error: ffi::NGHTTP2_INTERNAL_ERROR as u32,
    nghttp2_flow_control_error: ffi::NGHTTP2_FLOW_CONTROL_ERROR as u32,
    nghttp2_settings_timeout: ffi::NGHTTP2_SETTINGS_TIMEOUT as u32,
    nghttp2_stream_closed: ffi::NGHTTP2_STREAM_CLOSED as u32,
    nghttp2_frame_size_error: ffi::NGHTTP2_FRAME_SIZE_ERROR as u32,
    nghttp2_refused_stream: ffi::NGHTTP2_REFUSED_STREAM as u32,
    nghttp2_cancel: ffi::NGHTTP2_CANCEL as u32,
    nghttp2_compression_error: ffi::NGHTTP2_COMPRESSION_ERROR as u32,
    nghttp2_connect_error: ffi::NGHTTP2_CONNECT_ERROR as u32,
    nghttp2_enhance_your_calm: ffi::NGHTTP2_ENHANCE_YOUR_CALM as u32,
    nghttp2_inadequate_security: ffi::NGHTTP2_INADEQUATE_SECURITY as u32,
    nghttp2_http_1_1_required: ffi::NGHTTP2_HTTP_1_1_REQUIRED as u32,

    header_table_size: ffi::NGHTTP2_SETTINGS_HEADER_TABLE_SIZE as u32,
    enable_push: ffi::NGHTTP2_SETTINGS_ENABLE_PUSH as u32,
    max_concurrent_streams: ffi::NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS as u32,
    initial_window_size: ffi::NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE as u32,
    max_frame_size: ffi::NGHTTP2_SETTINGS_MAX_FRAME_SIZE as u32,
    max_header_list_size: ffi::NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE as u32,
    enable_connect_protocol: 8,

    nghttp2_default_weight: 16,
  }
}