fix: multi-repo local dev (file:../ deps, package exports, missing deps)
This commit is contained in:
@@ -0,0 +1 @@
|
||||
* text=auto eol=lf
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
name: Integrate
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
permissions:
|
||||
contents: read
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
name: Lint
|
||||
steps:
|
||||
- uses: holepunchto/actions/node-base@v1
|
||||
- run: npm run lint
|
||||
test:
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- os: ubuntu-latest
|
||||
platform: linux
|
||||
- os: macos-latest
|
||||
platform: darwin
|
||||
- os: windows-latest
|
||||
platform: win32
|
||||
runs-on: ${{ matrix.os }}
|
||||
name: Test / ${{ matrix.platform }}
|
||||
steps:
|
||||
- uses: holepunchto/actions/bare-base@v1
|
||||
- run: npm test
|
||||
+1
@@ -0,0 +1 @@
|
||||
"prettier-config-holepunch"
|
||||
+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.
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
Copyright 2023 Holepunch Inc
|
||||
|
||||
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.
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
# compact-encoding-net
|
||||
|
||||
[compact-encoding](https://github.com/compact-encoding/compact-encoding) codecs for net types.
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
npm install compact-encoding-net
|
||||
```
|
||||
|
||||
## Codecs
|
||||
|
||||
### `port`
|
||||
|
||||
Codec for 16 bit port numbers.
|
||||
|
||||
```js
|
||||
const { port } = require('compact-encoding-net')
|
||||
```
|
||||
|
||||
#### Encoding
|
||||
|
||||
```js
|
||||
const buffer = cenc.encode(port, 8080)
|
||||
```
|
||||
|
||||
#### Decoding
|
||||
|
||||
```js
|
||||
cenc.decode(port, buffer)
|
||||
// 8080
|
||||
```
|
||||
|
||||
### `ipv4`
|
||||
|
||||
Codec for IPv4 addresses.
|
||||
|
||||
> :warning: The codec is only defined for valid IPv4 addresses.
|
||||
|
||||
```js
|
||||
const { ipv4 } = require('compact-encoding-net')
|
||||
```
|
||||
|
||||
#### Encoding
|
||||
|
||||
```js
|
||||
const buffer = cenc.encode(ipv4, '127.0.0.1')
|
||||
```
|
||||
|
||||
#### Decoding
|
||||
|
||||
```js
|
||||
cenc.decode(ipv4, buffer)
|
||||
// '127.0.0.1'
|
||||
```
|
||||
|
||||
### `ipv4Address`
|
||||
|
||||
Codec for IPv4 addresses plus a port.
|
||||
|
||||
```js
|
||||
const { ipv4Address } = require('compact-encoding-net')
|
||||
```
|
||||
|
||||
#### Encoding
|
||||
|
||||
```js
|
||||
const buffer = cenc.encode(ipv4, { host: '127.0.0.1', port: 8080 })
|
||||
```
|
||||
|
||||
#### Decoding
|
||||
|
||||
```js
|
||||
cenc.decode(ipv4Address, buffer)
|
||||
// { host: '127.0.0.1', port: 8080 }
|
||||
```
|
||||
|
||||
### `ipv6`
|
||||
|
||||
Codec for IPv6 addresses.
|
||||
|
||||
> :warning: The codec is only defined for valid IPv6 addresses.
|
||||
|
||||
```js
|
||||
const { ipv6 } = require('compact-encoding-net')
|
||||
```
|
||||
|
||||
#### Encoding
|
||||
|
||||
```js
|
||||
const buffer = cenc.encode(ipv6, '::1')
|
||||
```
|
||||
|
||||
#### Decoding
|
||||
|
||||
```js
|
||||
cenc.decode(ipv6, buffer)
|
||||
// '0:0:0:0:0:0:0:1'
|
||||
```
|
||||
|
||||
### `ipv6Address`
|
||||
|
||||
Codec for IPv6 addresses plus a port.
|
||||
|
||||
```js
|
||||
const { ipv6Address } = require('compact-encoding-net')
|
||||
```
|
||||
|
||||
#### Encoding
|
||||
|
||||
```js
|
||||
const buffer = cenc.encode(ipv6Address, { host: '::1', port: 8080 })
|
||||
```
|
||||
|
||||
#### Decoding
|
||||
|
||||
```js
|
||||
cenc.decode(ipv6Address, buffer)
|
||||
// { host: '0:0:0:0:0:0:0:1', port: 8080 }
|
||||
```
|
||||
|
||||
### `ip`
|
||||
|
||||
Codec for dual IPv4/6 addresses.
|
||||
|
||||
> :warning: The codec is only defined for valid IPv4 and IPv6 addresses.
|
||||
|
||||
```js
|
||||
const { ip } = require('compact-encoding-net')
|
||||
```
|
||||
|
||||
#### Encoding
|
||||
|
||||
```js
|
||||
const buffer = cenc.encode(ip, '::1')
|
||||
```
|
||||
|
||||
#### Decoding
|
||||
|
||||
```js
|
||||
cenc.decode(ip, buffer)
|
||||
// '0:0:0:0:0:0:0:1'
|
||||
```
|
||||
|
||||
### `ipAddress`
|
||||
|
||||
Codec for dual IPv4/6 addresses plus a port.
|
||||
|
||||
```js
|
||||
const { ipAddress } = require('compact-encoding-net')
|
||||
```
|
||||
|
||||
#### Encoding
|
||||
|
||||
```js
|
||||
const buffer = cenc.encode(ipAddress, { host: '::1', port: 8080 })
|
||||
```
|
||||
|
||||
#### Decoding
|
||||
|
||||
```js
|
||||
cenc.decode(ipv6Address, buffer)
|
||||
// { host: '0:0:0:0:0:0:0:1', family: 6, port: 8080 }
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
Apache 2.0
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
const bench = require('nanobench')
|
||||
const c = require('compact-encoding')
|
||||
const { ipv4 } = require('../')
|
||||
|
||||
const ip = '1.2.3.4'
|
||||
|
||||
bench('ipv4 encode', function (b) {
|
||||
b.start()
|
||||
|
||||
let result
|
||||
|
||||
for (let i = 0; i < 10000000; i++) {
|
||||
result = c.encode(ipv4, ip)
|
||||
}
|
||||
|
||||
b.log(result.toString('hex'))
|
||||
b.end()
|
||||
})
|
||||
|
||||
bench('ipv4 encode', function (b) {
|
||||
const buffer = c.encode(ipv4, ip)
|
||||
|
||||
b.start()
|
||||
|
||||
let result
|
||||
|
||||
for (let i = 0; i < 10000000; i++) {
|
||||
result = c.decode(ipv4, buffer)
|
||||
}
|
||||
|
||||
b.log(JSON.stringify(result))
|
||||
b.end()
|
||||
})
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
const bench = require('nanobench')
|
||||
const c = require('compact-encoding')
|
||||
const { ipv6 } = require('../')
|
||||
|
||||
const ip = '1:2:3:4:5:6:7:8'
|
||||
|
||||
bench('ipv6 encode', function (b) {
|
||||
b.start()
|
||||
|
||||
let result
|
||||
|
||||
for (let i = 0; i < 10000000; i++) {
|
||||
result = c.encode(ipv6, ip)
|
||||
}
|
||||
|
||||
b.log(result.toString('hex'))
|
||||
b.end()
|
||||
})
|
||||
|
||||
bench('ipv6 encode', function (b) {
|
||||
const buffer = c.encode(ipv6, ip)
|
||||
|
||||
b.start()
|
||||
|
||||
let result
|
||||
|
||||
for (let i = 0; i < 10000000; i++) {
|
||||
result = c.decode(ipv6, buffer)
|
||||
}
|
||||
|
||||
b.log(JSON.stringify(result))
|
||||
b.end()
|
||||
})
|
||||
+215
@@ -0,0 +1,215 @@
|
||||
const c = require('compact-encoding')
|
||||
|
||||
const port = c.uint16
|
||||
|
||||
const address = (host, family) => {
|
||||
return {
|
||||
preencode(state, m) {
|
||||
host.preencode(state, m.host)
|
||||
port.preencode(state, m.port || 0)
|
||||
},
|
||||
encode(state, m) {
|
||||
host.encode(state, m.host)
|
||||
port.encode(state, m.port || 0)
|
||||
},
|
||||
decode(state) {
|
||||
return {
|
||||
host: host.decode(state),
|
||||
family,
|
||||
port: port.decode(state)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ipv4 = {
|
||||
preencode(state) {
|
||||
state.end += 4
|
||||
},
|
||||
encode(state, string) {
|
||||
string = string || '127.0.0.1'
|
||||
|
||||
const start = state.start
|
||||
const end = start + 4
|
||||
|
||||
let i = 0
|
||||
|
||||
while (i < string.length) {
|
||||
let n = 0
|
||||
let c
|
||||
|
||||
while (
|
||||
i < string.length &&
|
||||
(c = string.charCodeAt(i++)) !== /* . */ 0x2e
|
||||
) {
|
||||
n = n * 10 + (c - /* 0 */ 0x30)
|
||||
}
|
||||
|
||||
state.buffer[state.start++] = n
|
||||
}
|
||||
|
||||
state.start = end
|
||||
},
|
||||
decode(state) {
|
||||
if (state.end - state.start < 4) throw new Error('Out of bounds')
|
||||
return (
|
||||
state.buffer[state.start++] +
|
||||
'.' +
|
||||
state.buffer[state.start++] +
|
||||
'.' +
|
||||
state.buffer[state.start++] +
|
||||
'.' +
|
||||
state.buffer[state.start++]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const ipv4Address = address(ipv4, 4)
|
||||
|
||||
const ipv6 = {
|
||||
preencode(state) {
|
||||
state.end += 16
|
||||
},
|
||||
encode(state, string) {
|
||||
string = string || '::1'
|
||||
|
||||
const start = state.start
|
||||
const end = start + 16
|
||||
|
||||
let i = 0
|
||||
let split = null
|
||||
|
||||
while (i < string.length) {
|
||||
let n = 0
|
||||
let c
|
||||
|
||||
while (
|
||||
i < string.length &&
|
||||
(c = string.charCodeAt(i++)) !== /* : */ 0x3a
|
||||
) {
|
||||
if (c >= 0x30 && c <= 0x39) n = n * 0x10 + (c - /* 0 */ 0x30)
|
||||
else if (c >= 0x41 && c <= 0x46) n = n * 0x10 + (c - /* A */ 0x41 + 10)
|
||||
else if (c >= 0x61 && c <= 0x66) n = n * 0x10 + (c - /* a */ 0x61 + 10)
|
||||
}
|
||||
|
||||
state.buffer[state.start++] = n >>> 8
|
||||
state.buffer[state.start++] = n
|
||||
|
||||
if (i < string.length && string.charCodeAt(i) === /* : */ 0x3a) {
|
||||
i++
|
||||
split = state.start
|
||||
}
|
||||
}
|
||||
|
||||
if (split !== null) {
|
||||
const offset = end - state.start
|
||||
state.buffer
|
||||
.copyWithin(split + offset, split)
|
||||
.fill(0, split, split + offset)
|
||||
}
|
||||
|
||||
state.start = end
|
||||
},
|
||||
decode(state) {
|
||||
if (state.end - state.start < 16) throw new Error('Out of bounds')
|
||||
return (
|
||||
(
|
||||
state.buffer[state.start++] * 256 +
|
||||
state.buffer[state.start++]
|
||||
).toString(16) +
|
||||
':' +
|
||||
(
|
||||
state.buffer[state.start++] * 256 +
|
||||
state.buffer[state.start++]
|
||||
).toString(16) +
|
||||
':' +
|
||||
(
|
||||
state.buffer[state.start++] * 256 +
|
||||
state.buffer[state.start++]
|
||||
).toString(16) +
|
||||
':' +
|
||||
(
|
||||
state.buffer[state.start++] * 256 +
|
||||
state.buffer[state.start++]
|
||||
).toString(16) +
|
||||
':' +
|
||||
(
|
||||
state.buffer[state.start++] * 256 +
|
||||
state.buffer[state.start++]
|
||||
).toString(16) +
|
||||
':' +
|
||||
(
|
||||
state.buffer[state.start++] * 256 +
|
||||
state.buffer[state.start++]
|
||||
).toString(16) +
|
||||
':' +
|
||||
(
|
||||
state.buffer[state.start++] * 256 +
|
||||
state.buffer[state.start++]
|
||||
).toString(16) +
|
||||
':' +
|
||||
(
|
||||
state.buffer[state.start++] * 256 +
|
||||
state.buffer[state.start++]
|
||||
).toString(16)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const ipv6Address = address(ipv6, 6)
|
||||
|
||||
const ip = {
|
||||
preencode(state, string) {
|
||||
string = string || '127.0.0.1'
|
||||
|
||||
const family = string.includes(':') ? 6 : 4
|
||||
c.uint8.preencode(state, family)
|
||||
|
||||
if (family === 4) ipv4.preencode(state)
|
||||
else ipv6.preencode(state)
|
||||
},
|
||||
encode(state, string) {
|
||||
string = string || '127.0.0.1'
|
||||
|
||||
const family = string.includes(':') ? 6 : 4
|
||||
c.uint8.encode(state, family)
|
||||
|
||||
if (family === 4) ipv4.encode(state, string)
|
||||
else ipv6.encode(state, string)
|
||||
},
|
||||
decode(state) {
|
||||
const family = c.uint8.decode(state)
|
||||
|
||||
if (family === 4) return ipv4.decode(state)
|
||||
else return ipv6.decode(state)
|
||||
}
|
||||
}
|
||||
|
||||
const ipAddress = {
|
||||
preencode(state, m) {
|
||||
ip.preencode(state, m.host)
|
||||
port.preencode(state, m.port || 0)
|
||||
},
|
||||
encode(state, m) {
|
||||
ip.encode(state, m.host)
|
||||
port.encode(state, m.port || 0)
|
||||
},
|
||||
decode(state) {
|
||||
const family = c.uint8.decode(state)
|
||||
return {
|
||||
host: family === 4 ? ipv4.decode(state) : ipv6.decode(state),
|
||||
family,
|
||||
port: port.decode(state)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
port,
|
||||
ipv4,
|
||||
ipv4Address,
|
||||
ipv6,
|
||||
ipv6Address,
|
||||
ip,
|
||||
ipAddress
|
||||
}
|
||||
+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.
|
||||
+165
@@ -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
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
const LE = (exports.LE =
|
||||
new Uint8Array(new Uint16Array([0xff]).buffer)[0] === 0xff)
|
||||
|
||||
exports.BE = !LE
|
||||
+1091
File diff suppressed because it is too large
Load Diff
+117
@@ -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
|
||||
}
|
||||
+34
@@ -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"
|
||||
}
|
||||
+170
@@ -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))
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "compact-encoding-net",
|
||||
"version": "1.3.0",
|
||||
"description": "Compact codecs for net types",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"format": "prettier --write .",
|
||||
"lint": "prettier --check . && lunte",
|
||||
"test": "npm run test:node && npm run test:bare",
|
||||
"test:node": "brittle-node test.mjs",
|
||||
"test:bare": "brittle-bare test.mjs"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/compact-encoding/compact-encoding-net.git"
|
||||
},
|
||||
"author": "Kasper Isager Dalsgarð <[email protected]>",
|
||||
"license": "Apache-2.0",
|
||||
"bugs": {
|
||||
"url": "https://github.com/compact-encoding/compact-encoding-net/issues"
|
||||
},
|
||||
"homepage": "https://github.com/compact-encoding/compact-encoding-net#readme",
|
||||
"dependencies": {
|
||||
"compact-encoding": "^3.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"brittle": "^3.16.3",
|
||||
"lunte": "^1.4.0",
|
||||
"nanobench": "^2.1.1",
|
||||
"prettier": "^3.6.2",
|
||||
"prettier-config-holepunch": "^1.0.0"
|
||||
}
|
||||
}
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
import test from 'brittle'
|
||||
import c from 'compact-encoding'
|
||||
|
||||
import {
|
||||
port,
|
||||
ipv4,
|
||||
ipv6,
|
||||
ipv4Address,
|
||||
ipv6Address,
|
||||
ip,
|
||||
ipAddress
|
||||
} from './index.js'
|
||||
|
||||
test('port', (t) => {
|
||||
const p = 0x1234
|
||||
const buf = Buffer.from([0x34, 0x12])
|
||||
|
||||
t.alike(c.encode(port, p), buf)
|
||||
t.alike(c.decode(port, buf), p)
|
||||
})
|
||||
|
||||
test('ipv4', (t) => {
|
||||
const ip = '1.2.3.4'
|
||||
const buf = Buffer.from([1, 2, 3, 4])
|
||||
|
||||
t.alike(c.encode(ipv4, ip), buf)
|
||||
t.alike(c.decode(ipv4, buf), ip)
|
||||
})
|
||||
|
||||
test('ipv4 + port', (t) => {
|
||||
const host = '1.2.3.4'
|
||||
const port = 1234
|
||||
|
||||
t.alike(c.decode(ipv4Address, c.encode(ipv4Address, { host, port })), {
|
||||
host,
|
||||
family: 4,
|
||||
port
|
||||
})
|
||||
})
|
||||
|
||||
test('default ipv4', (t) => {
|
||||
const host = null
|
||||
const port = 1234
|
||||
|
||||
t.alike(c.decode(ipv4Address, c.encode(ipv4Address, { host, port })), {
|
||||
host: '127.0.0.1',
|
||||
family: 4,
|
||||
port
|
||||
})
|
||||
})
|
||||
|
||||
test('ipv6', (t) => {
|
||||
const ip = '1:2:3:4:5:6:7:8'
|
||||
const buf = Buffer.from([0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8])
|
||||
|
||||
t.alike(c.encode(ipv6, ip), buf)
|
||||
t.alike(c.decode(ipv6, buf), ip)
|
||||
|
||||
t.test('abbreviated', (t) => {
|
||||
const buf = Buffer.from([0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 8])
|
||||
|
||||
t.alike(c.encode(ipv6, '1:2::7:8'), buf)
|
||||
t.alike(c.decode(ipv6, buf), '1:2:0:0:0:0:7:8')
|
||||
})
|
||||
|
||||
t.test('prefix abbreviated', (t) => {
|
||||
const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 0, 7, 0, 8])
|
||||
|
||||
t.alike(c.encode(ipv6, '::5:6:7:8'), buf)
|
||||
t.alike(c.decode(ipv6, buf), '0:0:0:0:5:6:7:8')
|
||||
})
|
||||
|
||||
t.test('suffix abbreviated', (t) => {
|
||||
const buf = Buffer.from([0, 1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0])
|
||||
|
||||
t.alike(c.encode(ipv6, '1:2:3:4::'), buf)
|
||||
t.alike(c.decode(ipv6, buf), '1:2:3:4:0:0:0:0')
|
||||
})
|
||||
|
||||
t.test('any', (t) => {
|
||||
const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
||||
|
||||
t.alike(c.encode(ipv6, '::'), buf)
|
||||
t.alike(c.decode(ipv6, buf), '0:0:0:0:0:0:0:0')
|
||||
})
|
||||
|
||||
t.test('lowercase hex', (t) => {
|
||||
const buf = Buffer.from([
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd
|
||||
])
|
||||
|
||||
t.alike(c.encode(ipv6, '::abcd'), buf)
|
||||
t.alike(c.decode(ipv6, buf), '0:0:0:0:0:0:0:abcd')
|
||||
})
|
||||
|
||||
t.test('uppercase hex', (t) => {
|
||||
const buf = Buffer.from([
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd
|
||||
])
|
||||
|
||||
t.alike(c.encode(ipv6, '::ABCD'), buf)
|
||||
t.alike(c.decode(ipv6, buf), '0:0:0:0:0:0:0:abcd')
|
||||
})
|
||||
})
|
||||
|
||||
test('ipv6 + port', (t) => {
|
||||
const host = '1:2:3:4:5:6:7:8'
|
||||
const port = 1234
|
||||
|
||||
t.alike(c.decode(ipv6Address, c.encode(ipv6Address, { host, port })), {
|
||||
host,
|
||||
family: 6,
|
||||
port
|
||||
})
|
||||
})
|
||||
|
||||
test('default ipv6', (t) => {
|
||||
const host = null
|
||||
const port = 1234
|
||||
|
||||
t.alike(c.decode(ipv6Address, c.encode(ipv6Address, { host, port })), {
|
||||
host: '0:0:0:0:0:0:0:1',
|
||||
family: 6,
|
||||
port
|
||||
})
|
||||
})
|
||||
|
||||
test('dual ip', (t) => {
|
||||
{
|
||||
const host = '1.2.3.4'
|
||||
|
||||
t.alike(c.decode(ip, c.encode(ip, host)), host, 'ipv4')
|
||||
}
|
||||
{
|
||||
const host = '1:2:3:4:5:6:7:8'
|
||||
|
||||
t.alike(c.decode(ip, c.encode(ip, host)), host, 'ipv6')
|
||||
}
|
||||
})
|
||||
|
||||
test('dual ip + port', (t) => {
|
||||
const port = 1234
|
||||
|
||||
{
|
||||
const host = '1.2.3.4'
|
||||
|
||||
t.alike(
|
||||
c.decode(ipAddress, c.encode(ipAddress, { host, port })),
|
||||
{
|
||||
host,
|
||||
family: 4,
|
||||
port
|
||||
},
|
||||
'ipv4'
|
||||
)
|
||||
}
|
||||
{
|
||||
const host = '1:2:3:4:5:6:7:8'
|
||||
|
||||
t.alike(
|
||||
c.decode(ipAddress, c.encode(ipAddress, { host, port })),
|
||||
{
|
||||
host,
|
||||
family: 6,
|
||||
port
|
||||
},
|
||||
'ipv6'
|
||||
)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user