@hyperfrontend/network-protocol/browser/datadata
Browser-side data payload factory and encrypt/decrypt helpers built on the Web Crypto API.
Overview
A Data<T> is the structured envelope that wraps every application message inside a packet (pid, id, sequence, key, message, schema, schemaHash). This entry point binds the runtime-agnostic lib/data factory to @hyperfrontend/cryptography/browser, so createData hashes via Web Crypto and encryptData / decryptData use AES-GCM via crypto.subtle.
Usage
import { createData, encryptData, decryptData } from '@hyperfrontend/network-protocol/browser/data'
const data = createData({ type: 'greeting', content: 'hello' })
const encrypted = await encryptData(data, password)
const restored = await decryptData(encrypted, password)
Notes
createDataderives a stableschemaHashfrom the inferred JSON schema so receivers can detect message-shape changes.- Serialization helpers (
serializeData,deserializeData,asJSONString,parseJSONString,isJSONString) are re-exported for callers that need to manipulate the payload outside the queue pipeline. - Validation helpers (
isValidId,isValidPid,isValidSequence,isValidMessage,isValidSchema,isValidSchemaHash,isValidUnencryptedData,isValidUnserializedData,isValidSerializedData) are exposed for upstream guards. - The Node counterpart lives at
/node/dataand uses Node'scryptomodule instead.
API Reference
ƒ Functions
Parameters
| Name | Type | Description |
|---|---|---|
§value | string | The string value to cast as JSONString |
Returns
JSONString<T>Example
Casting a validated string
const jsonStr = asJSONString<User>('{"name":"Alice"}')Parameters
| Name | Type | Description |
|---|---|---|
§serialized | SerializedData<T> | The serialized data to deserialize |
Returns
Data<T>Example
Deserializing data with JSON message
const data = deserializeData({ ...metadata, message: '{"action":"update"}' })
// => { ...metadata, message: { action: 'update' } }Parameters
| Name | Type | Description |
|---|---|---|
§value | unknown | The value to check |
Returns
unknownExample
Checking JSON string types
isJSONString('{"key":"value"}')
// => true
isJSONString({ key: 'value' })
// => falseParameters
| Name | Type | Description |
|---|---|---|
§id | unknown | The value to validate as a data message ID |
Returns
booleanExample
Validating data message IDs
isValidId('550e8400-e29b-41d4-a716-446655440000')
// => true
isValidId('invalid-id')
// => falseParameters
| Name | Type | Description |
|---|---|---|
§message | T | The message to validate |
Returns
booleanExample
Validating message serializability
isValidMessage({ name: 'Alice', items: [1, 2, 3] })
// => true
isValidMessage({ callback: () => {} })
// => false (functions are not serializable)Parameters
| Name | Type | Description |
|---|---|---|
§pid | unknown | The value to validate as a protocol ID |
Returns
booleanExample
Validating protocol IDs
isValidPid('550e8400-e29b-41d4-a716-446655440000')
// => true
isValidPid('not-a-uuid')
// => falseParameters
| Name | Type | Description |
|---|---|---|
§schema | unknown | The value to validate as a JSON Schema |
Returns
booleanExample
Validating JSON schemas
isValidSchema({ type: 'object', properties: { name: { type: 'string' } } })
// => true
isValidSchema({ type: 'invalid-type' })
// => falseParameters
| Name | Type | Description |
|---|---|---|
§schemaHash | unknown | The value to validate as a schema hash |
Returns
booleanExample
Validating schema hashes
isValidSchemaHash('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')
// => true
isValidSchemaHash('invalid-hash')
// => falseParameters
| Name | Type | Description |
|---|---|---|
§sequence | unknown | The value to validate as a sequence number |
Returns
booleanExample
Validating sequence numbers
isValidSequence(1)
// => true
isValidSequence(0)
// => falseParameters
| Name | Type | Description |
|---|---|---|
§data | unknown | The value to validate as serialized data |
Returns
booleanExample
Validating serialized data
isValidSerializedData('{"key":"value"}')
// => true
isValidSerializedData('')
// => falseParameters
| Name | Type | Description |
|---|---|---|
§data | unknown | The value to validate as unencrypted data |
Returns
booleanExample
Validating complete unencrypted data objects
isValidUnencryptedData({
pid: '550e8400-e29b-41d4-a716-446655440000',
id: '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
sequence: 1,
message: { action: 'update' },
schema: { type: 'object' },
schemaHash: 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
})
// => trueNote: Uses multiple checks to handle cross-realm scenarios (e.g., jsdom in tests) where
instanceof Uint8Array may fail due to different global contexts.Parameters
| Name | Type | Description |
|---|---|---|
§data | unknown | The value to validate as unserialized data |
Returns
booleanExample
Validating Uint8Array data
isValidUnserializedData(new Uint8Array([1, 2, 3]))
// => true
isValidUnserializedData('string-data')
// => falseParameters
| Name | Type | Description |
|---|---|---|
§jsonString | JSONString<T> | The JSON string to parse |
Returns
TExample
Parsing a JSON string to typed object
const user = parseJSONString<User>('{"name":"Alice"}')
// => { name: 'Alice' }Parameters
| Name | Type | Description |
|---|---|---|
§data | Data<T> | The data to serialize |
Returns
SerializedData<T>Example
Serializing data for transmission
const serialized = serializeData({ ...metadata, message: { action: 'update' } })
// => { ...metadata, message: '{"action":"update"}' }◈ Interfaces
Properties
Properties
◆ Types
type DataCreater = (pid: string, sequence: number, message: T) => Promise<SerializedData<T>>type DataDecrypter = (data: Uint8Array, password: string) => Promise<SerializedData<T>>type DataEncrypter = (data: SerializedData<T>, password: string) => Promise<Uint8Array>type JSONString = string & { __jsonBrand: unknown; __type: T }