@aztec/foundation 0.8.7 → 0.8.9
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/dest/index.d.ts +27 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +28 -0
- package/dest/json-rpc/client/json_rpc_client.js +3 -3
- package/dest/log/logger.js +6 -4
- package/dest/serialize/free_funcs.d.ts +5 -19
- package/dest/serialize/free_funcs.d.ts.map +1 -1
- package/dest/serialize/free_funcs.js +6 -21
- package/dest/serialize/index.d.ts +0 -2
- package/dest/serialize/index.d.ts.map +1 -1
- package/dest/serialize/index.js +1 -3
- package/package.json +3 -2
- package/src/index.ts +27 -0
- package/src/json-rpc/client/json_rpc_client.ts +2 -2
- package/src/log/logger.ts +4 -3
- package/src/serialize/free_funcs.ts +5 -21
- package/src/serialize/index.ts +0 -2
- package/dest/serialize/deserializer.d.ts +0 -134
- package/dest/serialize/deserializer.d.ts.map +0 -1
- package/dest/serialize/deserializer.js +0 -145
- package/dest/serialize/serializer.d.ts +0 -89
- package/dest/serialize/serializer.d.ts.map +0 -1
- package/dest/serialize/serializer.js +0 -111
- package/src/serialize/deserializer.ts +0 -180
- package/src/serialize/serializer.ts +0 -128
package/dest/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export * as abi from './abi/index.js';
|
|
2
|
+
export * as asyncMap from './async-map/index.js';
|
|
3
|
+
export * as aztecAddress from './aztec-address/index.js';
|
|
4
|
+
export * as bigintBuffer from './bigint-buffer/index.js';
|
|
5
|
+
export * as collection from './collection/index.js';
|
|
6
|
+
export * as committable from './committable/index.js';
|
|
7
|
+
export * as crypto from './crypto/index.js';
|
|
8
|
+
export * as errors from './errors/index.js';
|
|
9
|
+
export * as ethAddress from './eth-address/index.js';
|
|
10
|
+
export * as fields from './fields/index.js';
|
|
11
|
+
export * as fifo from './fifo/index.js';
|
|
12
|
+
export * as jsonRpc from './json-rpc/index.js';
|
|
13
|
+
export * as jsonRpcClient from './json-rpc/client/index.js';
|
|
14
|
+
export * as jsonRpcServer from './json-rpc/server/index.js';
|
|
15
|
+
export * as log from './log/index.js';
|
|
16
|
+
export * as mutex from './mutex/index.js';
|
|
17
|
+
export * as retry from './retry/index.js';
|
|
18
|
+
export * as runningPromise from './running-promise/index.js';
|
|
19
|
+
export * as serialize from './serialize/index.js';
|
|
20
|
+
export * as sleep from './sleep/index.js';
|
|
21
|
+
export * as timer from './timer/index.js';
|
|
22
|
+
export * as transport from './transport/index.js';
|
|
23
|
+
export * as types from './types/index.js';
|
|
24
|
+
export * as url from './url/index.js';
|
|
25
|
+
export * as wasm from './wasm/index.js';
|
|
26
|
+
export * as worker from './worker/index.js';
|
|
27
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,QAAQ,MAAM,sBAAsB,CAAC;AACjD,OAAO,KAAK,YAAY,MAAM,0BAA0B,CAAC;AACzD,OAAO,KAAK,YAAY,MAAM,0BAA0B,CAAC;AACzD,OAAO,KAAK,UAAU,MAAM,uBAAuB,CAAC;AACpD,OAAO,KAAK,WAAW,MAAM,wBAAwB,CAAC;AACtD,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC;AAC5C,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC;AAC5C,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AACrD,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC;AAC5C,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAC;AACxC,OAAO,KAAK,OAAO,MAAM,qBAAqB,CAAC;AAC/C,OAAO,KAAK,aAAa,MAAM,4BAA4B,CAAC;AAC5D,OAAO,KAAK,aAAa,MAAM,4BAA4B,CAAC;AAC5D,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,KAAK,MAAM,kBAAkB,CAAC;AAC1C,OAAO,KAAK,KAAK,MAAM,kBAAkB,CAAC;AAC1C,OAAO,KAAK,cAAc,MAAM,4BAA4B,CAAC;AAC7D,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAC;AAClD,OAAO,KAAK,KAAK,MAAM,kBAAkB,CAAC;AAC1C,OAAO,KAAK,KAAK,MAAM,kBAAkB,CAAC;AAC1C,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAC;AAClD,OAAO,KAAK,KAAK,MAAM,kBAAkB,CAAC;AAC1C,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAC;AACxC,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC"}
|
package/dest/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Reexport all folders at the root for packages targeting CommonJS
|
|
2
|
+
export * as abi from './abi/index.js';
|
|
3
|
+
export * as asyncMap from './async-map/index.js';
|
|
4
|
+
export * as aztecAddress from './aztec-address/index.js';
|
|
5
|
+
export * as bigintBuffer from './bigint-buffer/index.js';
|
|
6
|
+
export * as collection from './collection/index.js';
|
|
7
|
+
export * as committable from './committable/index.js';
|
|
8
|
+
export * as crypto from './crypto/index.js';
|
|
9
|
+
export * as errors from './errors/index.js';
|
|
10
|
+
export * as ethAddress from './eth-address/index.js';
|
|
11
|
+
export * as fields from './fields/index.js';
|
|
12
|
+
export * as fifo from './fifo/index.js';
|
|
13
|
+
export * as jsonRpc from './json-rpc/index.js';
|
|
14
|
+
export * as jsonRpcClient from './json-rpc/client/index.js';
|
|
15
|
+
export * as jsonRpcServer from './json-rpc/server/index.js';
|
|
16
|
+
export * as log from './log/index.js';
|
|
17
|
+
export * as mutex from './mutex/index.js';
|
|
18
|
+
export * as retry from './retry/index.js';
|
|
19
|
+
export * as runningPromise from './running-promise/index.js';
|
|
20
|
+
export * as serialize from './serialize/index.js';
|
|
21
|
+
export * as sleep from './sleep/index.js';
|
|
22
|
+
export * as timer from './timer/index.js';
|
|
23
|
+
export * as transport from './transport/index.js';
|
|
24
|
+
export * as types from './types/index.js';
|
|
25
|
+
export * as url from './url/index.js';
|
|
26
|
+
export * as wasm from './wasm/index.js';
|
|
27
|
+
export * as worker from './worker/index.js';
|
|
28
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsbUVBQW1FO0FBQ25FLE9BQU8sS0FBSyxHQUFHLE1BQU0sZ0JBQWdCLENBQUM7QUFDdEMsT0FBTyxLQUFLLFFBQVEsTUFBTSxzQkFBc0IsQ0FBQztBQUNqRCxPQUFPLEtBQUssWUFBWSxNQUFNLDBCQUEwQixDQUFDO0FBQ3pELE9BQU8sS0FBSyxZQUFZLE1BQU0sMEJBQTBCLENBQUM7QUFDekQsT0FBTyxLQUFLLFVBQVUsTUFBTSx1QkFBdUIsQ0FBQztBQUNwRCxPQUFPLEtBQUssV0FBVyxNQUFNLHdCQUF3QixDQUFDO0FBQ3RELE9BQU8sS0FBSyxNQUFNLE1BQU0sbUJBQW1CLENBQUM7QUFDNUMsT0FBTyxLQUFLLE1BQU0sTUFBTSxtQkFBbUIsQ0FBQztBQUM1QyxPQUFPLEtBQUssVUFBVSxNQUFNLHdCQUF3QixDQUFDO0FBQ3JELE9BQU8sS0FBSyxNQUFNLE1BQU0sbUJBQW1CLENBQUM7QUFDNUMsT0FBTyxLQUFLLElBQUksTUFBTSxpQkFBaUIsQ0FBQztBQUN4QyxPQUFPLEtBQUssT0FBTyxNQUFNLHFCQUFxQixDQUFDO0FBQy9DLE9BQU8sS0FBSyxhQUFhLE1BQU0sNEJBQTRCLENBQUM7QUFDNUQsT0FBTyxLQUFLLGFBQWEsTUFBTSw0QkFBNEIsQ0FBQztBQUM1RCxPQUFPLEtBQUssR0FBRyxNQUFNLGdCQUFnQixDQUFDO0FBQ3RDLE9BQU8sS0FBSyxLQUFLLE1BQU0sa0JBQWtCLENBQUM7QUFDMUMsT0FBTyxLQUFLLEtBQUssTUFBTSxrQkFBa0IsQ0FBQztBQUMxQyxPQUFPLEtBQUssY0FBYyxNQUFNLDRCQUE0QixDQUFDO0FBQzdELE9BQU8sS0FBSyxTQUFTLE1BQU0sc0JBQXNCLENBQUM7QUFDbEQsT0FBTyxLQUFLLEtBQUssTUFBTSxrQkFBa0IsQ0FBQztBQUMxQyxPQUFPLEtBQUssS0FBSyxNQUFNLGtCQUFrQixDQUFDO0FBQzFDLE9BQU8sS0FBSyxTQUFTLE1BQU0sc0JBQXNCLENBQUM7QUFDbEQsT0FBTyxLQUFLLEtBQUssTUFBTSxrQkFBa0IsQ0FBQztBQUMxQyxPQUFPLEtBQUssR0FBRyxNQUFNLGdCQUFnQixDQUFDO0FBQ3RDLE9BQU8sS0FBSyxJQUFJLE1BQU0saUJBQWlCLENBQUM7QUFDeEMsT0FBTyxLQUFLLE1BQU0sTUFBTSxtQkFBbUIsQ0FBQyJ9
|
|
@@ -44,10 +44,10 @@ export async function defaultFetch(host, rpcMethod, body, useApiEndpoints, noRet
|
|
|
44
44
|
}
|
|
45
45
|
if (!resp.ok) {
|
|
46
46
|
if (noRetry) {
|
|
47
|
-
throw new NoRetryError(responseJson.error.message);
|
|
47
|
+
throw new NoRetryError('(JSON-RPC PROPAGATED) ' + responseJson.error.message);
|
|
48
48
|
}
|
|
49
49
|
else {
|
|
50
|
-
throw new Error(responseJson.error.message);
|
|
50
|
+
throw new Error('(JSON-RPC PROPAGATED) ' + responseJson.error.message);
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
return responseJson;
|
|
@@ -102,4 +102,4 @@ export function createJsonRpcClient(host, stringClassMap, objectClassMap, useApi
|
|
|
102
102
|
},
|
|
103
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sRUFBRSxTQUFTLENBQUMsQ0FBQztZQUNqRixPQUFPLENBQUMsR0FBRyxNQUFhLEVBQUUsRUFBRTtnQkFDMUIsS0FBSyxDQUFDLE1BQU0sQ0FBQywyQkFBMkIsRUFBRSxPQUFPLEVBQUUsU0FBUyxFQUFFLElBQUksRUFBRSxNQUFNLENBQUMsQ0FBQyxDQUFDO2dCQUM3RSxPQUFPLE9BQU8sQ0FBQyxTQUFTLEVBQUUsTUFBTSxDQUFDLENBQUM7WUFDcEMsQ0FBQyxDQUFDO1FBQ0osQ0FBQztLQUNGLENBQ2lCLENBQUM7QUFDdkIsQ0FBQyJ9
|
package/dest/log/logger.js
CHANGED
|
@@ -45,12 +45,14 @@ function logWithDebug(debug, level, msg, data) {
|
|
|
45
45
|
for (const handler of logHandlers) {
|
|
46
46
|
handler(level, debug.namespace, msg, data);
|
|
47
47
|
}
|
|
48
|
-
|
|
48
|
+
msg = data ? `${msg} ${fmtLogData(data)}` : msg;
|
|
49
49
|
if (debug.enabled) {
|
|
50
|
-
debug
|
|
50
|
+
if (level !== 'debug')
|
|
51
|
+
msg = `${level.toUpperCase()} ${msg}`;
|
|
52
|
+
debug(msg);
|
|
51
53
|
}
|
|
52
54
|
else if (LogLevels.indexOf(level) <= LogLevels.indexOf(currentLevel)) {
|
|
53
|
-
printLog(`${getPrefix(debug, level)} ${
|
|
55
|
+
printLog(`${getPrefix(debug, level)} ${msg}`);
|
|
54
56
|
}
|
|
55
57
|
}
|
|
56
58
|
/**
|
|
@@ -95,4 +97,4 @@ function fmtLogData(data) {
|
|
|
95
97
|
.map(([key, value]) => `${key}=${value}`)
|
|
96
98
|
.join(' ');
|
|
97
99
|
}
|
|
98
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
100
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG9nZ2VyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2xvZy9sb2d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QyxDQUFDLEdBQUcsQ0FBQztBQUNoRCxDQUFDO0FBRUQ7OztHQUdHO0FBQ0gsU0FBUyxVQUFVLENBQUMsSUFBYztJQUNoQyxPQUFPLE1BQU0sQ0FBQyxPQUFPLENBQUMsSUFBSSxJQUFJLEVBQUUsQ0FBQztTQUM5QixHQUFHLENBQUMsQ0FBQyxDQUFDLEdBQUcsRUFBRSxLQUFLLENBQUMsRUFBRSxFQUFFLENBQUMsR0FBRyxHQUFHLElBQUksS0FBSyxFQUFFLENBQUM7U0FDeEMsSUFBSSxDQUFDLEdBQUcsQ0FBQyxDQUFDO0FBQ2YsQ0FBQyJ9
|
|
@@ -52,14 +52,11 @@ export declare function numToInt32BE(n: number, bufferSize?: number): Buffer;
|
|
|
52
52
|
*/
|
|
53
53
|
export declare function numToUInt8(n: number): Buffer;
|
|
54
54
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* @param buf - The input Buffer to be serialized into a vector format.
|
|
60
|
-
* @returns A Buffer containing the serialized vector with the encoded length header.
|
|
55
|
+
* Adds a 4-byte byte-length prefix to a buffer.
|
|
56
|
+
* @param buf - The input Buffer to be prefixed
|
|
57
|
+
* @returns A Buffer with 4-byte byte-length prefix.
|
|
61
58
|
*/
|
|
62
|
-
export declare function
|
|
59
|
+
export declare function prefixBufferWithLength(buf: Buffer): Buffer;
|
|
63
60
|
/**
|
|
64
61
|
* Serialize a BigInt value into a Buffer of specified width.
|
|
65
62
|
* The function converts the input BigInt into its big-endian representation and stores it in a Buffer of the given width.
|
|
@@ -93,18 +90,6 @@ export declare function deserializeBigInt(buf: Buffer, offset?: number, width?:
|
|
|
93
90
|
* @returns A Buffer containing the serialized timestamp of the input Date object.
|
|
94
91
|
*/
|
|
95
92
|
export declare function serializeDate(date: Date): Buffer;
|
|
96
|
-
/**
|
|
97
|
-
* Deserialize a buffer from a vector by reading the length from its first 4 bytes, and then extracting the contents of the buffer.
|
|
98
|
-
* The function returns an object containing the deserialized buffer as 'elem' and the number of bytes advanced ('adv') after deserialization.
|
|
99
|
-
*
|
|
100
|
-
* @param vector - The input buffer containing the serialized vector.
|
|
101
|
-
* @param offset - The starting position from where the deserialization should begin (default is 0).
|
|
102
|
-
* @returns An object with the deserialized buffer as 'elem' and the number of bytes advanced ('adv') after deserialization.
|
|
103
|
-
*/
|
|
104
|
-
export declare function deserializeBufferFromVector(vector: Buffer, offset?: number): {
|
|
105
|
-
elem: Buffer;
|
|
106
|
-
adv: number;
|
|
107
|
-
};
|
|
108
93
|
/**
|
|
109
94
|
* Deserialize a boolean value from a given buffer at the specified offset.
|
|
110
95
|
* Reads a single byte at the provided offset in the buffer and returns
|
|
@@ -176,6 +161,7 @@ export declare function serializeBufferArrayToVector(arr: Buffer[]): Buffer;
|
|
|
176
161
|
* @param vector - The input buffer containing the serialized array.
|
|
177
162
|
* @param offset - An optional starting position in the buffer for deserializing the array.
|
|
178
163
|
* @returns An object containing the deserialized array and the total number of bytes used during deserialization (adv).
|
|
164
|
+
* @deprecated User BufferReader instead.
|
|
179
165
|
*/
|
|
180
166
|
export declare function deserializeArrayFromVector<T>(deserialize: (buf: Buffer, offset: number) => {
|
|
181
167
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"free_funcs.d.ts","sourceRoot":"","sources":["../../src/serialize/free_funcs.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAE9C;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,OAAO,UAIpC;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,UAAU,SAAI,UAItD;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,UAAU,SAAI,UAItD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,EAAE,UAA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;EAGvD;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,SAAI;;;EAGvD;AAED;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAAC,GAAG,EAAE,MAAM,EAAE,UAIzD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,EAC1C,WAAW,EAAE,CACX,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,MAAM,KACX;IACH;;OAEG;IACH,IAAI,EAAE,CAAC,CAAC;IACR;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;CACb,EACD,MAAM,EAAE,MAAM,EACd,MAAM,SAAI;;;EAYX;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAM5C;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,CAW/C"}
|
|
@@ -73,14 +73,11 @@ export function numToUInt8(n) {
|
|
|
73
73
|
return buf;
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
* @param buf - The input Buffer to be serialized into a vector format.
|
|
81
|
-
* @returns A Buffer containing the serialized vector with the encoded length header.
|
|
76
|
+
* Adds a 4-byte byte-length prefix to a buffer.
|
|
77
|
+
* @param buf - The input Buffer to be prefixed
|
|
78
|
+
* @returns A Buffer with 4-byte byte-length prefix.
|
|
82
79
|
*/
|
|
83
|
-
export function
|
|
80
|
+
export function prefixBufferWithLength(buf) {
|
|
84
81
|
const lengthBuf = Buffer.alloc(4);
|
|
85
82
|
lengthBuf.writeUInt32BE(buf.length, 0);
|
|
86
83
|
return Buffer.concat([lengthBuf, buf]);
|
|
@@ -121,19 +118,6 @@ export function deserializeBigInt(buf, offset = 0, width = 32) {
|
|
|
121
118
|
export function serializeDate(date) {
|
|
122
119
|
return serializeBigInt(BigInt(date.getTime()), 8);
|
|
123
120
|
}
|
|
124
|
-
/**
|
|
125
|
-
* Deserialize a buffer from a vector by reading the length from its first 4 bytes, and then extracting the contents of the buffer.
|
|
126
|
-
* The function returns an object containing the deserialized buffer as 'elem' and the number of bytes advanced ('adv') after deserialization.
|
|
127
|
-
*
|
|
128
|
-
* @param vector - The input buffer containing the serialized vector.
|
|
129
|
-
* @param offset - The starting position from where the deserialization should begin (default is 0).
|
|
130
|
-
* @returns An object with the deserialized buffer as 'elem' and the number of bytes advanced ('adv') after deserialization.
|
|
131
|
-
*/
|
|
132
|
-
export function deserializeBufferFromVector(vector, offset = 0) {
|
|
133
|
-
const length = vector.readUInt32BE(offset);
|
|
134
|
-
const adv = 4 + length;
|
|
135
|
-
return { elem: vector.subarray(offset + 4, offset + adv), adv };
|
|
136
|
-
}
|
|
137
121
|
/**
|
|
138
122
|
* Deserialize a boolean value from a given buffer at the specified offset.
|
|
139
123
|
* Reads a single byte at the provided offset in the buffer and returns
|
|
@@ -209,6 +193,7 @@ export function serializeBufferArrayToVector(arr) {
|
|
|
209
193
|
* @param vector - The input buffer containing the serialized array.
|
|
210
194
|
* @param offset - An optional starting position in the buffer for deserializing the array.
|
|
211
195
|
* @returns An object containing the deserialized array and the total number of bytes used during deserialization (adv).
|
|
196
|
+
* @deprecated User BufferReader instead.
|
|
212
197
|
*/
|
|
213
198
|
export function deserializeArrayFromVector(deserialize, vector, offset = 0) {
|
|
214
199
|
let pos = offset;
|
|
@@ -247,4 +232,4 @@ export function to2Fields(buf) {
|
|
|
247
232
|
const buf2 = Buffer.concat([Buffer.alloc(16), buf.subarray(16, 32)]);
|
|
248
233
|
return [Fr.fromBuffer(buf1), Fr.fromBuffer(buf2)];
|
|
249
234
|
}
|
|
250
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
235
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZnJlZV9mdW5jcy5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SyxFQUFFLEVBQUU7UUFDckIsTUFBTSxJQUFJLEtBQUssQ0FBQyx5QkFBeUIsQ0FBQyxDQUFDO0tBQzVDO0lBRUQsNkpBQTZKO0lBQzdKLG1EQUFtRDtJQUNuRCxNQUFNLElBQUksR0FBRyxNQUFNLENBQUMsTUFBTSxDQUFDLENBQUMsTUFBTSxDQUFDLEtBQUssQ0FBQyxFQUFFLENBQUMsRUFBRSxHQUFHLENBQUMsUUFBUSxDQUFDLENBQUMsRUFBRSxFQUFFLENBQUMsQ0FBQyxDQUFDLENBQUM7SUFDcEUsTUFBTSxJQUFJLEdBQUcsTUFBTSxDQUFDLE1BQU0sQ0FBQyxDQUFDLE1BQU0sQ0FBQyxLQUFLLENBQUMsRUFBRSxDQUFDLEVBQUUsR0FBRyxDQUFDLFFBQVEsQ0FBQyxFQUFFLEVBQUUsRUFBRSxDQUFDLENBQUMsQ0FBQyxDQUFDO0lBRXJFLE9BQU8sQ0FBQyxFQUFFLENBQUMsVUFBVSxDQUFDLElBQUksQ0FBQyxFQUFFLEVBQUUsQ0FBQyxVQUFVLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQztBQUNwRCxDQUFDIn0=
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/serialize/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/serialize/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC;AACnC,cAAc,YAAY,CAAC"}
|
package/dest/serialize/index.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
export * from './free_funcs.js';
|
|
2
|
-
export * from './deserializer.js';
|
|
3
|
-
export * from './serializer.js';
|
|
4
2
|
export * from './buffer_reader.js';
|
|
5
3
|
export * from './types.js';
|
|
6
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
4
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc2VyaWFsaXplL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLGNBQWMsaUJBQWlCLENBQUM7QUFDaEMsY0FBYyxvQkFBb0IsQ0FBQztBQUNuQyxjQUFjLFlBQVksQ0FBQyJ9
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/foundation",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.9",
|
|
4
4
|
"packageManager": "yarn@3.4.1",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"main": "./dest/index.js",
|
|
7
|
+
"types": "./dest/index.d.ts",
|
|
6
8
|
"exports": {
|
|
7
9
|
"./eslint": "./.eslintrc.cjs",
|
|
8
10
|
"./prettier": "./.prettierrc.json",
|
|
@@ -106,7 +108,6 @@
|
|
|
106
108
|
"src",
|
|
107
109
|
"!*.test.*"
|
|
108
110
|
],
|
|
109
|
-
"types": "./dest/index.d.ts",
|
|
110
111
|
"engines": {
|
|
111
112
|
"node": ">=18"
|
|
112
113
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Reexport all folders at the root for packages targeting CommonJS
|
|
2
|
+
export * as abi from './abi/index.js';
|
|
3
|
+
export * as asyncMap from './async-map/index.js';
|
|
4
|
+
export * as aztecAddress from './aztec-address/index.js';
|
|
5
|
+
export * as bigintBuffer from './bigint-buffer/index.js';
|
|
6
|
+
export * as collection from './collection/index.js';
|
|
7
|
+
export * as committable from './committable/index.js';
|
|
8
|
+
export * as crypto from './crypto/index.js';
|
|
9
|
+
export * as errors from './errors/index.js';
|
|
10
|
+
export * as ethAddress from './eth-address/index.js';
|
|
11
|
+
export * as fields from './fields/index.js';
|
|
12
|
+
export * as fifo from './fifo/index.js';
|
|
13
|
+
export * as jsonRpc from './json-rpc/index.js';
|
|
14
|
+
export * as jsonRpcClient from './json-rpc/client/index.js';
|
|
15
|
+
export * as jsonRpcServer from './json-rpc/server/index.js';
|
|
16
|
+
export * as log from './log/index.js';
|
|
17
|
+
export * as mutex from './mutex/index.js';
|
|
18
|
+
export * as retry from './retry/index.js';
|
|
19
|
+
export * as runningPromise from './running-promise/index.js';
|
|
20
|
+
export * as serialize from './serialize/index.js';
|
|
21
|
+
export * as sleep from './sleep/index.js';
|
|
22
|
+
export * as timer from './timer/index.js';
|
|
23
|
+
export * as transport from './transport/index.js';
|
|
24
|
+
export * as types from './types/index.js';
|
|
25
|
+
export * as url from './url/index.js';
|
|
26
|
+
export * as wasm from './wasm/index.js';
|
|
27
|
+
export * as worker from './worker/index.js';
|
|
@@ -57,9 +57,9 @@ export async function defaultFetch(
|
|
|
57
57
|
}
|
|
58
58
|
if (!resp.ok) {
|
|
59
59
|
if (noRetry) {
|
|
60
|
-
throw new NoRetryError(responseJson.error.message);
|
|
60
|
+
throw new NoRetryError('(JSON-RPC PROPAGATED) ' + responseJson.error.message);
|
|
61
61
|
} else {
|
|
62
|
-
throw new Error(responseJson.error.message);
|
|
62
|
+
throw new Error('(JSON-RPC PROPAGATED) ' + responseJson.error.message);
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
|
package/src/log/logger.ts
CHANGED
|
@@ -76,11 +76,12 @@ function logWithDebug(debug: debug.Debugger, level: LogLevel, msg: string, data?
|
|
|
76
76
|
handler(level, debug.namespace, msg, data);
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
msg = data ? `${msg} ${fmtLogData(data)}` : msg;
|
|
80
80
|
if (debug.enabled) {
|
|
81
|
-
debug(
|
|
81
|
+
if (level !== 'debug') msg = `${level.toUpperCase()} ${msg}`;
|
|
82
|
+
debug(msg);
|
|
82
83
|
} else if (LogLevels.indexOf(level) <= LogLevels.indexOf(currentLevel)) {
|
|
83
|
-
printLog(`${getPrefix(debug, level)} ${
|
|
84
|
+
printLog(`${getPrefix(debug, level)} ${msg}`);
|
|
84
85
|
}
|
|
85
86
|
}
|
|
86
87
|
|
|
@@ -79,14 +79,11 @@ export function numToUInt8(n: number) {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
* @param buf - The input Buffer to be serialized into a vector format.
|
|
87
|
-
* @returns A Buffer containing the serialized vector with the encoded length header.
|
|
82
|
+
* Adds a 4-byte byte-length prefix to a buffer.
|
|
83
|
+
* @param buf - The input Buffer to be prefixed
|
|
84
|
+
* @returns A Buffer with 4-byte byte-length prefix.
|
|
88
85
|
*/
|
|
89
|
-
export function
|
|
86
|
+
export function prefixBufferWithLength(buf: Buffer) {
|
|
90
87
|
const lengthBuf = Buffer.alloc(4);
|
|
91
88
|
lengthBuf.writeUInt32BE(buf.length, 0);
|
|
92
89
|
return Buffer.concat([lengthBuf, buf]);
|
|
@@ -131,20 +128,6 @@ export function serializeDate(date: Date) {
|
|
|
131
128
|
return serializeBigInt(BigInt(date.getTime()), 8);
|
|
132
129
|
}
|
|
133
130
|
|
|
134
|
-
/**
|
|
135
|
-
* Deserialize a buffer from a vector by reading the length from its first 4 bytes, and then extracting the contents of the buffer.
|
|
136
|
-
* The function returns an object containing the deserialized buffer as 'elem' and the number of bytes advanced ('adv') after deserialization.
|
|
137
|
-
*
|
|
138
|
-
* @param vector - The input buffer containing the serialized vector.
|
|
139
|
-
* @param offset - The starting position from where the deserialization should begin (default is 0).
|
|
140
|
-
* @returns An object with the deserialized buffer as 'elem' and the number of bytes advanced ('adv') after deserialization.
|
|
141
|
-
*/
|
|
142
|
-
export function deserializeBufferFromVector(vector: Buffer, offset = 0) {
|
|
143
|
-
const length = vector.readUInt32BE(offset);
|
|
144
|
-
const adv = 4 + length;
|
|
145
|
-
return { elem: vector.subarray(offset + 4, offset + adv), adv };
|
|
146
|
-
}
|
|
147
|
-
|
|
148
131
|
/**
|
|
149
132
|
* Deserialize a boolean value from a given buffer at the specified offset.
|
|
150
133
|
* Reads a single byte at the provided offset in the buffer and returns
|
|
@@ -225,6 +208,7 @@ export function serializeBufferArrayToVector(arr: Buffer[]) {
|
|
|
225
208
|
* @param vector - The input buffer containing the serialized array.
|
|
226
209
|
* @param offset - An optional starting position in the buffer for deserializing the array.
|
|
227
210
|
* @returns An object containing the deserialized array and the total number of bytes used during deserialization (adv).
|
|
211
|
+
* @deprecated User BufferReader instead.
|
|
228
212
|
*/
|
|
229
213
|
export function deserializeArrayFromVector<T>(
|
|
230
214
|
deserialize: (
|
package/src/serialize/index.ts
CHANGED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
/**
|
|
3
|
-
* DeserializeFn is a type representing a deserialization function for a specific data type. The function takes
|
|
4
|
-
* a buffer and an offset as input, and returns an object containing the deserialized element of the data type and
|
|
5
|
-
* the number of bytes advanced in the buffer. This type is used to provide custom deserialization logic for arrays,
|
|
6
|
-
* objects or custom data types while working with the Deserializer class.
|
|
7
|
-
*/
|
|
8
|
-
export type DeserializeFn<T> = (buf: Buffer, offset: number) => {
|
|
9
|
-
/**
|
|
10
|
-
* The deserialized element of the specified data type.
|
|
11
|
-
*/
|
|
12
|
-
elem: T;
|
|
13
|
-
/**
|
|
14
|
-
* The number of bytes advanced in the buffer during deserialization.
|
|
15
|
-
*/
|
|
16
|
-
adv: number;
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* Deserializer class provides a set of methods to deserialize different data types from a buffer.
|
|
20
|
-
* It maintains an internal buffer and offset, updating the offset as it deserializes each data type.
|
|
21
|
-
* The class supports deserialization of various data types including boolean, integers, big integers,
|
|
22
|
-
* buffers, strings, dates, and arrays with custom deserialization functions.
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* const deserializer = new Deserializer(buffer);
|
|
26
|
-
* const boolValue = deserializer.bool();
|
|
27
|
-
* const intValue = deserializer.int32();
|
|
28
|
-
* const bigIntValue = deserializer.bigInt();
|
|
29
|
-
* const stringValue = deserializer.string();
|
|
30
|
-
* const dateValue = deserializer.date();
|
|
31
|
-
* const arrayValue = deserializer.deserializeArray(customDeserializeFn);
|
|
32
|
-
*/
|
|
33
|
-
export declare class Deserializer {
|
|
34
|
-
private buf;
|
|
35
|
-
private offset;
|
|
36
|
-
constructor(buf: Buffer, offset?: number);
|
|
37
|
-
/**
|
|
38
|
-
* Deserialize a boolean value from the buffer at the current offset.
|
|
39
|
-
* Advances the internal offset by one byte after deserialization.
|
|
40
|
-
* Returns 'true' if the deserialized value is non-zero, otherwise returns 'false'.
|
|
41
|
-
*
|
|
42
|
-
* @returns The deserialized boolean value.
|
|
43
|
-
*/
|
|
44
|
-
bool(): boolean;
|
|
45
|
-
/**
|
|
46
|
-
* Deserialize a 32-bit unsigned integer from the buffer at the current offset.
|
|
47
|
-
* Advances the internal buffer offset by 4 after successful deserialization.
|
|
48
|
-
* The result is returned as a JavaScript number.
|
|
49
|
-
*
|
|
50
|
-
* @returns A 32-bit unsigned integer value.
|
|
51
|
-
*/
|
|
52
|
-
uInt32(): number;
|
|
53
|
-
/**
|
|
54
|
-
* Deserialize a 32-bit signed integer from the internal buffer.
|
|
55
|
-
* Reads 4 bytes from the current offset in the buffer and interprets them as a little-endian int32 value.
|
|
56
|
-
* Advances the internal offset by 4 bytes after successful deserialization.
|
|
57
|
-
*
|
|
58
|
-
* @returns The deserialized 32-bit signed integer value.
|
|
59
|
-
*/
|
|
60
|
-
int32(): number;
|
|
61
|
-
/**
|
|
62
|
-
* Deserialize a BigInt from the buffer, taking into account the specified width.
|
|
63
|
-
* The method reads 'width' bytes from the buffer starting at the current offset and converts it to a BigInt.
|
|
64
|
-
* The offset is advanced by 'width' bytes after successful deserialization.
|
|
65
|
-
*
|
|
66
|
-
* @param width - The number of bytes to read from the buffer to construct the BigInt (default is 32).
|
|
67
|
-
* @returns The deserialized BigInt value.
|
|
68
|
-
*/
|
|
69
|
-
bigInt(width?: number): bigint;
|
|
70
|
-
/**
|
|
71
|
-
* Deserialize a variable-length byte array from the internal buffer.
|
|
72
|
-
* This method reads the length of the array and then extracts the corresponding bytes.
|
|
73
|
-
* It advances the internal offset by the number of bytes read, including the length prefix.
|
|
74
|
-
*
|
|
75
|
-
* @returns A Buffer instance containing the deserialized byte array.
|
|
76
|
-
*/
|
|
77
|
-
vector(): Buffer;
|
|
78
|
-
/**
|
|
79
|
-
* Extract a sub-buffer with the specified width, advancing the internal offset.
|
|
80
|
-
* The function slices the buffer from the current offset to the offset plus the provided width,
|
|
81
|
-
* and advances the internal offset by the width. This can be useful for working with fixed-width
|
|
82
|
-
* structures within the original buffer.
|
|
83
|
-
*
|
|
84
|
-
* @param width - The number of bytes to include in the extracted sub-buffer.
|
|
85
|
-
* @returns A sub-buffer containing the specified number of bytes from the original buffer.
|
|
86
|
-
*/
|
|
87
|
-
buffer(width: number): Buffer;
|
|
88
|
-
/**
|
|
89
|
-
* Deserialize a string from the internal buffer.
|
|
90
|
-
* It first deserializes a vector representing the UTF-8 encoded string from the buffer,
|
|
91
|
-
* and then converts it to a string.
|
|
92
|
-
*
|
|
93
|
-
* @returns The deserialized string.
|
|
94
|
-
*/
|
|
95
|
-
string(): string;
|
|
96
|
-
/**
|
|
97
|
-
* Deserialize a Date object from the internal buffer.
|
|
98
|
-
* The date value is expected to be stored as a 64-bit BigInt representing the number of milliseconds since the Unix epoch.
|
|
99
|
-
* Advances the internal offset by 8 bytes after deserialization.
|
|
100
|
-
*
|
|
101
|
-
* @returns A Date instance representing the deserialized date value.
|
|
102
|
-
*/
|
|
103
|
-
date(): Date;
|
|
104
|
-
/**
|
|
105
|
-
* Deserialize an array of elements using the provided deserialization function.
|
|
106
|
-
* This method reads the serialized data from the buffer and deserializes each element in the array
|
|
107
|
-
* using the given 'fn' deserialization function. The returned array contains the deserialized elements
|
|
108
|
-
* in their original order.
|
|
109
|
-
*
|
|
110
|
-
* @param fn - The deserialization function to be applied on each element in the array.
|
|
111
|
-
* @returns An array containing the deserialized elements.
|
|
112
|
-
*/
|
|
113
|
-
deserializeArray<T>(fn: DeserializeFn<T>): T[];
|
|
114
|
-
/**
|
|
115
|
-
* Executes the given deserialization function on this Deserializer's buffer and updates the internal offset.
|
|
116
|
-
* The DeserializeFn<T> should take a Buffer and an offset as input, and return an object containing the deserialized
|
|
117
|
-
* element and the number of bytes advanced in the buffer. This method is useful for custom deserialization logic
|
|
118
|
-
* or implementing new deserialization functions.
|
|
119
|
-
*
|
|
120
|
-
* @typeparam T - The type of the deserialized element.
|
|
121
|
-
* @param fn - The deserialization function to execute.
|
|
122
|
-
* @returns The deserialized element of type T.
|
|
123
|
-
*/
|
|
124
|
-
exec<T>(fn: DeserializeFn<T>): T;
|
|
125
|
-
/**
|
|
126
|
-
* Returns the current offset value in the Deserializer instance.
|
|
127
|
-
* The offset is updated as elements are deserialized from the buffer.
|
|
128
|
-
* It can be useful for tracking the position in the buffer during complex deserialization processes.
|
|
129
|
-
*
|
|
130
|
-
* @returns The current offset value as a number.
|
|
131
|
-
*/
|
|
132
|
-
getOffset(): number;
|
|
133
|
-
}
|
|
134
|
-
//# sourceMappingURL=deserializer.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"deserializer.d.ts","sourceRoot":"","sources":["../../src/serialize/deserializer.ts"],"names":[],"mappings":";AASA;;;;;GAKG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAC7B,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,MAAM,KACX;IACH;;OAEG;IACH,IAAI,EAAE,CAAC,CAAC;IACR;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,qBAAa,YAAY;IACX,OAAO,CAAC,GAAG;IAAU,OAAO,CAAC,MAAM;gBAA3B,GAAG,EAAE,MAAM,EAAU,MAAM,SAAI;IAEnD;;;;;;OAMG;IACI,IAAI;IAIX;;;;;;OAMG;IACI,MAAM;IAIb;;;;;;OAMG;IACI,KAAK;IAIZ;;;;;;;OAOG;IACI,MAAM,CAAC,KAAK,SAAK;IAIxB;;;;;;OAMG;IACI,MAAM;IAIb;;;;;;;;OAQG;IACI,MAAM,CAAC,KAAK,EAAE,MAAM;IAM3B;;;;;;OAMG;IACI,MAAM;IAIb;;;;;;OAMG;IACI,IAAI;IAIX;;;;;;;;OAQG;IACI,gBAAgB,CAAC,CAAC,EAAE,EAAE,EAAE,aAAa,CAAC,CAAC,CAAC;IAI/C;;;;;;;;;OASG;IACI,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC;IAMvC;;;;;;OAMG;IACI,SAAS;CAGjB"}
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import { deserializeArrayFromVector, deserializeBigInt, deserializeBool, deserializeBufferFromVector, deserializeInt32, deserializeUInt32, } from './free_funcs.js';
|
|
2
|
-
/**
|
|
3
|
-
* Deserializer class provides a set of methods to deserialize different data types from a buffer.
|
|
4
|
-
* It maintains an internal buffer and offset, updating the offset as it deserializes each data type.
|
|
5
|
-
* The class supports deserialization of various data types including boolean, integers, big integers,
|
|
6
|
-
* buffers, strings, dates, and arrays with custom deserialization functions.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* const deserializer = new Deserializer(buffer);
|
|
10
|
-
* const boolValue = deserializer.bool();
|
|
11
|
-
* const intValue = deserializer.int32();
|
|
12
|
-
* const bigIntValue = deserializer.bigInt();
|
|
13
|
-
* const stringValue = deserializer.string();
|
|
14
|
-
* const dateValue = deserializer.date();
|
|
15
|
-
* const arrayValue = deserializer.deserializeArray(customDeserializeFn);
|
|
16
|
-
*/
|
|
17
|
-
export class Deserializer {
|
|
18
|
-
constructor(buf, offset = 0) {
|
|
19
|
-
this.buf = buf;
|
|
20
|
-
this.offset = offset;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Deserialize a boolean value from the buffer at the current offset.
|
|
24
|
-
* Advances the internal offset by one byte after deserialization.
|
|
25
|
-
* Returns 'true' if the deserialized value is non-zero, otherwise returns 'false'.
|
|
26
|
-
*
|
|
27
|
-
* @returns The deserialized boolean value.
|
|
28
|
-
*/
|
|
29
|
-
bool() {
|
|
30
|
-
return this.exec(deserializeBool) ? true : false;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Deserialize a 32-bit unsigned integer from the buffer at the current offset.
|
|
34
|
-
* Advances the internal buffer offset by 4 after successful deserialization.
|
|
35
|
-
* The result is returned as a JavaScript number.
|
|
36
|
-
*
|
|
37
|
-
* @returns A 32-bit unsigned integer value.
|
|
38
|
-
*/
|
|
39
|
-
uInt32() {
|
|
40
|
-
return this.exec(deserializeUInt32);
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Deserialize a 32-bit signed integer from the internal buffer.
|
|
44
|
-
* Reads 4 bytes from the current offset in the buffer and interprets them as a little-endian int32 value.
|
|
45
|
-
* Advances the internal offset by 4 bytes after successful deserialization.
|
|
46
|
-
*
|
|
47
|
-
* @returns The deserialized 32-bit signed integer value.
|
|
48
|
-
*/
|
|
49
|
-
int32() {
|
|
50
|
-
return this.exec(deserializeInt32);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Deserialize a BigInt from the buffer, taking into account the specified width.
|
|
54
|
-
* The method reads 'width' bytes from the buffer starting at the current offset and converts it to a BigInt.
|
|
55
|
-
* The offset is advanced by 'width' bytes after successful deserialization.
|
|
56
|
-
*
|
|
57
|
-
* @param width - The number of bytes to read from the buffer to construct the BigInt (default is 32).
|
|
58
|
-
* @returns The deserialized BigInt value.
|
|
59
|
-
*/
|
|
60
|
-
bigInt(width = 32) {
|
|
61
|
-
return this.exec((buf, offset) => deserializeBigInt(buf, offset, width));
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Deserialize a variable-length byte array from the internal buffer.
|
|
65
|
-
* This method reads the length of the array and then extracts the corresponding bytes.
|
|
66
|
-
* It advances the internal offset by the number of bytes read, including the length prefix.
|
|
67
|
-
*
|
|
68
|
-
* @returns A Buffer instance containing the deserialized byte array.
|
|
69
|
-
*/
|
|
70
|
-
vector() {
|
|
71
|
-
return this.exec(deserializeBufferFromVector);
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Extract a sub-buffer with the specified width, advancing the internal offset.
|
|
75
|
-
* The function slices the buffer from the current offset to the offset plus the provided width,
|
|
76
|
-
* and advances the internal offset by the width. This can be useful for working with fixed-width
|
|
77
|
-
* structures within the original buffer.
|
|
78
|
-
*
|
|
79
|
-
* @param width - The number of bytes to include in the extracted sub-buffer.
|
|
80
|
-
* @returns A sub-buffer containing the specified number of bytes from the original buffer.
|
|
81
|
-
*/
|
|
82
|
-
buffer(width) {
|
|
83
|
-
const buf = this.buf.slice(this.offset, this.offset + width);
|
|
84
|
-
this.offset += width;
|
|
85
|
-
return buf;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Deserialize a string from the internal buffer.
|
|
89
|
-
* It first deserializes a vector representing the UTF-8 encoded string from the buffer,
|
|
90
|
-
* and then converts it to a string.
|
|
91
|
-
*
|
|
92
|
-
* @returns The deserialized string.
|
|
93
|
-
*/
|
|
94
|
-
string() {
|
|
95
|
-
return this.vector().toString();
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Deserialize a Date object from the internal buffer.
|
|
99
|
-
* The date value is expected to be stored as a 64-bit BigInt representing the number of milliseconds since the Unix epoch.
|
|
100
|
-
* Advances the internal offset by 8 bytes after deserialization.
|
|
101
|
-
*
|
|
102
|
-
* @returns A Date instance representing the deserialized date value.
|
|
103
|
-
*/
|
|
104
|
-
date() {
|
|
105
|
-
return new Date(Number(this.bigInt(8)));
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Deserialize an array of elements using the provided deserialization function.
|
|
109
|
-
* This method reads the serialized data from the buffer and deserializes each element in the array
|
|
110
|
-
* using the given 'fn' deserialization function. The returned array contains the deserialized elements
|
|
111
|
-
* in their original order.
|
|
112
|
-
*
|
|
113
|
-
* @param fn - The deserialization function to be applied on each element in the array.
|
|
114
|
-
* @returns An array containing the deserialized elements.
|
|
115
|
-
*/
|
|
116
|
-
deserializeArray(fn) {
|
|
117
|
-
return this.exec((buf, offset) => deserializeArrayFromVector(fn, buf, offset));
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Executes the given deserialization function on this Deserializer's buffer and updates the internal offset.
|
|
121
|
-
* The DeserializeFn<T> should take a Buffer and an offset as input, and return an object containing the deserialized
|
|
122
|
-
* element and the number of bytes advanced in the buffer. This method is useful for custom deserialization logic
|
|
123
|
-
* or implementing new deserialization functions.
|
|
124
|
-
*
|
|
125
|
-
* @typeparam T - The type of the deserialized element.
|
|
126
|
-
* @param fn - The deserialization function to execute.
|
|
127
|
-
* @returns The deserialized element of type T.
|
|
128
|
-
*/
|
|
129
|
-
exec(fn) {
|
|
130
|
-
const { elem, adv } = fn(this.buf, this.offset);
|
|
131
|
-
this.offset += adv;
|
|
132
|
-
return elem;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Returns the current offset value in the Deserializer instance.
|
|
136
|
-
* The offset is updated as elements are deserialized from the buffer.
|
|
137
|
-
* It can be useful for tracking the position in the buffer during complex deserialization processes.
|
|
138
|
-
*
|
|
139
|
-
* @returns The current offset value as a number.
|
|
140
|
-
*/
|
|
141
|
-
getOffset() {
|
|
142
|
-
return this.offset;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVzZXJpYWxpemVyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3NlcmlhbGl6ZS9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CxJQUFJLENBQUMsTUFBTSxJQUFJLEdBQUcsQ0FBQztRQUNuQixPQUFPLElBQUksQ0FBQztJQUNkLENBQUM7SUFFRDs7Ozs7O09BTUc7SUFDSSxTQUFTO1FBQ2QsT0FBTyxJQUFJLENBQUMsTUFBTSxDQUFDO0lBQ3JCLENBQUM7Q0FDRiJ9
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
/**
|
|
3
|
-
* The Serializer class provides a convenient and efficient way to serialize various data types into binary format.
|
|
4
|
-
* It supports serialization of primitive types (like boolean, signed/unsigned integers, BigInt), as well as more complex types (like Buffer, Date, and custom structures with their own 'toBuffer()' methods).
|
|
5
|
-
* The class maintains an internal buffer array that accumulates serialized data, allowing for easy concatenation and retrieval of the final serialized Buffer.
|
|
6
|
-
* This can be useful in various applications such as network communication, file storage, or other scenarios where binary data representation is needed.
|
|
7
|
-
*/
|
|
8
|
-
export declare class Serializer {
|
|
9
|
-
private buf;
|
|
10
|
-
constructor();
|
|
11
|
-
/**
|
|
12
|
-
* Serialize a boolean value into a Buffer and append it to the internal buffer array.
|
|
13
|
-
* The serialized boolean will be stored as a single byte, where true is represented as 1 and false as 0.
|
|
14
|
-
* This method updates the Serializer's internal state and does not return any values.
|
|
15
|
-
*
|
|
16
|
-
* @param bool - The boolean value to be serialized.
|
|
17
|
-
*/
|
|
18
|
-
bool(bool: boolean): void;
|
|
19
|
-
/**
|
|
20
|
-
* Encodes a given unsigned 32-bit integer into a Buffer and appends it to the internal buffer array.
|
|
21
|
-
* The provided number should be within the range of 0 and 2^32 - 1, inclusive.
|
|
22
|
-
* Throws an error if the input value is out of range or not a valid number.
|
|
23
|
-
*
|
|
24
|
-
* @param num - The unsigned 32-bit integer to be encoded and appended.
|
|
25
|
-
*/
|
|
26
|
-
uInt32(num: number): void;
|
|
27
|
-
/**
|
|
28
|
-
* Serialize a signed 32-bit integer (int32) into the internal buffer.
|
|
29
|
-
* The number should be within the range of -2147483648 to 2147483647 inclusive.
|
|
30
|
-
* Throws an error if the input is not within the valid int32 range.
|
|
31
|
-
*
|
|
32
|
-
* @param num - The signed 32-bit integer to serialize.
|
|
33
|
-
*/
|
|
34
|
-
int32(num: number): void;
|
|
35
|
-
/**
|
|
36
|
-
* Serializes a BigInt into a Buffer and appends it to the internal buffer array.
|
|
37
|
-
* The given 'num' is treated as a signed integer and is serialized using
|
|
38
|
-
* little-endian byte order. This method is useful for efficiently storing
|
|
39
|
-
* large integer values that may not fit within the range of a standard number.
|
|
40
|
-
*
|
|
41
|
-
* @param num - The BigInt value to serialize.
|
|
42
|
-
*/
|
|
43
|
-
bigInt(num: bigint): void;
|
|
44
|
-
/**
|
|
45
|
-
* The given buffer is of variable length. Prefixes the buffer with its length.
|
|
46
|
-
* @param buf - The buffer to serialize as a variable-length vector.
|
|
47
|
-
*/
|
|
48
|
-
vector(buf: Buffer): void;
|
|
49
|
-
/**
|
|
50
|
-
* Directly serializes a buffer that maybe of fixed, or variable length.
|
|
51
|
-
* It is assumed the corresponding deserialize function will handle variable length data, thus the length
|
|
52
|
-
* does not need to be prefixed here.
|
|
53
|
-
* If serializing a raw, variable length buffer, use vector().
|
|
54
|
-
* @param buf - The buffer to serialize as a fixed-length array.
|
|
55
|
-
*/
|
|
56
|
-
buffer(buf: Buffer): void;
|
|
57
|
-
/**
|
|
58
|
-
* Serialize a string by first converting it to a buffer and then encoding its length as a prefix.
|
|
59
|
-
* The serialized string can be deserialized by reading the prefixed length and extracting the corresponding data.
|
|
60
|
-
* This method is useful for serializing strings of variable length in a consistent format.
|
|
61
|
-
*
|
|
62
|
-
* @param str - The input string to be serialized.
|
|
63
|
-
*/
|
|
64
|
-
string(str: string): void;
|
|
65
|
-
/**
|
|
66
|
-
* Serialize a given Date instance into a Buffer and append it to the internal buffer list.
|
|
67
|
-
* The serialized date is stored as an 8-byte BigInt representing the number of milliseconds since the Unix epoch.
|
|
68
|
-
* This function facilitates serialization of JavaScript's built-in Date objects for subsequent data transmission or storage.
|
|
69
|
-
*
|
|
70
|
-
* @param date - The Date instance to be serialized.
|
|
71
|
-
*/
|
|
72
|
-
date(date: Date): void;
|
|
73
|
-
/**
|
|
74
|
-
* Returns the serialized Buffer object that was created by calling various serialization methods on this Serializer instance.
|
|
75
|
-
* The resulting buffer can be used for sending or storing serialized data in binary format.
|
|
76
|
-
*
|
|
77
|
-
* @returns A Buffer containing the serialized data.
|
|
78
|
-
*/
|
|
79
|
-
getBuffer(): Buffer;
|
|
80
|
-
/**
|
|
81
|
-
* Serializes an array of elements, where each element has a 'toBuffer()' method, into a single Buffer.
|
|
82
|
-
* The resulting buffer is prefixed with its length (number of elements), allowing for easy deserialization.
|
|
83
|
-
* This method is useful for serializing arrays of custom classes or data structures that have their own serialization logic.
|
|
84
|
-
*
|
|
85
|
-
* @param arr - The array of elements to be serialized. Each element must have a 'toBuffer()' method for serialization.
|
|
86
|
-
*/
|
|
87
|
-
serializeArray<T>(arr: T[]): void;
|
|
88
|
-
}
|
|
89
|
-
//# sourceMappingURL=serializer.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"serializer.d.ts","sourceRoot":"","sources":["../../src/serialize/serializer.ts"],"names":[],"mappings":";AAUA;;;;;GAKG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,GAAG,CAAgB;;IAI3B;;;;;;OAMG;IACI,IAAI,CAAC,IAAI,EAAE,OAAO;IAIzB;;;;;;OAMG;IACI,MAAM,CAAC,GAAG,EAAE,MAAM;IAIzB;;;;;;OAMG;IACI,KAAK,CAAC,GAAG,EAAE,MAAM;IAIxB;;;;;;;OAOG;IACI,MAAM,CAAC,GAAG,EAAE,MAAM;IAIzB;;;OAGG;IACI,MAAM,CAAC,GAAG,EAAE,MAAM;IAIzB;;;;;;OAMG;IACI,MAAM,CAAC,GAAG,EAAE,MAAM;IAIzB;;;;;;OAMG;IACI,MAAM,CAAC,GAAG,EAAE,MAAM;IAIzB;;;;;;OAMG;IACI,IAAI,CAAC,IAAI,EAAE,IAAI;IAItB;;;;;OAKG;IACI,SAAS;IAIhB;;;;;;OAMG;IACI,cAAc,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE;CAGlC"}
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { boolToByte, numToInt32BE, numToUInt32BE, serializeBigInt, serializeBufferToVector, serializeDate, } from './free_funcs.js';
|
|
2
|
-
import { serializeBufferArrayToVector } from './index.js';
|
|
3
|
-
/**
|
|
4
|
-
* The Serializer class provides a convenient and efficient way to serialize various data types into binary format.
|
|
5
|
-
* It supports serialization of primitive types (like boolean, signed/unsigned integers, BigInt), as well as more complex types (like Buffer, Date, and custom structures with their own 'toBuffer()' methods).
|
|
6
|
-
* The class maintains an internal buffer array that accumulates serialized data, allowing for easy concatenation and retrieval of the final serialized Buffer.
|
|
7
|
-
* This can be useful in various applications such as network communication, file storage, or other scenarios where binary data representation is needed.
|
|
8
|
-
*/
|
|
9
|
-
export class Serializer {
|
|
10
|
-
constructor() {
|
|
11
|
-
this.buf = [];
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Serialize a boolean value into a Buffer and append it to the internal buffer array.
|
|
15
|
-
* The serialized boolean will be stored as a single byte, where true is represented as 1 and false as 0.
|
|
16
|
-
* This method updates the Serializer's internal state and does not return any values.
|
|
17
|
-
*
|
|
18
|
-
* @param bool - The boolean value to be serialized.
|
|
19
|
-
*/
|
|
20
|
-
bool(bool) {
|
|
21
|
-
this.buf.push(boolToByte(bool));
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Encodes a given unsigned 32-bit integer into a Buffer and appends it to the internal buffer array.
|
|
25
|
-
* The provided number should be within the range of 0 and 2^32 - 1, inclusive.
|
|
26
|
-
* Throws an error if the input value is out of range or not a valid number.
|
|
27
|
-
*
|
|
28
|
-
* @param num - The unsigned 32-bit integer to be encoded and appended.
|
|
29
|
-
*/
|
|
30
|
-
uInt32(num) {
|
|
31
|
-
this.buf.push(numToUInt32BE(num));
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Serialize a signed 32-bit integer (int32) into the internal buffer.
|
|
35
|
-
* The number should be within the range of -2147483648 to 2147483647 inclusive.
|
|
36
|
-
* Throws an error if the input is not within the valid int32 range.
|
|
37
|
-
*
|
|
38
|
-
* @param num - The signed 32-bit integer to serialize.
|
|
39
|
-
*/
|
|
40
|
-
int32(num) {
|
|
41
|
-
this.buf.push(numToInt32BE(num));
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Serializes a BigInt into a Buffer and appends it to the internal buffer array.
|
|
45
|
-
* The given 'num' is treated as a signed integer and is serialized using
|
|
46
|
-
* little-endian byte order. This method is useful for efficiently storing
|
|
47
|
-
* large integer values that may not fit within the range of a standard number.
|
|
48
|
-
*
|
|
49
|
-
* @param num - The BigInt value to serialize.
|
|
50
|
-
*/
|
|
51
|
-
bigInt(num) {
|
|
52
|
-
this.buf.push(serializeBigInt(num));
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* The given buffer is of variable length. Prefixes the buffer with its length.
|
|
56
|
-
* @param buf - The buffer to serialize as a variable-length vector.
|
|
57
|
-
*/
|
|
58
|
-
vector(buf) {
|
|
59
|
-
this.buf.push(serializeBufferToVector(buf));
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Directly serializes a buffer that maybe of fixed, or variable length.
|
|
63
|
-
* It is assumed the corresponding deserialize function will handle variable length data, thus the length
|
|
64
|
-
* does not need to be prefixed here.
|
|
65
|
-
* If serializing a raw, variable length buffer, use vector().
|
|
66
|
-
* @param buf - The buffer to serialize as a fixed-length array.
|
|
67
|
-
*/
|
|
68
|
-
buffer(buf) {
|
|
69
|
-
this.buf.push(buf);
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Serialize a string by first converting it to a buffer and then encoding its length as a prefix.
|
|
73
|
-
* The serialized string can be deserialized by reading the prefixed length and extracting the corresponding data.
|
|
74
|
-
* This method is useful for serializing strings of variable length in a consistent format.
|
|
75
|
-
*
|
|
76
|
-
* @param str - The input string to be serialized.
|
|
77
|
-
*/
|
|
78
|
-
string(str) {
|
|
79
|
-
this.vector(Buffer.from(str));
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Serialize a given Date instance into a Buffer and append it to the internal buffer list.
|
|
83
|
-
* The serialized date is stored as an 8-byte BigInt representing the number of milliseconds since the Unix epoch.
|
|
84
|
-
* This function facilitates serialization of JavaScript's built-in Date objects for subsequent data transmission or storage.
|
|
85
|
-
*
|
|
86
|
-
* @param date - The Date instance to be serialized.
|
|
87
|
-
*/
|
|
88
|
-
date(date) {
|
|
89
|
-
this.buf.push(serializeDate(date));
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Returns the serialized Buffer object that was created by calling various serialization methods on this Serializer instance.
|
|
93
|
-
* The resulting buffer can be used for sending or storing serialized data in binary format.
|
|
94
|
-
*
|
|
95
|
-
* @returns A Buffer containing the serialized data.
|
|
96
|
-
*/
|
|
97
|
-
getBuffer() {
|
|
98
|
-
return Buffer.concat(this.buf);
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Serializes an array of elements, where each element has a 'toBuffer()' method, into a single Buffer.
|
|
102
|
-
* The resulting buffer is prefixed with its length (number of elements), allowing for easy deserialization.
|
|
103
|
-
* This method is useful for serializing arrays of custom classes or data structures that have their own serialization logic.
|
|
104
|
-
*
|
|
105
|
-
* @param arr - The array of elements to be serialized. Each element must have a 'toBuffer()' method for serialization.
|
|
106
|
-
*/
|
|
107
|
-
serializeArray(arr) {
|
|
108
|
-
this.buf.push(serializeBufferArrayToVector(arr.map((e) => e.toBuffer())));
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2VyaWFsaXplci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4u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_funcs.js';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* DeserializeFn is a type representing a deserialization function for a specific data type. The function takes
|
|
12
|
-
* a buffer and an offset as input, and returns an object containing the deserialized element of the data type and
|
|
13
|
-
* the number of bytes advanced in the buffer. This type is used to provide custom deserialization logic for arrays,
|
|
14
|
-
* objects or custom data types while working with the Deserializer class.
|
|
15
|
-
*/
|
|
16
|
-
export type DeserializeFn<T> = (
|
|
17
|
-
buf: Buffer,
|
|
18
|
-
offset: number,
|
|
19
|
-
) => {
|
|
20
|
-
/**
|
|
21
|
-
* The deserialized element of the specified data type.
|
|
22
|
-
*/
|
|
23
|
-
elem: T;
|
|
24
|
-
/**
|
|
25
|
-
* The number of bytes advanced in the buffer during deserialization.
|
|
26
|
-
*/
|
|
27
|
-
adv: number;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Deserializer class provides a set of methods to deserialize different data types from a buffer.
|
|
32
|
-
* It maintains an internal buffer and offset, updating the offset as it deserializes each data type.
|
|
33
|
-
* The class supports deserialization of various data types including boolean, integers, big integers,
|
|
34
|
-
* buffers, strings, dates, and arrays with custom deserialization functions.
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
* const deserializer = new Deserializer(buffer);
|
|
38
|
-
* const boolValue = deserializer.bool();
|
|
39
|
-
* const intValue = deserializer.int32();
|
|
40
|
-
* const bigIntValue = deserializer.bigInt();
|
|
41
|
-
* const stringValue = deserializer.string();
|
|
42
|
-
* const dateValue = deserializer.date();
|
|
43
|
-
* const arrayValue = deserializer.deserializeArray(customDeserializeFn);
|
|
44
|
-
*/
|
|
45
|
-
export class Deserializer {
|
|
46
|
-
constructor(private buf: Buffer, private offset = 0) {}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Deserialize a boolean value from the buffer at the current offset.
|
|
50
|
-
* Advances the internal offset by one byte after deserialization.
|
|
51
|
-
* Returns 'true' if the deserialized value is non-zero, otherwise returns 'false'.
|
|
52
|
-
*
|
|
53
|
-
* @returns The deserialized boolean value.
|
|
54
|
-
*/
|
|
55
|
-
public bool() {
|
|
56
|
-
return this.exec(deserializeBool) ? true : false;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Deserialize a 32-bit unsigned integer from the buffer at the current offset.
|
|
61
|
-
* Advances the internal buffer offset by 4 after successful deserialization.
|
|
62
|
-
* The result is returned as a JavaScript number.
|
|
63
|
-
*
|
|
64
|
-
* @returns A 32-bit unsigned integer value.
|
|
65
|
-
*/
|
|
66
|
-
public uInt32() {
|
|
67
|
-
return this.exec(deserializeUInt32);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Deserialize a 32-bit signed integer from the internal buffer.
|
|
72
|
-
* Reads 4 bytes from the current offset in the buffer and interprets them as a little-endian int32 value.
|
|
73
|
-
* Advances the internal offset by 4 bytes after successful deserialization.
|
|
74
|
-
*
|
|
75
|
-
* @returns The deserialized 32-bit signed integer value.
|
|
76
|
-
*/
|
|
77
|
-
public int32() {
|
|
78
|
-
return this.exec(deserializeInt32);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Deserialize a BigInt from the buffer, taking into account the specified width.
|
|
83
|
-
* The method reads 'width' bytes from the buffer starting at the current offset and converts it to a BigInt.
|
|
84
|
-
* The offset is advanced by 'width' bytes after successful deserialization.
|
|
85
|
-
*
|
|
86
|
-
* @param width - The number of bytes to read from the buffer to construct the BigInt (default is 32).
|
|
87
|
-
* @returns The deserialized BigInt value.
|
|
88
|
-
*/
|
|
89
|
-
public bigInt(width = 32) {
|
|
90
|
-
return this.exec((buf: Buffer, offset: number) => deserializeBigInt(buf, offset, width));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Deserialize a variable-length byte array from the internal buffer.
|
|
95
|
-
* This method reads the length of the array and then extracts the corresponding bytes.
|
|
96
|
-
* It advances the internal offset by the number of bytes read, including the length prefix.
|
|
97
|
-
*
|
|
98
|
-
* @returns A Buffer instance containing the deserialized byte array.
|
|
99
|
-
*/
|
|
100
|
-
public vector() {
|
|
101
|
-
return this.exec(deserializeBufferFromVector);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Extract a sub-buffer with the specified width, advancing the internal offset.
|
|
106
|
-
* The function slices the buffer from the current offset to the offset plus the provided width,
|
|
107
|
-
* and advances the internal offset by the width. This can be useful for working with fixed-width
|
|
108
|
-
* structures within the original buffer.
|
|
109
|
-
*
|
|
110
|
-
* @param width - The number of bytes to include in the extracted sub-buffer.
|
|
111
|
-
* @returns A sub-buffer containing the specified number of bytes from the original buffer.
|
|
112
|
-
*/
|
|
113
|
-
public buffer(width: number) {
|
|
114
|
-
const buf = this.buf.slice(this.offset, this.offset + width);
|
|
115
|
-
this.offset += width;
|
|
116
|
-
return buf;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Deserialize a string from the internal buffer.
|
|
121
|
-
* It first deserializes a vector representing the UTF-8 encoded string from the buffer,
|
|
122
|
-
* and then converts it to a string.
|
|
123
|
-
*
|
|
124
|
-
* @returns The deserialized string.
|
|
125
|
-
*/
|
|
126
|
-
public string() {
|
|
127
|
-
return this.vector().toString();
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Deserialize a Date object from the internal buffer.
|
|
132
|
-
* The date value is expected to be stored as a 64-bit BigInt representing the number of milliseconds since the Unix epoch.
|
|
133
|
-
* Advances the internal offset by 8 bytes after deserialization.
|
|
134
|
-
*
|
|
135
|
-
* @returns A Date instance representing the deserialized date value.
|
|
136
|
-
*/
|
|
137
|
-
public date() {
|
|
138
|
-
return new Date(Number(this.bigInt(8)));
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Deserialize an array of elements using the provided deserialization function.
|
|
143
|
-
* This method reads the serialized data from the buffer and deserializes each element in the array
|
|
144
|
-
* using the given 'fn' deserialization function. The returned array contains the deserialized elements
|
|
145
|
-
* in their original order.
|
|
146
|
-
*
|
|
147
|
-
* @param fn - The deserialization function to be applied on each element in the array.
|
|
148
|
-
* @returns An array containing the deserialized elements.
|
|
149
|
-
*/
|
|
150
|
-
public deserializeArray<T>(fn: DeserializeFn<T>) {
|
|
151
|
-
return this.exec((buf: Buffer, offset: number) => deserializeArrayFromVector(fn, buf, offset));
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Executes the given deserialization function on this Deserializer's buffer and updates the internal offset.
|
|
156
|
-
* The DeserializeFn<T> should take a Buffer and an offset as input, and return an object containing the deserialized
|
|
157
|
-
* element and the number of bytes advanced in the buffer. This method is useful for custom deserialization logic
|
|
158
|
-
* or implementing new deserialization functions.
|
|
159
|
-
*
|
|
160
|
-
* @typeparam T - The type of the deserialized element.
|
|
161
|
-
* @param fn - The deserialization function to execute.
|
|
162
|
-
* @returns The deserialized element of type T.
|
|
163
|
-
*/
|
|
164
|
-
public exec<T>(fn: DeserializeFn<T>): T {
|
|
165
|
-
const { elem, adv } = fn(this.buf, this.offset);
|
|
166
|
-
this.offset += adv;
|
|
167
|
-
return elem;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Returns the current offset value in the Deserializer instance.
|
|
172
|
-
* The offset is updated as elements are deserialized from the buffer.
|
|
173
|
-
* It can be useful for tracking the position in the buffer during complex deserialization processes.
|
|
174
|
-
*
|
|
175
|
-
* @returns The current offset value as a number.
|
|
176
|
-
*/
|
|
177
|
-
public getOffset() {
|
|
178
|
-
return this.offset;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
boolToByte,
|
|
3
|
-
numToInt32BE,
|
|
4
|
-
numToUInt32BE,
|
|
5
|
-
serializeBigInt,
|
|
6
|
-
serializeBufferToVector,
|
|
7
|
-
serializeDate,
|
|
8
|
-
} from './free_funcs.js';
|
|
9
|
-
import { serializeBufferArrayToVector } from './index.js';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* The Serializer class provides a convenient and efficient way to serialize various data types into binary format.
|
|
13
|
-
* It supports serialization of primitive types (like boolean, signed/unsigned integers, BigInt), as well as more complex types (like Buffer, Date, and custom structures with their own 'toBuffer()' methods).
|
|
14
|
-
* The class maintains an internal buffer array that accumulates serialized data, allowing for easy concatenation and retrieval of the final serialized Buffer.
|
|
15
|
-
* This can be useful in various applications such as network communication, file storage, or other scenarios where binary data representation is needed.
|
|
16
|
-
*/
|
|
17
|
-
export class Serializer {
|
|
18
|
-
private buf: Buffer[] = [];
|
|
19
|
-
|
|
20
|
-
constructor() {}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Serialize a boolean value into a Buffer and append it to the internal buffer array.
|
|
24
|
-
* The serialized boolean will be stored as a single byte, where true is represented as 1 and false as 0.
|
|
25
|
-
* This method updates the Serializer's internal state and does not return any values.
|
|
26
|
-
*
|
|
27
|
-
* @param bool - The boolean value to be serialized.
|
|
28
|
-
*/
|
|
29
|
-
public bool(bool: boolean) {
|
|
30
|
-
this.buf.push(boolToByte(bool));
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Encodes a given unsigned 32-bit integer into a Buffer and appends it to the internal buffer array.
|
|
35
|
-
* The provided number should be within the range of 0 and 2^32 - 1, inclusive.
|
|
36
|
-
* Throws an error if the input value is out of range or not a valid number.
|
|
37
|
-
*
|
|
38
|
-
* @param num - The unsigned 32-bit integer to be encoded and appended.
|
|
39
|
-
*/
|
|
40
|
-
public uInt32(num: number) {
|
|
41
|
-
this.buf.push(numToUInt32BE(num));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Serialize a signed 32-bit integer (int32) into the internal buffer.
|
|
46
|
-
* The number should be within the range of -2147483648 to 2147483647 inclusive.
|
|
47
|
-
* Throws an error if the input is not within the valid int32 range.
|
|
48
|
-
*
|
|
49
|
-
* @param num - The signed 32-bit integer to serialize.
|
|
50
|
-
*/
|
|
51
|
-
public int32(num: number) {
|
|
52
|
-
this.buf.push(numToInt32BE(num));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Serializes a BigInt into a Buffer and appends it to the internal buffer array.
|
|
57
|
-
* The given 'num' is treated as a signed integer and is serialized using
|
|
58
|
-
* little-endian byte order. This method is useful for efficiently storing
|
|
59
|
-
* large integer values that may not fit within the range of a standard number.
|
|
60
|
-
*
|
|
61
|
-
* @param num - The BigInt value to serialize.
|
|
62
|
-
*/
|
|
63
|
-
public bigInt(num: bigint) {
|
|
64
|
-
this.buf.push(serializeBigInt(num));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* The given buffer is of variable length. Prefixes the buffer with its length.
|
|
69
|
-
* @param buf - The buffer to serialize as a variable-length vector.
|
|
70
|
-
*/
|
|
71
|
-
public vector(buf: Buffer) {
|
|
72
|
-
this.buf.push(serializeBufferToVector(buf));
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Directly serializes a buffer that maybe of fixed, or variable length.
|
|
77
|
-
* It is assumed the corresponding deserialize function will handle variable length data, thus the length
|
|
78
|
-
* does not need to be prefixed here.
|
|
79
|
-
* If serializing a raw, variable length buffer, use vector().
|
|
80
|
-
* @param buf - The buffer to serialize as a fixed-length array.
|
|
81
|
-
*/
|
|
82
|
-
public buffer(buf: Buffer) {
|
|
83
|
-
this.buf.push(buf);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Serialize a string by first converting it to a buffer and then encoding its length as a prefix.
|
|
88
|
-
* The serialized string can be deserialized by reading the prefixed length and extracting the corresponding data.
|
|
89
|
-
* This method is useful for serializing strings of variable length in a consistent format.
|
|
90
|
-
*
|
|
91
|
-
* @param str - The input string to be serialized.
|
|
92
|
-
*/
|
|
93
|
-
public string(str: string) {
|
|
94
|
-
this.vector(Buffer.from(str));
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Serialize a given Date instance into a Buffer and append it to the internal buffer list.
|
|
99
|
-
* The serialized date is stored as an 8-byte BigInt representing the number of milliseconds since the Unix epoch.
|
|
100
|
-
* This function facilitates serialization of JavaScript's built-in Date objects for subsequent data transmission or storage.
|
|
101
|
-
*
|
|
102
|
-
* @param date - The Date instance to be serialized.
|
|
103
|
-
*/
|
|
104
|
-
public date(date: Date) {
|
|
105
|
-
this.buf.push(serializeDate(date));
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Returns the serialized Buffer object that was created by calling various serialization methods on this Serializer instance.
|
|
110
|
-
* The resulting buffer can be used for sending or storing serialized data in binary format.
|
|
111
|
-
*
|
|
112
|
-
* @returns A Buffer containing the serialized data.
|
|
113
|
-
*/
|
|
114
|
-
public getBuffer() {
|
|
115
|
-
return Buffer.concat(this.buf);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Serializes an array of elements, where each element has a 'toBuffer()' method, into a single Buffer.
|
|
120
|
-
* The resulting buffer is prefixed with its length (number of elements), allowing for easy deserialization.
|
|
121
|
-
* This method is useful for serializing arrays of custom classes or data structures that have their own serialization logic.
|
|
122
|
-
*
|
|
123
|
-
* @param arr - The array of elements to be serialized. Each element must have a 'toBuffer()' method for serialization.
|
|
124
|
-
*/
|
|
125
|
-
public serializeArray<T>(arr: T[]) {
|
|
126
|
-
this.buf.push(serializeBufferArrayToVector(arr.map((e: any) => e.toBuffer())));
|
|
127
|
-
}
|
|
128
|
-
}
|