@aztec/foundation 0.23.0 → 0.24.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/dest/fields/fields.js +3 -3
- package/package.json +2 -2
- package/src/abi/abi.ts +337 -0
- package/src/abi/buffer.ts +36 -0
- package/src/abi/decoder.ts +176 -0
- package/src/abi/encoder.ts +143 -0
- package/src/abi/index.ts +6 -0
- package/src/abi/selector.ts +243 -0
- package/src/abi/utils.ts +50 -0
- package/src/array/array.ts +86 -0
- package/src/array/index.ts +1 -0
- package/src/async-map/index.ts +18 -0
- package/src/aztec-address/index.ts +36 -0
- package/src/bigint-buffer/index.ts +87 -0
- package/src/collection/array.ts +64 -0
- package/src/collection/index.ts +1 -0
- package/src/committable/committable.ts +46 -0
- package/src/committable/index.ts +1 -0
- package/src/crypto/index.ts +16 -0
- package/src/crypto/keccak/index.ts +33 -0
- package/src/crypto/pedersen/index.ts +1 -0
- package/src/crypto/pedersen/pedersen.elliptic.ts +584 -0
- package/src/crypto/pedersen/pedersen.noble.ts +573 -0
- package/src/crypto/pedersen/pedersen.wasm.ts +42 -0
- package/src/crypto/random/index.ts +42 -0
- package/src/crypto/sha256/index.ts +3 -0
- package/src/errors/index.ts +6 -0
- package/src/eth-address/index.ts +234 -0
- package/src/fields/coordinate.ts +104 -0
- package/src/fields/fields.ts +328 -0
- package/src/fields/index.ts +3 -0
- package/src/fields/point.ts +145 -0
- package/src/fifo/bounded_serial_queue.ts +100 -0
- package/src/fifo/index.ts +4 -0
- package/src/fifo/memory_fifo.ts +118 -0
- package/src/fifo/semaphore.ts +33 -0
- package/src/fifo/serial_queue.ts +81 -0
- package/src/index.ts +29 -0
- package/src/json-rpc/README.md +55 -0
- package/src/json-rpc/class_converter.ts +213 -0
- package/src/json-rpc/client/index.ts +1 -0
- package/src/json-rpc/client/json_rpc_client.ts +147 -0
- package/src/json-rpc/convert.ts +163 -0
- package/src/json-rpc/fixtures/class_a.ts +15 -0
- package/src/json-rpc/fixtures/class_b.ts +15 -0
- package/src/json-rpc/fixtures/test_state.ts +59 -0
- package/src/json-rpc/index.ts +8 -0
- package/src/json-rpc/js_utils.ts +20 -0
- package/src/json-rpc/server/index.ts +2 -0
- package/src/json-rpc/server/json_proxy.ts +60 -0
- package/src/json-rpc/server/json_rpc_server.ts +269 -0
- package/src/log/console.ts +39 -0
- package/src/log/debug.ts +83 -0
- package/src/log/index.ts +5 -0
- package/src/log/log_fn.ts +5 -0
- package/src/log/log_history.ts +44 -0
- package/src/log/logger.ts +137 -0
- package/src/mutex/index.ts +83 -0
- package/src/mutex/mutex_database.ts +12 -0
- package/src/noir/index.ts +1 -0
- package/src/noir/noir_package_config.ts +54 -0
- package/src/retry/index.ts +99 -0
- package/src/running-promise/index.ts +60 -0
- package/src/serialize/buffer_reader.ts +286 -0
- package/src/serialize/field_reader.ts +143 -0
- package/src/serialize/free_funcs.ts +147 -0
- package/src/serialize/index.ts +5 -0
- package/src/serialize/serialize.ts +303 -0
- package/src/serialize/types.ts +40 -0
- package/src/sleep/index.ts +71 -0
- package/src/testing/index.ts +1 -0
- package/src/testing/test_data.ts +36 -0
- package/src/timer/elapsed.ts +23 -0
- package/src/timer/index.ts +3 -0
- package/src/timer/timeout.ts +64 -0
- package/src/timer/timer.ts +48 -0
- package/src/transport/browser/index.ts +4 -0
- package/src/transport/browser/message_port_socket.ts +48 -0
- package/src/transport/browser/shared_worker_connector.ts +21 -0
- package/src/transport/browser/shared_worker_listener.ts +53 -0
- package/src/transport/browser/worker_connector.ts +30 -0
- package/src/transport/browser/worker_listener.ts +54 -0
- package/src/transport/dispatch/create_dispatch_fn.ts +35 -0
- package/src/transport/dispatch/create_dispatch_proxy.ts +141 -0
- package/src/transport/dispatch/messages.ts +58 -0
- package/src/transport/index.ts +11 -0
- package/src/transport/interface/connector.ts +9 -0
- package/src/transport/interface/listener.ts +16 -0
- package/src/transport/interface/socket.ts +15 -0
- package/src/transport/interface/transferable.ts +125 -0
- package/src/transport/node/index.ts +2 -0
- package/src/transport/node/node_connector.ts +30 -0
- package/src/transport/node/node_connector_socket.ts +52 -0
- package/src/transport/node/node_listener.ts +34 -0
- package/src/transport/node/node_listener_socket.ts +48 -0
- package/src/transport/transport_client.ts +131 -0
- package/src/transport/transport_server.ts +108 -0
- package/src/trees/index.ts +54 -0
- package/src/types/index.ts +8 -0
- package/src/url/index.ts +73 -0
- package/src/wasm/README.md +6 -0
- package/src/wasm/empty_wasi_sdk.ts +166 -0
- package/src/wasm/fixtures/gcd.wasm +0 -0
- package/src/wasm/fixtures/gcd.wat +27 -0
- package/src/wasm/index.ts +1 -0
- package/src/wasm/wasm_module.ts +260 -0
- package/src/worker/browser/index.ts +2 -0
- package/src/worker/browser/start_web_module.ts +23 -0
- package/src/worker/browser/web_data_store.ts +38 -0
- package/src/worker/browser/web_worker.ts +24 -0
- package/src/worker/data_store.ts +19 -0
- package/src/worker/index.ts +2 -0
- package/src/worker/node/index.ts +2 -0
- package/src/worker/node/node_data_store.ts +27 -0
- package/src/worker/node/node_worker.ts +22 -0
- package/src/worker/node/start_node_module.ts +29 -0
- package/src/worker/wasm_worker.ts +7 -0
- package/src/worker/worker_pool.ts +73 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { MutexDatabase } from './mutex_database.js';
|
|
2
|
+
|
|
3
|
+
export * from './mutex_database.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Mutex class provides a mutual exclusion mechanism for critical sections of code using a named lock.
|
|
7
|
+
* The lock is acquired and released via the `lock` and `unlock` methods. Locks can be optionally pinged
|
|
8
|
+
* to keep them alive when they are held for longer durations, avoiding unintended release due to timeouts.
|
|
9
|
+
*
|
|
10
|
+
* The underlying lock state is managed in a MutexDatabase instance which can be shared across multiple Mutex instances.
|
|
11
|
+
* This allows for synchronization between different parts of an application or even across different instances of an application.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const mutex = new Mutex(mutexDatabase, 'myLock');
|
|
15
|
+
* await mutex.lock();
|
|
16
|
+
* // Critical section here
|
|
17
|
+
* await mutex.unlock();
|
|
18
|
+
*/
|
|
19
|
+
export class Mutex {
|
|
20
|
+
private id = 0;
|
|
21
|
+
private pingTimeout!: NodeJS.Timeout;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
private readonly db: MutexDatabase,
|
|
25
|
+
private readonly name: string,
|
|
26
|
+
private readonly timeout = 5000,
|
|
27
|
+
private readonly tryLockInterval = 2000,
|
|
28
|
+
private readonly pingInterval = 2000,
|
|
29
|
+
) {}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Acquire a lock on the mutex. If 'untilAcquired' is true, the method will keep trying to acquire the lock until it
|
|
33
|
+
* successfully acquires it. If 'untilAcquired' is false, the method will try to acquire the lock once and return
|
|
34
|
+
* immediately with a boolean indicating if the lock has been acquired or not.
|
|
35
|
+
*
|
|
36
|
+
* @param untilAcquired - Optional parameter, set to true by default. If true, the method will keep trying to acquire the lock until success. If false, the method will try only once and return a boolean value.
|
|
37
|
+
* @returns A Promise that resolves to true if the lock has been acquired, or false when 'untilAcquired' is false and the lock could not be immediately acquired.
|
|
38
|
+
*/
|
|
39
|
+
public async lock(untilAcquired = true) {
|
|
40
|
+
while (true) {
|
|
41
|
+
if (await this.db.acquireLock(this.name, this.timeout)) {
|
|
42
|
+
const id = this.id;
|
|
43
|
+
this.pingTimeout = setTimeout(() => this.ping(id), this.pingInterval);
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!untilAcquired) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
await new Promise(resolve => setTimeout(resolve, this.tryLockInterval));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Unlocks the mutex, allowing other instances to acquire the lock.
|
|
56
|
+
* This method also clears the internal ping timeout and increments the internal ID
|
|
57
|
+
* to ensure stale pings do not extend the lock after it has been released.
|
|
58
|
+
*
|
|
59
|
+
* @returns A promise that resolves once the lock has been released in the database.
|
|
60
|
+
*/
|
|
61
|
+
public async unlock() {
|
|
62
|
+
clearTimeout(this.pingTimeout);
|
|
63
|
+
this.id++;
|
|
64
|
+
await this.db.releaseLock(this.name);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Periodically extends the lock's lifetime by updating the database record with a new expiration time.
|
|
69
|
+
* This method is called recursively using setTimeout. If the id passed to the ping method does not match
|
|
70
|
+
* the current lock instance's id, it means the lock has been released or acquired by another instance
|
|
71
|
+
* and the ping should not proceed further.
|
|
72
|
+
*
|
|
73
|
+
* @param id - The id of the current lock instance.
|
|
74
|
+
*/
|
|
75
|
+
private async ping(id: number) {
|
|
76
|
+
if (id !== this.id) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
await this.db.extendLock(this.name, this.timeout);
|
|
81
|
+
this.pingTimeout = setTimeout(() => this.ping(id), this.pingInterval);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a mutual exclusion (mutex) database interface.
|
|
3
|
+
* Provides functionality for acquiring, extending, and releasing locks on resources to ensure exclusive access and prevent conflicts in concurrent applications.
|
|
4
|
+
*/
|
|
5
|
+
export interface MutexDatabase {
|
|
6
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
7
|
+
acquireLock(name: string, timeout: number): Promise<boolean>;
|
|
8
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
9
|
+
extendLock(name: string, timeout: number): Promise<void>;
|
|
10
|
+
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
11
|
+
releaseLock(name: string): Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './noir_package_config.js';
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
const noirGitDependencySchema = z.object({
|
|
4
|
+
git: z.string(),
|
|
5
|
+
tag: z.string(),
|
|
6
|
+
directory: z.string().optional(),
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
const noirLocalDependencySchema = z.object({
|
|
10
|
+
path: z.string(),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const noirPackageConfigSchema = z.object({
|
|
14
|
+
package: z.object({
|
|
15
|
+
name: z.string().default(''),
|
|
16
|
+
type: z.enum(['lib', 'contract', 'bin']).default('bin'),
|
|
17
|
+
entry: z.string().optional(),
|
|
18
|
+
description: z.string().optional(),
|
|
19
|
+
authors: z.array(z.string()).optional(),
|
|
20
|
+
// eslint-disable-next-line camelcase
|
|
21
|
+
compiler_version: z.string().optional(),
|
|
22
|
+
backend: z.string().optional(),
|
|
23
|
+
license: z.string().optional(),
|
|
24
|
+
}),
|
|
25
|
+
dependencies: z.record(z.union([noirGitDependencySchema, noirLocalDependencySchema])).default({}),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Noir package configuration.
|
|
30
|
+
*/
|
|
31
|
+
export type NoirPackageConfig = z.infer<typeof noirPackageConfigSchema>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A remote package dependency.
|
|
35
|
+
*/
|
|
36
|
+
export type NoirGitDependencyConfig = z.infer<typeof noirGitDependencySchema>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A local package dependency.
|
|
40
|
+
*/
|
|
41
|
+
export type NoirLocalDependencyConfig = z.infer<typeof noirLocalDependencySchema>;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A package dependency.
|
|
45
|
+
*/
|
|
46
|
+
export type NoirDependencyConfig = NoirGitDependencyConfig | NoirLocalDependencyConfig;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Checks that an object is a package configuration.
|
|
50
|
+
* @param config - Config to check
|
|
51
|
+
*/
|
|
52
|
+
export function parseNoirPackageConfig(config: any): NoirPackageConfig {
|
|
53
|
+
return noirPackageConfigSchema.parse(config);
|
|
54
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { createDebugLogger } from '../log/index.js';
|
|
2
|
+
import { sleep } from '../sleep/index.js';
|
|
3
|
+
import { Timer } from '../timer/index.js';
|
|
4
|
+
|
|
5
|
+
/** An error that indicates that the operation should not be retried. */
|
|
6
|
+
export class NoRetryError extends Error {}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Generates a backoff sequence for retrying operations with an increasing delay.
|
|
10
|
+
* The backoff sequence follows this pattern: 1, 1, 1, 2, 4, 8, 16, 32, 64, ...
|
|
11
|
+
* This generator can be used in combination with the `retry` function to perform
|
|
12
|
+
* retries with exponential backoff and capped at 64 seconds between attempts.
|
|
13
|
+
*
|
|
14
|
+
* @returns A generator that yields the next backoff value in seconds as an integer.
|
|
15
|
+
*/
|
|
16
|
+
export function* backoffGenerator() {
|
|
17
|
+
const v = [1, 1, 1, 2, 4, 8, 16, 32, 64];
|
|
18
|
+
let i = 0;
|
|
19
|
+
while (true) {
|
|
20
|
+
yield v[Math.min(i++, v.length - 1)];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Generates a backoff sequence based on the array of retry intervals to use with the `retry` function.
|
|
26
|
+
* @param retries - Intervals to retry (in seconds).
|
|
27
|
+
* @returns A generator sequence.
|
|
28
|
+
*/
|
|
29
|
+
export function* makeBackoff(retries: number[]) {
|
|
30
|
+
for (const retry of retries) {
|
|
31
|
+
yield retry;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Retry a given asynchronous function with a specific backoff strategy, until it succeeds or backoff generator ends.
|
|
37
|
+
* It logs the error and retry interval in case an error is caught. The function can be named for better log output.
|
|
38
|
+
*
|
|
39
|
+
* @param fn - The asynchronous function to be retried.
|
|
40
|
+
* @param name - The optional name of the operation, used for logging purposes.
|
|
41
|
+
* @param backoff - The optional backoff generator providing the intervals in seconds between retries. Defaults to a predefined series.
|
|
42
|
+
* @param log - Logger to use for logging.
|
|
43
|
+
* @param failSilently - Do not log errors while retrying.
|
|
44
|
+
* @returns A Promise that resolves with the successful result of the provided function, or rejects if backoff generator ends.
|
|
45
|
+
* @throws If `NoRetryError` is thrown by the `fn`, it is rethrown.
|
|
46
|
+
*/
|
|
47
|
+
export async function retry<Result>(
|
|
48
|
+
fn: () => Promise<Result>,
|
|
49
|
+
name = 'Operation',
|
|
50
|
+
backoff = backoffGenerator(),
|
|
51
|
+
log = createDebugLogger('aztec:foundation:retry'),
|
|
52
|
+
failSilently = false,
|
|
53
|
+
) {
|
|
54
|
+
while (true) {
|
|
55
|
+
try {
|
|
56
|
+
return await fn();
|
|
57
|
+
} catch (err: any) {
|
|
58
|
+
if (err instanceof NoRetryError) {
|
|
59
|
+
// A special error that indicates that the operation should not be retried. Rethrow it.
|
|
60
|
+
throw err;
|
|
61
|
+
}
|
|
62
|
+
const s = backoff.next().value;
|
|
63
|
+
if (s === undefined) {
|
|
64
|
+
throw err;
|
|
65
|
+
}
|
|
66
|
+
log(`${name} failed. Will retry in ${s}s...`);
|
|
67
|
+
!failSilently && log.error(err);
|
|
68
|
+
await sleep(s * 1000);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Retry an asynchronous function until it returns a truthy value or the specified timeout is exceeded.
|
|
76
|
+
* The function is retried periodically with a fixed interval between attempts. The operation can be named for better error messages.
|
|
77
|
+
* Will never timeout if the value is 0.
|
|
78
|
+
*
|
|
79
|
+
* @param fn - The asynchronous function to be retried, which should return a truthy value upon success or undefined otherwise.
|
|
80
|
+
* @param name - The optional name of the operation, used for generating timeout error message.
|
|
81
|
+
* @param timeout - The optional maximum time, in seconds, to keep retrying before throwing a timeout error. Defaults to 0 (never timeout).
|
|
82
|
+
* @param interval - The optional interval, in seconds, between retry attempts. Defaults to 1 second.
|
|
83
|
+
* @returns A Promise that resolves with the successful (truthy) result of the provided function, or rejects if timeout is exceeded.
|
|
84
|
+
*/
|
|
85
|
+
export async function retryUntil<T>(fn: () => Promise<T | undefined>, name = '', timeout = 0, interval = 1) {
|
|
86
|
+
const timer = new Timer();
|
|
87
|
+
while (true) {
|
|
88
|
+
const result = await fn();
|
|
89
|
+
if (result) {
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
await sleep(interval * 1000);
|
|
94
|
+
|
|
95
|
+
if (timeout && timer.s() > timeout) {
|
|
96
|
+
throw new Error(name ? `Timeout awaiting ${name}` : 'Timeout');
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RunningPromise is a utility class that helps manage the execution of an asynchronous function
|
|
3
|
+
* at a specified polling interval. It allows starting, stopping, and checking the status of the
|
|
4
|
+
* internally managed promise. The class also supports interrupting the polling process when stopped.
|
|
5
|
+
*/
|
|
6
|
+
export class RunningPromise {
|
|
7
|
+
private running = false;
|
|
8
|
+
private runningPromise = Promise.resolve();
|
|
9
|
+
private interruptPromise = Promise.resolve();
|
|
10
|
+
private interruptResolve = () => {};
|
|
11
|
+
constructor(private fn: () => Promise<void>, private pollingInterval = 10000) {}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Starts the running promise.
|
|
15
|
+
*/
|
|
16
|
+
public start() {
|
|
17
|
+
this.running = true;
|
|
18
|
+
this.interruptPromise = new Promise(resolve => (this.interruptResolve = resolve));
|
|
19
|
+
|
|
20
|
+
const poll = async () => {
|
|
21
|
+
while (this.running) {
|
|
22
|
+
await this.fn();
|
|
23
|
+
await this.interruptibleSleep(this.pollingInterval);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
this.runningPromise = poll();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Stops the running promise, resolves any pending interruptible sleep,
|
|
31
|
+
* and waits for the currently executing function to complete.
|
|
32
|
+
*/
|
|
33
|
+
async stop(): Promise<void> {
|
|
34
|
+
this.running = false;
|
|
35
|
+
this.interruptResolve();
|
|
36
|
+
await this.runningPromise;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A sleep function that can be interrupted before the specified time.
|
|
41
|
+
* The sleep duration is determined by 'timeInMs', and it can be terminated early if the 'interruptPromise' is resolved.
|
|
42
|
+
* @param timeInMs - The time in milliseconds.
|
|
43
|
+
*/
|
|
44
|
+
private async interruptibleSleep(timeInMs: number) {
|
|
45
|
+
let timeout!: NodeJS.Timeout;
|
|
46
|
+
const sleepPromise = new Promise(resolve => {
|
|
47
|
+
timeout = setTimeout(resolve, timeInMs);
|
|
48
|
+
});
|
|
49
|
+
await Promise.race([sleepPromise, this.interruptPromise]);
|
|
50
|
+
clearTimeout(timeout);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Checks if the running promise is currently active.
|
|
55
|
+
* @returns True if the promise is running.
|
|
56
|
+
*/
|
|
57
|
+
public isRunning() {
|
|
58
|
+
return this.running;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { Tuple } from './types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The BufferReader class provides a utility for reading various data types from a buffer.
|
|
5
|
+
* It supports reading numbers, booleans, byte arrays, Fr and Fq field elements,
|
|
6
|
+
* vectors, arrays, objects, strings, and maps. It maintains an internal index to
|
|
7
|
+
* keep track of the current reading position in the buffer.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* Create a new instance of BufferReader with a buffer and an optional offset.
|
|
11
|
+
* Use the provided methods to read desired data types from the buffer.
|
|
12
|
+
* The reading methods automatically advance the internal index.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const reader = new BufferReader(someBuffer);
|
|
16
|
+
* const num = reader.readNumber();
|
|
17
|
+
* const bool = reader.readBoolean();
|
|
18
|
+
* const byteArray = reader.readBytes(4);
|
|
19
|
+
*/
|
|
20
|
+
export class BufferReader {
|
|
21
|
+
private index: number;
|
|
22
|
+
constructor(private buffer: Buffer, offset = 0) {
|
|
23
|
+
this.index = offset;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Creates a BufferReader instance from either a Buffer or an existing BufferReader.
|
|
28
|
+
* If the input is a Buffer, it creates a new BufferReader with the given buffer.
|
|
29
|
+
* If the input is already a BufferReader, it returns the input unchanged.
|
|
30
|
+
*
|
|
31
|
+
* @param bufferOrReader - A Buffer or BufferReader to initialize the BufferReader.
|
|
32
|
+
* @returns An instance of BufferReader.
|
|
33
|
+
*/
|
|
34
|
+
public static asReader(bufferOrReader: Uint8Array | Buffer | BufferReader): BufferReader {
|
|
35
|
+
if (bufferOrReader instanceof BufferReader) {
|
|
36
|
+
return bufferOrReader;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const buf = Buffer.isBuffer(bufferOrReader)
|
|
40
|
+
? bufferOrReader
|
|
41
|
+
: Buffer.from(bufferOrReader.buffer, bufferOrReader.byteOffset, bufferOrReader.byteLength);
|
|
42
|
+
|
|
43
|
+
return new BufferReader(buf);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Reads a 32-bit unsigned integer from the buffer at the current index position.
|
|
48
|
+
* Updates the index position by 4 bytes after reading the number.
|
|
49
|
+
*
|
|
50
|
+
* @returns The read 32-bit unsigned integer value.
|
|
51
|
+
*/
|
|
52
|
+
public readNumber(): number {
|
|
53
|
+
this.index += 4;
|
|
54
|
+
return this.buffer.readUint32BE(this.index - 4);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Reads `count` 32-bit unsigned integers from the buffer at the current index position.
|
|
59
|
+
* @param count - The number of 32-bit unsigned integers to read.
|
|
60
|
+
* @returns An array of 32-bit unsigned integers.
|
|
61
|
+
*/
|
|
62
|
+
public readNumbers<N extends number>(count: N): Tuple<number, N> {
|
|
63
|
+
const result = Array.from({ length: count }, () => this.readNumber());
|
|
64
|
+
return result as Tuple<number, N>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Reads a 16-bit unsigned integer from the buffer at the current index position.
|
|
69
|
+
* Updates the index position by 2 bytes after reading the number.
|
|
70
|
+
*
|
|
71
|
+
* @returns The read 16 bit value.
|
|
72
|
+
*/
|
|
73
|
+
public readUInt16(): number {
|
|
74
|
+
this.index += 2;
|
|
75
|
+
return this.buffer.readUInt16BE(this.index - 2);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Reads a 8-bit unsigned integer from the buffer at the current index position.
|
|
80
|
+
* Updates the index position by 1 byte after reading the number.
|
|
81
|
+
*
|
|
82
|
+
* @returns The read 8 bit value.
|
|
83
|
+
*/
|
|
84
|
+
public readUInt8(): number {
|
|
85
|
+
this.index += 1;
|
|
86
|
+
return this.buffer.readUInt8(this.index - 1);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Reads and returns the next boolean value from the buffer.
|
|
91
|
+
* Advances the internal index by 1, treating the byte at the current index as a boolean value.
|
|
92
|
+
* Returns true if the byte is non-zero, false otherwise.
|
|
93
|
+
*
|
|
94
|
+
* @returns A boolean value representing the byte at the current index.
|
|
95
|
+
*/
|
|
96
|
+
public readBoolean(): boolean {
|
|
97
|
+
this.index += 1;
|
|
98
|
+
return Boolean(this.buffer.at(this.index - 1));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Reads a specified number of bytes from the buffer and returns a new Buffer containing those bytes.
|
|
103
|
+
* Advances the reader's index by the number of bytes read. Throws an error if there are not enough
|
|
104
|
+
* bytes left in the buffer to satisfy the requested number of bytes.
|
|
105
|
+
*
|
|
106
|
+
* @param n - The number of bytes to read from the buffer.
|
|
107
|
+
* @returns A new Buffer containing the read bytes.
|
|
108
|
+
*/
|
|
109
|
+
public readBytes(n: number): Buffer {
|
|
110
|
+
this.index += n;
|
|
111
|
+
return Buffer.from(this.buffer.subarray(this.index - n, this.index));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Reads until the end of the buffer. */
|
|
115
|
+
public readToEnd(): Buffer {
|
|
116
|
+
const result = this.buffer.subarray(this.index);
|
|
117
|
+
this.index = this.buffer.length;
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Reads a vector of numbers from the buffer and returns it as an array of numbers.
|
|
123
|
+
* The method utilizes the 'readVector' method, passing a deserializer that reads numbers.
|
|
124
|
+
*
|
|
125
|
+
* @returns An array of numbers representing the vector read from the buffer.
|
|
126
|
+
*/
|
|
127
|
+
public readNumberVector(): number[] {
|
|
128
|
+
return this.readVector({
|
|
129
|
+
fromBuffer: (reader: BufferReader) => reader.readNumber(),
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Reads a vector of fixed size from the buffer and deserializes its elements using the provided itemDeserializer object.
|
|
135
|
+
* The 'itemDeserializer' object should have a 'fromBuffer' method that takes a BufferReader instance and returns the deserialized element.
|
|
136
|
+
* The method first reads the size of the vector (a number) from the buffer, then iterates through its elements,
|
|
137
|
+
* deserializing each one using the 'fromBuffer' method of 'itemDeserializer'.
|
|
138
|
+
*
|
|
139
|
+
* @param itemDeserializer - Object with 'fromBuffer' method to deserialize vector elements.
|
|
140
|
+
* @returns An array of deserialized elements of type T.
|
|
141
|
+
*/
|
|
142
|
+
public readVector<T>(itemDeserializer: {
|
|
143
|
+
/**
|
|
144
|
+
* A method to deserialize data from a buffer.
|
|
145
|
+
*/
|
|
146
|
+
fromBuffer: (reader: BufferReader) => T;
|
|
147
|
+
}): T[] {
|
|
148
|
+
const size = this.readNumber();
|
|
149
|
+
const result = new Array<T>(size);
|
|
150
|
+
for (let i = 0; i < size; i++) {
|
|
151
|
+
result[i] = itemDeserializer.fromBuffer(this);
|
|
152
|
+
}
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Read an array of a fixed size with elements of type T from the buffer.
|
|
158
|
+
* The 'itemDeserializer' object should have a 'fromBuffer' method that takes a BufferReader instance as input,
|
|
159
|
+
* and returns an instance of the desired deserialized data type T.
|
|
160
|
+
* This method will call the 'fromBuffer' method for each element in the array and return the resulting array.
|
|
161
|
+
*
|
|
162
|
+
* @param size - The fixed number of elements in the array.
|
|
163
|
+
* @param itemDeserializer - An object with a 'fromBuffer' method to deserialize individual elements of type T.
|
|
164
|
+
* @returns An array of instances of type T.
|
|
165
|
+
*/
|
|
166
|
+
public readArray<T, N extends number>(
|
|
167
|
+
size: N,
|
|
168
|
+
itemDeserializer: {
|
|
169
|
+
/**
|
|
170
|
+
* A function for deserializing data from a BufferReader instance.
|
|
171
|
+
*/
|
|
172
|
+
fromBuffer: (reader: BufferReader) => T;
|
|
173
|
+
},
|
|
174
|
+
): Tuple<T, N> {
|
|
175
|
+
const result = Array.from({ length: size }, () => itemDeserializer.fromBuffer(this));
|
|
176
|
+
return result as Tuple<T, N>;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Read a variable sized Buffer array where elements are represented by length + data.
|
|
181
|
+
* The method consecutively looks for a number which is the size of the proceeding buffer,
|
|
182
|
+
* then reads the bytes until it reaches the end of the reader's internal buffer.
|
|
183
|
+
* NOTE: if `size` is not provided, this will run to the end of the reader's buffer.
|
|
184
|
+
* @param size - Size of the buffer array in bytes (full remaining buffer length if left empty).
|
|
185
|
+
* @returns An array of variable sized buffers.
|
|
186
|
+
*/
|
|
187
|
+
public readBufferArray(size = -1): Buffer[] {
|
|
188
|
+
const result: Buffer[] = [];
|
|
189
|
+
const end = size >= 0 ? this.index + size : this.buffer.length;
|
|
190
|
+
while (this.index < end) {
|
|
191
|
+
const item = this.readBuffer();
|
|
192
|
+
result.push(item);
|
|
193
|
+
}
|
|
194
|
+
// Ensure that all bytes have been read.
|
|
195
|
+
if (this.index !== end) {
|
|
196
|
+
throw new Error(
|
|
197
|
+
`Reader buffer was not fully consumed. Consumed up to ${this.index} bytes. End of data: ${end} bytes.`,
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Reads a serialized object from a buffer and returns the deserialized object using the given deserializer.
|
|
205
|
+
*
|
|
206
|
+
* @typeparam T - The type of the deserialized object.
|
|
207
|
+
* @param deserializer - An object with a 'fromBuffer' method that takes a BufferReader instance and returns an instance of the deserialized object.
|
|
208
|
+
* @returns The deserialized object of type T.
|
|
209
|
+
*/
|
|
210
|
+
public readObject<T>(deserializer: {
|
|
211
|
+
/**
|
|
212
|
+
* A method that takes a BufferReader instance and returns an instance of the deserialized data type.
|
|
213
|
+
*/
|
|
214
|
+
fromBuffer: (reader: BufferReader) => T;
|
|
215
|
+
}): T {
|
|
216
|
+
return deserializer.fromBuffer(this);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Returns a Buffer containing the next n bytes from the current buffer without modifying the reader's index position.
|
|
221
|
+
* If n is not provided or exceeds the remaining length of the buffer, it returns all bytes from the current position till the end of the buffer.
|
|
222
|
+
*
|
|
223
|
+
* @param n - The number of bytes to peek from the current buffer. (Optional).
|
|
224
|
+
* @returns A Buffer with the next n bytes or the remaining bytes if n is not provided or exceeds the buffer length.
|
|
225
|
+
*/
|
|
226
|
+
public peekBytes(n?: number): Buffer {
|
|
227
|
+
return this.buffer.subarray(this.index, n ? this.index + n : undefined);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Reads a string from the buffer and returns it.
|
|
232
|
+
* The method first reads the size of the string, then reads the corresponding
|
|
233
|
+
* number of bytes from the buffer and converts them to a string.
|
|
234
|
+
*
|
|
235
|
+
* @returns The read string from the buffer.
|
|
236
|
+
*/
|
|
237
|
+
public readString(): string {
|
|
238
|
+
return this.readBuffer().toString();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Reads a buffer from the current position of the reader and advances the index.
|
|
243
|
+
* The method first reads the size (number) of bytes to be read, and then returns
|
|
244
|
+
* a Buffer with that size containing the bytes. Useful for reading variable-length
|
|
245
|
+
* binary data encoded as (size, data) format.
|
|
246
|
+
*
|
|
247
|
+
* @returns A Buffer containing the read bytes.
|
|
248
|
+
*/
|
|
249
|
+
public readBuffer(): Buffer {
|
|
250
|
+
const size = this.readNumber();
|
|
251
|
+
return this.readBytes(size);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Reads and constructs a map object from the current buffer using the provided deserializer.
|
|
256
|
+
* The method reads the number of entries in the map, followed by iterating through each key-value pair.
|
|
257
|
+
* The key is read as a string, while the value is obtained using the passed deserializer's `fromBuffer` method.
|
|
258
|
+
* The resulting map object is returned, containing all the key-value pairs read from the buffer.
|
|
259
|
+
*
|
|
260
|
+
* @param deserializer - An object with a `fromBuffer` method to deserialize the values in the map.
|
|
261
|
+
* @returns A map object with string keys and deserialized values based on the provided deserializer.
|
|
262
|
+
*/
|
|
263
|
+
public readMap<T>(deserializer: {
|
|
264
|
+
/**
|
|
265
|
+
* Deserializes an element of type T from a BufferReader instance.
|
|
266
|
+
*/
|
|
267
|
+
fromBuffer: (reader: BufferReader) => T;
|
|
268
|
+
}): { [key: string]: T } {
|
|
269
|
+
const numEntries = this.readNumber();
|
|
270
|
+
const map: { [key: string]: T } = {};
|
|
271
|
+
for (let i = 0; i < numEntries; i++) {
|
|
272
|
+
const key = this.readString();
|
|
273
|
+
const value = this.readObject<T>(deserializer);
|
|
274
|
+
map[key] = value;
|
|
275
|
+
}
|
|
276
|
+
return map;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Get the length of the reader's buffer.
|
|
281
|
+
* @returns The length of the underlying reader's buffer.
|
|
282
|
+
*/
|
|
283
|
+
public getLength(): number {
|
|
284
|
+
return this.buffer.length;
|
|
285
|
+
}
|
|
286
|
+
}
|