compact-encoding 2.18.0 → 2.19.1
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/index.js +46 -1
- package/package.json +4 -4
- package/test.js +43 -0
package/index.js
CHANGED
|
@@ -665,7 +665,21 @@ const any = (exports.any = {
|
|
|
665
665
|
}
|
|
666
666
|
})
|
|
667
667
|
|
|
668
|
-
const port = (exports.port =
|
|
668
|
+
const port = (exports.port = {
|
|
669
|
+
preencode(state, p) {
|
|
670
|
+
if (!p) throw new Error('Port must be >0 and <65536')
|
|
671
|
+
uint16.preencode(state, p)
|
|
672
|
+
},
|
|
673
|
+
encode(state, p) {
|
|
674
|
+
if (!p) throw new Error('Port must be >0 and <65536')
|
|
675
|
+
uint16.encode(state, p)
|
|
676
|
+
},
|
|
677
|
+
decode(state) {
|
|
678
|
+
const p = uint16.decode(state)
|
|
679
|
+
if (!p) throw new Error('Port must be >0 and <65536')
|
|
680
|
+
return p
|
|
681
|
+
}
|
|
682
|
+
})
|
|
669
683
|
|
|
670
684
|
const address = (host, family) => {
|
|
671
685
|
return {
|
|
@@ -858,6 +872,37 @@ exports.ipAddress = {
|
|
|
858
872
|
}
|
|
859
873
|
}
|
|
860
874
|
|
|
875
|
+
const record = (exports.record = function (keyEncoding, valueEncoding) {
|
|
876
|
+
return {
|
|
877
|
+
preencode(state, v) {
|
|
878
|
+
const keys = Object.keys(v)
|
|
879
|
+
uint.preencode(state, keys.length)
|
|
880
|
+
for (const k of keys) {
|
|
881
|
+
keyEncoding.preencode(state, k)
|
|
882
|
+
valueEncoding.preencode(state, v[k])
|
|
883
|
+
}
|
|
884
|
+
},
|
|
885
|
+
encode(state, v) {
|
|
886
|
+
const keys = Object.keys(v)
|
|
887
|
+
uint.encode(state, keys.length)
|
|
888
|
+
for (const k of keys) {
|
|
889
|
+
keyEncoding.encode(state, k)
|
|
890
|
+
valueEncoding.encode(state, v[k])
|
|
891
|
+
}
|
|
892
|
+
},
|
|
893
|
+
decode(state) {
|
|
894
|
+
const out = Object.create(null)
|
|
895
|
+
const keys = uint.decode(state)
|
|
896
|
+
for (let i = 0; i < keys; i++) {
|
|
897
|
+
out[keyEncoding.decode(state)] = valueEncoding.decode(state)
|
|
898
|
+
}
|
|
899
|
+
return out
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
})
|
|
903
|
+
|
|
904
|
+
exports.stringRecord = record(utf8, utf8)
|
|
905
|
+
|
|
861
906
|
function getType(o) {
|
|
862
907
|
if (o === null || o === undefined) return 0
|
|
863
908
|
if (typeof o === 'boolean') return 1
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "compact-encoding",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.19.1",
|
|
4
4
|
"description": "A series of compact encoding schemes for building small and fast parsers and serializers",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
@@ -17,12 +17,12 @@
|
|
|
17
17
|
},
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|
|
20
|
-
"url": "https://github.com/
|
|
20
|
+
"url": "https://github.com/holepunchto/compact-encoding.git"
|
|
21
21
|
},
|
|
22
22
|
"author": "Mathias Buus (@mafintosh)",
|
|
23
23
|
"license": "Apache-2.0",
|
|
24
24
|
"bugs": {
|
|
25
|
-
"url": "https://github.com/
|
|
25
|
+
"url": "https://github.com/holepunchto/compact-encoding/issues"
|
|
26
26
|
},
|
|
27
|
-
"homepage": "https://github.com/
|
|
27
|
+
"homepage": "https://github.com/holepunchto/compact-encoding"
|
|
28
28
|
}
|
package/test.js
CHANGED
|
@@ -1139,3 +1139,46 @@ test('dual ip + port', function (t) {
|
|
|
1139
1139
|
)
|
|
1140
1140
|
}
|
|
1141
1141
|
})
|
|
1142
|
+
|
|
1143
|
+
test('record', function (t) {
|
|
1144
|
+
const encoding = enc.record(enc.string, enc.string)
|
|
1145
|
+
|
|
1146
|
+
t.alike(
|
|
1147
|
+
enc.decode(encoding, enc.encode(encoding, { a: 'hello', b: 'world' })),
|
|
1148
|
+
Object.assign(Object.create(null), {
|
|
1149
|
+
a: 'hello',
|
|
1150
|
+
b: 'world'
|
|
1151
|
+
})
|
|
1152
|
+
)
|
|
1153
|
+
})
|
|
1154
|
+
|
|
1155
|
+
test('record - nested', function (t) {
|
|
1156
|
+
const encoding = enc.record(enc.string, enc.record(enc.string, enc.string))
|
|
1157
|
+
|
|
1158
|
+
t.alike(
|
|
1159
|
+
enc.decode(
|
|
1160
|
+
encoding,
|
|
1161
|
+
enc.encode(encoding, {
|
|
1162
|
+
a: { b: 'record' },
|
|
1163
|
+
c: { d: 'nested', e: 'test' }
|
|
1164
|
+
})
|
|
1165
|
+
),
|
|
1166
|
+
Object.assign(Object.create(null), {
|
|
1167
|
+
a: Object.assign(Object.create(null), { b: 'record' }),
|
|
1168
|
+
c: Object.assign(Object.create(null), { d: 'nested', e: 'test' })
|
|
1169
|
+
})
|
|
1170
|
+
)
|
|
1171
|
+
})
|
|
1172
|
+
|
|
1173
|
+
test('stringRecord', function (t) {
|
|
1174
|
+
t.alike(
|
|
1175
|
+
enc.decode(
|
|
1176
|
+
enc.stringRecord,
|
|
1177
|
+
enc.encode(enc.stringRecord, { a: 'hello', b: 'world' })
|
|
1178
|
+
),
|
|
1179
|
+
Object.assign(Object.create(null), {
|
|
1180
|
+
a: 'hello',
|
|
1181
|
+
b: 'world'
|
|
1182
|
+
})
|
|
1183
|
+
)
|
|
1184
|
+
})
|