{"name":"hyperschema","version":"0.0.1","author":{"name":"Holepunch"},"license":"UNLICENSED","_id":"hyperschema@0.0.1","homepage":"https://github.com/holepunchto/hyperschema#readme","bugs":{"url":"https://github.com/holepunchto/hyperschema/issues"},"bin":{"hyperschema":"bin.js"},"dist":{"shasum":"41bead038ddfff2df0bd7eb687ac2bb2fa921a18","tarball":"http://registry-npm.ywwl.com/hyperschema/download/hyperschema-0.0.1.tgz","fileCount":13,"integrity":"sha512-E+tNN8/GhzK+KbCtf0DEz7OHRBxWKxf/4rRhG0v0CwCVRNXv7dk3GcnTGi6PchJzHGNI6yEvddtkVyKQsIm1+g==","signatures":[{"sig":"MEQCICj02HUAyd2GdidkwP/VBsQgwAkqwZtDlZxq6GKtGk1cAiB2I34Tcw4C5i1ZKQlzDSGZb+4MXxMqiSP3WLNpWRNgxw==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":52293,"size":11282,"noattachment":false,"key":"/hyperschema/-/hyperschema-0.0.1.tgz"},"main":"index.js","gitHead":"103d62e47205ca78d4559abb92ff4e0fa7be51de","scripts":{"test":"standard && brittle test/index.js"},"_npmUser":{"name":"andrewosh","email":"andrewosh@gmail.com"},"repository":{"url":"git+https://github.com/holepunchto/hyperschema.git","type":"git"},"_npmVersion":"10.5.2","description":"Create registries of declarative compact-encoding schemas","directories":{},"_nodeVersion":"20.13.0","dependencies":{"same-object":"^1.0.2","compact-encoding":"^2.15.0","generate-object-property":"^1.2.0"},"_hasShrinkwrap":false,"devDependencies":{"brittle":"^3.7.0","standard":"^17.1.0"},"_npmOperationalInternal":{"tmp":"tmp/hyperschema_0.0.1_1725448028659_0.9644719825651731","host":"s3://npm-registry-packages"},"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":1725448028796,"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"andrewosh","email":"andrewosh@gmail.com"}],"_cnpm_publish_time":1725448028796,"dist-tags":{"latest":"1.26.2"}}