@leofcoin/codec-format-interface 1.7.14 → 1.7.15
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/README.md +35 -10
- package/exports/README.md +8 -0
- package/exports/basic-interface.d.ts +4 -0
- package/exports/basic-interface.js +92 -14
- package/exports/basic-interface.js.map +1 -0
- package/exports/cjs/basic-interface.js +209 -0
- package/exports/cjs/basic-interface.js.map +1 -0
- package/exports/cjs/codec-format-interface.js +175 -0
- package/exports/cjs/codec-format-interface.js.map +1 -0
- package/exports/cjs/codec-hash.js +223 -0
- package/exports/cjs/codec-hash.js.map +1 -0
- package/exports/cjs/codec.js +114 -0
- package/exports/cjs/codec.js.map +1 -0
- package/exports/cjs/index.js +42 -0
- package/exports/cjs/index.js.map +1 -0
- package/exports/codec-format-interface.d.ts +1 -1
- package/exports/codec-format-interface.js +23 -7
- package/exports/codec-format-interface.js.map +1 -0
- package/exports/codec-hash.d.ts +2 -0
- package/exports/codec-hash.js +85 -13
- package/exports/codec-hash.js.map +1 -0
- package/exports/codec.js +1 -0
- package/exports/codec.js.map +1 -0
- package/exports/index.d.ts +15 -0
- package/exports/index.js +15 -0
- package/exports/index.js.map +1 -0
- package/exports/json-interface.d.ts +1 -1
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# codec-format-interface
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
TypeScript/JavaScript interface for defining and working with codec formats.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
4
6
|
|
|
5
7
|
```js
|
|
6
8
|
import FormatInterface from '@leofcoin/codec-format-interface'
|
|
@@ -14,19 +16,42 @@ class MyFormat extends FormatInterface {
|
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
constructor(data) {
|
|
17
|
-
super(data, { name: 'my-format', hashFormat
|
|
19
|
+
super(data, { name: 'my-format', hashFormat: 'bs32' })
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
|
-
|
|
21
22
|
```
|
|
22
23
|
|
|
23
|
-
##
|
|
24
|
-
|
|
24
|
+
## API
|
|
25
|
+
|
|
26
|
+
- **FormatInterface**: Base class for creating custom codec formats.
|
|
27
|
+
- `constructor(data, options)`: Initialize with data and options.
|
|
28
|
+
- `proto`: Getter for the protocol definition.
|
|
29
|
+
- `toBs58()`, `toBs32()`, `hash()`: Encoding and hashing utilities.
|
|
30
|
+
|
|
31
|
+
## Build for Browser
|
|
32
|
+
|
|
33
|
+
### Rollup Example
|
|
25
34
|
```js
|
|
26
35
|
import resolve from '@rollup/plugin-node-resolve'
|
|
27
|
-
|
|
36
|
+
import typescript from '@rollup/plugin-typescript'
|
|
37
|
+
|
|
38
|
+
export default {
|
|
39
|
+
input: 'src/index.ts',
|
|
40
|
+
output: {
|
|
41
|
+
dir: 'exports',
|
|
42
|
+
format: 'es'
|
|
43
|
+
},
|
|
28
44
|
plugins: [
|
|
29
|
-
resolve()
|
|
45
|
+
resolve(),
|
|
46
|
+
typescript()
|
|
30
47
|
]
|
|
31
|
-
|
|
32
|
-
```
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Contributing
|
|
52
|
+
|
|
53
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
MIT
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Exports Directory
|
|
2
|
+
|
|
3
|
+
This directory contains the public API surface of the package. Only files in this directory are considered part of the public API and are safe to import from other projects.
|
|
4
|
+
|
|
5
|
+
- `index.js` / `index.d.ts`: Main entry point
|
|
6
|
+
- Other files: Specific interfaces and codecs
|
|
7
|
+
|
|
8
|
+
Do not import from internal `src/` files directly.
|
|
@@ -14,6 +14,10 @@ export default class BasicInterface {
|
|
|
14
14
|
get proto(): object;
|
|
15
15
|
decode(encoded?: Uint8Array): Object;
|
|
16
16
|
encode(decoded?: object): Uint8Array;
|
|
17
|
+
static _protoCache: WeakMap<object, {
|
|
18
|
+
keys: string[];
|
|
19
|
+
values: any[];
|
|
20
|
+
}>;
|
|
17
21
|
protoEncode(data: object): Uint8Array;
|
|
18
22
|
protoDecode(data: Uint8Array): object;
|
|
19
23
|
isHex(string: string): boolean;
|
|
@@ -4,8 +4,44 @@ import isHex from '@vandeurenglenn/is-hex';
|
|
|
4
4
|
import proto from '@vandeurenglenn/proto-array';
|
|
5
5
|
import { fromBase58, fromHex, toHex, toBase32, toBase58 } from '@vandeurenglenn/typed-array-utils';
|
|
6
6
|
|
|
7
|
-
const
|
|
8
|
-
const
|
|
7
|
+
const BASE64_CHUNK_SIZE = 0x8000;
|
|
8
|
+
const uint8ArrayToBase64 = (value) => {
|
|
9
|
+
if (typeof Buffer !== 'undefined')
|
|
10
|
+
return Buffer.from(value).toString('base64');
|
|
11
|
+
let binary = '';
|
|
12
|
+
for (let offset = 0; offset < value.length; offset += BASE64_CHUNK_SIZE) {
|
|
13
|
+
const slice = value.subarray(offset, offset + BASE64_CHUNK_SIZE);
|
|
14
|
+
binary += String.fromCharCode(...slice);
|
|
15
|
+
}
|
|
16
|
+
return btoa(binary);
|
|
17
|
+
};
|
|
18
|
+
const base64ToUint8Array = (value) => {
|
|
19
|
+
if (typeof Buffer !== 'undefined')
|
|
20
|
+
return new Uint8Array(Buffer.from(value, 'base64'));
|
|
21
|
+
const binary = atob(value);
|
|
22
|
+
const output = new Uint8Array(binary.length);
|
|
23
|
+
for (let i = 0; i < binary.length; i++)
|
|
24
|
+
output[i] = binary.charCodeAt(i);
|
|
25
|
+
return output;
|
|
26
|
+
};
|
|
27
|
+
const jsonStringifyBigInt = (key, value) => {
|
|
28
|
+
if (typeof value === 'bigint')
|
|
29
|
+
return { $bigint: value.toString() };
|
|
30
|
+
if (value instanceof Uint8Array)
|
|
31
|
+
return { $uint8array: uint8ArrayToBase64(value) };
|
|
32
|
+
return value;
|
|
33
|
+
};
|
|
34
|
+
const jsonParseBigInt = (key, value) => {
|
|
35
|
+
if (typeof value === 'object' && value) {
|
|
36
|
+
if (value.$bigint)
|
|
37
|
+
return BigInt(value.$bigint);
|
|
38
|
+
if (value.$uint8array)
|
|
39
|
+
return base64ToUint8Array(value.$uint8array);
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
};
|
|
43
|
+
const _textEncoder = new TextEncoder();
|
|
44
|
+
const _textDecoder = new TextDecoder();
|
|
9
45
|
class BasicInterface {
|
|
10
46
|
#encoded;
|
|
11
47
|
#decoded;
|
|
@@ -40,20 +76,49 @@ class BasicInterface {
|
|
|
40
76
|
}
|
|
41
77
|
decode(encoded) {
|
|
42
78
|
encoded = encoded || this.encoded;
|
|
43
|
-
|
|
79
|
+
// Example: decode as JSON if possible (override in subclass)
|
|
80
|
+
try {
|
|
81
|
+
return JSON.parse(_textDecoder.decode(encoded), jsonParseBigInt);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return new Object();
|
|
85
|
+
}
|
|
44
86
|
}
|
|
45
87
|
encode(decoded) {
|
|
46
88
|
decoded = decoded || this.decoded;
|
|
47
|
-
|
|
89
|
+
// Example: encode as JSON (override in subclass)
|
|
90
|
+
return _textEncoder.encode(JSON.stringify(decoded, jsonStringifyBigInt));
|
|
48
91
|
}
|
|
49
92
|
// get Codec(): Codec {}
|
|
93
|
+
// Cache proto keys/values for reuse
|
|
94
|
+
static _protoCache = new WeakMap();
|
|
50
95
|
protoEncode(data) {
|
|
51
|
-
|
|
96
|
+
let cache = BasicInterface._protoCache.get(this.proto);
|
|
97
|
+
if (!cache) {
|
|
98
|
+
cache = {
|
|
99
|
+
keys: Object.keys(this.proto),
|
|
100
|
+
values: Object.values(this.proto)
|
|
101
|
+
};
|
|
102
|
+
BasicInterface._protoCache.set(this.proto, cache);
|
|
103
|
+
}
|
|
104
|
+
// Use proto.encode directly, but avoid new array allocations inside encode if possible
|
|
52
105
|
return proto.encode(this.proto, data, false);
|
|
53
106
|
}
|
|
54
107
|
protoDecode(data) {
|
|
55
|
-
//
|
|
56
|
-
|
|
108
|
+
// Use a static output object if possible (not thread-safe, but safe for single-threaded use)
|
|
109
|
+
if (!this._decodeOutput)
|
|
110
|
+
this._decodeOutput = {};
|
|
111
|
+
const result = proto.decode(this.proto, data, false);
|
|
112
|
+
// Copy properties to static object to avoid new allocations
|
|
113
|
+
Object.keys(result).forEach((k) => {
|
|
114
|
+
this._decodeOutput[k] = result[k];
|
|
115
|
+
});
|
|
116
|
+
// Remove any keys not in result
|
|
117
|
+
Object.keys(this._decodeOutput).forEach((k) => {
|
|
118
|
+
if (!(k in result))
|
|
119
|
+
delete this._decodeOutput[k];
|
|
120
|
+
});
|
|
121
|
+
return this._decodeOutput;
|
|
57
122
|
}
|
|
58
123
|
isHex(string) {
|
|
59
124
|
return isHex(string);
|
|
@@ -86,7 +151,10 @@ class BasicInterface {
|
|
|
86
151
|
return this.decode(fromHex(string));
|
|
87
152
|
}
|
|
88
153
|
fromArray(array) {
|
|
89
|
-
|
|
154
|
+
// Avoid unnecessary copy if already Uint8Array
|
|
155
|
+
if (array instanceof Uint8Array)
|
|
156
|
+
return this.decode(array);
|
|
157
|
+
return this.decode(Uint8Array.from(array));
|
|
90
158
|
}
|
|
91
159
|
fromEncoded(encoded) {
|
|
92
160
|
return this.decode(encoded);
|
|
@@ -94,15 +162,21 @@ class BasicInterface {
|
|
|
94
162
|
toString() {
|
|
95
163
|
if (!this.encoded)
|
|
96
164
|
this.encode();
|
|
97
|
-
|
|
165
|
+
// Use cached string if available
|
|
166
|
+
if (!this._string)
|
|
167
|
+
this._string = Array.prototype.join.call(this.encoded, ',');
|
|
168
|
+
return this._string;
|
|
98
169
|
}
|
|
99
170
|
toHex() {
|
|
100
171
|
if (!this.encoded)
|
|
101
172
|
this.encode();
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
.
|
|
105
|
-
|
|
173
|
+
// Use cached hex if available
|
|
174
|
+
if (!this._hex) {
|
|
175
|
+
if (!this._string)
|
|
176
|
+
this._string = Array.prototype.join.call(this.encoded, ',');
|
|
177
|
+
this._hex = toHex(this._string.split(',').map(Number));
|
|
178
|
+
}
|
|
179
|
+
return this._hex;
|
|
106
180
|
}
|
|
107
181
|
/**
|
|
108
182
|
* @return {String} encoded
|
|
@@ -110,7 +184,10 @@ class BasicInterface {
|
|
|
110
184
|
toBs32() {
|
|
111
185
|
if (!this.encoded)
|
|
112
186
|
this.encode();
|
|
113
|
-
|
|
187
|
+
// Use cached bs32 if available
|
|
188
|
+
if (!this._bs32)
|
|
189
|
+
this._bs32 = toBase32(this.encoded);
|
|
190
|
+
return this._bs32;
|
|
114
191
|
}
|
|
115
192
|
/**
|
|
116
193
|
* @return {String} encoded
|
|
@@ -123,3 +200,4 @@ class BasicInterface {
|
|
|
123
200
|
}
|
|
124
201
|
|
|
125
202
|
export { BasicInterface as default, jsonParseBigInt, jsonStringifyBigInt };
|
|
203
|
+
//# sourceMappingURL=basic-interface.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"basic-interface.js","sources":["../src/basic-interface.ts"],"sourcesContent":["import base32 from '@vandeurenglenn/base32'\nimport base58 from '@vandeurenglenn/base58'\nimport type { base58String } from '@vandeurenglenn/base58'\nimport type { base32String } from '@vandeurenglenn/base32'\nimport isHex from '@vandeurenglenn/is-hex'\nimport proto from '@vandeurenglenn/proto-array'\nimport {\n fromBase32,\n fromBase58,\n fromString,\n fromHex,\n fromArrayLike,\n fromUintArrayString,\n toBase32,\n toBase58,\n toHex\n} from '@vandeurenglenn/typed-array-utils'\n\nconst BASE64_CHUNK_SIZE = 0x8000\n\nconst uint8ArrayToBase64 = (value: Uint8Array): string => {\n if (typeof Buffer !== 'undefined') return Buffer.from(value).toString('base64')\n let binary = ''\n for (let offset = 0; offset < value.length; offset += BASE64_CHUNK_SIZE) {\n const slice = value.subarray(offset, offset + BASE64_CHUNK_SIZE)\n binary += String.fromCharCode(...slice)\n }\n return btoa(binary)\n}\n\nconst base64ToUint8Array = (value: string): Uint8Array => {\n if (typeof Buffer !== 'undefined') return new Uint8Array(Buffer.from(value, 'base64'))\n const binary = atob(value)\n const output = new Uint8Array(binary.length)\n for (let i = 0; i < binary.length; i++) output[i] = binary.charCodeAt(i)\n return output\n}\n\nexport const jsonStringifyBigInt = (key, value) => {\n if (typeof value === 'bigint') return { $bigint: value.toString() }\n if (value instanceof Uint8Array) return { $uint8array: uint8ArrayToBase64(value) }\n return value\n}\n\nexport const jsonParseBigInt = (key, value) => {\n if (typeof value === 'object' && value) {\n if (value.$bigint) return BigInt(value.$bigint)\n if (value.$uint8array) return base64ToUint8Array(value.$uint8array)\n }\n return value\n}\n\nconst _textEncoder = new TextEncoder()\nconst _textDecoder = new TextDecoder()\n\nexport default class BasicInterface {\n #encoded: Uint8Array\n #decoded: object\n name: string\n #proto: object\n\n get keys() {\n // handles proto keys\n // protokey -> key\n return Object.keys(this.#proto).map((key) => (key.endsWith('?') ? key.split('?')[0] : key))\n }\n\n get encoded() {\n if (!this.#encoded) this.#encoded = this.encode()\n return this.#encoded\n }\n\n set encoded(value) {\n this.#encoded = value\n }\n\n get decoded() {\n if (!this.#decoded) this.#decoded = this.decode()\n return this.#decoded\n }\n\n set decoded(value) {\n this.#decoded = value\n }\n\n set proto(value) {\n this.#proto = value\n }\n\n get proto() {\n return this.#proto\n }\n\n decode(encoded?: Uint8Array): Object {\n encoded = encoded || this.encoded\n // Example: decode as JSON if possible (override in subclass)\n try {\n return JSON.parse(_textDecoder.decode(encoded), jsonParseBigInt)\n } catch {\n return new Object()\n }\n }\n\n encode(decoded?: object): Uint8Array {\n decoded = decoded || this.decoded\n // Example: encode as JSON (override in subclass)\n return _textEncoder.encode(JSON.stringify(decoded, jsonStringifyBigInt))\n }\n // get Codec(): Codec {}\n\n // Cache proto keys/values for reuse\n static _protoCache = new WeakMap<object, { keys: string[]; values: any[] }>()\n\n protoEncode(data: object): Uint8Array {\n let cache = BasicInterface._protoCache.get(this.proto)\n if (!cache) {\n cache = {\n keys: Object.keys(this.proto),\n values: Object.values(this.proto)\n }\n BasicInterface._protoCache.set(this.proto, cache)\n }\n // Use proto.encode directly, but avoid new array allocations inside encode if possible\n return proto.encode(this.proto, data, false)\n }\n\n protoDecode(data: Uint8Array): object {\n // Use a static output object if possible (not thread-safe, but safe for single-threaded use)\n if (!this._decodeOutput) this._decodeOutput = {}\n const result = proto.decode(this.proto, data, false)\n // Copy properties to static object to avoid new allocations\n Object.keys(result).forEach((k) => {\n this._decodeOutput[k] = result[k]\n })\n // Remove any keys not in result\n Object.keys(this._decodeOutput).forEach((k) => {\n if (!(k in result)) delete this._decodeOutput[k]\n })\n return this._decodeOutput\n }\n\n isHex(string: string): boolean {\n return isHex(string)\n }\n isBase32(string: string): boolean {\n return base32.isBase32(string)\n }\n isBase58(string: string): boolean {\n return base58.isBase58(string)\n }\n\n fromBs32(encoded: string): object {\n return this.decode(base32.decode(encoded))\n }\n\n fromBs58(encoded: base58String): object {\n return this.decode(fromBase58(encoded))\n }\n\n async toArray() {\n const array: number[] = []\n for await (const value of this.encoded.values()) {\n array.push(value)\n }\n return array\n }\n\n fromString(string: string): object {\n const array: string[] = string.split(',')\n const arrayLike = array.map((string) => Number(string))\n return this.decode(Uint8Array.from(arrayLike))\n }\n\n fromHex(string: string): object {\n return this.decode(fromHex(string))\n }\n\n fromArray(array: number[]): object {\n // Avoid unnecessary copy if already Uint8Array\n if (array instanceof Uint8Array) return this.decode(array)\n return this.decode(Uint8Array.from(array))\n }\n\n fromEncoded(encoded: Uint8Array) {\n return this.decode(encoded)\n }\n\n toString(): string {\n if (!this.encoded) this.encode()\n // Use cached string if available\n if (!this._string) this._string = Array.prototype.join.call(this.encoded, ',')\n return this._string\n }\n\n toHex(): string {\n if (!this.encoded) this.encode()\n // Use cached hex if available\n if (!this._hex) {\n if (!this._string) this._string = Array.prototype.join.call(this.encoded, ',')\n this._hex = toHex(this._string.split(',').map(Number))\n }\n return this._hex\n }\n\n /**\n * @return {String} encoded\n */\n toBs32(): base32String {\n if (!this.encoded) this.encode()\n // Use cached bs32 if available\n if (!this._bs32) this._bs32 = toBase32(this.encoded)\n return this._bs32\n }\n\n /**\n * @return {String} encoded\n */\n toBs58(): base58String {\n if (!this.encoded) this.encode()\n return toBase58(this.encoded)\n }\n}\n"],"names":[],"mappings":";;;;;;AAkBA,MAAM,iBAAiB,GAAG,MAAM;AAEhC,MAAM,kBAAkB,GAAG,CAAC,KAAiB,KAAY;IACvD,IAAI,OAAO,MAAM,KAAK,WAAW;QAAE,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC;IAC/E,IAAI,MAAM,GAAG,EAAE;AACf,IAAA,KAAK,IAAI,MAAM,GAAG,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC,MAAM,EAAE,MAAM,IAAI,iBAAiB,EAAE;AACvE,QAAA,MAAM,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,iBAAiB,CAAC;QAChE,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,GAAG,KAAK,CAAC;IACzC;AACA,IAAA,OAAO,IAAI,CAAC,MAAM,CAAC;AACrB,CAAC;AAED,MAAM,kBAAkB,GAAG,CAAC,KAAa,KAAgB;IACvD,IAAI,OAAO,MAAM,KAAK,WAAW;AAAE,QAAA,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;AACtF,IAAA,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC;IAC1B,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC;AAC5C,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE;QAAE,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;AACxE,IAAA,OAAO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AO;AAAE,YAAA,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC;QAC9E,OAAO,IAAI,CAAC,OAAO;IACrB;IAEA,KAAK,GAAA;QACH,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,IAAI,CAAC,MAAM,EAAE;;AAEhC,QAAA,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE;YACd,IAAI,CAAC,IAAI,CAAC,OAAO;AAAE,gBAAA,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC;AAC9E,YAAA,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACxD;QACA,OAAO,IAAI,CAAC,IAAI;IAClB;AAEA;;AAEG;IACH,MAAM,GAAA;QACJ,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,IAAI,CAAC,MAAM,EAAE;;QAEhC,IAAI,CAAC,IAAI,CAAC,KAAK;YAAE,IAAI,CAAC,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC;QACpD,OAAO,IAAI,CAAC,KAAK;IACnB;AAEA;;AAEG;IACH,MAAM,GAAA;QACJ,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,IAAI,CAAC,MAAM,EAAE;AAChC,QAAA,OAAO,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC;IAC/B;;;;;"}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var base32 = require('@vandeurenglenn/base32');
|
|
6
|
+
var base58 = require('@vandeurenglenn/base58');
|
|
7
|
+
var isHex = require('@vandeurenglenn/is-hex');
|
|
8
|
+
var proto = require('@vandeurenglenn/proto-array');
|
|
9
|
+
var typedArrayUtils = require('@vandeurenglenn/typed-array-utils');
|
|
10
|
+
|
|
11
|
+
const BASE64_CHUNK_SIZE = 0x8000;
|
|
12
|
+
const uint8ArrayToBase64 = (value) => {
|
|
13
|
+
if (typeof Buffer !== 'undefined')
|
|
14
|
+
return Buffer.from(value).toString('base64');
|
|
15
|
+
let binary = '';
|
|
16
|
+
for (let offset = 0; offset < value.length; offset += BASE64_CHUNK_SIZE) {
|
|
17
|
+
const slice = value.subarray(offset, offset + BASE64_CHUNK_SIZE);
|
|
18
|
+
binary += String.fromCharCode(...slice);
|
|
19
|
+
}
|
|
20
|
+
return btoa(binary);
|
|
21
|
+
};
|
|
22
|
+
const base64ToUint8Array = (value) => {
|
|
23
|
+
if (typeof Buffer !== 'undefined')
|
|
24
|
+
return new Uint8Array(Buffer.from(value, 'base64'));
|
|
25
|
+
const binary = atob(value);
|
|
26
|
+
const output = new Uint8Array(binary.length);
|
|
27
|
+
for (let i = 0; i < binary.length; i++)
|
|
28
|
+
output[i] = binary.charCodeAt(i);
|
|
29
|
+
return output;
|
|
30
|
+
};
|
|
31
|
+
const jsonStringifyBigInt = (key, value) => {
|
|
32
|
+
if (typeof value === 'bigint')
|
|
33
|
+
return { $bigint: value.toString() };
|
|
34
|
+
if (value instanceof Uint8Array)
|
|
35
|
+
return { $uint8array: uint8ArrayToBase64(value) };
|
|
36
|
+
return value;
|
|
37
|
+
};
|
|
38
|
+
const jsonParseBigInt = (key, value) => {
|
|
39
|
+
if (typeof value === 'object' && value) {
|
|
40
|
+
if (value.$bigint)
|
|
41
|
+
return BigInt(value.$bigint);
|
|
42
|
+
if (value.$uint8array)
|
|
43
|
+
return base64ToUint8Array(value.$uint8array);
|
|
44
|
+
}
|
|
45
|
+
return value;
|
|
46
|
+
};
|
|
47
|
+
const _textEncoder = new TextEncoder();
|
|
48
|
+
const _textDecoder = new TextDecoder();
|
|
49
|
+
class BasicInterface {
|
|
50
|
+
#encoded;
|
|
51
|
+
#decoded;
|
|
52
|
+
name;
|
|
53
|
+
#proto;
|
|
54
|
+
get keys() {
|
|
55
|
+
// handles proto keys
|
|
56
|
+
// protokey -> key
|
|
57
|
+
return Object.keys(this.#proto).map((key) => (key.endsWith('?') ? key.split('?')[0] : key));
|
|
58
|
+
}
|
|
59
|
+
get encoded() {
|
|
60
|
+
if (!this.#encoded)
|
|
61
|
+
this.#encoded = this.encode();
|
|
62
|
+
return this.#encoded;
|
|
63
|
+
}
|
|
64
|
+
set encoded(value) {
|
|
65
|
+
this.#encoded = value;
|
|
66
|
+
}
|
|
67
|
+
get decoded() {
|
|
68
|
+
if (!this.#decoded)
|
|
69
|
+
this.#decoded = this.decode();
|
|
70
|
+
return this.#decoded;
|
|
71
|
+
}
|
|
72
|
+
set decoded(value) {
|
|
73
|
+
this.#decoded = value;
|
|
74
|
+
}
|
|
75
|
+
set proto(value) {
|
|
76
|
+
this.#proto = value;
|
|
77
|
+
}
|
|
78
|
+
get proto() {
|
|
79
|
+
return this.#proto;
|
|
80
|
+
}
|
|
81
|
+
decode(encoded) {
|
|
82
|
+
encoded = encoded || this.encoded;
|
|
83
|
+
// Example: decode as JSON if possible (override in subclass)
|
|
84
|
+
try {
|
|
85
|
+
return JSON.parse(_textDecoder.decode(encoded), jsonParseBigInt);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return new Object();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
encode(decoded) {
|
|
92
|
+
decoded = decoded || this.decoded;
|
|
93
|
+
// Example: encode as JSON (override in subclass)
|
|
94
|
+
return _textEncoder.encode(JSON.stringify(decoded, jsonStringifyBigInt));
|
|
95
|
+
}
|
|
96
|
+
// get Codec(): Codec {}
|
|
97
|
+
// Cache proto keys/values for reuse
|
|
98
|
+
static _protoCache = new WeakMap();
|
|
99
|
+
protoEncode(data) {
|
|
100
|
+
let cache = BasicInterface._protoCache.get(this.proto);
|
|
101
|
+
if (!cache) {
|
|
102
|
+
cache = {
|
|
103
|
+
keys: Object.keys(this.proto),
|
|
104
|
+
values: Object.values(this.proto)
|
|
105
|
+
};
|
|
106
|
+
BasicInterface._protoCache.set(this.proto, cache);
|
|
107
|
+
}
|
|
108
|
+
// Use proto.encode directly, but avoid new array allocations inside encode if possible
|
|
109
|
+
return proto.encode(this.proto, data, false);
|
|
110
|
+
}
|
|
111
|
+
protoDecode(data) {
|
|
112
|
+
// Use a static output object if possible (not thread-safe, but safe for single-threaded use)
|
|
113
|
+
if (!this._decodeOutput)
|
|
114
|
+
this._decodeOutput = {};
|
|
115
|
+
const result = proto.decode(this.proto, data, false);
|
|
116
|
+
// Copy properties to static object to avoid new allocations
|
|
117
|
+
Object.keys(result).forEach((k) => {
|
|
118
|
+
this._decodeOutput[k] = result[k];
|
|
119
|
+
});
|
|
120
|
+
// Remove any keys not in result
|
|
121
|
+
Object.keys(this._decodeOutput).forEach((k) => {
|
|
122
|
+
if (!(k in result))
|
|
123
|
+
delete this._decodeOutput[k];
|
|
124
|
+
});
|
|
125
|
+
return this._decodeOutput;
|
|
126
|
+
}
|
|
127
|
+
isHex(string) {
|
|
128
|
+
return isHex(string);
|
|
129
|
+
}
|
|
130
|
+
isBase32(string) {
|
|
131
|
+
return base32.isBase32(string);
|
|
132
|
+
}
|
|
133
|
+
isBase58(string) {
|
|
134
|
+
return base58.isBase58(string);
|
|
135
|
+
}
|
|
136
|
+
fromBs32(encoded) {
|
|
137
|
+
return this.decode(base32.decode(encoded));
|
|
138
|
+
}
|
|
139
|
+
fromBs58(encoded) {
|
|
140
|
+
return this.decode(typedArrayUtils.fromBase58(encoded));
|
|
141
|
+
}
|
|
142
|
+
async toArray() {
|
|
143
|
+
const array = [];
|
|
144
|
+
for await (const value of this.encoded.values()) {
|
|
145
|
+
array.push(value);
|
|
146
|
+
}
|
|
147
|
+
return array;
|
|
148
|
+
}
|
|
149
|
+
fromString(string) {
|
|
150
|
+
const array = string.split(',');
|
|
151
|
+
const arrayLike = array.map((string) => Number(string));
|
|
152
|
+
return this.decode(Uint8Array.from(arrayLike));
|
|
153
|
+
}
|
|
154
|
+
fromHex(string) {
|
|
155
|
+
return this.decode(typedArrayUtils.fromHex(string));
|
|
156
|
+
}
|
|
157
|
+
fromArray(array) {
|
|
158
|
+
// Avoid unnecessary copy if already Uint8Array
|
|
159
|
+
if (array instanceof Uint8Array)
|
|
160
|
+
return this.decode(array);
|
|
161
|
+
return this.decode(Uint8Array.from(array));
|
|
162
|
+
}
|
|
163
|
+
fromEncoded(encoded) {
|
|
164
|
+
return this.decode(encoded);
|
|
165
|
+
}
|
|
166
|
+
toString() {
|
|
167
|
+
if (!this.encoded)
|
|
168
|
+
this.encode();
|
|
169
|
+
// Use cached string if available
|
|
170
|
+
if (!this._string)
|
|
171
|
+
this._string = Array.prototype.join.call(this.encoded, ',');
|
|
172
|
+
return this._string;
|
|
173
|
+
}
|
|
174
|
+
toHex() {
|
|
175
|
+
if (!this.encoded)
|
|
176
|
+
this.encode();
|
|
177
|
+
// Use cached hex if available
|
|
178
|
+
if (!this._hex) {
|
|
179
|
+
if (!this._string)
|
|
180
|
+
this._string = Array.prototype.join.call(this.encoded, ',');
|
|
181
|
+
this._hex = typedArrayUtils.toHex(this._string.split(',').map(Number));
|
|
182
|
+
}
|
|
183
|
+
return this._hex;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* @return {String} encoded
|
|
187
|
+
*/
|
|
188
|
+
toBs32() {
|
|
189
|
+
if (!this.encoded)
|
|
190
|
+
this.encode();
|
|
191
|
+
// Use cached bs32 if available
|
|
192
|
+
if (!this._bs32)
|
|
193
|
+
this._bs32 = typedArrayUtils.toBase32(this.encoded);
|
|
194
|
+
return this._bs32;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* @return {String} encoded
|
|
198
|
+
*/
|
|
199
|
+
toBs58() {
|
|
200
|
+
if (!this.encoded)
|
|
201
|
+
this.encode();
|
|
202
|
+
return typedArrayUtils.toBase58(this.encoded);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
exports.default = BasicInterface;
|
|
207
|
+
exports.jsonParseBigInt = jsonParseBigInt;
|
|
208
|
+
exports.jsonStringifyBigInt = jsonStringifyBigInt;
|
|
209
|
+
//# sourceMappingURL=basic-interface.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"basic-interface.js","sources":["../../src/basic-interface.ts"],"sourcesContent":["import base32 from '@vandeurenglenn/base32'\nimport base58 from '@vandeurenglenn/base58'\nimport type { base58String } from '@vandeurenglenn/base58'\nimport type { base32String } from '@vandeurenglenn/base32'\nimport isHex from '@vandeurenglenn/is-hex'\nimport proto from '@vandeurenglenn/proto-array'\nimport {\n fromBase32,\n fromBase58,\n fromString,\n fromHex,\n fromArrayLike,\n fromUintArrayString,\n toBase32,\n toBase58,\n toHex\n} from '@vandeurenglenn/typed-array-utils'\n\nconst BASE64_CHUNK_SIZE = 0x8000\n\nconst uint8ArrayToBase64 = (value: Uint8Array): string => {\n if (typeof Buffer !== 'undefined') return Buffer.from(value).toString('base64')\n let binary = ''\n for (let offset = 0; offset < value.length; offset += BASE64_CHUNK_SIZE) {\n const slice = value.subarray(offset, offset + BASE64_CHUNK_SIZE)\n binary += String.fromCharCode(...slice)\n }\n return btoa(binary)\n}\n\nconst base64ToUint8Array = (value: string): Uint8Array => {\n if (typeof Buffer !== 'undefined') return new Uint8Array(Buffer.from(value, 'base64'))\n const binary = atob(value)\n const output = new Uint8Array(binary.length)\n for (let i = 0; i < binary.length; i++) output[i] = binary.charCodeAt(i)\n return output\n}\n\nexport const jsonStringifyBigInt = (key, value) => {\n if (typeof value === 'bigint') return { $bigint: value.toString() }\n if (value instanceof Uint8Array) return { $uint8array: uint8ArrayToBase64(value) }\n return value\n}\n\nexport const jsonParseBigInt = (key, value) => {\n if (typeof value === 'object' && value) {\n if (value.$bigint) return BigInt(value.$bigint)\n if (value.$uint8array) return base64ToUint8Array(value.$uint8array)\n }\n return value\n}\n\nconst _textEncoder = new TextEncoder()\nconst _textDecoder = new TextDecoder()\n\nexport default class BasicInterface {\n #encoded: Uint8Array\n #decoded: object\n name: string\n #proto: object\n\n get keys() {\n // handles proto keys\n // protokey -> key\n return Object.keys(this.#proto).map((key) => (key.endsWith('?') ? key.split('?')[0] : key))\n }\n\n get encoded() {\n if (!this.#encoded) this.#encoded = this.encode()\n return this.#encoded\n }\n\n set encoded(value) {\n this.#encoded = value\n }\n\n get decoded() {\n if (!this.#decoded) this.#decoded = this.decode()\n return this.#decoded\n }\n\n set decoded(value) {\n this.#decoded = value\n }\n\n set proto(value) {\n this.#proto = value\n }\n\n get proto() {\n return this.#proto\n }\n\n decode(encoded?: Uint8Array): Object {\n encoded = encoded || this.encoded\n // Example: decode as JSON if possible (override in subclass)\n try {\n return JSON.parse(_textDecoder.decode(encoded), jsonParseBigInt)\n } catch {\n return new Object()\n }\n }\n\n encode(decoded?: object): Uint8Array {\n decoded = decoded || this.decoded\n // Example: encode as JSON (override in subclass)\n return _textEncoder.encode(JSON.stringify(decoded, jsonStringifyBigInt))\n }\n // get Codec(): Codec {}\n\n // Cache proto keys/values for reuse\n static _protoCache = new WeakMap<object, { keys: string[]; values: any[] }>()\n\n protoEncode(data: object): Uint8Array {\n let cache = BasicInterface._protoCache.get(this.proto)\n if (!cache) {\n cache = {\n keys: Object.keys(this.proto),\n values: Object.values(this.proto)\n }\n BasicInterface._protoCache.set(this.proto, cache)\n }\n // Use proto.encode directly, but avoid new array allocations inside encode if possible\n return proto.encode(this.proto, data, false)\n }\n\n protoDecode(data: Uint8Array): object {\n // Use a static output object if possible (not thread-safe, but safe for single-threaded use)\n if (!this._decodeOutput) this._decodeOutput = {}\n const result = proto.decode(this.proto, data, false)\n // Copy properties to static object to avoid new allocations\n Object.keys(result).forEach((k) => {\n this._decodeOutput[k] = result[k]\n })\n // Remove any keys not in result\n Object.keys(this._decodeOutput).forEach((k) => {\n if (!(k in result)) delete this._decodeOutput[k]\n })\n return this._decodeOutput\n }\n\n isHex(string: string): boolean {\n return isHex(string)\n }\n isBase32(string: string): boolean {\n return base32.isBase32(string)\n }\n isBase58(string: string): boolean {\n return base58.isBase58(string)\n }\n\n fromBs32(encoded: string): object {\n return this.decode(base32.decode(encoded))\n }\n\n fromBs58(encoded: base58String): object {\n return this.decode(fromBase58(encoded))\n }\n\n async toArray() {\n const array: number[] = []\n for await (const value of this.encoded.values()) {\n array.push(value)\n }\n return array\n }\n\n fromString(string: string): object {\n const array: string[] = string.split(',')\n const arrayLike = array.map((string) => Number(string))\n return this.decode(Uint8Array.from(arrayLike))\n }\n\n fromHex(string: string): object {\n return this.decode(fromHex(string))\n }\n\n fromArray(array: number[]): object {\n // Avoid unnecessary copy if already Uint8Array\n if (array instanceof Uint8Array) return this.decode(array)\n return this.decode(Uint8Array.from(array))\n }\n\n fromEncoded(encoded: Uint8Array) {\n return this.decode(encoded)\n }\n\n toString(): string {\n if (!this.encoded) this.encode()\n // Use cached string if available\n if (!this._string) this._string = Array.prototype.join.call(this.encoded, ',')\n return this._string\n }\n\n toHex(): string {\n if (!this.encoded) this.encode()\n // Use cached hex if available\n if (!this._hex) {\n if (!this._string) this._string = Array.prototype.join.call(this.encoded, ',')\n this._hex = toHex(this._string.split(',').map(Number))\n }\n return this._hex\n }\n\n /**\n * @return {String} encoded\n */\n toBs32(): base32String {\n if (!this.encoded) this.encode()\n // Use cached bs32 if available\n if (!this._bs32) this._bs32 = toBase32(this.encoded)\n return this._bs32\n }\n\n /**\n * @return {String} encoded\n */\n toBs58(): base58String {\n if (!this.encoded) this.encode()\n return toBase58(this.encoded)\n }\n}\n"],"names":["fromBase58","fromHex","toHex","toBase32","toBase58"],"mappings":";;;;;;;;;;AAkBA,MAAM,iBAAiB,GAAG,MAAM;AAEhC,MAAM,kBAAkB,GAAG,CAAC,KAAiB,KAAY;IACvD,IAAI,OAAO,MAAM,KAAK,WAAW;QAAE,OAAO,MAAM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AE,gBAAA,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC;AAC9E,YAAA,IAAI,CAAC,IAAI,GAAGC,qBAAK,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACxD;QACA,OAAO,IAAI,CAAC,IAAI;IAClB;AAEA;;AAEG;IACH,MAAM,GAAA;QACJ,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,IAAI,CAAC,MAAM,EAAE;;QAEhC,IAAI,CAAC,IAAI,CAAC,KAAK;YAAE,IAAI,CAAC,KAAK,GAAGC,wBAAQ,CAAC,IAAI,CAAC,OAAO,CAAC;QACpD,OAAO,IAAI,CAAC,KAAK;IACnB;AAEA;;AAEG;IACH,MAAM,GAAA;QACJ,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,IAAI,CAAC,MAAM,EAAE;AAChC,QAAA,OAAOC,wBAAQ,CAAC,IAAI,CAAC,OAAO,CAAC;IAC/B;;;;;;;"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var basicInterface = require('./basic-interface.js');
|
|
4
|
+
var codec = require('./codec.js');
|
|
5
|
+
var codecHash = require('./codec-hash.js');
|
|
6
|
+
require('@vandeurenglenn/base32');
|
|
7
|
+
require('@vandeurenglenn/base58');
|
|
8
|
+
require('@vandeurenglenn/is-hex');
|
|
9
|
+
require('@vandeurenglenn/proto-array');
|
|
10
|
+
require('@vandeurenglenn/typed-array-utils');
|
|
11
|
+
require('varint');
|
|
12
|
+
require('@leofcoin/codecs');
|
|
13
|
+
require('multiformats/cid');
|
|
14
|
+
require('hash-wasm');
|
|
15
|
+
|
|
16
|
+
class FormatInterface extends basicInterface.default {
|
|
17
|
+
hashFormat;
|
|
18
|
+
#hash;
|
|
19
|
+
#encoded;
|
|
20
|
+
get encoded() {
|
|
21
|
+
return this.#encoded || this.encode();
|
|
22
|
+
}
|
|
23
|
+
set encoded(value) {
|
|
24
|
+
this.#encoded = value;
|
|
25
|
+
}
|
|
26
|
+
init(buffer) {
|
|
27
|
+
if (buffer instanceof FormatInterface && buffer?.name === this.name) {
|
|
28
|
+
return buffer;
|
|
29
|
+
}
|
|
30
|
+
else if (buffer instanceof Uint8Array) {
|
|
31
|
+
this.fromUint8Array(buffer);
|
|
32
|
+
}
|
|
33
|
+
else if (buffer instanceof ArrayBuffer) {
|
|
34
|
+
this.fromArrayBuffer(buffer);
|
|
35
|
+
}
|
|
36
|
+
else if (typeof buffer === 'string') {
|
|
37
|
+
if (this.isHex(buffer))
|
|
38
|
+
this.fromHex(buffer);
|
|
39
|
+
else if (this.isBase58(buffer))
|
|
40
|
+
this.fromBs58(buffer);
|
|
41
|
+
else if (this.isBase32(buffer))
|
|
42
|
+
this.fromBs32(buffer);
|
|
43
|
+
else
|
|
44
|
+
this.fromString(buffer);
|
|
45
|
+
}
|
|
46
|
+
else if (typeof buffer === 'object' && buffer !== null) {
|
|
47
|
+
this.create(buffer);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
// Explicitly reject all other types (number, boolean, undefined, symbol, function, null)
|
|
51
|
+
throw new TypeError(`Invalid input type for FormatInterface: ${typeof buffer}`);
|
|
52
|
+
}
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
hasCodec() {
|
|
56
|
+
if (!this.encoded)
|
|
57
|
+
return false;
|
|
58
|
+
const codec$1 = new codec(this.encoded);
|
|
59
|
+
if (codec$1.name)
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
decode(encoded) {
|
|
63
|
+
encoded = encoded || this.encoded;
|
|
64
|
+
const codec$1 = new codec(encoded);
|
|
65
|
+
if (codec$1.codecBuffer) {
|
|
66
|
+
encoded = encoded.slice(codec$1.codecBuffer.length);
|
|
67
|
+
this.name = codec$1.name;
|
|
68
|
+
this.decoded = this.protoDecode(encoded);
|
|
69
|
+
// try {
|
|
70
|
+
// this.decoded = JSON.parse(this.decoded)
|
|
71
|
+
// } catch {
|
|
72
|
+
// }
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
throw new Error(`no codec found`);
|
|
76
|
+
}
|
|
77
|
+
return this.decoded;
|
|
78
|
+
}
|
|
79
|
+
encode(decoded) {
|
|
80
|
+
let encoded;
|
|
81
|
+
decoded = decoded || this.decoded;
|
|
82
|
+
const codec$1 = new codec(this.name);
|
|
83
|
+
if (decoded instanceof Uint8Array)
|
|
84
|
+
encoded = decoded;
|
|
85
|
+
else
|
|
86
|
+
encoded = this.protoEncode(decoded);
|
|
87
|
+
if (codec$1.codecBuffer) {
|
|
88
|
+
const uint8Array = new Uint8Array(encoded.length + codec$1.codecBuffer.length);
|
|
89
|
+
uint8Array.set(codec$1.codecBuffer);
|
|
90
|
+
uint8Array.set(encoded, codec$1.codecBuffer.length);
|
|
91
|
+
this.encoded = uint8Array;
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
throw new Error(`invalid codec`);
|
|
95
|
+
}
|
|
96
|
+
return this.encoded;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
100
|
+
* @param {Object} proto - {protoObject}
|
|
101
|
+
* @param {Object} options - {hashFormat, name}
|
|
102
|
+
*/
|
|
103
|
+
constructor(buffer, proto, options) {
|
|
104
|
+
super();
|
|
105
|
+
this.proto = proto;
|
|
106
|
+
this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
|
|
107
|
+
if (options?.name)
|
|
108
|
+
this.name = options.name;
|
|
109
|
+
this.init(buffer);
|
|
110
|
+
}
|
|
111
|
+
get format() {
|
|
112
|
+
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
113
|
+
return `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
114
|
+
}
|
|
115
|
+
beforeHashing(decoded) {
|
|
116
|
+
// Avoid copying if not needed
|
|
117
|
+
if (decoded && Object.prototype.hasOwnProperty.call(decoded, 'hash')) {
|
|
118
|
+
// Only copy if hash is present
|
|
119
|
+
const rest = { ...decoded };
|
|
120
|
+
delete rest.hash;
|
|
121
|
+
return rest;
|
|
122
|
+
}
|
|
123
|
+
return decoded;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* @return {PeernetHash}
|
|
127
|
+
*/
|
|
128
|
+
get peernetHash() {
|
|
129
|
+
const decoded = this.beforeHashing({ ...this.decoded });
|
|
130
|
+
// @ts-ignore
|
|
131
|
+
return new codecHash(decoded, { name: this.name });
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* @return {peernetHash}
|
|
135
|
+
*/
|
|
136
|
+
async hash() {
|
|
137
|
+
if (this.#hash)
|
|
138
|
+
return this.#hash;
|
|
139
|
+
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
140
|
+
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
141
|
+
this.#hash = (await this.peernetHash)[`to${format}`]();
|
|
142
|
+
return this.#hash;
|
|
143
|
+
}
|
|
144
|
+
fromUint8Array(buffer) {
|
|
145
|
+
this.encoded = buffer;
|
|
146
|
+
return this.hasCodec()
|
|
147
|
+
? this.decode()
|
|
148
|
+
: this.create(JSON.parse(basicInterface.default._textDecoder.decode(this.encoded), basicInterface.jsonParseBigInt));
|
|
149
|
+
}
|
|
150
|
+
fromArrayBuffer(buffer) {
|
|
151
|
+
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
152
|
+
return this.hasCodec()
|
|
153
|
+
? this.decode()
|
|
154
|
+
: this.create(JSON.parse(basicInterface.default._textDecoder.decode(this.encoded), basicInterface.jsonParseBigInt));
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* @param {Object} data
|
|
158
|
+
*/
|
|
159
|
+
create(data) {
|
|
160
|
+
const decoded = {};
|
|
161
|
+
// @ts-ignore
|
|
162
|
+
if (data.hash)
|
|
163
|
+
this.#hash = data.hash;
|
|
164
|
+
if (this.keys?.length > 0) {
|
|
165
|
+
for (const key of this.keys) {
|
|
166
|
+
decoded[key] = data[key];
|
|
167
|
+
}
|
|
168
|
+
this.decoded = decoded;
|
|
169
|
+
// return this.encode(decoded)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
module.exports = FormatInterface;
|
|
175
|
+
//# sourceMappingURL=codec-format-interface.js.map
|