@hyperfrontend/network-protocol/node/packetpacket
Node.js-side packet encryption, serialization, and obfuscation pipeline with dynamic-key support.
A packet is the on-the-wire unit produced from a Data<T> envelope after encryption, serialization, and obfuscation. This entry point binds the runtime-agnostic lib/packet factories to @hyperfrontend/cryptography/node (Node webcrypto AES-GCM) and @hyperfrontend/string-utils/node (Buffer-based base64), exposing encryptPacket, decryptPacket, obfuscatePacket, deobfuscatePacket, and the high-level createSerializedEncryptedPacket / createDeserializedEncryptedPacket builders. Used by the /node/v1 and /node/v2 protocols to assemble outgoing wire packets and parse incoming ones. Function signatures match /browser/packet so encoded packets cross runtime boundaries cleanly.
API Reference
ƒ Functions
createDynamicKeyEncryptionFactory<T>(encryptPacket: PacketEncrypter, decryptPacket: PacketDecrypter, firstMessageHandler: FirstMessageHandler<T>): (provider: () => string) => EncryptionSuite<T>
This factory accepts packet encryption and decryption functions and returns a function that creates encryption suites with dynamic key providers. The key provider is evaluated at the time of each encryption/decryption operation, allowing for keys to change dynamically.
First Message Handling: When the key provider returns undefined/empty (first message scenario), the encryption is skipped and the packet is passed through with a special marker. On the receiving end, when no key exists, decryption is skipped and the data is parsed directly.
Parameters
Returns
(provider: () => string) => EncryptionSuite<T>Example
Creating a dynamic key encryption suite
const factory = createDynamicKeyEncryptionFactory(encryptPacket, decryptPacket, firstMessageHandler)
let sessionKey: string | undefined
const suite = factory(() => sessionKey)
const encrypted = await suite.packetEncryption(packet)createDynamicKeyObfuscationFactory(obfuscatePacket: PacketObfuscater, deobfuscatePacket: PacketDeobfuscater): (provider: () => string) => ObfuscationSuite
This factory accepts packet obfuscation and deobfuscation functions and returns a function that creates obfuscation suites with dynamic key providers. The key provider is evaluated at the time of each obfuscation/deobfuscation operation, allowing for keys to change dynamically.
Parameters
Returns
(provider: () => string) => ObfuscationSuiteExample
Creating a dynamic key obfuscation suite
const factory = createDynamicKeyObfuscationFactory(obfuscatePacket, deobfuscatePacket)
let rotatingKey = 'initial-key'
const suite = factory(() => rotatingKey)
const obfuscated = await suite.packetObfuscation(packet)Parameters
| Name | Type | Description |
|---|---|---|
§packet | unknown | The value to validate as an obfuscated packet |
Returns
booleanExample
Validating obfuscated packets
isValidObfuscatedPacket(new Uint8Array([1, 2, 3]))
// => true
isValidObfuscatedPacket({ data: 'not binary' })
// => falseParameters
| Name | Type | Description |
|---|---|---|
§origin | unknown | The value to validate as a packet origin |
Returns
booleanExample
Validating packet origins
isValidOrigin('550e8400-e29b-41d4-a716-446655440000')
// => true
isValidOrigin('not-a-uuid')
// => falseParameters
| Name | Type | Description |
|---|---|---|
§refreshRate | number | The refresh rate value to validate |
Returns
booleanExample
Validating refresh rates
isValidRefreshRate(5)
// => true
isValidRefreshRate(0)
// => falseParameters
| Name | Type | Description |
|---|---|---|
§packet | unknown | The value to validate as a serialized encrypted packet |
Returns
booleanExample
Validating serialized encrypted packets
isValidSerializedEncryptedPacket({
origin: '550e8400-e29b-41d4-a716-446655440000',
target: '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
data: { key: 'abc', message: '{"action":"ping"}' }
})
// => trueParameters
| Name | Type | Description |
|---|---|---|
§target | unknown | The value to validate as a packet target |
Returns
booleanExample
Validating packet targets
isValidTarget('6ba7b810-9dad-11d1-80b4-00c04fd430c8')
// => true
isValidTarget('invalid')
// => falseParameters
| Name | Type | Description |
|---|---|---|
§packet | unknown | The value to validate as an unencrypted packet |
Returns
booleanExample
Validating unencrypted packets
isValidUnencryptedPacket({
origin: '550e8400-e29b-41d4-a716-446655440000',
target: '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
data: { key: 'session-abc', message: { action: 'ping' } }
})
// => trueParameters
| Name | Type | Description |
|---|---|---|
§packet | unknown | The value to validate as an unobfuscated packet base |
Returns
ValidUnobfuscatedPacketBaseResultExample
Validating packet base structure
const { isValid, pkt } = isValidUnobfuscatedPacketBase({
origin: '550e8400-e29b-41d4-a716-446655440000',
target: '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
data: { key: 'abc', message: {} }
})
// => { isValid: true, pkt: { origin, target, data } }Parameters
| Name | Type | Description |
|---|---|---|
§packet | unknown | The value to validate as an unserialized encrypted packet |
Returns
booleanExample
Validating unserialized encrypted packets
isValidUnserializedEncryptedPacket({
origin: '550e8400-e29b-41d4-a716-446655440000',
target: '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
data: new Uint8Array([1, 2, 3])
})
// => true◈ Interfaces
◆ Types
type Packet = ObfuscatedPacket | UnobfuscatedPacket<T>type PacketDecrypter = (packet: UnserializedEncryptedPacket, password: string) => Promise<UnencryptedPacket<T>>type PacketDecryption = (packet: UnserializedEncryptedPacket) => Promise<UnencryptedPacket<T>>type PacketDeobfuscater = (packet: ObfuscatedPacket, password: string) => Promise<SerializedEncryptedPacket>type PacketDeobfuscation = (packet: ObfuscatedPacket) => Promise<SerializedEncryptedPacket>type PacketDeserialization = (packet: SerializedEncryptedPacket) => UnserializedEncryptedPackettype PacketEncrypter = (packet: UnencryptedPacket<T>, password: string) => Promise<UnserializedEncryptedPacket>type PacketEncryption = (packet: UnencryptedPacket<T>) => Promise<UnserializedEncryptedPacket>type PacketObfuscater = (packet: SerializedEncryptedPacket, password: string) => Promise<ObfuscatedPacket>type PacketObfuscation = (packet: SerializedEncryptedPacket) => Promise<ObfuscatedPacket>type PacketSerialization = (packet: UnserializedEncryptedPacket) => SerializedEncryptedPacket