{"name":"hyperschema","version":"1.26.2","description":"Create registries of declarative compact-encoding schemas","exports":{"./package":"./package.json",".":{"types":"./builder.d.ts","import":"./builder.mjs","default":"./builder.cjs"},"./runtime":{"import":"./runtime.mjs","default":"./runtime.cjs"},"./primitives":{"import":"./primitives.mjs","default":"./primitives.cjs"}},"imports":{"fs":{"bare":"bare-fs","default":"fs"},"path":{"bare":"bare-path","default":"path"}},"scripts":{"format":"prettier . --write","test":"prettier . --check && brittle test/index.js","test:bare":"prettier . --check && bare test/index.js"},"repository":{"type":"git","url":"git+https://github.com/holepunchto/hyperschema.git"},"author":{"name":"Holepunch"},"license":"Apache-2.0","bugs":{"url":"https://github.com/holepunchto/hyperschema/issues"},"homepage":"https://github.com/holepunchto/hyperschema#readme","dependencies":{"bare-fs":"^4.0.1","compact-encoding":"^3.5.0","generate-object-property":"^2.0.0","generate-string":"^1.0.1"},"devDependencies":{"brittle":"^3.7.0","hyperschema-test":"^1.1.0","prettier":"^3.6.2","prettier-config-holepunch":"^2.0.0","test-tmp":"^1.3.0"},"gitHead":"dcf65cd1409ba7494f4adfbc8d013dea09cca79e","_id":"hyperschema@1.26.2","_nodeVersion":"24.20.0","_npmVersion":"11.19.0","dist":{"integrity":"sha512-5Y7C5zCfH28yiK7RhuMl7VkWt24Mr0E5sp1r2ntd3Ab3+yOBdkDWau0/d/F5beC799Vd5e4xqIAu5NJLM7i//w==","shasum":"d3e82b5ad087702e2b43a2d82b8344dbfaf1d463","tarball":"http://registry-npm.ywwl.com/hyperschema/download/hyperschema-1.26.2.tgz","fileCount":12,"unpackedSize":72760,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/hyperschema@1.26.2","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQDU0hmMha5nuLAcZBDYNJMwujxxgpHDlG0OsXvOghxzGwIgKkLQO48SiIFWKLqFII8PEyKyAxQ43IsCmMs+OfVhX+A="}],"size":18828,"noattachment":false,"key":"/hyperschema/-/hyperschema-1.26.2.tgz"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:f72b358d-4e50-40c6-a4cd-463c5078420b"}},"directories":{},"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/hyperschema_1.26.2_1789471735911_0.9823095235289603"},"_hasShrinkwrap":false,"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":1789471736064,"_cnpm_publish_time":1789471736064,"dist-tags":{"latest":"1.26.2"}}