@leofcoin/codec-format-interface 1.7.14 → 1.8.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/README.md +35 -10
- package/exports/README.md +8 -0
- package/exports/basic-interface.d.ts +4 -0
- package/exports/basic-interface.js +68 -14
- package/exports/basic-interface.js.map +1 -0
- package/exports/cjs/basic-interface.js +185 -0
- package/exports/cjs/basic-interface.js.map +1 -0
- package/exports/cjs/codec-format-interface.js +197 -0
- package/exports/cjs/codec-format-interface.js.map +1 -0
- package/exports/cjs/codec-hash.js +147 -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 +41 -0
- package/exports/cjs/index.js.map +1 -0
- package/exports/codec-format-interface.d.ts +5 -1
- package/exports/codec-format-interface.js +46 -8
- package/exports/codec-format-interface.js.map +1 -0
- package/exports/codec-hash.d.ts +6 -6
- package/exports/codec-hash.js +54 -58
- 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 +14 -0
- package/exports/index.js.map +1 -0
- package/exports/json-interface.d.ts +1 -1
- package/package.json +4 -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,20 @@ 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 jsonStringifyBigInt = (key, value) =>
|
|
8
|
-
|
|
7
|
+
const jsonStringifyBigInt = (key, value) => {
|
|
8
|
+
if (typeof value === 'bigint')
|
|
9
|
+
return { $bigint: value.toString() };
|
|
10
|
+
return value;
|
|
11
|
+
};
|
|
12
|
+
const jsonParseBigInt = (key, value) => {
|
|
13
|
+
if (typeof value === 'object' && value) {
|
|
14
|
+
if (value.$bigint)
|
|
15
|
+
return BigInt(value.$bigint);
|
|
16
|
+
}
|
|
17
|
+
return value;
|
|
18
|
+
};
|
|
19
|
+
const _textEncoder = new TextEncoder();
|
|
20
|
+
const _textDecoder = new TextDecoder();
|
|
9
21
|
class BasicInterface {
|
|
10
22
|
#encoded;
|
|
11
23
|
#decoded;
|
|
@@ -40,20 +52,49 @@ class BasicInterface {
|
|
|
40
52
|
}
|
|
41
53
|
decode(encoded) {
|
|
42
54
|
encoded = encoded || this.encoded;
|
|
43
|
-
|
|
55
|
+
// Example: decode as JSON if possible (override in subclass)
|
|
56
|
+
try {
|
|
57
|
+
return JSON.parse(_textDecoder.decode(encoded), jsonParseBigInt);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return new Object();
|
|
61
|
+
}
|
|
44
62
|
}
|
|
45
63
|
encode(decoded) {
|
|
46
64
|
decoded = decoded || this.decoded;
|
|
47
|
-
|
|
65
|
+
// Example: encode as JSON (override in subclass)
|
|
66
|
+
return _textEncoder.encode(JSON.stringify(decoded, jsonStringifyBigInt));
|
|
48
67
|
}
|
|
49
68
|
// get Codec(): Codec {}
|
|
69
|
+
// Cache proto keys/values for reuse
|
|
70
|
+
static _protoCache = new WeakMap();
|
|
50
71
|
protoEncode(data) {
|
|
51
|
-
|
|
72
|
+
let cache = BasicInterface._protoCache.get(this.proto);
|
|
73
|
+
if (!cache) {
|
|
74
|
+
cache = {
|
|
75
|
+
keys: Object.keys(this.proto),
|
|
76
|
+
values: Object.values(this.proto)
|
|
77
|
+
};
|
|
78
|
+
BasicInterface._protoCache.set(this.proto, cache);
|
|
79
|
+
}
|
|
80
|
+
// Use proto.encode directly, but avoid new array allocations inside encode if possible
|
|
52
81
|
return proto.encode(this.proto, data, false);
|
|
53
82
|
}
|
|
54
83
|
protoDecode(data) {
|
|
55
|
-
//
|
|
56
|
-
|
|
84
|
+
// Use a static output object if possible (not thread-safe, but safe for single-threaded use)
|
|
85
|
+
if (!this._decodeOutput)
|
|
86
|
+
this._decodeOutput = {};
|
|
87
|
+
const result = proto.decode(this.proto, data, false);
|
|
88
|
+
// Copy properties to static object to avoid new allocations
|
|
89
|
+
Object.keys(result).forEach((k) => {
|
|
90
|
+
this._decodeOutput[k] = result[k];
|
|
91
|
+
});
|
|
92
|
+
// Remove any keys not in result
|
|
93
|
+
Object.keys(this._decodeOutput).forEach((k) => {
|
|
94
|
+
if (!(k in result))
|
|
95
|
+
delete this._decodeOutput[k];
|
|
96
|
+
});
|
|
97
|
+
return this._decodeOutput;
|
|
57
98
|
}
|
|
58
99
|
isHex(string) {
|
|
59
100
|
return isHex(string);
|
|
@@ -86,7 +127,10 @@ class BasicInterface {
|
|
|
86
127
|
return this.decode(fromHex(string));
|
|
87
128
|
}
|
|
88
129
|
fromArray(array) {
|
|
89
|
-
|
|
130
|
+
// Avoid unnecessary copy if already Uint8Array
|
|
131
|
+
if (array instanceof Uint8Array)
|
|
132
|
+
return this.decode(array);
|
|
133
|
+
return this.decode(Uint8Array.from(array));
|
|
90
134
|
}
|
|
91
135
|
fromEncoded(encoded) {
|
|
92
136
|
return this.decode(encoded);
|
|
@@ -94,15 +138,21 @@ class BasicInterface {
|
|
|
94
138
|
toString() {
|
|
95
139
|
if (!this.encoded)
|
|
96
140
|
this.encode();
|
|
97
|
-
|
|
141
|
+
// Use cached string if available
|
|
142
|
+
if (!this._string)
|
|
143
|
+
this._string = Array.prototype.join.call(this.encoded, ',');
|
|
144
|
+
return this._string;
|
|
98
145
|
}
|
|
99
146
|
toHex() {
|
|
100
147
|
if (!this.encoded)
|
|
101
148
|
this.encode();
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
.
|
|
105
|
-
|
|
149
|
+
// Use cached hex if available
|
|
150
|
+
if (!this._hex) {
|
|
151
|
+
if (!this._string)
|
|
152
|
+
this._string = Array.prototype.join.call(this.encoded, ',');
|
|
153
|
+
this._hex = toHex(this._string.split(',').map(Number));
|
|
154
|
+
}
|
|
155
|
+
return this._hex;
|
|
106
156
|
}
|
|
107
157
|
/**
|
|
108
158
|
* @return {String} encoded
|
|
@@ -110,7 +160,10 @@ class BasicInterface {
|
|
|
110
160
|
toBs32() {
|
|
111
161
|
if (!this.encoded)
|
|
112
162
|
this.encode();
|
|
113
|
-
|
|
163
|
+
// Use cached bs32 if available
|
|
164
|
+
if (!this._bs32)
|
|
165
|
+
this._bs32 = toBase32(this.encoded);
|
|
166
|
+
return this._bs32;
|
|
114
167
|
}
|
|
115
168
|
/**
|
|
116
169
|
* @return {String} encoded
|
|
@@ -123,3 +176,4 @@ class BasicInterface {
|
|
|
123
176
|
}
|
|
124
177
|
|
|
125
178
|
export { BasicInterface as default, jsonParseBigInt, jsonStringifyBigInt };
|
|
179
|
+
//# 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 return value\n}\n\nexport const jsonParseBigInt = (key, value) => {\n if (typeof value === 'object' && value) {\n if (value.$bigint) return BigInt(value.$bigint)\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":";;;;;;MAsCa,mBAAmB,GAAG,CAAC,GAAG,EAAE,KAAK,KAAI;IAChD,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,QAAQ,EAAE,EAAE;AACnE,IAAA,OAAO,KAAK;AACd;MAEa,eAAe,GAAG,CAAC,GAAG,EAAE,KAAK,KAAI;AAC5C,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,EAAE;QACtC,IAAI,KAAK,CAAC,OAAO;AAAE,YAAA,OAAO,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC;IACjD;AACA,IAAA,OAAO,KAAK;AACd;AAEA,MAAM,YAAY,GAAG,IAAI,WAAW,EAAE;AACtC,MAAM,YAAY,GAAG,IAAI,WAAW,EAAE;AAExB,MAAO,cAAc,CAAA;AACjC,IAAA,QAAQ;AACR,IAAA,QAAQ;AACR,IAAA,IAAI;AACJ,IAAA,MAAM;AAEN,IAAA,IAAI,IAAI,GAA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C,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,185 @@
|
|
|
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 jsonStringifyBigInt = (key, value) => {
|
|
12
|
+
if (typeof value === 'bigint')
|
|
13
|
+
return { $bigint: value.toString() };
|
|
14
|
+
return value;
|
|
15
|
+
};
|
|
16
|
+
const jsonParseBigInt = (key, value) => {
|
|
17
|
+
if (typeof value === 'object' && value) {
|
|
18
|
+
if (value.$bigint)
|
|
19
|
+
return BigInt(value.$bigint);
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
const _textEncoder = new TextEncoder();
|
|
24
|
+
const _textDecoder = new TextDecoder();
|
|
25
|
+
class BasicInterface {
|
|
26
|
+
#encoded;
|
|
27
|
+
#decoded;
|
|
28
|
+
name;
|
|
29
|
+
#proto;
|
|
30
|
+
get keys() {
|
|
31
|
+
// handles proto keys
|
|
32
|
+
// protokey -> key
|
|
33
|
+
return Object.keys(this.#proto).map((key) => (key.endsWith('?') ? key.split('?')[0] : key));
|
|
34
|
+
}
|
|
35
|
+
get encoded() {
|
|
36
|
+
if (!this.#encoded)
|
|
37
|
+
this.#encoded = this.encode();
|
|
38
|
+
return this.#encoded;
|
|
39
|
+
}
|
|
40
|
+
set encoded(value) {
|
|
41
|
+
this.#encoded = value;
|
|
42
|
+
}
|
|
43
|
+
get decoded() {
|
|
44
|
+
if (!this.#decoded)
|
|
45
|
+
this.#decoded = this.decode();
|
|
46
|
+
return this.#decoded;
|
|
47
|
+
}
|
|
48
|
+
set decoded(value) {
|
|
49
|
+
this.#decoded = value;
|
|
50
|
+
}
|
|
51
|
+
set proto(value) {
|
|
52
|
+
this.#proto = value;
|
|
53
|
+
}
|
|
54
|
+
get proto() {
|
|
55
|
+
return this.#proto;
|
|
56
|
+
}
|
|
57
|
+
decode(encoded) {
|
|
58
|
+
encoded = encoded || this.encoded;
|
|
59
|
+
// Example: decode as JSON if possible (override in subclass)
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(_textDecoder.decode(encoded), jsonParseBigInt);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return new Object();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
encode(decoded) {
|
|
68
|
+
decoded = decoded || this.decoded;
|
|
69
|
+
// Example: encode as JSON (override in subclass)
|
|
70
|
+
return _textEncoder.encode(JSON.stringify(decoded, jsonStringifyBigInt));
|
|
71
|
+
}
|
|
72
|
+
// get Codec(): Codec {}
|
|
73
|
+
// Cache proto keys/values for reuse
|
|
74
|
+
static _protoCache = new WeakMap();
|
|
75
|
+
protoEncode(data) {
|
|
76
|
+
let cache = BasicInterface._protoCache.get(this.proto);
|
|
77
|
+
if (!cache) {
|
|
78
|
+
cache = {
|
|
79
|
+
keys: Object.keys(this.proto),
|
|
80
|
+
values: Object.values(this.proto)
|
|
81
|
+
};
|
|
82
|
+
BasicInterface._protoCache.set(this.proto, cache);
|
|
83
|
+
}
|
|
84
|
+
// Use proto.encode directly, but avoid new array allocations inside encode if possible
|
|
85
|
+
return proto.encode(this.proto, data, false);
|
|
86
|
+
}
|
|
87
|
+
protoDecode(data) {
|
|
88
|
+
// Use a static output object if possible (not thread-safe, but safe for single-threaded use)
|
|
89
|
+
if (!this._decodeOutput)
|
|
90
|
+
this._decodeOutput = {};
|
|
91
|
+
const result = proto.decode(this.proto, data, false);
|
|
92
|
+
// Copy properties to static object to avoid new allocations
|
|
93
|
+
Object.keys(result).forEach((k) => {
|
|
94
|
+
this._decodeOutput[k] = result[k];
|
|
95
|
+
});
|
|
96
|
+
// Remove any keys not in result
|
|
97
|
+
Object.keys(this._decodeOutput).forEach((k) => {
|
|
98
|
+
if (!(k in result))
|
|
99
|
+
delete this._decodeOutput[k];
|
|
100
|
+
});
|
|
101
|
+
return this._decodeOutput;
|
|
102
|
+
}
|
|
103
|
+
isHex(string) {
|
|
104
|
+
return isHex(string);
|
|
105
|
+
}
|
|
106
|
+
isBase32(string) {
|
|
107
|
+
return base32.isBase32(string);
|
|
108
|
+
}
|
|
109
|
+
isBase58(string) {
|
|
110
|
+
return base58.isBase58(string);
|
|
111
|
+
}
|
|
112
|
+
fromBs32(encoded) {
|
|
113
|
+
return this.decode(base32.decode(encoded));
|
|
114
|
+
}
|
|
115
|
+
fromBs58(encoded) {
|
|
116
|
+
return this.decode(typedArrayUtils.fromBase58(encoded));
|
|
117
|
+
}
|
|
118
|
+
async toArray() {
|
|
119
|
+
const array = [];
|
|
120
|
+
for await (const value of this.encoded.values()) {
|
|
121
|
+
array.push(value);
|
|
122
|
+
}
|
|
123
|
+
return array;
|
|
124
|
+
}
|
|
125
|
+
fromString(string) {
|
|
126
|
+
const array = string.split(',');
|
|
127
|
+
const arrayLike = array.map((string) => Number(string));
|
|
128
|
+
return this.decode(Uint8Array.from(arrayLike));
|
|
129
|
+
}
|
|
130
|
+
fromHex(string) {
|
|
131
|
+
return this.decode(typedArrayUtils.fromHex(string));
|
|
132
|
+
}
|
|
133
|
+
fromArray(array) {
|
|
134
|
+
// Avoid unnecessary copy if already Uint8Array
|
|
135
|
+
if (array instanceof Uint8Array)
|
|
136
|
+
return this.decode(array);
|
|
137
|
+
return this.decode(Uint8Array.from(array));
|
|
138
|
+
}
|
|
139
|
+
fromEncoded(encoded) {
|
|
140
|
+
return this.decode(encoded);
|
|
141
|
+
}
|
|
142
|
+
toString() {
|
|
143
|
+
if (!this.encoded)
|
|
144
|
+
this.encode();
|
|
145
|
+
// Use cached string if available
|
|
146
|
+
if (!this._string)
|
|
147
|
+
this._string = Array.prototype.join.call(this.encoded, ',');
|
|
148
|
+
return this._string;
|
|
149
|
+
}
|
|
150
|
+
toHex() {
|
|
151
|
+
if (!this.encoded)
|
|
152
|
+
this.encode();
|
|
153
|
+
// Use cached hex if available
|
|
154
|
+
if (!this._hex) {
|
|
155
|
+
if (!this._string)
|
|
156
|
+
this._string = Array.prototype.join.call(this.encoded, ',');
|
|
157
|
+
this._hex = typedArrayUtils.toHex(this._string.split(',').map(Number));
|
|
158
|
+
}
|
|
159
|
+
return this._hex;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* @return {String} encoded
|
|
163
|
+
*/
|
|
164
|
+
toBs32() {
|
|
165
|
+
if (!this.encoded)
|
|
166
|
+
this.encode();
|
|
167
|
+
// Use cached bs32 if available
|
|
168
|
+
if (!this._bs32)
|
|
169
|
+
this._bs32 = typedArrayUtils.toBase32(this.encoded);
|
|
170
|
+
return this._bs32;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* @return {String} encoded
|
|
174
|
+
*/
|
|
175
|
+
toBs58() {
|
|
176
|
+
if (!this.encoded)
|
|
177
|
+
this.encode();
|
|
178
|
+
return typedArrayUtils.toBase58(this.encoded);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
exports.default = BasicInterface;
|
|
183
|
+
exports.jsonParseBigInt = jsonParseBigInt;
|
|
184
|
+
exports.jsonStringifyBigInt = jsonStringifyBigInt;
|
|
185
|
+
//# 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 return value\n}\n\nexport const jsonParseBigInt = (key, value) => {\n if (typeof value === 'object' && value) {\n if (value.$bigint) return BigInt(value.$bigint)\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":";;;;;;;;;;MAsCa,mBAAmB,GAAG,CAAC,GAAG,EAAE,KAAK,KAAI;IAChD,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,QAAQ,EAAE,EAAE;AACnE,IAAA,OAAO,KAAK;AACd;MAEa,eAAe,GAAG,CAAC,GAAG,EAAE,KAAK,KAAI;AAC5C,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,EAAE;QACtC,IAAI,KAAK,CAAC,OAAO;AAAE,YAAA,OAAO,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC;IACjD;AACA,IAAA,OAAO,KAAK;AACd;AAEA,MAAM,YAAY,GAAG,IAAI,WAAW,EAAE;AACtC,MAAM,YAAY,GAAG,IAAI,WAAW,EAAE;AAExB,MAAO,cAAc,CAAA;AACjC,IAAA,QAAQ;AACR,IAAA,QAAQ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AI,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,197 @@
|
|
|
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('hash-wasm');
|
|
14
|
+
|
|
15
|
+
class FormatInterface extends basicInterface.default {
|
|
16
|
+
hashFormat;
|
|
17
|
+
#hash;
|
|
18
|
+
#encoded;
|
|
19
|
+
get encoded() {
|
|
20
|
+
return this.#encoded || this.encode();
|
|
21
|
+
}
|
|
22
|
+
set encoded(value) {
|
|
23
|
+
this.#encoded = value;
|
|
24
|
+
}
|
|
25
|
+
init(buffer) {
|
|
26
|
+
if (buffer instanceof FormatInterface && buffer?.name === this.name) {
|
|
27
|
+
return buffer;
|
|
28
|
+
}
|
|
29
|
+
else if (buffer instanceof Uint8Array) {
|
|
30
|
+
this.fromUint8Array(buffer);
|
|
31
|
+
}
|
|
32
|
+
else if (buffer instanceof ArrayBuffer) {
|
|
33
|
+
this.fromArrayBuffer(buffer);
|
|
34
|
+
}
|
|
35
|
+
else if (typeof buffer === 'string') {
|
|
36
|
+
if (this.isHex(buffer))
|
|
37
|
+
this.fromHex(buffer);
|
|
38
|
+
else if (this.isBase58(buffer))
|
|
39
|
+
this.fromBs58(buffer);
|
|
40
|
+
else if (this.isBase32(buffer))
|
|
41
|
+
this.fromBs32(buffer);
|
|
42
|
+
else
|
|
43
|
+
this.fromString(buffer);
|
|
44
|
+
}
|
|
45
|
+
else if (typeof buffer === 'object' && buffer !== null) {
|
|
46
|
+
this.create(buffer);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// Explicitly reject all other types (number, boolean, undefined, symbol, function, null)
|
|
50
|
+
throw new TypeError(`Invalid input type for FormatInterface: ${typeof buffer}`);
|
|
51
|
+
}
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
hasCodec() {
|
|
55
|
+
if (!this.encoded)
|
|
56
|
+
return false;
|
|
57
|
+
const codec$1 = new codec(this.encoded);
|
|
58
|
+
if (codec$1.name)
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
decode(encoded) {
|
|
62
|
+
encoded = encoded || this.encoded;
|
|
63
|
+
const codec$1 = new codec(encoded);
|
|
64
|
+
if (codec$1.codecBuffer) {
|
|
65
|
+
encoded = encoded.slice(codec$1.codecBuffer.length);
|
|
66
|
+
this.name = codec$1.name;
|
|
67
|
+
this.decoded = this.protoDecode(encoded);
|
|
68
|
+
// try {
|
|
69
|
+
// this.decoded = JSON.parse(this.decoded)
|
|
70
|
+
// } catch {
|
|
71
|
+
// }
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
throw new Error(`no codec found`);
|
|
75
|
+
}
|
|
76
|
+
return this.decoded;
|
|
77
|
+
}
|
|
78
|
+
encode(decoded) {
|
|
79
|
+
let encoded;
|
|
80
|
+
decoded = decoded || this.decoded;
|
|
81
|
+
const codec$1 = new codec(this.name);
|
|
82
|
+
if (decoded instanceof Uint8Array)
|
|
83
|
+
encoded = decoded;
|
|
84
|
+
else
|
|
85
|
+
encoded = this.protoEncode(decoded);
|
|
86
|
+
if (codec$1.codecBuffer) {
|
|
87
|
+
const uint8Array = new Uint8Array(encoded.length + codec$1.codecBuffer.length);
|
|
88
|
+
uint8Array.set(codec$1.codecBuffer);
|
|
89
|
+
uint8Array.set(encoded, codec$1.codecBuffer.length);
|
|
90
|
+
this.encoded = uint8Array;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
throw new Error(`invalid codec`);
|
|
94
|
+
}
|
|
95
|
+
return this.encoded;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* @param {Buffer|String|Object} buffer - data - The data needed to create the desired message
|
|
99
|
+
* @param {Object} proto - {protoObject}
|
|
100
|
+
* @param {Object} options - {hashFormat, name}
|
|
101
|
+
*/
|
|
102
|
+
constructor(buffer, proto, options) {
|
|
103
|
+
super();
|
|
104
|
+
this.proto = proto;
|
|
105
|
+
this.hashFormat = options?.hashFormat ? options.hashFormat : 'bs32';
|
|
106
|
+
if (options?.name)
|
|
107
|
+
this.name = options.name;
|
|
108
|
+
this.init(buffer);
|
|
109
|
+
}
|
|
110
|
+
get format() {
|
|
111
|
+
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
112
|
+
return `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Extract content bytes without codec prefix
|
|
116
|
+
*/
|
|
117
|
+
get contentBytes() {
|
|
118
|
+
if (!this.encoded)
|
|
119
|
+
return new Uint8Array(0);
|
|
120
|
+
const codec$1 = new codec(this.encoded);
|
|
121
|
+
if (codec$1.codecBuffer) {
|
|
122
|
+
return this.encoded.slice(codec$1.codecBuffer.length);
|
|
123
|
+
}
|
|
124
|
+
return this.encoded;
|
|
125
|
+
}
|
|
126
|
+
beforeHashing(decoded) {
|
|
127
|
+
// Avoid copying if not needed
|
|
128
|
+
if (decoded && Object.prototype.hasOwnProperty.call(decoded, 'hash')) {
|
|
129
|
+
// Only copy if hash is present
|
|
130
|
+
const rest = { ...decoded };
|
|
131
|
+
delete rest.hash;
|
|
132
|
+
return rest;
|
|
133
|
+
}
|
|
134
|
+
return decoded;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* @return {PeernetHash}
|
|
138
|
+
*/
|
|
139
|
+
get peernetHash() {
|
|
140
|
+
// Optimize: if decoded has no hash property, contentBytes are already correct
|
|
141
|
+
// Since protoEncode is deterministic (varint-based), we can use them directly
|
|
142
|
+
if (this.encoded &&
|
|
143
|
+
this.decoded &&
|
|
144
|
+
!Object.prototype.hasOwnProperty.call(this.decoded, 'hash')) {
|
|
145
|
+
// @ts-ignore
|
|
146
|
+
const hash = new codecHash({ name: this.name });
|
|
147
|
+
return hash.init(this.contentBytes);
|
|
148
|
+
}
|
|
149
|
+
// Fallback: must re-encode without hash property
|
|
150
|
+
const decoded = this.beforeHashing({ ...this.decoded });
|
|
151
|
+
// @ts-ignore
|
|
152
|
+
const hash = new codecHash({ name: this.name });
|
|
153
|
+
return hash.init(decoded);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* @return {peernetHash}
|
|
157
|
+
*/
|
|
158
|
+
async hash() {
|
|
159
|
+
if (this.#hash)
|
|
160
|
+
return this.#hash;
|
|
161
|
+
const upper = this.hashFormat.charAt(0).toUpperCase();
|
|
162
|
+
const format = `${upper}${this.hashFormat.substring(1, this.hashFormat.length)}`;
|
|
163
|
+
this.#hash = (await this.peernetHash)[`to${format}`]();
|
|
164
|
+
return this.#hash;
|
|
165
|
+
}
|
|
166
|
+
fromUint8Array(buffer) {
|
|
167
|
+
this.encoded = buffer;
|
|
168
|
+
return this.hasCodec()
|
|
169
|
+
? this.decode()
|
|
170
|
+
: this.create(JSON.parse(basicInterface.default._textDecoder.decode(this.encoded), basicInterface.jsonParseBigInt));
|
|
171
|
+
}
|
|
172
|
+
fromArrayBuffer(buffer) {
|
|
173
|
+
this.encoded = new Uint8Array(buffer, buffer.byteOffset, buffer.byteLength);
|
|
174
|
+
return this.hasCodec()
|
|
175
|
+
? this.decode()
|
|
176
|
+
: this.create(JSON.parse(basicInterface.default._textDecoder.decode(this.encoded), basicInterface.jsonParseBigInt));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* @param {Object} data
|
|
180
|
+
*/
|
|
181
|
+
create(data) {
|
|
182
|
+
const decoded = {};
|
|
183
|
+
// @ts-ignore
|
|
184
|
+
if (data.hash)
|
|
185
|
+
this.#hash = data.hash;
|
|
186
|
+
if (this.keys?.length > 0) {
|
|
187
|
+
for (const key of this.keys) {
|
|
188
|
+
decoded[key] = data[key];
|
|
189
|
+
}
|
|
190
|
+
this.decoded = decoded;
|
|
191
|
+
// return this.encode(decoded)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
module.exports = FormatInterface;
|
|
197
|
+
//# sourceMappingURL=codec-format-interface.js.map
|