@leofcoin/codec-format-interface 1.0.1 → 1.2.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 CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
3
5
  var bs32 = require('@vandeurenglenn/base32');
4
6
  var bs58 = require('@vandeurenglenn/base58');
5
7
  var isHex = require('@vandeurenglenn/is-hex');
@@ -8,6 +10,24 @@ var createKeccakHash = require('keccak');
8
10
 
9
11
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
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
+
11
31
  var bs32__default = /*#__PURE__*/_interopDefaultLegacy(bs32);
12
32
  var bs58__default = /*#__PURE__*/_interopDefaultLegacy(bs58);
13
33
  var isHex__default = /*#__PURE__*/_interopDefaultLegacy(isHex);
@@ -17,12 +37,12 @@ var createKeccakHash__default = /*#__PURE__*/_interopDefaultLegacy(createKeccakH
17
37
  class BasicInterface {
18
38
  #handleDecode() {
19
39
  if (!this.decode) throw new Error('bad implementation: needs decode func')
20
- this.decode();
40
+ return this.decode()
21
41
  }
22
42
 
23
43
  #handleEncode() {
24
44
  if (!this.encode) throw new Error('bad implementation: needs encode func')
25
- this.encode();
45
+ return this.encode()
26
46
  }
27
47
  isHex(string) {
28
48
  return isHex__default["default"](string)
@@ -83,8 +103,8 @@ class BasicInterface {
83
103
  return this.#handleDecode()
84
104
  }
85
105
 
86
- toString(encoding = 'utf8') {
87
- if (!this.encoded) this.#handleEncode();
106
+ async toString(encoding = 'utf8') {
107
+ if (!this.encoded) await this.#handleEncode();
88
108
  return this.encoded.toString(encoding)
89
109
  }
90
110
 
@@ -98,16 +118,16 @@ class BasicInterface {
98
118
  /**
99
119
  * @return {String} encoded
100
120
  */
101
- toBs32() {
102
- if (!this.encoded) this.#handleEncode();
121
+ async toBs32() {
122
+ if (!this.encoded) await this.#handleEncode();
103
123
  return bs32__default["default"].encode(this.encoded)
104
124
  }
105
125
 
106
126
  /**
107
127
  * @return {String} encoded
108
128
  */
109
- toBs58() {
110
- if (!this.encoded) this.#handleEncode();
129
+ async toBs58() {
130
+ if (!this.encoded) await this.#handleEncode();
111
131
  return bs58__default["default"].encode(this.encoded)
112
132
  }
113
133
 
@@ -129,11 +149,16 @@ class BasicInterface {
129
149
  }
130
150
 
131
151
  this.decoded = decoded;
132
- this.encode();
152
+ return this.encode()
133
153
  }
134
154
  }
135
155
  }
136
156
 
157
+ var basicInterface = /*#__PURE__*/Object.freeze({
158
+ __proto__: null,
159
+ 'default': BasicInterface
160
+ });
161
+
137
162
  var codecs = {
138
163
  // just a hash
139
164
  'disco-hash': {
@@ -214,6 +239,11 @@ var codecs = {
214
239
  },
215
240
  };
216
241
 
242
+ var codecs$1 = /*#__PURE__*/Object.freeze({
243
+ __proto__: null,
244
+ 'default': codecs
245
+ });
246
+
217
247
  class PeernetCodec extends BasicInterface {
218
248
  get codecs() {
219
249
  return {...globalThis.peernet.codecs, ...codecs}
@@ -305,6 +335,11 @@ class PeernetCodec extends BasicInterface {
305
335
  }
306
336
  }
307
337
 
338
+ var codec = /*#__PURE__*/Object.freeze({
339
+ __proto__: null,
340
+ 'default': PeernetCodec
341
+ });
342
+
308
343
  class CodecHash extends BasicInterface {
309
344
  constructor(buffer, options = {}) {
310
345
  super();
@@ -428,7 +463,44 @@ class CodecHash extends BasicInterface {
428
463
  }
429
464
  }
430
465
 
466
+ var codecHash = /*#__PURE__*/Object.freeze({
467
+ __proto__: null,
468
+ 'default': CodecHash
469
+ });
470
+
471
+ let protons;
472
+
431
473
  class FormatInterface extends BasicInterface {
474
+ async #importProtons() {
475
+ let importee = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(/* webpackChunkName: "protons" */ 'protons')); });
476
+ return importee.default
477
+ }
478
+
479
+ async protoEncode(data) {
480
+ if (!protons) protons = await this.#importProtons();
481
+ return protons(this.proto)[this.messageName].encode(data)
482
+ }
483
+
484
+ async protoDecode(data) {
485
+ if (!protons) protons = await this.#importProtons();
486
+ return protons(this.proto)[this.messageName].decode(data)
487
+ }
488
+
489
+ async #init(buffer) {
490
+ if (buffer instanceof Uint8Array) await this.fromUint8Array(buffer);
491
+ else if (buffer instanceof ArrayBuffer) await this.fromArrayBuffer(buffer);
492
+ else if (buffer?.name === this.name) return buffer
493
+ else if (buffer instanceof String) {
494
+ if (this.isHex(buffer)) await this.fromHex(buffer);
495
+ else if (this.isBase32(buffer)) await this.fromBs32(buffer);
496
+ else if (this.isBase58(buffer)) await this.fromBs58(buffer);
497
+ else throw new Error(`unsupported string ${buffer}`)
498
+ } else {
499
+ await this.create(buffer);
500
+ }
501
+ return this
502
+ }
503
+
432
504
  /**
433
505
  * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
434
506
  * @param {Object} proto - {encode, decode}
@@ -436,21 +508,10 @@ class FormatInterface extends BasicInterface {
436
508
  */
437
509
  constructor(buffer, proto, options = {}) {
438
510
  super();
439
- this.protoEncode = proto.encode;
440
- this.protoDecode = proto.decode;
511
+ this.proto = proto;
441
512
  this.hashFormat = options.hashFormat || 'bs32';
442
513
  if (options.name) this.name = options.name;
443
- if (buffer instanceof Uint8Array) this.fromUint8Array(buffer);
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
- }
514
+ return this.#init(buffer)
454
515
  }
455
516
 
456
517
  /**
@@ -472,22 +533,22 @@ class FormatInterface extends BasicInterface {
472
533
  /**
473
534
  * @return {Object}
474
535
  */
475
- decode() {
536
+ async decode() {
476
537
  let encoded = this.encoded;
477
538
  const discoCodec = new PeernetCodec(this.encoded);
478
539
  encoded = encoded.slice(discoCodec.codecBuffer.length);
479
540
  this.name = discoCodec.name;
480
- this.decoded = this.protoDecode(encoded);
541
+ this.decoded = await this.protoDecode(encoded);
481
542
  return this.decoded
482
543
  }
483
544
 
484
545
  /**
485
546
  * @return {Buffer}
486
547
  */
487
- encode(decoded) {
548
+ async encode(decoded) {
488
549
  if (!decoded) decoded = this.decoded;
489
550
  const codec = new PeernetCodec(this.name);
490
- const encoded = this.protoEncode(decoded);
551
+ const encoded = await this.protoEncode(decoded);
491
552
  const uint8Array = new Uint8Array(encoded.length + codec.codecBuffer.length);
492
553
  uint8Array.set(codec.codecBuffer);
493
554
  uint8Array.set(encoded, codec.codecBuffer.length);
@@ -503,21 +564,26 @@ class FormatInterface extends BasicInterface {
503
564
 
504
565
  fromUint8Array(buffer) {
505
566
  this.encoded = buffer;
506
- if (!this.hasCodec()) this.create(
567
+ return this.hasCodec() ? this.decode() : this.create(
507
568
  JSON.parse(new TextDecoder().decode(this.encoded))
508
- );
509
- else this.decode();
569
+ )
510
570
  }
511
571
 
512
572
  fromArrayBuffer(buffer) {
513
573
  this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
514
- if (!this.hasCodec()) this.create(
574
+ return this.hasCodec() ? this.decode() : this.create(
515
575
  JSON.parse(new TextDecoder().decode(this.encoded))
516
- );
517
- else this.decode();
576
+ )
518
577
  }
519
578
  }
520
579
 
521
- var index = { codecs, Codec: PeernetCodec, CodecHash, FormatInterface, BasicInterface };
580
+ var codecFormatInterface = /*#__PURE__*/Object.freeze({
581
+ __proto__: null,
582
+ 'default': FormatInterface
583
+ });
522
584
 
523
- module.exports = index;
585
+ exports.BasicInterface = basicInterface;
586
+ exports.Codec = codec;
587
+ exports.CodecHash = codecHash;
588
+ exports.FormatInterface = codecFormatInterface;
589
+ exports.codecs = codecs$1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/codec-format-interface",
3
- "version": "1.0.1",
3
+ "version": "1.2.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": {
@@ -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(/* webpackChunkName: "protons" */ '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.protoEncode = proto.encode
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
- if (buffer instanceof Uint8Array) this.fromUint8Array(buffer)
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
- if (!this.hasCodec()) this.create(
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
- if (!this.hasCodec()) this.create(
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/src/index.js CHANGED
@@ -1,7 +1,5 @@
1
- import BasicInterface from './basic-interface.js'
2
- import FormatInterface from './codec-format-interface.js'
3
- import CodecHash from './codec-hash.js'
4
- import Codec from './codec.js'
5
- import codecs from './codecs.js'
6
-
7
- export default { codecs, Codec, CodecHash, FormatInterface, BasicInterface }
1
+ export * as BasicInterface from './basic-interface.js'
2
+ export * as FormatInterface from './codec-format-interface.js'
3
+ export * as CodecHash from './codec-hash.js'
4
+ export * as Codec from './codec.js'
5
+ export * as codecs from './codecs.js'
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
- test('', tape => {
5
- tape.plan(1)
6
- const int = new FormatInterface()
7
- console.log(int);
8
- tape.plan('ok')
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
  })