fix: multi-repo local dev (file:../ deps, package exports, missing deps)

This commit is contained in:
Raven Scott
2026-05-21 20:14:54 -04:00
parent 76e3272ced
commit b63275c6cc
802 changed files with 94636 additions and 4 deletions
+201
View File
@@ -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.
+39
View File
@@ -0,0 +1,39 @@
# compact-encoding-bitfield
[compact-encoding](https://github.com/compact-encoding/compact-encoding) codec for bitfields. Uses a variable length encoding that is ABI compatible with `cenc.uint`.
```sh
npm install compact-encoding-bitfield
```
## Usage
```js
const cenc = require('compact-encoding')
const bitfield = require('compact-encoding-bitfield')
const buffer = cenc.encode(bitfield(8), 0b1110_1011)
// <Buffer fd eb 00>
cenc.decode(bitfield(8), buffer)
// <Buffer eb>
```
Bitfields are represented as buffers. For each byte, the least significant bit denotes the first bit and the most significant bit denotes the last bit. For example, bit `0` will be the least significant bit of the first byte and bit `15` will be the most significant bit of the second byte.
The encoding will convert numbers to buffers. Buffers may also be passed directly and will be returned on decode.
## ABI
Depending on `length`, the bitfield is stored using a variable number of bytes:
1. `length < 8`: 1 byte with no prefix.
2. `length <= 16`: 2 bytes with a `0xfd` prefix.
3. `length <= 32`: 4 bytes with a `0xfe` prefix.
4. `length <= 64`: 8 bytes with a `0xff` prefix.
`length` must not exceed 64 bits.
## License
Apache 2.0
+69
View File
@@ -0,0 +1,69 @@
const c = require('compact-encoding')
module.exports = function bitfield(length) {
if (length > 64) {
throw new RangeError('Bitfield cannot be larger than 64 bits')
}
let byteLength
if (length < 8) byteLength = 1
else if (length <= 16) byteLength = 2
else if (length <= 32) byteLength = 4
else byteLength = 8
return {
preencode(state) {
state.end++ // Length byte, used for data when byteLength === 1
if (byteLength === 1);
else if (byteLength === 2) c.uint16.preencode(state)
else if (byteLength === 4) c.uint32.preencode(state)
else c.uint64.preencode(state)
},
encode(state, b) {
if (byteLength === 1);
else if (byteLength === 2) c.uint8.encode(state, 0xfd)
else if (byteLength === 4) c.uint8.encode(state, 0xfe)
else c.uint8.encode(state, 0xff)
if (typeof b === 'number') {
if (byteLength === 1) c.uint8.encode(state, b)
else if (byteLength === 2) c.uint16.encode(state, b)
else if (byteLength === 4) c.uint32.encode(state, b)
else c.uint64.encode(state, b)
} else {
state.buffer.set(b, state.start)
if (b.byteLength < byteLength) {
// Zero-fill the rest of the byte length.
state.buffer.fill(
0,
state.start + b.byteLength,
state.start + byteLength
)
}
state.start += byteLength
}
},
decode(state) {
const byte = state.buffer[state.start]
let byteLength
if (byte <= 0xfc) byteLength = 1
else if (byte === 0xfd) byteLength = 2
else if (byte === 0xfe) byteLength = 4
else byteLength = 8
if (byteLength > 1) state.start++ // Skip the length byte
if (state.end - state.start < byteLength) throw new Error('Out of bounds')
const b = state.buffer.subarray(state.start, (state.start += byteLength))
return length <= 8 ? b.subarray(0, 1) : b
}
}
}
@@ -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.
@@ -0,0 +1,165 @@
# compact-encoding
A series of compact encoding schemes for building small and fast parsers and serializers
```
npm install compact-encoding
```
## Usage
```js
const cenc = require('compact-encoding')
const state = cenc.state()
// use preencode to figure out how big a buffer is needed
cenc.uint.preencode(state, 42)
cenc.string.preencode(state, 'hi')
console.log(state) // { start: 0, end: 4, buffer: null }
state.buffer = Buffer.allocUnsafe(state.end)
// then use encode to actually encode it to the buffer
cenc.uint.encode(state, 42)
cenc.string.encode(state, 'hi')
// to decode it simply use decode instead
state.start = 0
cenc.uint.decode(state) // 42
cenc.string.decode(state) // 'hi'
```
## Encoder API
#### `state`
Should be an object that looks like this `{ start, end, buffer }`.
You can also get a blank state object using `cenc.state()`.
- `start` is the byte offset to start encoding/decoding at.
- `end` is the byte offset indicating the end of the buffer.
- `buffer` should be either a Node.js Buffer or Uint8Array.
#### `enc.preencode(state, val)`
Does a fast preencode dry-run that only sets state.end.
Use this to figure out how big of a buffer you need.
#### `enc.encode(state, val)`
Encodes `val` into `state.buffer` at position `state.start`.
Updates `state.start` to point after the encoded value when done.
#### `val = enc.decode(state)`
Decodes a value from `state.buffer` as position `state.start`.
Updates `state.start` to point after the decoded value when done in the buffer.
## Helpers
If you are just encoding to a buffer or decoding from one you can use the `encode` and `decode` helpers
to reduce your boilerplate
```js
const buf = cenc.encode(cenc.bool, true)
const bool = cenc.decode(cenc.bool, buf)
```
## Bundled encodings
The following encodings are bundled as they are primitives that can be used
to build others on top. Feel free to PR more that are missing.
- `cenc.raw` - Pass through encodes a buffer, i.e. a basic copy.
- `cenc.uint` - Encodes a uint using the smallest fixed size encoding with a prefix to signal which one. Useful for uints that can be a wide range of values.
- `cenc.uint8` - Encodes a fixed size uint8.
- `cenc.uint16` - Encodes a fixed size uint16. Useful for things like ports.
- `cenc.uint24` - Encodes a fixed size uint24. Useful for message framing.
- `cenc.uint32` - Encodes a fixed size uint32. Useful for very large message framing.
- `cenc.uint40` - Encodes a fixed size uint40.
- `cenc.uint48` - Encodes a fixed size uint48.
- `cenc.uint56` - Encodes a fixed size uint56.
- `cenc.uint64` - Encodes a fixed size uint64.
- `cenc.int` - Encodes an int using `cenc.uint` with ZigZag encoding.
- `cenc.int8` - Encodes a fixed size int8 using `cenc.uint8` with ZigZag encoding.
- `cenc.int16` - Encodes a fixed size int16 using `cenc.uint16` with ZigZag encoding.
- `cenc.int24` - Encodes a fixed size int24 using `cenc.uint24` with ZigZag encoding.
- `cenc.int32` - Encodes a fixed size int32 using `cenc.uint32` with ZigZag encoding.
- `cenc.int40` - Encodes a fixed size int40 using `cenc.uint40` with ZigZag encoding.
- `cenc.int48` - Encodes a fixed size int48 using `cenc.uint48` with ZigZag encoding.
- `cenc.int56` - Encodes a fixed size int56 using `cenc.uint56` with ZigZag encoding.
- `cenc.int64` - Encodes a fixed size int64 using `cenc.uint64` with ZigZag encoding.
- `cenc.biguint64` - Encodes a fixed size biguint64.
- `cenc.bigint64` - Encodes a fixed size bigint64 using `cenc.biguint64` with ZigZag encoding.
- `cenc.biguint` - Encodes a biguint with its word count uint prefixed.
- `cenc.bigint` - Encodes a bigint using `cenc.biguint` with ZigZag encoding.
- `cenc.float32` - Encodes a fixed size float32.
- `cenc.float64` - Encodes a fixed size float64.
- `cenc.buffer` - Encodes a buffer with its length uint prefixed. When decoding an empty buffer, `null` is returned.
- `cenc.raw.buffer` - Encodes a buffer without a length prefixed.
- `cenc.arraybuffer` - Encodes an arraybuffer with its length uint prefixed.
- `cenc.raw.arraybuffer` - Encodes an arraybuffer without a length prefixed.
- `cenc.uint8array` - Encodes a uint8array with its element length uint prefixed.
- `cenc.raw.uint8array` - Encodes a uint8array without a length prefixed.
- `cenc.uint16array` - Encodes a uint16array with its element length uint prefixed.
- `cenc.raw.uint16array` - Encodes a uint16array without a length prefixed.
- `cenc.uint32array` - Encodes a uint32array with its element length uint prefixed.
- `cenc.raw.uint32array` - Encodes a uint32array without a length prefixed.
- `cenc.int8array` - Encodes a int8array with its element length uint prefixed.
- `cenc.raw.int8array` - Encodes a int8array without a length prefixed.
- `cenc.int16array` - Encodes a int16array with its element length uint prefixed.
- `cenc.raw.int16array` - Encodes a int16array without a length prefixed.
- `cenc.int32array` - Encodes a int32array with its element length uint prefixed.
- `cenc.raw.int32array` - Encodes a int32array without a length prefixed.
- `cenc.biguint64array` - Encodes a biguint64array with its element length uint prefixed.
- `cenc.raw.biguint64array` - Encodes a biguint64array without a length prefixed.
- `cenc.bigint64array` - Encodes a bigint64array with its element length uint prefixed.
- `cenc.raw.bigint64array` - Encodes a bigint64array without a length prefixed.
- `cenc.float32array` - Encodes a float32array with its element length uint prefixed.
- `cenc.raw.float32array` - Encodes a float32array without a length prefixed.
- `cenc.float64array` - Encodes a float64array with its element length uint prefixed.
- `cenc.raw.float64array` - Encodes a float64array without a length prefixed.
- `cenc.bool` - Encodes a boolean as 1 or 0.
- `cenc.string`, `cenc.utf8` - Encodes a utf-8 string, similar to buffer.
- `cenc.raw.string`, `cenc.raw.utf8` - Encodes a utf-8 string without a length prefixed.
- `cenc.string.fixed(n)`, `cenc.utf8.fixed(n)` - Encodes a fixed sized utf-8 string.
- `cenc.ascii` - Encodes an ascii string.
- `cenc.raw.ascii` - Encodes an ascii string without a length prefixed.
- `cenc.ascii.fixed(n)` - Encodes a fixed size ascii string.
- `cenc.hex` - Encodes a hex string.
- `cenc.raw.hex` - Encodes a hex string without a length prefixed.
- `cenc.hex.fixed(n)` - Encodes a fixed size hex string.
- `cenc.base64` - Encodes a base64 string.
- `cenc.raw.base64` - Encodes a base64 string without a length prefixed.
- `cenc.base64.fixed(n)` - Encodes a fixed size base64 string.
- `cenc.utf16le`, `cenc.ucs2` - Encodes a utf16le string.
- `cenc.raw.utf16le`, `cenc.raw.ucs2` - Encodes a utf16le string without a length prefixed.
- `cenc.utf16le.fixed(n)`, `cenc.ucs2.fixed(n)` - Encodes a fixed size utf16le string.
- `cenc.fixed32` - Encodes a fixed 32 byte buffer.
- `cenc.fixed64` - Encodes a fixed 64 byte buffer.
- `cenc.fixed(n)` - Makes a fixed sized encoder.
- `cenc.date(d)` - Encodes a date object.
- `cenc.array(enc)` - Makes an array encoder from another encoder. Arrays are uint prefixed with their length.
- `cenc.raw.array(enc)` - Makes an array encoder from another encoder, without a length prefixed.
- `cenc.json` - Encodes a JSON value as utf-8.
- `cenc.raw.json` - Encodes a JSON value as utf-8 without a length prefixed.
- `cenc.ndjson` - Encodes a JSON value as newline delimited utf-8.
- `cenc.raw.ndjson` - Encodes a JSON value as newline delimited utf-8 without a length prefixed.
- `cenc.any` - Encodes any JSON representable value into a self described buffer. Like JSON + buffer, but using compact types. Useful for schemaless codecs.
- `cenc.port` - Encodes a port number for network addresses.
- `cenc.ipv4` - Encodes an IPv4 network address.
- `cenc.ipv4Address` Encodes an IPv4 network address and a port number.
- `cenc.ipv6` - Encodes an IPv6 network address.
- `cenc.ipv6Address` Encodes an IPv6 network address and a port number.
- `cenc.ip` - Encodes a dual IPv4/6 network address.
- `cenc.ipAddress` Encodes a dual IPv4/6 network address and a port number.
- `cenc.from(enc)` - Makes a compact encoder from a [codec](https://github.com/mafintosh/codecs) or [abstract-encoding](https://github.com/mafintosh/abstract-encoding).
- `cenc.none` - Helper for when you want to just express nothing
## License
Apache 2.0
@@ -0,0 +1,4 @@
const LE = (exports.LE =
new Uint8Array(new Uint16Array([0xff]).buffer)[0] === 0xff)
exports.BE = !LE
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,117 @@
module.exports = {
preencode,
encode,
decode
}
function preencode(state, num) {
if (num < 251) {
state.end++
} else if (num < 256) {
state.end += 2
} else if (num < 0x10000) {
state.end += 3
} else if (num < 0x1000000) {
state.end += 4
} else if (num < 0x100000000) {
state.end += 5
} else {
state.end++
const exp = Math.floor(Math.log(num) / Math.log(2)) - 32
preencode(state, exp)
state.end += 6
}
}
function encode(state, num) {
const max = 251
const x = num - max
if (num < max) {
state.buffer[state.start++] = num
} else if (num < 256) {
state.buffer[state.start++] = max
state.buffer[state.start++] = x
} else if (num < 0x10000) {
state.buffer[state.start++] = max + 1
state.buffer[state.start++] = (x >> 8) & 0xff
state.buffer[state.start++] = x & 0xff
} else if (num < 0x1000000) {
state.buffer[state.start++] = max + 2
state.buffer[state.start++] = x >> 16
state.buffer[state.start++] = (x >> 8) & 0xff
state.buffer[state.start++] = x & 0xff
} else if (num < 0x100000000) {
state.buffer[state.start++] = max + 3
state.buffer[state.start++] = x >> 24
state.buffer[state.start++] = (x >> 16) & 0xff
state.buffer[state.start++] = (x >> 8) & 0xff
state.buffer[state.start++] = x & 0xff
} else {
// need to use Math here as bitwise ops are 32 bit
const exp = Math.floor(Math.log(x) / Math.log(2)) - 32
state.buffer[state.start++] = 0xff
encode(state, exp)
const rem = x / Math.pow(2, exp - 11)
for (let i = 5; i >= 0; i--) {
state.buffer[state.start++] = (rem / Math.pow(2, 8 * i)) & 0xff
}
}
}
function decode(state) {
const max = 251
if (state.end - state.start < 1) throw new Error('Out of bounds')
const flag = state.buffer[state.start++]
if (flag < max) return flag
if (state.end - state.start < flag - max + 1) {
throw new Error('Out of bounds.')
}
if (flag < 252) {
return state.buffer[state.start++] + max
}
if (flag < 253) {
return (
(state.buffer[state.start++] << 8) + state.buffer[state.start++] + max
)
}
if (flag < 254) {
return (
(state.buffer[state.start++] << 16) +
(state.buffer[state.start++] << 8) +
state.buffer[state.start++] +
max
)
}
// << 24 result may be interpreted as negative
if (flag < 255) {
return (
state.buffer[state.start++] * 0x1000000 +
(state.buffer[state.start++] << 16) +
(state.buffer[state.start++] << 8) +
state.buffer[state.start++] +
max
)
}
const exp = decode(state)
if (state.end - state.start < 6) throw new Error('Out of bounds')
let rem = 0
for (let i = 5; i >= 0; i--) {
rem += state.buffer[state.start++] * Math.pow(2, 8 * i)
}
return rem * Math.pow(2, exp - 11) + max
}
@@ -0,0 +1,34 @@
{
"name": "compact-encoding",
"version": "3.1.0",
"description": "A series of compact encoding schemes for building small and fast parsers and serializers",
"main": "index.js",
"files": [
"endian.js",
"index.js",
"lexint.js",
"raw.js"
],
"dependencies": {
"b4a": "^1.3.0"
},
"devDependencies": {
"brittle": "^3.0.0",
"prettier": "^3.6.2",
"prettier-config-holepunch": "^1.0.0"
},
"scripts": {
"format": "prettier . --write",
"test": "prettier . --check && brittle test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/holepunchto/compact-encoding.git"
},
"author": "Mathias Buus (@mafintosh)",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/holepunchto/compact-encoding/issues"
},
"homepage": "https://github.com/holepunchto/compact-encoding"
}
@@ -0,0 +1,170 @@
const b4a = require('b4a')
const { BE } = require('./endian')
exports = module.exports = {
preencode(state, b) {
state.end += b.byteLength
},
encode(state, b) {
state.buffer.set(b, state.start)
state.start += b.byteLength
},
decode(state) {
const b = state.buffer.subarray(state.start, state.end)
state.start = state.end
return b
}
}
const buffer = (exports.buffer = {
preencode(state, b) {
uint8array.preencode(state, b)
},
encode(state, b) {
uint8array.encode(state, b)
},
decode(state) {
const b = state.buffer.subarray(state.start)
state.start = state.end
return b
}
})
exports.binary = {
...buffer,
preencode(state, b) {
if (typeof b === 'string') utf8.preencode(state, b)
else buffer.preencode(state, b)
},
encode(state, b) {
if (typeof b === 'string') utf8.encode(state, b)
else buffer.encode(state, b)
}
}
exports.arraybuffer = {
preencode(state, b) {
state.end += b.byteLength
},
encode(state, b) {
const view = new Uint8Array(b)
state.buffer.set(view, state.start)
state.start += b.byteLength
},
decode(state) {
const b = new ArrayBuffer(state.end - state.start)
const view = new Uint8Array(b)
view.set(state.buffer.subarray(state.start))
state.start = state.end
return b
}
}
function typedarray(TypedArray, swap) {
const n = TypedArray.BYTES_PER_ELEMENT
return {
preencode(state, b) {
state.end += b.byteLength
},
encode(state, b) {
const view = new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
if (BE && swap) swap(view)
state.buffer.set(view, state.start)
state.start += b.byteLength
},
decode(state) {
let b = state.buffer.subarray(state.start)
if (b.byteOffset % n !== 0) b = new Uint8Array(b)
if (BE && swap) swap(b)
state.start = state.end
return new TypedArray(b.buffer, b.byteOffset, b.byteLength / n)
}
}
}
const uint8array = (exports.uint8array = typedarray(Uint8Array))
exports.uint16array = typedarray(Uint16Array, b4a.swap16)
exports.uint32array = typedarray(Uint32Array, b4a.swap32)
exports.int8array = typedarray(Int8Array)
exports.int16array = typedarray(Int16Array, b4a.swap16)
exports.int32array = typedarray(Int32Array, b4a.swap32)
exports.biguint64array = typedarray(BigUint64Array, b4a.swap64)
exports.bigint64array = typedarray(BigInt64Array, b4a.swap64)
exports.float32array = typedarray(Float32Array, b4a.swap32)
exports.float64array = typedarray(Float64Array, b4a.swap64)
function string(encoding) {
return {
preencode(state, s) {
state.end += b4a.byteLength(s, encoding)
},
encode(state, s) {
state.start += b4a.write(state.buffer, s, state.start, encoding)
},
decode(state) {
const s = b4a.toString(state.buffer, encoding, state.start)
state.start = state.end
return s
}
}
}
const utf8 = (exports.string = exports.utf8 = string('utf-8'))
exports.ascii = string('ascii')
exports.hex = string('hex')
exports.base64 = string('base64')
exports.ucs2 = exports.utf16le = string('utf16le')
exports.array = function array(enc) {
return {
preencode(state, list) {
for (const value of list) enc.preencode(state, value)
},
encode(state, list) {
for (const value of list) enc.encode(state, value)
},
decode(state) {
const arr = []
while (state.start < state.end) arr.push(enc.decode(state))
return arr
}
}
}
exports.json = {
preencode(state, v) {
utf8.preencode(state, JSON.stringify(v))
},
encode(state, v) {
utf8.encode(state, JSON.stringify(v))
},
decode(state) {
return JSON.parse(utf8.decode(state))
}
}
exports.ndjson = {
preencode(state, v) {
utf8.preencode(state, JSON.stringify(v) + '\n')
},
encode(state, v) {
utf8.encode(state, JSON.stringify(v) + '\n')
},
decode(state) {
return JSON.parse(utf8.decode(state))
}
}
+42
View File
@@ -0,0 +1,42 @@
{
"name": "compact-encoding-bitfield",
"version": "1.1.0",
"description": "Compact codec for bitfields",
"main": "index.js",
"files": [
"index.js"
],
"scripts": {
"format": "prettier --write .",
"lint": "prettier --check . && lunte",
"test": "npm run test:node && npm run test:bare",
"test:node": "brittle-node --coverage test.mjs",
"test:bare": "brittle-bare --coverage test.mjs"
},
"repository": {
"type": "git",
"url": "git+https://github.com/compact-encoding/compact-encoding-bitfield.git"
},
"author": "Holepunch",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/compact-encoding/compact-encoding-bitfield/issues"
},
"homepage": "https://github.com/compact-encoding/compact-encoding-bitfield#readme",
"imports": {
"util": {
"bare": "bare-utils",
"default": "util"
}
},
"dependencies": {
"compact-encoding": "^3.0.0"
},
"devDependencies": {
"bare-utils": "^1.5.1",
"brittle": "^3.1.0",
"lunte": "^1.3.0",
"prettier": "^3.6.2",
"prettier-config-holepunch": "^1.0.0"
}
}