@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
@@ -4,23 +4,1758 @@ const __cjs_require = __cjs_createRequire(import.meta.url);
4
4
 
5
5
  const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process;
6
6
 
7
+ const __cjs_getBuiltinModule = (module) => {
8
+ // Check if we're in Node.js and version supports getBuiltinModule
9
+ if (typeof __cjs_getProcess !== "undefined" && __cjs_getProcess.versions && __cjs_getProcess.versions.node) {
10
+ const [major, minor] = __cjs_getProcess.versions.node.split(".").map(Number);
11
+ // Node.js 20.16.0+ and 22.3.0+
12
+ if (major > 22 || (major === 22 && minor >= 3) || (major === 20 && minor >= 16)) {
13
+ return __cjs_getProcess.getBuiltinModule(module);
14
+ }
15
+ }
16
+ // Fallback to createRequire
17
+ return __cjs_require(module);
18
+ };
19
+
7
20
  const {
8
21
  stdout,
9
22
  stderr,
10
23
  env
11
24
  } = __cjs_getProcess;
12
- import InteractiveManager from './packem_shared/InteractiveManager-CZ85hGNW.js';
13
- import { I as InteractiveStreamHook, c as clearTerminal } from './packem_shared/interactive-stream-hook-DG4BtN12.js';
14
- import { a as PailBrowserImpl, m as mergeTypes, b as arrayify } from './packem_shared/pail.browser-u2CSR_af.js';
15
- import { ProgressBar, applyStyleToOptions, MultiProgressBar } from './progress-bar.js';
16
- export { getBarChar } from './progress-bar.js';
17
- import { i as inspect } from './packem_shared/format-label-De49vNPd.js';
18
- import { E as EMPTY_SYMBOL } from './packem_shared/constants-omsTHUWB.js';
19
- import { w as writeStream } from './packem_shared/write-stream-BG8fhcs3.js';
20
- import { Spinner, MultiSpinner } from './spinner.js';
21
- import { g as getLongestLabel } from './packem_shared/get-longest-label-C9PWeyKq.js';
22
- import MessageFormatterProcessor from './processor/message-formatter-processor.js';
23
- import { PrettyReporter } from './packem_shared/PrettyReporter-DgZB2eBG.js';
25
+ import { t as terminalSize, w as wordWrap, W as WrapMode, g as getLongestLabel, E as EXTENDED_RFC_5424_LOG_LEVELS, a as EMPTY_SYMBOL, L as LOG_TYPES, i as inspect, b as writeStream, A as AbstractPrettyReporter, d as defaultInspectorConfig, c as getLongestBadge, e as getStringWidth, f as formatLabel, r as renderError } from './packem_shared/abstract-pretty-reporter-szQO-IgK.js';
26
+ const {
27
+ StringDecoder
28
+ } = __cjs_getBuiltinModule("node:string_decoder");
29
+ import { ProgressBar, applyStyleToOptions, MultiProgressBar } from './packem_shared/getBarChar-D7JfmdTr.js';
30
+ export { getBarChar } from './packem_shared/getBarChar-D7JfmdTr.js';
31
+ import { Spinner, MultiSpinner } from './packem_shared/Spinner-Cokext9b.js';
32
+ import colorize, { red, greenBright, cyan, green, grey, bgGrey, underline, white } from '@visulima/colorize';
33
+ export { PailError, createPailError } from './packem_shared/createPailError-B_sgL0nF.js';
34
+
35
+ class InteractiveManager {
36
+ #stream;
37
+ #isActive = false;
38
+ #isSuspended = false;
39
+ #lastLength = 0;
40
+ #outside = 0;
41
+ /**
42
+ * Creates a new InteractiveManager with the given stream hooks.
43
+ * @param stdout Hook for stdout stream
44
+ * @param stderr Hook for stderr stream
45
+ */
46
+ constructor(stdout, stderr) {
47
+ this.#stream = {
48
+ stderr,
49
+ stdout
50
+ };
51
+ }
52
+ /**
53
+ * Last printed rows count.
54
+ *
55
+ * Tracks the number of rows that were last written to the terminal.
56
+ * Used internally for managing cursor positioning and output updates.
57
+ */
58
+ get lastLength() {
59
+ return this.#lastLength;
60
+ }
61
+ /**
62
+ * Rows count outside editable area.
63
+ *
64
+ * Tracks the number of rows that extend beyond the current terminal height.
65
+ * Used for managing scrolling and ensuring all output remains visible.
66
+ */
67
+ get outside() {
68
+ return this.#outside;
69
+ }
70
+ /**
71
+ * Hook activity status.
72
+ *
73
+ * Indicates whether the interactive hooks are currently active.
74
+ * When true, streams are being intercepted for interactive output.
75
+ */
76
+ get isHooked() {
77
+ return this.#isActive;
78
+ }
79
+ /**
80
+ * Suspend status for active hooks.
81
+ *
82
+ * Indicates whether interactive mode is temporarily suspended.
83
+ * When suspended, external output can be written without interference.
84
+ */
85
+ get isSuspended() {
86
+ return this.#isSuspended;
87
+ }
88
+ /**
89
+ * Removes lines from the terminal output.
90
+ *
91
+ * Erases the specified number of lines from the bottom of the output,
92
+ * moving the cursor up and clearing the lines. Useful for removing
93
+ * previous interactive output before displaying new content.
94
+ * @param stream The stream to erase lines from ("stdout" or "stderr")
95
+ * @param count Number of lines to remove (defaults to lastLength)
96
+ * @throws {TypeError} If the specified stream is not available
97
+ */
98
+ erase(stream, count = this.#lastLength) {
99
+ this.#stream[stream].erase(count);
100
+ }
101
+ /**
102
+ * Hook stdout and stderr streams.
103
+ * @returns Success status
104
+ */
105
+ hook() {
106
+ if (!this.#isActive) {
107
+ const hooks = Object.values(this.#stream);
108
+ for (let i = 0; i < hooks.length; i += 1) {
109
+ hooks[i].active();
110
+ }
111
+ this.#clear(true);
112
+ }
113
+ return this.#isActive;
114
+ }
115
+ /**
116
+ * Resume suspend hooks.
117
+ * @param stream Stream to resume
118
+ * @param eraseRowCount erase output rows count
119
+ */
120
+ resume(stream, eraseRowCount) {
121
+ if (this.#isSuspended) {
122
+ this.#isSuspended = false;
123
+ if (eraseRowCount) {
124
+ this.erase(stream, eraseRowCount);
125
+ }
126
+ this.#lastLength = 0;
127
+ const hooks = Object.values(this.#stream);
128
+ for (let i = 0; i < hooks.length; i += 1) {
129
+ hooks[i].active();
130
+ }
131
+ }
132
+ }
133
+ /**
134
+ * Suspend active hooks for external output.
135
+ * @param stream Stream to suspend
136
+ * @param erase erase output
137
+ */
138
+ suspend(stream, erase = true) {
139
+ if (!this.#isSuspended) {
140
+ this.#isSuspended = true;
141
+ if (erase) {
142
+ this.erase(stream);
143
+ }
144
+ const hooks = Object.values(this.#stream);
145
+ for (let i = 0; i < hooks.length; i += 1) {
146
+ hooks[i].renew();
147
+ }
148
+ }
149
+ }
150
+ /**
151
+ * Unhooks both stdout and stderr streams and print their story of logs.
152
+ * @param separateHistory If `true`, will add an empty line to the history output for individual recorded lines and console logs
153
+ * @returns Success status
154
+ */
155
+ unhook(separateHistory = true) {
156
+ if (this.#isActive) {
157
+ const hooks = Object.values(this.#stream);
158
+ for (let i = 0; i < hooks.length; i += 1) {
159
+ hooks[i].inactive(separateHistory);
160
+ }
161
+ this.#clear();
162
+ }
163
+ return !this.#isActive;
164
+ }
165
+ /**
166
+ * Update output.
167
+ * @param stream Stream to write to
168
+ * @param rows Text lines to write to standard output
169
+ * @param from Index of the line starting from which the contents of the terminal are being overwritten
170
+ */
171
+ update(stream, rows, from = 0) {
172
+ if (rows.length > 0) {
173
+ const hook = this.#stream[stream];
174
+ const { columns: width, rows: height } = terminalSize();
175
+ const position = from > height ? height - 1 : Math.max(0, Math.min(height - 1, from));
176
+ const actualLength = this.lastLength - position;
177
+ const outside = Math.max(actualLength - height, this.outside);
178
+ let output = rows.reduce(
179
+ (accumulator, row) => [
180
+ ...accumulator,
181
+ wordWrap(row, {
182
+ trim: false,
183
+ width,
184
+ wrapMode: WrapMode.STRICT_WIDTH
185
+ })
186
+ ],
187
+ []
188
+ );
189
+ if (height <= actualLength) {
190
+ hook.erase(height);
191
+ if (position < outside) {
192
+ output = output.slice(outside - position + 1);
193
+ }
194
+ } else if (actualLength) {
195
+ hook.erase(actualLength);
196
+ }
197
+ hook.write(`${output.join("\n")}
198
+ `);
199
+ this.#lastLength = outside ? outside + output.length + 1 : output.length;
200
+ this.#outside = Math.max(this.lastLength - height, this.outside);
201
+ }
202
+ }
203
+ #clear(status = false) {
204
+ this.#isActive = status;
205
+ this.#lastLength = 0;
206
+ this.#outside = 0;
207
+ }
208
+ }
209
+
210
+ const ESC = "\x1B[";
211
+ const eraseScreen = `${ESC}2J`;
212
+ const eraseLine = `${ESC}2K`;
213
+ const cursorLeft = `${ESC}G`;
214
+ const cursorUp = (count = 1) => `${ESC}${String(count)}A`;
215
+ const clearTerminal = process.platform === "win32" ? `${eraseScreen}${ESC}0f` : (
216
+ // 1. Erases the screen (Only done in case `2` is not supported)
217
+ // 2. Erases the whole screen including scrollback buffer
218
+ // 3. Moves cursor to the top-left position
219
+ // More info: https://www.real-world-systems.com/docs/ANSIcode.html
220
+ `${eraseScreen}${ESC}3J${ESC}H`
221
+ );
222
+ const cursorHide = `${ESC}?25l`;
223
+ const cursorShow = `${ESC}?25h`;
224
+ const eraseLines = (count) => {
225
+ let clear = "";
226
+ for (let index = 0; index < count; index++) {
227
+ clear += eraseLine + (index < count - 1 ? cursorUp() : "");
228
+ }
229
+ if (count) {
230
+ clear += cursorLeft;
231
+ }
232
+ return clear;
233
+ };
234
+
235
+ class InteractiveStreamHook {
236
+ /** Constant indicating the stream write operation was successful */
237
+ static DRAIN = true;
238
+ #decoder = new StringDecoder();
239
+ #history = [];
240
+ #method;
241
+ #stream;
242
+ /**
243
+ * Creates a new InteractiveStreamHook for the given stream.
244
+ * @param stream The Node.js WriteStream to hook into (usually stdout or stderr)
245
+ */
246
+ constructor(stream) {
247
+ this.#method = stream.write.bind(stream);
248
+ this.#stream = stream;
249
+ }
250
+ /**
251
+ * Activates the stream hook.
252
+ *
253
+ * When active, all writes to the stream are captured in history instead of
254
+ * being written immediately. This allows for interactive features like
255
+ * progress bars that can update dynamically.
256
+ */
257
+ active() {
258
+ this.write(cursorHide);
259
+ this.#stream.write = (data, ...arguments_) => {
260
+ const callback = arguments_.at(-1);
261
+ this.#history.push(
262
+ // prettier-ignore
263
+ this.#decoder.write(
264
+ typeof data === "string" ? Buffer.from(data, typeof arguments_[0] === "string" ? arguments_[0] : void 0) : Buffer.from(data)
265
+ )
266
+ );
267
+ if (typeof callback === "function") {
268
+ callback();
269
+ }
270
+ return InteractiveStreamHook.DRAIN;
271
+ };
272
+ }
273
+ /**
274
+ * Erases the specified number of lines from the terminal.
275
+ *
276
+ * Uses ANSI escape sequences to remove lines from the current cursor position
277
+ * upwards, which is useful for clearing previous output in interactive applications.
278
+ * @param count Number of lines to erase (including the current line)
279
+ */
280
+ erase(count) {
281
+ if (count > 0) {
282
+ this.write(eraseLines(count + 1));
283
+ }
284
+ }
285
+ /**
286
+ * Deactivates the stream hook and replays captured output.
287
+ *
288
+ * Restores normal stream operation and outputs all captured history.
289
+ * Optionally adds a newline separator before replaying the history.
290
+ * @param separateHistory Whether to add a newline before replaying history
291
+ */
292
+ inactive(separateHistory = false) {
293
+ if (this.#history.length > 0) {
294
+ if (separateHistory) {
295
+ this.write("\n");
296
+ }
297
+ this.#history.forEach((element) => {
298
+ this.write(element);
299
+ });
300
+ this.#history = [];
301
+ }
302
+ this.renew();
303
+ }
304
+ /**
305
+ * Renews the stream hook state.
306
+ *
307
+ * Restores the original stream write method and shows the cursor.
308
+ * This is typically called when temporarily suspending interactive mode.
309
+ */
310
+ renew() {
311
+ this.#stream.write = this.#method;
312
+ this.write(cursorShow);
313
+ }
314
+ /**
315
+ * Writes a message directly to the underlying stream.
316
+ *
317
+ * Bypasses the hook mechanism and writes directly using the original
318
+ * stream write method. Useful for writing control sequences or
319
+ * messages that should not be captured in history.
320
+ * @param message The message to write to the stream
321
+ */
322
+ write(message) {
323
+ this.#method.apply(this.#stream, [message]);
324
+ }
325
+ }
326
+
327
+ function getDefaultExportFromCjs(x) {
328
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
329
+ }
330
+
331
+ var safeStableStringify = {exports: {}};
332
+
333
+ var hasRequiredSafeStableStringify;
334
+
335
+ function requireSafeStableStringify () {
336
+ if (hasRequiredSafeStableStringify) return safeStableStringify.exports;
337
+ hasRequiredSafeStableStringify = 1;
338
+ (function (module, exports$1) {
339
+ const { hasOwnProperty } = Object.prototype;
340
+ const stringify = configure();
341
+ stringify.configure = configure;
342
+ stringify.stringify = stringify;
343
+ stringify.default = stringify;
344
+ exports$1.stringify = stringify;
345
+ exports$1.configure = configure;
346
+ module.exports = stringify;
347
+ const strEscapeSequencesRegExp = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]/;
348
+ function strEscape(str) {
349
+ if (str.length < 5e3 && !strEscapeSequencesRegExp.test(str)) {
350
+ return `"${str}"`;
351
+ }
352
+ return JSON.stringify(str);
353
+ }
354
+ function sort(array, comparator) {
355
+ if (array.length > 200 || comparator) {
356
+ return array.sort(comparator);
357
+ }
358
+ for (let i = 1; i < array.length; i++) {
359
+ const currentValue = array[i];
360
+ let position = i;
361
+ while (position !== 0 && array[position - 1] > currentValue) {
362
+ array[position] = array[position - 1];
363
+ position--;
364
+ }
365
+ array[position] = currentValue;
366
+ }
367
+ return array;
368
+ }
369
+ const typedArrayPrototypeGetSymbolToStringTag = Object.getOwnPropertyDescriptor(
370
+ Object.getPrototypeOf(
371
+ Object.getPrototypeOf(
372
+ new Int8Array()
373
+ )
374
+ ),
375
+ Symbol.toStringTag
376
+ ).get;
377
+ function isTypedArrayWithEntries(value) {
378
+ return typedArrayPrototypeGetSymbolToStringTag.call(value) !== void 0 && value.length !== 0;
379
+ }
380
+ function stringifyTypedArray(array, separator, maximumBreadth) {
381
+ if (array.length < maximumBreadth) {
382
+ maximumBreadth = array.length;
383
+ }
384
+ const whitespace = separator === "," ? "" : " ";
385
+ let res = `"0":${whitespace}${array[0]}`;
386
+ for (let i = 1; i < maximumBreadth; i++) {
387
+ res += `${separator}"${i}":${whitespace}${array[i]}`;
388
+ }
389
+ return res;
390
+ }
391
+ function getCircularValueOption(options) {
392
+ if (hasOwnProperty.call(options, "circularValue")) {
393
+ const circularValue = options.circularValue;
394
+ if (typeof circularValue === "string") {
395
+ return `"${circularValue}"`;
396
+ }
397
+ if (circularValue == null) {
398
+ return circularValue;
399
+ }
400
+ if (circularValue === Error || circularValue === TypeError) {
401
+ return {
402
+ toString() {
403
+ throw new TypeError("Converting circular structure to JSON");
404
+ }
405
+ };
406
+ }
407
+ throw new TypeError('The "circularValue" argument must be of type string or the value null or undefined');
408
+ }
409
+ return '"[Circular]"';
410
+ }
411
+ function getDeterministicOption(options) {
412
+ let value;
413
+ if (hasOwnProperty.call(options, "deterministic")) {
414
+ value = options.deterministic;
415
+ if (typeof value !== "boolean" && typeof value !== "function") {
416
+ throw new TypeError('The "deterministic" argument must be of type boolean or comparator function');
417
+ }
418
+ }
419
+ return value === void 0 ? true : value;
420
+ }
421
+ function getBooleanOption(options, key) {
422
+ let value;
423
+ if (hasOwnProperty.call(options, key)) {
424
+ value = options[key];
425
+ if (typeof value !== "boolean") {
426
+ throw new TypeError(`The "${key}" argument must be of type boolean`);
427
+ }
428
+ }
429
+ return value === void 0 ? true : value;
430
+ }
431
+ function getPositiveIntegerOption(options, key) {
432
+ let value;
433
+ if (hasOwnProperty.call(options, key)) {
434
+ value = options[key];
435
+ if (typeof value !== "number") {
436
+ throw new TypeError(`The "${key}" argument must be of type number`);
437
+ }
438
+ if (!Number.isInteger(value)) {
439
+ throw new TypeError(`The "${key}" argument must be an integer`);
440
+ }
441
+ if (value < 1) {
442
+ throw new RangeError(`The "${key}" argument must be >= 1`);
443
+ }
444
+ }
445
+ return value === void 0 ? Infinity : value;
446
+ }
447
+ function getItemCount(number) {
448
+ if (number === 1) {
449
+ return "1 item";
450
+ }
451
+ return `${number} items`;
452
+ }
453
+ function getUniqueReplacerSet(replacerArray) {
454
+ const replacerSet = /* @__PURE__ */ new Set();
455
+ for (const value of replacerArray) {
456
+ if (typeof value === "string" || typeof value === "number") {
457
+ replacerSet.add(String(value));
458
+ }
459
+ }
460
+ return replacerSet;
461
+ }
462
+ function getStrictOption(options) {
463
+ if (hasOwnProperty.call(options, "strict")) {
464
+ const value = options.strict;
465
+ if (typeof value !== "boolean") {
466
+ throw new TypeError('The "strict" argument must be of type boolean');
467
+ }
468
+ if (value) {
469
+ return (value2) => {
470
+ let message = `Object can not safely be stringified. Received type ${typeof value2}`;
471
+ if (typeof value2 !== "function") message += ` (${value2.toString()})`;
472
+ throw new Error(message);
473
+ };
474
+ }
475
+ }
476
+ }
477
+ function configure(options) {
478
+ options = { ...options };
479
+ const fail = getStrictOption(options);
480
+ if (fail) {
481
+ if (options.bigint === void 0) {
482
+ options.bigint = false;
483
+ }
484
+ if (!("circularValue" in options)) {
485
+ options.circularValue = Error;
486
+ }
487
+ }
488
+ const circularValue = getCircularValueOption(options);
489
+ const bigint = getBooleanOption(options, "bigint");
490
+ const deterministic = getDeterministicOption(options);
491
+ const comparator = typeof deterministic === "function" ? deterministic : void 0;
492
+ const maximumDepth = getPositiveIntegerOption(options, "maximumDepth");
493
+ const maximumBreadth = getPositiveIntegerOption(options, "maximumBreadth");
494
+ function stringifyFnReplacer(key, parent, stack, replacer, spacer, indentation) {
495
+ let value = parent[key];
496
+ if (typeof value === "object" && value !== null && typeof value.toJSON === "function") {
497
+ value = value.toJSON(key);
498
+ }
499
+ value = replacer.call(parent, key, value);
500
+ switch (typeof value) {
501
+ case "string":
502
+ return strEscape(value);
503
+ case "object": {
504
+ if (value === null) {
505
+ return "null";
506
+ }
507
+ if (stack.indexOf(value) !== -1) {
508
+ return circularValue;
509
+ }
510
+ let res = "";
511
+ let join = ",";
512
+ const originalIndentation = indentation;
513
+ if (Array.isArray(value)) {
514
+ if (value.length === 0) {
515
+ return "[]";
516
+ }
517
+ if (maximumDepth < stack.length + 1) {
518
+ return '"[Array]"';
519
+ }
520
+ stack.push(value);
521
+ if (spacer !== "") {
522
+ indentation += spacer;
523
+ res += `
524
+ ${indentation}`;
525
+ join = `,
526
+ ${indentation}`;
527
+ }
528
+ const maximumValuesToStringify = Math.min(value.length, maximumBreadth);
529
+ let i = 0;
530
+ for (; i < maximumValuesToStringify - 1; i++) {
531
+ const tmp2 = stringifyFnReplacer(String(i), value, stack, replacer, spacer, indentation);
532
+ res += tmp2 !== void 0 ? tmp2 : "null";
533
+ res += join;
534
+ }
535
+ const tmp = stringifyFnReplacer(String(i), value, stack, replacer, spacer, indentation);
536
+ res += tmp !== void 0 ? tmp : "null";
537
+ if (value.length - 1 > maximumBreadth) {
538
+ const removedKeys = value.length - maximumBreadth - 1;
539
+ res += `${join}"... ${getItemCount(removedKeys)} not stringified"`;
540
+ }
541
+ if (spacer !== "") {
542
+ res += `
543
+ ${originalIndentation}`;
544
+ }
545
+ stack.pop();
546
+ return `[${res}]`;
547
+ }
548
+ let keys = Object.keys(value);
549
+ const keyLength = keys.length;
550
+ if (keyLength === 0) {
551
+ return "{}";
552
+ }
553
+ if (maximumDepth < stack.length + 1) {
554
+ return '"[Object]"';
555
+ }
556
+ let whitespace = "";
557
+ let separator = "";
558
+ if (spacer !== "") {
559
+ indentation += spacer;
560
+ join = `,
561
+ ${indentation}`;
562
+ whitespace = " ";
563
+ }
564
+ const maximumPropertiesToStringify = Math.min(keyLength, maximumBreadth);
565
+ if (deterministic && !isTypedArrayWithEntries(value)) {
566
+ keys = sort(keys, comparator);
567
+ }
568
+ stack.push(value);
569
+ for (let i = 0; i < maximumPropertiesToStringify; i++) {
570
+ const key2 = keys[i];
571
+ const tmp = stringifyFnReplacer(key2, value, stack, replacer, spacer, indentation);
572
+ if (tmp !== void 0) {
573
+ res += `${separator}${strEscape(key2)}:${whitespace}${tmp}`;
574
+ separator = join;
575
+ }
576
+ }
577
+ if (keyLength > maximumBreadth) {
578
+ const removedKeys = keyLength - maximumBreadth;
579
+ res += `${separator}"...":${whitespace}"${getItemCount(removedKeys)} not stringified"`;
580
+ separator = join;
581
+ }
582
+ if (spacer !== "" && separator.length > 1) {
583
+ res = `
584
+ ${indentation}${res}
585
+ ${originalIndentation}`;
586
+ }
587
+ stack.pop();
588
+ return `{${res}}`;
589
+ }
590
+ case "number":
591
+ return isFinite(value) ? String(value) : fail ? fail(value) : "null";
592
+ case "boolean":
593
+ return value === true ? "true" : "false";
594
+ case "undefined":
595
+ return void 0;
596
+ case "bigint":
597
+ if (bigint) {
598
+ return String(value);
599
+ }
600
+ // fallthrough
601
+ default:
602
+ return fail ? fail(value) : void 0;
603
+ }
604
+ }
605
+ function stringifyArrayReplacer(key, value, stack, replacer, spacer, indentation) {
606
+ if (typeof value === "object" && value !== null && typeof value.toJSON === "function") {
607
+ value = value.toJSON(key);
608
+ }
609
+ switch (typeof value) {
610
+ case "string":
611
+ return strEscape(value);
612
+ case "object": {
613
+ if (value === null) {
614
+ return "null";
615
+ }
616
+ if (stack.indexOf(value) !== -1) {
617
+ return circularValue;
618
+ }
619
+ const originalIndentation = indentation;
620
+ let res = "";
621
+ let join = ",";
622
+ if (Array.isArray(value)) {
623
+ if (value.length === 0) {
624
+ return "[]";
625
+ }
626
+ if (maximumDepth < stack.length + 1) {
627
+ return '"[Array]"';
628
+ }
629
+ stack.push(value);
630
+ if (spacer !== "") {
631
+ indentation += spacer;
632
+ res += `
633
+ ${indentation}`;
634
+ join = `,
635
+ ${indentation}`;
636
+ }
637
+ const maximumValuesToStringify = Math.min(value.length, maximumBreadth);
638
+ let i = 0;
639
+ for (; i < maximumValuesToStringify - 1; i++) {
640
+ const tmp2 = stringifyArrayReplacer(String(i), value[i], stack, replacer, spacer, indentation);
641
+ res += tmp2 !== void 0 ? tmp2 : "null";
642
+ res += join;
643
+ }
644
+ const tmp = stringifyArrayReplacer(String(i), value[i], stack, replacer, spacer, indentation);
645
+ res += tmp !== void 0 ? tmp : "null";
646
+ if (value.length - 1 > maximumBreadth) {
647
+ const removedKeys = value.length - maximumBreadth - 1;
648
+ res += `${join}"... ${getItemCount(removedKeys)} not stringified"`;
649
+ }
650
+ if (spacer !== "") {
651
+ res += `
652
+ ${originalIndentation}`;
653
+ }
654
+ stack.pop();
655
+ return `[${res}]`;
656
+ }
657
+ stack.push(value);
658
+ let whitespace = "";
659
+ if (spacer !== "") {
660
+ indentation += spacer;
661
+ join = `,
662
+ ${indentation}`;
663
+ whitespace = " ";
664
+ }
665
+ let separator = "";
666
+ for (const key2 of replacer) {
667
+ const tmp = stringifyArrayReplacer(key2, value[key2], stack, replacer, spacer, indentation);
668
+ if (tmp !== void 0) {
669
+ res += `${separator}${strEscape(key2)}:${whitespace}${tmp}`;
670
+ separator = join;
671
+ }
672
+ }
673
+ if (spacer !== "" && separator.length > 1) {
674
+ res = `
675
+ ${indentation}${res}
676
+ ${originalIndentation}`;
677
+ }
678
+ stack.pop();
679
+ return `{${res}}`;
680
+ }
681
+ case "number":
682
+ return isFinite(value) ? String(value) : fail ? fail(value) : "null";
683
+ case "boolean":
684
+ return value === true ? "true" : "false";
685
+ case "undefined":
686
+ return void 0;
687
+ case "bigint":
688
+ if (bigint) {
689
+ return String(value);
690
+ }
691
+ // fallthrough
692
+ default:
693
+ return fail ? fail(value) : void 0;
694
+ }
695
+ }
696
+ function stringifyIndent(key, value, stack, spacer, indentation) {
697
+ switch (typeof value) {
698
+ case "string":
699
+ return strEscape(value);
700
+ case "object": {
701
+ if (value === null) {
702
+ return "null";
703
+ }
704
+ if (typeof value.toJSON === "function") {
705
+ value = value.toJSON(key);
706
+ if (typeof value !== "object") {
707
+ return stringifyIndent(key, value, stack, spacer, indentation);
708
+ }
709
+ if (value === null) {
710
+ return "null";
711
+ }
712
+ }
713
+ if (stack.indexOf(value) !== -1) {
714
+ return circularValue;
715
+ }
716
+ const originalIndentation = indentation;
717
+ if (Array.isArray(value)) {
718
+ if (value.length === 0) {
719
+ return "[]";
720
+ }
721
+ if (maximumDepth < stack.length + 1) {
722
+ return '"[Array]"';
723
+ }
724
+ stack.push(value);
725
+ indentation += spacer;
726
+ let res2 = `
727
+ ${indentation}`;
728
+ const join2 = `,
729
+ ${indentation}`;
730
+ const maximumValuesToStringify = Math.min(value.length, maximumBreadth);
731
+ let i = 0;
732
+ for (; i < maximumValuesToStringify - 1; i++) {
733
+ const tmp2 = stringifyIndent(String(i), value[i], stack, spacer, indentation);
734
+ res2 += tmp2 !== void 0 ? tmp2 : "null";
735
+ res2 += join2;
736
+ }
737
+ const tmp = stringifyIndent(String(i), value[i], stack, spacer, indentation);
738
+ res2 += tmp !== void 0 ? tmp : "null";
739
+ if (value.length - 1 > maximumBreadth) {
740
+ const removedKeys = value.length - maximumBreadth - 1;
741
+ res2 += `${join2}"... ${getItemCount(removedKeys)} not stringified"`;
742
+ }
743
+ res2 += `
744
+ ${originalIndentation}`;
745
+ stack.pop();
746
+ return `[${res2}]`;
747
+ }
748
+ let keys = Object.keys(value);
749
+ const keyLength = keys.length;
750
+ if (keyLength === 0) {
751
+ return "{}";
752
+ }
753
+ if (maximumDepth < stack.length + 1) {
754
+ return '"[Object]"';
755
+ }
756
+ indentation += spacer;
757
+ const join = `,
758
+ ${indentation}`;
759
+ let res = "";
760
+ let separator = "";
761
+ let maximumPropertiesToStringify = Math.min(keyLength, maximumBreadth);
762
+ if (isTypedArrayWithEntries(value)) {
763
+ res += stringifyTypedArray(value, join, maximumBreadth);
764
+ keys = keys.slice(value.length);
765
+ maximumPropertiesToStringify -= value.length;
766
+ separator = join;
767
+ }
768
+ if (deterministic) {
769
+ keys = sort(keys, comparator);
770
+ }
771
+ stack.push(value);
772
+ for (let i = 0; i < maximumPropertiesToStringify; i++) {
773
+ const key2 = keys[i];
774
+ const tmp = stringifyIndent(key2, value[key2], stack, spacer, indentation);
775
+ if (tmp !== void 0) {
776
+ res += `${separator}${strEscape(key2)}: ${tmp}`;
777
+ separator = join;
778
+ }
779
+ }
780
+ if (keyLength > maximumBreadth) {
781
+ const removedKeys = keyLength - maximumBreadth;
782
+ res += `${separator}"...": "${getItemCount(removedKeys)} not stringified"`;
783
+ separator = join;
784
+ }
785
+ if (separator !== "") {
786
+ res = `
787
+ ${indentation}${res}
788
+ ${originalIndentation}`;
789
+ }
790
+ stack.pop();
791
+ return `{${res}}`;
792
+ }
793
+ case "number":
794
+ return isFinite(value) ? String(value) : fail ? fail(value) : "null";
795
+ case "boolean":
796
+ return value === true ? "true" : "false";
797
+ case "undefined":
798
+ return void 0;
799
+ case "bigint":
800
+ if (bigint) {
801
+ return String(value);
802
+ }
803
+ // fallthrough
804
+ default:
805
+ return fail ? fail(value) : void 0;
806
+ }
807
+ }
808
+ function stringifySimple(key, value, stack) {
809
+ switch (typeof value) {
810
+ case "string":
811
+ return strEscape(value);
812
+ case "object": {
813
+ if (value === null) {
814
+ return "null";
815
+ }
816
+ if (typeof value.toJSON === "function") {
817
+ value = value.toJSON(key);
818
+ if (typeof value !== "object") {
819
+ return stringifySimple(key, value, stack);
820
+ }
821
+ if (value === null) {
822
+ return "null";
823
+ }
824
+ }
825
+ if (stack.indexOf(value) !== -1) {
826
+ return circularValue;
827
+ }
828
+ let res = "";
829
+ const hasLength = value.length !== void 0;
830
+ if (hasLength && Array.isArray(value)) {
831
+ if (value.length === 0) {
832
+ return "[]";
833
+ }
834
+ if (maximumDepth < stack.length + 1) {
835
+ return '"[Array]"';
836
+ }
837
+ stack.push(value);
838
+ const maximumValuesToStringify = Math.min(value.length, maximumBreadth);
839
+ let i = 0;
840
+ for (; i < maximumValuesToStringify - 1; i++) {
841
+ const tmp2 = stringifySimple(String(i), value[i], stack);
842
+ res += tmp2 !== void 0 ? tmp2 : "null";
843
+ res += ",";
844
+ }
845
+ const tmp = stringifySimple(String(i), value[i], stack);
846
+ res += tmp !== void 0 ? tmp : "null";
847
+ if (value.length - 1 > maximumBreadth) {
848
+ const removedKeys = value.length - maximumBreadth - 1;
849
+ res += `,"... ${getItemCount(removedKeys)} not stringified"`;
850
+ }
851
+ stack.pop();
852
+ return `[${res}]`;
853
+ }
854
+ let keys = Object.keys(value);
855
+ const keyLength = keys.length;
856
+ if (keyLength === 0) {
857
+ return "{}";
858
+ }
859
+ if (maximumDepth < stack.length + 1) {
860
+ return '"[Object]"';
861
+ }
862
+ let separator = "";
863
+ let maximumPropertiesToStringify = Math.min(keyLength, maximumBreadth);
864
+ if (hasLength && isTypedArrayWithEntries(value)) {
865
+ res += stringifyTypedArray(value, ",", maximumBreadth);
866
+ keys = keys.slice(value.length);
867
+ maximumPropertiesToStringify -= value.length;
868
+ separator = ",";
869
+ }
870
+ if (deterministic) {
871
+ keys = sort(keys, comparator);
872
+ }
873
+ stack.push(value);
874
+ for (let i = 0; i < maximumPropertiesToStringify; i++) {
875
+ const key2 = keys[i];
876
+ const tmp = stringifySimple(key2, value[key2], stack);
877
+ if (tmp !== void 0) {
878
+ res += `${separator}${strEscape(key2)}:${tmp}`;
879
+ separator = ",";
880
+ }
881
+ }
882
+ if (keyLength > maximumBreadth) {
883
+ const removedKeys = keyLength - maximumBreadth;
884
+ res += `${separator}"...":"${getItemCount(removedKeys)} not stringified"`;
885
+ }
886
+ stack.pop();
887
+ return `{${res}}`;
888
+ }
889
+ case "number":
890
+ return isFinite(value) ? String(value) : fail ? fail(value) : "null";
891
+ case "boolean":
892
+ return value === true ? "true" : "false";
893
+ case "undefined":
894
+ return void 0;
895
+ case "bigint":
896
+ if (bigint) {
897
+ return String(value);
898
+ }
899
+ // fallthrough
900
+ default:
901
+ return fail ? fail(value) : void 0;
902
+ }
903
+ }
904
+ function stringify2(value, replacer, space) {
905
+ if (arguments.length > 1) {
906
+ let spacer = "";
907
+ if (typeof space === "number") {
908
+ spacer = " ".repeat(Math.min(space, 10));
909
+ } else if (typeof space === "string") {
910
+ spacer = space.slice(0, 10);
911
+ }
912
+ if (replacer != null) {
913
+ if (typeof replacer === "function") {
914
+ return stringifyFnReplacer("", { "": value }, [], replacer, spacer, "");
915
+ }
916
+ if (Array.isArray(replacer)) {
917
+ return stringifyArrayReplacer("", value, [], getUniqueReplacerSet(replacer), spacer, "");
918
+ }
919
+ }
920
+ if (spacer.length !== 0) {
921
+ return stringifyIndent("", value, [], spacer, "");
922
+ }
923
+ }
924
+ return stringifySimple("", value, []);
925
+ }
926
+ return stringify2;
927
+ }
928
+ } (safeStableStringify, safeStableStringify.exports));
929
+ return safeStableStringify.exports;
930
+ }
931
+
932
+ var safeStableStringifyExports = requireSafeStableStringify();
933
+ const cjsModule = /*@__PURE__*/getDefaultExportFromCjs(safeStableStringifyExports);
934
+
935
+ const configure = cjsModule.configure;
936
+
937
+ const writeConsoleLogBasedOnLevel = (level) => {
938
+ const c = console;
939
+ if (level === "error") {
940
+ return c.__error ?? console.error;
941
+ }
942
+ if (level === "warn") {
943
+ return c.__warn ?? console.warn;
944
+ }
945
+ if (level === "trace") {
946
+ return c.__trace ?? console.trace;
947
+ }
948
+ return c.__log ?? console.log;
949
+ };
950
+
951
+ let RawReporter$1 = class RawReporter {
952
+ // eslint-disable-next-line class-methods-use-this
953
+ log(meta) {
954
+ const { context = [], message, type } = meta;
955
+ const consoleLogFunction = writeConsoleLogBasedOnLevel(type.level);
956
+ consoleLogFunction(message, ...context);
957
+ }
958
+ };
959
+
960
+ const arrayify = (x) => {
961
+ if (x === void 0) {
962
+ return [];
963
+ }
964
+ return Array.isArray(x) ? x : [x];
965
+ };
966
+
967
+ const mergeTypes = (standard, custom) => {
968
+ const types = { ...standard };
969
+ Object.keys(custom).forEach((type) => {
970
+ types[type] = { ...types[type], ...custom[type] };
971
+ });
972
+ return types;
973
+ };
974
+
975
+ const preventLoop = (function_) => {
976
+ let doing = false;
977
+ return function(...args) {
978
+ if (doing) {
979
+ return void 0;
980
+ }
981
+ doing = true;
982
+ try {
983
+ const result = function_.apply(this, args);
984
+ doing = false;
985
+ return result;
986
+ } catch (error) {
987
+ doing = false;
988
+ throw error;
989
+ }
990
+ };
991
+ };
992
+ class PailBrowserImpl {
993
+ timersMap;
994
+ countMap;
995
+ seqTimers;
996
+ lastLog;
997
+ logLevels;
998
+ disabled;
999
+ paused;
1000
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1001
+ messageQueue;
1002
+ scopeName;
1003
+ types;
1004
+ longestLabel;
1005
+ processors;
1006
+ generalLogLevel;
1007
+ reporters;
1008
+ throttle;
1009
+ throttleMin;
1010
+ stringify;
1011
+ groups;
1012
+ startTimerMessage;
1013
+ endTimerMessage;
1014
+ rawReporter;
1015
+ force = {};
1016
+ /**
1017
+ * Creates a new Pail browser logger instance.
1018
+ *
1019
+ * Initializes the logger with the provided configuration options,
1020
+ * setting up reporters, processors, log levels, and other internal state.
1021
+ * @param options Configuration options for the logger
1022
+ */
1023
+ constructor(options) {
1024
+ this.throttle = options.throttle ?? 1e3;
1025
+ this.throttleMin = options.throttleMin ?? 5;
1026
+ const parentLongestLabel = options.parentLongestLabel;
1027
+ const parentTypes = options.parentTypes;
1028
+ const parentStringify = options.parentStringify;
1029
+ const parentLogLevels = options.parentLogLevels;
1030
+ this.stringify = parentStringify ?? configure({
1031
+ strict: true
1032
+ });
1033
+ this.startTimerMessage = options.messages?.timerStart ?? "Initialized timer...";
1034
+ this.endTimerMessage = options.messages?.timerEnd ?? "Timer run for:";
1035
+ if (parentTypes && parentLongestLabel) {
1036
+ this.types = parentTypes;
1037
+ this.longestLabel = parentLongestLabel;
1038
+ } else {
1039
+ this.types = mergeTypes(LOG_TYPES, options.types ?? {});
1040
+ this.longestLabel = getLongestLabel(this.types);
1041
+ }
1042
+ this.logLevels = parentLogLevels && !options.logLevels ? parentLogLevels : { ...EXTENDED_RFC_5424_LOG_LEVELS, ...options.logLevels };
1043
+ this.generalLogLevel = this.#normalizeLogLevel(options.logLevel);
1044
+ this.reporters = /* @__PURE__ */ new Set();
1045
+ this.processors = /* @__PURE__ */ new Set();
1046
+ this.disabled = options.disabled ?? false;
1047
+ this.paused = false;
1048
+ this.messageQueue = [];
1049
+ this.scopeName = arrayify(options.scope).filter(Boolean);
1050
+ this.timersMap = /* @__PURE__ */ new Map();
1051
+ this.countMap = /* @__PURE__ */ new Map();
1052
+ this.groups = [];
1053
+ this.seqTimers = /* @__PURE__ */ new Set();
1054
+ this.lastLog = {};
1055
+ this.logger = preventLoop(this.logger.bind(this));
1056
+ this.#initializeBoundMethods();
1057
+ if (Array.isArray(options.reporters)) {
1058
+ this.registerReporters(options.reporters);
1059
+ }
1060
+ this.rawReporter = this.extendReporter(options.rawReporter ?? new RawReporter$1());
1061
+ if (Array.isArray(options.processors)) {
1062
+ this.registerProcessors(options.processors);
1063
+ }
1064
+ }
1065
+ /**
1066
+ * Initializes bound methods for all logger types.
1067
+ *
1068
+ * Creates bound methods for both regular and force logging methods.
1069
+ * This is separated to allow reuse when types haven't changed.
1070
+ */
1071
+ #initializeBoundMethods() {
1072
+ for (const type in this.types) {
1073
+ this[type] = this.logger.bind(this, type, false, false);
1074
+ }
1075
+ for (const type in this.types) {
1076
+ this.force[type] = this.logger.bind(this, type, false, true);
1077
+ }
1078
+ }
1079
+ /**
1080
+ * Wraps the global console methods to redirect them through the logger.
1081
+ *
1082
+ * This method replaces console methods (log, info, warn, error, etc.) with
1083
+ * calls to the corresponding logger methods. The original console methods
1084
+ * are backed up and can be restored using restoreConsole().
1085
+ * @example
1086
+ * ```typescript
1087
+ * const logger = createPail();
1088
+ * logger.wrapConsole();
1089
+ *
1090
+ * console.log("This will go through the logger");
1091
+ * console.error("This too!");
1092
+ *
1093
+ * logger.restoreConsole(); // Restore original console methods
1094
+ * ```
1095
+ */
1096
+ wrapConsole() {
1097
+ for (const type in this.types) {
1098
+ if (!console[`__${type}`]) {
1099
+ console[`__${type}`] = console[type];
1100
+ }
1101
+ console[type] = this[type];
1102
+ }
1103
+ }
1104
+ /**
1105
+ * Restores the original global console methods.
1106
+ *
1107
+ * This method restores the console methods that were backed up by wrapConsole().
1108
+ * After calling this, console methods will work as they did before wrapping.
1109
+ * @example
1110
+ * ```typescript
1111
+ * const logger = createPail();
1112
+ * logger.wrapConsole();
1113
+ *
1114
+ * // Console methods are now wrapped
1115
+ * logger.restoreConsole();
1116
+ * // Console methods are restored to original behavior
1117
+ * ```
1118
+ */
1119
+ restoreConsole() {
1120
+ for (const type in this.types) {
1121
+ if (console[`__${type}`]) {
1122
+ console[type] = console[`__${type}`];
1123
+ delete console[`__${type}`];
1124
+ }
1125
+ }
1126
+ }
1127
+ /**
1128
+ * Wraps uncaught exception and unhandled rejection handlers.
1129
+ *
1130
+ * This method sets up global error handlers that will log uncaught exceptions
1131
+ * and unhandled promise rejections through the logger. This is useful for
1132
+ * capturing and logging application crashes.
1133
+ * @example
1134
+ * ```typescript
1135
+ * const logger = createPail();
1136
+ * logger.wrapException();
1137
+ *
1138
+ * // Now uncaught errors will be logged
1139
+ * throw new Error("This will be logged");
1140
+ * ```
1141
+ */
1142
+ wrapException() {
1143
+ {
1144
+ process.on("uncaughtException", (error) => {
1145
+ this.error(error);
1146
+ });
1147
+ process.on("unhandledRejection", (error) => {
1148
+ this.error(error);
1149
+ });
1150
+ }
1151
+ }
1152
+ /**
1153
+ * Disables all logging output.
1154
+ *
1155
+ * When disabled, all log calls will be silently ignored and no output
1156
+ * will be produced by any reporters. This can be useful for temporarily
1157
+ * suppressing log output in production or during testing.
1158
+ * @example
1159
+ * ```typescript
1160
+ * const logger = createPail();
1161
+ * logger.disable();
1162
+ * logger.info("This won't be logged"); // Silent
1163
+ * logger.enable();
1164
+ * logger.info("This will be logged"); // Output produced
1165
+ * ```
1166
+ */
1167
+ disable() {
1168
+ this.disabled = true;
1169
+ }
1170
+ /**
1171
+ * Enables logging output.
1172
+ *
1173
+ * Re-enables logging after it has been disabled. All subsequent log calls
1174
+ * will produce output according to the configured reporters.
1175
+ * @example
1176
+ * ```typescript
1177
+ * const logger = createPail();
1178
+ * logger.disable();
1179
+ * logger.info("This won't be logged");
1180
+ * logger.enable(); // Re-enable logging
1181
+ * logger.info("This will be logged");
1182
+ * ```
1183
+ */
1184
+ enable() {
1185
+ this.disabled = false;
1186
+ }
1187
+ /**
1188
+ * Checks if logging is currently enabled.
1189
+ *
1190
+ * Returns true if logging is enabled and false if it has been disabled.
1191
+ * @returns True if logging is enabled, false if disabled
1192
+ * @example
1193
+ * ```typescript
1194
+ * const logger = createPail();
1195
+ * console.log(logger.isEnabled()); // true
1196
+ * logger.disable();
1197
+ * console.log(logger.isEnabled()); // false
1198
+ * ```
1199
+ */
1200
+ isEnabled() {
1201
+ return !this.disabled;
1202
+ }
1203
+ /**
1204
+ * Pauses logging and starts queuing messages.
1205
+ *
1206
+ * When paused, all log calls will be queued instead of being output immediately.
1207
+ * The queued messages will be processed when resume() is called. This is useful
1208
+ * for temporarily buffering log output during critical operations.
1209
+ * @example
1210
+ * ```typescript
1211
+ * const logger = createPail();
1212
+ * logger.pause();
1213
+ * logger.info("This will be queued"); // Queued, not output yet
1214
+ * logger.warn("This too"); // Also queued
1215
+ * logger.resume(); // Now both messages are output
1216
+ * ```
1217
+ */
1218
+ pause() {
1219
+ this.paused = true;
1220
+ }
1221
+ /**
1222
+ * Resumes logging and flushes all queued messages.
1223
+ *
1224
+ * Processes all messages that were queued during the pause period and
1225
+ * resumes normal logging behavior. Messages are output in the order
1226
+ * they were originally called.
1227
+ * @example
1228
+ * ```typescript
1229
+ * const logger = createPail();
1230
+ * logger.pause();
1231
+ * logger.info("Message 1"); // Queued
1232
+ * logger.info("Message 2"); // Queued
1233
+ * logger.resume(); // Both messages are now output in order
1234
+ * logger.info("Message 3"); // Output immediately
1235
+ * ```
1236
+ */
1237
+ resume() {
1238
+ this.paused = false;
1239
+ const queue = this.messageQueue.splice(0);
1240
+ for (let i = 0; i < queue.length; i += 1) {
1241
+ const { messageObject, raw, type } = queue[i];
1242
+ this.logger(type, raw, false, ...messageObject);
1243
+ }
1244
+ }
1245
+ /**
1246
+ * Creates a scoped logger instance.
1247
+ *
1248
+ * Returns a new logger instance that inherits all configuration but adds
1249
+ * the specified scope names to all log messages. This is useful for
1250
+ * categorizing logs by component, module, or feature.
1251
+ * @template N - The new custom logger type names
1252
+ * @param name Scope names to apply to all log messages
1253
+ * @returns A new scoped logger instance
1254
+ * @throws {Error} If no scope name is provided
1255
+ * @example
1256
+ * ```typescript
1257
+ * const logger = createPail();
1258
+ * const scopedLogger = logger.scope("auth", "login");
1259
+ * scopedLogger.info("User logged in"); // Will include scope: ["auth", "login"]
1260
+ * ```
1261
+ */
1262
+ scope(...name) {
1263
+ if (name.length === 0) {
1264
+ throw new Error("No scope name was defined.");
1265
+ }
1266
+ this.scopeName = name.flat();
1267
+ return this;
1268
+ }
1269
+ /**
1270
+ * Removes the current scope from the logger.
1271
+ *
1272
+ * Clears all scope names that were set by previous scope() calls.
1273
+ * After calling this, log messages will no longer include scope information.
1274
+ * @example
1275
+ * ```typescript
1276
+ * const logger = createPail();
1277
+ * const scopedLogger = logger.scope("auth");
1278
+ * scopedLogger.info("Scoped message"); // Has scope
1279
+ * scopedLogger.unscope();
1280
+ * scopedLogger.info("Unscoped message"); // No scope
1281
+ * ```
1282
+ */
1283
+ unscope() {
1284
+ this.scopeName = [];
1285
+ }
1286
+ /**
1287
+ * Creates a child logger that inherits settings from the parent.
1288
+ *
1289
+ * Returns a new logger instance that inherits all configuration from the parent
1290
+ * (reporters, processors, types, log levels, throttle settings, etc.) while allowing
1291
+ * you to override only what you need. Child loggers are independent instances with
1292
+ * their own state (timers, counters, etc.).
1293
+ * @template N - The new custom logger type names
1294
+ * @template LC - The new log level types
1295
+ * @param options Configuration options to override or extend parent settings
1296
+ * @returns A new child logger instance
1297
+ * @example
1298
+ * ```typescript
1299
+ * const parent = createPail({
1300
+ * logLevel: "info",
1301
+ * types: { http: { label: "HTTP", logLevel: "info" } },
1302
+ * reporters: [new PrettyReporter()],
1303
+ * });
1304
+ *
1305
+ * // Child inherits parent settings but overrides log level
1306
+ * const child = parent.child({ logLevel: "debug" });
1307
+ * child.info("This will be logged"); // Uses debug level from child
1308
+ * child.http("GET /api 200"); // Inherits http type from parent
1309
+ *
1310
+ * // Child can add new types
1311
+ * const childWithNewType = parent.child({
1312
+ * types: { db: { label: "DB", logLevel: "info" } },
1313
+ * });
1314
+ * childWithNewType.db("Query executed"); // New type available
1315
+ * ```
1316
+ */
1317
+ child(options) {
1318
+ const typesChanged = options?.types !== void 0;
1319
+ const mergedTypes = typesChanged ? mergeTypes(this.types, options.types) : this.types;
1320
+ const childReporters = options?.reporters ?? [];
1321
+ const allReporters = childReporters.length > 0 ? [...this.reporters, ...childReporters] : [...this.reporters];
1322
+ const childProcessors = options?.processors ?? [];
1323
+ const allProcessors = childProcessors.length > 0 ? [...this.processors, ...childProcessors] : [...this.processors];
1324
+ const mergedLogLevels = options?.logLevels ? { ...this.logLevels, ...options.logLevels } : this.logLevels;
1325
+ let mergedScope;
1326
+ if (options?.scope) {
1327
+ const childScope = arrayify(options.scope).filter(Boolean);
1328
+ mergedScope = this.scopeName.length > 0 ? [...this.scopeName, ...childScope] : childScope;
1329
+ } else {
1330
+ mergedScope = this.scopeName.length > 0 ? this.scopeName : [];
1331
+ }
1332
+ const mergedMessages = options?.messages ? {
1333
+ timerEnd: this.endTimerMessage,
1334
+ timerStart: this.startTimerMessage,
1335
+ ...options.messages
1336
+ } : {
1337
+ timerEnd: this.endTimerMessage,
1338
+ timerStart: this.startTimerMessage
1339
+ };
1340
+ const childOptions = {
1341
+ disabled: options?.disabled ?? this.disabled,
1342
+ logLevel: options?.logLevel ?? this.generalLogLevel,
1343
+ logLevels: mergedLogLevels,
1344
+ messages: mergedMessages,
1345
+ processors: allProcessors,
1346
+ rawReporter: options?.rawReporter ?? this.rawReporter,
1347
+ reporters: allReporters,
1348
+ scope: mergedScope,
1349
+ throttle: options?.throttle ?? this.throttle,
1350
+ throttleMin: options?.throttleMin ?? this.throttleMin
1351
+ };
1352
+ if (typesChanged) {
1353
+ childOptions.parentTypes = mergedTypes;
1354
+ childOptions.parentLongestLabel = getLongestLabel(mergedTypes);
1355
+ } else {
1356
+ childOptions.parentTypes = this.types;
1357
+ childOptions.parentLongestLabel = this.longestLabel;
1358
+ }
1359
+ if (!options?.logLevels) {
1360
+ childOptions.parentLogLevels = this.logLevels;
1361
+ }
1362
+ if (!options?.messages) {
1363
+ childOptions.parentMessages = {
1364
+ timerEnd: this.endTimerMessage,
1365
+ timerStart: this.startTimerMessage
1366
+ };
1367
+ }
1368
+ childOptions.parentStringify = this.stringify;
1369
+ return new PailBrowserImpl(childOptions);
1370
+ }
1371
+ /**
1372
+ * Starts a timer with the specified label.
1373
+ *
1374
+ * Records the current timestamp and associates it with the given label.
1375
+ * Multiple timers can be active simultaneously with different labels.
1376
+ * @param label The timer label (defaults to "default")
1377
+ * @example
1378
+ * ```typescript
1379
+ * const logger = createPail();
1380
+ * logger.time("operation");
1381
+ * // ... some operation ...
1382
+ * logger.timeEnd("operation"); // Logs: "Timer run for: X ms"
1383
+ * ```
1384
+ */
1385
+ time(label = "default") {
1386
+ if (this.seqTimers.has(label)) {
1387
+ this.logger("warn", false, false, {
1388
+ message: `Timer '${label}' already exists`,
1389
+ prefix: label
1390
+ });
1391
+ } else {
1392
+ this.seqTimers.add(label);
1393
+ this.timersMap.set(label, Date.now());
1394
+ this.logger("start", false, false, {
1395
+ message: this.startTimerMessage,
1396
+ prefix: label
1397
+ });
1398
+ }
1399
+ }
1400
+ /**
1401
+ * Logs the current elapsed time for a timer without stopping it.
1402
+ *
1403
+ * Calculates and logs the time elapsed since the timer was started,
1404
+ * but keeps the timer running. If no label is provided, uses the
1405
+ * most recently started timer.
1406
+ * @param label The timer label (uses last timer if not specified)
1407
+ * @param data Additional data to include in the log message
1408
+ * @example
1409
+ * ```typescript
1410
+ * const logger = createPail();
1411
+ * logger.time("task");
1412
+ * // ... some work ...
1413
+ * logger.timeLog("task"); // Logs current elapsed time
1414
+ * // ... more work ...
1415
+ * logger.timeEnd("task"); // Logs final time and stops timer
1416
+ * ```
1417
+ */
1418
+ timeLog(label, ...data) {
1419
+ if (!label && this.seqTimers.size > 0) {
1420
+ label = [...this.seqTimers].pop();
1421
+ }
1422
+ if (label && this.timersMap.has(label)) {
1423
+ const span = Date.now() - this.timersMap.get(label);
1424
+ this.logger("info", false, false, {
1425
+ context: data,
1426
+ message: span < 1e3 ? `${String(span)} ms` : `${(span / 1e3).toFixed(2)} s`,
1427
+ prefix: label
1428
+ });
1429
+ } else {
1430
+ this.logger("warn", false, false, {
1431
+ context: data,
1432
+ message: "Timer not found",
1433
+ prefix: label
1434
+ });
1435
+ }
1436
+ }
1437
+ /**
1438
+ * Stops a timer and logs the final elapsed time.
1439
+ *
1440
+ * Calculates the total time elapsed since the timer was started,
1441
+ * logs the result, and removes the timer. If no label is provided,
1442
+ * uses the most recently started timer.
1443
+ * @param label The timer label (uses last timer if not specified)
1444
+ * @example
1445
+ * ```typescript
1446
+ * const logger = createPail();
1447
+ * logger.time("operation");
1448
+ * // ... perform operation ...
1449
+ * logger.timeEnd("operation"); // Logs: "Timer run for: X ms"
1450
+ * ```
1451
+ */
1452
+ timeEnd(label) {
1453
+ if (!label && this.seqTimers.size > 0) {
1454
+ label = [...this.seqTimers].pop();
1455
+ }
1456
+ if (label && this.timersMap.has(label)) {
1457
+ const span = Date.now() - this.timersMap.get(label);
1458
+ this.timersMap.delete(label);
1459
+ this.logger("stop", false, false, {
1460
+ message: `${this.endTimerMessage} ${span < 1e3 ? `${String(span)} ms` : `${(span / 1e3).toFixed(2)} s`}`,
1461
+ prefix: label
1462
+ });
1463
+ } else {
1464
+ this.logger("warn", false, false, {
1465
+ message: "Timer not found",
1466
+ prefix: label
1467
+ });
1468
+ }
1469
+ }
1470
+ /**
1471
+ * Starts a log group with the specified label.
1472
+ *
1473
+ * Groups related log messages together. In browser environments,
1474
+ * this uses the native console.group() functionality. In other
1475
+ * environments, it tracks group nesting internally.
1476
+ * @param label The group label (defaults to "console.group")
1477
+ * @example
1478
+ * ```typescript
1479
+ * const logger = createPail();
1480
+ * logger.group("Database Operations");
1481
+ * logger.info("Connecting to database");
1482
+ * logger.info("Running migration");
1483
+ * logger.groupEnd(); // End the group
1484
+ * ```
1485
+ */
1486
+ group(label = "console.group") {
1487
+ if (globalThis.window === void 0) {
1488
+ this.groups.push(label);
1489
+ } else {
1490
+ console.group(label);
1491
+ }
1492
+ }
1493
+ /**
1494
+ * Ends the current log group.
1495
+ *
1496
+ * Closes the most recently opened log group. In browser environments,
1497
+ * this uses the native console.groupEnd() functionality.
1498
+ * @example
1499
+ * ```typescript
1500
+ * const logger = createPail();
1501
+ * logger.group("Processing");
1502
+ * logger.info("Step 1");
1503
+ * logger.info("Step 2");
1504
+ * logger.groupEnd(); // Closes the "Processing" group
1505
+ * ```
1506
+ */
1507
+ groupEnd() {
1508
+ if (globalThis.window === void 0) {
1509
+ this.groups.pop();
1510
+ } else {
1511
+ console.groupEnd();
1512
+ }
1513
+ }
1514
+ /**
1515
+ * Increments and logs a counter with the specified label.
1516
+ *
1517
+ * Maintains an internal counter for each label and logs the current count
1518
+ * each time it's called. Useful for tracking how many times certain
1519
+ * code paths are executed.
1520
+ * @param label The counter label (defaults to "default")
1521
+ * @example
1522
+ * ```typescript
1523
+ * const logger = createPail();
1524
+ * logger.count("requests"); // Logs: "requests: 1"
1525
+ * logger.count("requests"); // Logs: "requests: 2"
1526
+ * logger.count("errors"); // Logs: "errors: 1"
1527
+ * ```
1528
+ */
1529
+ count(label = "default") {
1530
+ const current = this.countMap.get(label) ?? 0;
1531
+ this.countMap.set(label, current + 1);
1532
+ this.logger("log", false, false, {
1533
+ message: `${label}: ${String(current + 1)}`,
1534
+ prefix: label
1535
+ });
1536
+ }
1537
+ /**
1538
+ * Resets a counter to zero.
1539
+ *
1540
+ * Removes the counter with the specified label, effectively resetting
1541
+ * it to zero. If the counter doesn't exist, logs a warning.
1542
+ * @param label The counter label to reset (defaults to "default")
1543
+ * @example
1544
+ * ```typescript
1545
+ * const logger = createPail();
1546
+ * logger.count("requests"); // Logs: "requests: 1"
1547
+ * logger.countReset("requests"); // Resets counter
1548
+ * logger.count("requests"); // Logs: "requests: 1" (starts over)
1549
+ * ```
1550
+ */
1551
+ countReset(label = "default") {
1552
+ if (this.countMap.has(label)) {
1553
+ this.countMap.delete(label);
1554
+ } else {
1555
+ this.logger("warn", false, false, {
1556
+ message: `Count for ${label} does not exist`,
1557
+ prefix: label
1558
+ });
1559
+ }
1560
+ }
1561
+ /**
1562
+ * Clears the console output.
1563
+ *
1564
+ * Calls the native console.clear() method to clear all output from
1565
+ * the console. This is a convenience method that wraps the native
1566
+ * console.clear() functionality.
1567
+ * @example
1568
+ * ```typescript
1569
+ * const logger = createPail();
1570
+ * logger.info("Some message");
1571
+ * logger.clear(); // Clears the console
1572
+ * ```
1573
+ */
1574
+ // eslint-disable-next-line class-methods-use-this
1575
+ clear() {
1576
+ console.clear();
1577
+ }
1578
+ /**
1579
+ * Logs a raw message bypassing normal processing.
1580
+ *
1581
+ * Sends a message directly to the raw reporter without going through
1582
+ * the normal logging pipeline (processors, throttling, etc.). This is
1583
+ * useful for logging that needs to bypass all formatting and processing.
1584
+ * @param message The raw message to log
1585
+ * @param arguments_ Additional arguments to include
1586
+ * @example
1587
+ * ```typescript
1588
+ * const logger = createPail();
1589
+ * logger.raw("Direct message", { data: "value" });
1590
+ * ```
1591
+ */
1592
+ raw(message, ...arguments_) {
1593
+ if (this.disabled) {
1594
+ return;
1595
+ }
1596
+ this.logger("log", true, false, {
1597
+ context: arguments_,
1598
+ message
1599
+ });
1600
+ }
1601
+ extendReporter(reporter) {
1602
+ if (typeof reporter.setLoggerTypes === "function") {
1603
+ reporter.setLoggerTypes(this.types);
1604
+ }
1605
+ if (typeof reporter.setStringify === "function") {
1606
+ reporter.setStringify(this.stringify);
1607
+ }
1608
+ return reporter;
1609
+ }
1610
+ registerReporters(reporters) {
1611
+ for (let i = 0; i < reporters.length; i += 1) {
1612
+ this.reporters.add(this.extendReporter(reporters[i]));
1613
+ }
1614
+ }
1615
+ registerProcessors(processors) {
1616
+ for (let i = 0; i < processors.length; i += 1) {
1617
+ const processor = processors[i];
1618
+ if (typeof processor.setStringify === "function") {
1619
+ processor.setStringify(this.stringify);
1620
+ }
1621
+ this.processors.add(processor);
1622
+ }
1623
+ }
1624
+ #report(meta, raw) {
1625
+ if (raw) {
1626
+ this.rawReporter.log(Object.freeze(meta));
1627
+ } else {
1628
+ for (const reporter of this.reporters) {
1629
+ reporter.log(Object.freeze(meta));
1630
+ }
1631
+ }
1632
+ }
1633
+ #normalizeLogLevel(level) {
1634
+ return level && this.logLevels[level] ? level : "debug";
1635
+ }
1636
+ // eslint-disable-next-line sonarjs/cognitive-complexity
1637
+ #buildMeta(typeName, type, ...arguments_) {
1638
+ const meta = {
1639
+ badge: void 0,
1640
+ context: void 0,
1641
+ error: void 0,
1642
+ label: void 0,
1643
+ message: EMPTY_SYMBOL,
1644
+ prefix: void 0,
1645
+ repeated: void 0,
1646
+ scope: void 0,
1647
+ suffix: void 0
1648
+ };
1649
+ meta.type = {
1650
+ level: type.logLevel,
1651
+ name: typeName
1652
+ };
1653
+ meta.groups = this.groups;
1654
+ meta.scope = this.scopeName;
1655
+ meta.date = /* @__PURE__ */ new Date();
1656
+ if (arguments_.length > 0 && arguments_[0] instanceof Error) {
1657
+ meta.error = arguments_[0];
1658
+ if (arguments_.length > 1) {
1659
+ meta.context = arguments_.slice(1);
1660
+ }
1661
+ } else if (arguments_.length > 0 && typeof arguments_[0] === "object" && arguments_[0] !== null && "message" in arguments_[0]) {
1662
+ const { context, message, prefix, suffix } = arguments_[0];
1663
+ if (context) {
1664
+ meta.context = context;
1665
+ }
1666
+ if (prefix) {
1667
+ meta.prefix = prefix;
1668
+ }
1669
+ if (suffix) {
1670
+ meta.suffix = suffix;
1671
+ }
1672
+ meta.message = message;
1673
+ if (arguments_.length > 1) {
1674
+ const additionalContext = arguments_.slice(1);
1675
+ if (meta.context) {
1676
+ meta.context = Array.isArray(meta.context) ? [...meta.context, ...additionalContext] : [meta.context, ...additionalContext];
1677
+ } else {
1678
+ meta.context = additionalContext;
1679
+ }
1680
+ }
1681
+ } else if (arguments_.length > 1) {
1682
+ meta.message = arguments_[0];
1683
+ meta.context = arguments_.slice(1);
1684
+ } else if (arguments_.length === 1) {
1685
+ meta.message = arguments_[0];
1686
+ } else {
1687
+ meta.message = void 0;
1688
+ }
1689
+ if (type.logLevel === "trace") {
1690
+ meta.traceError = new Error("Trace");
1691
+ }
1692
+ if (type.badge) {
1693
+ meta.badge = type.badge;
1694
+ }
1695
+ if (type.label) {
1696
+ meta.label = type.label;
1697
+ }
1698
+ return meta;
1699
+ }
1700
+ // eslint-disable-next-line sonarjs/cognitive-complexity
1701
+ logger(type, raw, force, ...messageObject) {
1702
+ if (this.disabled) {
1703
+ return;
1704
+ }
1705
+ if (this.paused) {
1706
+ this.messageQueue.push({ messageObject, raw, type });
1707
+ return;
1708
+ }
1709
+ const typeConfig = this.types[type];
1710
+ const logLevel = this.#normalizeLogLevel(typeConfig.logLevel);
1711
+ if (force || this.logLevels[logLevel] >= this.logLevels[this.generalLogLevel]) {
1712
+ let meta = this.#buildMeta(type, typeConfig, ...messageObject);
1713
+ const resolveLog = (newLog = false) => {
1714
+ const repeated = (this.lastLog.count ?? 0) - this.throttleMin;
1715
+ if (this.lastLog.object && repeated > 0) {
1716
+ const lastMeta = { ...this.lastLog.object };
1717
+ if (repeated > 1) {
1718
+ lastMeta.repeated = repeated;
1719
+ }
1720
+ this.#report(lastMeta, raw);
1721
+ this.lastLog.count = 1;
1722
+ }
1723
+ if (newLog) {
1724
+ for (const processor of this.processors) {
1725
+ meta = { ...processor.process(meta) };
1726
+ }
1727
+ this.lastLog.object = meta;
1728
+ this.#report(meta, raw);
1729
+ }
1730
+ };
1731
+ clearTimeout(this.lastLog.timeout);
1732
+ const diffTime = this.lastLog.time && meta.date ? new Date(meta.date).getTime() - this.lastLog.time.getTime() : 0;
1733
+ this.lastLog.time = new Date(meta.date);
1734
+ if (diffTime < this.throttle) {
1735
+ try {
1736
+ const isSameLog = this.lastLog.object && JSON.stringify([meta.label, meta.scope, meta.type, meta.message, meta.prefix, meta.suffix, meta.context]) === JSON.stringify([
1737
+ this.lastLog.object.label,
1738
+ this.lastLog.object.scope,
1739
+ this.lastLog.object.type,
1740
+ this.lastLog.object.message,
1741
+ this.lastLog.object.prefix,
1742
+ this.lastLog.object.suffix,
1743
+ this.lastLog.object.context
1744
+ ]);
1745
+ if (isSameLog) {
1746
+ this.lastLog.count = (this.lastLog.count ?? 0) + 1;
1747
+ if (this.lastLog.count > this.throttleMin) {
1748
+ this.lastLog.timeout = setTimeout(resolveLog, this.throttle);
1749
+ return;
1750
+ }
1751
+ }
1752
+ } catch {
1753
+ }
1754
+ }
1755
+ resolveLog(true);
1756
+ }
1757
+ }
1758
+ }
24
1759
 
25
1760
  class RawReporter {
26
1761
  #stdout;
@@ -58,7 +1793,7 @@ class RawReporter {
58
1793
  if (typeof value === "object") {
59
1794
  return ` ${inspect(value, this.#inspectOptions)}`;
60
1795
  }
61
- return ` ${value}`;
1796
+ return ` ${String(value)}`;
62
1797
  })
63
1798
  );
64
1799
  }
@@ -134,6 +1869,7 @@ class PailServerImpl extends PailBrowserImpl {
134
1869
  this.registerProcessors(processors);
135
1870
  }
136
1871
  }
1872
+ options;
137
1873
  stdout;
138
1874
  stderr;
139
1875
  interactiveManager;
@@ -179,7 +1915,7 @@ class PailServerImpl extends PailBrowserImpl {
179
1915
  */
180
1916
  // @ts-expect-error - override signature differs due to server-specific options
181
1917
  child(options) {
182
- const typesChanged = options?.types !== void 0 && options.types !== null;
1918
+ const typesChanged = options?.types !== void 0;
183
1919
  const mergedTypes = typesChanged ? mergeTypes(this.types, options.types) : this.types;
184
1920
  const childReporters = options?.reporters ?? [];
185
1921
  const allReporters = childReporters.length > 0 ? [...this.reporters, ...childReporters] : [...this.reporters];
@@ -505,6 +2241,924 @@ class PailServerImpl extends PailBrowserImpl {
505
2241
  }
506
2242
  const PailServer = PailServerImpl;
507
2243
 
2244
+ const colorKeywords = /* @__PURE__ */ new Map([
2245
+ ["aliceblue", "#f0f8ff"],
2246
+ ["antiquewhite", "#faebd7"],
2247
+ ["aqua", "#00ffff"],
2248
+ ["aquamarine", "#7fffd4"],
2249
+ ["azure", "#f0ffff"],
2250
+ ["beige", "#f5f5dc"],
2251
+ ["bisque", "#ffe4c4"],
2252
+ ["black", "#000000"],
2253
+ ["blanchedalmond", "#ffebcd"],
2254
+ ["blue", "#0000ff"],
2255
+ ["blueviolet", "#8a2be2"],
2256
+ ["brown", "#a52a2a"],
2257
+ ["burlywood", "#deb887"],
2258
+ ["cadetblue", "#5f9ea0"],
2259
+ ["chartreuse", "#7fff00"],
2260
+ ["chocolate", "#d2691e"],
2261
+ ["coral", "#ff7f50"],
2262
+ ["cornflowerblue", "#6495ed"],
2263
+ ["cornsilk", "#fff8dc"],
2264
+ ["crimson", "#dc143c"],
2265
+ ["cyan", "#00ffff"],
2266
+ ["darkblue", "#00008b"],
2267
+ ["darkcyan", "#008b8b"],
2268
+ ["darkgoldenrod", "#b8860b"],
2269
+ ["darkgray", "#a9a9a9"],
2270
+ ["darkgreen", "#006400"],
2271
+ ["darkgrey", "#a9a9a9"],
2272
+ ["darkkhaki", "#bdb76b"],
2273
+ ["darkmagenta", "#8b008b"],
2274
+ ["darkolivegreen", "#556b2f"],
2275
+ ["darkorange", "#ff8c00"],
2276
+ ["darkorchid", "#9932cc"],
2277
+ ["darkred", "#8b0000"],
2278
+ ["darksalmon", "#e9967a"],
2279
+ ["darkseagreen", "#8fbc8f"],
2280
+ ["darkslateblue", "#483d8b"],
2281
+ ["darkslategray", "#2f4f4f"],
2282
+ ["darkslategrey", "#2f4f4f"],
2283
+ ["darkturquoise", "#00ced1"],
2284
+ ["darkviolet", "#9400d3"],
2285
+ ["deeppink", "#ff1493"],
2286
+ ["deepskyblue", "#00bfff"],
2287
+ ["dimgray", "#696969"],
2288
+ ["dimgrey", "#696969"],
2289
+ ["dodgerblue", "#1e90ff"],
2290
+ ["firebrick", "#b22222"],
2291
+ ["floralwhite", "#fffaf0"],
2292
+ ["forestgreen", "#228b22"],
2293
+ ["fuchsia", "#ff00ff"],
2294
+ ["gainsboro", "#dcdcdc"],
2295
+ ["ghostwhite", "#f8f8ff"],
2296
+ ["gold", "#ffd700"],
2297
+ ["goldenrod", "#daa520"],
2298
+ ["gray", "#808080"],
2299
+ ["green", "#008000"],
2300
+ ["greenyellow", "#adff2f"],
2301
+ ["grey", "#808080"],
2302
+ ["honeydew", "#f0fff0"],
2303
+ ["hotpink", "#ff69b4"],
2304
+ ["indianred", "#cd5c5c"],
2305
+ ["indigo", "#4b0082"],
2306
+ ["ivory", "#fffff0"],
2307
+ ["khaki", "#f0e68c"],
2308
+ ["lavender", "#e6e6fa"],
2309
+ ["lavenderblush", "#fff0f5"],
2310
+ ["lawngreen", "#7cfc00"],
2311
+ ["lemonchiffon", "#fffacd"],
2312
+ ["lightblue", "#add8e6"],
2313
+ ["lightcoral", "#f08080"],
2314
+ ["lightcyan", "#e0ffff"],
2315
+ ["lightgoldenrodyellow", "#fafad2"],
2316
+ ["lightgray", "#d3d3d3"],
2317
+ ["lightgreen", "#90ee90"],
2318
+ ["lightgrey", "#d3d3d3"],
2319
+ ["lightpink", "#ffb6c1"],
2320
+ ["lightsalmon", "#ffa07a"],
2321
+ ["lightseagreen", "#20b2aa"],
2322
+ ["lightskyblue", "#87cefa"],
2323
+ ["lightslategray", "#778899"],
2324
+ ["lightslategrey", "#778899"],
2325
+ ["lightsteelblue", "#b0c4de"],
2326
+ ["lightyellow", "#ffffe0"],
2327
+ ["lime", "#00ff00"],
2328
+ ["limegreen", "#32cd32"],
2329
+ ["linen", "#faf0e6"],
2330
+ ["magenta", "#ff00ff"],
2331
+ ["maroon", "#800000"],
2332
+ ["mediumaquamarine", "#66cdaa"],
2333
+ ["mediumblue", "#0000cd"],
2334
+ ["mediumorchid", "#ba55d3"],
2335
+ ["mediumpurple", "#9370db"],
2336
+ ["mediumseagreen", "#3cb371"],
2337
+ ["mediumslateblue", "#7b68ee"],
2338
+ ["mediumspringgreen", "#00fa9a"],
2339
+ ["mediumturquoise", "#48d1cc"],
2340
+ ["mediumvioletred", "#c71585"],
2341
+ ["midnightblue", "#191970"],
2342
+ ["mintcream", "#f5fffa"],
2343
+ ["mistyrose", "#ffe4e1"],
2344
+ ["moccasin", "#ffe4b5"],
2345
+ ["navajowhite", "#ffdead"],
2346
+ ["navy", "#000080"],
2347
+ ["oldlace", "#fdf5e6"],
2348
+ ["olive", "#808000"],
2349
+ ["olivedrab", "#6b8e23"],
2350
+ ["orange", "#ffa500"],
2351
+ ["orangered", "#ff4500"],
2352
+ ["orchid", "#da70d6"],
2353
+ ["palegoldenrod", "#eee8aa"],
2354
+ ["palegreen", "#98fb98"],
2355
+ ["paleturquoise", "#afeeee"],
2356
+ ["palevioletred", "#db7093"],
2357
+ ["papayawhip", "#ffefd5"],
2358
+ ["peachpuff", "#ffdab9"],
2359
+ ["peru", "#cd853f"],
2360
+ ["pink", "#ffc0cb"],
2361
+ ["plum", "#dda0dd"],
2362
+ ["powderblue", "#b0e0e6"],
2363
+ ["purple", "#800080"],
2364
+ ["rebeccapurple", "#663399"],
2365
+ ["red", "#ff0000"],
2366
+ ["rosybrown", "#bc8f8f"],
2367
+ ["royalblue", "#4169e1"],
2368
+ ["saddlebrown", "#8b4513"],
2369
+ ["salmon", "#fa8072"],
2370
+ ["sandybrown", "#f4a460"],
2371
+ ["seagreen", "#2e8b57"],
2372
+ ["seashell", "#fff5ee"],
2373
+ ["sienna", "#a0522d"],
2374
+ ["silver", "#c0c0c0"],
2375
+ ["skyblue", "#87ceeb"],
2376
+ ["slateblue", "#6a5acd"],
2377
+ ["slategray", "#708090"],
2378
+ ["slategrey", "#708090"],
2379
+ ["snow", "#fffafa"],
2380
+ ["springgreen", "#00ff7f"],
2381
+ ["steelblue", "#4682b4"],
2382
+ ["tan", "#d2b48c"],
2383
+ ["teal", "#008080"],
2384
+ ["thistle", "#d8bfd8"],
2385
+ ["tomato", "#ff6347"],
2386
+ ["turquoise", "#40e0d0"],
2387
+ ["violet", "#ee82ee"],
2388
+ ["wheat", "#f5deb3"],
2389
+ ["white", "#ffffff"],
2390
+ ["whitesmoke", "#f5f5f5"],
2391
+ ["yellow", "#ffff00"],
2392
+ ["yellowgreen", "#9acd32"]
2393
+ ]);
2394
+ const HASH_PATTERN = /^#([\dA-F]{2})([\dA-F]{2})([\dA-F]{2})([\dA-F]{2})?$/i;
2395
+ const SMALL_HASH_PATTERN = /^#([\dA-F])([\dA-F])([\dA-F])([\dA-F])?$/i;
2396
+ const RGB_PATTERN = /^rgba?\(\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*(,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*)?\)$/;
2397
+ const HSL_PATTERN = /^hsla?\(\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))%\s*,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))%\s*(,\s*([+-]?(?:\d+(?:\.\d+)?|\.\d+))\s*)?\)$/;
2398
+ const getDefaultCss = () => {
2399
+ return {
2400
+ __proto__: null,
2401
+ backgroundColor: null,
2402
+ color: null,
2403
+ fontStyle: null,
2404
+ fontWeight: null,
2405
+ textDecorationColor: null,
2406
+ textDecorationLine: []
2407
+ };
2408
+ };
2409
+ const SPACE_PATTERN = /\s+/;
2410
+ const parseCssColor = (colorString) => {
2411
+ if (colorKeywords.has(colorString)) {
2412
+ colorString = colorKeywords.get(colorString);
2413
+ }
2414
+ const hashMatch = HASH_PATTERN.exec(colorString);
2415
+ if (hashMatch) {
2416
+ return [Number.parseInt(hashMatch[1], 16), Number.parseInt(hashMatch[2], 16), Number.parseInt(hashMatch[3], 16)];
2417
+ }
2418
+ const smallHashMatch = SMALL_HASH_PATTERN.exec(colorString);
2419
+ if (smallHashMatch) {
2420
+ return [
2421
+ Number.parseInt(`${smallHashMatch[1]}${smallHashMatch[1]}`, 16),
2422
+ Number.parseInt(`${smallHashMatch[2]}${smallHashMatch[2]}`, 16),
2423
+ Number.parseInt(`${smallHashMatch[3]}${smallHashMatch[3]}`, 16)
2424
+ ];
2425
+ }
2426
+ const rgbMatch = RGB_PATTERN.exec(colorString);
2427
+ if (rgbMatch) {
2428
+ return [
2429
+ Math.round(Math.max(0, Math.min(255, Number(rgbMatch[1])))),
2430
+ Math.round(Math.max(0, Math.min(255, Number(rgbMatch[2])))),
2431
+ Math.round(Math.max(0, Math.min(255, Number(rgbMatch[3]))))
2432
+ ];
2433
+ }
2434
+ const hslMatch = HSL_PATTERN.exec(colorString);
2435
+ if (hslMatch) {
2436
+ let h = Number(hslMatch[1]) % 360;
2437
+ if (h < 0) {
2438
+ h += 360;
2439
+ }
2440
+ const s = Math.max(0, Math.min(100, Number(hslMatch[2]))) / 100;
2441
+ const l = Math.max(0, Math.min(100, Number(hslMatch[3]))) / 100;
2442
+ const c = (1 - Math.abs(2 * l - 1)) * s;
2443
+ const x = c * (1 - Math.abs(h / 60 % 2 - 1));
2444
+ const m = l - c / 2;
2445
+ let r_;
2446
+ let g_;
2447
+ let b_;
2448
+ if (h < 60) {
2449
+ ({ 0: r_, 1: g_, 2: b_ } = [c, x, 0]);
2450
+ } else if (h < 120) {
2451
+ ({ 0: r_, 1: g_, 2: b_ } = [x, c, 0]);
2452
+ } else if (h < 180) {
2453
+ ({ 0: r_, 1: g_, 2: b_ } = [0, c, x]);
2454
+ } else if (h < 240) {
2455
+ ({ 0: r_, 1: g_, 2: b_ } = [0, x, c]);
2456
+ } else if (h < 300) {
2457
+ ({ 0: r_, 1: g_, 2: b_ } = [x, 0, c]);
2458
+ } else {
2459
+ ({ 0: r_, 1: g_, 2: b_ } = [c, 0, x]);
2460
+ }
2461
+ return [Math.round((r_ + m) * 255), Math.round((g_ + m) * 255), Math.round((b_ + m) * 255)];
2462
+ }
2463
+ return null;
2464
+ };
2465
+ const colorEquals = (color1, color2) => color1?.[0] === color2?.[0] && color1?.[1] === color2?.[1] && color1?.[2] === color2?.[2];
2466
+ const parseCss = (cssString) => {
2467
+ const css = getDefaultCss();
2468
+ const rawEntries = [];
2469
+ let inValue = false;
2470
+ let currentKey = null;
2471
+ let parenthesesDepth = 0;
2472
+ let currentPart = "";
2473
+ for (const c of cssString) {
2474
+ if (c === "(") {
2475
+ parenthesesDepth += 1;
2476
+ } else if (parenthesesDepth > 0) {
2477
+ if (c === ")") {
2478
+ parenthesesDepth -= 1;
2479
+ }
2480
+ } else if (inValue) {
2481
+ if (c === ";") {
2482
+ const value = currentPart.trim();
2483
+ if (value !== "") {
2484
+ rawEntries.push([currentKey, value]);
2485
+ }
2486
+ currentKey = null;
2487
+ currentPart = "";
2488
+ inValue = false;
2489
+ continue;
2490
+ }
2491
+ } else if (c === ":") {
2492
+ currentKey = currentPart.trim();
2493
+ currentPart = "";
2494
+ inValue = true;
2495
+ continue;
2496
+ }
2497
+ currentPart += c;
2498
+ }
2499
+ if (inValue && parenthesesDepth === 0) {
2500
+ const value = currentPart.trim();
2501
+ if (value !== "") {
2502
+ rawEntries.push([currentKey, value]);
2503
+ }
2504
+ currentKey = null;
2505
+ currentPart = "";
2506
+ }
2507
+ for (const { 0: key, 1: value } of rawEntries) {
2508
+ switch (key) {
2509
+ case "background-color": {
2510
+ if (value != void 0) {
2511
+ css.backgroundColor = value;
2512
+ }
2513
+ break;
2514
+ }
2515
+ case "color": {
2516
+ if (value != void 0) {
2517
+ css.color = value;
2518
+ }
2519
+ break;
2520
+ }
2521
+ case "font-style": {
2522
+ if (["italic", "oblique", "oblique 14deg"].includes(value)) {
2523
+ css.fontStyle = "italic";
2524
+ }
2525
+ break;
2526
+ }
2527
+ case "font-weight": {
2528
+ if (value === "bold") {
2529
+ css.fontWeight = value;
2530
+ }
2531
+ break;
2532
+ }
2533
+ case "text-decoration": {
2534
+ css.textDecorationColor = null;
2535
+ css.textDecorationLine = [];
2536
+ const arguments_ = value.split(SPACE_PATTERN);
2537
+ for (const argument of arguments_) {
2538
+ const maybeColor = parseCssColor(argument);
2539
+ if (maybeColor != void 0) {
2540
+ css.textDecorationColor = maybeColor;
2541
+ } else if (["line-through", "overline", "underline"].includes(argument)) {
2542
+ css.textDecorationLine.push(argument);
2543
+ }
2544
+ }
2545
+ break;
2546
+ }
2547
+ case "text-decoration-color": {
2548
+ const color = parseCssColor(value);
2549
+ if (color != void 0) {
2550
+ css.textDecorationColor = color;
2551
+ }
2552
+ break;
2553
+ }
2554
+ case "text-decoration-line": {
2555
+ css.textDecorationLine = [];
2556
+ const lineTypes = value.split(SPACE_PATTERN);
2557
+ for (const lineType of lineTypes) {
2558
+ if (["line-through", "overline", "underline"].includes(lineType)) {
2559
+ css.textDecorationLine.push(lineType);
2560
+ }
2561
+ }
2562
+ break;
2563
+ }
2564
+ }
2565
+ }
2566
+ return css;
2567
+ };
2568
+ const cssToAnsi = (css, previousCss = null) => {
2569
+ previousCss = previousCss ?? getDefaultCss();
2570
+ let ansi = "";
2571
+ if (!colorEquals(css.backgroundColor, previousCss.backgroundColor)) {
2572
+ if (css.backgroundColor == void 0) {
2573
+ ansi += "\x1B[49m";
2574
+ } else {
2575
+ switch (css.backgroundColor) {
2576
+ case "black": {
2577
+ ansi += "\x1B[40m";
2578
+ break;
2579
+ }
2580
+ case "blue": {
2581
+ ansi += "\x1B[44m";
2582
+ break;
2583
+ }
2584
+ case "cyan": {
2585
+ ansi += "\x1B[46m";
2586
+ break;
2587
+ }
2588
+ case "green": {
2589
+ ansi += "\x1B[42m";
2590
+ break;
2591
+ }
2592
+ case "magenta": {
2593
+ ansi += "\x1B[45m";
2594
+ break;
2595
+ }
2596
+ case "red": {
2597
+ ansi += "\x1B[41m";
2598
+ break;
2599
+ }
2600
+ case "white": {
2601
+ ansi += "\x1B[47m";
2602
+ break;
2603
+ }
2604
+ case "yellow": {
2605
+ ansi += "\x1B[43m";
2606
+ break;
2607
+ }
2608
+ default: {
2609
+ if (Array.isArray(css.backgroundColor)) {
2610
+ const { 0: r, 1: g, 2: b } = css.backgroundColor;
2611
+ ansi += `\x1B[48;2;${String(r)};${String(g)};${String(b)}m`;
2612
+ } else {
2613
+ const parsed = parseCssColor(css.backgroundColor);
2614
+ if (parsed === null) {
2615
+ ansi += "\x1B[49m";
2616
+ } else {
2617
+ const { 0: r, 1: g, 2: b } = parsed;
2618
+ ansi += `\x1B[48;2;${String(r)};${String(g)};${String(b)}m`;
2619
+ }
2620
+ }
2621
+ }
2622
+ }
2623
+ }
2624
+ }
2625
+ if (!colorEquals(css.color, previousCss.color)) {
2626
+ if (css.color == void 0) {
2627
+ ansi += "\x1B[39m";
2628
+ } else {
2629
+ switch (css.color) {
2630
+ case "black": {
2631
+ ansi += "\x1B[30m";
2632
+ break;
2633
+ }
2634
+ case "blue": {
2635
+ ansi += "\x1B[34m";
2636
+ break;
2637
+ }
2638
+ case "cyan": {
2639
+ ansi += "\x1B[36m";
2640
+ break;
2641
+ }
2642
+ case "green": {
2643
+ ansi += "\x1B[32m";
2644
+ break;
2645
+ }
2646
+ case "magenta": {
2647
+ ansi += "\x1B[35m";
2648
+ break;
2649
+ }
2650
+ case "red": {
2651
+ ansi += "\x1B[31m";
2652
+ break;
2653
+ }
2654
+ case "white": {
2655
+ ansi += "\x1B[37m";
2656
+ break;
2657
+ }
2658
+ case "yellow": {
2659
+ ansi += "\x1B[33m";
2660
+ break;
2661
+ }
2662
+ default: {
2663
+ if (Array.isArray(css.color)) {
2664
+ const { 0: r, 1: g, 2: b } = css.color;
2665
+ ansi += `\x1B[38;2;${String(r)};${String(g)};${String(b)}m`;
2666
+ } else {
2667
+ const parsed = parseCssColor(css.color);
2668
+ if (parsed === null) {
2669
+ ansi += "\x1B[39m";
2670
+ } else {
2671
+ const { 0: r, 1: g, 2: b } = parsed;
2672
+ ansi += `\x1B[38;2;${String(r)};${String(g)};${String(b)}m`;
2673
+ }
2674
+ }
2675
+ }
2676
+ }
2677
+ }
2678
+ }
2679
+ if (css.fontWeight !== previousCss.fontWeight) {
2680
+ ansi += css.fontWeight === "bold" ? "\x1B[1m" : "\x1B[22m";
2681
+ }
2682
+ if (css.fontStyle !== previousCss.fontStyle) {
2683
+ ansi += css.fontStyle === "italic" ? "\x1B[3m" : "\x1B[23m";
2684
+ }
2685
+ if (!colorEquals(css.textDecorationColor, previousCss.textDecorationColor)) {
2686
+ if (css.textDecorationColor == void 0) {
2687
+ ansi += "\x1B[59m";
2688
+ } else {
2689
+ const { 0: r, 1: g, 2: b } = css.textDecorationColor;
2690
+ ansi += `\x1B[58;2;${String(r)};${String(g)};${String(b)}m`;
2691
+ }
2692
+ }
2693
+ if (css.textDecorationLine.includes("line-through") !== previousCss.textDecorationLine.includes("line-through")) {
2694
+ ansi += css.textDecorationLine.includes("line-through") ? "\x1B[9m" : "\x1B[29m";
2695
+ }
2696
+ if (css.textDecorationLine.includes("overline") !== previousCss.textDecorationLine.includes("overline")) {
2697
+ ansi += css.textDecorationLine.includes("overline") ? "\x1B[53m" : "\x1B[55m";
2698
+ }
2699
+ if (css.textDecorationLine.includes("underline") !== previousCss.textDecorationLine.includes("underline")) {
2700
+ ansi += css.textDecorationLine.includes("underline") ? "\x1B[4m" : "\x1B[24m";
2701
+ }
2702
+ return ansi;
2703
+ };
2704
+ const tryStringify = (o) => {
2705
+ try {
2706
+ return JSON.stringify(o);
2707
+ } catch {
2708
+ return '"[Circular]"';
2709
+ }
2710
+ };
2711
+ const CHAR_PERCENT = "%".codePointAt(0);
2712
+ const CHAR_s = "s".codePointAt(0);
2713
+ const CHAR_d = "d".codePointAt(0);
2714
+ const CHAR_f = "f".codePointAt(0);
2715
+ const CHAR_i = "i".codePointAt(0);
2716
+ const CHAR_O = "O".codePointAt(0);
2717
+ const CHAR_o = "o".codePointAt(0);
2718
+ const CHAR_j = "j".codePointAt(0);
2719
+ const CHAR_c = "c".codePointAt(0);
2720
+ const format = (fmt, arguments_ = [], options = {}) => {
2721
+ if (typeof fmt !== "string" && typeof fmt !== "object" || fmt === null) {
2722
+ throw new TypeError(`fmt must be a string or object, got ${fmt === null ? "null" : typeof fmt}`);
2723
+ }
2724
+ const stringify = options.stringify ?? tryStringify;
2725
+ const offset = 1;
2726
+ if (typeof fmt === "object") {
2727
+ const argumentsLength = arguments_.length + offset;
2728
+ if (argumentsLength === 1) {
2729
+ return "{}";
2730
+ }
2731
+ const objects = Array.from({ length: argumentsLength });
2732
+ objects[0] = stringify(fmt);
2733
+ for (let index = 1; index < argumentsLength; index += 1) {
2734
+ objects[index] = stringify(arguments_[index - offset]);
2735
+ }
2736
+ return objects.join(" ");
2737
+ }
2738
+ if (arguments_.length === 0) {
2739
+ return fmt;
2740
+ }
2741
+ let result = "";
2742
+ let a = 1 - offset;
2743
+ let lastPosition = -1;
2744
+ let usedStyle = false;
2745
+ let previousCss = null;
2746
+ for (let index = 0; index < fmt.length; ) {
2747
+ if (fmt.codePointAt(index) === CHAR_PERCENT && index + 1 < fmt.length) {
2748
+ lastPosition = lastPosition > -1 ? lastPosition : 0;
2749
+ const c = fmt.codePointAt(index + 1);
2750
+ if (c === void 0) {
2751
+ a += 1;
2752
+ break;
2753
+ }
2754
+ switch (c) {
2755
+ case CHAR_c: {
2756
+ if (globalThis.window === void 0) {
2757
+ const css = parseCss(arguments_[a]);
2758
+ if (lastPosition < index) {
2759
+ result += fmt.slice(lastPosition, index);
2760
+ }
2761
+ result += cssToAnsi(css, previousCss);
2762
+ if (result !== "") {
2763
+ usedStyle = true;
2764
+ previousCss = css;
2765
+ }
2766
+ }
2767
+ lastPosition = index + 2;
2768
+ index += 1;
2769
+ break;
2770
+ }
2771
+ case CHAR_d:
2772
+ case CHAR_f: {
2773
+ if (a >= arguments_.length || arguments_[a] == void 0) {
2774
+ break;
2775
+ }
2776
+ if (lastPosition < index) {
2777
+ result += fmt.slice(lastPosition, index);
2778
+ }
2779
+ result += Number(arguments_[a]).toString();
2780
+ lastPosition = index + 2;
2781
+ index += 1;
2782
+ break;
2783
+ }
2784
+ case CHAR_i: {
2785
+ if (a >= arguments_.length || arguments_[a] == void 0) {
2786
+ break;
2787
+ }
2788
+ if (lastPosition < index) {
2789
+ result += fmt.slice(lastPosition, index);
2790
+ }
2791
+ result += Math.floor(Number(arguments_[a])).toString();
2792
+ lastPosition = index + 2;
2793
+ index += 1;
2794
+ break;
2795
+ }
2796
+ case CHAR_j:
2797
+ case CHAR_O:
2798
+ case CHAR_o: {
2799
+ if (a >= arguments_.length || arguments_[a] === void 0) {
2800
+ break;
2801
+ }
2802
+ if (lastPosition < index) {
2803
+ result += fmt.slice(lastPosition, index);
2804
+ }
2805
+ const temporaryArgument = arguments_[a];
2806
+ const type = typeof temporaryArgument;
2807
+ if (type === "string") {
2808
+ result += `'${temporaryArgument}'`;
2809
+ lastPosition = index + 2;
2810
+ break;
2811
+ }
2812
+ if (type === "function") {
2813
+ result += temporaryArgument.name ? `[Function: ${temporaryArgument.name}]` : "[Function: <anonymous>]";
2814
+ lastPosition = index + 2;
2815
+ break;
2816
+ }
2817
+ result += stringify(temporaryArgument);
2818
+ lastPosition = index + 2;
2819
+ index += 1;
2820
+ break;
2821
+ }
2822
+ case CHAR_PERCENT: {
2823
+ if (lastPosition < index) {
2824
+ result += fmt.slice(lastPosition, index);
2825
+ }
2826
+ result += "%";
2827
+ lastPosition = index + 2;
2828
+ index += 1;
2829
+ a -= 1;
2830
+ break;
2831
+ }
2832
+ case CHAR_s: {
2833
+ if (a >= arguments_.length) {
2834
+ break;
2835
+ }
2836
+ if (lastPosition < index) {
2837
+ result += fmt.slice(lastPosition, index);
2838
+ }
2839
+ result += typeof arguments_[a] === "object" ? stringify(arguments_[a]) : String(arguments_[a]);
2840
+ lastPosition = index + 2;
2841
+ index += 1;
2842
+ break;
2843
+ }
2844
+ default: {
2845
+ if (typeof options.formatters?.[c] === "function") {
2846
+ if (lastPosition < index) {
2847
+ result += fmt.slice(lastPosition, index);
2848
+ }
2849
+ result += options.formatters[c](arguments_[a]);
2850
+ lastPosition = index + 2;
2851
+ index += 1;
2852
+ }
2853
+ }
2854
+ }
2855
+ a += 1;
2856
+ }
2857
+ index += 1;
2858
+ }
2859
+ if (lastPosition === -1) {
2860
+ return fmt;
2861
+ }
2862
+ if (lastPosition < fmt.length) {
2863
+ result += fmt.slice(lastPosition);
2864
+ }
2865
+ if (usedStyle) {
2866
+ result += "\x1B[0m";
2867
+ }
2868
+ return result;
2869
+ };
2870
+ const build = (options = {}) => {
2871
+ const formatters = {};
2872
+ if (typeof options.formatters === "object") {
2873
+ Object.entries(options.formatters).forEach(([key, formatterFunction]) => {
2874
+ if (key.length === 0) {
2875
+ throw new Error(`Formatter %${key} has no characters`);
2876
+ }
2877
+ if (key.length > 1) {
2878
+ throw new Error(`Formatter %${key} has more than one character`);
2879
+ }
2880
+ if (typeof formatterFunction !== "function") {
2881
+ throw new TypeError(`Formatter for %${key} is not a function`);
2882
+ }
2883
+ const c = key.codePointAt(0);
2884
+ if (c === void 0) {
2885
+ throw new Error(`${key}.codePointAt(0) failed to return a value, please report this issue`);
2886
+ }
2887
+ formatters[c] = formatterFunction;
2888
+ });
2889
+ }
2890
+ return (f, arguments_ = [], formatOptions = {}) => format(f, arguments_, { ...formatOptions, formatters });
2891
+ };
2892
+
2893
+ class MessageFormatterProcessor {
2894
+ /** Custom stringify function for object serialization */
2895
+ #stringify;
2896
+ /** Custom formatters for message interpolation */
2897
+ #formatters;
2898
+ /**
2899
+ * Creates a new MessageFormatterProcessor instance.
2900
+ * @param options Configuration options
2901
+ * @param options.formatters Custom formatters for message interpolation
2902
+ */
2903
+ constructor(options = {}) {
2904
+ this.#formatters = options.formatters;
2905
+ }
2906
+ /**
2907
+ * Sets the stringify function for object serialization.
2908
+ * @param function_ The stringify function to use for serializing objects
2909
+ */
2910
+ setStringify(function_) {
2911
+ this.#stringify = function_;
2912
+ }
2913
+ /**
2914
+ * Processes log metadata to format messages.
2915
+ *
2916
+ * Applies string interpolation and custom formatters to the message
2917
+ * and contextual data in the log metadata.
2918
+ * @param meta The log metadata to process
2919
+ * @returns The processed metadata with formatted messages
2920
+ */
2921
+ process(meta) {
2922
+ const formatter = build({
2923
+ formatters: this.#formatters});
2924
+ if (meta.message !== void 0) {
2925
+ meta.message = this.#format(formatter, meta.message, meta.context ?? []);
2926
+ }
2927
+ return meta;
2928
+ }
2929
+ /**
2930
+ * Recursively formats data using the formatter.
2931
+ *
2932
+ * Applies string interpolation and formatting to strings, arrays, and objects.
2933
+ * @param formatter The formatter function to use
2934
+ * @param data The data to format (string, array, or object)
2935
+ * @param arguments_ Additional arguments for formatting
2936
+ * @returns The formatted data
2937
+ * @private
2938
+ */
2939
+ #format(formatter, data, arguments_ = []) {
2940
+ if (typeof data === "string") {
2941
+ return formatter(data, arguments_);
2942
+ }
2943
+ if (typeof data === "object" && data !== null) {
2944
+ const record = data;
2945
+ const keys = Object.keys(record);
2946
+ for (let i = 0; i < keys.length; i += 1) {
2947
+ const index = keys[i];
2948
+ const value = record[index];
2949
+ if (typeof value === "string" || Array.isArray(value) || typeof value === "object") {
2950
+ record[index] = this.#format(formatter, value, arguments_);
2951
+ }
2952
+ }
2953
+ }
2954
+ return data;
2955
+ }
2956
+ }
2957
+
2958
+ const PAIL_DIST_REGEX = /[\\/]pail[\\/]dist/;
2959
+ const pailFileFilter = (line) => !PAIL_DIST_REGEX.test(line);
2960
+ class PrettyReporter extends AbstractPrettyReporter {
2961
+ #stdout;
2962
+ #stderr;
2963
+ #interactiveManager;
2964
+ #interactive = false;
2965
+ #inspectOptions;
2966
+ #errorOptions;
2967
+ /**
2968
+ * Creates a new Server Pretty Reporter instance.
2969
+ * @param options Configuration options for styling, error rendering, and object inspection
2970
+ */
2971
+ constructor(options = {}) {
2972
+ const { error: errorOptions, inspect: inspectOptions, ...rest } = options;
2973
+ super({
2974
+ uppercase: {
2975
+ label: true,
2976
+ ...rest.uppercase
2977
+ },
2978
+ ...rest
2979
+ });
2980
+ this.#inspectOptions = { ...defaultInspectorConfig, ...inspectOptions };
2981
+ this.#errorOptions = {
2982
+ ...errorOptions,
2983
+ color: {
2984
+ fileLine: green,
2985
+ hint: cyan,
2986
+ marker: red,
2987
+ message: red,
2988
+ method: greenBright,
2989
+ title: red
2990
+ }
2991
+ };
2992
+ this.#stdout = stdout;
2993
+ this.#stderr = stderr;
2994
+ }
2995
+ /**
2996
+ * Sets the stdout stream for the reporter.
2997
+ * @param stdout_ The writable stream to use for standard output
2998
+ */
2999
+ setStdout(stdout_) {
3000
+ this.#stdout = stdout_;
3001
+ }
3002
+ /**
3003
+ * Sets the stderr stream for the reporter.
3004
+ * @param stderr_ The writable stream to use for error output
3005
+ */
3006
+ setStderr(stderr_) {
3007
+ this.#stderr = stderr_;
3008
+ }
3009
+ /**
3010
+ * Sets the interactive manager for handling interactive output.
3011
+ * @param manager The interactive manager instance, or undefined to disable
3012
+ */
3013
+ setInteractiveManager(manager) {
3014
+ this.#interactiveManager = manager;
3015
+ }
3016
+ /**
3017
+ * Enables or disables interactive mode.
3018
+ * @param interactive Whether to enable interactive terminal features
3019
+ */
3020
+ setIsInteractive(interactive) {
3021
+ this.#interactive = interactive;
3022
+ }
3023
+ log(meta) {
3024
+ this._log(this._formatMessage(meta), meta.type.level);
3025
+ }
3026
+ // eslint-disable-next-line sonarjs/cognitive-complexity, no-underscore-dangle
3027
+ _formatMessage(data) {
3028
+ const { columns } = terminalSize();
3029
+ let size = columns;
3030
+ if (typeof this.styles.messageLength === "number") {
3031
+ size = this.styles.messageLength;
3032
+ }
3033
+ const { badge, context, date, error, file, groups, label, message, prefix, repeated, scope, suffix, traceError, type } = data;
3034
+ const { color } = this.loggerTypes[type.name];
3035
+ const colorized = color ? colorize[color] : white;
3036
+ const groupSpaces = groups.map(() => " ").join("");
3037
+ const items = [];
3038
+ if (groups.length > 0) {
3039
+ items.push(`${groupSpaces + grey(`[${groups.at(-1) ?? ""}]`)} `);
3040
+ }
3041
+ if (date) {
3042
+ items.push(`${grey(this.styles.dateFormatter(typeof date === "string" ? new Date(date) : date))} `);
3043
+ }
3044
+ if (badge) {
3045
+ items.push(colorized(badge));
3046
+ } else {
3047
+ const longestBadge = getLongestBadge(this.loggerTypes);
3048
+ if (longestBadge.length > 0) {
3049
+ items.push(`${grey(".".repeat(longestBadge.length))} `);
3050
+ }
3051
+ }
3052
+ const longestLabel = getLongestLabel(this.loggerTypes);
3053
+ if (label) {
3054
+ const longestLabelWidth = getStringWidth(longestLabel);
3055
+ const labelWidth = getStringWidth(label);
3056
+ items.push(`${colorized(formatLabel(label, this.styles))} `, grey(".".repeat(Math.max(0, longestLabelWidth - labelWidth))));
3057
+ } else {
3058
+ items.push(grey(".".repeat(longestLabel.length + 2)));
3059
+ }
3060
+ if (repeated) {
3061
+ items.push(`${bgGrey.white(`[${String(repeated)}x]`)} `);
3062
+ }
3063
+ if (Array.isArray(scope) && scope.length > 0) {
3064
+ items.push(` ${grey(`[${scope.join(" > ")}]`)} `);
3065
+ }
3066
+ if (prefix) {
3067
+ items.push(
3068
+ `${grey(`${Array.isArray(scope) && scope.length > 0 ? ". " : " "}[${this.styles.underline.prefix ? underline(prefix) : prefix}]`)} `
3069
+ );
3070
+ }
3071
+ const titleSize = getStringWidth(items.join(" "));
3072
+ if (file) {
3073
+ const fileMessage = (file.name ?? "") + (file.line ? `:${String(file.line)}` : "");
3074
+ const fileMessageSize = getStringWidth(fileMessage);
3075
+ if (fileMessageSize + titleSize + 2 > size) {
3076
+ items.push(grey(` ${fileMessage}`));
3077
+ } else {
3078
+ const dots = Math.max(0, size - titleSize - fileMessageSize - 2);
3079
+ items.push(grey(`${".".repeat(dots)} ${fileMessage}`));
3080
+ }
3081
+ } else {
3082
+ items.push(grey(".".repeat(Math.max(0, size - titleSize - 1))));
3083
+ }
3084
+ if (items.length > 0) {
3085
+ items.push("\n\n");
3086
+ }
3087
+ if (message !== EMPTY_SYMBOL) {
3088
+ const formattedMessage = typeof message === "string" ? message : inspect(message, this.#inspectOptions);
3089
+ items.push(
3090
+ groupSpaces + wordWrap(formattedMessage, {
3091
+ trim: false,
3092
+ width: size - 3,
3093
+ wrapMode: WrapMode.STRICT_WIDTH
3094
+ })
3095
+ );
3096
+ }
3097
+ if (context) {
3098
+ let hasError = false;
3099
+ items.push(
3100
+ ...context.map((value) => {
3101
+ if (value instanceof Error) {
3102
+ hasError = true;
3103
+ return `
3104
+
3105
+ ${renderError(value, {
3106
+ ...this.#errorOptions,
3107
+ filterStacktrace: pailFileFilter,
3108
+ prefix: groupSpaces
3109
+ })}`;
3110
+ }
3111
+ if (typeof value === "object") {
3112
+ return ` ${inspect(value, this.#inspectOptions)}`;
3113
+ }
3114
+ const newValue = (hasError ? "\n\n" : " ") + String(value);
3115
+ hasError = false;
3116
+ return newValue;
3117
+ })
3118
+ );
3119
+ }
3120
+ if (error) {
3121
+ items.push(
3122
+ renderError(error, {
3123
+ ...this.#errorOptions,
3124
+ filterStacktrace: pailFileFilter,
3125
+ prefix: groupSpaces
3126
+ })
3127
+ );
3128
+ }
3129
+ if (traceError) {
3130
+ items.push(
3131
+ `
3132
+
3133
+ ${renderError(traceError, {
3134
+ ...this.#errorOptions,
3135
+ filterStacktrace: pailFileFilter,
3136
+ hideErrorCauseCodeView: true,
3137
+ hideErrorCodeView: true,
3138
+ hideErrorErrorsCodeView: true,
3139
+ hideMessage: true,
3140
+ prefix: groupSpaces
3141
+ })}`
3142
+ );
3143
+ }
3144
+ if (suffix) {
3145
+ items.push("\n", groupSpaces + grey(this.styles.underline.suffix ? underline(suffix) : suffix));
3146
+ }
3147
+ return items.join("");
3148
+ }
3149
+ // eslint-disable-next-line no-underscore-dangle
3150
+ _log(message, logLevel) {
3151
+ const streamType = ["error", "trace", "warn"].includes(logLevel) ? "stderr" : "stdout";
3152
+ const stream = streamType === "stderr" ? this.#stderr : this.#stdout;
3153
+ if (this.#interactive && this.#interactiveManager !== void 0 && stream.isTTY) {
3154
+ this.#interactiveManager.update(streamType, message.split("\n"), 0);
3155
+ } else {
3156
+ writeStream(`${message}
3157
+ `, stream);
3158
+ }
3159
+ }
3160
+ }
3161
+
508
3162
  const getDefaultLogLevel = () => {
509
3163
  if (env.NODE_ENV === "debug" || env.DEBUG !== void 0) {
510
3164
  return "debug";