{"name":"hyperschema","version":"1.2.0","author":{"name":"Holepunch"},"license":"UNLICENSED","_id":"hyperschema@1.2.0","homepage":"https://github.com/holepunchto/hyperschema#readme","bugs":{"url":"https://github.com/holepunchto/hyperschema/issues"},"bin":{"hyperschema":"bin.js"},"dist":{"shasum":"b815af7001fdf98c7e79c2a5234f56d542c1e183","tarball":"http://registry-npm.ywwl.com/hyperschema/download/hyperschema-1.2.0.tgz","fileCount":7,"integrity":"sha512-H/x5UE+EboYWGxX7oxp9DpeK7R0HpYfKcCbKm8fCs1gtfYYrDGGr1kP8jMOl9yGadzHXQ+s9TO3SFJ4h9UkrAA==","signatures":[{"sig":"MEQCICeIjqaoSinvX5m+lPB02gpiXm4otMd/d1bjRGSFFEAyAiAxaGStDAlEydiCDoPXvdzj9r4bDQW4ZZibsYkanzqb9A==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":34402,"size":10202,"noattachment":false,"key":"/hyperschema/-/hyperschema-1.2.0.tgz"},"main":"index.js","gitHead":"5546072f67bf23978a73840417a3d13dd28262cb","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.0","description":"Create registries of declarative compact-encoding schemas","directories":{},"_nodeVersion":"22.12.0","dependencies":{"bare-fs":"^4.0.1","same-object":"^1.0.2","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.2.0_1735395643085_0.738616389477371","host":"s3://npm-registry-packages-npm-production"},"readme":"# hyperschema\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\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```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```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(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```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```js\nconst encoding1 = resolveStruct('@namespace-1/basic-struct', 1)\nconst encoding2 = resolveStruct('@namespace-1/basic-struct', 2)\n\n// { id: 10, other: 20 }\nc.decode(c.encode(encoding1, { id: 10, other: 20, another: 30 }))\n// { id: 10, other: 20, another: 30 }\nc.decode(c.encode(encoding2, { id: 10, other: 20, another: 30 }))\n```\n\n### Schema Definition\nAll struct definitions must take the following form:\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* `name`: (required) A string name for you struct\n* `fields`: (required) (defined below)\n* `compact`: (optional) If this struct will 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* `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\n#### Alias Definition\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### API\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)`\nCreate a new Hyperschema instance, either from a JSON object or from an output directory path.\n\n#### `Hyperschema.toDisk(schema)`\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)`\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#### `schema.register(definition)`\nRegister a new schema/alias definition, as described in the Schema Definition section above.\n\n### License\nApache 2.0\n","publish_time":1735395643260,"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"andrewosh","email":"andrewosh@gmail.com"}],"_cnpm_publish_time":1735395643260,"dist-tags":{"latest":"1.26.2"}}