@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).serializeEvents(events)
Serialize multiple events to NDJSON format.parseLine(line)
Parse a single NDJSON line into an event.parseLines(data)
Parse multiple NDJSON lines into events.NdjsonStreamParser
Incremental NDJSON parser for streaming data. Handles partial lines across chunks.createNdjsonParseStream()
Create a transform stream that parses NDJSON. Works with browserfetch() and Node.js streams.
createNdjsonSerializeStream()
Create a transform stream that serializes events to NDJSON.parseNdjsonStream(stream)
Async generator that parses NDJSON from a ReadableStream.ECDH Key Exchange
generateKeyPair(curve?)
Generate an ephemeral ECDH key pair.| Parameter | Type | Default | Description |
|---|---|---|---|
curve | SupportedCurve | 'P-256' | Elliptic curve to use |
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.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.| Parameter | Type | Default | Description |
|---|---|---|---|
sharedSecret | Uint8Array | Required | Shared secret from ECDH |
salt | Uint8Array | null | null | Optional salt (defaults to zeros) |
info | string | Required | Context info string |
keyLength | number | 32 | Output key length in bytes |
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.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.Reconnection
ConnectionState
Connection state enumeration.DEFAULT_RECONNECTION_CONFIG
Default reconnection configuration.ReconnectionStateMachine
Manages connection state and automatic reconnection.SequenceTracker
Tracks sequence numbers and detects gaps for replay.EventBuffer
Buffer for storing events during reconnection.HeartbeatMonitor
Monitors heartbeats to detect stale connections.Re-exported Types
This package re-exports all types from@enclave-vm/types:
Related
- Stream Overview - Usage guide
- @enclave-vm/types - Type definitions
- @enclave-vm/client - Client SDK
- Streaming Protocol - Protocol concepts