hyperlane-plugin-rpc 23.2.0

A JSON-RPC 2.0 server plugin for the Hyperlane framework with async method routing, batch dispatch, notification handling, and configurable payload and batch DoS bounds.
Documentation
/// JSON-RPC 2.0 protocol version literal.
pub const JSONRPC_VERSION: &str = "2.0";

/// Default HTTP endpoint path handled by [`crate::RpcHook`].
pub const DEFAULT_RPC_ENDPOINT: &str = "/rpc";

/// Default maximum request body size accepted by [`crate::RpcHook`] (1 MiB).
pub const DEFAULT_RPC_MAX_BODY_SIZE: usize = 1024 * 1024;

/// Default maximum number of items permitted in one JSON-RPC batch array.
pub const DEFAULT_RPC_MAX_BATCH_SIZE: usize = 64;

/// JSON-RPC 2.0 Parse Error code.
pub const RPC_PARSE_ERROR: i64 = -32700;

/// JSON-RPC 2.0 Invalid Request code.
pub const RPC_INVALID_REQUEST: i64 = -32600;

/// JSON-RPC 2.0 Method Not Found code.
pub const RPC_METHOD_NOT_FOUND: i64 = -32601;

/// JSON-RPC 2.0 Invalid Params code.
pub const RPC_INVALID_PARAMS: i64 = -32602;

/// JSON-RPC 2.0 Internal Error code.
pub const RPC_INTERNAL_ERROR: i64 = -32603;

/// Standard Parse Error message.
pub(crate) const MSG_PARSE_ERROR: &str = "Parse error";

/// Standard Invalid Request message.
pub(crate) const MSG_INVALID_REQUEST: &str = "Invalid Request";

/// Standard Method Not Found message.
pub(crate) const MSG_METHOD_NOT_FOUND: &str = "Method not found";

/// Standard Invalid Params message.
pub(crate) const MSG_INVALID_PARAMS: &str = "Invalid params";

/// Standard Internal Error message.
pub(crate) const MSG_INTERNAL_ERROR: &str = "Internal error";

/// Prebuilt JSON-RPC 2.0 Parse Error response bytes avoiding per-error serialization.
pub(crate) const PREBUILT_PARSE_ERROR: &[u8] =
    br#"{"jsonrpc":"2.0","error":{"code":-32700,"message":"Parse error"},"id":null}"#;

/// Prebuilt JSON-RPC 2.0 Invalid Request response bytes avoiding per-error serialization.
pub(crate) const PREBUILT_INVALID_REQUEST: &[u8] =
    br#"{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid Request"},"id":null}"#;

/// Prebuilt JSON-RPC 2.0 batch-limit-exceeded error response bytes.
pub(crate) const PREBUILT_BATCH_TOO_LARGE: &[u8] = br#"{"jsonrpc":"2.0","error":{"code":-32600,"message":"Batch size exceeds configured limit"},"id":null}"#;

/// Initial buffer capacity in bytes when serializing a single JSON-RPC 2.0 response.
pub(crate) const RPC_SINGLE_RESPONSE_CAP: usize = 128;

/// JSON-RPC 2.0 request field name `"jsonrpc"`.
pub(crate) const FIELD_JSONRPC: &str = "jsonrpc";

/// JSON-RPC 2.0 request field name `"method"`.
pub(crate) const FIELD_METHOD: &str = "method";

/// JSON-RPC 2.0 request field name `"params"`.
pub(crate) const FIELD_PARAMS: &str = "params";

/// JSON-RPC 2.0 request field name `"id"`.
pub(crate) const FIELD_ID: &str = "id";

/// Serde visitor expectation message for JSON-RPC field keys.
pub(crate) const EXPECT_FIELD_KEY: &str = "JSON-RPC field key";

/// Serde visitor expectation message for lenient JSON strings.
pub(crate) const EXPECT_LENIENT_STR: &str = "JSON value";

/// Serde visitor expectation message for JSON-RPC correlation identifiers.
pub(crate) const EXPECT_RPC_ID: &str = "JSON-RPC id";

/// Serde visitor expectation message for JSON-RPC request or batch elements.
pub(crate) const EXPECT_BATCH_ITEM: &str = "JSON-RPC request item";

/// Static JSON prefix for compact JSON-RPC 2.0 response objects.
pub(crate) const RPC_JSON_PREFIX: &[u8] = br#"{"jsonrpc":"2.0","id":"#;

/// Static JSON field separator before the `"result"` payload.
pub(crate) const RPC_JSON_RESULT_KEY: &[u8] = br#","result":"#;

/// Static JSON field separator before the `"error"` payload.
pub(crate) const RPC_JSON_ERROR_KEY: &[u8] = br#","error":"#;

/// Pre-serialized JSON error object for `Invalid Request` (`-32600`).
pub(crate) const STATIC_ERR_INVALID_REQUEST: &[u8] =
    br#"{"code":-32600,"message":"Invalid Request"}"#;

/// Pre-serialized JSON error object for `Method not found` (`-32601`).
pub(crate) const STATIC_ERR_METHOD_NOT_FOUND: &[u8] =
    br#"{"code":-32601,"message":"Method not found"}"#;

/// Static JSON literal for `null`.
pub(crate) const JSON_NULL: &[u8] = b"null";

/// Static JSON literal for `true`.
pub(crate) const JSON_TRUE: &[u8] = b"true";

/// Static JSON literal for `false`.
pub(crate) const JSON_FALSE: &[u8] = b"false";

/// HTTP/1.1 `200 OK` JSON response prefix up to the `content-length` value.
pub(crate) const WIRE_HTTP11_200_JSON_PREFIX: &[u8] =
    b"HTTP/1.1 200 OK\r\ncontent-type: application/json\r\nconnection: keep-alive\r\ncontent-length: ";

/// HTTP/1.1 header block terminator (`\r\n\r\n`).
pub(crate) const WIRE_HEADER_END: &[u8] = b"\r\n\r\n";

/// Stack buffer capacity in bytes for zero-allocation HTTP/1.1 JSON-RPC wire responses.
pub(crate) const RPC_WIRE_STACK_CAP: usize = 1024;

/// Maximum JSON payload length eligible for stack-buffered single HTTP/1.1 framing.
pub(crate) const RPC_PAYLOAD_STACK_CAP: usize = 512;

/// Multiplicative 64-bit mixing constant for zero-allocation [`FxHasher`].
pub(crate) const FX_HASH_K: u64 = 0x517c_c1b7_2722_0a95;

/// Two-digit ASCII decimal lookup pairs for `00..=99`.
pub(crate) const DEC_DIGITS_LUT: &[u8; 200] = b"00010203040506070809\
10111213141516171819\
20212223242526272829\
30313233343536373839\
40414243444546474849\
50515253545556575859\
60616263646566676869\
70717273747576777879\
80818283848586878889\
90919293949596979899";

/// ASCII opening brace byte (`'{'`).
pub(crate) const BYTE_LBRACE: u8 = b'{';

/// ASCII closing brace byte (`'}'`).
pub(crate) const BYTE_RBRACE: u8 = b'}';

/// ASCII opening bracket byte (`'['`).
pub(crate) const BYTE_LBRACKET: u8 = b'[';

/// ASCII closing bracket byte (`']'`).
pub(crate) const BYTE_RBRACKET: u8 = b']';

/// ASCII comma byte (`','`).
pub(crate) const BYTE_COMMA: u8 = b',';

/// ASCII double quote byte (`'"'`).
pub(crate) const BYTE_QUOTE: u8 = b'"';

/// ASCII backslash byte (`'\\'`).
pub(crate) const BYTE_BACKSLASH: u8 = b'\\';

/// ASCII hyphen byte (`'-'`).
pub(crate) const BYTE_DASH: u8 = b'-';

/// ASCII zero digit byte (`'0'`).
pub(crate) const BYTE_ZERO: u8 = b'0';

/// ASCII space byte.
pub(crate) const BYTE_SPACE: u8 = b' ';

/// ASCII horizontal tab byte.
pub(crate) const BYTE_TAB: u8 = b'\t';

/// ASCII carriage return byte.
pub(crate) const BYTE_CR: u8 = b'\r';

/// ASCII line feed byte.
pub(crate) const BYTE_LF: u8 = b'\n';