@visulima/pail 4.0.0-alpha.6 → 4.0.0-alpha.7

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE.md +2 -2
  3. package/README.md +323 -0
  4. package/dist/error.d.ts +104 -0
  5. package/dist/error.js +76 -0
  6. package/dist/index.browser.d.ts +2 -0
  7. package/dist/index.browser.js +2 -1
  8. package/dist/index.server.d.ts +2 -0
  9. package/dist/index.server.js +3 -1
  10. package/dist/middleware/elysia.d.ts +71 -0
  11. package/dist/middleware/elysia.js +70 -0
  12. package/dist/middleware/express.d.ts +86 -0
  13. package/dist/middleware/express.js +29 -0
  14. package/dist/middleware/fastify.d.ts +81 -0
  15. package/dist/middleware/fastify.js +46 -0
  16. package/dist/middleware/hono.d.ts +85 -0
  17. package/dist/middleware/hono.js +33 -0
  18. package/dist/middleware/next/handler.d.ts +36 -0
  19. package/dist/middleware/next/handler.js +53 -0
  20. package/dist/middleware/next/middleware.d.ts +59 -0
  21. package/dist/middleware/next/storage.d.ts +14 -0
  22. package/dist/middleware/shared/create-middleware-logger.d.ts +82 -0
  23. package/dist/middleware/shared/headers.d.ts +14 -0
  24. package/dist/middleware/shared/routes.d.ts +30 -0
  25. package/dist/middleware/shared/storage.d.ts +29 -0
  26. package/dist/middleware/sveltekit.d.ts +123 -0
  27. package/dist/middleware/sveltekit.js +43 -0
  28. package/dist/packem_shared/{AbstractJsonReporter-DWRpTtGw.js → AbstractJsonReporter-CGKHS8_M.js} +103 -21
  29. package/dist/packem_shared/{AbstractJsonReporter-BaZ33PlE.js → AbstractJsonReporter-DDjDkciI.js} +103 -21
  30. package/dist/packem_shared/{JsonReporter-BV5lMnJX.js → JsonReporter-B3XX8GHN.js} +1 -1
  31. package/dist/packem_shared/{JsonReporter-BRw4skd5.js → JsonReporter-p_BXg6Sj.js} +1 -1
  32. package/dist/packem_shared/{PrettyReporter-BjXCFQlo.js → PrettyReporter-CvBn-hxP.js} +2 -1
  33. package/dist/packem_shared/createPailError-B11aRfrT.js +76 -0
  34. package/dist/packem_shared/headers-Cp4uLtr4.js +123 -0
  35. package/dist/packem_shared/pailMiddleware-Ci88geIF.js +24 -0
  36. package/dist/packem_shared/storage-D0vqz8OX.js +36 -0
  37. package/dist/packem_shared/useLogger-D0rU3lcX.js +33 -0
  38. package/dist/processor/environment-processor.d.ts +124 -0
  39. package/dist/processor/environment-processor.js +78 -0
  40. package/dist/processor/message-formatter-processor.d.ts +1 -2
  41. package/dist/processor/sampling-processor.d.ts +111 -0
  42. package/dist/processor/sampling-processor.js +59 -0
  43. package/dist/reporter/file/json-file-reporter.js +1 -1
  44. package/dist/reporter/http/abstract-http-reporter.js +1 -1
  45. package/dist/reporter/http/http-reporter.edge-light.js +103 -21
  46. package/dist/reporter/json/index.browser.js +2 -2
  47. package/dist/reporter/json/index.js +2 -2
  48. package/dist/reporter/pretty/index.js +1 -1
  49. package/dist/reporter/simple/simple-reporter.server.js +2 -1
  50. package/dist/wide-event.d.ts +300 -0
  51. package/dist/wide-event.js +281 -0
  52. package/package.json +65 -1
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Extract safe headers from a Web API Headers object, filtering out
3
+ * sensitive headers like Authorization, Cookie, and API keys.
4
+ * @param headers Web API Headers object
5
+ * @returns Plain object of safe header key-value pairs
6
+ */
7
+ export declare const extractSafeHeaders: (headers: Headers) => Record<string, string>;
8
+ /**
9
+ * Extract safe headers from a Node.js IncomingHttpHeaders object,
10
+ * filtering out sensitive headers.
11
+ * @param headers Node.js IncomingHttpHeaders-like object
12
+ * @returns Plain object of safe header key-value pairs
13
+ */
14
+ export declare const extractSafeNodeHeaders: (headers: Record<string, string | string[] | undefined>) => Record<string, string>;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Check if a path matches a glob pattern.
3
+ */
4
+ export declare const matchesPattern: (path: string, pattern: string) => boolean;
5
+ /**
6
+ * Route configuration for a specific path pattern.
7
+ */
8
+ export interface RouteConfig {
9
+ /** Override the service name for requests matching this route. */
10
+ service?: string;
11
+ }
12
+ /**
13
+ * Determine whether a request path should be logged based on include/exclude patterns.
14
+ *
15
+ * - Exclusions take precedence over inclusions.
16
+ * - If no include patterns are provided, all non-excluded paths are logged.
17
+ * @param path The request path
18
+ * @param include Glob patterns of paths to include
19
+ * @param exclude Glob patterns of paths to exclude
20
+ * @returns Whether the path should be logged
21
+ */
22
+ export declare const shouldLog: (path: string, include?: string[], exclude?: string[]) => boolean;
23
+ /**
24
+ * Get the service name override for a given path based on route configuration.
25
+ * Returns the first matching route's service name, or undefined if no match.
26
+ * @param path The request path
27
+ * @param routes Route configuration map (pattern → config)
28
+ * @returns The service name override, or undefined
29
+ */
30
+ export declare const getServiceForPath: (path: string, routes?: Record<string, RouteConfig>) => string | undefined;
@@ -0,0 +1,29 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ import type { WideEvent } from "../../wide-event.d.ts";
3
+ /**
4
+ * Create an isolated AsyncLocalStorage instance and a `useLogger` accessor
5
+ * for retrieving the request-scoped WideEvent from anywhere in the call stack.
6
+ *
7
+ * Each framework adapter should call this once at module level to get its own
8
+ * isolated storage instance.
9
+ * @param contextHint A description of the expected context, used in the
10
+ * error message when `useLogger` is called outside of a request scope.
11
+ * @returns An object with `storage` and `useLogger`
12
+ * @example
13
+ * ```typescript
14
+ * const { storage, useLogger } = createLoggerStorage(
15
+ * "Express middleware context. Make sure evlog middleware is registered."
16
+ * );
17
+ *
18
+ * // In middleware:
19
+ * storage.run(wideEvent, () => next());
20
+ *
21
+ * // In handler:
22
+ * const log = useLogger();
23
+ * log.set({ user: { id: 1 } });
24
+ * ```
25
+ */
26
+ export declare const createLoggerStorage: (contextHint: string) => {
27
+ storage: AsyncLocalStorage<WideEvent>;
28
+ useLogger: () => WideEvent;
29
+ };
@@ -0,0 +1,123 @@
1
+ import type { WideEvent } from "../wide-event.d.ts";
2
+ import type { PailMiddlewareOptions } from "./shared/create-middleware-logger.d.ts";
3
+ /**
4
+ * A SvelteKit-like event object with access to the pail logger via `locals.log`.
5
+ * @example
6
+ * ```typescript
7
+ * // In a SvelteKit load function or action:
8
+ * import type { PailSvelteKitEvent } from "@visulima/pail/middleware/sveltekit";
9
+ *
10
+ * export const load = async (event: PailSvelteKitEvent) => {
11
+ * event.locals.log?.set({ user: { id: 1 } });
12
+ * };
13
+ * ```
14
+ */
15
+ interface PailSvelteKitEvent {
16
+ [key: string]: unknown;
17
+ locals: Record<string, unknown> & {
18
+ log?: WideEvent;
19
+ };
20
+ request: Request;
21
+ url: URL;
22
+ }
23
+ /**
24
+ * Options passed to SvelteKit's resolve function.
25
+ */
26
+ interface PailSvelteKitResolveOptions {
27
+ filterSerializedResponseHeaders?: (name: string) => boolean;
28
+ preload?: (input: {
29
+ type: string;
30
+ }) => boolean;
31
+ transformPageChunk?: (input: {
32
+ html: string;
33
+ }) => string;
34
+ }
35
+ /**
36
+ * SvelteKit resolve function type.
37
+ */
38
+ type PailSvelteKitResolve = (event: PailSvelteKitEvent, options?: PailSvelteKitResolveOptions) => Promise<Response>;
39
+ /**
40
+ * Input for the SvelteKit handle hook.
41
+ */
42
+ interface PailSvelteKitHandleInput {
43
+ event: PailSvelteKitEvent;
44
+ resolve: PailSvelteKitResolve;
45
+ }
46
+ /**
47
+ * SvelteKit handle hook function type returned by `pailHandle()`.
48
+ */
49
+ type PailSvelteKitHandle = (input: PailSvelteKitHandleInput) => Promise<Response>;
50
+ /**
51
+ * Input for the SvelteKit handleError hook.
52
+ */
53
+ interface PailSvelteKitHandleErrorInput {
54
+ error: unknown;
55
+ event: PailSvelteKitEvent;
56
+ message: string;
57
+ status: number;
58
+ }
59
+ /**
60
+ * SvelteKit handleError hook function type returned by `pailHandleError()`.
61
+ */
62
+ type PailSvelteKitHandleError = (input: PailSvelteKitHandleErrorInput) => void;
63
+ /**
64
+ * Retrieve the request-scoped WideEvent logger from AsyncLocalStorage.
65
+ * Must be called within a request handled by the pail SvelteKit handle hook.
66
+ * @returns The request-scoped WideEvent logger
67
+ * @throws Error if called outside of the hook context
68
+ */
69
+ declare const useLogger: () => WideEvent;
70
+ type SvelteKitHandleOptions<T extends string = string> = PailMiddlewareOptions<T>;
71
+ /**
72
+ * Create a SvelteKit `handle` hook that attaches a WideEvent logger to each request.
73
+ *
74
+ * The logger is available via:
75
+ * - `event.locals.log` in server load functions and actions
76
+ * - `useLogger()` from anywhere in the async call stack
77
+ *
78
+ * The wide event is automatically emitted when the response is sent or an error occurs.
79
+ * @param options Hook configuration
80
+ * @returns SvelteKit handle hook function
81
+ * @example
82
+ * ```typescript
83
+ * // src/hooks.server.ts
84
+ * import { createPail } from "@visulima/pail";
85
+ * import { pailHandle, pailHandleError } from "@visulima/pail/middleware/sveltekit";
86
+ *
87
+ * const logger = createPail();
88
+ *
89
+ * export const handle = pailHandle({ pail: logger });
90
+ * export const handleError = pailHandleError();
91
+ * ```
92
+ */
93
+ export declare const pailHandle: <T extends string = string>(options: SvelteKitHandleOptions<T>) => PailSvelteKitHandle;
94
+ /**
95
+ * Create a SvelteKit `handleError` hook that captures errors into the WideEvent logger.
96
+ *
97
+ * Should be used alongside `pailHandle` to ensure errors are recorded in the wide event.
98
+ * @returns SvelteKit handleError hook function
99
+ */
100
+ export declare const pailHandleError: () => PailSvelteKitHandleError;
101
+ /**
102
+ * Convenience function that returns both handle and handleError hooks.
103
+ * @param options Hook configuration
104
+ * @returns Object with `handle` and `handleError` hooks
105
+ * @example
106
+ * ```typescript
107
+ * // src/hooks.server.ts
108
+ * import { createPail } from "@visulima/pail";
109
+ * import { createPailHooks } from "@visulima/pail/middleware/sveltekit";
110
+ *
111
+ * const logger = createPail();
112
+ * const { handle, handleError } = createPailHooks({ pail: logger });
113
+ *
114
+ * export { handle, handleError };
115
+ * ```
116
+ */
117
+ export declare const createPailHooks: <T extends string = string>(options: SvelteKitHandleOptions<T>) => {
118
+ handle: PailSvelteKitHandle;
119
+ handleError: PailSvelteKitHandleError;
120
+ };
121
+ export { useLogger };
122
+ export type { PailSvelteKitEvent, PailSvelteKitHandle, PailSvelteKitHandleError, PailSvelteKitHandleErrorInput, PailSvelteKitHandleInput, PailSvelteKitResolve, PailSvelteKitResolveOptions, SvelteKitHandleOptions, };
123
+ export type { WideEvent } from "../wide-event.d.ts";
@@ -0,0 +1,43 @@
1
+ import { a as extractSafeHeaders, c as createMiddlewareLogger } from '../packem_shared/headers-Cp4uLtr4.js';
2
+ import { c as createLoggerStorage } from '../packem_shared/storage-D0vqz8OX.js';
3
+
4
+ const loggerStorage = createLoggerStorage("SvelteKit handle hook context. Make sure pailHandle is added to your hooks.server.ts.");
5
+ const useLogger = () => loggerStorage.useLogger();
6
+ const pailHandle = (options) => async ({ event, resolve }) => {
7
+ const requestId = event.request.headers.get("x-request-id") ?? crypto.randomUUID();
8
+ const safeHeaders = extractSafeHeaders(event.request.headers);
9
+ const { finish, logger, skipped } = createMiddlewareLogger(options, {
10
+ headers: safeHeaders,
11
+ method: event.request.method,
12
+ path: event.url.pathname,
13
+ requestId
14
+ });
15
+ if (skipped) {
16
+ return resolve(event);
17
+ }
18
+ event.locals.log = logger;
19
+ return loggerStorage.storage.run(logger, async () => {
20
+ try {
21
+ const response = await resolve(event);
22
+ finish({ status: response.status });
23
+ return response;
24
+ } catch (error) {
25
+ finish({ error: error instanceof Error ? error : new Error(String(error)) });
26
+ throw error;
27
+ }
28
+ });
29
+ };
30
+ const pailHandleError = () => ({ error, event }) => {
31
+ const logger = event.locals.log;
32
+ if (logger && error instanceof Error) {
33
+ logger.error(error.message, error);
34
+ }
35
+ };
36
+ const createPailHooks = (options) => {
37
+ return {
38
+ handle: pailHandle(options),
39
+ handleError: pailHandleError()
40
+ };
41
+ };
42
+
43
+ export { createPailHooks, pailHandle, pailHandleError, useLogger };
@@ -12,7 +12,7 @@ const ErrorProto = Object.create(
12
12
  {},
13
13
  {
14
14
  cause: {
15
- enumerable: true,
15
+ enumerable: false,
16
16
  value: void 0,
17
17
  writable: true
18
18
  },
@@ -22,32 +22,55 @@ const ErrorProto = Object.create(
22
22
  writable: true
23
23
  },
24
24
  errors: {
25
- enumerable: true,
25
+ enumerable: false,
26
26
  value: void 0,
27
27
  writable: true
28
28
  },
29
29
  message: {
30
- enumerable: true,
30
+ enumerable: false,
31
31
  value: void 0,
32
32
  writable: true
33
33
  },
34
34
  name: {
35
- enumerable: true,
35
+ enumerable: false,
36
36
  value: void 0,
37
37
  writable: true
38
38
  },
39
39
  stack: {
40
- enumerable: true,
40
+ enumerable: false,
41
41
  value: void 0,
42
42
  writable: true
43
43
  }
44
44
  }
45
45
  );
46
46
  const toJsonWasCalled = /* @__PURE__ */ new WeakSet();
47
+ const makePropertiesEnumerable = (object) => {
48
+ if (!object || typeof object !== "object") {
49
+ return;
50
+ }
51
+ const props = Object.getOwnPropertyNames(object);
52
+ for (const prop of props) {
53
+ const descriptor = Object.getOwnPropertyDescriptor(object, prop);
54
+ if (descriptor) {
55
+ if (!descriptor.enumerable) {
56
+ Object.defineProperty(object, prop, {
57
+ ...descriptor,
58
+ enumerable: true
59
+ });
60
+ }
61
+ if (descriptor.value && typeof descriptor.value === "object" && !Array.isArray(descriptor.value) && (Object.getPrototypeOf(descriptor.value) === Object.prototype || Object.getPrototypeOf(descriptor.value) === null)) {
62
+ makePropertiesEnumerable(descriptor.value);
63
+ }
64
+ }
65
+ }
66
+ };
47
67
  const toJSON = (from) => {
48
68
  toJsonWasCalled.add(from);
49
69
  const json = from.toJSON();
50
70
  toJsonWasCalled.delete(from);
71
+ if (json && typeof json === "object" && Object.isExtensible(json)) {
72
+ makePropertiesEnumerable(json);
73
+ }
51
74
  return json;
52
75
  };
53
76
  const serializeValue = (value, seen, depth, options) => {
@@ -58,7 +81,7 @@ const serializeValue = (value, seen, depth, options) => {
58
81
  return "[object Stream]";
59
82
  }
60
83
  if (value instanceof Error) {
61
- if (seen.includes(value)) {
84
+ if (seen.has(value)) {
62
85
  return "[Circular]";
63
86
  }
64
87
  depth += 1;
@@ -73,11 +96,14 @@ const serializeValue = (value, seen, depth, options) => {
73
96
  if (typeof value === "function") {
74
97
  return `[Function: ${value.name || "anonymous"}]`;
75
98
  }
99
+ if (typeof value === "bigint") {
100
+ return `${value}n`;
101
+ }
76
102
  if (isPlainObject(value)) {
77
- depth += 1;
78
- if (options.maxDepth && depth >= options.maxDepth) {
103
+ if (options.maxDepth !== void 0 && options.maxDepth !== Number.POSITIVE_INFINITY && depth + 1 >= options.maxDepth) {
79
104
  return {};
80
105
  }
106
+ depth += 1;
81
107
  const plainObject = {};
82
108
  for (const key in value) {
83
109
  plainObject[key] = serializeValue(value[key], seen, depth, options);
@@ -91,7 +117,7 @@ const serializeValue = (value, seen, depth, options) => {
91
117
  }
92
118
  };
93
119
  const _serialize = (error, options, seen, depth) => {
94
- seen.push(error);
120
+ seen.add(error);
95
121
  if (options.maxDepth === 0) {
96
122
  return {};
97
123
  }
@@ -99,31 +125,87 @@ const _serialize = (error, options, seen, depth) => {
99
125
  return toJSON(error);
100
126
  }
101
127
  const protoError = Object.create(ErrorProto);
102
- protoError.name = Object.prototype.toString.call(error.constructor) === "[object Function]" ? error.constructor.name : error.name;
103
- protoError.message = error.message;
104
- protoError.stack = error.stack;
128
+ Object.defineProperty(protoError, "name", {
129
+ configurable: true,
130
+ enumerable: true,
131
+ value: Object.prototype.toString.call(error.constructor) === "[object Function]" ? error.constructor.name : error.name,
132
+ writable: true
133
+ });
134
+ Object.defineProperty(protoError, "message", {
135
+ configurable: true,
136
+ enumerable: true,
137
+ value: error.message,
138
+ writable: true
139
+ });
140
+ Object.defineProperty(protoError, "stack", {
141
+ configurable: true,
142
+ enumerable: true,
143
+ value: error.stack,
144
+ writable: true
145
+ });
105
146
  if (Array.isArray(error.errors)) {
106
147
  const aggregateErrors = [];
107
148
  for (const aggregateError of error.errors) {
108
149
  if (!(aggregateError instanceof Error)) {
109
150
  throw new TypeError("All errors in the 'errors' property must be instances of Error");
110
151
  }
111
- if (seen.includes(aggregateError)) {
112
- protoError.errors = [];
152
+ if (seen.has(aggregateError)) {
153
+ Object.defineProperty(protoError, "errors", {
154
+ configurable: true,
155
+ enumerable: true,
156
+ value: [],
157
+ writable: true
158
+ });
113
159
  return protoError;
114
160
  }
115
161
  aggregateErrors.push(_serialize(aggregateError, options, seen, depth));
116
162
  }
117
- protoError.errors = aggregateErrors;
163
+ Object.defineProperty(protoError, "errors", {
164
+ configurable: true,
165
+ enumerable: true,
166
+ value: aggregateErrors,
167
+ writable: true
168
+ });
118
169
  }
119
- if (error.cause instanceof Error && !seen.includes(error.cause)) {
120
- protoError.cause = _serialize(error.cause, options, seen, depth);
170
+ if (error.cause !== void 0 && error.cause !== null) {
171
+ if (error.cause instanceof Error) {
172
+ if (seen.has(error.cause)) {
173
+ Object.defineProperty(protoError, "cause", {
174
+ configurable: true,
175
+ enumerable: true,
176
+ value: "[Circular]",
177
+ writable: true
178
+ });
179
+ } else {
180
+ Object.defineProperty(protoError, "cause", {
181
+ configurable: true,
182
+ enumerable: true,
183
+ value: _serialize(error.cause, options, seen, depth),
184
+ writable: true
185
+ });
186
+ }
187
+ } else {
188
+ const serializedCause = serializeValue(error.cause, seen, depth, options);
189
+ Object.defineProperty(protoError, "cause", {
190
+ configurable: true,
191
+ enumerable: true,
192
+ value: serializedCause,
193
+ writable: true
194
+ });
195
+ }
121
196
  }
122
197
  for (const key in error) {
123
- if (protoError[key] === void 0) {
124
- const value = error[key];
125
- protoError[key] = serializeValue(value, seen, depth, options);
198
+ if (key === "name" || key === "message" || key === "stack" || key === "cause" || key === "errors") {
199
+ continue;
126
200
  }
201
+ const value = error[key];
202
+ const serializedValue = serializeValue(value, seen, depth, options);
203
+ Object.defineProperty(protoError, key, {
204
+ configurable: true,
205
+ enumerable: true,
206
+ value: serializedValue,
207
+ writable: true
208
+ });
127
209
  }
128
210
  if (Array.isArray(options.exclude) && options.exclude.length > 0) {
129
211
  for (const key of options.exclude) {
@@ -142,7 +224,7 @@ const serialize = (error, options = {}) => _serialize(
142
224
  maxDepth: options.maxDepth ?? Number.POSITIVE_INFINITY,
143
225
  useToJSON: options.useToJSON ?? false
144
226
  },
145
- [],
227
+ /* @__PURE__ */ new Set(),
146
228
  0
147
229
  );
148
230
 
@@ -12,7 +12,7 @@ const ErrorProto = Object.create(
12
12
  {},
13
13
  {
14
14
  cause: {
15
- enumerable: true,
15
+ enumerable: false,
16
16
  value: void 0,
17
17
  writable: true
18
18
  },
@@ -22,32 +22,55 @@ const ErrorProto = Object.create(
22
22
  writable: true
23
23
  },
24
24
  errors: {
25
- enumerable: true,
25
+ enumerable: false,
26
26
  value: void 0,
27
27
  writable: true
28
28
  },
29
29
  message: {
30
- enumerable: true,
30
+ enumerable: false,
31
31
  value: void 0,
32
32
  writable: true
33
33
  },
34
34
  name: {
35
- enumerable: true,
35
+ enumerable: false,
36
36
  value: void 0,
37
37
  writable: true
38
38
  },
39
39
  stack: {
40
- enumerable: true,
40
+ enumerable: false,
41
41
  value: void 0,
42
42
  writable: true
43
43
  }
44
44
  }
45
45
  );
46
46
  const toJsonWasCalled = /* @__PURE__ */ new WeakSet();
47
+ const makePropertiesEnumerable = (object) => {
48
+ if (!object || typeof object !== "object") {
49
+ return;
50
+ }
51
+ const props = Object.getOwnPropertyNames(object);
52
+ for (const prop of props) {
53
+ const descriptor = Object.getOwnPropertyDescriptor(object, prop);
54
+ if (descriptor) {
55
+ if (!descriptor.enumerable) {
56
+ Object.defineProperty(object, prop, {
57
+ ...descriptor,
58
+ enumerable: true
59
+ });
60
+ }
61
+ if (descriptor.value && typeof descriptor.value === "object" && !Array.isArray(descriptor.value) && (Object.getPrototypeOf(descriptor.value) === Object.prototype || Object.getPrototypeOf(descriptor.value) === null)) {
62
+ makePropertiesEnumerable(descriptor.value);
63
+ }
64
+ }
65
+ }
66
+ };
47
67
  const toJSON = (from) => {
48
68
  toJsonWasCalled.add(from);
49
69
  const json = from.toJSON();
50
70
  toJsonWasCalled.delete(from);
71
+ if (json && typeof json === "object" && Object.isExtensible(json)) {
72
+ makePropertiesEnumerable(json);
73
+ }
51
74
  return json;
52
75
  };
53
76
  const serializeValue = (value, seen, depth, options) => {
@@ -58,7 +81,7 @@ const serializeValue = (value, seen, depth, options) => {
58
81
  return "[object Stream]";
59
82
  }
60
83
  if (value instanceof Error) {
61
- if (seen.includes(value)) {
84
+ if (seen.has(value)) {
62
85
  return "[Circular]";
63
86
  }
64
87
  depth += 1;
@@ -73,11 +96,14 @@ const serializeValue = (value, seen, depth, options) => {
73
96
  if (typeof value === "function") {
74
97
  return `[Function: ${value.name || "anonymous"}]`;
75
98
  }
99
+ if (typeof value === "bigint") {
100
+ return `${value}n`;
101
+ }
76
102
  if (isPlainObject(value)) {
77
- depth += 1;
78
- if (options.maxDepth && depth >= options.maxDepth) {
103
+ if (options.maxDepth !== void 0 && options.maxDepth !== Number.POSITIVE_INFINITY && depth + 1 >= options.maxDepth) {
79
104
  return {};
80
105
  }
106
+ depth += 1;
81
107
  const plainObject = {};
82
108
  for (const key in value) {
83
109
  plainObject[key] = serializeValue(value[key], seen, depth, options);
@@ -91,7 +117,7 @@ const serializeValue = (value, seen, depth, options) => {
91
117
  }
92
118
  };
93
119
  const _serialize = (error, options, seen, depth) => {
94
- seen.push(error);
120
+ seen.add(error);
95
121
  if (options.maxDepth === 0) {
96
122
  return {};
97
123
  }
@@ -99,31 +125,87 @@ const _serialize = (error, options, seen, depth) => {
99
125
  return toJSON(error);
100
126
  }
101
127
  const protoError = Object.create(ErrorProto);
102
- protoError.name = Object.prototype.toString.call(error.constructor) === "[object Function]" ? error.constructor.name : error.name;
103
- protoError.message = error.message;
104
- protoError.stack = error.stack;
128
+ Object.defineProperty(protoError, "name", {
129
+ configurable: true,
130
+ enumerable: true,
131
+ value: Object.prototype.toString.call(error.constructor) === "[object Function]" ? error.constructor.name : error.name,
132
+ writable: true
133
+ });
134
+ Object.defineProperty(protoError, "message", {
135
+ configurable: true,
136
+ enumerable: true,
137
+ value: error.message,
138
+ writable: true
139
+ });
140
+ Object.defineProperty(protoError, "stack", {
141
+ configurable: true,
142
+ enumerable: true,
143
+ value: error.stack,
144
+ writable: true
145
+ });
105
146
  if (Array.isArray(error.errors)) {
106
147
  const aggregateErrors = [];
107
148
  for (const aggregateError of error.errors) {
108
149
  if (!(aggregateError instanceof Error)) {
109
150
  throw new TypeError("All errors in the 'errors' property must be instances of Error");
110
151
  }
111
- if (seen.includes(aggregateError)) {
112
- protoError.errors = [];
152
+ if (seen.has(aggregateError)) {
153
+ Object.defineProperty(protoError, "errors", {
154
+ configurable: true,
155
+ enumerable: true,
156
+ value: [],
157
+ writable: true
158
+ });
113
159
  return protoError;
114
160
  }
115
161
  aggregateErrors.push(_serialize(aggregateError, options, seen, depth));
116
162
  }
117
- protoError.errors = aggregateErrors;
163
+ Object.defineProperty(protoError, "errors", {
164
+ configurable: true,
165
+ enumerable: true,
166
+ value: aggregateErrors,
167
+ writable: true
168
+ });
118
169
  }
119
- if (error.cause instanceof Error && !seen.includes(error.cause)) {
120
- protoError.cause = _serialize(error.cause, options, seen, depth);
170
+ if (error.cause !== void 0 && error.cause !== null) {
171
+ if (error.cause instanceof Error) {
172
+ if (seen.has(error.cause)) {
173
+ Object.defineProperty(protoError, "cause", {
174
+ configurable: true,
175
+ enumerable: true,
176
+ value: "[Circular]",
177
+ writable: true
178
+ });
179
+ } else {
180
+ Object.defineProperty(protoError, "cause", {
181
+ configurable: true,
182
+ enumerable: true,
183
+ value: _serialize(error.cause, options, seen, depth),
184
+ writable: true
185
+ });
186
+ }
187
+ } else {
188
+ const serializedCause = serializeValue(error.cause, seen, depth, options);
189
+ Object.defineProperty(protoError, "cause", {
190
+ configurable: true,
191
+ enumerable: true,
192
+ value: serializedCause,
193
+ writable: true
194
+ });
195
+ }
121
196
  }
122
197
  for (const key in error) {
123
- if (protoError[key] === void 0) {
124
- const value = error[key];
125
- protoError[key] = serializeValue(value, seen, depth, options);
198
+ if (key === "name" || key === "message" || key === "stack" || key === "cause" || key === "errors") {
199
+ continue;
126
200
  }
201
+ const value = error[key];
202
+ const serializedValue = serializeValue(value, seen, depth, options);
203
+ Object.defineProperty(protoError, key, {
204
+ configurable: true,
205
+ enumerable: true,
206
+ value: serializedValue,
207
+ writable: true
208
+ });
127
209
  }
128
210
  if (Array.isArray(options.exclude) && options.exclude.length > 0) {
129
211
  for (const key of options.exclude) {
@@ -142,7 +224,7 @@ const serialize = (error, options = {}) => _serialize(
142
224
  maxDepth: options.maxDepth ?? Number.POSITIVE_INFINITY,
143
225
  useToJSON: options.useToJSON ?? false
144
226
  },
145
- [],
227
+ /* @__PURE__ */ new Set(),
146
228
  0
147
229
  );
148
230
 
@@ -9,7 +9,7 @@ const {
9
9
  stderr
10
10
  } = __cjs_getProcess;
11
11
  import { w as writeStream } from './write-stream-BG8fhcs3.js';
12
- import { AbstractJsonReporter } from './AbstractJsonReporter-DWRpTtGw.js';
12
+ import { AbstractJsonReporter } from './AbstractJsonReporter-CGKHS8_M.js';
13
13
 
14
14
  class JsonReporter extends AbstractJsonReporter {
15
15
  /** Standard output stream */
@@ -1,4 +1,4 @@
1
- import { AbstractJsonReporter } from './AbstractJsonReporter-BaZ33PlE.js';
1
+ import { AbstractJsonReporter } from './AbstractJsonReporter-DDjDkciI.js';
2
2
  import { w as writeConsoleLogBasedOnLevel } from './write-console-log-based-on-level-DBmRYXpj.js';
3
3
 
4
4
  class JsonReporter extends AbstractJsonReporter {