@aztec/foundation 0.16.3 → 0.16.5
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/abi/function_selector.js +2 -2
- package/dest/aztec-address/index.js +2 -2
- package/dest/json-rpc/server/index.d.ts +1 -1
- package/dest/json-rpc/server/index.d.ts.map +1 -1
- package/dest/json-rpc/server/index.js +2 -2
- package/dest/json-rpc/server/json_rpc_server.d.ts +7 -0
- package/dest/json-rpc/server/json_rpc_server.d.ts.map +1 -1
- package/dest/json-rpc/server/json_rpc_server.js +13 -1
- package/package.json +2 -2
- package/src/abi/abi.ts +0 -310
- package/src/abi/abi_coder.ts +0 -25
- package/src/abi/decoder.ts +0 -161
- package/src/abi/encoder.ts +0 -120
- package/src/abi/function_selector.ts +0 -135
- package/src/abi/index.ts +0 -6
- package/src/abi/utils.ts +0 -28
- package/src/async-map/index.ts +0 -18
- package/src/aztec-address/index.ts +0 -30
- package/src/bigint-buffer/index.ts +0 -74
- package/src/collection/array.ts +0 -54
- package/src/collection/index.ts +0 -1
- package/src/committable/committable.ts +0 -46
- package/src/committable/index.ts +0 -1
- package/src/crypto/index.ts +0 -16
- package/src/crypto/keccak/index.ts +0 -33
- package/src/crypto/pedersen/index.ts +0 -1
- package/src/crypto/pedersen/pedersen.elliptic.ts +0 -584
- package/src/crypto/pedersen/pedersen.noble.ts +0 -573
- package/src/crypto/pedersen/pedersen.wasm.ts +0 -42
- package/src/crypto/random/index.ts +0 -42
- package/src/crypto/sha256/index.ts +0 -3
- package/src/errors/index.ts +0 -6
- package/src/eth-address/index.ts +0 -244
- package/src/fields/coordinate.ts +0 -104
- package/src/fields/fields.ts +0 -261
- package/src/fields/index.ts +0 -3
- package/src/fields/point.ts +0 -140
- package/src/fifo/bounded_serial_queue.ts +0 -100
- package/src/fifo/index.ts +0 -4
- package/src/fifo/memory_fifo.ts +0 -114
- package/src/fifo/semaphore.ts +0 -33
- package/src/fifo/serial_queue.ts +0 -78
- package/src/index.ts +0 -29
- package/src/json-rpc/README.md +0 -55
- package/src/json-rpc/class_converter.ts +0 -209
- package/src/json-rpc/client/index.ts +0 -1
- package/src/json-rpc/client/json_rpc_client.ts +0 -136
- package/src/json-rpc/convert.ts +0 -163
- package/src/json-rpc/fixtures/class_a.ts +0 -15
- package/src/json-rpc/fixtures/class_b.ts +0 -15
- package/src/json-rpc/fixtures/test_state.ts +0 -59
- package/src/json-rpc/index.ts +0 -8
- package/src/json-rpc/js_utils.ts +0 -20
- package/src/json-rpc/server/index.ts +0 -2
- package/src/json-rpc/server/json_proxy.ts +0 -43
- package/src/json-rpc/server/json_rpc_server.ts +0 -190
- package/src/log/console.ts +0 -39
- package/src/log/debug.ts +0 -83
- package/src/log/index.ts +0 -5
- package/src/log/log_fn.ts +0 -5
- package/src/log/log_history.ts +0 -44
- package/src/log/logger.ts +0 -137
- package/src/mutex/index.ts +0 -83
- package/src/mutex/mutex_database.ts +0 -12
- package/src/noir/index.ts +0 -1
- package/src/noir/noir_package_config.ts +0 -54
- package/src/retry/index.ts +0 -99
- package/src/running-promise/index.ts +0 -60
- package/src/serialize/buffer_reader.ts +0 -250
- package/src/serialize/free_funcs.ts +0 -279
- package/src/serialize/index.ts +0 -3
- package/src/serialize/types.ts +0 -40
- package/src/sleep/index.ts +0 -71
- package/src/testing/index.ts +0 -1
- package/src/testing/test_data.ts +0 -36
- package/src/timer/elapsed.ts +0 -23
- package/src/timer/index.ts +0 -3
- package/src/timer/timeout.ts +0 -64
- package/src/timer/timer.ts +0 -48
- package/src/transport/browser/index.ts +0 -4
- package/src/transport/browser/message_port_socket.ts +0 -48
- package/src/transport/browser/shared_worker_connector.ts +0 -21
- package/src/transport/browser/shared_worker_listener.ts +0 -53
- package/src/transport/browser/worker_connector.ts +0 -30
- package/src/transport/browser/worker_listener.ts +0 -54
- package/src/transport/dispatch/create_dispatch_fn.ts +0 -35
- package/src/transport/dispatch/create_dispatch_proxy.ts +0 -141
- package/src/transport/dispatch/messages.ts +0 -58
- package/src/transport/index.ts +0 -11
- package/src/transport/interface/connector.ts +0 -9
- package/src/transport/interface/listener.ts +0 -16
- package/src/transport/interface/socket.ts +0 -15
- package/src/transport/interface/transferable.ts +0 -125
- package/src/transport/node/index.ts +0 -2
- package/src/transport/node/node_connector.ts +0 -30
- package/src/transport/node/node_connector_socket.ts +0 -52
- package/src/transport/node/node_listener.ts +0 -34
- package/src/transport/node/node_listener_socket.ts +0 -48
- package/src/transport/transport_client.ts +0 -131
- package/src/transport/transport_server.ts +0 -108
- package/src/trees/index.ts +0 -48
- package/src/types/index.ts +0 -7
- package/src/url/index.ts +0 -73
- package/src/wasm/README.md +0 -6
- package/src/wasm/empty_wasi_sdk.ts +0 -166
- package/src/wasm/fixtures/gcd.wasm +0 -0
- package/src/wasm/fixtures/gcd.wat +0 -27
- package/src/wasm/index.ts +0 -1
- package/src/wasm/wasm_module.ts +0 -260
- package/src/worker/browser/index.ts +0 -2
- package/src/worker/browser/start_web_module.ts +0 -23
- package/src/worker/browser/web_data_store.ts +0 -38
- package/src/worker/browser/web_worker.ts +0 -24
- package/src/worker/data_store.ts +0 -19
- package/src/worker/index.ts +0 -2
- package/src/worker/node/index.ts +0 -2
- package/src/worker/node/node_data_store.ts +0 -27
- package/src/worker/node/node_worker.ts +0 -22
- package/src/worker/node/start_node_module.ts +0 -29
- package/src/worker/wasm_worker.ts +0 -7
- package/src/worker/worker_pool.ts +0 -73
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
import cors from '@koa/cors';
|
|
2
|
-
import http from 'http';
|
|
3
|
-
import Koa from 'koa';
|
|
4
|
-
import bodyParser from 'koa-bodyparser';
|
|
5
|
-
import compress from 'koa-compress';
|
|
6
|
-
import Router from 'koa-router';
|
|
7
|
-
|
|
8
|
-
import { createDebugLogger } from '../../log/index.js';
|
|
9
|
-
import { JsonClassConverterInput, StringClassConverterInput } from '../class_converter.js';
|
|
10
|
-
import { convertBigintsInObj } from '../convert.js';
|
|
11
|
-
import { JsonProxy } from './json_proxy.js';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* JsonRpcServer.
|
|
15
|
-
* Minimal, dev-friendly mechanism to create a server from an object.
|
|
16
|
-
*/
|
|
17
|
-
export class JsonRpcServer {
|
|
18
|
-
proxy: JsonProxy;
|
|
19
|
-
constructor(
|
|
20
|
-
private handler: object,
|
|
21
|
-
stringClassMap: StringClassConverterInput,
|
|
22
|
-
objectClassMap: JsonClassConverterInput,
|
|
23
|
-
private createApi: boolean,
|
|
24
|
-
private disallowedMethods: string[] = [],
|
|
25
|
-
private log = createDebugLogger('aztec:foundation:json-rpc:server'),
|
|
26
|
-
) {
|
|
27
|
-
this.proxy = new JsonProxy(handler, stringClassMap, objectClassMap);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Get an express app object.
|
|
32
|
-
* @param prefix - Our server prefix.
|
|
33
|
-
* @returns The app object.
|
|
34
|
-
*/
|
|
35
|
-
public getApp(prefix = '') {
|
|
36
|
-
const router = this.getRouter(prefix);
|
|
37
|
-
const exceptionHandler = async (ctx: Koa.Context, next: () => Promise<void>) => {
|
|
38
|
-
try {
|
|
39
|
-
await next();
|
|
40
|
-
} catch (err: any) {
|
|
41
|
-
this.log.error(err);
|
|
42
|
-
if (err instanceof SyntaxError) {
|
|
43
|
-
ctx.status = 400;
|
|
44
|
-
ctx.body = {
|
|
45
|
-
jsonrpc: '2.0',
|
|
46
|
-
id: null,
|
|
47
|
-
error: {
|
|
48
|
-
code: -32700,
|
|
49
|
-
message: 'Parse error',
|
|
50
|
-
},
|
|
51
|
-
};
|
|
52
|
-
} else {
|
|
53
|
-
ctx.status = 500;
|
|
54
|
-
ctx.body = {
|
|
55
|
-
jsonrpc: '2.0',
|
|
56
|
-
id: null,
|
|
57
|
-
error: {
|
|
58
|
-
code: -32603,
|
|
59
|
-
message: 'Internal error',
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
const app = new Koa();
|
|
66
|
-
app.on('error', error => {
|
|
67
|
-
this.log.error(`Error on API handler: ${error}`);
|
|
68
|
-
});
|
|
69
|
-
app.use(exceptionHandler);
|
|
70
|
-
app.use(compress({ br: false } as any));
|
|
71
|
-
app.use(
|
|
72
|
-
bodyParser({
|
|
73
|
-
jsonLimit: '10mb',
|
|
74
|
-
enableTypes: ['json'],
|
|
75
|
-
detectJSON: () => true,
|
|
76
|
-
}),
|
|
77
|
-
);
|
|
78
|
-
app.use(cors());
|
|
79
|
-
app.use(router.routes());
|
|
80
|
-
app.use(router.allowedMethods());
|
|
81
|
-
|
|
82
|
-
return app;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Get a router object wrapping our RPC class.
|
|
87
|
-
* @param prefix - The server prefix.
|
|
88
|
-
* @returns The router object.
|
|
89
|
-
*/
|
|
90
|
-
private getRouter(prefix: string) {
|
|
91
|
-
const router = new Router({ prefix });
|
|
92
|
-
const proto = Object.getPrototypeOf(this.handler);
|
|
93
|
-
// Find all our endpoints from the handler methods
|
|
94
|
-
|
|
95
|
-
if (this.createApi) {
|
|
96
|
-
// "API mode" where an endpoint is created for each method
|
|
97
|
-
for (const method of Object.getOwnPropertyNames(proto)) {
|
|
98
|
-
// Ignore if not a function or function is not allowed
|
|
99
|
-
if (
|
|
100
|
-
method === 'constructor' ||
|
|
101
|
-
typeof proto[method] !== 'function' ||
|
|
102
|
-
this.disallowedMethods.includes(method)
|
|
103
|
-
) {
|
|
104
|
-
continue;
|
|
105
|
-
}
|
|
106
|
-
router.post(`/${method}`, async (ctx: Koa.Context) => {
|
|
107
|
-
const { params = [], jsonrpc, id } = ctx.request.body as any;
|
|
108
|
-
try {
|
|
109
|
-
const result = await this.proxy.call(method, params);
|
|
110
|
-
ctx.body = {
|
|
111
|
-
jsonrpc,
|
|
112
|
-
id,
|
|
113
|
-
result: convertBigintsInObj(result),
|
|
114
|
-
};
|
|
115
|
-
ctx.status = 200;
|
|
116
|
-
} catch (err: any) {
|
|
117
|
-
// Propagate the error message to the client. Plenty of the errors are expected to occur (e.g. adding
|
|
118
|
-
// a duplicate recipient) so this is necessary.
|
|
119
|
-
ctx.status = 400;
|
|
120
|
-
ctx.body = {
|
|
121
|
-
jsonrpc,
|
|
122
|
-
id,
|
|
123
|
-
error: {
|
|
124
|
-
// TODO assign error codes - https://github.com/AztecProtocol/aztec-packages/issues/2633
|
|
125
|
-
code: -32000,
|
|
126
|
-
message: err.message,
|
|
127
|
-
},
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
} else {
|
|
133
|
-
// "JSON RPC mode" where a single endpoint is used and the method is given in the request body
|
|
134
|
-
router.post('/', async (ctx: Koa.Context) => {
|
|
135
|
-
const { params = [], jsonrpc, id, method } = ctx.request.body as any;
|
|
136
|
-
// Ignore if not a function
|
|
137
|
-
if (
|
|
138
|
-
method === 'constructor' ||
|
|
139
|
-
typeof proto[method] !== 'function' ||
|
|
140
|
-
this.disallowedMethods.includes(method)
|
|
141
|
-
) {
|
|
142
|
-
ctx.status = 400;
|
|
143
|
-
ctx.body = {
|
|
144
|
-
jsonrpc,
|
|
145
|
-
id,
|
|
146
|
-
error: {
|
|
147
|
-
code: -32601,
|
|
148
|
-
message: `Method not found: ${method}`,
|
|
149
|
-
},
|
|
150
|
-
};
|
|
151
|
-
} else {
|
|
152
|
-
try {
|
|
153
|
-
const result = await this.proxy.call(method, params);
|
|
154
|
-
ctx.body = {
|
|
155
|
-
jsonrpc,
|
|
156
|
-
id,
|
|
157
|
-
result: convertBigintsInObj(result),
|
|
158
|
-
};
|
|
159
|
-
ctx.status = 200;
|
|
160
|
-
} catch (err: any) {
|
|
161
|
-
// Propagate the error message to the client. Plenty of the errors are expected to occur (e.g. adding
|
|
162
|
-
// a duplicate recipient) so this is necessary.
|
|
163
|
-
ctx.status = 400;
|
|
164
|
-
ctx.body = {
|
|
165
|
-
jsonrpc,
|
|
166
|
-
id,
|
|
167
|
-
error: {
|
|
168
|
-
// TODO assign error codes - https://github.com/AztecProtocol/aztec-packages/issues/2633
|
|
169
|
-
code: -32000,
|
|
170
|
-
message: err.message,
|
|
171
|
-
},
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return router;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Start this server with koa.
|
|
183
|
-
* @param port - Port number.
|
|
184
|
-
* @param prefix - Prefix string.
|
|
185
|
-
*/
|
|
186
|
-
public start(port: number, prefix = '') {
|
|
187
|
-
const httpServer = http.createServer(this.getApp(prefix).callback());
|
|
188
|
-
httpServer.listen(port);
|
|
189
|
-
}
|
|
190
|
-
}
|
package/src/log/console.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
import { LogFn } from './log_fn.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* ConsoleLogger is a utility class that provides customizable console logging functionality.
|
|
6
|
-
* It allows setting a custom prefix for log messages and an optional custom logger function,
|
|
7
|
-
* which can be useful for controlling the format of the output or redirecting logs to a different destination.
|
|
8
|
-
*/
|
|
9
|
-
class ConsoleLogger {
|
|
10
|
-
constructor(private prefix: string, private logger: (...args: any[]) => void = console.log) {}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Log messages with the specified prefix using the provided logger.
|
|
14
|
-
* By default, it uses 'console.log' as the logger but can be overridden
|
|
15
|
-
* during ConsoleLogger instantiation. This method allows for easy
|
|
16
|
-
* organization and readability of log messages in the console.
|
|
17
|
-
*
|
|
18
|
-
* @param args - The data to be logged, any number of arguments can be passed to this function.
|
|
19
|
-
*/
|
|
20
|
-
public log(...args: any[]) {
|
|
21
|
-
this.logger(`${this.prefix}:`, ...args);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Creates a Logger function with an optional prefix for log messages.
|
|
27
|
-
* If a prefix is provided, the created logger will prepend it to each log message.
|
|
28
|
-
* If no prefix is provided, the default console.log will be returned.
|
|
29
|
-
*
|
|
30
|
-
* @param prefix - The optional string to prepend to each log message.
|
|
31
|
-
* @returns A Logger function that accepts any number of arguments and logs them with the specified prefix.
|
|
32
|
-
*/
|
|
33
|
-
export function createConsoleLogger(prefix?: string): LogFn {
|
|
34
|
-
if (prefix) {
|
|
35
|
-
const logger = new ConsoleLogger(prefix, console.log);
|
|
36
|
-
return (...args: any[]) => logger.log(...args);
|
|
37
|
-
}
|
|
38
|
-
return console.log;
|
|
39
|
-
}
|
package/src/log/debug.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import debug from 'debug';
|
|
2
|
-
|
|
3
|
-
import { LogFn } from './log_fn.js';
|
|
4
|
-
|
|
5
|
-
let preLogHook: ((...args: any[]) => void) | undefined;
|
|
6
|
-
let postLogHook: ((...args: any[]) => void) | undefined;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Process and handle the logging of messages through custom hooks and the given logger.
|
|
10
|
-
* This function checks if the logger's namespace is enabled, executes any preLogHook functions, logs the message using the provided logger, and then executes any postLogHook functions.
|
|
11
|
-
*
|
|
12
|
-
* @param logger - The debug logger instance to be used for logging.
|
|
13
|
-
* @param args - The arguments to be passed to the logger and any hook functions.
|
|
14
|
-
*/
|
|
15
|
-
function theFunctionThroughWhichAllLogsPass(logger: any, ...args: any[]) {
|
|
16
|
-
if (!debug.enabled(logger.namespace)) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
if (preLogHook) {
|
|
20
|
-
preLogHook(logger.namespace, ...args);
|
|
21
|
-
}
|
|
22
|
-
logger(...args);
|
|
23
|
-
if (postLogHook) {
|
|
24
|
-
postLogHook(logger.namespace, ...args);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Return a logger, meant to be silent by default and verbose during debugging.
|
|
30
|
-
* @param name - The module name of the logger.
|
|
31
|
-
* @returns A callable log function.
|
|
32
|
-
*/
|
|
33
|
-
export function createDebugOnlyLogger(name: string): LogFn {
|
|
34
|
-
const logger = debug(name);
|
|
35
|
-
return (...args: any[]) => theFunctionThroughWhichAllLogsPass(logger, ...args);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Set a function to be called before each log message is handled by the debug logger.
|
|
40
|
-
* The hook function will receive the logger namespace and any arguments passed to the logger.
|
|
41
|
-
* This can be useful for adding additional context, filtering logs, or performing side-effects
|
|
42
|
-
* based on logged messages.
|
|
43
|
-
*
|
|
44
|
-
* @param fn - The function to be called before each log message.
|
|
45
|
-
*/
|
|
46
|
-
export function setPreDebugLogHook(fn: (...args: any[]) => void) {
|
|
47
|
-
preLogHook = fn;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Set a callback function to be executed after each log is written by the debug logger.
|
|
52
|
-
* This allows additional behavior or side effects to occur after a log has been written,
|
|
53
|
-
* such as sending logs to external services, formatting output, or triggering events.
|
|
54
|
-
*
|
|
55
|
-
* @param fn - The callback function to be executed after each log. It receives the same arguments as the original log function call.
|
|
56
|
-
*/
|
|
57
|
-
export function setPostDebugLogHook(fn: (...args: any[]) => void) {
|
|
58
|
-
postLogHook = fn;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Enable logs for the specified namespace(s) or wildcard pattern(s).
|
|
63
|
-
* This function activates the logging functionality for the given
|
|
64
|
-
* namespace(s) or pattern(s), allowing developers to selectively display
|
|
65
|
-
* debug logs that match the provided string(s).
|
|
66
|
-
*
|
|
67
|
-
* @param str - The namespace(s) or wildcard pattern(s) for which logs should be enabled.
|
|
68
|
-
*/
|
|
69
|
-
export function enableLogs(str: string) {
|
|
70
|
-
debug.enable(str);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Check if the logging is enabled for a given namespace.
|
|
75
|
-
* The input 'str' represents the namespace for which the log status is being checked.
|
|
76
|
-
* Returns true if the logging is enabled, otherwise false.
|
|
77
|
-
*
|
|
78
|
-
* @param str - The namespace string used to determine if logging is enabled.
|
|
79
|
-
* @returns A boolean indicating whether logging is enabled for the given namespace.
|
|
80
|
-
*/
|
|
81
|
-
export function isLogEnabled(str: string) {
|
|
82
|
-
return debug.enabled(str);
|
|
83
|
-
}
|
package/src/log/index.ts
DELETED
package/src/log/log_fn.ts
DELETED
package/src/log/log_history.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { setPreDebugLogHook } from './debug.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* LogHistory is a utility class that provides the ability to store and manage debug logs.
|
|
5
|
-
* It can be enabled to record logs along with their timestamps, retrieve a specified number
|
|
6
|
-
* of recent logs, or clear stored logs based on a given count. This can be useful for debugging
|
|
7
|
-
* purposes, monitoring application activities, and maintaining log history.
|
|
8
|
-
*/
|
|
9
|
-
export class LogHistory {
|
|
10
|
-
private logs: any[][] = [];
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Enables the logging of debug messages with timestamps.
|
|
14
|
-
* Hooks into the pre-debug log and stores each log entry along with its timestamp in the logs array.
|
|
15
|
-
*/
|
|
16
|
-
public enable() {
|
|
17
|
-
setPreDebugLogHook((...args: any[]) => {
|
|
18
|
-
this.logs.push([new Date().toISOString(), ...args]);
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Retrieves a specified number of logs from the end of the log history or all logs if no argument is provided.
|
|
24
|
-
* The logs are ordered chronologically, with the oldest logs at the beginning of the array.
|
|
25
|
-
*
|
|
26
|
-
* @param last - Optional number representing the amount of recent logs to return. Defaults to 0, which returns all logs.
|
|
27
|
-
* @returns An array of log arrays, each containing a timestamp and log arguments.
|
|
28
|
-
*/
|
|
29
|
-
public getLogs(last = 0) {
|
|
30
|
-
return last ? this.logs.slice(-last) : this.logs;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Clear a specified number of logs from the beginning of the logs array.
|
|
35
|
-
* If no count is provided, it will clear all logs.
|
|
36
|
-
*
|
|
37
|
-
* @param count - The number of logs to be removed (default: total logs length).
|
|
38
|
-
*/
|
|
39
|
-
public clear(count = this.logs.length) {
|
|
40
|
-
this.logs = this.logs.slice(count);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export const logHistory = new LogHistory();
|
package/src/log/logger.ts
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import debug from 'debug';
|
|
2
|
-
import isNode from 'detect-node';
|
|
3
|
-
import { isatty } from 'tty';
|
|
4
|
-
|
|
5
|
-
import { LogData, LogFn } from './log_fn.js';
|
|
6
|
-
|
|
7
|
-
// Matches a subset of Winston log levels
|
|
8
|
-
const LogLevels = ['silent', 'error', 'warn', 'info', 'verbose', 'debug'] as const;
|
|
9
|
-
const DefaultLogLevel = process.env.NODE_ENV === 'test' ? ('silent' as const) : ('info' as const);
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* A valid log severity level.
|
|
13
|
-
*/
|
|
14
|
-
export type LogLevel = (typeof LogLevels)[number];
|
|
15
|
-
|
|
16
|
-
const envLogLevel = process.env.LOG_LEVEL?.toLowerCase() as LogLevel;
|
|
17
|
-
const currentLevel = LogLevels.includes(envLogLevel) ? envLogLevel : DefaultLogLevel;
|
|
18
|
-
|
|
19
|
-
/** Log function that accepts an exception object */
|
|
20
|
-
type ErrorLogFn = (msg: string, err?: Error | unknown, data?: LogData) => void;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Logger that supports multiple severity levels.
|
|
24
|
-
*/
|
|
25
|
-
export type Logger = { [K in LogLevel]: LogFn } & { /** Error log function */ error: ErrorLogFn };
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Logger that supports multiple severity levels and can be called directly to issue a debug statement.
|
|
29
|
-
* Intended as a drop-in replacement for the debug module.
|
|
30
|
-
*/
|
|
31
|
-
export type DebugLogger = LogFn & Logger;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Creates a new DebugLogger for the current module, defaulting to the LOG_LEVEL env var.
|
|
35
|
-
* If DEBUG="[module]" env is set, will enable debug logging if the module matches.
|
|
36
|
-
* Uses npm debug for debug level and console.error for other levels.
|
|
37
|
-
* @param name - Name of the module.
|
|
38
|
-
* @returns A debug logger.
|
|
39
|
-
*/
|
|
40
|
-
export function createDebugLogger(name: string): DebugLogger {
|
|
41
|
-
const debugLogger = debug(name);
|
|
42
|
-
if (currentLevel === 'debug') {
|
|
43
|
-
debugLogger.enabled = true;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const logger = {
|
|
47
|
-
silent: () => {},
|
|
48
|
-
error: (msg: string, err?: unknown, data?: LogData) => logWithDebug(debugLogger, 'error', fmtErr(msg, err), data),
|
|
49
|
-
warn: (msg: string, data?: LogData) => logWithDebug(debugLogger, 'warn', msg, data),
|
|
50
|
-
info: (msg: string, data?: LogData) => logWithDebug(debugLogger, 'info', msg, data),
|
|
51
|
-
verbose: (msg: string, data?: LogData) => logWithDebug(debugLogger, 'verbose', msg, data),
|
|
52
|
-
debug: (msg: string, data?: LogData) => logWithDebug(debugLogger, 'debug', msg, data),
|
|
53
|
-
};
|
|
54
|
-
return Object.assign((msg: string, data?: LogData) => logWithDebug(debugLogger, 'debug', msg, data), logger);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** A callback to capture all logs. */
|
|
58
|
-
export type LogHandler = (level: LogLevel, namespace: string, msg: string, data?: LogData) => void;
|
|
59
|
-
|
|
60
|
-
const logHandlers: LogHandler[] = [];
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Registers a callback for all logs, whether they are emitted in the current log level or not.
|
|
64
|
-
* @param handler - Callback to be called on every log.
|
|
65
|
-
*/
|
|
66
|
-
export function onLog(handler: LogHandler) {
|
|
67
|
-
logHandlers.push(handler);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Logs args to npm debug if enabled or log level is debug, console.error otherwise.
|
|
72
|
-
* @param debug - Instance of npm debug.
|
|
73
|
-
* @param level - Intended log level.
|
|
74
|
-
* @param args - Args to log.
|
|
75
|
-
*/
|
|
76
|
-
function logWithDebug(debug: debug.Debugger, level: LogLevel, msg: string, data?: LogData) {
|
|
77
|
-
for (const handler of logHandlers) {
|
|
78
|
-
handler(level, debug.namespace, msg, data);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
msg = data ? `${msg} ${fmtLogData(data)}` : msg;
|
|
82
|
-
if (debug.enabled) {
|
|
83
|
-
if (level !== 'debug') {
|
|
84
|
-
msg = `${level.toUpperCase()} ${msg}`;
|
|
85
|
-
}
|
|
86
|
-
debug(msg);
|
|
87
|
-
} else if (LogLevels.indexOf(level) <= LogLevels.indexOf(currentLevel)) {
|
|
88
|
-
printLog(`${getPrefix(debug, level)} ${msg}`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Returns a log prefix that emulates that of npm debug. Uses colors if in node and in a tty.
|
|
94
|
-
* @param debugLogger - Instance of npm debug logger.
|
|
95
|
-
* @param level - Intended log level (printed out if strictly above current log level).
|
|
96
|
-
* @returns Log prefix.
|
|
97
|
-
*/
|
|
98
|
-
function getPrefix(debugLogger: debug.Debugger, level: LogLevel) {
|
|
99
|
-
const levelLabel = currentLevel !== level ? ` ${level.toUpperCase()}` : '';
|
|
100
|
-
const prefix = `${debugLogger.namespace.replace(/^aztec:/, '')}${levelLabel}`;
|
|
101
|
-
if (!isNode || !isatty(process.stderr.fd)) {
|
|
102
|
-
return prefix;
|
|
103
|
-
}
|
|
104
|
-
const colorIndex = debug.selectColor(debugLogger.namespace) as number;
|
|
105
|
-
const colorCode = '\u001B[3' + (colorIndex < 8 ? colorIndex : '8;5;' + colorIndex);
|
|
106
|
-
return ` ${colorCode};1m${prefix}\u001B[0m`;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Outputs to console error.
|
|
111
|
-
* @param msg - What to log.
|
|
112
|
-
*/
|
|
113
|
-
function printLog(msg: string) {
|
|
114
|
-
// eslint-disable-next-line no-console
|
|
115
|
-
console.error(msg);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Concatenates a log message and an exception.
|
|
120
|
-
* @param msg - Log message
|
|
121
|
-
* @param err - Error to log
|
|
122
|
-
* @returns A string with both the log message and the error message.
|
|
123
|
-
*/
|
|
124
|
-
function fmtErr(msg: string, err?: Error | unknown): string {
|
|
125
|
-
const errStr = err && [(err as Error).name, (err as Error).message].filter(x => !!x).join(' ');
|
|
126
|
-
return err ? `${msg}: ${errStr || err}` : msg;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Formats structured log data as a string for console output.
|
|
131
|
-
* @param data - Optional log data.
|
|
132
|
-
*/
|
|
133
|
-
function fmtLogData(data?: LogData): string {
|
|
134
|
-
return Object.entries(data ?? {})
|
|
135
|
-
.map(([key, value]) => `${key}=${value}`)
|
|
136
|
-
.join(' ');
|
|
137
|
-
}
|
package/src/mutex/index.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
}
|
package/src/noir/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './noir_package_config.js';
|
|
@@ -1,54 +0,0 @@
|
|
|
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
|
-
}
|