@leofcoin/codec-format-interface 1.7.5 → 1.7.6
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,5 +1,5 @@
|
|
|
1
|
-
import BasicInterface from
|
|
2
|
-
import Hash from
|
|
1
|
+
import BasicInterface from "./basic-interface.js";
|
|
2
|
+
import Hash from "./codec-hash.js";
|
|
3
3
|
export default class FormatInterface extends BasicInterface implements FormatInterface {
|
|
4
4
|
#private;
|
|
5
5
|
hashFormat: string;
|
|
@@ -25,10 +25,10 @@ export default class FormatInterface extends BasicInterface implements FormatInt
|
|
|
25
25
|
* @return {peernetHash}
|
|
26
26
|
*/
|
|
27
27
|
hash(): Promise<any>;
|
|
28
|
-
fromUint8Array(buffer: any): object;
|
|
29
|
-
fromArrayBuffer(buffer: any): object;
|
|
28
|
+
fromUint8Array(buffer: any): void | object;
|
|
29
|
+
fromArrayBuffer(buffer: any): void | object;
|
|
30
30
|
/**
|
|
31
31
|
* @param {Object} data
|
|
32
32
|
*/
|
|
33
|
-
create(data: object):
|
|
33
|
+
create(data: object): void;
|
|
34
34
|
}
|
|
@@ -13,13 +13,13 @@ class FormatInterface extends BasicInterface {
|
|
|
13
13
|
hashFormat;
|
|
14
14
|
#hash;
|
|
15
15
|
init(buffer) {
|
|
16
|
-
if (buffer instanceof
|
|
16
|
+
if (buffer instanceof FormatInterface && buffer?.name === this.name)
|
|
17
|
+
return buffer;
|
|
18
|
+
else if (buffer instanceof Uint8Array)
|
|
17
19
|
this.fromUint8Array(buffer);
|
|
18
20
|
else if (buffer instanceof ArrayBuffer)
|
|
19
21
|
this.fromArrayBuffer(buffer);
|
|
20
|
-
else if (
|
|
21
|
-
return buffer;
|
|
22
|
-
else if (typeof buffer === 'string') {
|
|
22
|
+
else if (typeof buffer === "string") {
|
|
23
23
|
if (this.isHex(buffer))
|
|
24
24
|
this.fromHex(buffer);
|
|
25
25
|
else if (this.isBase58(buffer))
|
|
@@ -85,7 +85,7 @@ class FormatInterface extends BasicInterface {
|
|
|
85
85
|
constructor(buffer, proto, options) {
|
|
86
86
|
super();
|
|
87
87
|
this.proto = proto;
|
|
88
|
-
this.hashFormat = options?.hashFormat ? options.hashFormat :
|
|
88
|
+
this.hashFormat = options?.hashFormat ? options.hashFormat : "bs32";
|
|
89
89
|
if (options?.name)
|
|
90
90
|
this.name = options.name;
|
|
91
91
|
this.init(buffer);
|
|
@@ -116,17 +116,22 @@ class FormatInterface extends BasicInterface {
|
|
|
116
116
|
}
|
|
117
117
|
fromUint8Array(buffer) {
|
|
118
118
|
this.encoded = buffer;
|
|
119
|
-
return this.hasCodec()
|
|
119
|
+
return this.hasCodec()
|
|
120
|
+
? this.decode()
|
|
121
|
+
: this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
120
122
|
}
|
|
121
123
|
fromArrayBuffer(buffer) {
|
|
122
124
|
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
123
|
-
return this.hasCodec()
|
|
125
|
+
return this.hasCodec()
|
|
126
|
+
? this.decode()
|
|
127
|
+
: this.create(JSON.parse(new TextDecoder().decode(this.encoded)));
|
|
124
128
|
}
|
|
125
129
|
/**
|
|
126
130
|
* @param {Object} data
|
|
127
131
|
*/
|
|
128
132
|
create(data) {
|
|
129
133
|
const decoded = {};
|
|
134
|
+
// @ts-ignore
|
|
130
135
|
if (data.hash)
|
|
131
136
|
this.#hash = data.hash;
|
|
132
137
|
if (this.keys?.length > 0) {
|