@aztec/foundation 0.0.1-commit.e61ad554 → 0.0.1-commit.ec5f612
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/array/sorted_array.d.ts +6 -1
- package/dest/array/sorted_array.d.ts.map +1 -1
- package/dest/array/sorted_array.js +18 -15
- package/dest/collection/array.d.ts +7 -1
- package/dest/collection/array.d.ts.map +1 -1
- package/dest/collection/array.js +24 -0
- package/dest/config/env_var.d.ts +2 -2
- package/dest/config/env_var.d.ts.map +1 -1
- package/dest/config/network_name.d.ts +2 -2
- package/dest/config/network_name.d.ts.map +1 -1
- package/dest/config/network_name.js +2 -0
- package/dest/crypto/poseidon/index.js +13 -13
- package/dest/crypto/random/randomness_singleton.d.ts +4 -3
- package/dest/crypto/random/randomness_singleton.d.ts.map +1 -1
- package/dest/crypto/random/randomness_singleton.js +4 -4
- package/dest/curves/bn254/field.d.ts +2 -1
- package/dest/curves/bn254/field.d.ts.map +1 -1
- package/dest/curves/bn254/field.js +5 -2
- package/dest/jest/setup.js +4 -1
- package/dest/json-rpc/client/safe_json_rpc_client.d.ts +2 -1
- package/dest/json-rpc/client/safe_json_rpc_client.d.ts.map +1 -1
- package/dest/json-rpc/client/safe_json_rpc_client.js +1 -1
- package/dest/json-rpc/server/api_key_auth.d.ts +19 -0
- package/dest/json-rpc/server/api_key_auth.d.ts.map +1 -0
- package/dest/json-rpc/server/api_key_auth.js +57 -0
- package/dest/json-rpc/server/index.d.ts +2 -1
- package/dest/json-rpc/server/index.d.ts.map +1 -1
- package/dest/json-rpc/server/index.js +1 -0
- package/dest/log/bigint-utils.d.ts +5 -0
- package/dest/log/bigint-utils.d.ts.map +1 -0
- package/dest/log/bigint-utils.js +18 -0
- package/dest/log/gcloud-logger-config.d.ts +1 -1
- package/dest/log/gcloud-logger-config.d.ts.map +1 -1
- package/dest/log/gcloud-logger-config.js +3 -0
- package/dest/log/libp2p_logger.d.ts +5 -2
- package/dest/log/libp2p_logger.d.ts.map +1 -1
- package/dest/log/libp2p_logger.js +14 -4
- package/dest/log/log-filters.d.ts +17 -4
- package/dest/log/log-filters.d.ts.map +1 -1
- package/dest/log/log-filters.js +26 -12
- package/dest/log/pino-logger-server.d.ts +9 -0
- package/dest/log/pino-logger-server.d.ts.map +1 -0
- package/dest/log/pino-logger-server.js +18 -0
- package/dest/log/pino-logger.d.ts +37 -8
- package/dest/log/pino-logger.d.ts.map +1 -1
- package/dest/log/pino-logger.js +121 -28
- package/dest/queue/base_memory_queue.d.ts +2 -2
- package/dest/queue/base_memory_queue.d.ts.map +1 -1
- package/dest/queue/semaphore.d.ts +5 -1
- package/dest/queue/semaphore.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.d.ts +8 -1
- package/dest/serialize/buffer_reader.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.js +13 -0
- package/dest/serialize/serialize.d.ts +19 -1
- package/dest/serialize/serialize.d.ts.map +1 -1
- package/dest/serialize/serialize.js +31 -0
- package/dest/timer/date.d.ts +25 -1
- package/dest/timer/date.d.ts.map +1 -1
- package/dest/timer/date.js +33 -0
- package/dest/transport/transport_client.js +2 -2
- package/dest/types/index.d.ts +3 -1
- package/dest/types/index.d.ts.map +1 -1
- package/dest/types/index.js +3 -0
- package/package.json +23 -2
- package/src/array/sorted_array.ts +22 -17
- package/src/collection/array.ts +21 -0
- package/src/config/env_var.ts +38 -2
- package/src/config/network_name.ts +4 -1
- package/src/crypto/poseidon/index.ts +13 -13
- package/src/crypto/random/randomness_singleton.ts +9 -5
- package/src/curves/bn254/field.ts +6 -2
- package/src/jest/setup.mjs +4 -1
- package/src/json-rpc/client/safe_json_rpc_client.ts +2 -0
- package/src/json-rpc/server/api_key_auth.ts +63 -0
- package/src/json-rpc/server/index.ts +1 -0
- package/src/log/bigint-utils.ts +22 -0
- package/src/log/gcloud-logger-config.ts +5 -0
- package/src/log/libp2p_logger.ts +12 -5
- package/src/log/log-filters.ts +29 -11
- package/src/log/pino-logger-server.ts +25 -0
- package/src/log/pino-logger.ts +142 -38
- package/src/queue/base_memory_queue.ts +1 -1
- package/src/queue/semaphore.ts +5 -0
- package/src/serialize/buffer_reader.ts +15 -0
- package/src/serialize/serialize.ts +32 -0
- package/src/timer/date.ts +48 -0
- package/src/transport/transport_client.ts +2 -2
- package/src/types/index.ts +5 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Barretenberg } from '@aztec/bb.js';
|
|
2
2
|
|
|
3
3
|
import { Fr } from '../../curves/bn254/field.js';
|
|
4
4
|
import { type Fieldable, serializeToFields } from '../../serialize/serialize.js';
|
|
@@ -10,9 +10,9 @@ import { type Fieldable, serializeToFields } from '../../serialize/serialize.js'
|
|
|
10
10
|
*/
|
|
11
11
|
export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
|
|
12
12
|
const inputFields = serializeToFields(input);
|
|
13
|
-
await
|
|
14
|
-
const api =
|
|
15
|
-
const response = api.poseidon2Hash({
|
|
13
|
+
await Barretenberg.initSingleton();
|
|
14
|
+
const api = Barretenberg.getSingleton();
|
|
15
|
+
const response = await api.poseidon2Hash({
|
|
16
16
|
inputs: inputFields.map(i => i.toBuffer()),
|
|
17
17
|
});
|
|
18
18
|
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
@@ -27,9 +27,9 @@ export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
|
|
|
27
27
|
export async function poseidon2HashWithSeparator(input: Fieldable[], separator: number): Promise<Fr> {
|
|
28
28
|
const inputFields = serializeToFields(input);
|
|
29
29
|
inputFields.unshift(new Fr(separator));
|
|
30
|
-
await
|
|
31
|
-
const api =
|
|
32
|
-
const response = api.poseidon2Hash({
|
|
30
|
+
await Barretenberg.initSingleton();
|
|
31
|
+
const api = Barretenberg.getSingleton();
|
|
32
|
+
const response = await api.poseidon2Hash({
|
|
33
33
|
inputs: inputFields.map(i => i.toBuffer()),
|
|
34
34
|
});
|
|
35
35
|
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
@@ -44,9 +44,9 @@ export async function poseidon2Permutation(input: Fieldable[]): Promise<Fr[]> {
|
|
|
44
44
|
const inputFields = serializeToFields(input);
|
|
45
45
|
// We'd like this assertion but it's not possible to use it in the browser.
|
|
46
46
|
// assert(input.length === 4, 'Input state must be of size 4');
|
|
47
|
-
await
|
|
48
|
-
const api =
|
|
49
|
-
const response = api.poseidon2Permutation({
|
|
47
|
+
await Barretenberg.initSingleton();
|
|
48
|
+
const api = Barretenberg.getSingleton();
|
|
49
|
+
const response = await api.poseidon2Permutation({
|
|
50
50
|
inputs: inputFields.map(i => i.toBuffer()),
|
|
51
51
|
});
|
|
52
52
|
// We'd like this assertion but it's not possible to use it in the browser.
|
|
@@ -65,9 +65,9 @@ export async function poseidon2HashBytes(input: Buffer): Promise<Fr> {
|
|
|
65
65
|
inputFields.push(Fr.fromBuffer(fieldBytes));
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
await
|
|
69
|
-
const api =
|
|
70
|
-
const response = api.poseidon2Hash({
|
|
68
|
+
await Barretenberg.initSingleton();
|
|
69
|
+
const api = Barretenberg.getSingleton();
|
|
70
|
+
const response = await api.poseidon2Hash({
|
|
71
71
|
inputs: inputFields.map(i => i.toBuffer()),
|
|
72
72
|
});
|
|
73
73
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createLogger } from '../../log/pino-logger.js';
|
|
1
|
+
import { type Logger, type LoggerBindings, createLogger } from '../../log/pino-logger.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A number generator which is used as a source of randomness in the system. If the SEED env variable is set, the
|
|
@@ -12,9 +12,13 @@ export class RandomnessSingleton {
|
|
|
12
12
|
private static instance: RandomnessSingleton;
|
|
13
13
|
|
|
14
14
|
private counter = 0;
|
|
15
|
-
private
|
|
15
|
+
private log: Logger;
|
|
16
16
|
|
|
17
|
-
private constructor(
|
|
17
|
+
private constructor(
|
|
18
|
+
private readonly seed?: number,
|
|
19
|
+
bindings?: LoggerBindings,
|
|
20
|
+
) {
|
|
21
|
+
this.log = createLogger('foundation:randomness_singleton', bindings);
|
|
18
22
|
if (seed !== undefined) {
|
|
19
23
|
this.log.debug(`Using pseudo-randomness with seed: ${seed}`);
|
|
20
24
|
this.counter = seed;
|
|
@@ -23,10 +27,10 @@ export class RandomnessSingleton {
|
|
|
23
27
|
}
|
|
24
28
|
}
|
|
25
29
|
|
|
26
|
-
public static getInstance(): RandomnessSingleton {
|
|
30
|
+
public static getInstance(bindings?: LoggerBindings): RandomnessSingleton {
|
|
27
31
|
if (!RandomnessSingleton.instance) {
|
|
28
32
|
const seed = process.env.SEED ? Number(process.env.SEED) : undefined;
|
|
29
|
-
RandomnessSingleton.instance = new RandomnessSingleton(seed);
|
|
33
|
+
RandomnessSingleton.instance = new RandomnessSingleton(seed, bindings);
|
|
30
34
|
}
|
|
31
35
|
|
|
32
36
|
return RandomnessSingleton.instance;
|
|
@@ -118,14 +118,18 @@ abstract class BaseField {
|
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
cmp(rhs: BaseField): -1 | 0 | 1 {
|
|
121
|
-
|
|
122
|
-
return this.asBigInt === rhsBigInt ? 0 : this.asBigInt < rhsBigInt ? -1 : 1;
|
|
121
|
+
return BaseField.cmpAsBigInt(this.asBigInt, rhs.asBigInt);
|
|
123
122
|
}
|
|
124
123
|
|
|
125
124
|
static cmp(lhs: BaseField, rhs: BaseField): -1 | 0 | 1 {
|
|
126
125
|
return lhs.cmp(rhs);
|
|
127
126
|
}
|
|
128
127
|
|
|
128
|
+
// Actual bigint comparison. Arguments must have been validated previously.
|
|
129
|
+
static cmpAsBigInt(lhs: bigint, rhs: bigint): -1 | 0 | 1 {
|
|
130
|
+
return lhs === rhs ? 0 : lhs < rhs ? -1 : 1;
|
|
131
|
+
}
|
|
132
|
+
|
|
129
133
|
isZero(): boolean {
|
|
130
134
|
return this.asBigInt === 0n;
|
|
131
135
|
}
|
package/src/jest/setup.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parseBooleanEnv } from '@aztec/foundation/config';
|
|
1
2
|
import { overwriteLoggingStream, pinoPrettyOpts } from '@aztec/foundation/log';
|
|
2
3
|
|
|
3
4
|
import pretty from 'pino-pretty';
|
|
@@ -6,4 +7,6 @@ import pretty from 'pino-pretty';
|
|
|
6
7
|
// file so we don't mess up with dependencies in non-testing environments,
|
|
7
8
|
// since pino-pretty messes up with browser bundles.
|
|
8
9
|
// See also https://www.npmjs.com/package/pino-pretty?activeTab=readme#user-content-usage-with-jest
|
|
9
|
-
|
|
10
|
+
if (!parseBooleanEnv(process.env.LOG_JSON)) {
|
|
11
|
+
overwriteLoggingStream(pretty(pinoPrettyOpts));
|
|
12
|
+
}
|
|
@@ -24,6 +24,7 @@ export type SafeJsonRpcClientOptions = {
|
|
|
24
24
|
batchWindowMS?: number;
|
|
25
25
|
maxBatchSize?: number;
|
|
26
26
|
maxRequestBodySize?: number;
|
|
27
|
+
extraHeaders?: Record<string, string>;
|
|
27
28
|
onResponse?: (res: {
|
|
28
29
|
response: any;
|
|
29
30
|
headers: { get: (header: string) => string | null | undefined };
|
|
@@ -129,6 +130,7 @@ export function createSafeJsonRpcClient<T extends object>(
|
|
|
129
130
|
const { response, headers } = await fetch(
|
|
130
131
|
host,
|
|
131
132
|
rpcCalls.map(({ request }) => request),
|
|
133
|
+
config.extraHeaders,
|
|
132
134
|
);
|
|
133
135
|
|
|
134
136
|
if (config.onResponse) {
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { timingSafeEqual } from 'crypto';
|
|
2
|
+
import type Koa from 'koa';
|
|
3
|
+
|
|
4
|
+
import { sha256 } from '../../crypto/sha256/index.js';
|
|
5
|
+
import { createLogger } from '../../log/index.js';
|
|
6
|
+
|
|
7
|
+
const log = createLogger('json-rpc:api-key-auth');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Computes the SHA-256 hash of a string and returns it as a Buffer.
|
|
11
|
+
* @param input - The input string to hash.
|
|
12
|
+
* @returns The SHA-256 hash as a Buffer.
|
|
13
|
+
*/
|
|
14
|
+
export function sha256Hash(input: string): Buffer {
|
|
15
|
+
return sha256(Buffer.from(input));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Creates a Koa middleware that enforces API key authentication on all requests
|
|
20
|
+
* except the health check endpoint (GET /status).
|
|
21
|
+
*
|
|
22
|
+
* The API key can be provided via the `x-api-key` header or the `Authorization: Bearer <key>` header.
|
|
23
|
+
* Comparison is done by hashing the provided key with SHA-256 and comparing against the stored hash.
|
|
24
|
+
*
|
|
25
|
+
* @param apiKeyHash - The SHA-256 hash of the expected API key as a Buffer.
|
|
26
|
+
* @returns A Koa middleware that rejects requests without a valid API key.
|
|
27
|
+
*/
|
|
28
|
+
export function getApiKeyAuthMiddleware(
|
|
29
|
+
apiKeyHash: Buffer,
|
|
30
|
+
): (ctx: Koa.Context, next: () => Promise<void>) => Promise<void> {
|
|
31
|
+
return async (ctx: Koa.Context, next: () => Promise<void>) => {
|
|
32
|
+
// Allow health check through without auth
|
|
33
|
+
if (ctx.path === '/status' && ctx.method === 'GET') {
|
|
34
|
+
return next();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const providedKey = ctx.get('x-api-key') || ctx.get('authorization')?.replace(/^Bearer\s+/i, '');
|
|
38
|
+
if (!providedKey) {
|
|
39
|
+
log.warn(`Rejected admin RPC request from ${ctx.ip}: missing API key`);
|
|
40
|
+
ctx.status = 401;
|
|
41
|
+
ctx.body = {
|
|
42
|
+
jsonrpc: '2.0',
|
|
43
|
+
id: null,
|
|
44
|
+
error: { code: -32000, message: 'Unauthorized: invalid or missing API key' },
|
|
45
|
+
};
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const providedHashBuf = sha256Hash(providedKey);
|
|
50
|
+
if (!timingSafeEqual(apiKeyHash, providedHashBuf)) {
|
|
51
|
+
log.warn(`Rejected admin RPC request from ${ctx.ip}: invalid API key`);
|
|
52
|
+
ctx.status = 401;
|
|
53
|
+
ctx.body = {
|
|
54
|
+
jsonrpc: '2.0',
|
|
55
|
+
id: null,
|
|
56
|
+
error: { code: -32000, message: 'Unauthorized: invalid or missing API key' },
|
|
57
|
+
};
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
await next();
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts bigint values to strings recursively in a log object to avoid serialization issues.
|
|
3
|
+
*/
|
|
4
|
+
export function convertBigintsToStrings(obj: unknown): unknown {
|
|
5
|
+
if (typeof obj === 'bigint') {
|
|
6
|
+
return String(obj);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (Array.isArray(obj)) {
|
|
10
|
+
return obj.map(item => convertBigintsToStrings(item));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (obj !== null && typeof obj === 'object') {
|
|
14
|
+
const result: Record<string, unknown> = {};
|
|
15
|
+
for (const key in obj) {
|
|
16
|
+
result[key] = convertBigintsToStrings((obj as Record<string, unknown>)[key]);
|
|
17
|
+
}
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return obj;
|
|
22
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { pino } from 'pino';
|
|
2
2
|
|
|
3
|
+
import { convertBigintsToStrings } from './bigint-utils.js';
|
|
4
|
+
|
|
3
5
|
/* eslint-disable camelcase */
|
|
4
6
|
|
|
5
7
|
const GOOGLE_CLOUD_TRACE_ID = 'logging.googleapis.com/trace';
|
|
@@ -15,6 +17,9 @@ export const GoogleCloudLoggerConfig = {
|
|
|
15
17
|
messageKey: 'message',
|
|
16
18
|
formatters: {
|
|
17
19
|
log(object: Record<string, unknown>): Record<string, unknown> {
|
|
20
|
+
// Convert bigints to strings recursively to avoid serialization issues
|
|
21
|
+
object = convertBigintsToStrings(object) as Record<string, unknown>;
|
|
22
|
+
|
|
18
23
|
// Add trace context attributes following Cloud Logging structured log format described
|
|
19
24
|
// in https://cloud.google.com/logging/docs/structured-logging#special-payload-fields
|
|
20
25
|
const { trace_id, span_id, trace_flags, ...rest } = object;
|
package/src/log/libp2p_logger.ts
CHANGED
|
@@ -2,15 +2,17 @@ import type { ComponentLogger, Logger } from '@libp2p/interface';
|
|
|
2
2
|
|
|
3
3
|
import { getLogLevelFromFilters } from './log-filters.js';
|
|
4
4
|
import type { LogLevel } from './log-levels.js';
|
|
5
|
-
import { logFilters, logger } from './pino-logger.js';
|
|
5
|
+
import { type LoggerBindings, logFilters, logger } from './pino-logger.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Creates a libp2p compatible logger that wraps our pino logger.
|
|
9
9
|
* This adapter implements the ComponentLogger interface required by libp2p.
|
|
10
|
+
* @param namespace - Base namespace for the logger
|
|
11
|
+
* @param bindings - Optional bindings to pass to the logger (actor, instanceId)
|
|
10
12
|
*/
|
|
11
|
-
export function createLibp2pComponentLogger(namespace: string): ComponentLogger {
|
|
13
|
+
export function createLibp2pComponentLogger(namespace: string, bindings?: LoggerBindings): ComponentLogger {
|
|
12
14
|
return {
|
|
13
|
-
forComponent: (component: string) => createLibp2pLogger(`${namespace}:${component}
|
|
15
|
+
forComponent: (component: string) => createLibp2pLogger(`${namespace}:${component}`, bindings),
|
|
14
16
|
};
|
|
15
17
|
}
|
|
16
18
|
|
|
@@ -24,9 +26,14 @@ function replaceFormatting(message: string) {
|
|
|
24
26
|
return message.replace(/(%p|%a)/g, '%s');
|
|
25
27
|
}
|
|
26
28
|
|
|
27
|
-
function createLibp2pLogger(component: string): Logger {
|
|
29
|
+
function createLibp2pLogger(component: string, bindings?: LoggerBindings): Logger {
|
|
28
30
|
// Create a direct pino logger instance for libp2p that supports string interpolation
|
|
29
|
-
const
|
|
31
|
+
const actor = bindings?.actor;
|
|
32
|
+
const instanceId = bindings?.instanceId;
|
|
33
|
+
const log = logger.child(
|
|
34
|
+
{ module: component, ...(actor && { actor }), ...(instanceId && { instanceId }) },
|
|
35
|
+
{ level: getLogLevelFromFilters(logFilters, component) },
|
|
36
|
+
);
|
|
30
37
|
|
|
31
38
|
const logIfEnabled = (level: LogLevel, message: string, ...args: unknown[]) => {
|
|
32
39
|
if (!log.isLevelEnabled(level)) {
|
package/src/log/log-filters.ts
CHANGED
|
@@ -19,22 +19,40 @@ export function getLogLevelFromFilters(filters: LogFilters, module: string): Log
|
|
|
19
19
|
return undefined;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Parses the LOG_LEVEL env string into a default level and per-module filter overrides.
|
|
24
|
+
*
|
|
25
|
+
* Format: `<default_level>;<level>:<module1>,<module2>;<level>:<module3>;...`
|
|
26
|
+
* - First segment (before the first `;`) is the default log level for all modules.
|
|
27
|
+
* - Remaining segments are `level:module` pairs: apply the given level to the listed modules (comma-separated).
|
|
28
|
+
* - Later filters override earlier ones for overlapping module matches.
|
|
29
|
+
* - The `aztec:` prefix is stripped from module names; spaces are trimmed.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* parseLogLevel('debug;warn:module1,module2;error:module3', 'info')
|
|
34
|
+
* // => ['debug', [['module3', 'error'], ['module2', 'warn'], ['module1', 'warn']]]
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function parseLogLevelEnvVar(
|
|
38
|
+
logLevelEnvVar: string | undefined,
|
|
39
|
+
defaultLevel: LogLevel,
|
|
40
|
+
): [LogLevel, LogFilters] {
|
|
41
|
+
if (!logLevelEnvVar) {
|
|
42
|
+
return [defaultLevel, []];
|
|
25
43
|
}
|
|
44
|
+
const [level] = logLevelEnvVar.split(';', 1);
|
|
45
|
+
assertValidLogLevel(level);
|
|
46
|
+
return [level, parseFilters(logLevelEnvVar.slice(level.length + 1))];
|
|
26
47
|
}
|
|
27
48
|
|
|
28
|
-
|
|
29
|
-
if (!
|
|
30
|
-
|
|
49
|
+
function assertValidLogLevel(level: string): asserts level is LogLevel {
|
|
50
|
+
if (!LogLevels.includes(level as LogLevel)) {
|
|
51
|
+
throw new Error(`Invalid log level: ${level}`);
|
|
31
52
|
}
|
|
32
|
-
const [level] = env.split(';', 1);
|
|
33
|
-
assertLogLevel(level);
|
|
34
|
-
return [level, parseFilters(env.slice(level.length + 1))];
|
|
35
53
|
}
|
|
36
54
|
|
|
37
|
-
|
|
55
|
+
function parseFilters(definition: string | undefined): LogFilters {
|
|
38
56
|
if (!definition) {
|
|
39
57
|
return [];
|
|
40
58
|
}
|
|
@@ -48,7 +66,7 @@ export function parseFilters(definition: string | undefined): LogFilters {
|
|
|
48
66
|
throw new Error(`Invalid log filter statement: ${statement}`);
|
|
49
67
|
}
|
|
50
68
|
const sanitizedLevel = level.trim().toLowerCase();
|
|
51
|
-
|
|
69
|
+
assertValidLogLevel(sanitizedLevel);
|
|
52
70
|
for (const module of modules.split(',')) {
|
|
53
71
|
filters.push([
|
|
54
72
|
module
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
|
|
3
|
+
import { type LoggerBindings, addLogBindingsHandler, removeLogBindingsHandler } from './pino-logger.js';
|
|
4
|
+
|
|
5
|
+
/** AsyncLocalStorage for logger bindings context propagation (Node.js only). */
|
|
6
|
+
const bindingsStorage = new AsyncLocalStorage<LoggerBindings>();
|
|
7
|
+
|
|
8
|
+
/** Returns the current bindings from AsyncLocalStorage, if any. */
|
|
9
|
+
export function getBindings(): LoggerBindings | undefined {
|
|
10
|
+
return bindingsStorage.getStore();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Runs a callback within a bindings context. All loggers created within the callback
|
|
15
|
+
* will automatically inherit the bindings (actor, instanceId) via the log bindings handler.
|
|
16
|
+
*/
|
|
17
|
+
export async function withLoggerBindings<T>(bindings: LoggerBindings, callback: () => Promise<T>): Promise<T> {
|
|
18
|
+
const handler = () => bindingsStorage.getStore();
|
|
19
|
+
addLogBindingsHandler(handler);
|
|
20
|
+
try {
|
|
21
|
+
return await bindingsStorage.run(bindings, callback);
|
|
22
|
+
} finally {
|
|
23
|
+
removeLogBindingsHandler(handler);
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/log/pino-logger.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createColors, isColorSupported } from 'colorette';
|
|
1
|
+
import { type Color, createColors, isColorSupported } from 'colorette';
|
|
2
2
|
import isNode from 'detect-node';
|
|
3
3
|
import { pino, symbols } from 'pino';
|
|
4
4
|
import type { Writable } from 'stream';
|
|
@@ -7,14 +7,57 @@ import { inspect } from 'util';
|
|
|
7
7
|
import { compactArray } from '../collection/array.js';
|
|
8
8
|
import type { EnvVar } from '../config/index.js';
|
|
9
9
|
import { parseBooleanEnv } from '../config/parse-env.js';
|
|
10
|
+
import { convertBigintsToStrings } from './bigint-utils.js';
|
|
10
11
|
import { GoogleCloudLoggerConfig } from './gcloud-logger-config.js';
|
|
11
|
-
import { getLogLevelFromFilters,
|
|
12
|
+
import { getLogLevelFromFilters, parseLogLevelEnvVar } from './log-filters.js';
|
|
12
13
|
import type { LogLevel } from './log-levels.js';
|
|
13
14
|
import type { LogData, LogFn } from './log_fn.js';
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
/** Optional bindings to pass to createLogger for additional context. */
|
|
17
|
+
export type LoggerBindings = {
|
|
18
|
+
/** Actor label shown in logs (e.g., 'MAIN', 'prover-node'). */
|
|
19
|
+
actor?: string;
|
|
20
|
+
/** Instance identifier for distinguishing multiple instances of the same component. */
|
|
21
|
+
instanceId?: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Allow global hooks for providing default bindings.
|
|
25
|
+
// Used by withLoggerBindings in pino-logger-server to propagate bindings via AsyncLocalStorage.
|
|
26
|
+
type LogBindingsHandler = () => LoggerBindings | undefined;
|
|
27
|
+
const logBindingsHandlers: LogBindingsHandler[] = [];
|
|
28
|
+
|
|
29
|
+
export function addLogBindingsHandler(handler: LogBindingsHandler): void {
|
|
30
|
+
logBindingsHandlers.push(handler);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function removeLogBindingsHandler(handler: LogBindingsHandler) {
|
|
34
|
+
const index = logBindingsHandlers.indexOf(handler);
|
|
35
|
+
if (index !== -1) {
|
|
36
|
+
logBindingsHandlers.splice(index, 1);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function getBindingsFromHandlers(): LoggerBindings | undefined {
|
|
41
|
+
for (const handler of logBindingsHandlers) {
|
|
42
|
+
const bindings = handler();
|
|
43
|
+
if (bindings) {
|
|
44
|
+
return bindings;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function createLogger(module: string, bindings?: LoggerBindings): Logger {
|
|
51
|
+
module = module.replace(/^aztec:/, '');
|
|
52
|
+
|
|
53
|
+
const resolvedBindings = { ...getBindingsFromHandlers(), ...bindings };
|
|
54
|
+
const actor = resolvedBindings?.actor;
|
|
55
|
+
const instanceId = resolvedBindings?.instanceId;
|
|
56
|
+
|
|
57
|
+
const pinoLogger = logger.child(
|
|
58
|
+
{ module, ...(actor && { actor }), ...(instanceId && { instanceId }) },
|
|
59
|
+
{ level: getLogLevelFromFilters(logFilters, module) },
|
|
60
|
+
);
|
|
18
61
|
|
|
19
62
|
// We check manually for isLevelEnabled to avoid calling processLogData unnecessarily.
|
|
20
63
|
// Note that isLevelEnabled is missing from the browser version of pino.
|
|
@@ -44,11 +87,24 @@ export function createLogger(module: string): Logger {
|
|
|
44
87
|
isLevelEnabled: (level: LogLevel) => isLevelEnabled(pinoLogger, level),
|
|
45
88
|
/** Module name for the logger. */
|
|
46
89
|
module,
|
|
47
|
-
/** Creates another logger by extending this logger module name. */
|
|
48
|
-
createChild: (childModule: string) => createLogger(`${module}:${childModule}
|
|
90
|
+
/** Creates another logger by extending this logger module name and preserving bindings. */
|
|
91
|
+
createChild: (childModule: string) => createLogger(`${module}:${childModule}`, { actor, instanceId }),
|
|
92
|
+
/** Returns the bindings (actor, instanceId) for this logger. */
|
|
93
|
+
getBindings: () => ({ actor, instanceId }),
|
|
49
94
|
};
|
|
50
95
|
}
|
|
51
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Returns a logger for the given module. If loggerOrBindings is already a Logger, returns it directly.
|
|
99
|
+
* Otherwise, creates a new logger with the given module name and bindings.
|
|
100
|
+
*/
|
|
101
|
+
export function resolveLogger(module: string, loggerOrBindings?: Logger | LoggerBindings): Logger {
|
|
102
|
+
if (loggerOrBindings && 'info' in loggerOrBindings) {
|
|
103
|
+
return loggerOrBindings as Logger;
|
|
104
|
+
}
|
|
105
|
+
return createLogger(module, loggerOrBindings);
|
|
106
|
+
}
|
|
107
|
+
|
|
52
108
|
// Allow global hooks for processing log data.
|
|
53
109
|
// Used for injecting OTEL trace_id in telemetry client.
|
|
54
110
|
type LogDataHandler = (data: LogData) => LogData;
|
|
@@ -62,31 +118,6 @@ function processLogData(data: LogData): LogData {
|
|
|
62
118
|
return logDataHandlers.reduce((accum, handler) => handler(accum), data);
|
|
63
119
|
}
|
|
64
120
|
|
|
65
|
-
// Allow global hooks for tweaking module names.
|
|
66
|
-
// Used in tests to add a uid to modules, so we can differentiate multiple nodes in the same process.
|
|
67
|
-
type LogNameHandler = (module: string) => string;
|
|
68
|
-
const logNameHandlers: LogNameHandler[] = [];
|
|
69
|
-
|
|
70
|
-
export function addLogNameHandler(handler: LogNameHandler): void {
|
|
71
|
-
logNameHandlers.push(handler);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function removeLogNameHandler(handler: LogNameHandler) {
|
|
75
|
-
const index = logNameHandlers.indexOf(handler);
|
|
76
|
-
if (index !== -1) {
|
|
77
|
-
logNameHandlers.splice(index, 1);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Creates all loggers within the given callback with the suffix appended to the module name. */
|
|
82
|
-
export async function withLogNameSuffix<T>(suffix: string, callback: () => Promise<T>): Promise<T> {
|
|
83
|
-
const logNameHandler = (module: string) => `${module}:${suffix}`;
|
|
84
|
-
addLogNameHandler(logNameHandler);
|
|
85
|
-
const result = await callback();
|
|
86
|
-
removeLogNameHandler(logNameHandler);
|
|
87
|
-
return result;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
121
|
// Patch isLevelEnabled missing from pino/browser.
|
|
91
122
|
function isLevelEnabled(logger: pino.Logger<'verbose', boolean>, level: LogLevel): boolean {
|
|
92
123
|
return typeof logger.isLevelEnabled === 'function'
|
|
@@ -96,7 +127,7 @@ function isLevelEnabled(logger: pino.Logger<'verbose', boolean>, level: LogLevel
|
|
|
96
127
|
|
|
97
128
|
// Load log levels from environment variables.
|
|
98
129
|
const defaultLogLevel = process.env.NODE_ENV === 'test' ? 'silent' : 'info';
|
|
99
|
-
export const [logLevel, logFilters] =
|
|
130
|
+
export const [logLevel, logFilters] = parseLogLevelEnvVar(process.env.LOG_LEVEL, defaultLogLevel);
|
|
100
131
|
|
|
101
132
|
// Define custom logging levels for pino.
|
|
102
133
|
const customLevels = { verbose: 25 };
|
|
@@ -135,6 +166,9 @@ const pinoOpts: pino.LoggerOptions<keyof typeof customLevels> = {
|
|
|
135
166
|
...redactedPaths.map(p => `opts.${p}`),
|
|
136
167
|
],
|
|
137
168
|
},
|
|
169
|
+
formatters: {
|
|
170
|
+
log: obj => convertBigintsToStrings(obj) as Record<string, unknown>,
|
|
171
|
+
},
|
|
138
172
|
...(useGcloudLogging ? GoogleCloudLoggerConfig : {}),
|
|
139
173
|
};
|
|
140
174
|
|
|
@@ -146,22 +180,90 @@ export const levels = {
|
|
|
146
180
|
// Transport options for pretty logging to stderr via pino-pretty.
|
|
147
181
|
const colorEnv = process.env['FORCE_COLOR' satisfies EnvVar];
|
|
148
182
|
const useColor = colorEnv === undefined ? isColorSupported : parseBooleanEnv(colorEnv);
|
|
149
|
-
const { bold, reset
|
|
150
|
-
|
|
183
|
+
const { bold, reset, cyan, magenta, yellow, blue, green, magentaBright, yellowBright, blueBright, greenBright } =
|
|
184
|
+
createColors({ useColor });
|
|
185
|
+
|
|
186
|
+
// Per-actor coloring: each unique actor gets a different color for easier visual distinction.
|
|
187
|
+
// Disabled when LOG_NO_COLOR_PER_ACTOR is set to a truthy value.
|
|
188
|
+
const useColorPerActor = useColor && !parseBooleanEnv(process.env['LOG_NO_COLOR_PER_ACTOR' satisfies EnvVar]);
|
|
189
|
+
const actorColors: Color[] = [yellow, magenta, blue, green, magentaBright, yellowBright, blueBright, greenBright];
|
|
190
|
+
const actorColorMap = new Map<string, Color>();
|
|
191
|
+
let nextColorIndex = 0;
|
|
192
|
+
|
|
193
|
+
/** Returns the color function assigned to a given actor, assigning a new one if needed. */
|
|
194
|
+
export function getActorColor(actor: string): Color {
|
|
195
|
+
let color = actorColorMap.get(actor);
|
|
196
|
+
if (!color) {
|
|
197
|
+
color = actorColors[nextColorIndex % actorColors.length];
|
|
198
|
+
actorColorMap.set(actor, color);
|
|
199
|
+
nextColorIndex++;
|
|
200
|
+
}
|
|
201
|
+
return color;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Resets the actor-to-color mapping. Useful for testing. */
|
|
205
|
+
export function resetActorColors(): void {
|
|
206
|
+
actorColorMap.clear();
|
|
207
|
+
nextColorIndex = 0;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// String template for messageFormat (used in worker threads and when per-actor coloring is disabled).
|
|
211
|
+
const messageFormatString = `${bold('{module}')}{if actor} ${cyan('{actor}')}{end}{if instanceId} ${reset(cyan('{instanceId}'))}{end} ${reset('{msg}')}`;
|
|
212
|
+
|
|
213
|
+
// Function for messageFormat when per-actor coloring is enabled (can only be used in-process, not worker threads).
|
|
214
|
+
type LogObject = { actor?: string; module?: string; instanceId?: string; msg?: string };
|
|
215
|
+
|
|
216
|
+
/** Formats a log message with per-actor coloring. Actor, module, and instanceId share the same color. */
|
|
217
|
+
export function formatLogMessage(log: LogObject, messageKey: string): string {
|
|
218
|
+
const actor = log.actor;
|
|
219
|
+
const module = log.module ?? '';
|
|
220
|
+
const instanceId = log.instanceId;
|
|
221
|
+
const msg = log[messageKey as keyof LogObject] ?? '';
|
|
222
|
+
|
|
223
|
+
// Use actor color for actor, module, and instanceId when actor is present
|
|
224
|
+
const color = actor ? getActorColor(actor) : cyan;
|
|
225
|
+
|
|
226
|
+
let result = bold(color(module));
|
|
227
|
+
if (actor) {
|
|
228
|
+
result += ' ' + color(actor);
|
|
229
|
+
}
|
|
230
|
+
if (instanceId) {
|
|
231
|
+
result += ' ' + reset(color(instanceId));
|
|
232
|
+
}
|
|
233
|
+
result += ' ' + reset(String(msg));
|
|
234
|
+
return result;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Base options for pino-pretty (shared between transport and direct use).
|
|
238
|
+
const pinoPrettyBaseOpts = {
|
|
151
239
|
destination: 2,
|
|
152
240
|
sync: true,
|
|
153
241
|
colorize: useColor,
|
|
154
|
-
ignore: 'module,pid,hostname,trace_id,span_id,trace_flags,severity',
|
|
155
|
-
messageFormat: `${bold('{module}')} ${reset('{msg}')}`,
|
|
242
|
+
ignore: 'module,actor,instanceId,pid,hostname,trace_id,span_id,trace_flags,severity',
|
|
156
243
|
customLevels: 'fatal:60,error:50,warn:40,info:30,verbose:25,debug:20,trace:10',
|
|
157
244
|
customColors: 'fatal:bgRed,error:red,warn:yellow,info:green,verbose:magenta,debug:blue,trace:gray',
|
|
158
245
|
minimumLevel: 'trace' as const,
|
|
159
246
|
singleLine: !parseBooleanEnv(process.env['LOG_MULTILINE' satisfies EnvVar]),
|
|
160
247
|
};
|
|
161
248
|
|
|
249
|
+
/**
|
|
250
|
+
* Pino-pretty options for direct use (e.g., jest/setup.mjs).
|
|
251
|
+
* Includes function-based messageFormat for per-actor coloring when enabled.
|
|
252
|
+
*/
|
|
253
|
+
export const pinoPrettyOpts = {
|
|
254
|
+
...pinoPrettyBaseOpts,
|
|
255
|
+
messageFormat: useColorPerActor ? formatLogMessage : messageFormatString,
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
// Transport options use string template only (functions can't be serialized to worker threads).
|
|
259
|
+
const prettyTransportOpts = {
|
|
260
|
+
...pinoPrettyBaseOpts,
|
|
261
|
+
messageFormat: messageFormatString,
|
|
262
|
+
};
|
|
263
|
+
|
|
162
264
|
const prettyTransport: pino.TransportTargetOptions = {
|
|
163
265
|
target: 'pino-pretty',
|
|
164
|
-
options:
|
|
266
|
+
options: prettyTransportOpts,
|
|
165
267
|
level: 'trace',
|
|
166
268
|
};
|
|
167
269
|
|
|
@@ -262,6 +364,8 @@ export type Logger = { [K in LogLevel]: LogFn } & { /** Error log function */ er
|
|
|
262
364
|
isLevelEnabled: (level: LogLevel) => boolean;
|
|
263
365
|
module: string;
|
|
264
366
|
createChild: (childModule: string) => Logger;
|
|
367
|
+
/** Returns the bindings (actor, instanceId) for this logger. */
|
|
368
|
+
getBindings: () => LoggerBindings;
|
|
265
369
|
};
|
|
266
370
|
|
|
267
371
|
/**
|
|
@@ -122,7 +122,7 @@ export abstract class BaseMemoryQueue<T> {
|
|
|
122
122
|
* @param handler - A function that takes an item of type T and returns a Promise<void> after processing the item.
|
|
123
123
|
* @returns A Promise<void> that resolves when the queue is finished processing.
|
|
124
124
|
*/
|
|
125
|
-
public async process(handler: (item: T) => Promise<void>) {
|
|
125
|
+
public async process(handler: (item: T) => Promise<void> | void) {
|
|
126
126
|
try {
|
|
127
127
|
while (true) {
|
|
128
128
|
const item = await this.get();
|
package/src/queue/semaphore.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { FifoMemoryQueue } from './fifo_memory_queue.js';
|
|
2
2
|
|
|
3
|
+
export interface ISemaphore {
|
|
4
|
+
acquire(): Promise<void>;
|
|
5
|
+
release(): void;
|
|
6
|
+
}
|
|
7
|
+
|
|
3
8
|
/**
|
|
4
9
|
* Allows the acquiring of up to `size` tokens before calls to acquire block, waiting for a call to release().
|
|
5
10
|
*/
|