{"name":"hyperschema","version":"1.12.2","author":{"name":"Holepunch"},"license":"Apache-2.0","_id":"hyperschema@1.12.2","maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"andrewosh","email":"andrewosh@gmail.com"}],"homepage":"https://github.com/holepunchto/hyperschema#readme","bugs":{"url":"https://github.com/holepunchto/hyperschema/issues"},"dist":{"shasum":"da99826dbc68bbc9ac00a86b9722c9ff1f650079","tarball":"http://registry-npm.ywwl.com/hyperschema/download/hyperschema-1.12.2.tgz","fileCount":9,"integrity":"sha512-7R8VMnP1UjaClINQG7gm22UOOzVkLlTCiynk7/TAOhrKHKOABq3eys5ZDLk4330t0HLLJH2W+/JOIOkjK6UkEQ==","signatures":[{"sig":"MEQCIEIoLiZOWl5slHoDv/+lsXLiPs+8JOvAlVFcdoZ1OQLWAiACqsv1dJq3b/Estb/CxW36aTarcPe2egETU5IFBDjAjA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":50448,"size":13118,"noattachment":false,"key":"/hyperschema/-/hyperschema-1.12.2.tgz"},"exports":{".":{"import":"./builder.mjs","default":"./builder.cjs"},"./package":"./package.json","./runtime":{"import":"./runtime.mjs","default":"./runtime.cjs"}},"gitHead":"200f07a3c952317842b805dac7f1dc724066a1c1","imports":{"fs":{"bare":"bare-fs","default":"fs"},"path":{"bare":"bare-path","default":"path"}},"scripts":{"test":"standard && brittle test/index.js","test:bare":"standard && bare test/index.js"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"repository":{"url":"git+https://github.com/holepunchto/hyperschema.git","type":"git"},"_npmVersion":"10.9.2","description":"Create registries of declarative compact-encoding schemas","directories":{},"_nodeVersion":"22.16.0","dependencies":{"bare-fs":"^4.0.1","generate-string":"^1.0.1","compact-encoding":"^2.15.0","generate-object-property":"^2.0.0"},"_hasShrinkwrap":false,"devDependencies":{"brittle":"^3.7.0","standard":"^17.1.0","test-tmp":"^1.3.0"},"_npmOperationalInternal":{"tmp":"tmp/hyperschema_1.12.2_1749482100671_0.3177444966942591","host":"s3://npm-registry-packages-npm-production"},"readme":"# hyperschema\n\nCreate and update declarative/versioned binary encoding definitions.\n\nHyperschema provides a schema builder and a code generator that's designed to enforce versioning rules across updates. This is particularly useful for P2P systems where different peers will be using different schema versions.\n\nEvery schema update produces a corresponding version bump, and due to the append-only rule, you'll always be able to encode/decode an object with a particular schema version.\n\n### Usage\n\nWith Hyperschema, you create namespaces and register struct definitions on those namespaces. The `from` function will attept to load an existing schema from an output directory. The `toDisk` function will write a `schema.json` file (for versioning) and a generated encodings file in `index.js`.\n\n```js\nconst Hyperschema = require('.')\n\nconst schema = Hyperschema.from('./schema')\nconst ns1 = schema.namespace('namespace-1')\nns1.register({\n  name: 'basic-struct',\n  fields: [\n    {\n      name: 'id',\n      type: 'uint',\n      required: true\n    },\n    {\n      name: 'other',\n      type: 'uint'\n    }\n  ]\n})\n\nHyperschema.toDisk(schema)\n```\n\nIf you want to generate as ESM, simply use `import` instead of `require` above or set the option explictly in `toDisk` like so\n\n```js\nHyperschema.toDisk(schema, { esm: true })\n```\n\n`index.js` will contain generated `compact-encoding` definitions. You can then load/use them as follows:\n\n```js\nconst c = require('compact-encoding')\nconst { resolveStruct } = require('./schema')\n\nconst encoding = resolveStruct('@namespace-1/basic-struct', 1)\n\n// { id: 10, other: 20 }\nc.decode(encoding, c.encode(encoding, { id: 10, other: 20 }))\n```\n\nYou can subsequently update your definition of `@namespace-1/basic-struct`, so long as that update follows append-only rules (i.e. only additional optional fields can be added).\n\nLet's say we perform this update:\n\n```js\nns1.register({\n  name: 'basic-struct',\n  fields: [\n    {\n      name: 'id',\n      type: 'uint',\n      required: true\n    },\n    {\n      name: 'other',\n      type: 'uint'\n    },\n    {\n      name: 'another',\n      type: 'string'\n    }\n  ]\n})\n```\n\nAfter rebuilding, you'll then be able to encode/decode with different versions of `@namespace-1/basic-struct`:\n\n```js\nconst encoding1 = resolveStruct('@namespace-1/basic-struct', 1)\nconst encoding2 = resolveStruct('@namespace-1/basic-struct', 2)\n\n// { id: 10, other: 20, another: null }\nc.decode(encoding1, c.encode(encoding1, { id: 10, other: 20, another: 'foo' }))\n// { id: 10, other: 20, another: 'foo' }\nc.decode(encoding2, c.encode(encoding2, { id: 10, other: 20, another: 'foo' }))\n```\n\n### Schema Definition\n\nAll struct definitions must take the following form:\n\n```\n{\n  name: 'struct-name',\n  compact?: true|false,\n  flagsPosition?: -1,\n  fields: [\n    {\n      name: 'fieldName',\n      type: 'uint' || '@namespace/another-type' // either a built-in or a predefined type\n    },\n    ...\n  ]\n}\n```\n\n#### Struct Definition\n\n- `name`: (required) A string name for you struct\n- `fields`: (required) (defined below)\n- `compact`: (optional) If true, this struct cannot be extended in the future (if embedded in another struct, will not frame the encoding)\n- `flagsPosition`: (optional) The position that the flags for optional fields should be encoded at (default to before first optional field)\n\n#### Struct Field Definitions\n\n- `name`: (required) The name of the field. This should be camel-case.\n- `type`: (required) Either a built-in type (i.e. `uint`) or a fully-qualified user-defined type (i.e. `@namespace/another-struct`)\n- `required`: (optional - default `false`) Is the field required\n- `array`: (optional - default `false`) Is the field an array of values. An array of `bool` is packed into a bitfield: a `uint` length prefix followed by `ceil(length / 8)` bytes, least significant bit first\n- `record`: (optional - default `false`) Is the field a record of key/values\n- `useDefault`: (optional - default `true`) If there is no value, use a default for the type\n- `inline`: (optional) Whether to recursively inline the field using the parent struct's flags bitfield for skipping non-required fields. This can make the encoded size smaller. A field's type must be set as `compact` to be inlined.\n- `constant`: (optional) A literal (`bool`, integer, `string` or `null`) that the decoder always emits for this field. A constant field is never encoded, takes no flag bit and adding one does not bump the schema version. Useful for runtime-only properties the caller overwrites after decoding (a seq, a cached value) so the decoded object has a stable shape. Cannot be combined with `required`, `array` or `inline`.\n\n#### Alias Definition\n\n- `name`: (required) The name of the alias.\n- `type`: (required) Either a built-in type (i.e. `uint`) or a fully-qualified user-defined type (i.e. `@namespace/another-struct`)\n\n#### Versioned Type Definition\n\n- `name`: (required) The name of the versioned type.\n- `versions`: (required) An array of `{ version, type, map }`, where `type` is the struct encoding that version and `map` optionally names an exported function that projects it onto the newest shape.\n- `framed`: (optional) When embedded in another struct, length-prefix the encoding for the inner version struct if it is not compact. New types default to `true`. A type loaded from a `schema.json` written before framing existed keeps its unframed layout; set `framed: true` on it to migrate, which changes the bytes it writes.\n\n### API\n\nHyperschema lets you define structs and aliases. All [`compact-encoding`](https://github.com/holepunchto/compact-encoding) types are available as built-in types.\n\n#### `const schema = Hyperschema.from(json|dir)`\n\nCreate a new Hyperschema instance, either from a JSON object or from an output directory path.\n\n#### `Hyperschema.toDisk(schema)`\n\nPersist the generated encodings for a Hyperschema instance (previously loaded with `Hyperschema.from(outputDir)`). If the encodings have changed, the version will be bumped.\n\n#### `const ns = schema.namespace(name)`\n\nReturn a new schema namespace. All structs/aliases for this namespace will be registered with the `@name` prefix. You can then reference these structs/aliases in subsequent definitions.\n\n#### `ns.register(definition)`\n\nRegister a new schema/alias definition on a namespace, as described in the Schema Definition section above.\n\n### License\n\nApache 2.0\n","publish_time":1749482100854,"_cnpm_publish_time":1749482100854,"dist-tags":{"latest":"1.26.2"}}