@leofcoin/codec-format-interface 1.2.6 → 1.3.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 +36 -49
- package/dist/module.js +4836 -0
- package/package.json +2 -3
- package/rollup.config.js +12 -0
- package/src/basic-interface.js +11 -11
- package/src/codec-format-interface.js +12 -12
- package/src/index.js +11 -5
- /package/test/{test.js → index.js} +0 -0
package/dist/index.js
CHANGED
|
@@ -10,37 +10,19 @@ var createKeccakHash = require('keccak');
|
|
|
10
10
|
|
|
11
11
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
12
|
|
|
13
|
-
function _interopNamespace(e) {
|
|
14
|
-
if (e && e.__esModule) return e;
|
|
15
|
-
var n = Object.create(null);
|
|
16
|
-
if (e) {
|
|
17
|
-
Object.keys(e).forEach(function (k) {
|
|
18
|
-
if (k !== 'default') {
|
|
19
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
20
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
get: function () { return e[k]; }
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
n["default"] = e;
|
|
28
|
-
return Object.freeze(n);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
13
|
var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
|
|
32
14
|
var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
|
|
33
15
|
var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
|
|
34
16
|
var varint__default = /*#__PURE__*/_interopDefaultLegacy(varint);
|
|
35
17
|
var createKeccakHash__default = /*#__PURE__*/_interopDefaultLegacy(createKeccakHash);
|
|
36
18
|
|
|
37
|
-
class BasicInterface {
|
|
38
|
-
|
|
19
|
+
class BasicInterface$1 {
|
|
20
|
+
handleDecode() {
|
|
39
21
|
if (!this.decode) throw new Error('bad implementation: needs decode func')
|
|
40
22
|
return this.decode()
|
|
41
23
|
}
|
|
42
24
|
|
|
43
|
-
|
|
25
|
+
handleEncode() {
|
|
44
26
|
if (!this.encode) throw new Error('bad implementation: needs encode func')
|
|
45
27
|
return this.encode()
|
|
46
28
|
}
|
|
@@ -58,7 +40,7 @@ class BasicInterface {
|
|
|
58
40
|
*/
|
|
59
41
|
fromBs32(encoded) {
|
|
60
42
|
this.encoded = bs32__default["default"].decode(encoded);
|
|
61
|
-
return this
|
|
43
|
+
return this.handleDecode()
|
|
62
44
|
}
|
|
63
45
|
|
|
64
46
|
/**
|
|
@@ -66,7 +48,7 @@ class BasicInterface {
|
|
|
66
48
|
*/
|
|
67
49
|
fromBs58(encoded) {
|
|
68
50
|
this.encoded = bs58__default["default"].decode(encoded);
|
|
69
|
-
return this
|
|
51
|
+
return this.handleDecode()
|
|
70
52
|
}
|
|
71
53
|
|
|
72
54
|
async toArray() {
|
|
@@ -79,12 +61,12 @@ class BasicInterface {
|
|
|
79
61
|
|
|
80
62
|
fromString(string) {
|
|
81
63
|
this.encoded = new Uint8Array(string.split(','));
|
|
82
|
-
return this
|
|
64
|
+
return this.handleDecode()
|
|
83
65
|
}
|
|
84
66
|
|
|
85
67
|
fromArray(array) {
|
|
86
68
|
this.encoded = new Uint8Array([...array]);
|
|
87
|
-
return this
|
|
69
|
+
return this.handleDecode()
|
|
88
70
|
}
|
|
89
71
|
|
|
90
72
|
/**
|
|
@@ -92,7 +74,7 @@ class BasicInterface {
|
|
|
92
74
|
*/
|
|
93
75
|
fromEncoded(encoded) {
|
|
94
76
|
this.encoded = encoded;
|
|
95
|
-
return this
|
|
77
|
+
return this.handleDecode()
|
|
96
78
|
}
|
|
97
79
|
|
|
98
80
|
/**
|
|
@@ -100,11 +82,11 @@ class BasicInterface {
|
|
|
100
82
|
*/
|
|
101
83
|
fromHex(encoded) {
|
|
102
84
|
this.encoded = Buffer.from(encoded, 'hex');
|
|
103
|
-
return this
|
|
85
|
+
return this.handleDecode()
|
|
104
86
|
}
|
|
105
87
|
|
|
106
88
|
async toString(encoding = 'utf8') {
|
|
107
|
-
if (!this.encoded) await this
|
|
89
|
+
if (!this.encoded) await this.handleEncode();
|
|
108
90
|
return this.encoded.toString(encoding)
|
|
109
91
|
}
|
|
110
92
|
|
|
@@ -119,7 +101,7 @@ class BasicInterface {
|
|
|
119
101
|
* @return {String} encoded
|
|
120
102
|
*/
|
|
121
103
|
async toBs32() {
|
|
122
|
-
if (!this.encoded) await this
|
|
104
|
+
if (!this.encoded) await this.handleEncode();
|
|
123
105
|
return bs32__default["default"].encode(this.encoded)
|
|
124
106
|
}
|
|
125
107
|
|
|
@@ -127,7 +109,7 @@ class BasicInterface {
|
|
|
127
109
|
* @return {String} encoded
|
|
128
110
|
*/
|
|
129
111
|
async toBs58() {
|
|
130
|
-
if (!this.encoded) await this
|
|
112
|
+
if (!this.encoded) await this.handleEncode();
|
|
131
113
|
return bs58__default["default"].encode(this.encoded)
|
|
132
114
|
}
|
|
133
115
|
|
|
@@ -242,7 +224,7 @@ var codecs = {
|
|
|
242
224
|
}
|
|
243
225
|
};
|
|
244
226
|
|
|
245
|
-
class PeernetCodec extends BasicInterface {
|
|
227
|
+
class PeernetCodec extends BasicInterface$1 {
|
|
246
228
|
get codecs() {
|
|
247
229
|
return {...globalThis.peernet.codecs, ...codecs}
|
|
248
230
|
}
|
|
@@ -333,7 +315,7 @@ class PeernetCodec extends BasicInterface {
|
|
|
333
315
|
}
|
|
334
316
|
}
|
|
335
317
|
|
|
336
|
-
class CodecHash extends BasicInterface {
|
|
318
|
+
class CodecHash$1 extends BasicInterface$1 {
|
|
337
319
|
constructor(buffer, options = {}) {
|
|
338
320
|
super();
|
|
339
321
|
if (options.name) this.name = options.name;
|
|
@@ -456,25 +438,19 @@ class CodecHash extends BasicInterface {
|
|
|
456
438
|
}
|
|
457
439
|
}
|
|
458
440
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
class FormatInterface extends BasicInterface {
|
|
462
|
-
async #importProtons() {
|
|
463
|
-
let importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(/* webpackChunkName: "protons" */ 'protons')); });
|
|
464
|
-
return importee.default
|
|
465
|
-
}
|
|
441
|
+
class FormatInterface$1 extends BasicInterface$1 {
|
|
466
442
|
|
|
467
443
|
async protoEncode(data) {
|
|
468
|
-
|
|
469
|
-
return
|
|
444
|
+
// check schema
|
|
445
|
+
return new TextEncoder().encode(data)
|
|
470
446
|
}
|
|
471
447
|
|
|
472
448
|
async protoDecode(data) {
|
|
473
|
-
|
|
474
|
-
return
|
|
449
|
+
// check schema
|
|
450
|
+
return new TextDecoder().decode(data)
|
|
475
451
|
}
|
|
476
452
|
|
|
477
|
-
async
|
|
453
|
+
async init(buffer) {
|
|
478
454
|
if (buffer instanceof Uint8Array) await this.fromUint8Array(buffer);
|
|
479
455
|
else if (buffer instanceof ArrayBuffer) await this.fromArrayBuffer(buffer);
|
|
480
456
|
else if (buffer?.name === this.name) return buffer
|
|
@@ -499,14 +475,14 @@ class FormatInterface extends BasicInterface {
|
|
|
499
475
|
this.proto = proto;
|
|
500
476
|
this.hashFormat = options.hashFormat || 'bs32';
|
|
501
477
|
if (options.name) this.name = options.name;
|
|
502
|
-
return this
|
|
478
|
+
return this.init(buffer)
|
|
503
479
|
}
|
|
504
480
|
|
|
505
481
|
/**
|
|
506
482
|
* @return {PeernetHash}
|
|
507
483
|
*/
|
|
508
484
|
get peernetHash() {
|
|
509
|
-
return new CodecHash(this.decoded, {name: this.name})
|
|
485
|
+
return new CodecHash$1(this.decoded, {name: this.name})
|
|
510
486
|
}
|
|
511
487
|
|
|
512
488
|
/**
|
|
@@ -527,6 +503,11 @@ class FormatInterface extends BasicInterface {
|
|
|
527
503
|
encoded = encoded.slice(discoCodec.codecBuffer.length);
|
|
528
504
|
this.name = discoCodec.name;
|
|
529
505
|
this.decoded = await this.protoDecode(encoded);
|
|
506
|
+
try {
|
|
507
|
+
this.decoded = JSON.parse(this.decoded);
|
|
508
|
+
} catch {
|
|
509
|
+
|
|
510
|
+
}
|
|
530
511
|
return this.decoded
|
|
531
512
|
}
|
|
532
513
|
|
|
@@ -536,7 +517,7 @@ class FormatInterface extends BasicInterface {
|
|
|
536
517
|
async encode(decoded) {
|
|
537
518
|
if (!decoded) decoded = this.decoded;
|
|
538
519
|
const codec = new PeernetCodec(this.name);
|
|
539
|
-
const encoded = await this.protoEncode(decoded);
|
|
520
|
+
const encoded = await this.protoEncode(typeof decoded === 'object' ? JSON.stringify(decoded) : decoded);
|
|
540
521
|
const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
|
|
541
522
|
uint8Array.set(codec.codecBuffer);
|
|
542
523
|
uint8Array.set(encoded, codec.codecBuffer.length);
|
|
@@ -565,8 +546,14 @@ class FormatInterface extends BasicInterface {
|
|
|
565
546
|
}
|
|
566
547
|
}
|
|
567
548
|
|
|
549
|
+
const BasicInterface = BasicInterface$1;
|
|
550
|
+
const FormatInterface = FormatInterface$1;
|
|
551
|
+
const CodecHash = CodecHash$1;
|
|
552
|
+
const Codec = PeernetCodec;
|
|
553
|
+
const Codecs = codecs;
|
|
554
|
+
|
|
568
555
|
exports.BasicInterface = BasicInterface;
|
|
569
|
-
exports.Codec =
|
|
556
|
+
exports.Codec = Codec;
|
|
570
557
|
exports.CodecHash = CodecHash;
|
|
558
|
+
exports.Codecs = Codecs;
|
|
571
559
|
exports.FormatInterface = FormatInterface;
|
|
572
|
-
exports.codecs = codecs;
|