@leofcoin/codec-format-interface 1.6.1 → 1.6.2

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.
@@ -1,20 +1,25 @@
1
1
  export default class BasicInterface {
2
+ #private;
2
3
  encoded: Uint8Array;
3
- decoded: object | string;
4
+ decoded: object;
4
5
  keys: string[];
5
6
  name: string;
7
+ set proto(value: object);
8
+ get proto(): object;
9
+ decode(encoded?: Uint8Array): Object;
10
+ encode(decoded?: object): Uint8Array;
6
11
  protoEncode(data: object): Uint8Array;
7
12
  protoDecode(data: Uint8Array): object;
8
- isHex(string: any): any;
9
- isBase32(string: any): boolean;
10
- isBase58(string: any): boolean;
11
- fromBs32(encoded: base58String): object;
12
- fromBs58(encoded: any): object;
13
+ isHex(string: string): boolean;
14
+ isBase32(string: string): boolean;
15
+ isBase58(string: string): boolean;
16
+ fromBs32(encoded: string): object;
17
+ fromBs58(encoded: base58String): object;
13
18
  toArray(): Promise<number[]>;
14
19
  fromString(string: string): object;
15
- fromHex(string: any): any;
20
+ fromHex(string: string): object;
16
21
  fromArray(array: number[]): object;
17
- fromEncoded(encoded: Uint8Array): any;
22
+ fromEncoded(encoded: Uint8Array): Object;
18
23
  toString(): string;
19
24
  toHex(): string;
20
25
  /**
@@ -24,9 +29,9 @@ export default class BasicInterface {
24
29
  /**
25
30
  * @return {String} encoded
26
31
  */
27
- toBs58(): string;
32
+ toBs58(): base58String;
28
33
  /**
29
34
  * @param {Object} data
30
35
  */
31
- create(data: object): any;
36
+ create(data: object): Uint8Array;
32
37
  }
@@ -1,18 +1,20 @@
1
1
  import BasicInterface from './basic-interface.js';
2
2
  import Hash from './codec-hash.js';
3
3
  export default class FormatInterface extends BasicInterface implements FormatInterface {
4
- set proto(value: any);
5
- get proto(): any;
6
- init(buffer: any): any;
4
+ hashFormat: string;
5
+ init(buffer: Uint8Array | ArrayBuffer | FormatInterface | string): FormatInterface;
7
6
  hasCodec(): boolean;
8
- decode(): string | object;
7
+ decode(encoded?: Uint8Array): object;
9
8
  encode(decoded?: object): Uint8Array;
10
9
  /**
11
10
  * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
12
11
  * @param {Object} proto - {protoObject}
13
12
  * @param {Object} options - {hashFormat, name}
14
13
  */
15
- constructor(buffer: any, proto: any, options?: {});
14
+ constructor(buffer: any, proto: any, options?: {
15
+ hashFormat?: string;
16
+ name?: string;
17
+ });
16
18
  /**
17
19
  * @return {PeernetHash}
18
20
  */
@@ -21,6 +23,6 @@ export default class FormatInterface extends BasicInterface implements FormatInt
21
23
  * @return {peernetHash}
22
24
  */
23
25
  hash(): Promise<any>;
24
- fromUint8Array(buffer: any): any;
25
- fromArrayBuffer(buffer: any): any;
26
+ fromUint8Array(buffer: any): object;
27
+ fromArrayBuffer(buffer: any): object;
26
28
  }
@@ -5,20 +5,20 @@ export default class CodecHash extends BasicInterface {
5
5
  init(uint8Array: any): Promise<CodecHash>;
6
6
  discoCodec: Codec;
7
7
  get prefix(): Uint8Array;
8
- get length(): any;
8
+ get length(): number[];
9
9
  get buffer(): Uint8Array;
10
10
  get hash(): Uint8Array;
11
11
  fromJSON(json: any): Promise<Uint8Array>;
12
12
  encode(buffer: any, name: any): Promise<Uint8Array>;
13
13
  digest: any;
14
14
  size: any;
15
- codec: any;
15
+ codec: Uint8Array;
16
16
  validate(buffer: any): Promise<void>;
17
17
  decode(buffer: any): {
18
- codec: any;
18
+ codec: Uint8Array;
19
19
  name: any;
20
20
  size: any;
21
- length: any;
21
+ length: number[];
22
22
  digest: any;
23
23
  };
24
24
  }
package/dist/codec.d.ts CHANGED
@@ -1,13 +1,16 @@
1
1
  import BasicInterface from './basic-interface.js';
2
2
  export default class Codec extends BasicInterface {
3
+ codecBuffer: Uint8Array;
4
+ codec: number;
5
+ hashAlg: string;
3
6
  get codecs(): any;
4
- constructor(buffer: any);
5
- fromEncoded(encoded: any): void;
6
- getCodec(name: any): any;
7
- getCodecName(codec: any): string;
8
- getHashAlg(name: any): any;
9
- fromCodec(codec: any): void;
10
- fromName(name: any): void;
11
- decode(): void;
12
- encode(): Uint8Array;
7
+ constructor(buffer: string | number | object | Uint8Array | ArrayBuffer);
8
+ fromEncoded(encoded: Uint8Array): object;
9
+ getCodec(name: string): number;
10
+ getCodecName(codec: number): string;
11
+ getHashAlg(name: string): string;
12
+ fromCodec(codec: number): void;
13
+ fromName(name: string): void;
14
+ decode(encoded?: Uint8Array): object;
15
+ encode(codec?: number): Uint8Array;
13
16
  }
package/dist/index.js CHANGED
@@ -15,6 +15,22 @@ let BasicInterface$1 = class BasicInterface {
15
15
  decoded;
16
16
  keys;
17
17
  name;
18
+ #proto;
19
+ set proto(value) {
20
+ this.#proto = value;
21
+ this.keys = Object.keys(value);
22
+ }
23
+ get proto() {
24
+ return this.#proto;
25
+ }
26
+ decode(encoded) {
27
+ encoded = encoded || this.encoded;
28
+ return new Object();
29
+ }
30
+ encode(decoded) {
31
+ decoded = decoded || this.decoded;
32
+ return new Uint8Array();
33
+ }
18
34
  // get Codec(): Codec {}
19
35
  protoEncode(data) {
20
36
  // check schema
@@ -34,12 +50,10 @@ let BasicInterface$1 = class BasicInterface {
34
50
  return base58.isBase58(string);
35
51
  }
36
52
  fromBs32(encoded) {
37
- this.encoded = bs32.decode(encoded);
38
- return this.decode();
53
+ return this.decode(bs32.decode(encoded));
39
54
  }
40
55
  fromBs58(encoded) {
41
- this.encoded = fromBase58(encoded);
42
- return this.decode();
56
+ return this.decode(fromBase58(encoded));
43
57
  }
44
58
  async toArray() {
45
59
  const array = [];
@@ -51,20 +65,16 @@ let BasicInterface$1 = class BasicInterface {
51
65
  fromString(string) {
52
66
  const array = string.split(',');
53
67
  const arrayLike = array.map(string => Number(string));
54
- this.encoded = Uint8Array.from(arrayLike);
55
- return this.decode();
68
+ return this.decode(Uint8Array.from(arrayLike));
56
69
  }
57
70
  fromHex(string) {
58
- this.encoded = fromHex(string);
59
- return this.decode();
71
+ return this.decode(fromHex(string));
60
72
  }
61
73
  fromArray(array) {
62
- this.encoded = Uint8Array.from([...array]);
63
- return this.decode();
74
+ return this.decode(Uint8Array.from([...array]));
64
75
  }
65
76
  fromEncoded(encoded) {
66
- this.encoded = encoded;
67
- return this.decode();
77
+ return this.decode(encoded);
68
78
  }
69
79
  toString() {
70
80
  if (!this.encoded)
@@ -74,7 +84,7 @@ let BasicInterface$1 = class BasicInterface {
74
84
  toHex() {
75
85
  if (!this.encoded)
76
86
  this.encode();
77
- return toHex(this.encoded);
87
+ return toHex(this.encoded.toString().split(',').map(number => Number(number)));
78
88
  }
79
89
  /**
80
90
  * @return {String} encoded
@@ -109,7 +119,7 @@ let BasicInterface$1 = class BasicInterface {
109
119
  });
110
120
  }
111
121
  this.decoded = decoded;
112
- return this.encode();
122
+ return this.encode(decoded);
113
123
  }
114
124
  }
115
125
  };
@@ -208,6 +218,9 @@ var codecs$1 = /*#__PURE__*/Object.freeze({
208
218
  });
209
219
 
210
220
  let Codec$1 = class Codec extends BasicInterface$1 {
221
+ codecBuffer;
222
+ codec;
223
+ hashAlg;
211
224
  get codecs() {
212
225
  return { ...globalThis.peernet.codecs, ...codecs };
213
226
  }
@@ -227,16 +240,17 @@ let Codec$1 = class Codec extends BasicInterface$1 {
227
240
  }
228
241
  }
229
242
  else if (buffer instanceof ArrayBuffer) {
230
- const encoded = new Uint8Array(buffer.byteLength);
231
- for (let i = 0; i < buffer.byteLength; i++) {
232
- encoded[i] = buffer[i];
243
+ const codec = varint.decode(buffer);
244
+ const name = this.getCodecName(codec);
245
+ if (name) {
246
+ this.name = name;
247
+ this.decode(buffer);
248
+ }
249
+ else {
250
+ this.encode(buffer);
233
251
  }
234
- this.encoded = encoded;
235
- // this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength)
236
- this.decode(buffer);
237
- return;
238
252
  }
239
- if (typeof buffer === 'string') {
253
+ else if (typeof buffer === 'string') {
240
254
  if (this.codecs[buffer])
241
255
  this.fromName(buffer);
242
256
  else if (this.isHex(buffer))
@@ -258,7 +272,7 @@ let Codec$1 = class Codec extends BasicInterface$1 {
258
272
  const name = this.getCodecName(codec);
259
273
  this.name = name;
260
274
  this.encoded = encoded;
261
- this.decode(encoded);
275
+ return this.decode(encoded);
262
276
  }
263
277
  getCodec(name) {
264
278
  return this.codecs[name].codec;
@@ -288,13 +302,15 @@ let Codec$1 = class Codec extends BasicInterface$1 {
288
302
  this.hashAlg = this.getHashAlg(name);
289
303
  this.codecBuffer = varint.encode(codec);
290
304
  }
291
- decode() {
292
- const codec = varint.decode(this.encoded);
305
+ decode(encoded) {
306
+ encoded = encoded || this.encoded;
307
+ const codec = varint.decode(encoded);
293
308
  this.fromCodec(codec);
309
+ return this.decoded;
294
310
  }
295
- encode() {
296
- const codec = varint.encode(this.decoded);
297
- this.encoded = codec;
311
+ encode(codec) {
312
+ codec = codec || this.codec;
313
+ this.encoded = varint.encode(codec);
298
314
  return this.encoded;
299
315
  }
300
316
  };
@@ -325,7 +341,7 @@ let CodecHash$1 = class CodecHash extends BasicInterface$1 {
325
341
  }
326
342
  if (typeof uint8Array === 'string') {
327
343
  if (this.isHex(uint8Array))
328
- await this.fromHex(uint8Array);
344
+ await this.fromHex({ string: uint8Array });
329
345
  if (this.isBase32(uint8Array))
330
346
  await this.fromBs32(uint8Array);
331
347
  else if (this.isBase58(uint8Array))
@@ -398,7 +414,7 @@ let CodecHash$1 = class CodecHash extends BasicInterface$1 {
398
414
  }
399
415
  if (typeof buffer === 'string') {
400
416
  if (this.isHex(buffer))
401
- this.fromHex(buffer);
417
+ this.fromHex({ string: buffer });
402
418
  if (this.isBase32(buffer))
403
419
  this.fromBs32(buffer);
404
420
  }
@@ -430,19 +446,13 @@ let CodecHash$1 = class CodecHash extends BasicInterface$1 {
430
446
  };
431
447
 
432
448
  let FormatInterface$1 = class FormatInterface extends BasicInterface$1 {
433
- set proto(value) {
434
- this._proto = value;
435
- this.keys = Object.keys(this._proto);
436
- }
437
- get proto() {
438
- return this._proto;
439
- }
449
+ hashFormat;
440
450
  init(buffer) {
441
451
  if (buffer instanceof Uint8Array)
442
452
  this.fromUint8Array(buffer);
443
453
  else if (buffer instanceof ArrayBuffer)
444
454
  this.fromArrayBuffer(buffer);
445
- else if (buffer?.name === this.name)
455
+ else if (buffer instanceof FormatInterface$1 && buffer?.name === this.name)
446
456
  return buffer;
447
457
  else if (buffer instanceof String) {
448
458
  if (this.isHex(buffer))
@@ -466,18 +476,17 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface$1 {
466
476
  if (codec.name)
467
477
  return true;
468
478
  }
469
- decode() {
470
- let encoded = this.encoded;
479
+ decode(encoded) {
480
+ encoded = encoded || this.encoded;
471
481
  const codec = new Codec$1(this.encoded);
472
482
  if (codec.codecBuffer) {
473
483
  encoded = encoded.slice(codec.codecBuffer.length);
474
484
  this.name = codec.name;
475
485
  this.decoded = this.protoDecode(encoded);
476
- try {
477
- this.decoded = JSON.parse(this.decoded);
478
- }
479
- catch {
480
- }
486
+ // try {
487
+ // this.decoded = JSON.parse(this.decoded)
488
+ // } catch {
489
+ // }
481
490
  }
482
491
  else {
483
492
  throw new Error(`no codec found`);
@@ -509,11 +518,11 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface$1 {
509
518
  * @param {Object} proto - {protoObject}
510
519
  * @param {Object} options - {hashFormat, name}
511
520
  */
512
- constructor(buffer, proto, options = {}) {
521
+ constructor(buffer, proto, options) {
513
522
  super();
514
523
  this.proto = proto;
515
- this.hashFormat = options.hashFormat || 'bs32';
516
- if (options.name)
524
+ this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
525
+ if (options?.name)
517
526
  this.name = options.name;
518
527
  this.init(buffer);
519
528
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/codec-format-interface",
3
- "version": "1.6.1",
3
+ "version": "1.6.2",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {
@@ -38,6 +38,7 @@
38
38
  "@rollup/plugin-commonjs": "^23.0.3",
39
39
  "@rollup/plugin-node-resolve": "^15.0.1",
40
40
  "@rollup/plugin-typescript": "^10.0.1",
41
+ "@types/varint": "^6.0.1",
41
42
  "rollup": "^3.5.1",
42
43
  "tape": "^5.5.3",
43
44
  "tslib": "^2.4.1"