feat: hyperbee-chat - P2P Hyperbee chat prototype (KV msgs, swarm discovery/replicate)
Writer appends timestamped msgs every 3s. Reader scans recent. Separate storages, named core. Local test silent (no output/exit 0, no storage) - deps ok per npm.
This commit is contained in:
+201
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
# hyperschema
|
||||
|
||||
Create and update declarative/versioned binary encoding definitions.
|
||||
|
||||
Hyperschema 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.
|
||||
|
||||
Every 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.
|
||||
|
||||
### Usage
|
||||
|
||||
With 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`.
|
||||
|
||||
```js
|
||||
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)
|
||||
```
|
||||
|
||||
If you want to generate as ESM, simply use `import` instead of `require` above or set the option explictly in `toDisk` like so
|
||||
|
||||
```js
|
||||
Hyperschema.toDisk(schema, { esm: true })
|
||||
```
|
||||
|
||||
`index.js` will contain generated `compact-encoding` definitions. You can then load/use them as follows:
|
||||
|
||||
```js
|
||||
const c = require('compact-encoding')
|
||||
const { resolveStruct } = require('./schema')
|
||||
|
||||
const encoding = resolveStruct('@namespace-1/basic-struct', 1)
|
||||
|
||||
// { id: 10, other: 20 }
|
||||
c.decode(c.encode(encoding, { id: 10, other: 20 }))
|
||||
```
|
||||
|
||||
You 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).
|
||||
|
||||
Let's say we perform this update:
|
||||
|
||||
```js
|
||||
ns1.register({
|
||||
name: 'basic-struct',
|
||||
fields: [
|
||||
{
|
||||
name: 'id',
|
||||
type: 'uint',
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: 'other',
|
||||
type: 'uint'
|
||||
},
|
||||
{
|
||||
name: 'another'.
|
||||
type: 'string'
|
||||
}
|
||||
]
|
||||
})
|
||||
```
|
||||
|
||||
After rebuilding, you'll then be able to encode/decode with different versions of `@namespace-1/basic-struct`:
|
||||
|
||||
```js
|
||||
const encoding1 = resolveStruct('@namespace-1/basic-struct', 1)
|
||||
const encoding2 = resolveStruct('@namespace-1/basic-struct', 2)
|
||||
|
||||
// { id: 10, other: 20 }
|
||||
c.decode(c.encode(encoding1, { id: 10, other: 20, another: 30 }))
|
||||
// { id: 10, other: 20, another: 30 }
|
||||
c.decode(c.encode(encoding2, { id: 10, other: 20, another: 30 }))
|
||||
```
|
||||
|
||||
### Schema Definition
|
||||
|
||||
All struct definitions must take the following form:
|
||||
|
||||
```
|
||||
{
|
||||
name: 'struct-name',
|
||||
compact?: true|false,
|
||||
flagsPosition?: -1,
|
||||
fields: [
|
||||
{
|
||||
name: 'fieldName',
|
||||
type: 'uint' || '@namespace/another-type' // either a built-in or a predefined type
|
||||
},
|
||||
...
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
#### Struct Definition
|
||||
|
||||
- `name`: (required) A string name for you struct
|
||||
- `fields`: (required) (defined below)
|
||||
- `compact`: (optional) If true, this struct cannot be extended in the future (if embedded in another struct, will not frame the encoding)
|
||||
- `flagsPosition`: (optional) The position that the flags for optional fields should be encoded at (default to before first optional field)
|
||||
|
||||
#### Struct Field Definitions
|
||||
|
||||
- `name`: (required) The name of the field. This should be camel-case.
|
||||
- `type`: (required) Either a built-in type (i.e. `uint`) or a fully-qualified user-defined type (i.e. `@namespace/another-struct`)
|
||||
- `required`: (optional - default `false`) Is the field required
|
||||
- `array`: (optional - default `false`) Is the field an array of values
|
||||
- `record`: (optional - default `false`) Is the field a record of key/values
|
||||
- `useDefault`: (optional - default `true`) If there is no value, use a default for the type
|
||||
- `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.
|
||||
|
||||
#### Alias Definition
|
||||
|
||||
- `name`: (required) The name of the alias.
|
||||
- `type`: (required) Either a built-in type (i.e. `uint`) or a fully-qualified user-defined type (i.e. `@namespace/another-struct`)
|
||||
|
||||
### API
|
||||
|
||||
Hyperschema lets you define structs and aliases. All [`compact-encoding`](https://github.com/holepunchto/compact-encoding) types are available as built-in types.
|
||||
|
||||
#### `const schema = Hyperschema.from(json|dir)`
|
||||
|
||||
Create a new Hyperschema instance, either from a JSON object or from an output directory path.
|
||||
|
||||
#### `Hyperschema.toDisk(schema)`
|
||||
|
||||
Persist the generated encodings for a Hyperschema instance (previously loaded with `Hyperschema.from(outputDir)`). If the encodings have changed, the version will be bumped.
|
||||
|
||||
#### `const ns = schema.namespace(name)`
|
||||
|
||||
Return 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.
|
||||
|
||||
#### `ns.register(definition)`
|
||||
|
||||
Register a new schema/alias definition on a namespace, as described in the Schema Definition section above.
|
||||
|
||||
### License
|
||||
|
||||
Apache 2.0
|
||||
+743
@@ -0,0 +1,743 @@
|
||||
const fs = require('fs')
|
||||
const p = require('path')
|
||||
|
||||
const {
|
||||
SupportedTypes,
|
||||
BitwiseNumericTypes,
|
||||
getBitwiseSize,
|
||||
getDefaultValue
|
||||
} = require('./lib/types.js')
|
||||
|
||||
const generateCode = require('./lib/codegen')
|
||||
|
||||
const JSON_FILE_NAME = 'schema.json'
|
||||
const CODE_FILE_NAME = 'index.js'
|
||||
|
||||
class ResolvedType {
|
||||
constructor(hyperschema, fqn, description, existing) {
|
||||
this.hyperschema = hyperschema
|
||||
this.description = description
|
||||
this.name = description?.name || fqn
|
||||
this.namespace = description?.namespace || ''
|
||||
this.derived = description.derived
|
||||
this.existing = existing
|
||||
this.fqn = fqn
|
||||
|
||||
this.isPrimitive = false
|
||||
this.isEnum = false
|
||||
this.isStruct = false
|
||||
this.isArray = false
|
||||
this.isRecord = false
|
||||
this.isAlias = false
|
||||
this.isExternal = false
|
||||
this.isVersioned = false
|
||||
|
||||
this.version = -1
|
||||
}
|
||||
|
||||
link() {}
|
||||
|
||||
postlink() {}
|
||||
|
||||
frameable() {
|
||||
return false
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
name: this.name,
|
||||
namespace: this.namespace
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Primitive extends ResolvedType {
|
||||
constructor(name) {
|
||||
super(null, name, name, null)
|
||||
this.isPrimitive = true
|
||||
this.bool = this.name === 'bool'
|
||||
this.bitwise = getBitwiseSize(this.name)
|
||||
this.default = getDefaultValue(this.name)
|
||||
}
|
||||
|
||||
static AllPrimitives = new Map(
|
||||
[...SupportedTypes].map((name) => {
|
||||
return [name, new this(name)]
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
class Alias extends ResolvedType {
|
||||
constructor(hyperschema, fqn, description, existing) {
|
||||
super(hyperschema, fqn, description, existing)
|
||||
this.isAlias = true
|
||||
|
||||
this.type = hyperschema.resolve(description.alias)
|
||||
if (!this.type)
|
||||
throw new Error(`Cannot resolve alias target ${description.alias} in ${description.name}`)
|
||||
|
||||
this.default = this.type.default
|
||||
|
||||
if (existing) {
|
||||
if (existing.type.name !== this.type.name) {
|
||||
throw new Error(`Remapping an alias: ${fqn}`)
|
||||
}
|
||||
this.version = existing.version
|
||||
} else if (!this.derived) {
|
||||
this.hyperschema.maybeBumpVersion()
|
||||
this.version = this.hyperschema.initializing
|
||||
? this.description.version
|
||||
: this.hyperschema.version
|
||||
}
|
||||
}
|
||||
|
||||
frameable() {
|
||||
return this.type.frameable()
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
name: this.name,
|
||||
namespace: this.namespace,
|
||||
alias: this.type.fqn,
|
||||
version: this.version
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ExternalType extends ResolvedType {
|
||||
constructor(hyperschema, fqn, description, existing) {
|
||||
super(hyperschema, fqn, description, existing)
|
||||
|
||||
this.isExternal = true
|
||||
this.default = null
|
||||
this.filename = hyperschema.namespaces.get(description.namespace)?.external || null
|
||||
this.external = description.external
|
||||
}
|
||||
|
||||
require(filename) {
|
||||
return p.relative(p.join(filename, '..'), p.resolve(this.filename)).replaceAll('\\', '/')
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
name: this.description.name,
|
||||
namespace: this.namespace,
|
||||
external: this.description.external
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Enum extends ResolvedType {
|
||||
constructor(hyperschema, fqn, description, existing) {
|
||||
super(hyperschema, fqn, description, existing)
|
||||
|
||||
this.isEnum = true
|
||||
this.enum = []
|
||||
this.offset = typeof description.offset === 'number' ? description.offset : 1
|
||||
this.default = description.strings ? null : 0
|
||||
this.strings = !!description.strings
|
||||
|
||||
if (this.existing) {
|
||||
if (!this.existing.enum) {
|
||||
throw new Error('Previous declaration was not an enum')
|
||||
}
|
||||
|
||||
if (this.existing.enum.length > description.enum.length) {
|
||||
throw new Error('An enum value was removed')
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < description.enum.length; i++) {
|
||||
const d = description.enum[i]
|
||||
const key = typeof d === 'string' ? d : d.key
|
||||
const prev = i < this.existing?.enum.length ? this.existing.enum[i] : null
|
||||
|
||||
if (prev && prev.key !== key) {
|
||||
throw new Error(`Enum ${i} in ${fqn} changed. Was "${prev.key}" but is now "${key}`)
|
||||
}
|
||||
|
||||
if (!prev) {
|
||||
hyperschema.maybeBumpVersion()
|
||||
}
|
||||
|
||||
this.enum.push({
|
||||
key,
|
||||
version: hyperschema.initializing ? d.version : prev ? prev.version : hyperschema.version
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
name: this.description.name,
|
||||
namespace: this.namespace,
|
||||
offset: this.offset,
|
||||
enum: this.enum,
|
||||
strings: this.strings
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class StructField {
|
||||
constructor(hyperschema, struct, position, flag, description) {
|
||||
this.hyperschema = hyperschema
|
||||
this.description = description
|
||||
this.name = this.description.name
|
||||
this.required = this.description.required
|
||||
this.external = this.description.external
|
||||
this.inline = this.description.inline
|
||||
this.useDefault = this.description.useDefault !== false
|
||||
|
||||
this.position = position
|
||||
this.struct = struct
|
||||
this.flag = flag
|
||||
|
||||
this.type = hyperschema.resolve(description.type) || null
|
||||
this.typeFqn = this.type ? this.type.fqn : description.type
|
||||
|
||||
this.array = !!this.description.array
|
||||
this.record = !!this.description.record
|
||||
|
||||
if (this.record) {
|
||||
throw new Error(`Record not supported as field. Use @example/my-record`)
|
||||
}
|
||||
|
||||
this.version = description.version || hyperschema.version
|
||||
|
||||
if (this.struct.existing) {
|
||||
const tag = `${this.struct.fqn}/${this.description.name}`
|
||||
const prevField = this.struct.existing.fields[position]
|
||||
|
||||
if (prevField) {
|
||||
if (prevField.typeFqn !== this.typeFqn) {
|
||||
throw new Error(`Field was modified: ${tag}`)
|
||||
} else if (prevField.required !== this.required) {
|
||||
throw new Error(`A required field must always stay required: ${tag}`)
|
||||
}
|
||||
this.version = prevField.version
|
||||
} else if (!this.struct.derived) {
|
||||
hyperschema.maybeBumpVersion()
|
||||
this.version = hyperschema.version
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getDefaultValue() {
|
||||
return !this.useDefault ? undefined : this.array ? null : this.type.default
|
||||
}
|
||||
|
||||
link() {
|
||||
if (this.type === null) this.type = this.hyperschema.resolve(this.description.type) || null
|
||||
if (this.type === null) {
|
||||
throw new Error(`Cannot resolve field type ${this.description.type} in ${this.name}`)
|
||||
}
|
||||
}
|
||||
|
||||
get framed() {
|
||||
return this.type.frameable()
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
name: this.description.name,
|
||||
required: this.description.required,
|
||||
array: this.description.array,
|
||||
record: this.description.record,
|
||||
inline: this.description.inline,
|
||||
type: this.typeFqn,
|
||||
version: this.version
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Array extends ResolvedType {
|
||||
constructor(hyperschema, fqn, description, existing) {
|
||||
super(hyperschema, fqn, description, existing)
|
||||
this.isArray = true
|
||||
this.default = null
|
||||
|
||||
if (!description.type) {
|
||||
throw new Error(`Array ${this.fqn}: required 'type' definition is missing`)
|
||||
}
|
||||
|
||||
this.type = hyperschema.resolve(description.type)
|
||||
this.framed = this.type.frameable()
|
||||
|
||||
if (!description.name) {
|
||||
throw new Error(`Array ${this.fqn}: required 'name' definition is missing`)
|
||||
}
|
||||
|
||||
if (!description.namespace) {
|
||||
throw new Error(`Array ${this.fqn}: required 'namespace' definition is missing`)
|
||||
}
|
||||
|
||||
if (this.existing) {
|
||||
if (this.existing.type.fqn !== this.type.fqn) {
|
||||
throw new Error(`Array was modified: ${this.fqn}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
name: this.name,
|
||||
namespace: this.namespace,
|
||||
array: true,
|
||||
type: this.type.fqn
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Record extends ResolvedType {
|
||||
constructor(hyperschema, fqn, description, existing) {
|
||||
super(hyperschema, fqn, description, existing)
|
||||
this.isRecord = true
|
||||
this.default = null
|
||||
|
||||
if (!description.key) {
|
||||
throw new Error(`Record ${this.fqn}: required 'key' definition is missing`)
|
||||
}
|
||||
|
||||
if (!description.value) {
|
||||
throw new Error(`Record ${this.fqn}: required 'value' definition is missing`)
|
||||
}
|
||||
|
||||
this.key = hyperschema.resolve(description.key)
|
||||
this.value = hyperschema.resolve(description.value)
|
||||
|
||||
if (!description.name) {
|
||||
throw new Error(`Record ${this.fqn}: required 'name' definition is missing`)
|
||||
}
|
||||
|
||||
if (!description.namespace) {
|
||||
throw new Error(`Record ${this.fqn}: required 'namespace' definition is missing`)
|
||||
}
|
||||
|
||||
if (this.existing) {
|
||||
if (this.existing.key.fqn !== this.key.fqn || this.existing.value.fqn !== this.value.fqn) {
|
||||
throw new Error(`Record was modified: ${this.fqn}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
name: this.name,
|
||||
namespace: this.namespace,
|
||||
record: true,
|
||||
key: this.key.fqn,
|
||||
value: this.value.fqn
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class VersionedType extends ResolvedType {
|
||||
constructor(hyperschema, fqn, description, existing) {
|
||||
super(hyperschema, fqn, description, existing)
|
||||
this.isVersioned = true
|
||||
this.default = null
|
||||
this.filename = hyperschema.namespaces.get(description.namespace)?.external || null
|
||||
|
||||
if (!description.versions) {
|
||||
throw new Error(`VersionedType ${this.fqn}: required 'versions' definition is missing`)
|
||||
}
|
||||
|
||||
this.versions = description.versions.map((v) => {
|
||||
return {
|
||||
type: hyperschema.resolve(v.type),
|
||||
version: v.version,
|
||||
map: v.map || null
|
||||
}
|
||||
})
|
||||
|
||||
for (const v of this.versions) {
|
||||
v.type.expectsVersion = true
|
||||
}
|
||||
|
||||
this.framed = true
|
||||
|
||||
if (!description.name) {
|
||||
throw new Error(`VersionedType ${this.fqn}: required 'name' definition is missing`)
|
||||
}
|
||||
|
||||
if (!description.namespace) {
|
||||
throw new Error(`VersionedType ${this.fqn}: required 'namespace' definition is missing`)
|
||||
}
|
||||
|
||||
if (this.existing) {
|
||||
if (this.existing.fqn !== fqn) {
|
||||
throw new Error(`VersionedType was modified: ${this.fqn}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
require(filename) {
|
||||
return p.relative(p.join(filename, '..'), p.resolve(this.filename)).replaceAll('\\', '/')
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
name: this.name,
|
||||
namespace: this.namespace,
|
||||
versions: this.versions.map((version) => ({
|
||||
type: version.type.fqn,
|
||||
map: version.map,
|
||||
version: version.version
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Struct extends ResolvedType {
|
||||
constructor(hyperschema, fqn, description, existing) {
|
||||
super(hyperschema, fqn, description, existing)
|
||||
this.isStruct = true
|
||||
this.isInlined = false
|
||||
|
||||
this.default = null
|
||||
this.expectsVersion = false
|
||||
|
||||
this.fields = []
|
||||
this.fieldsByName = new Map()
|
||||
this.resolvedFields = []
|
||||
|
||||
this.optionals = []
|
||||
this.maxFlag = 0
|
||||
this.flagsPosition = -1
|
||||
|
||||
this.linked = false
|
||||
|
||||
this.compact = !!description.compact
|
||||
|
||||
if (Number.isInteger(description.flagsPosition)) {
|
||||
this.flagsPosition = description.flagsPosition
|
||||
}
|
||||
|
||||
if (!description.name) {
|
||||
throw new Error(`Struct ${this.fqn}: required 'name' definition is missing`)
|
||||
}
|
||||
|
||||
if (!description.fields) {
|
||||
throw new Error(`Struct ${this.fqn}: required 'fields' definition is missing`)
|
||||
}
|
||||
|
||||
if (this.existing) {
|
||||
const oldLength = this.existing.fields.length
|
||||
const newLength = this.description.fields.length
|
||||
if (oldLength > newLength) {
|
||||
throw new Error(`A field was removed: ${this.fqn}`)
|
||||
} else if (this.compact && oldLength !== newLength) {
|
||||
throw new Error(`A compact struct was expanded: ${this.fqn}`)
|
||||
}
|
||||
} else if (!this.derived) {
|
||||
this.hyperschema.maybeBumpVersion()
|
||||
this.version = this.hyperschema.initializing
|
||||
? this.description.version
|
||||
: this.hyperschema.version
|
||||
}
|
||||
|
||||
let indexBeforeOptional = -1
|
||||
for (let i = 0; i < description.fields.length; i++) {
|
||||
const fieldDescription = description.fields[i]
|
||||
|
||||
// bools or bitwise uints can only be set in the flag, so auto downgrade the from required
|
||||
// TODO: if we add semantic meaning to required, ie "user MUST set this", we should
|
||||
// add an additional state for this
|
||||
if (fieldDescription.required) {
|
||||
if (fieldDescription.type === 'bool' || BitwiseNumericTypes.has(fieldDescription.type)) {
|
||||
fieldDescription.required = false
|
||||
}
|
||||
}
|
||||
const flag = !fieldDescription.required ? (this.maxFlag === 0 ? 1 : this.maxFlag * 2) : 0
|
||||
const field = new StructField(hyperschema, this, i, flag, fieldDescription)
|
||||
|
||||
if (fieldDescription.inline) {
|
||||
if (fieldDescription.record) {
|
||||
throw new Error(`Struct ${this.fqn}: Records cannot be inlined`)
|
||||
}
|
||||
if (!field.type.compact) throw new Error(`Struct ${this.fqn}: inline requires compact`)
|
||||
if (fieldDescription.array) {
|
||||
throw new Error(`Struct ${this.fqn}: Arrays cannot be inlined`)
|
||||
}
|
||||
}
|
||||
|
||||
this.fields.push(field)
|
||||
this.fieldsByName.set(field.name, field)
|
||||
|
||||
if (!fieldDescription.required) {
|
||||
this.optionals.push(field)
|
||||
this.maxFlag = flag
|
||||
}
|
||||
if (indexBeforeOptional === -1 && (!fieldDescription.required || fieldDescription.inline)) {
|
||||
indexBeforeOptional = i
|
||||
if (this.flagsPosition === -1) {
|
||||
this.flagsPosition = i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.flagsPosition > indexBeforeOptional) {
|
||||
throw new Error(
|
||||
`Struct ${this.fqn}: flagsPosition (${this.flagsPosition}) must be before optional fields (max ${indexBeforeOptional})`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
_resolveField(field, flag, shift) {
|
||||
const bitwise = getBitwiseSize(field.type.fqn)
|
||||
const entry = {
|
||||
bits: bitwise ? bitwise - 1 : 0, // -1 cause its optional always
|
||||
field,
|
||||
flag,
|
||||
shift,
|
||||
fields: []
|
||||
}
|
||||
|
||||
if (!field.type.isStruct || !field.inline) {
|
||||
return entry
|
||||
}
|
||||
|
||||
const bits = entry.bits
|
||||
for (const f of field.type.fields) {
|
||||
if (!f.required) {
|
||||
entry.bits++
|
||||
flag = nextFlag(flag, 1)
|
||||
}
|
||||
|
||||
const next = this._resolveField(f, flag, entry.bits - bits)
|
||||
|
||||
entry.bits += next.bits
|
||||
entry.fields.push(next)
|
||||
|
||||
flag = nextFlag(flag, next.bits)
|
||||
}
|
||||
|
||||
return entry
|
||||
}
|
||||
|
||||
link() {
|
||||
for (const f of this.fields) f.link()
|
||||
}
|
||||
|
||||
postlink() {
|
||||
if (this.linked) return
|
||||
this.linked = true
|
||||
|
||||
let bits = 0
|
||||
let maxIncrease = 1
|
||||
let flag = 0
|
||||
|
||||
for (const f of this.fields) {
|
||||
f.flag *= maxIncrease
|
||||
if (f.inline && f.type.isStruct) f.type.isInlined = true
|
||||
|
||||
const b = f.required ? bits : bits + 1
|
||||
if (!f.required) {
|
||||
flag = nextFlag(flag, 1)
|
||||
bits++
|
||||
}
|
||||
const next = this._resolveField(f, flag, bits)
|
||||
bits += next.bits
|
||||
flag = nextFlag(flag, next.bits)
|
||||
this.resolvedFields.push(next)
|
||||
|
||||
if (next.bits) maxIncrease *= 2 ** next.bits
|
||||
}
|
||||
|
||||
this.maxFlag *= maxIncrease
|
||||
}
|
||||
|
||||
frameable() {
|
||||
return !this.compact
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
name: this.name,
|
||||
namespace: this.namespace,
|
||||
compact: this.compact,
|
||||
flagsPosition: this.flagsPosition,
|
||||
fields: this.fields.map((f) => f.toJSON())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class HyperschemaNamespace {
|
||||
constructor(hyperschema, name) {
|
||||
this.hyperschema = hyperschema
|
||||
this.name = name
|
||||
this.external = null
|
||||
}
|
||||
|
||||
require(filename) {
|
||||
this.external = filename
|
||||
}
|
||||
|
||||
register(description) {
|
||||
return this.hyperschema.register({
|
||||
...description,
|
||||
namespace: this.name
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = class Hyperschema {
|
||||
constructor(json, { dir = null, versioned = true } = {}) {
|
||||
this.version = json ? json.version : versioned ? 0 : 1
|
||||
this.versioned = versioned
|
||||
this.schema = []
|
||||
this.dir = dir
|
||||
|
||||
this.namespaces = new Map()
|
||||
this.positionsByType = new Map()
|
||||
this.typesByPosition = new Map()
|
||||
this.types = new Map()
|
||||
|
||||
this.changed = false
|
||||
this.initializing = true
|
||||
|
||||
if (json) {
|
||||
for (let i = 0; i < json.schema.length; i++) {
|
||||
const description = json.schema[i]
|
||||
this.register(description)
|
||||
}
|
||||
}
|
||||
this.initializing = false
|
||||
this.linkAll() // link all existing ones
|
||||
}
|
||||
|
||||
static esm = false
|
||||
|
||||
_getFullyQualifiedName(description) {
|
||||
if (description.namespace === null) return description.name
|
||||
return '@' + description.namespace + '/' + description.name
|
||||
}
|
||||
|
||||
maybeBumpVersion() {
|
||||
if (this.changed || this.initializing) return
|
||||
this.changed = true
|
||||
if (this.versioned) this.version += 1
|
||||
}
|
||||
|
||||
linkAll() {
|
||||
for (const t of this.types.values()) t.link()
|
||||
for (const t of this.types.values()) t.postlink()
|
||||
}
|
||||
|
||||
register(description) {
|
||||
const fqn = this._getFullyQualifiedName(description)
|
||||
const existing = this.types.get(fqn)
|
||||
const existingPosition = this.positionsByType.get(fqn)
|
||||
|
||||
let type = null
|
||||
if (description.alias) {
|
||||
type = new Alias(this, fqn, description, existing)
|
||||
} else if (description.enum) {
|
||||
type = new Enum(this, fqn, description, existing)
|
||||
} else if (description.array) {
|
||||
type = new Array(this, fqn, description, existing)
|
||||
} else if (description.record) {
|
||||
type = new Record(this, fqn, description, existing)
|
||||
} else if (description.external) {
|
||||
type = new ExternalType(this, fqn, description, existing)
|
||||
} else if (description.versions) {
|
||||
type = new VersionedType(this, fqn, description, existing)
|
||||
} else {
|
||||
type = new Struct(this, fqn, description, existing)
|
||||
}
|
||||
this.types.set(fqn, type)
|
||||
|
||||
const json = type.toJSON()
|
||||
if (existing) {
|
||||
this.schema[existingPosition] = json
|
||||
} else {
|
||||
const position = this.schema.push(json) - 1
|
||||
this.positionsByType.set(fqn, position)
|
||||
this.typesByPosition.set(position, fqn)
|
||||
}
|
||||
|
||||
return type
|
||||
}
|
||||
|
||||
namespace(name) {
|
||||
if (this.namespaces.has(name)) throw new Error('Namespace already exists')
|
||||
const ns = new HyperschemaNamespace(this, name)
|
||||
this.namespaces.set(name, ns)
|
||||
return ns
|
||||
}
|
||||
|
||||
resolve(fqn, { aliases = true } = {}) {
|
||||
if (Primitive.AllPrimitives.has(fqn)) return Primitive.AllPrimitives.get(fqn)
|
||||
const type = this.types.get(fqn)
|
||||
if (!aliases && type.isAlias) return type.type
|
||||
return type
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
this.linkAll()
|
||||
|
||||
const json = {
|
||||
version: this.version,
|
||||
schema: this.schema.filter((t) => !t.derived)
|
||||
}
|
||||
return json
|
||||
}
|
||||
|
||||
toCode({ esm = this.constructor.esm, filename } = {}) {
|
||||
this.linkAll()
|
||||
|
||||
return generateCode(this, { esm, filename })
|
||||
}
|
||||
|
||||
static toDisk(hyperschema, dir, opts) {
|
||||
if (typeof dir === 'object' && dir) {
|
||||
opts = dir
|
||||
dir = null
|
||||
}
|
||||
|
||||
hyperschema.linkAll()
|
||||
|
||||
if (!dir) dir = hyperschema.dir
|
||||
fs.mkdirSync(dir, { recursive: true })
|
||||
|
||||
const jsonPath = p.join(p.resolve(dir), JSON_FILE_NAME)
|
||||
const codePath = p.join(p.resolve(dir), CODE_FILE_NAME)
|
||||
|
||||
fs.writeFileSync(jsonPath, JSON.stringify(hyperschema.toJSON(), null, 2) + '\n', {
|
||||
encoding: 'utf-8'
|
||||
})
|
||||
fs.writeFileSync(codePath, hyperschema.toCode({ ...opts, filename: codePath }), {
|
||||
encoding: 'utf-8'
|
||||
})
|
||||
}
|
||||
|
||||
static from(json, opts) {
|
||||
if (typeof json === 'string') {
|
||||
const jsonFilePath = p.join(p.resolve(json), JSON_FILE_NAME)
|
||||
let exists = false
|
||||
try {
|
||||
fs.statSync(jsonFilePath)
|
||||
exists = true
|
||||
} catch (err) {
|
||||
if (err.code !== 'ENOENT') throw err
|
||||
}
|
||||
if (exists)
|
||||
return new this(JSON.parse(fs.readFileSync(jsonFilePath)), {
|
||||
...opts,
|
||||
dir: json
|
||||
})
|
||||
return new this(null, { ...opts, dir: json })
|
||||
}
|
||||
return new this(json, opts)
|
||||
}
|
||||
}
|
||||
|
||||
function nextFlag(f, n) {
|
||||
if (n === 0) return f
|
||||
if (f === 0) {
|
||||
f = 1
|
||||
n--
|
||||
}
|
||||
return f * 2 ** n
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import Hyperschema from './builder.cjs'
|
||||
|
||||
class ESMHyperschema extends Hyperschema {
|
||||
static esm = true
|
||||
}
|
||||
|
||||
export default ESMHyperschema
|
||||
+770
@@ -0,0 +1,770 @@
|
||||
const gen = require('generate-object-property')
|
||||
const s = require('generate-string')
|
||||
const { BitwiseNumericTypes, getBitwiseSize } = require('./types.js')
|
||||
|
||||
module.exports = function generateSchema(hyperschema, { esm = false, filename } = {}) {
|
||||
const structs = []
|
||||
const structsByName = new Map()
|
||||
const deferred = []
|
||||
const dedup = new Map()
|
||||
const externals = new Map()
|
||||
|
||||
for (let i = 0; i < hyperschema.schema.length; i++) {
|
||||
const fqn = hyperschema.typesByPosition.get(i)
|
||||
const type = hyperschema.resolve(fqn)
|
||||
if (type.isStruct) {
|
||||
structs.push(type)
|
||||
}
|
||||
if (type.isArray) {
|
||||
structs.push(type)
|
||||
}
|
||||
if (type.isRecord) {
|
||||
structs.push(type)
|
||||
}
|
||||
if (type.isEnum) {
|
||||
structs.push(type)
|
||||
}
|
||||
if (type.isExternal) {
|
||||
structs.push(type)
|
||||
}
|
||||
if (type.isVersioned) {
|
||||
structs.push(type)
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < structs.length; i++) {
|
||||
const struct = structs[i]
|
||||
const id = 'encoding' + i
|
||||
const result = { encoder: null, id }
|
||||
structsByName.set(struct.fqn, result)
|
||||
}
|
||||
for (let i = 0; i < structs.length; i++) {
|
||||
const struct = structs[i]
|
||||
const result = structsByName.get(struct.fqn)
|
||||
|
||||
if (struct.isExternal) {
|
||||
const v = requireExternal(struct, filename)
|
||||
result.encoder = `const ${result.id} = ${gen(v, struct.external)}\n`
|
||||
continue
|
||||
}
|
||||
|
||||
result.encoder = struct.isEnum
|
||||
? generateEnum(result.id, struct)
|
||||
: struct.isVersioned
|
||||
? generateVersioned(result.id, struct)
|
||||
: generateStruct(result.id, struct, deferred, false)
|
||||
|
||||
if (struct.isInlined) {
|
||||
result.encoder += '\n' + generateStruct(result.id, struct, deferred, true)
|
||||
}
|
||||
}
|
||||
|
||||
let str = ''
|
||||
|
||||
str += '// This file is autogenerated by the hyperschema compiler\n'
|
||||
str += `// Schema Version: ${hyperschema.version}\n`
|
||||
str += '/* eslint-disable camelcase */\n'
|
||||
str += '/* eslint-disable quotes */\n'
|
||||
str += '/* eslint-disable space-before-function-paren */\n'
|
||||
str += '\n'
|
||||
|
||||
if (esm) {
|
||||
str += `import { c } from 'hyperschema/runtime'\n`
|
||||
} else {
|
||||
str += `const { c } = require('hyperschema/runtime')\n`
|
||||
}
|
||||
for (const [req, v] of externals) {
|
||||
if (esm) {
|
||||
str += `import * as ${v} from ${s(req)}\n`
|
||||
} else {
|
||||
str += `const ${v} = require(${s(req)})\n`
|
||||
}
|
||||
}
|
||||
|
||||
str += '\n'
|
||||
str += `const VERSION = ${hyperschema.version}\n`
|
||||
str += '\n'
|
||||
|
||||
str += '// eslint-disable-next-line no-unused-vars\n'
|
||||
str += 'let version = VERSION\n'
|
||||
str += '\n'
|
||||
|
||||
for (let i = 0; i < structs.length; i++) {
|
||||
const struct = structs[i]
|
||||
const { encoder } = structsByName.get(struct.fqn)
|
||||
str += encoder
|
||||
str += '\n'
|
||||
}
|
||||
|
||||
if (deferred.length) {
|
||||
str += deferred.join('')
|
||||
str += '\n'
|
||||
}
|
||||
|
||||
str += 'function setVersion(v) {\n'
|
||||
str += ' version = v\n'
|
||||
str += '}\n'
|
||||
str += '\n'
|
||||
|
||||
str += 'function encode(name, value, v = VERSION) {\n'
|
||||
str += ' version = v\n'
|
||||
str += ' return c.encode(getEncoding(name), value)\n'
|
||||
str += '}\n'
|
||||
str += '\n'
|
||||
|
||||
str += 'function decode(name, buffer, v = VERSION) {\n'
|
||||
str += ' version = v\n'
|
||||
str += ' return c.decode(getEncoding(name), buffer)\n'
|
||||
str += '}\n'
|
||||
str += '\n'
|
||||
|
||||
str += 'function getEnum(name) {\n'
|
||||
str += ' switch (name) {\n'
|
||||
for (let i = 0; i < structs.length; i++) {
|
||||
const struct = structs[i]
|
||||
if (struct.enum) {
|
||||
str += ` case ${s(struct.fqn)}:\n`
|
||||
str += ` return ${structsByName.get(struct.fqn).id}_enum\n`
|
||||
}
|
||||
}
|
||||
str += ' default:\n'
|
||||
str += " throw new Error('Enum not found ' + name)\n"
|
||||
str += ' }\n'
|
||||
str += '}\n'
|
||||
|
||||
str += '\n'
|
||||
|
||||
str += 'function getEncoding(name) {\n'
|
||||
str += ' switch (name) {\n'
|
||||
for (let i = 0; i < structs.length; i++) {
|
||||
const struct = structs[i]
|
||||
str += ` case ${s(struct.fqn)}:\n`
|
||||
str += ` return ${structsByName.get(struct.fqn).id}\n`
|
||||
}
|
||||
str += ' default:\n'
|
||||
str += " throw new Error('Encoder not found ' + name)\n"
|
||||
str += ' }\n'
|
||||
str += '}\n'
|
||||
|
||||
str += '\n'
|
||||
|
||||
str += 'function getStruct(name, v = VERSION) {\n'
|
||||
str += ' const enc = getEncoding(name)\n'
|
||||
str += ' return {\n'
|
||||
str += ' preencode(state, m) {\n'
|
||||
str += ' version = v\n'
|
||||
str += ' enc.preencode(state, m)\n'
|
||||
str += ' },\n'
|
||||
str += ' encode(state, m) {\n'
|
||||
str += ' version = v\n'
|
||||
str += ' enc.encode(state, m)\n'
|
||||
str += ' },\n'
|
||||
str += ' decode(state) {\n'
|
||||
str += ' version = v\n'
|
||||
str += ' return enc.decode(state)\n'
|
||||
str += ' }\n'
|
||||
str += ' }\n'
|
||||
str += '}\n'
|
||||
str += '\n'
|
||||
str += 'const resolveStruct = getStruct // compat\n'
|
||||
str += '\n'
|
||||
|
||||
if (esm) {
|
||||
str +=
|
||||
'export { resolveStruct, getStruct, getEnum, getEncoding, encode, decode, setVersion, version }\n'
|
||||
} else {
|
||||
str +=
|
||||
'module.exports = {\n resolveStruct,\n getStruct,\n getEnum,\n getEncoding,\n encode,\n decode,\n setVersion,\n version\n}\n'
|
||||
}
|
||||
|
||||
return str
|
||||
|
||||
function requireExternal(struct, filename) {
|
||||
if (!filename) throw new Error('Must provide filename if using external types')
|
||||
const req = struct.require(filename)
|
||||
let v = externals.get(req)
|
||||
if (v) return v
|
||||
v = 'external' + externals.size
|
||||
externals.set(req, v)
|
||||
return v
|
||||
}
|
||||
|
||||
function generateEnum(id, struct) {
|
||||
let str = ''
|
||||
|
||||
str += `const ${id}_enum = {\n`
|
||||
|
||||
for (let i = 0; i < struct.enum.length; i++) {
|
||||
const e = struct.enum[i]
|
||||
const value = struct.strings ? s(e.key) : i + struct.offset
|
||||
str += ` ${gen.property(e.key)}: ${value}${i < struct.enum.length - 1 ? ',' : ''}\n`
|
||||
}
|
||||
|
||||
str += '}\n\n'
|
||||
|
||||
const preencode = generateEncode({ preencode: true })
|
||||
const encode = generateEncode()
|
||||
const decode = generateDecode()
|
||||
|
||||
str += `// ${struct.fqn} enum\n`
|
||||
str += `const ${id} = {\n`
|
||||
str += ' preencode (state, m) {\n'
|
||||
str += `${preencode}`
|
||||
str += ' },\n'
|
||||
str += ' encode (state, m) {\n'
|
||||
str += `${encode}`
|
||||
str += ' },\n'
|
||||
str += ' decode (state) {\n'
|
||||
str += `${decode}`
|
||||
str += ' }\n'
|
||||
str += '}\n'
|
||||
|
||||
return str
|
||||
|
||||
function generateEncode({ preencode = false } = {}) {
|
||||
const max = struct.enum.length + struct.offset - 1
|
||||
const encode = preencode ? 'preencode' : 'encode'
|
||||
|
||||
if (preencode && max <= 0xfc) {
|
||||
return ` state.end++ // max enum is ${max} so always one byte\n`
|
||||
}
|
||||
|
||||
if (!struct.strings) {
|
||||
let str = ''
|
||||
str += ` if (m > ${max}) throw new Error('Unknown enum')\n`
|
||||
str += ` c.uint.${encode}(state, m)\n`
|
||||
return str
|
||||
}
|
||||
|
||||
let str = ' switch (m) {\n'
|
||||
for (let i = 0; i < struct.enum.length; i++) {
|
||||
str += ` case ${s(struct.enum[i].key)}:\n`
|
||||
str += ` c.uint.${encode}(state, ${i + struct.offset})\n`
|
||||
str += ' break\n'
|
||||
}
|
||||
str += ' default:\n'
|
||||
str += " throw new Error('Unknown enum')\n"
|
||||
str += ' }\n'
|
||||
return str
|
||||
}
|
||||
|
||||
function generateDecode() {
|
||||
if (!struct.strings) return ' return c.uint.decode(state)\n'
|
||||
|
||||
let str = ' switch (c.uint.decode(state)) {\n'
|
||||
for (let i = 0; i < struct.enum.length; i++) {
|
||||
str += ` case ${i + struct.offset}:\n`
|
||||
str += ` return ${s(struct.enum[i].key)}\n`
|
||||
}
|
||||
str += ' default: return null\n'
|
||||
str += ' }\n'
|
||||
return str
|
||||
}
|
||||
}
|
||||
|
||||
function getGeneratedTypeEncoder(type) {
|
||||
if (type.isPrimitive) {
|
||||
return getPrimitiveEncoder(type)
|
||||
}
|
||||
|
||||
if (type.isAlias) {
|
||||
return getGeneratedTypeEncoder(type.type)
|
||||
}
|
||||
|
||||
return structsByName.get(type.fqn).id
|
||||
}
|
||||
|
||||
function dedupConst(name, def, deferred = false) {
|
||||
if (dedup.has(def)) return `const ${name} = ${dedup.get(def).name}\n`
|
||||
dedup.set(def, { deferred, name })
|
||||
return `const ${name} = ${def}\n`
|
||||
}
|
||||
|
||||
function getGeneratedFieldEncoder(field) {
|
||||
const typeStr = getGeneratedTypeEncoder(field.type)
|
||||
|
||||
if (field.array) {
|
||||
if (field.framed) {
|
||||
return `c.array(c.frame(${typeStr}))`
|
||||
}
|
||||
return `c.array(${typeStr})`
|
||||
}
|
||||
|
||||
if (field.record) {
|
||||
const keyType = getGeneratedTypeEncoder(field.key)
|
||||
const valueType = getGeneratedTypeEncoder(field.value)
|
||||
return `c.record(${keyType}, ${valueType})`
|
||||
}
|
||||
|
||||
if (field.framed) {
|
||||
return `c.frame(${typeStr})`
|
||||
}
|
||||
|
||||
return typeStr
|
||||
}
|
||||
|
||||
function generateVersioned(id, struct) {
|
||||
let str = ''
|
||||
let enc = ''
|
||||
|
||||
str += `// ${struct.fqn}\n`
|
||||
str += ''
|
||||
|
||||
const preencode = generateEncode({ preencode: true })
|
||||
const encode = generateEncode()
|
||||
const decode = generateDecode()
|
||||
|
||||
enc += '{\n'
|
||||
enc += ' preencode(state, m) {\n'
|
||||
enc += `${preencode.trimRight()}\n`
|
||||
enc += ' },\n'
|
||||
enc += ' encode(state, m) {\n'
|
||||
enc += `${encode.trimRight()}\n`
|
||||
enc += ' },\n'
|
||||
enc += ' decode(state) {\n'
|
||||
enc += `${decode.trimRight()}\n`
|
||||
enc += ' }\n'
|
||||
enc += '}'
|
||||
|
||||
str += dedupConst(id, enc)
|
||||
|
||||
return str
|
||||
|
||||
function generateEncode({ preencode = false } = {}) {
|
||||
const fn = preencode ? 'preencode' : 'encode'
|
||||
let str = ''
|
||||
str += ` c.uint.${fn}(state, m.version)\n`
|
||||
str += ' switch (m.version) {\n'
|
||||
let version = 0
|
||||
for (let i = 0; i < struct.versions.length; i++) {
|
||||
const def = struct.versions[i]
|
||||
while (version <= def.version) str += ` case ${version++}:\n`
|
||||
str += ` ${getEncoder(def.type)}.${fn}(state, m)\n`
|
||||
str += ' break\n'
|
||||
}
|
||||
str += ' default:\n'
|
||||
str += " throw new Error('Unsupported version')\n"
|
||||
str += ' }'
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
function generateDecode() {
|
||||
let str = ''
|
||||
str += ' const start = state.start\n'
|
||||
str += ' const v = c.uint.decode(state)\n'
|
||||
str += ' state.start = start\n'
|
||||
str += ' switch (v) {'
|
||||
let version = 0
|
||||
for (let i = 0; i < struct.versions.length; i++) {
|
||||
const def = struct.versions[i]
|
||||
while (version <= def.version) str += `\n case ${version++}:`
|
||||
str += ' {\n'
|
||||
str += ` const decoded = ${getEncoder(def.type)}.decode(state)\n`
|
||||
if (def.map) {
|
||||
const v = requireExternal(struct, filename)
|
||||
str += ` const map = ${gen(v, def.map)}\n`
|
||||
str += ' return map(decoded)\n'
|
||||
} else {
|
||||
str += ' return decoded\n'
|
||||
}
|
||||
str += ' }'
|
||||
}
|
||||
str += '\n default:\n'
|
||||
str += " throw new Error('Unsupported version')\n"
|
||||
str += ' }'
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
function getEncoder(type) {
|
||||
if (type.isAlias) return getEncoder(type.type)
|
||||
if (type.isPrimitive) return getPrimitiveEncoder(type)
|
||||
return structsByName.get(type.fqn).id
|
||||
}
|
||||
}
|
||||
|
||||
function generateStruct(id, struct, deferred, inlinable) {
|
||||
const fieldTypes = new Map()
|
||||
|
||||
if (inlinable) id += '_inline'
|
||||
|
||||
const fields = struct.isArray ? [] : struct.resolvedFields
|
||||
const flagType = struct.compact ? getFittingType(struct.maxFlag) : 'uint'
|
||||
|
||||
let str = ''
|
||||
let enc = ''
|
||||
let keyEnc = ''
|
||||
|
||||
if (struct.isArray) {
|
||||
enc = getGeneratedTypeEncoder(struct.type)
|
||||
} else if (struct.isRecord) {
|
||||
keyEnc = getGeneratedTypeEncoder(struct.key)
|
||||
enc = getGeneratedTypeEncoder(struct.value)
|
||||
} else {
|
||||
for (let i = 0; i < struct.fields.length; i++) {
|
||||
const field = struct.fields[i]
|
||||
if (!field.framed && !field.array) continue
|
||||
|
||||
const fieldId = `${id}_${i}`
|
||||
fieldTypes.set(field.name, fieldId)
|
||||
|
||||
const fieldEnc = getGeneratedFieldEncoder(field)
|
||||
const isDeferred = !!dedup.get(fieldEnc)?.deferred
|
||||
|
||||
// if recursive, defer the field type
|
||||
if (structsByName.get(field.type.fqn)?.encoder === null || isDeferred) {
|
||||
let d = ''
|
||||
d += `// ${struct.fqn}.${field.name}, deferred due to recusive use\n`
|
||||
d += dedupConst(fieldId, fieldEnc, true)
|
||||
deferred.push(d)
|
||||
} else {
|
||||
str += `// ${struct.fqn}.${field.name}\n`
|
||||
str += dedupConst(fieldId, fieldEnc, false)
|
||||
}
|
||||
}
|
||||
|
||||
if (str) {
|
||||
str += '\n'
|
||||
}
|
||||
|
||||
const preencode = generateEncode({ preencode: true })
|
||||
const encode = generateEncode()
|
||||
const decode = generateDecode()
|
||||
|
||||
enc += '{\n'
|
||||
enc += ` preencode(state, m) {\n`
|
||||
enc += `${preencode.trimRight()}\n`
|
||||
enc += ' },\n'
|
||||
enc += ` encode(state, m) {\n`
|
||||
enc += `${encode.trimRight()}\n`
|
||||
enc += ' },\n'
|
||||
enc += ` decode(state${inlinable ? ', inlining' : ''}) {\n`
|
||||
enc += `${decode.trimRight()}\n`
|
||||
enc += ' }\n'
|
||||
enc += '}'
|
||||
}
|
||||
|
||||
str += `// ${struct.fqn}${inlinable ? ' (inline)' : ''}\n`
|
||||
|
||||
if (struct.isArray) {
|
||||
if (struct.framed) {
|
||||
str += dedupConst(id, `c.array(c.frame(${enc}))`)
|
||||
} else {
|
||||
str += dedupConst(id, `c.array(${enc})`)
|
||||
}
|
||||
} else if (struct.isRecord) {
|
||||
str += dedupConst(id, `c.record(${keyEnc}, ${enc})`)
|
||||
} else {
|
||||
str += dedupConst(id, enc)
|
||||
}
|
||||
|
||||
return str
|
||||
|
||||
function generateFlags(indent, flag, parent = 'm') {
|
||||
const first = !flag
|
||||
const entries = (flag ? flag.fields : fields).filter((f) => !f.field.external)
|
||||
const mutable = first && entries.some((f) => f.fields.length > 0)
|
||||
const mut = first ? (mutable ? 'let' : 'const') : ''
|
||||
|
||||
const flags = entries
|
||||
.filter((f) => !f.field.required)
|
||||
.map((f) => generateBitFlag(f.field, parent, f.flag))
|
||||
|
||||
let str = combineFlags(indent, mut, first ? '=' : '|=', flags)
|
||||
|
||||
for (const f of entries) {
|
||||
if (!f.fields.length) continue
|
||||
const name = gen(parent, f.field.name)
|
||||
|
||||
str += `${indent}if (${name}) {\n`
|
||||
str += generateFlags(`${indent} `, f, name)
|
||||
str += `${indent}}\n`
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
function generateEncode({ preencode = false } = {}) {
|
||||
const fn = preencode ? 'preencode' : 'encode'
|
||||
let str = ''
|
||||
let fastFlags = false
|
||||
const flags = generateFlags(' ')
|
||||
const smallUint = struct.maxFlag < 128
|
||||
|
||||
if (flags !== ' const flags = 0\n') {
|
||||
if ((smallUint || struct.compact) && preencode) {
|
||||
fastFlags = !struct.compact
|
||||
} else if (!inlinable) {
|
||||
str += flags
|
||||
str += '\n'
|
||||
}
|
||||
}
|
||||
|
||||
let bitfield = false
|
||||
let optional = false
|
||||
|
||||
for (let i = 0; i < struct.fields.length; i++) {
|
||||
const field = struct.fields[i]
|
||||
if (field.external) continue
|
||||
|
||||
if (i === struct.flagsPosition && !inlinable) {
|
||||
if (fastFlags) {
|
||||
str += ` state.end++ // max flag is ${struct.maxFlag} so always one byte`
|
||||
} else if (struct.compact && preencode) {
|
||||
if (getByteSize(struct.maxFlag) === 1) {
|
||||
str += ' state.end++ // flags are fixed size'
|
||||
} else {
|
||||
str += ` state.end += ${getByteSize(struct.maxFlag)} // flags are fixed size`
|
||||
}
|
||||
} else {
|
||||
str += ` c.${flagType}.${fn}(state, flags)`
|
||||
}
|
||||
bitfield = true
|
||||
}
|
||||
if (field.type.bool || field.type.bitwise) continue
|
||||
|
||||
if (bitfield === true) {
|
||||
str += '\n'
|
||||
bitfield = false
|
||||
}
|
||||
if (!field.required && !optional) {
|
||||
str += '\n'
|
||||
optional = true
|
||||
}
|
||||
|
||||
let prefix = ' '
|
||||
if (!field.required) {
|
||||
prefix += ` ${vPrefix(field.version, gen('m', field.name))}`
|
||||
}
|
||||
|
||||
const suffix = field.inline ? '_inline' : ''
|
||||
const encoder = `${getFieldEncoder(field)}${suffix}.${fn}(state, ${gen('m', field.name)})`
|
||||
|
||||
str += `${prefix} ${encoder}`
|
||||
|
||||
if (i !== struct.fields.length - 1) {
|
||||
let onlyBools = true
|
||||
for (let j = i + 1; j < struct.fields.length; j++) {
|
||||
const field = struct.fields[j]
|
||||
if (!field) break
|
||||
if (!field.type.bool && !field.type.bitwise) {
|
||||
onlyBools = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!onlyBools || !bitfield) str += '\n'
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
if (str[i] !== '\n') {
|
||||
str = str.slice(i)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
function generateDecode() {
|
||||
let str = ''
|
||||
let seen = 0
|
||||
|
||||
if (struct.expectsVersion) {
|
||||
str += ' const v = c.uint.decode(state)\n'
|
||||
}
|
||||
|
||||
const pre = new Map()
|
||||
for (let i = 0; i < fields.length; i++) {
|
||||
const field = fields[i].field
|
||||
if (!field.required || field.type.bool || i === struct.flagsPosition || field.inline) {
|
||||
break
|
||||
}
|
||||
seen++
|
||||
if (field.external) continue
|
||||
pre.set(field, 'r' + i)
|
||||
str += ` const r${i} = ${getFieldEncoder(field)}.decode(state)\n`
|
||||
}
|
||||
|
||||
if (seen === struct.flagsPosition) {
|
||||
if (inlinable) {
|
||||
str += ' const flags = inlining\n\n'
|
||||
} else if (alwaysOptional(struct)) {
|
||||
str += ` const flags = c.${flagType}.decode(state)\n\n`
|
||||
} else {
|
||||
str += ` const flags = state.start < state.end ? c.${flagType}.decode(state) : 0\n\n`
|
||||
}
|
||||
} else if (pre.size) {
|
||||
str += '\n'
|
||||
}
|
||||
|
||||
str += ' return {\n'
|
||||
if (struct.expectsVersion) str += ' version: v,\n'
|
||||
|
||||
for (let i = 0; i < fields.length; i++) {
|
||||
const f = fields[i]
|
||||
const field = f.field
|
||||
const end = i < fields.length - 1 ? ',\n' : '\n'
|
||||
const flag = vInlinePrefix(field.version, `(flags & ${field.flag}) !== 0`)
|
||||
const prop = gen.property(field.name)
|
||||
|
||||
let shift = ''
|
||||
if (field.inline) {
|
||||
shift = `, ${shiftRight('flags', f.shift, false, field.struct)}`
|
||||
}
|
||||
|
||||
const suffix = field.inline ? '_inline' : ''
|
||||
|
||||
const decoder = `${getFieldEncoder(field)}${suffix}.decode(state${shift})`
|
||||
|
||||
if (field.external) {
|
||||
str += ` ${prop}: ${field.getDefaultValue()}${end}`
|
||||
continue
|
||||
}
|
||||
|
||||
if (pre.has(field)) {
|
||||
str += ` ${prop}: ${pre.get(field)}${end}`
|
||||
continue
|
||||
}
|
||||
|
||||
if (field.type.bool) {
|
||||
str += ` ${prop}: ${flag}${end}`
|
||||
continue
|
||||
}
|
||||
|
||||
if (field.type.bitwise) {
|
||||
const mask = 2 ** field.type.bitwise - 1
|
||||
const prefix = vInlinePrefix(field.version)
|
||||
const shift = Math.log2(field.flag)
|
||||
const value = `${shiftRight('flags', shift, true, field.struct)} & ${mask}`
|
||||
const v = prefix ? `${prefix} ? ${value} : 0` : `${value}`
|
||||
str += ` ${prop}: ${v}${end}`
|
||||
continue
|
||||
}
|
||||
|
||||
if (field.required) {
|
||||
str += ` ${prop}: ${decoder}${end}`
|
||||
continue
|
||||
}
|
||||
|
||||
str += ` ${prop}: ${flag} ? ${decoder} : ${field.getDefaultValue()}${end}`
|
||||
}
|
||||
|
||||
str += ' }'
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
function vPrefix(v, condition) {
|
||||
if (v === 1) return condition ? `if (${condition})` : ''
|
||||
return condition ? `if (version >= ${v} && ${condition})` : `if (version >= ${v}) `
|
||||
}
|
||||
|
||||
function vInlinePrefix(v, condition) {
|
||||
if (v === 1) return condition || ''
|
||||
return condition ? `(version >= ${v} && ${condition})` : `version >= ${v}`
|
||||
}
|
||||
|
||||
function generateBitFlag(field, parent = 'm', flag = field.flag) {
|
||||
const id = gen(parent, field.name)
|
||||
if (field.type.bitwise) {
|
||||
const mask = 2 ** field.type.bitwise - 1
|
||||
const prefix = vInlinePrefix(field.version)
|
||||
const shift = Math.log2(flag)
|
||||
const value = shiftLeft(`(${id} & ${mask})`, shift, !prefix, field.struct)
|
||||
return prefix ? `(${prefix} ? ${value} : 0)` : `${value}`
|
||||
}
|
||||
return `(${vInlinePrefix(field.version, id)} ? ${flag} : 0)`
|
||||
}
|
||||
|
||||
function combineFlags(indent, mut, op, flags) {
|
||||
if (mut) mut += ' '
|
||||
|
||||
let s = `${indent}${mut}flags ${op}`
|
||||
|
||||
if (flags.length === 0) flags.push('(0)')
|
||||
|
||||
if (flags.length === 1) {
|
||||
flags = flags.slice(0)
|
||||
flags[0] = flags[0].slice(1, -1)
|
||||
}
|
||||
|
||||
const inline = flags.join(' | ')
|
||||
if (s.length + inline.length + 1 <= 100) return s + ' ' + inline + '\n'
|
||||
if (inline.length + indent.length + 2 <= 100) return s + '\n ' + indent + inline + '\n'
|
||||
|
||||
s += '\n'
|
||||
for (let i = 0; i < flags.length; i++) {
|
||||
s += indent + ' ' + flags[i] + (i < flags.length - 1 ? ' |\n' : '\n')
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
function getFieldEncoder(field) {
|
||||
if (fieldTypes.has(field.name)) return fieldTypes.get(field.name)
|
||||
return getEncoder(field.type)
|
||||
}
|
||||
|
||||
function getEncoder(type) {
|
||||
if (type.isAlias) return getEncoder(type.type)
|
||||
if (type.isPrimitive) return getPrimitiveEncoder(type)
|
||||
return structsByName.get(type.fqn).id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function alwaysOptional(struct) {
|
||||
for (let i = 0; i < struct.fields.length; i++) {
|
||||
const f = struct.fields[i]
|
||||
if (f.required) continue
|
||||
|
||||
for (let j = 0; j < i; j++) {
|
||||
const p = struct.fields[j]
|
||||
if (p.version !== f.version) return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
function getPrimitiveEncoder(type) {
|
||||
return `c.${type.name}`
|
||||
}
|
||||
|
||||
function shiftRight(v, n, paren, struct) {
|
||||
if (n === 0) return v
|
||||
const start = paren ? '(' : ''
|
||||
const end = paren ? ')' : ''
|
||||
if (struct.maxFlag <= 2 ** 31) {
|
||||
return `${start}${v} >>> ${n}${end}`
|
||||
}
|
||||
return `Math.floor(${v} / ${2 ** n})`
|
||||
}
|
||||
|
||||
function shiftLeft(v, n, paren, struct) {
|
||||
if (n === 0) return v
|
||||
const start = paren ? '(' : ''
|
||||
const end = paren ? ')' : ''
|
||||
if (struct.maxFlag <= 2 ** 31) {
|
||||
return `${start}${v} << ${n}${end}`
|
||||
}
|
||||
return `${start}${v} * ${2 ** n}${end}`
|
||||
}
|
||||
|
||||
function getByteSize(bit) {
|
||||
if (bit < 2 ** 8) return 1
|
||||
if (bit < 2 ** 16) return 2
|
||||
if (bit < 2 ** 24) return 3
|
||||
if (bit < 2 ** 32) return 4
|
||||
if (bit < 2 ** 40) return 5
|
||||
if (bit < 2 ** 48) return 6
|
||||
if (bit < 2 ** 56) return 7
|
||||
return 8
|
||||
}
|
||||
function getFittingType(bit) {
|
||||
return 'uint' + getByteSize(bit) * 8
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
const BitwiseNumericTypes = new Set(['uint1', 'uint2', 'uint3', 'uint4', 'uint5', 'uint6', 'uint7'])
|
||||
const NumericTypes = new Set([
|
||||
'uint',
|
||||
...BitwiseNumericTypes,
|
||||
'uint8',
|
||||
'uint16',
|
||||
'uint24',
|
||||
'uint32',
|
||||
'uint40',
|
||||
'uint48',
|
||||
'uint56',
|
||||
'uint64',
|
||||
'int',
|
||||
'int8',
|
||||
'int16',
|
||||
'int24',
|
||||
'int32',
|
||||
'int40',
|
||||
'int48',
|
||||
'int56',
|
||||
'int64',
|
||||
'float32',
|
||||
'float64',
|
||||
'port',
|
||||
'lexint'
|
||||
])
|
||||
const StringTypes = new Set(['string', 'utf8', 'ascii', 'hex'])
|
||||
const BigIntTypes = new Set(['bigint', 'biguint64', 'bigint64'])
|
||||
const ObjectTypes = new Set(['fixed32', 'fixed64', 'buffer', 'date'])
|
||||
const BooleanTypes = new Set(['bool'])
|
||||
const NetworkTypes = new Set(['ip', 'ipv4', 'ipv6', 'ipAddress', 'ipv4Address', 'ipv6Address'])
|
||||
const SupportedTypes = new Set([
|
||||
...NumericTypes,
|
||||
...StringTypes,
|
||||
...BigIntTypes,
|
||||
...ObjectTypes,
|
||||
...BooleanTypes,
|
||||
...NetworkTypes,
|
||||
'none',
|
||||
'raw',
|
||||
'json'
|
||||
])
|
||||
|
||||
function getBitwiseSize(type) {
|
||||
if (type === 'uint1') return 1
|
||||
if (type === 'uint2') return 2
|
||||
if (type === 'uint3') return 3
|
||||
if (type === 'uint4') return 4
|
||||
if (type === 'uint5') return 5
|
||||
if (type === 'uint6') return 6
|
||||
if (type === 'uint7') return 7
|
||||
return 0
|
||||
}
|
||||
|
||||
function getDefaultValue(type) {
|
||||
if (NumericTypes.has(type)) return 0
|
||||
if (BigIntTypes.has(type)) return 0n
|
||||
if (BooleanTypes.has(type)) return false
|
||||
return null
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
SupportedTypes,
|
||||
BitwiseNumericTypes,
|
||||
getDefaultValue,
|
||||
getBitwiseSize
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
{
|
||||
"name": "hyperschema",
|
||||
"version": "1.20.1",
|
||||
"description": "Create registries of declarative compact-encoding schemas",
|
||||
"files": [
|
||||
"lib/*.js",
|
||||
"builder.mjs",
|
||||
"builder.cjs",
|
||||
"runtime.mjs",
|
||||
"runtime.cjs",
|
||||
"primitives.mjs",
|
||||
"primitives.cjs"
|
||||
],
|
||||
"exports": {
|
||||
"./package": "./package.json",
|
||||
".": {
|
||||
"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": "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": "^2.19.0",
|
||||
"generate-object-property": "^2.0.0",
|
||||
"generate-string": "^1.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"brittle": "^3.7.0",
|
||||
"prettier": "^3.6.2",
|
||||
"prettier-config-holepunch": "^2.0.0",
|
||||
"test-tmp": "^1.3.0"
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
module.exports = new Set(require('./lib/types').SupportedTypes)
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
const prims = new Set(require('./lib/types').SupportedTypes)
|
||||
|
||||
export default prims
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
c: require('compact-encoding')
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import c from 'compact-encoding'
|
||||
|
||||
export { c }
|
||||
Reference in New Issue
Block a user