### Load or Create Hyperschema Instance Source: https://context7.com/holepunchto/hyperschema/llms.txt Use `Hyperschema.from()` to load an existing schema from a directory or a JSON object, or to start a new schema definition. ```javascript const Hyperschema = require('hyperschema') // Load from a directory (reads ./my-schema/schema.json if it exists) const schema = Hyperschema.from('./my-schema') // Load from a raw JSON object const schemaFromJson = Hyperschema.from({ version: 3, schema: [ { name: 'user', namespace: 'app', fields: [{ name: 'id', type: 'uint', required: true }] } ] }) ``` -------------------------------- ### Decode Data with Different Schema Versions Source: https://github.com/holepunchto/hyperschema/blob/main/README.md After updating a schema, you can use `resolveStruct` to get encodings for specific versions and decode data accordingly. This example shows decoding with version 1 and version 2 of the struct. ```javascript const encoding1 = resolveStruct('@namespace-1/basic-struct', 1) const encoding2 = resolveStruct('@namespace-1/basic-struct', 2) // { id: 10, other: 20, another: null } c.decode(encoding1, c.encode(encoding1, { id: 10, other: 20, another: 'foo' })) // { id: 10, other: 20, another: 'foo' } c.decode(encoding2, c.encode(encoding2, { id: 10, other: 20, another: 'foo' })) ``` -------------------------------- ### Embed compact struct flags into parent bitfield with `inline: true` Source: https://context7.com/holepunchto/hyperschema/llms.txt Use `inline: true` on a struct field to merge its optional-field flags into the parent struct's bitfield. This requires the inner struct to be marked with `compact: true` and results in smaller encoded output. The example demonstrates registering a 'meta' struct and then inlining it into a 'packet' struct. ```javascript const Hyperschema = require('hyperschema') const schema = Hyperschema.from('./my-schema') const ns = schema.namespace('msg') ns.register({ name: 'meta', compact: true, // required for inlining fields: [ { name: 'seq', type: 'uint' }, { name: 'ttl', type: 'uint' } ] }) ns.register({ name: 'packet', fields: [ { name: 'id', type: 'uint', required: true }, { name: 'meta', type: '@msg/meta', inline: true } // flags merged into packet's bitfield ] }) Hyperschema.toDisk(schema) // Usage const c = require('compact-encoding') const { resolveStruct } = require('./my-schema') const enc = resolveStruct('@msg/packet') const buf = c.encode(enc, { id: 1, meta: { seq: 42, ttl: 10 } }) const obj = c.decode(enc, buf) // => { id: 1, meta: { seq: 42, ttl: 10 } } ``` -------------------------------- ### Initialize and Register Schema with Hyperschema Source: https://github.com/holepunchto/hyperschema/blob/main/README.md Use this to load an existing schema from a directory or create a new one, then register struct definitions on namespaces. The `toDisk` function will write schema and generated encodings. ```javascript const Hyperschema = require('.') const schema = Hyperschema.from('./schema') const ns1 = schema.namespace('namespace-1') ns1.register({ name: 'basic-struct', fields: [ { name: 'id', type: 'uint', required: true }, { name: 'other', type: 'uint' } ] }) Hyperschema.toDisk(schema) ``` -------------------------------- ### Hyperschema.from(dirOrJson) Source: https://context7.com/holepunchto/hyperschema/llms.txt Loads or creates a schema instance. It can initialize from a directory path (reading schema.json if present) or directly from a JSON object. ```APIDOC ## Hyperschema.from(dirOrJson) — Load or create a schema instance Creates a new `Hyperschema` instance. If passed a directory path string, it looks for a `schema.json` file and loads the existing schema; if no file is found, it starts fresh. Can also accept a raw JSON object directly. ### Parameters - **dirOrJson** (string | object) - Required - A directory path or a raw JSON object representing the schema. ### Request Example ```js // Load from a directory (reads ./my-schema/schema.json if it exists) const schema = Hyperschema.from('./my-schema') // Load from a raw JSON object const schemaFromJson = Hyperschema.from({ version: 3, schema: [ { name: 'user', namespace: 'app', fields: [{ name: 'id', type: 'uint', required: true }] } ] }) ``` ``` -------------------------------- ### Hyperschema.toDisk(schema, [dir], [opts]) Source: https://context7.com/holepunchto/hyperschema/llms.txt Persists the schema and generated code to disk. It writes a `schema.json` version manifest and an `index.js` file with compact-encoding definitions. The schema version is automatically bumped if definitions change. ```APIDOC ## Hyperschema.toDisk(schema, [dir], [opts]) — Persist schema and generated code Writes two files into the schema directory: `schema.json` (the version manifest) and `index.js` (the generated `compact-encoding` definitions). If any type definitions changed since the last write, the schema version is automatically bumped. Accepts an `{ esm: true }` option to emit ES module output instead of CommonJS. ### Parameters - **schema** (Hyperschema) - Required - The schema instance to persist. - **dir** (string) - Optional - The directory to write the files to. Defaults to the schema's directory. - **opts** (object) - Optional - Options for output format. - **esm** (boolean) - Optional - If true, emits ES module output instead of CommonJS. Defaults to false. ### Request Example ```js const Hyperschema = require('hyperschema') const schema = Hyperschema.from('./my-schema') const ns = schema.namespace('app') ns.register({ name: 'message', fields: [ { name: 'id', type: 'uint', required: true }, { name: 'body', type: 'string', required: true }, { name: 'ts', type: 'uint' } ] }) // CJS output (default) Hyperschema.toDisk(schema) // ESM output Hyperschema.toDisk(schema, { esm: true }) // Write to a different directory than schema.dir Hyperschema.toDisk(schema, './output-dir', { esm: false }) // Produces: // ./my-schema/schema.json — version manifest // ./my-schema/index.js — generated encoders ``` ``` -------------------------------- ### Create and Use Namespaces Source: https://context7.com/holepunchto/hyperschema/llms.txt Use `schema.namespace()` to create isolated namespaces for type definitions. This allows for cross-namespace references using the `@namespace/type-name` format and prevents naming conflicts. ```javascript const Hyperschema = require('hyperschema') const schema = Hyperschema.from('./my-schema') const core = schema.namespace('core') const auth = schema.namespace('auth') // Types will be accessible as '@core/...' and '@auth/...' core.register({ name: 'id-type', alias: 'uint' }) auth.register({ name: 'session', fields: [ { name: 'userId', type: '@core/id-type', required: true }, { name: 'token', type: 'string', required: true } ] }) Hyperschema.toDisk(schema) ``` -------------------------------- ### Create Schema Namespace Source: https://github.com/holepunchto/hyperschema/blob/main/README.md Use `schema.namespace(name)` to create a new schema namespace. Structs and aliases defined within this namespace will be prefixed with `@name`, allowing for clear referencing in subsequent definitions. ```javascript const ns = schema.namespace(name) ``` -------------------------------- ### Register Schema Definition on Namespace Source: https://github.com/holepunchto/hyperschema/blob/main/README.md Use `ns.register(definition)` to add a new schema or alias definition to an existing namespace. Refer to the Schema Definition section for details on the structure of a definition. ```javascript ns.register(definition) ``` -------------------------------- ### Convenience Encode/Decode Functions Source: https://context7.com/holepunchto/hyperschema/llms.txt The generated module provides `encode` and `decode` as shorthand helpers that internally call `getEncoding` and `c.encode`/`c.decode`. The version defaults to the schema's `VERSION` constant but can be specified. ```javascript const { encode, decode } = require('./my-schema') const buf = encode('@net/peer', { id: Buffer.alloc(32), address: '::1', port: 9000 }) const obj = decode('@net/peer', buf) // => { id: , address: '::1', port: 9000, seen: 0 } // Encode/decode at a specific old version const bufOld = encode('@net/peer', { id: Buffer.alloc(32), address: '::1' }, 1) const objOld = decode('@net/peer', bufOld, 1) ``` -------------------------------- ### schema.namespace(name) Source: https://context7.com/holepunchto/hyperschema/llms.txt Creates a new `HyperschemaNamespace` instance for organizing types. Types registered within a namespace are prefixed with `@name/`, allowing for cross-namespace references. ```APIDOC ## schema.namespace(name) — Create a namespace Returns a new `HyperschemaNamespace` instance. All types registered on the namespace are prefixed with `@name/`, enabling cross-namespace references. Each name can only be used once per schema instance. ### Parameters - **name** (string) - Required - The name of the namespace. ### Request Example ```js const Hyperschema = require('hyperschema') const schema = Hyperschema.from('./my-schema') const core = schema.namespace('core') const auth = schema.namespace('auth') // Types will be accessible as '@core/...' and '@auth/...' core.register({ name: 'id-type', alias: 'uint' }) auth.register({ name: 'session', fields: [ { name: 'userId', type: '@core/id-type', required: true }, { name: 'token', type: 'string', required: true } ] }) Hyperschema.toDisk(schema) ``` ``` -------------------------------- ### ns.require(filename) Source: https://context7.com/holepunchto/hyperschema/llms.txt Associates an external JavaScript module with a namespace, allowing generated code to import types from that file. This must be called before registering types that reference external symbols, particularly for versioned types. ```APIDOC ## `ns.require(filename)` — Declare external type file for a namespace Associates an external JavaScript module with a namespace, enabling the generated code to import types (e.g., for `VersionedType` map functions) from that file. Must be called before registering types that reference external symbols. ### Parameters - **filename** (string) - The path to the external JavaScript module. ### Examples Pointing a namespace to an external module for versioned-type mapping: ```js const path = require('path') const Hyperschema = require('hyperschema') const schema = Hyperschema.from('./my-schema') const ns = schema.namespace('proto') // Point the namespace at an external module for versioned-type mapping ns.require(path.join(__dirname, './mappers.js')) ns.register({ name: 'v0', fields: [{ name: 'value', type: 'string', required: true }] }) ns.register({ name: 'v1', fields: [{ name: 'value', type: 'uint', required: true }] }) // mappers.js must export `map` ns.register({ name: 'message', versions: [ { version: 0, type: '@proto/v0', map: 'map' }, // decoded v0 → passed through map() { version: 2, type: '@proto/v1' } ] }) Hyperschema.toDisk(schema) ``` ``` -------------------------------- ### encode(name, value, [version]) / decode(name, buffer, [version]) Source: https://context7.com/holepunchto/hyperschema/llms.txt Convenience functions provided in the generated module for encoding and decoding data. These functions internally call `getEncoding` and the respective `c.encode` or `c.decode` methods. ```APIDOC ## `encode(name, value, [version])` / `decode(name, buffer, [version])` — Convenience encode/decode Shorthand helpers in the generated module that internally call `getEncoding` and `c.encode` / `c.decode`. Version defaults to the schema's `VERSION` constant. ### Parameters - **name** (string) - The name of the type to encode/decode (e.g., '@net/peer'). - **value** (any) - The data value to encode (for `encode`). - **buffer** (Buffer) - The buffer to decode (for `decode`). - **version** (number, optional) - The specific schema version to use for encoding/decoding. Defaults to the schema's `VERSION` constant. ### Examples Basic encode/decode: ```js const { encode, decode } = require('./my-schema') const buf = encode('@net/peer', { id: Buffer.alloc(32), address: '::1', port: 9000 }) const obj = decode('@net/peer', buf) // => { id: , address: '::1', port: 9000, seen: 0 } ``` Encode/decode at a specific old version: ```js const bufOld = encode('@net/peer', { id: Buffer.alloc(32), address: '::1' }, 1) const objOld = decode('@net/peer', bufOld, 1) ``` ``` -------------------------------- ### Persist Schema and Generated Code Source: https://context7.com/holepunchto/hyperschema/llms.txt Use `Hyperschema.toDisk()` to save the schema manifest (`schema.json`) and generated `compact-encoding` definitions (`index.js`). The schema version automatically increments if definitions change. Supports CommonJS (default) and ES module output. ```javascript const Hyperschema = require('hyperschema') const schema = Hyperschema.from('./my-schema') const ns = schema.namespace('app') ns.register({ name: 'message', fields: [ { name: 'id', type: 'uint', required: true }, { name: 'body', type: 'string', required: true }, { name: 'ts', type: 'uint' } ] }) // CJS output (default) Hyperschema.toDisk(schema) // ESM output Hyperschema.toDisk(schema, { esm: true }) // Write to a different directory than schema.dir Hyperschema.toDisk(schema, './output-dir', { esm: false }) // Produces: // ./my-schema/schema.json — version manifest // ./my-schema/index.js — generated encoders ``` -------------------------------- ### Load and Use Generated Compact-Encoding Structures Source: https://github.com/holepunchto/hyperschema/blob/main/README.md After Hyperschema generates `compact-encoding` definitions, you can load and use them to encode and decode data. The `resolveStruct` function retrieves the encoding for a specific struct version. ```javascript const c = require('compact-encoding') const { resolveStruct } = require('./schema') const encoding = resolveStruct('@namespace-1/basic-struct', 1) // { id: 10, other: 20 } c.decode(encoding, c.encode(encoding, { id: 10, other: 20 })) ``` -------------------------------- ### Update Schema Definition and Handle Versioning Source: https://github.com/holepunchto/hyperschema/blob/main/README.md Demonstrates updating a struct definition by adding an optional field. Hyperschema ensures that both old and new schema versions can be used for encoding/decoding after rebuilding. ```javascript ns1.register({ name: 'basic-struct', fields: [ { name: 'id', type: 'uint', required: true }, { name: 'other', type: 'uint' }, { name: 'another', type: 'string' } ] }) ``` -------------------------------- ### ns.register(definition) Source: https://context7.com/holepunchto/hyperschema/llms.txt Registers a type definition (struct, alias, enum, array, record, or versioned type) within a namespace. The structure of the `definition` object determines the type being created, and all registrations are validated against the existing schema. ```APIDOC ## `ns.register(definition)` — Register a struct, alias, enum, array, record, or versioned type The primary method for adding type definitions to a namespace. The shape of `definition` determines what kind of type is created. Every call is validated against the existing schema to enforce append-only rules. ### Parameters - **definition** (object) - An object defining the type to be registered. Its properties determine the type: - `name` (string) - The name of the type. - `alias` (string) - For creating an alias type. - `enum` (array) - For creating an enum type (values are numbers by default). - `strings` (boolean) - If true, enum values will be strings. - `compact` (boolean) - If true, creates a compact struct that cannot be extended. - `fields` (array) - For structs, an array of field definitions. - `name` (string) - The name of the field. - `type` (string) - The type of the field (can be a reference to another type). - `required` (boolean) - Whether the field is required. - `array` (boolean) - If true, creates an array type. - `type` (string) - The type of elements in an array. - `record` (boolean) - If true, creates a record (key-value map) type. - `key` (string) - The type of the keys in a record. - `value` (string) - The type of the values in a record. - `versions` (array) - For versioned types, an array of version definitions. - `version` (number) - The version number. - `type` (string) - The type definition for this version. - `map` (string) - A function name for mapping data. ### Examples Registering an alias: ```js ns.register({ name: 'node-id', alias: 'buffer' }) ``` Registering a numeric enum: ```js ns.register({ name: 'status', enum: ['pending', 'active', 'closed'] }) ``` Registering a string enum: ```js ns.register({ name: 'level', strings: true, enum: ['debug', 'info', 'error'] }) ``` Registering a compact struct: ```js ns.register({ name: 'point', compact: true, fields: [ { name: 'x', type: 'float64', required: true }, { name: 'y', type: 'float64', required: true } ] }) ``` Registering a regular struct with cross-namespace reference: ```js ns.register({ name: 'peer', fields: [ { name: 'id', type: '@net/node-id', required: true }, { name: 'address', type: 'string', required: true }, { name: 'port', type: 'uint' }, { name: 'seen', type: 'uint' } ] }) ``` Registering an array type: ```js ns.register({ name: 'peer-list', array: true, type: '@net/peer' }) ``` Registering a record type: ```js ns.register({ name: 'peer-map', record: true, key: 'string', value: '@net/peer' }) ``` ``` -------------------------------- ### Generate ESM Output with Hyperschema Source: https://github.com/holepunchto/hyperschema/blob/main/README.md Configure Hyperschema to generate ECMAScript Modules (ESM) output by setting the `esm` option to `true` in the `toDisk` function. ```javascript Hyperschema.toDisk(schema, { esm: true }) ``` -------------------------------- ### Control optional-field bitmask position with `flagsPosition` Source: https://context7.com/holepunchto/hyperschema/llms.txt The `flagsPosition` option allows specifying the zero-based index for the flags byte. By default, it's placed before the first optional field. Setting `flagsPosition: 0` places it as the very first byte, useful for protocol alignment or forward-compatible framing. ```javascript const Hyperschema = require('hyperschema') const schema = Hyperschema.from('./my-schema') const ns = schema.namespace('proto') ns.register({ name: 'frame', flagsPosition: 0, // flags are the very first encoded byte fields: [ { name: 'type', type: 'uint', required: true }, { name: 'payload', type: 'buffer', required: true }, { name: 'seq', type: 'uint' }, { name: 'ack', type: 'uint' } ] }) Hyperschema.toDisk(schema) ``` -------------------------------- ### resolveStruct(name, [version]) / getStruct(name, [version]) Source: https://context7.com/holepunchto/hyperschema/llms.txt Retrieves a versioned encoder/decoder object for a specified type name. These functions are typically found in the generated `index.js` file and are used for encoding and decoding data according to the schema. ```APIDOC ## `resolveStruct(name, [version])` / `getStruct(name, [version])` — Get a versioned encoder Returned by the **generated** `index.js`. Resolves a compact-encoding codec object for a named type pinned to a specific schema version. `resolveStruct` is an alias for `getStruct` kept for backwards compatibility. ### Parameters - **name** (string) - The name of the type to resolve (e.g., '@net/peer'). - **version** (number, optional) - The specific schema version to use. If omitted, the latest version is used. ### Returns - (object) - A compact-encoding codec object for the specified type and version. ### Examples Encode/decode at the latest schema version: ```js const c = require('compact-encoding') const { resolveStruct, getStruct } = require('./my-schema') const enc = resolveStruct('@net/peer') const buf = c.encode(enc, { id: Buffer.alloc(32), address: '127.0.0.1', port: 3000 }) const obj = c.decode(enc, buf) // => { id: , address: '127.0.0.1', port: 3000, seen: 0 } ``` Encode/decode at an older schema version (v1): ```js const encV1 = resolveStruct('@net/peer', 1) const bufV1 = c.encode(encV1, { id: Buffer.alloc(32), address: '10.0.0.1', port: 8000 }) const objV1 = c.decode(encV1, bufV1) ``` ``` -------------------------------- ### Declaring External Type Mappings with ns.require Source: https://context7.com/holepunchto/hyperschema/llms.txt Use `ns.require` to associate an external JavaScript module with a namespace, enabling generated code to import types for versioned type mapping functions. This must be called before registering types that reference external symbols. ```javascript const path = require('path') const Hyperschema = require('hyperschema') const schema = Hyperschema.from('./my-schema') const ns = schema.namespace('proto') // Point the namespace at an external module for versioned-type mapping ns.require(path.join(__dirname, './mappers.js')) ns.register({ name: 'v0', fields: [{ name: 'value', type: 'string', required: true }] }) ns.register({ name: 'v1', fields: [{ name: 'value', type: 'uint', required: true }] }) // mappers.js must export `map` ns.register({ name: 'message', versions: [ { version: 0, type: '@proto/v0', map: 'map' }, // decoded v0 → passed through map() { version: 2, type: '@proto/v1' } ] }) Hyperschema.toDisk(schema) ``` -------------------------------- ### Hyperschema Struct Definition Schema Source: https://github.com/holepunchto/hyperschema/blob/main/README.md Defines the structure for schema definitions, including name, optional compact flag, optional flagsPosition, and a list of fields. Fields can be built-in types or user-defined types. ```json { name: 'struct-name', compact?: true|false, flagsPosition?: -1, fields: [ { name: 'fieldName', type: 'uint' || '@namespace/another-type' // either a built-in or a predefined type }, ... ] } ``` -------------------------------- ### Registering Hyperschema Types with ns.register Source: https://context7.com/holepunchto/hyperschema/llms.txt Use `ns.register` to define new types within a namespace. The shape of the `definition` object determines the type being created (alias, enum, struct, array, record). Ensure types are registered before they are referenced by other types. ```javascript const Hyperschema = require('hyperschema') const schema = Hyperschema.from('./my-schema') const ns = schema.namespace('net') // Alias ns.register({ name: 'node-id', alias: 'buffer' }) // Enum (numeric values 1, 2, 3 by default) ns.register({ name: 'status', enum: ['pending', 'active', 'closed'] }) // Enum with string values ns.register({ name: 'level', strings: true, enum: ['debug', 'info', 'error'] }) // Compact struct (cannot be extended later) ns.register({ name: 'point', compact: true, fields: [ { name: 'x', type: 'float64', required: true }, { name: 'y', type: 'float64', required: true } ] }) // Regular struct with optional fields and cross-namespace reference ns.register({ name: 'peer', fields: [ { name: 'id', type: '@net/node-id', required: true }, { name: 'address', type: 'string', required: true }, { name: 'port', type: 'uint' }, { name: 'seen', type: 'uint' } ] }) // Array type ns.register({ name: 'peer-list', array: true, type: '@net/peer' }) // Record type (key→value map) ns.register({ name: 'peer-map', record: true, key: 'string', value: '@net/peer' }) Hyperschema.toDisk(schema) ``` -------------------------------- ### Resolving Struct Encoders with resolveStruct/getStruct Source: https://context7.com/holepunchto/hyperschema/llms.txt Use `resolveStruct` or `getStruct` from the generated `index.js` to obtain a compact-encoding codec object for a named type at a specific schema version. This is useful for encoding and decoding data according to the schema. ```javascript const c = require('compact-encoding') const { resolveStruct, getStruct } = require('./my-schema') // Encode/decode at the latest schema version const enc = resolveStruct('@net/peer') const buf = c.encode(enc, { id: Buffer.alloc(32), address: '127.0.0.1', port: 3000 }) const obj = c.decode(enc, buf) // => { id: , address: '127.0.0.1', port: 3000, seen: 0 } // Encode/decode at an older schema version (v1) — older peers still decode correctly const encV1 = resolveStruct('@net/peer', 1) const bufV1 = c.encode(encV1, { id: Buffer.alloc(32), address: '10.0.0.1', port: 8000 }) const objV1 = c.decode(encV1, bufV1) ``` -------------------------------- ### Versioned schema evolution with append-only field updates Source: https://context7.com/holepunchto/hyperschema/llms.txt Hyperschema guarantees backward compatibility when adding optional fields to a struct. Adding an optional field increments the schema version but allows buffers encoded with older versions to be decoded correctly. Required fields and existing field types cannot be changed without breaking compatibility. ```javascript const Hyperschema = require('hyperschema') const c = require('compact-encoding') // --- Build v1 --- let schema = Hyperschema.from('./evolving-schema') const ns = schema.namespace('app') ns.register({ name: 'user', fields: [ { name: 'id', type: 'uint', required: true }, { name: 'name', type: 'string', required: true } ] }) Hyperschema.toDisk(schema) // schema version → 1 // --- Evolve to v2 (add optional field) --- schema = Hyperschema.from('./evolving-schema') const ns2 = schema.namespace('app') ns2.register({ name: 'user', fields: [ { name: 'id', type: 'uint', required: true }, { name: 'name', type: 'string', required: true }, { name: 'email', type: 'string' } // new optional field ] }) Hyperschema.toDisk(schema) // schema version → 2 // --- Decode across versions --- const { resolveStruct } = require('./evolving-schema') const encV1 = resolveStruct('@app/user', 1) const encV2 = resolveStruct('@app/user', 2) // v1 buffer decoded with v2 encoder → email is null (default) const bufV1 = c.encode(encV1, { id: 1, name: 'Alice' }) console.log(c.decode(encV2, bufV1)) // => { id: 1, name: 'Alice', email: null } // v2 buffer decoded with v1 encoder → email silently ignored const bufV2 = c.encode(encV2, { id: 2, name: 'Bob', email: 'bob@example.com' }) console.log(c.decode(encV1, bufV2)) // => { id: 2, name: 'Bob' } ``` -------------------------------- ### Retrieving Enum Constant Maps with getEnum Source: https://context7.com/holepunchto/hyperschema/llms.txt Use `getEnum` to retrieve the object mapping enum key names to their encoded numeric or string values, as generated in `index.js`. This is useful for working with enum values in a type-safe manner. ```javascript const { getEnum, encode, decode } = require('./my-schema') const Status = getEnum('@net/status') // => { pending: 1, active: 2, closed: 3 } const buf = encode('@net/peer-status', { status: Status.active }) const obj = decode('@net/peer-status', buf) // => { status: 2 } // String enum const Level = getEnum('@net/level') // => { debug: 'debug', info: 'info', error: 'error' } ``` -------------------------------- ### getEnum(name) Source: https://context7.com/holepunchto/hyperschema/llms.txt Retrieves the enum constant map for a given enum name. This map provides the mapping between enum key names and their encoded numeric or string values. ```APIDOC ## `getEnum(name)` — Retrieve enum constant map Returns the object mapping enum key names to their encoded numeric (or string) values as generated in `index.js`. ### Parameters - **name** (string) - The name of the enum type (e.g., '@net/status'). ### Returns - (object) - An object mapping enum keys to their values. ### Examples Retrieving a numeric enum map: ```js const { getEnum, encode, decode } = require('./my-schema') const Status = getEnum('@net/status') // => { pending: 1, active: 2, closed: 3 } const buf = encode('@net/peer-status', { status: Status.active }) const obj = decode('@net/peer-status', buf) // => { status: 2 } ``` Retrieving a string enum map: ```js const Level = getEnum('@net/level') // => { debug: 'debug', info: 'info', error: 'error' } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.