@visulima/pail 4.0.0-alpha.1 → 4.0.0-alpha.10

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 (81) hide show
  1. package/CHANGELOG.md +164 -0
  2. package/LICENSE.md +434 -6
  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 +3 -1
  7. package/dist/index.browser.js +1490 -3
  8. package/dist/index.server.d.ts +3 -1
  9. package/dist/index.server.js +2668 -14
  10. package/dist/interactive/index.js +2 -2
  11. package/dist/middleware/elysia.d.ts +71 -0
  12. package/dist/middleware/elysia.js +70 -0
  13. package/dist/middleware/express.d.ts +86 -0
  14. package/dist/middleware/express.js +29 -0
  15. package/dist/middleware/fastify.d.ts +81 -0
  16. package/dist/middleware/fastify.js +46 -0
  17. package/dist/middleware/hono.d.ts +85 -0
  18. package/dist/middleware/hono.js +33 -0
  19. package/dist/middleware/next/handler.d.ts +36 -0
  20. package/dist/middleware/next/handler.js +53 -0
  21. package/dist/middleware/next/middleware.d.ts +59 -0
  22. package/dist/middleware/next/storage.d.ts +14 -0
  23. package/dist/middleware/shared/create-middleware-logger.d.ts +82 -0
  24. package/dist/middleware/shared/headers.d.ts +14 -0
  25. package/dist/middleware/shared/routes.d.ts +30 -0
  26. package/dist/middleware/shared/storage.d.ts +29 -0
  27. package/dist/middleware/sveltekit.d.ts +123 -0
  28. package/dist/middleware/sveltekit.js +43 -0
  29. package/dist/object-tree.d.ts +2 -2
  30. package/dist/object-tree.js +7 -7
  31. package/dist/packem_shared/AbstractJsonReporter-CjtVgHbU.js +288 -0
  32. package/dist/packem_shared/{AbstractJsonReporter-BaZ33PlE.js → AbstractJsonReporter-DlugSJpY.js} +111 -27
  33. package/dist/packem_shared/{InteractiveManager-CZ85hGNW.js → InteractiveManager-CowYA3Hx.js} +17 -11
  34. package/dist/packem_shared/{interactive-stream-hook-DG4BtN12.js → InteractiveStreamHook-BypRlYTX.js} +3 -11
  35. package/dist/packem_shared/{JsonReporter-VzgyLEYz.js → JsonReporter-BgPvIyC2.js} +2 -2
  36. package/dist/packem_shared/{JsonReporter-BRw4skd5.js → JsonReporter-Dbw82ewj.js} +2 -2
  37. package/dist/packem_shared/{PrettyReporter-DySIXBjQ.js → PrettyReporter-C2dCzIaf.js} +54 -8
  38. package/dist/packem_shared/{format-label-De49vNPd.js → PrettyReporter-gMqa7j_m.js} +370 -75
  39. package/dist/packem_shared/Spinner-Cokext9b.js +2183 -0
  40. package/dist/packem_shared/abstract-pretty-reporter-szQO-IgK.js +2635 -0
  41. package/dist/packem_shared/constants-B1RjD_ps.js +99 -0
  42. package/dist/packem_shared/createPailError-B_sgL0nF.js +76 -0
  43. package/dist/packem_shared/getBarChar-D7JfmdTr.js +459 -0
  44. package/dist/packem_shared/headers-BxHWM6KI.js +127 -0
  45. package/dist/packem_shared/{index-DqKWykfa.js → index-BEfVUy9P.js} +174 -64
  46. package/dist/packem_shared/{index-BomQ3E6J.js → index-Bx3-C0j9.js} +29 -21
  47. package/dist/packem_shared/pailMiddleware-Ci88geIF.js +24 -0
  48. package/dist/packem_shared/storage-D0vqz8OX.js +36 -0
  49. package/dist/packem_shared/{InteractiveStreamHook-DiSubbJ1.js → useLogger-D0rU3lcX.js} +13 -1
  50. package/dist/packem_shared/{write-console-log-based-on-level-DBmRYXpj.js → write-console-log-based-on-level-ree2lDPw.js} +5 -4
  51. package/dist/packem_shared/{write-stream-BG8fhcs3.js → write-stream-BuFtjATz.js} +1 -1
  52. package/dist/pail.browser.d.ts +1 -1
  53. package/dist/processor/environment-processor.d.ts +124 -0
  54. package/dist/processor/environment-processor.js +82 -0
  55. package/dist/processor/message-formatter-processor.d.ts +2 -3
  56. package/dist/processor/message-formatter-processor.js +654 -5
  57. package/dist/processor/opentelemetry-processor.js +4 -4
  58. package/dist/processor/redact-processor.d.ts +1 -1
  59. package/dist/processor/redact-processor.js +2 -1
  60. package/dist/processor/sampling-processor.d.ts +111 -0
  61. package/dist/processor/sampling-processor.js +59 -0
  62. package/dist/progress-bar.d.ts +10 -1
  63. package/dist/progress-bar.js +75 -20
  64. package/dist/reporter/file/json-file-reporter.js +1 -1
  65. package/dist/reporter/http/abstract-http-reporter.js +23 -26
  66. package/dist/reporter/http/http-reporter.edge-light.js +133 -52
  67. package/dist/reporter/json/abstract-json-reporter.d.ts +1 -1
  68. package/dist/reporter/json/index.browser.js +2 -2
  69. package/dist/reporter/json/index.js +2 -2
  70. package/dist/reporter/pretty/index.browser.js +1 -1
  71. package/dist/reporter/pretty/index.js +1 -1
  72. package/dist/reporter/simple/simple-reporter.server.js +8 -12
  73. package/dist/spinner.js +37 -4
  74. package/dist/types.d.ts +3 -3
  75. package/dist/wide-event.d.ts +300 -0
  76. package/dist/wide-event.js +283 -0
  77. package/package.json +71 -7
  78. package/dist/packem_shared/PrettyReporter-DgZB2eBG.js +0 -222
  79. package/dist/packem_shared/abstract-pretty-reporter-Di_sdm2r.js +0 -50
  80. package/dist/packem_shared/get-longest-label-C9PWeyKq.js +0 -9
  81. package/dist/packem_shared/pail.browser-u2CSR_af.js +0 -1427
@@ -0,0 +1,82 @@
1
+ import type { PailBrowserImpl } from "../../pail.d.ts";
2
+ import type { DefaultLogTypes, LoggerFunction } from "../../types.d.ts";
3
+ import type { WideEventFinishOptions } from "../../wide-event.d.ts";
4
+ import { WideEvent } from "../../wide-event.d.ts";
5
+ import type { RouteConfig } from "./routes.d.ts";
6
+ /**
7
+ * A pail instance with dynamically generated log methods.
8
+ */
9
+ type PailLike<T extends string = string> = PailBrowserImpl<T> & Record<DefaultLogTypes | T, LoggerFunction>;
10
+ /**
11
+ * Base options shared by all framework middleware adapters.
12
+ * @template T - Custom logger type names from the pail instance
13
+ */
14
+ export interface PailMiddlewareOptions<T extends string = string> {
15
+ /**
16
+ * Glob patterns for paths to exclude from logging.
17
+ * Exclusions take precedence over inclusions.
18
+ * @example ["/health", "/api/_internal/**"]
19
+ */
20
+ exclude?: string[];
21
+ /**
22
+ * Glob patterns for paths to include in logging.
23
+ * If not set, all non-excluded paths are logged.
24
+ * @example ["/api/**"]
25
+ */
26
+ include?: string[];
27
+ /**
28
+ * The pail logger instance to use for wide event emission.
29
+ */
30
+ pail: PailLike<T>;
31
+ /**
32
+ * Route-specific configuration. Maps glob patterns to config.
33
+ * First matching route wins.
34
+ * @example { "/api/auth/**": { service: "auth-service" } }
35
+ */
36
+ routes?: Record<string, RouteConfig>;
37
+ /**
38
+ * Default service name for all wide events.
39
+ * Can be overridden per-route via `routes`.
40
+ */
41
+ service?: string;
42
+ }
43
+ /**
44
+ * Result of creating a middleware logger for a request.
45
+ */
46
+ export interface MiddlewareLoggerResult {
47
+ /**
48
+ * Finalize and emit the wide event. Sets status/error before emitting.
49
+ * Safe to call multiple times — only the first call emits.
50
+ */
51
+ finish: (options?: WideEventFinishOptions) => void;
52
+ /**
53
+ * The request-scoped WideEvent logger.
54
+ * Use `set()`, `info()`, `warn()`, `error()`, `debug()` to accumulate context.
55
+ */
56
+ logger: WideEvent;
57
+ /**
58
+ * Whether this request was skipped (excluded from logging).
59
+ * When true, `logger` and `finish` should not be used.
60
+ */
61
+ skipped: boolean;
62
+ }
63
+ /**
64
+ * Core factory function used by all framework middleware adapters.
65
+ *
66
+ * Creates a WideEvent for the given request, checks route inclusion/exclusion,
67
+ * resolves the service name, and returns a logger + finish callback.
68
+ * @param options Middleware options including pail instance and route config
69
+ * @param request Request metadata for the current HTTP request
70
+ * @param request.headers Safe headers extracted from the request
71
+ * @param request.method The HTTP method (GET, POST, etc.)
72
+ * @param request.path The URL path of the request
73
+ * @param request.requestId A unique identifier for this request
74
+ * @returns A result object with logger, finish callback, and skipped flag
75
+ */
76
+ export declare const createMiddlewareLogger: <T extends string = string>(options: PailMiddlewareOptions<T>, request: {
77
+ headers?: Record<string, string>;
78
+ method: string;
79
+ path: string;
80
+ requestId: string;
81
+ }) => MiddlewareLoggerResult;
82
+ export {};
@@ -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-BxHWM6KI.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 };
@@ -16,7 +16,7 @@ export type TreeSortFunction = (a: string, b: string) => number;
16
16
  */
17
17
  export interface ObjectTreeOptions {
18
18
  /** Text to display for circular references (default: " (circular ref.)") */
19
- breakCircularWith?: string | null | undefined;
19
+ breakCircularWith?: string | null;
20
20
  /** Whether to return as single string or array of lines (default: true) */
21
21
  joined?: boolean;
22
22
  /** Connector for neighbor keys (default: "├─ ") */
@@ -28,7 +28,7 @@ export interface ObjectTreeOptions {
28
28
  /** Separator between key and value (default: ": ") */
29
29
  separator?: string;
30
30
  /** Function to sort object keys (default: natural order) */
31
- sortFn?: TreeSortFunction | undefined;
31
+ sortFn?: TreeSortFunction;
32
32
  /** Spacer for neighbor branches (default: "│ ") */
33
33
  spacerNeighbour?: string;
34
34
  /** Spacer for non-neighbor branches (default: " ") */
@@ -50,22 +50,22 @@ const renderObjectTree = (tree, options) => {
50
50
  const result = [];
51
51
  const rootRendered = context.renderFn(tree);
52
52
  if (rootRendered !== void 0) {
53
- result.push(String(rootRendered));
53
+ result.push(rootRendered);
54
54
  }
55
55
  const sort = (input) => {
56
56
  if (context.sortFn === void 0) {
57
57
  return input;
58
58
  }
59
- return input.toSorted((a, b) => context?.sortFn?.(b, a) ?? 0);
59
+ return input.toSorted((a, b) => context.sortFn?.(b, a) ?? 0);
60
60
  };
61
61
  const neighbours = [];
62
62
  const keys = sort(Object.keys(tree)).map((k) => [k]);
63
63
  const lookup = [tree];
64
64
  while (keys.length > 0) {
65
65
  const key = keys.pop() ?? [];
66
- const node = lookup[key.length - 1][key[key.length - 1] ?? ""];
66
+ const node = lookup[key.length - 1][key.at(-1) ?? ""];
67
67
  const isCircular = context.breakCircularWith !== null && lookup.slice(0, key.length).includes(node);
68
- neighbours[key.length - 1] = keys.length > 0 && (keys[keys.length - 1]?.length ?? 0) === key.length;
68
+ neighbours[key.length - 1] = keys.length > 0 && (keys.at(-1)?.length ?? 0) === key.length;
69
69
  const indent = neighbours.slice(0, key.length - 1).map((n) => {
70
70
  if (n) {
71
71
  return context.spacerNeighbour;
@@ -73,17 +73,17 @@ const renderObjectTree = (tree, options) => {
73
73
  return context.spacerNoNeighbour;
74
74
  }).join("");
75
75
  const connector = neighbours[key.length - 1] ? context.keyNeighbour : context.keyNoNeighbour;
76
- const keyName = key[key.length - 1];
76
+ const keyName = key.at(-1) ?? "";
77
77
  const nodeRendered = context.renderFn(node);
78
78
  const value = nodeRendered === void 0 ? "" : `${context.separator}${nodeRendered}`;
79
- const circular = isCircular ? context.breakCircularWith : "";
79
+ const circular = isCircular ? context.breakCircularWith ?? "" : "";
80
80
  result.push(`${indent}${connector}${keyName}${value}${circular}`);
81
81
  if (node !== null && typeof node === "object" && !Array.isArray(node) && !isCircular) {
82
82
  keys.push(...sort(Object.keys(node)).map((k) => [...key, k]));
83
83
  lookup[key.length] = node;
84
84
  }
85
85
  }
86
- return context.joined === true ? result.join("\n") : result;
86
+ return context.joined ? result.join("\n") : result;
87
87
  };
88
88
 
89
89
  export { renderObjectTree as default, renderObjectTree };
@@ -0,0 +1,288 @@
1
+ import { E as EMPTY_SYMBOL } from './constants-B1RjD_ps.js';
2
+
3
+ function isPlainObject(value) {
4
+ if (typeof value !== "object" || value === null) {
5
+ return false;
6
+ }
7
+ const prototype = Object.getPrototypeOf(value);
8
+ return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value);
9
+ }
10
+
11
+ const ErrorProto = Object.create(
12
+ {},
13
+ {
14
+ cause: {
15
+ enumerable: false,
16
+ value: void 0,
17
+ writable: true
18
+ },
19
+ code: {
20
+ enumerable: true,
21
+ value: void 0,
22
+ writable: true
23
+ },
24
+ errors: {
25
+ enumerable: false,
26
+ value: void 0,
27
+ writable: true
28
+ },
29
+ message: {
30
+ enumerable: false,
31
+ value: void 0,
32
+ writable: true
33
+ },
34
+ name: {
35
+ enumerable: false,
36
+ value: void 0,
37
+ writable: true
38
+ },
39
+ stack: {
40
+ enumerable: false,
41
+ value: void 0,
42
+ writable: true
43
+ }
44
+ }
45
+ );
46
+ const toJsonWasCalled = /* @__PURE__ */ new WeakSet();
47
+ const makePropertiesEnumerable = (object) => {
48
+ const props = Object.getOwnPropertyNames(object);
49
+ for (const prop of props) {
50
+ const descriptor = Object.getOwnPropertyDescriptor(object, prop);
51
+ if (descriptor) {
52
+ if (!descriptor.enumerable) {
53
+ Object.defineProperty(object, prop, {
54
+ ...descriptor,
55
+ enumerable: true
56
+ });
57
+ }
58
+ if (descriptor.value && typeof descriptor.value === "object" && !Array.isArray(descriptor.value) && (Object.getPrototypeOf(descriptor.value) === Object.prototype || Object.getPrototypeOf(descriptor.value) === null)) {
59
+ makePropertiesEnumerable(descriptor.value);
60
+ }
61
+ }
62
+ }
63
+ };
64
+ const toJSON = (from) => {
65
+ toJsonWasCalled.add(from);
66
+ const json = from.toJSON();
67
+ toJsonWasCalled.delete(from);
68
+ if (
69
+ // Only make properties enumerable if the object is extensible
70
+ // Non-extensible objects (like when toJSON returns 'this') should preserve original enumerability
71
+ Object.isExtensible(json)
72
+ ) {
73
+ makePropertiesEnumerable(json);
74
+ }
75
+ return json;
76
+ };
77
+ const serializeValue = (value, seen, depth, options) => {
78
+ if (value && value instanceof Uint8Array && value.constructor.name === "Buffer") {
79
+ return "[object Buffer]";
80
+ }
81
+ if (value !== null && typeof value === "object" && "pipe" in value && typeof value.pipe === "function") {
82
+ return "[object Stream]";
83
+ }
84
+ if (value instanceof Error) {
85
+ if (seen.has(value)) {
86
+ return "[Circular]";
87
+ }
88
+ depth += 1;
89
+ return _serialize(value, options, seen, depth);
90
+ }
91
+ if (options.useToJSON && value !== null && typeof value === "object" && "toJSON" in value && typeof value.toJSON === "function") {
92
+ return value.toJSON();
93
+ }
94
+ if (value instanceof Date) {
95
+ return value.toISOString();
96
+ }
97
+ if (typeof value === "function") {
98
+ return `[Function: ${value.name || "anonymous"}]`;
99
+ }
100
+ if (typeof value === "bigint") {
101
+ return `${String(value)}n`;
102
+ }
103
+ if (isPlainObject(value)) {
104
+ if (options.maxDepth !== void 0 && options.maxDepth !== Number.POSITIVE_INFINITY && depth + 1 >= options.maxDepth) {
105
+ return {};
106
+ }
107
+ depth += 1;
108
+ const plainObject = {};
109
+ for (const key in value) {
110
+ plainObject[key] = serializeValue(value[key], seen, depth, options);
111
+ }
112
+ return plainObject;
113
+ }
114
+ try {
115
+ return value;
116
+ } catch {
117
+ return "[Not Available]";
118
+ }
119
+ };
120
+ const _serialize = (error, options, seen, depth) => {
121
+ seen.add(error);
122
+ if (options.maxDepth === 0) {
123
+ return {};
124
+ }
125
+ if (options.useToJSON && typeof error.toJSON === "function" && !toJsonWasCalled.has(error)) {
126
+ return toJSON(error);
127
+ }
128
+ const protoError = Object.create(ErrorProto);
129
+ Object.defineProperty(protoError, "name", {
130
+ configurable: true,
131
+ enumerable: true,
132
+ value: Object.prototype.toString.call(error.constructor) === "[object Function]" ? error.constructor.name : error.name,
133
+ writable: true
134
+ });
135
+ Object.defineProperty(protoError, "message", {
136
+ configurable: true,
137
+ enumerable: true,
138
+ value: error.message,
139
+ writable: true
140
+ });
141
+ Object.defineProperty(protoError, "stack", {
142
+ configurable: true,
143
+ enumerable: true,
144
+ value: error.stack,
145
+ writable: true
146
+ });
147
+ if (Array.isArray(error.errors)) {
148
+ const aggregateErrors = [];
149
+ for (const aggregateError of error.errors) {
150
+ if (!(aggregateError instanceof Error)) {
151
+ throw new TypeError("All errors in the 'errors' property must be instances of Error");
152
+ }
153
+ if (seen.has(aggregateError)) {
154
+ Object.defineProperty(protoError, "errors", {
155
+ configurable: true,
156
+ enumerable: true,
157
+ value: [],
158
+ writable: true
159
+ });
160
+ return protoError;
161
+ }
162
+ aggregateErrors.push(_serialize(aggregateError, options, seen, depth));
163
+ }
164
+ Object.defineProperty(protoError, "errors", {
165
+ configurable: true,
166
+ enumerable: true,
167
+ value: aggregateErrors,
168
+ writable: true
169
+ });
170
+ }
171
+ const causeValue = error.cause;
172
+ if (causeValue !== void 0 && causeValue !== null) {
173
+ if (causeValue instanceof Error) {
174
+ if (seen.has(causeValue)) {
175
+ Object.defineProperty(protoError, "cause", {
176
+ configurable: true,
177
+ enumerable: true,
178
+ value: "[Circular]",
179
+ writable: true
180
+ });
181
+ } else {
182
+ Object.defineProperty(protoError, "cause", {
183
+ configurable: true,
184
+ enumerable: true,
185
+ value: _serialize(causeValue, options, seen, depth),
186
+ writable: true
187
+ });
188
+ }
189
+ } else {
190
+ const serializedCause = serializeValue(causeValue, seen, depth, options);
191
+ Object.defineProperty(protoError, "cause", {
192
+ configurable: true,
193
+ enumerable: true,
194
+ value: serializedCause,
195
+ writable: true
196
+ });
197
+ }
198
+ }
199
+ for (const key in error) {
200
+ if (key === "name" || key === "message" || key === "stack" || key === "cause" || key === "errors") {
201
+ continue;
202
+ }
203
+ const value = error[key];
204
+ const serializedValue = serializeValue(value, seen, depth, options);
205
+ Object.defineProperty(protoError, key, {
206
+ configurable: true,
207
+ enumerable: true,
208
+ value: serializedValue,
209
+ writable: true
210
+ });
211
+ }
212
+ if (Array.isArray(options.exclude) && options.exclude.length > 0) {
213
+ for (const key of options.exclude) {
214
+ try {
215
+ delete protoError[key];
216
+ } catch {
217
+ }
218
+ }
219
+ }
220
+ return protoError;
221
+ };
222
+ const serialize = (error, options = {}) => _serialize(
223
+ error,
224
+ {
225
+ exclude: options.exclude ?? [],
226
+ maxDepth: options.maxDepth ?? Number.POSITIVE_INFINITY,
227
+ useToJSON: options.useToJSON ?? false
228
+ },
229
+ /* @__PURE__ */ new Set(),
230
+ 0
231
+ );
232
+
233
+ class AbstractJsonReporter {
234
+ /** Custom stringify function for object serialization */
235
+ stringify;
236
+ /** Error serialization options */
237
+ errorOptions;
238
+ /**
239
+ * Creates a new AbstractJsonReporter instance.
240
+ * @param options Configuration options for JSON formatting and error handling
241
+ */
242
+ constructor(options = {}) {
243
+ this.errorOptions = options.error ?? {};
244
+ }
245
+ /**
246
+ * Sets a custom stringify function for object serialization.
247
+ * @param function_ The stringify function to use for serialization
248
+ */
249
+ setStringify(function_) {
250
+ this.stringify = function_;
251
+ }
252
+ // eslint-disable-next-line sonarjs/cognitive-complexity
253
+ log(meta) {
254
+ const { context, error, file, message, type, ...rest } = meta;
255
+ if (rest.label) {
256
+ rest.label = rest.label.trim();
257
+ }
258
+ if (file) {
259
+ rest.file = `${file.name ?? ""}:${String(file.line)}${file.column ? `:${String(file.column)}` : ""}`;
260
+ }
261
+ if (message === EMPTY_SYMBOL) {
262
+ rest.message = void 0;
263
+ } else {
264
+ rest.message = message;
265
+ }
266
+ if (error) {
267
+ rest.error = serialize(error, this.errorOptions);
268
+ }
269
+ if (context) {
270
+ const newContext = [];
271
+ for (let i = 0; i < context.length; i += 1) {
272
+ const item = context[i];
273
+ if (item === EMPTY_SYMBOL) {
274
+ continue;
275
+ }
276
+ if (item instanceof Error) {
277
+ newContext.push(serialize(item, this.errorOptions));
278
+ } else {
279
+ newContext.push(item);
280
+ }
281
+ }
282
+ rest.context = newContext;
283
+ }
284
+ this._log(this.stringify(rest), type.level);
285
+ }
286
+ }
287
+
288
+ export { AbstractJsonReporter };