@hyperfrontend/network-protocol/node/data

data

Node.js-side data envelope factory and validators; the schema hash comes from Node's crypto module.

A Data<T> is the envelope that wraps every application message inside a packet (pid, id, sequence, message, schema, schemaHash). This entry point binds the runtime-agnostic lib/data factory to createHash from @hyperfrontend/cryptography/node, so createData(pid, sequence, message) generates a JSON Schema for the message and hashes it with SHA-256 via node:crypto.

API Reference§

ƒ Functions

§function

asJSONString<T>(value: string): JSONString<T>

Safely cast a string to JSONString after validation. Use this when you know the string is valid JSON.
Parameters
NameTypeDescription
§value
stringThe string value to cast as JSONString
Returns
JSONString<T>
The value cast as a JSONString type
Example

Casting a validated string

const jsonStr = asJSONString<User>('{"name":"Alice"}')
§function

deserializeData<T>(serialized: SerializedData<T>): Data<T>

Convert SerializedData to Data by parsing the message.
Parameters
NameTypeDescription
§serialized
SerializedData<T>The serialized data to deserialize
Returns
Data<T>
The deserialized data with parsed message
Example

Deserializing data with JSON message

const data = deserializeData({ ...metadata, message: '{"action":"update"}' })
// => { ...metadata, message: { action: 'update' } }
§function

isJSONString<T>(value: unknown): unknown

Type guard to check if a value is a JSONString.
Parameters
NameTypeDescription
§value
unknownThe value to check
Returns
unknown
True if the value is a string type, false otherwise
Example

Checking JSON string types

isJSONString('{"key":"value"}')
// => true

isJSONString({ key: 'value' })
// => false
§function

isValidId(id: unknown): boolean

Validates whether the provided value is a valid data message ID. The ID must be a 36-character string in UUID v4 format.
Parameters
NameTypeDescription
§id
unknownThe value to validate as a data message ID
Returns
boolean
True if the value is a valid UUID v4 string, false otherwise
Example

Validating data message IDs

isValidId('550e8400-e29b-41d4-a716-446655440000')
// => true

isValidId('invalid-id')
// => false
§function

isValidMessage<T>(message: T): boolean

Validates whether the provided message contains only valid, serializable data. Traverses the entire message structure to ensure all values are serializable types.
Parameters
NameTypeDescription
§message
TThe message to validate
Returns
boolean
True if all message data is valid and serializable, false otherwise
Example

Validating message serializability

isValidMessage({ name: 'Alice', items: [1, 2, 3] })
// => true

isValidMessage({ callback: () => {} })
// => false (functions are not serializable)
§function

isValidPid(pid: unknown): boolean

Validates whether the provided value is a valid protocol ID (PID). The PID must be a 36-character string in UUID v4 format.
Parameters
NameTypeDescription
§pid
unknownThe value to validate as a protocol ID
Returns
boolean
True if the value is a valid UUID v4 string, false otherwise
Example

Validating protocol IDs

isValidPid('550e8400-e29b-41d4-a716-446655440000')
// => true

isValidPid('not-a-uuid')
// => false
§function

isValidSchema(schema: unknown): boolean

Validates whether the provided value is a valid JSON Schema v4 schema. Uses the jsonschema validator to check compliance with the JSON Schema Draft v4 specification.
Parameters
NameTypeDescription
§schema
unknownThe value to validate as a JSON Schema
Returns
boolean
True if the value is a valid JSON Schema v4 schema, false otherwise
Example

Validating JSON schemas

isValidSchema({ type: 'object', properties: { name: { type: 'string' } } })
// => true

isValidSchema({ type: 'invalid-type' })
// => false
§function

isValidSchemaHash(schemaHash: unknown): boolean

Validates whether the provided value is a valid schema hash. The hash must be a valid SHA-256 hash string.
Parameters
NameTypeDescription
§schemaHash
unknownThe value to validate as a schema hash
Returns
boolean
True if the value is a valid SHA-256 hash, false otherwise
Example

Validating schema hashes

isValidSchemaHash('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')
// => true

isValidSchemaHash('invalid-hash')
// => false
§function

isValidSequence(sequence: unknown): boolean

Validates whether the provided value is a valid schema sequence number. The sequence must be a positive number greater than zero.
Parameters
NameTypeDescription
§sequence
unknownThe value to validate as a sequence number
Returns
boolean
True if the value is a positive number, false otherwise
Example

Validating sequence numbers

isValidSequence(1)
// => true

isValidSequence(0)
// => false
§function

isValidUnencryptedData(data: unknown): boolean

Validates whether the provided value is valid unencrypted data. Checks that all required data fields are present and valid, including PID, ID, sequence number, message content, schema, and schema hash.
Parameters
NameTypeDescription
§data
unknownThe value to validate as unencrypted data
Returns
boolean
True if the value is a valid unencrypted data object, false otherwise
Example

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',
})
// => true
§function

parseJSONString<T>(jsonString: JSONString<T>): T

Parse a JSONString back to its original type.
Parameters
NameTypeDescription
§jsonString
JSONString<T>The JSON string to parse
Returns
T
The parsed object of type T
Example

Parsing a JSON string to typed object

const user = parseJSONString<User>('{"name":"Alice"}')
// => { name: 'Alice' }
§function

serializeData<T>(data: Data<T>): SerializedData<T>

Convert Data to SerializedData by stringifying the message.
Parameters
NameTypeDescription
§data
Data<T>The data to serialize
Returns
SerializedData<T>
The serialized data with stringified message
Example

Serializing data for transmission

const serialized = serializeData({ ...metadata, message: { action: 'update' } })
// => { ...metadata, message: '{"action":"update"}' }

◈ Interfaces

§interface

Data

Logical view of Data with deserialized message. This represents data after the message has been parsed from JSON. Use this type when working with the actual message object.
Properties
§readonly pid:stringIdentifies a process
§readonly id:stringIdentifies this message
§readonly sequence:numberA counter that increments by 1, representing steps of a process
§readonly message:TContents of a message (deserialized)
§readonly schema:SchemaSchema of a message
§readonly schemaHash:stringHash derived from the schema
§interface

SerializedData

Serialized wire format for Data. This represents data as it's stored, transmitted, and encrypted. The message field is always a JSON string representation.
Properties
§readonly pid:stringIdentifies a process
§readonly id:stringIdentifies this message
§readonly sequence:numberA counter that increments by 1, representing steps of a process
§readonly message:JSONString<T>Contents of a message as JSON string
§readonly schema:SchemaSchema of a message
§readonly schemaHash:stringHash derived from the schema
§interface

State

Traversal state for message validation.
Properties
§valid:booleanWhether the current traversal state is valid.

◆ Types

§type

DataCreater

Creates SerializedData from a message. Returns the wire format with JSON-serialized message.
type DataCreater = (pid: string, sequence: number, message: T) => Promise<SerializedData<T>>
§type

JSONString

Branded type representing a JSON-serialized string of type T. This is a nominal type that carries information about what type the string represents when parsed, while remaining a string at runtime.
type JSONString = string & JSONStringBrand<T>
Example
const jsonStr: JSONString<{ name: string }> = '{"name":"Alice"}' as JSONString<{ name: string }>
const parsed: { name: string } = JSON.parse(jsonStr)
§type

SchemaCreater

Creates a schema from input data.
type SchemaCreater = (data: unknown) => Schema

● Variables

§type

createData

§type

getSchema

Creates a JSON schema from the provided data.
Example

Generating a schema from an object

const schema = getSchema({ name: 'Alice', age: 30 })
// => { type: 'object', properties: { name: { type: 'string' }, age: { type: 'integer' } } }