@leofcoin/codec-format-interface 1.0.1 → 1.1.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/dist/index.js +69 -32
- package/package.json +2 -1
- package/src/basic-interface.js +9 -9
- package/src/codec-format-interface.js +39 -21
- package/test/test.js +22 -5
package/dist/index.js
CHANGED
|
@@ -8,6 +8,24 @@ var createKeccakHash = require('keccak');
|
|
|
8
8
|
|
|
9
9
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
10
10
|
|
|
11
|
+
function _interopNamespace(e) {
|
|
12
|
+
if (e && e.__esModule) return e;
|
|
13
|
+
var n = Object.create(null);
|
|
14
|
+
if (e) {
|
|
15
|
+
Object.keys(e).forEach(function (k) {
|
|
16
|
+
if (k !== 'default') {
|
|
17
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
18
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () { return e[k]; }
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
n["default"] = e;
|
|
26
|
+
return Object.freeze(n);
|
|
27
|
+
}
|
|
28
|
+
|
|
11
29
|
var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
|
|
12
30
|
var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
|
|
13
31
|
var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
|
|
@@ -17,12 +35,12 @@ var createKeccakHash__default = /*#__PURE__*/_interopDefaultLegacy(createKeccakH
|
|
|
17
35
|
class BasicInterface {
|
|
18
36
|
#handleDecode() {
|
|
19
37
|
if (!this.decode) throw new Error('bad implementation: needs decode func')
|
|
20
|
-
this.decode()
|
|
38
|
+
return this.decode()
|
|
21
39
|
}
|
|
22
40
|
|
|
23
41
|
#handleEncode() {
|
|
24
42
|
if (!this.encode) throw new Error('bad implementation: needs encode func')
|
|
25
|
-
this.encode()
|
|
43
|
+
return this.encode()
|
|
26
44
|
}
|
|
27
45
|
isHex(string) {
|
|
28
46
|
return isHex__default["default"](string)
|
|
@@ -83,8 +101,8 @@ class BasicInterface {
|
|
|
83
101
|
return this.#handleDecode()
|
|
84
102
|
}
|
|
85
103
|
|
|
86
|
-
toString(encoding = 'utf8') {
|
|
87
|
-
if (!this.encoded) this.#handleEncode();
|
|
104
|
+
async toString(encoding = 'utf8') {
|
|
105
|
+
if (!this.encoded) await this.#handleEncode();
|
|
88
106
|
return this.encoded.toString(encoding)
|
|
89
107
|
}
|
|
90
108
|
|
|
@@ -98,16 +116,16 @@ class BasicInterface {
|
|
|
98
116
|
/**
|
|
99
117
|
* @return {String} encoded
|
|
100
118
|
*/
|
|
101
|
-
toBs32() {
|
|
102
|
-
if (!this.encoded) this.#handleEncode();
|
|
119
|
+
async toBs32() {
|
|
120
|
+
if (!this.encoded) await this.#handleEncode();
|
|
103
121
|
return bs32__default["default"].encode(this.encoded)
|
|
104
122
|
}
|
|
105
123
|
|
|
106
124
|
/**
|
|
107
125
|
* @return {String} encoded
|
|
108
126
|
*/
|
|
109
|
-
toBs58() {
|
|
110
|
-
if (!this.encoded) this.#handleEncode();
|
|
127
|
+
async toBs58() {
|
|
128
|
+
if (!this.encoded) await this.#handleEncode();
|
|
111
129
|
return bs58__default["default"].encode(this.encoded)
|
|
112
130
|
}
|
|
113
131
|
|
|
@@ -129,7 +147,7 @@ class BasicInterface {
|
|
|
129
147
|
}
|
|
130
148
|
|
|
131
149
|
this.decoded = decoded;
|
|
132
|
-
this.encode()
|
|
150
|
+
return this.encode()
|
|
133
151
|
}
|
|
134
152
|
}
|
|
135
153
|
}
|
|
@@ -428,7 +446,39 @@ class CodecHash extends BasicInterface {
|
|
|
428
446
|
}
|
|
429
447
|
}
|
|
430
448
|
|
|
449
|
+
let protons;
|
|
450
|
+
|
|
431
451
|
class FormatInterface extends BasicInterface {
|
|
452
|
+
async #importProtons() {
|
|
453
|
+
let importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('protons')); });
|
|
454
|
+
return importee.default
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
async protoEncode(data) {
|
|
458
|
+
if (!protons) protons = await this.#importProtons();
|
|
459
|
+
return protons(this.proto)[this.messageName].encode(data)
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
async protoDecode(data) {
|
|
463
|
+
if (!protons) protons = await this.#importProtons();
|
|
464
|
+
return protons(this.proto)[this.messageName].decode(data)
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
async #init(buffer) {
|
|
468
|
+
if (buffer instanceof Uint8Array) await this.fromUint8Array(buffer);
|
|
469
|
+
else if (buffer instanceof ArrayBuffer) await this.fromArrayBuffer(buffer);
|
|
470
|
+
else if (buffer?.name === this.name) return buffer
|
|
471
|
+
else if (buffer instanceof String) {
|
|
472
|
+
if (this.isHex(buffer)) await this.fromHex(buffer);
|
|
473
|
+
else if (this.isBase32(buffer)) await this.fromBs32(buffer);
|
|
474
|
+
else if (this.isBase58(buffer)) await this.fromBs58(buffer);
|
|
475
|
+
else throw new Error(`unsupported string ${buffer}`)
|
|
476
|
+
} else {
|
|
477
|
+
await this.create(buffer);
|
|
478
|
+
}
|
|
479
|
+
return this
|
|
480
|
+
}
|
|
481
|
+
|
|
432
482
|
/**
|
|
433
483
|
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
434
484
|
* @param {Object} proto - {encode, decode}
|
|
@@ -436,21 +486,10 @@ class FormatInterface extends BasicInterface {
|
|
|
436
486
|
*/
|
|
437
487
|
constructor(buffer, proto, options = {}) {
|
|
438
488
|
super();
|
|
439
|
-
this.
|
|
440
|
-
this.protoDecode = proto.decode;
|
|
489
|
+
this.proto = proto;
|
|
441
490
|
this.hashFormat = options.hashFormat || 'bs32';
|
|
442
491
|
if (options.name) this.name = options.name;
|
|
443
|
-
|
|
444
|
-
else if (buffer instanceof ArrayBuffer) this.fromArrayBuffer(buffer);
|
|
445
|
-
else if (buffer.name === options.name) return buffer
|
|
446
|
-
else if (buffer instanceof String) {
|
|
447
|
-
if (this.isHex(buffer)) this.fromHex(buffer);
|
|
448
|
-
else if (this.isBase32(buffer)) this.fromBs32(buffer);
|
|
449
|
-
else if (this.isBase58(buffer)) this.fromBs58(buffer);
|
|
450
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
451
|
-
} else {
|
|
452
|
-
this.create(buffer);
|
|
453
|
-
}
|
|
492
|
+
return this.#init(buffer)
|
|
454
493
|
}
|
|
455
494
|
|
|
456
495
|
/**
|
|
@@ -472,22 +511,22 @@ class FormatInterface extends BasicInterface {
|
|
|
472
511
|
/**
|
|
473
512
|
* @return {Object}
|
|
474
513
|
*/
|
|
475
|
-
decode() {
|
|
514
|
+
async decode() {
|
|
476
515
|
let encoded = this.encoded;
|
|
477
516
|
const discoCodec = new PeernetCodec(this.encoded);
|
|
478
517
|
encoded = encoded.slice(discoCodec.codecBuffer.length);
|
|
479
518
|
this.name = discoCodec.name;
|
|
480
|
-
this.decoded = this.protoDecode(encoded);
|
|
519
|
+
this.decoded = await this.protoDecode(encoded);
|
|
481
520
|
return this.decoded
|
|
482
521
|
}
|
|
483
522
|
|
|
484
523
|
/**
|
|
485
524
|
* @return {Buffer}
|
|
486
525
|
*/
|
|
487
|
-
encode(decoded) {
|
|
526
|
+
async encode(decoded) {
|
|
488
527
|
if (!decoded) decoded = this.decoded;
|
|
489
528
|
const codec = new PeernetCodec(this.name);
|
|
490
|
-
const encoded = this.protoEncode(decoded);
|
|
529
|
+
const encoded = await this.protoEncode(decoded);
|
|
491
530
|
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
|
|
492
531
|
uint8Array.set(codec.codecBuffer);
|
|
493
532
|
uint8Array.set(encoded, codec.codecBuffer.length);
|
|
@@ -503,18 +542,16 @@ class FormatInterface extends BasicInterface {
|
|
|
503
542
|
|
|
504
543
|
fromUint8Array(buffer) {
|
|
505
544
|
this.encoded = buffer;
|
|
506
|
-
|
|
545
|
+
return this.hasCodec() ? this.decode() : this.create(
|
|
507
546
|
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
508
|
-
)
|
|
509
|
-
else this.decode();
|
|
547
|
+
)
|
|
510
548
|
}
|
|
511
549
|
|
|
512
550
|
fromArrayBuffer(buffer) {
|
|
513
551
|
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
514
|
-
|
|
552
|
+
return this.hasCodec() ? this.decode() : this.create(
|
|
515
553
|
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
516
|
-
)
|
|
517
|
-
else this.decode();
|
|
554
|
+
)
|
|
518
555
|
}
|
|
519
556
|
}
|
|
520
557
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/codec-format-interface",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "src/index.js",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"@vandeurenglenn/base58": "^1.1.0",
|
|
25
25
|
"@vandeurenglenn/is-hex": "^1.0.0",
|
|
26
26
|
"keccak": "^3.0.2",
|
|
27
|
+
"protons": "^2.0.1",
|
|
27
28
|
"varint": "^6.0.0"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|
package/src/basic-interface.js
CHANGED
|
@@ -5,12 +5,12 @@ import isHex from '@vandeurenglenn/is-hex';
|
|
|
5
5
|
export default class BasicInterface {
|
|
6
6
|
#handleDecode() {
|
|
7
7
|
if (!this.decode) throw new Error('bad implementation: needs decode func')
|
|
8
|
-
this.decode()
|
|
8
|
+
return this.decode()
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
#handleEncode() {
|
|
12
12
|
if (!this.encode) throw new Error('bad implementation: needs encode func')
|
|
13
|
-
this.encode()
|
|
13
|
+
return this.encode()
|
|
14
14
|
}
|
|
15
15
|
isHex(string) {
|
|
16
16
|
return isHex(string)
|
|
@@ -71,8 +71,8 @@ export default class BasicInterface {
|
|
|
71
71
|
return this.#handleDecode()
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
toString(encoding = 'utf8') {
|
|
75
|
-
if (!this.encoded) this.#handleEncode()
|
|
74
|
+
async toString(encoding = 'utf8') {
|
|
75
|
+
if (!this.encoded) await this.#handleEncode()
|
|
76
76
|
return this.encoded.toString(encoding)
|
|
77
77
|
}
|
|
78
78
|
|
|
@@ -86,16 +86,16 @@ export default class BasicInterface {
|
|
|
86
86
|
/**
|
|
87
87
|
* @return {String} encoded
|
|
88
88
|
*/
|
|
89
|
-
toBs32() {
|
|
90
|
-
if (!this.encoded) this.#handleEncode()
|
|
89
|
+
async toBs32() {
|
|
90
|
+
if (!this.encoded) await this.#handleEncode()
|
|
91
91
|
return bs32.encode(this.encoded)
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
95
|
* @return {String} encoded
|
|
96
96
|
*/
|
|
97
|
-
toBs58() {
|
|
98
|
-
if (!this.encoded) this.#handleEncode()
|
|
97
|
+
async toBs58() {
|
|
98
|
+
if (!this.encoded) await this.#handleEncode()
|
|
99
99
|
return bs58.encode(this.encoded)
|
|
100
100
|
}
|
|
101
101
|
|
|
@@ -117,7 +117,7 @@ export default class BasicInterface {
|
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
this.decoded = decoded
|
|
120
|
-
this.encode()
|
|
120
|
+
return this.encode()
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
}
|
|
@@ -1,8 +1,39 @@
|
|
|
1
1
|
import BasicInterface from './basic-interface.js'
|
|
2
2
|
import Codec from './codec.js';
|
|
3
3
|
import Hash from './codec-hash.js'
|
|
4
|
+
let protons
|
|
4
5
|
|
|
5
6
|
export default class FormatInterface extends BasicInterface {
|
|
7
|
+
async #importProtons() {
|
|
8
|
+
let importee = await import('protons')
|
|
9
|
+
return importee.default
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async protoEncode(data) {
|
|
13
|
+
if (!protons) protons = await this.#importProtons()
|
|
14
|
+
return protons(this.proto)[this.messageName].encode(data)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async protoDecode(data) {
|
|
18
|
+
if (!protons) protons = await this.#importProtons()
|
|
19
|
+
return protons(this.proto)[this.messageName].decode(data)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async #init(buffer) {
|
|
23
|
+
if (buffer instanceof Uint8Array) await this.fromUint8Array(buffer)
|
|
24
|
+
else if (buffer instanceof ArrayBuffer) await this.fromArrayBuffer(buffer)
|
|
25
|
+
else if (buffer?.name === this.name) return buffer
|
|
26
|
+
else if (buffer instanceof String) {
|
|
27
|
+
if (this.isHex(buffer)) await this.fromHex(buffer)
|
|
28
|
+
else if (this.isBase32(buffer)) await this.fromBs32(buffer)
|
|
29
|
+
else if (this.isBase58(buffer)) await this.fromBs58(buffer)
|
|
30
|
+
else throw new Error(`unsupported string ${buffer}`)
|
|
31
|
+
} else {
|
|
32
|
+
await this.create(buffer)
|
|
33
|
+
}
|
|
34
|
+
return this
|
|
35
|
+
}
|
|
36
|
+
|
|
6
37
|
/**
|
|
7
38
|
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
8
39
|
* @param {Object} proto - {encode, decode}
|
|
@@ -10,21 +41,10 @@ export default class FormatInterface extends BasicInterface {
|
|
|
10
41
|
*/
|
|
11
42
|
constructor(buffer, proto, options = {}) {
|
|
12
43
|
super()
|
|
13
|
-
this.
|
|
14
|
-
this.protoDecode = proto.decode
|
|
44
|
+
this.proto = proto
|
|
15
45
|
this.hashFormat = options.hashFormat || 'bs32'
|
|
16
46
|
if (options.name) this.name = options.name
|
|
17
|
-
|
|
18
|
-
else if (buffer instanceof ArrayBuffer) this.fromArrayBuffer(buffer)
|
|
19
|
-
else if (buffer.name === options.name) return buffer
|
|
20
|
-
else if (buffer instanceof String) {
|
|
21
|
-
if (this.isHex(buffer)) this.fromHex(buffer)
|
|
22
|
-
else if (this.isBase32(buffer)) this.fromBs32(buffer)
|
|
23
|
-
else if (this.isBase58(buffer)) this.fromBs58(buffer)
|
|
24
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
25
|
-
} else {
|
|
26
|
-
this.create(buffer)
|
|
27
|
-
}
|
|
47
|
+
return this.#init(buffer)
|
|
28
48
|
}
|
|
29
49
|
|
|
30
50
|
/**
|
|
@@ -46,22 +66,22 @@ export default class FormatInterface extends BasicInterface {
|
|
|
46
66
|
/**
|
|
47
67
|
* @return {Object}
|
|
48
68
|
*/
|
|
49
|
-
decode() {
|
|
69
|
+
async decode() {
|
|
50
70
|
let encoded = this.encoded;
|
|
51
71
|
const discoCodec = new Codec(this.encoded)
|
|
52
72
|
encoded = encoded.slice(discoCodec.codecBuffer.length)
|
|
53
73
|
this.name = discoCodec.name
|
|
54
|
-
this.decoded = this.protoDecode(encoded)
|
|
74
|
+
this.decoded = await this.protoDecode(encoded)
|
|
55
75
|
return this.decoded
|
|
56
76
|
}
|
|
57
77
|
|
|
58
78
|
/**
|
|
59
79
|
* @return {Buffer}
|
|
60
80
|
*/
|
|
61
|
-
encode(decoded) {
|
|
81
|
+
async encode(decoded) {
|
|
62
82
|
if (!decoded) decoded = this.decoded;
|
|
63
83
|
const codec = new Codec(this.name)
|
|
64
|
-
const encoded = this.protoEncode(decoded)
|
|
84
|
+
const encoded = await this.protoEncode(decoded)
|
|
65
85
|
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length)
|
|
66
86
|
uint8Array.set(codec.codecBuffer)
|
|
67
87
|
uint8Array.set(encoded, codec.codecBuffer.length)
|
|
@@ -77,17 +97,15 @@ export default class FormatInterface extends BasicInterface {
|
|
|
77
97
|
|
|
78
98
|
fromUint8Array(buffer) {
|
|
79
99
|
this.encoded = buffer
|
|
80
|
-
|
|
100
|
+
return this.hasCodec() ? this.decode() : this.create(
|
|
81
101
|
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
82
102
|
)
|
|
83
|
-
else this.decode()
|
|
84
103
|
}
|
|
85
104
|
|
|
86
105
|
fromArrayBuffer(buffer) {
|
|
87
106
|
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
|
|
88
|
-
|
|
107
|
+
return this.hasCodec() ? this.decode() : this.create(
|
|
89
108
|
JSON.parse(new TextDecoder().decode(this.encoded))
|
|
90
109
|
)
|
|
91
|
-
else this.decode()
|
|
92
110
|
}
|
|
93
111
|
}
|
package/test/test.js
CHANGED
|
@@ -1,9 +1,26 @@
|
|
|
1
1
|
const test = require('tape');
|
|
2
2
|
const {FormatInterface} = require('./../dist/index')
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
globalThis.peernet = {codecs: {}}
|
|
5
|
+
class FormatTest extends FormatInterface {
|
|
6
|
+
get keys() { return ['somedata'] }
|
|
7
|
+
get messageName() { return 'Message' }
|
|
8
|
+
|
|
9
|
+
constructor(data) {
|
|
10
|
+
super(data, `message Message {
|
|
11
|
+
required string somedata = 1;
|
|
12
|
+
}`, {name: 'disco-hash'})
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const encoded = [48, 10, 5, 104, 101, 108, 108, 111]
|
|
17
|
+
|
|
18
|
+
test('format', async (tape) => {
|
|
19
|
+
tape.plan(2)
|
|
20
|
+
const message = await new FormatTest({somedata: 'hello'})
|
|
21
|
+
await message.encode()
|
|
22
|
+
const m2 = await new FormatTest(message.encoded)
|
|
23
|
+
await m2.decode()
|
|
24
|
+
tape.ok(message.encoded, 'can encode')
|
|
25
|
+
tape.ok(m2.decoded.somedata === 'hello', 'can decode')
|
|
9
26
|
})
|