compact-encoding 2.16.1 → 2.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +1 -0
- package/.github/workflows/test-node.yml +7 -7
- package/.prettierrc +1 -0
- package/README.md +92 -86
- package/endian.js +2 -1
- package/index.js +423 -181
- package/lexint.js +19 -16
- package/package.json +5 -3
- package/raw.js +34 -34
- package/test.js +336 -38
package/.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* text=auto eol=lf
|
|
@@ -14,10 +14,10 @@ jobs:
|
|
|
14
14
|
os: [ubuntu-latest, macos-latest, windows-latest]
|
|
15
15
|
runs-on: ${{ matrix.os }}
|
|
16
16
|
steps:
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
- uses: actions/checkout@v2
|
|
18
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
19
|
+
uses: actions/setup-node@v2
|
|
20
|
+
with:
|
|
21
|
+
node-version: ${{ matrix.node-version }}
|
|
22
|
+
- run: npm install
|
|
23
|
+
- run: npm test
|
package/.prettierrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
prettier-config-holepunch
|
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ npm install compact-encoding
|
|
|
8
8
|
|
|
9
9
|
## Usage
|
|
10
10
|
|
|
11
|
-
```
|
|
11
|
+
```js
|
|
12
12
|
const cenc = require('compact-encoding')
|
|
13
13
|
|
|
14
14
|
const state = cenc.state()
|
|
@@ -17,7 +17,7 @@ const state = cenc.state()
|
|
|
17
17
|
cenc.uint.preencode(state, 42)
|
|
18
18
|
cenc.string.preencode(state, 'hi')
|
|
19
19
|
|
|
20
|
-
console.log(state) // { start: 0, end: 4, buffer: null
|
|
20
|
+
console.log(state) // { start: 0, end: 4, buffer: null }
|
|
21
21
|
|
|
22
22
|
state.buffer = Buffer.allocUnsafe(state.end)
|
|
23
23
|
|
|
@@ -36,14 +36,13 @@ cenc.string.decode(state) // 'hi'
|
|
|
36
36
|
|
|
37
37
|
#### `state`
|
|
38
38
|
|
|
39
|
-
Should be an object that looks like this `{ start, end, buffer
|
|
39
|
+
Should be an object that looks like this `{ start, end, buffer }`.
|
|
40
40
|
|
|
41
41
|
You can also get a blank state object using `cenc.state()`.
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
* `cache` is used internally be codecs, starts out as `null`.
|
|
43
|
+
- `start` is the byte offset to start encoding/decoding at.
|
|
44
|
+
- `end` is the byte offset indicating the end of the buffer.
|
|
45
|
+
- `buffer` should be either a Node.js Buffer or Uint8Array.
|
|
47
46
|
|
|
48
47
|
#### `enc.preencode(state, val)`
|
|
49
48
|
|
|
@@ -65,7 +64,7 @@ Updates `state.start` to point after the decoded value when done in the buffer.
|
|
|
65
64
|
If you are just encoding to a buffer or decoding from one you can use the `encode` and `decode` helpers
|
|
66
65
|
to reduce your boilerplate
|
|
67
66
|
|
|
68
|
-
```
|
|
67
|
+
```js
|
|
69
68
|
const buf = cenc.encode(cenc.bool, true)
|
|
70
69
|
const bool = cenc.decode(cenc.bool, buf)
|
|
71
70
|
```
|
|
@@ -75,84 +74,91 @@ const bool = cenc.decode(cenc.bool, buf)
|
|
|
75
74
|
The following encodings are bundled as they are primitives that can be used
|
|
76
75
|
to build others on top. Feel free to PR more that are missing.
|
|
77
76
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
77
|
+
- `cenc.raw` - Pass through encodes a buffer, i.e. a basic copy.
|
|
78
|
+
- `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.
|
|
79
|
+
- `cenc.uint8` - Encodes a fixed size uint8.
|
|
80
|
+
- `cenc.uint16` - Encodes a fixed size uint16. Useful for things like ports.
|
|
81
|
+
- `cenc.uint24` - Encodes a fixed size uint24. Useful for message framing.
|
|
82
|
+
- `cenc.uint32` - Encodes a fixed size uint32. Useful for very large message framing.
|
|
83
|
+
- `cenc.uint40` - Encodes a fixed size uint40.
|
|
84
|
+
- `cenc.uint48` - Encodes a fixed size uint48.
|
|
85
|
+
- `cenc.uint56` - Encodes a fixed size uint56.
|
|
86
|
+
- `cenc.uint64` - Encodes a fixed size uint64.
|
|
87
|
+
- `cenc.int` - Encodes an int using `cenc.uint` with ZigZag encoding.
|
|
88
|
+
- `cenc.int8` - Encodes a fixed size int8 using `cenc.uint8` with ZigZag encoding.
|
|
89
|
+
- `cenc.int16` - Encodes a fixed size int16 using `cenc.uint16` with ZigZag encoding.
|
|
90
|
+
- `cenc.int24` - Encodes a fixed size int24 using `cenc.uint24` with ZigZag encoding.
|
|
91
|
+
- `cenc.int32` - Encodes a fixed size int32 using `cenc.uint32` with ZigZag encoding.
|
|
92
|
+
- `cenc.int40` - Encodes a fixed size int40 using `cenc.uint40` with ZigZag encoding.
|
|
93
|
+
- `cenc.int48` - Encodes a fixed size int48 using `cenc.uint48` with ZigZag encoding.
|
|
94
|
+
- `cenc.int56` - Encodes a fixed size int56 using `cenc.uint56` with ZigZag encoding.
|
|
95
|
+
- `cenc.int64` - Encodes a fixed size int64 using `cenc.uint64` with ZigZag encoding.
|
|
96
|
+
- `cenc.biguint64` - Encodes a fixed size biguint64.
|
|
97
|
+
- `cenc.bigint64` - Encodes a fixed size bigint64 using `cenc.biguint64` with ZigZag encoding.
|
|
98
|
+
- `cenc.biguint` - Encodes a biguint with its word count uint prefixed.
|
|
99
|
+
- `cenc.bigint` - Encodes a bigint using `cenc.biguint` with ZigZag encoding.
|
|
100
|
+
- `cenc.float32` - Encodes a fixed size float32.
|
|
101
|
+
- `cenc.float64` - Encodes a fixed size float64.
|
|
102
|
+
- `cenc.buffer` - Encodes a buffer with its length uint prefixed. When decoding an empty buffer, `null` is returned.
|
|
103
|
+
- `cenc.raw.buffer` - Encodes a buffer without a length prefixed.
|
|
104
|
+
- `cenc.arraybuffer` - Encodes an arraybuffer with its length uint prefixed.
|
|
105
|
+
- `cenc.raw.arraybuffer` - Encodes an arraybuffer without a length prefixed.
|
|
106
|
+
- `cenc.uint8array` - Encodes a uint8array with its element length uint prefixed.
|
|
107
|
+
- `cenc.raw.uint8array` - Encodes a uint8array without a length prefixed.
|
|
108
|
+
- `cenc.uint16array` - Encodes a uint16array with its element length uint prefixed.
|
|
109
|
+
- `cenc.raw.uint16array` - Encodes a uint16array without a length prefixed.
|
|
110
|
+
- `cenc.uint32array` - Encodes a uint32array with its element length uint prefixed.
|
|
111
|
+
- `cenc.raw.uint32array` - Encodes a uint32array without a length prefixed.
|
|
112
|
+
- `cenc.int8array` - Encodes a int8array with its element length uint prefixed.
|
|
113
|
+
- `cenc.raw.int8array` - Encodes a int8array without a length prefixed.
|
|
114
|
+
- `cenc.int16array` - Encodes a int16array with its element length uint prefixed.
|
|
115
|
+
- `cenc.raw.int16array` - Encodes a int16array without a length prefixed.
|
|
116
|
+
- `cenc.int32array` - Encodes a int32array with its element length uint prefixed.
|
|
117
|
+
- `cenc.raw.int32array` - Encodes a int32array without a length prefixed.
|
|
118
|
+
- `cenc.biguint64array` - Encodes a biguint64array with its element length uint prefixed.
|
|
119
|
+
- `cenc.raw.biguint64array` - Encodes a biguint64array without a length prefixed.
|
|
120
|
+
- `cenc.bigint64array` - Encodes a bigint64array with its element length uint prefixed.
|
|
121
|
+
- `cenc.raw.bigint64array` - Encodes a bigint64array without a length prefixed.
|
|
122
|
+
- `cenc.float32array` - Encodes a float32array with its element length uint prefixed.
|
|
123
|
+
- `cenc.raw.float32array` - Encodes a float32array without a length prefixed.
|
|
124
|
+
- `cenc.float64array` - Encodes a float64array with its element length uint prefixed.
|
|
125
|
+
- `cenc.raw.float64array` - Encodes a float64array without a length prefixed.
|
|
126
|
+
- `cenc.bool` - Encodes a boolean as 1 or 0.
|
|
127
|
+
- `cenc.string`, `cenc.utf8` - Encodes a utf-8 string, similar to buffer.
|
|
128
|
+
- `cenc.raw.string`, `cenc.raw.utf8` - Encodes a utf-8 string without a length prefixed.
|
|
129
|
+
- `cenc.string.fixed(n)`, `cenc.utf8.fixed(n)` - Encodes a fixed sized utf-8 string.
|
|
130
|
+
- `cenc.ascii` - Encodes an ascii string.
|
|
131
|
+
- `cenc.raw.ascii` - Encodes an ascii string without a length prefixed.
|
|
132
|
+
- `cenc.ascii.fixed(n)` - Encodes a fixed size ascii string.
|
|
133
|
+
- `cenc.hex` - Encodes a hex string.
|
|
134
|
+
- `cenc.raw.hex` - Encodes a hex string without a length prefixed.
|
|
135
|
+
- `cenc.hex.fixed(n)` - Encodes a fixed size hex string.
|
|
136
|
+
- `cenc.base64` - Encodes a base64 string.
|
|
137
|
+
- `cenc.raw.base64` - Encodes a base64 string without a length prefixed.
|
|
138
|
+
- `cenc.base64.fixed(n)` - Encodes a fixed size base64 string.
|
|
139
|
+
- `cenc.utf16le`, `cenc.ucs2` - Encodes a utf16le string.
|
|
140
|
+
- `cenc.raw.utf16le`, `cenc.raw.ucs2` - Encodes a utf16le string without a length prefixed.
|
|
141
|
+
- `cenc.utf16le.fixed(n)`, `cenc.ucs2.fixed(n)` - Encodes a fixed size utf16le string.
|
|
142
|
+
- `cenc.fixed32` - Encodes a fixed 32 byte buffer.
|
|
143
|
+
- `cenc.fixed64` - Encodes a fixed 64 byte buffer.
|
|
144
|
+
- `cenc.fixed(n)` - Makes a fixed sized encoder.
|
|
145
|
+
- `cenc.date(d)` - Encodes a date object.
|
|
146
|
+
- `cenc.array(enc)` - Makes an array encoder from another encoder. Arrays are uint prefixed with their length.
|
|
147
|
+
- `cenc.raw.array(enc)` - Makes an array encoder from another encoder, without a length prefixed.
|
|
148
|
+
- `cenc.json` - Encodes a JSON value as utf-8.
|
|
149
|
+
- `cenc.raw.json` - Encodes a JSON value as utf-8 without a length prefixed.
|
|
150
|
+
- `cenc.ndjson` - Encodes a JSON value as newline delimited utf-8.
|
|
151
|
+
- `cenc.raw.ndjson` - Encodes a JSON value as newline delimited utf-8 without a length prefixed.
|
|
152
|
+
- `cenc.any` - Encodes any JSON representable value into a self described buffer. Like JSON + buffer, but using compact types. Useful for schemaless codecs.
|
|
153
|
+
- `cenc.port` - Encodes a port number for network addresses.
|
|
154
|
+
- `cenc.ipv4` - Encodes an IPv4 network address.
|
|
155
|
+
- `cenc.ipv4Address` Encodes an IPv4 network address and a port number.
|
|
156
|
+
- `cenc.ipv6` - Encodes an IPv6 network address.
|
|
157
|
+
- `cenc.ipv6Address` Encodes an IPv6 network address and a port number.
|
|
158
|
+
- `cenc.ip` - Encodes a dual IPv4/6 network address.
|
|
159
|
+
- `cenc.ipAddress` Encodes a dual IPv4/6 network address and a port number.
|
|
160
|
+
- `cenc.from(enc)` - Makes a compact encoder from a [codec](https://github.com/mafintosh/codecs) or [abstract-encoding](https://github.com/mafintosh/abstract-encoding).
|
|
161
|
+
- `cenc.none` - Helper for when you want to just express nothing
|
|
156
162
|
|
|
157
163
|
## License
|
|
158
164
|
|
package/endian.js
CHANGED