@leofcoin/codec-format-interface 1.7.3 → 1.7.5

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,9 +1,11 @@
1
1
  export default class BasicInterface {
2
2
  #private;
3
- encoded: Uint8Array;
4
- decoded: object;
5
3
  keys: string[];
6
4
  name: string;
5
+ get encoded(): Uint8Array;
6
+ set encoded(value: Uint8Array);
7
+ get decoded(): object;
8
+ set decoded(value: object);
7
9
  set proto(value: object);
8
10
  get proto(): object;
9
11
  decode(encoded?: Uint8Array): Object;
@@ -5,11 +5,27 @@ import proto from '@vandeurenglenn/proto-array';
5
5
  import { fromBase58, fromHex, toHex, toBase32, toBase58 } from '@vandeurenglenn/typed-array-utils';
6
6
 
7
7
  class BasicInterface {
8
- encoded;
9
- decoded;
8
+ #encoded;
9
+ #decoded;
10
10
  keys;
11
11
  name;
12
12
  #proto;
13
+ get encoded() {
14
+ if (!this.#encoded)
15
+ this.#encoded = this.encode();
16
+ return this.#encoded;
17
+ }
18
+ set encoded(value) {
19
+ this.#encoded = value;
20
+ }
21
+ get decoded() {
22
+ if (!this.#decoded)
23
+ this.#decoded = this.decode();
24
+ return this.#decoded;
25
+ }
26
+ set decoded(value) {
27
+ this.#decoded = value;
28
+ }
13
29
  set proto(value) {
14
30
  this.#proto = value;
15
31
  this.keys = Object.keys(value);
@@ -134,7 +134,7 @@ class FormatInterface extends BasicInterface {
134
134
  decoded[key] = data[key];
135
135
  }
136
136
  this.decoded = decoded;
137
- return this.encode(decoded);
137
+ // return this.encode(decoded)
138
138
  }
139
139
  }
140
140
  }
@@ -9,7 +9,7 @@ export default class CodecHash extends BasicInterface {
9
9
  get buffer(): Uint8Array;
10
10
  get hash(): Uint8Array;
11
11
  fromJSON(json: any): Promise<Uint8Array>;
12
- encode(buffer: any, name: any): Promise<Uint8Array>;
12
+ encode(buffer: any, name?: any): Promise<Uint8Array>;
13
13
  validate(buffer: any): Promise<void>;
14
14
  decode(buffer: any): {
15
15
  codec: any;
@@ -66,14 +66,14 @@ class CodecHash extends BasicInterface {
66
66
  return this.encoded;
67
67
  }
68
68
  fromJSON(json) {
69
- return this.encode(Buffer.from(JSON.stringify(json)));
69
+ return this.encode(new TextEncoder().encode(JSON.stringify(json)));
70
70
  }
71
71
  async encode(buffer, name) {
72
72
  if (!this.name && name)
73
73
  this.name = name;
74
74
  if (!buffer)
75
75
  buffer = this.buffer;
76
- this.discoCodec = new Codec(this.name, this.codecs);
76
+ this.discoCodec = new Codec(this.name);
77
77
  this.discoCodec.fromName(this.name);
78
78
  let hashAlg = this.discoCodec.hashAlg;
79
79
  const hashVariant = Number(hashAlg.split('-')[hashAlg.split('-').length - 1]);
package/exports/codec.js CHANGED
@@ -75,6 +75,12 @@ class Codec extends BasicInterface {
75
75
  this.hashAlg = this.getHashAlg(this.name);
76
76
  this.codec = this.getCodec(this.name);
77
77
  this.codecBuffer = varint.encode(this.codec);
78
+ this.decoded = {
79
+ name: this.name,
80
+ hashAlg: this.hashAlg,
81
+ codec: this.codec,
82
+ codecBuffer: this.codecBuffer
83
+ };
78
84
  }
79
85
  fromName(name) {
80
86
  const codec = this.getCodec(name);
@@ -82,6 +88,12 @@ class Codec extends BasicInterface {
82
88
  this.codec = codec;
83
89
  this.hashAlg = this.getHashAlg(name);
84
90
  this.codecBuffer = varint.encode(this.codec);
91
+ this.decoded = {
92
+ name: this.name,
93
+ hashAlg: this.hashAlg,
94
+ codec: this.codec,
95
+ codecBuffer: this.codecBuffer
96
+ };
85
97
  }
86
98
  decode(encoded) {
87
99
  encoded = encoded || this.encoded;
@@ -0,0 +1,34 @@
1
+ import BasicInterface from './basic-interface.js';
2
+ import Hash from './codec-hash.js';
3
+ export default class JsonInterface extends BasicInterface implements JsonInterface {
4
+ #private;
5
+ hashFormat: string;
6
+ init(buffer: Uint8Array | ArrayBuffer | JsonInterface | string): JsonInterface;
7
+ hasCodec(): boolean;
8
+ decode(encoded?: string): object;
9
+ encode(decoded?: object): Uint8Array;
10
+ /**
11
+ * @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
12
+ * @param {Object} proto - {protoObject}
13
+ * @param {Object} options - {hashFormat, name}
14
+ */
15
+ constructor(buffer: any, proto: any, options?: {
16
+ hashFormat?: string;
17
+ name?: string;
18
+ });
19
+ get format(): string;
20
+ /**
21
+ * @return {PeernetHash}
22
+ */
23
+ get peernetHash(): Hash;
24
+ /**
25
+ * @return {peernetHash}
26
+ */
27
+ hash(): Promise<any>;
28
+ fromUint8Array(buffer: any): object;
29
+ fromArrayBuffer(buffer: any): object;
30
+ /**
31
+ * @param {Object} data
32
+ */
33
+ create(data: object): Uint8Array;
34
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/codec-format-interface",
3
- "version": "1.7.3",
3
+ "version": "1.7.5",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "exports": {