Skip to main content
API reference for the @enclave-vm/stream package - streaming protocol implementation including NDJSON parsing, encryption, and reconnection handling.

Installation

NDJSON Parsing

serializeEvent(event)

Serialize an event to NDJSON format (single line).
Example:

serializeEvents(events)

Serialize multiple events to NDJSON format.
Example:

parseLine(line)

Parse a single NDJSON line into an event.
Example:

parseLines(data)

Parse multiple NDJSON lines into events.
Example:

NdjsonStreamParser

Incremental NDJSON parser for streaming data. Handles partial lines across chunks.
Example:

createNdjsonParseStream()

Create a transform stream that parses NDJSON. Works with browser fetch() and Node.js streams.
Example:

createNdjsonSerializeStream()

Create a transform stream that serializes events to NDJSON.

parseNdjsonStream(stream)

Async generator that parses NDJSON from a ReadableStream.
Example:

ECDH Key Exchange

generateKeyPair(curve?)

Generate an ephemeral ECDH key pair.
Example:

exportPublicKey(publicKey)

Export a public key to base64 format.

importPublicKey(publicKeyB64, curve?)

Import a public key from base64 format.

deriveSharedSecret(privateKey, peerPublicKey)

Derive shared secret from private key and peer’s public key.
Example:

createClientHello(keyPair)

Create a client hello message for the encryption handshake.

createServerHello(keyPair, keyId)

Create a server hello message for the encryption handshake.

processClientHello(clientHello)

Process a client hello and generate server response.

processServerHello(serverHello)

Process a server hello and extract peer’s public key.

EcdhError

Error class for ECDH operations.

Key Derivation (HKDF)

deriveKey(sharedSecret, salt, info, keyLength?)

Derive a key using HKDF-SHA256.

deriveSessionKeys(sharedSecret, sessionId)

Derive session keys for bidirectional communication.

importAesGcmKey(keyBytes)

Import raw key bytes as a CryptoKey for AES-GCM.

deriveSessionCryptoKeys(sharedSecret, sessionId)

Derive and import session keys as CryptoKeys.
Example:

HkdfError

Error class for HKDF operations.

AES-GCM Encryption

encrypt(key, plaintext, nonce, additionalData?)

Encrypt data using AES-GCM.

decrypt(key, ciphertext, nonce, additionalData?)

Decrypt data using AES-GCM.

encryptJson(key, keyId, data, nonce?)

Encrypt a JSON object and create an encrypted envelope payload.

decryptJson(key, payload)

Decrypt an encrypted envelope payload and parse as JSON.

createEncryptedEnvelope(key, keyId, sessionId, seq, innerEvent, nonce?)

Create an encrypted envelope from an event.

generateNonce()

Generate a random 12-byte nonce for AES-GCM.

generateCounterNonce(prefix, counter)

Generate a counter-based nonce (8 bytes prefix + 4 bytes counter).

toBase64(bytes)

Encode bytes to base64.

fromBase64(base64)

Decode base64 to bytes.

AesGcmError

Error class for AES-GCM operations.

SessionEncryptionContext

Manages encryption key state for a session.
Example:

Reconnection

ConnectionState

Connection state enumeration.

DEFAULT_RECONNECTION_CONFIG

Default reconnection configuration.

ReconnectionStateMachine

Manages connection state and automatic reconnection.
Example:

SequenceTracker

Tracks sequence numbers and detects gaps for replay.
Example:

EventBuffer

Buffer for storing events during reconnection.
Example:

HeartbeatMonitor

Monitors heartbeats to detect stale connections.
Example:

Re-exported Types

This package re-exports all types from @enclave-vm/types:
See @enclave-vm/types API for the complete type reference.