@visulima/pail 3.1.0 → 3.2.1

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 (111) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/LICENSE.md +3 -407
  3. package/README.md +298 -0
  4. package/dist/constants.d.ts +37 -0
  5. package/dist/index.browser.d.ts +47 -12
  6. package/dist/index.browser.js +12 -1
  7. package/dist/index.server.d.ts +65 -40
  8. package/dist/index.server.js +533 -2
  9. package/dist/interactive/index.d.ts +2 -28
  10. package/dist/interactive/index.js +2 -1
  11. package/dist/interactive/interactive-manager.d.ts +108 -0
  12. package/dist/interactive/interactive-stream-hook.d.ts +68 -0
  13. package/dist/object-tree.d.ts +65 -7
  14. package/dist/object-tree.js +89 -2
  15. package/dist/packem_shared/AbstractJsonReporter-intFdT_A.js +204 -0
  16. package/dist/packem_shared/InteractiveManager-CZ85hGNW.js +172 -0
  17. package/dist/packem_shared/InteractiveStreamHook-DiSubbJ1.js +21 -0
  18. package/dist/packem_shared/JsonReporter-C0AXk99i.js +58 -0
  19. package/dist/packem_shared/JsonReporter-DcM2LBX9.js +28 -0
  20. package/dist/packem_shared/PrettyReporter-BFWaYP_J.js +222 -0
  21. package/dist/packem_shared/PrettyReporter-CuLLKr6-.js +169 -0
  22. package/dist/packem_shared/abstract-pretty-reporter-DMPDCslJ.js +50 -0
  23. package/dist/packem_shared/constants-DfDr4MHC.js +119 -0
  24. package/dist/packem_shared/format-label-Btft2KGP.js +1194 -0
  25. package/dist/packem_shared/get-longest-label-C9PWeyKq.js +9 -0
  26. package/dist/packem_shared/index-BomQ3E6J.js +650 -0
  27. package/dist/packem_shared/index-DqKWykfa.js +1146 -0
  28. package/dist/packem_shared/interactive-stream-hook-DG4BtN12.js +141 -0
  29. package/dist/packem_shared/pail.browser-CPjQrsyy.js +1427 -0
  30. package/dist/packem_shared/write-console-log-based-on-level-DBmRYXpj.js +14 -0
  31. package/dist/packem_shared/write-stream-BG8fhcs3.js +6 -0
  32. package/dist/pail.browser.d.ts +412 -0
  33. package/dist/pail.server.d.ts +233 -0
  34. package/dist/processor/caller/caller-processor.d.ts +40 -7
  35. package/dist/processor/caller/caller-processor.js +59 -1
  36. package/dist/processor/caller/get-caller-filename.d.ts +23 -0
  37. package/dist/processor/message-formatter-processor.d.ts +44 -9
  38. package/dist/processor/message-formatter-processor.js +67 -1
  39. package/dist/processor/opentelemetry-processor.d.ts +70 -0
  40. package/dist/processor/opentelemetry-processor.js +52 -0
  41. package/dist/processor/redact-processor.d.ts +39 -8
  42. package/dist/processor/redact-processor.js +30 -1
  43. package/dist/progress-bar.d.ts +75 -15
  44. package/dist/progress-bar.js +404 -1
  45. package/dist/reporter/file/json-file-reporter.d.ts +39 -20
  46. package/dist/reporter/file/json-file-reporter.js +136 -4
  47. package/dist/reporter/file/utils/rotating-file-stream.d.ts +48 -0
  48. package/dist/reporter/http/abstract-http-reporter.d.ts +215 -0
  49. package/dist/reporter/http/abstract-http-reporter.js +435 -0
  50. package/dist/reporter/http/http-reporter.d.ts +39 -0
  51. package/dist/reporter/http/http-reporter.edge-light.d.ts +40 -0
  52. package/dist/reporter/http/http-reporter.edge-light.js +651 -0
  53. package/dist/reporter/http/http-reporter.js +13 -0
  54. package/dist/reporter/http/utils/compression.d.ts +7 -0
  55. package/dist/reporter/http/utils/log-size-error.d.ts +30 -0
  56. package/dist/reporter/http/utils/retry.d.ts +27 -0
  57. package/dist/reporter/json/abstract-json-reporter.d.ts +61 -0
  58. package/dist/reporter/json/index.browser.d.ts +3 -13
  59. package/dist/reporter/json/index.browser.js +2 -1
  60. package/dist/reporter/json/index.d.ts +3 -16
  61. package/dist/reporter/json/index.js +2 -1
  62. package/dist/reporter/json/json-reporter.browser.d.ts +40 -0
  63. package/dist/reporter/json/json-reporter.server.d.ts +50 -0
  64. package/dist/reporter/pretty/abstract-pretty-reporter.d.ts +83 -0
  65. package/dist/reporter/pretty/index.browser.d.ts +2 -13
  66. package/dist/reporter/pretty/index.browser.js +1 -1
  67. package/dist/reporter/pretty/index.d.ts +2 -25
  68. package/dist/reporter/pretty/index.js +1 -1
  69. package/dist/reporter/pretty/pretty-reporter.browser.d.ts +36 -0
  70. package/dist/reporter/pretty/pretty-reporter.server.d.ts +70 -0
  71. package/dist/reporter/raw/raw-reporter.browser.d.ts +5 -0
  72. package/dist/reporter/raw/raw-reporter.server.d.ts +13 -0
  73. package/dist/reporter/simple/simple-reporter.server.d.ts +10 -14
  74. package/dist/reporter/simple/simple-reporter.server.js +186 -8
  75. package/dist/reporter/utils/default-inspector-config.d.ts +3 -0
  76. package/dist/reporter/utils/format-label.d.ts +3 -0
  77. package/dist/spinner.d.ts +170 -104
  78. package/dist/spinner.js +2150 -1
  79. package/dist/types.d.ts +241 -0
  80. package/dist/utils/ansi-escapes.d.ts +4 -0
  81. package/dist/utils/arrayify.d.ts +2 -0
  82. package/dist/utils/get-longest-badge.d.ts +4 -0
  83. package/dist/utils/get-longest-label.d.ts +4 -0
  84. package/dist/utils/merge-types.d.ts +4 -0
  85. package/dist/utils/stream/safe-stream-handler.d.ts +21 -0
  86. package/dist/utils/write-console-log-based-on-level.d.ts +4 -0
  87. package/dist/utils/write-stream.d.ts +2 -0
  88. package/package.json +53 -4
  89. package/dist/packem_shared/AbstractJsonReporter-UftN6CIL.js +0 -1
  90. package/dist/packem_shared/InteractiveManager-CgmJyW9x.js +0 -3
  91. package/dist/packem_shared/InteractiveStreamHook-NtJu71aN.js +0 -1
  92. package/dist/packem_shared/JsonReporter-DTBtHNaD.js +0 -2
  93. package/dist/packem_shared/JsonReporter-Dl4m0xZe.js +0 -1
  94. package/dist/packem_shared/PrettyReporter-Bns0ZWLy.js +0 -12
  95. package/dist/packem_shared/PrettyReporter-CGKSTI7X.js +0 -5
  96. package/dist/packem_shared/abstract-json-reporter-CPsNkpz8.d.ts +0 -22
  97. package/dist/packem_shared/abstract-pretty-reporter-CUtSm20r.js +0 -1
  98. package/dist/packem_shared/abstract-pretty-reporter-DB2G-qlI.d.ts +0 -28
  99. package/dist/packem_shared/constants-DKfCaSUR.js +0 -1
  100. package/dist/packem_shared/format-label-CpyyTBom.js +0 -26
  101. package/dist/packem_shared/get-longest-label-B0NrI-o2.js +0 -1
  102. package/dist/packem_shared/index-CysYvHXs.js +0 -8
  103. package/dist/packem_shared/index-D9hWq9ka.js +0 -1
  104. package/dist/packem_shared/index.d-BR1GjZri.d.ts +0 -53
  105. package/dist/packem_shared/index.d-oxZvg_y7.d.ts +0 -20
  106. package/dist/packem_shared/interactive-stream-hook-CeVo4Kth.js +0 -2
  107. package/dist/packem_shared/pail.browser-BmHoDvEA.js +0 -19
  108. package/dist/packem_shared/pail.browser-CmWcqnn9.d.ts +0 -64
  109. package/dist/packem_shared/types-DVzG8TWL.d.ts +0 -95
  110. package/dist/packem_shared/write-console-log-based-on-level-BP95fgQZ.js +0 -1
  111. package/dist/packem_shared/write-stream-CD8XFv1L.js +0 -1
@@ -0,0 +1,40 @@
1
+ import type { AbstractHttpReporterOptions } from "./abstract-http-reporter.d.ts";
2
+ import { AbstractHttpReporter } from "./abstract-http-reporter.d.ts";
3
+ /**
4
+ * HTTP Reporter for Edge Runtime environments.
5
+ *
6
+ * A reporter optimized for Edge Runtime environments (Next.js Edge, Cloudflare Workers, etc.)
7
+ * that sends logs to HTTP endpoints. Edge compatibility mode is enabled by default.
8
+ * Supports batching, retries, and rate limiting. Compression is disabled for Edge compatibility.
9
+ * @template L - The log level type
10
+ * @example
11
+ * ```typescript
12
+ * import { createPail } from "@visulima/pail";
13
+ * import { HttpReporterEdgeLight } from "@visulima/pail/reporter/http/edge-light";
14
+ *
15
+ * const logger = createPail({
16
+ * reporters: [
17
+ * new HttpReporterEdgeLight({
18
+ * url: "https://api.example.com/logs",
19
+ * method: "POST",
20
+ * headers: {
21
+ * "Authorization": "Bearer token"
22
+ * },
23
+ * enableBatchSend: true,
24
+ * batchSize: 50
25
+ * })
26
+ * ]
27
+ * });
28
+ *
29
+ * logger.info("Edge function started");
30
+ * ```
31
+ */
32
+ declare class HttpReporterEdgeLight<L extends string = string> extends AbstractHttpReporter<L> {
33
+ /**
34
+ * Creates a new HTTP Reporter Edge Light instance.
35
+ * Edge compatibility mode is automatically enabled.
36
+ * @param options Configuration options for HTTP reporting
37
+ */
38
+ constructor(options: Omit<AbstractHttpReporterOptions, "edgeCompat">);
39
+ }
40
+ export default HttpReporterEdgeLight;
@@ -0,0 +1,651 @@
1
+ import { createRequire as __cjs_createRequire } from "node:module";
2
+
3
+ const __cjs_require = __cjs_createRequire(import.meta.url);
4
+
5
+ const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process;
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
+
20
+ const {
21
+ Buffer
22
+ } = __cjs_getBuiltinModule("node:buffer");
23
+ const {
24
+ gzipSync
25
+ } = __cjs_getBuiltinModule("node:zlib");
26
+
27
+ function isPlainObject(value) {
28
+ if (typeof value !== "object" || value === null) {
29
+ return false;
30
+ }
31
+ const prototype = Object.getPrototypeOf(value);
32
+ return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value);
33
+ }
34
+
35
+ const ErrorProto = Object.create(
36
+ {},
37
+ {
38
+ cause: {
39
+ enumerable: true,
40
+ value: void 0,
41
+ writable: true
42
+ },
43
+ code: {
44
+ enumerable: true,
45
+ value: void 0,
46
+ writable: true
47
+ },
48
+ errors: {
49
+ enumerable: true,
50
+ value: void 0,
51
+ writable: true
52
+ },
53
+ message: {
54
+ enumerable: true,
55
+ value: void 0,
56
+ writable: true
57
+ },
58
+ name: {
59
+ enumerable: true,
60
+ value: void 0,
61
+ writable: true
62
+ },
63
+ stack: {
64
+ enumerable: true,
65
+ value: void 0,
66
+ writable: true
67
+ }
68
+ }
69
+ );
70
+ const toJsonWasCalled = /* @__PURE__ */ new WeakSet();
71
+ const toJSON = (from) => {
72
+ toJsonWasCalled.add(from);
73
+ const json = from.toJSON();
74
+ toJsonWasCalled.delete(from);
75
+ return json;
76
+ };
77
+ const serializeValue = (value, seen, depth, options) => {
78
+ if (value && value instanceof Uint8Array && value.constructor.name === "Buffer") {
79
+ return "[object Buffer]";
80
+ }
81
+ if (value !== null && typeof value === "object" && typeof value.pipe === "function") {
82
+ return "[object Stream]";
83
+ }
84
+ if (value instanceof Error) {
85
+ if (seen.includes(value)) {
86
+ return "[Circular]";
87
+ }
88
+ depth += 1;
89
+ return _serialize(value, options, seen, depth);
90
+ }
91
+ if (options.useToJSON && typeof value.toJSON === "function") {
92
+ return value.toJSON();
93
+ }
94
+ if (typeof value === "object" && value instanceof Date) {
95
+ return value.toISOString();
96
+ }
97
+ if (typeof value === "function") {
98
+ return `[Function: ${value.name || "anonymous"}]`;
99
+ }
100
+ if (isPlainObject(value)) {
101
+ depth += 1;
102
+ if (options.maxDepth && depth >= options.maxDepth) {
103
+ return {};
104
+ }
105
+ const plainObject = {};
106
+ for (const key in value) {
107
+ plainObject[key] = serializeValue(value[key], seen, depth, options);
108
+ }
109
+ return plainObject;
110
+ }
111
+ try {
112
+ return value;
113
+ } catch {
114
+ return "[Not Available]";
115
+ }
116
+ };
117
+ const _serialize = (error, options, seen, depth) => {
118
+ seen.push(error);
119
+ if (options.maxDepth === 0) {
120
+ return {};
121
+ }
122
+ if (options.useToJSON && typeof error.toJSON === "function" && !toJsonWasCalled.has(error)) {
123
+ return toJSON(error);
124
+ }
125
+ const protoError = Object.create(ErrorProto);
126
+ protoError.name = Object.prototype.toString.call(error.constructor) === "[object Function]" ? error.constructor.name : error.name;
127
+ protoError.message = error.message;
128
+ protoError.stack = error.stack;
129
+ if (Array.isArray(error.errors)) {
130
+ const aggregateErrors = [];
131
+ for (const aggregateError of error.errors) {
132
+ if (!(aggregateError instanceof Error)) {
133
+ throw new TypeError("All errors in the 'errors' property must be instances of Error");
134
+ }
135
+ if (seen.includes(aggregateError)) {
136
+ protoError.errors = [];
137
+ return protoError;
138
+ }
139
+ aggregateErrors.push(_serialize(aggregateError, options, seen, depth));
140
+ }
141
+ protoError.errors = aggregateErrors;
142
+ }
143
+ if (error.cause instanceof Error && !seen.includes(error.cause)) {
144
+ protoError.cause = _serialize(error.cause, options, seen, depth);
145
+ }
146
+ for (const key in error) {
147
+ if (protoError[key] === void 0) {
148
+ const value = error[key];
149
+ protoError[key] = serializeValue(value, seen, depth, options);
150
+ }
151
+ }
152
+ if (Array.isArray(options.exclude) && options.exclude.length > 0) {
153
+ for (const key of options.exclude) {
154
+ try {
155
+ delete protoError[key];
156
+ } catch {
157
+ }
158
+ }
159
+ }
160
+ return protoError;
161
+ };
162
+ const serialize = (error, options = {}) => _serialize(
163
+ error,
164
+ {
165
+ exclude: options.exclude ?? [],
166
+ maxDepth: options.maxDepth ?? Number.POSITIVE_INFINITY,
167
+ useToJSON: options.useToJSON ?? false
168
+ },
169
+ [],
170
+ 0
171
+ );
172
+
173
+ const EMPTY_SYMBOL = Symbol("EMPTY");
174
+
175
+ class AbstractJsonReporter {
176
+ /** Custom stringify function for object serialization */
177
+ stringify;
178
+ /** Error serialization options */
179
+ errorOptions;
180
+ /**
181
+ * Creates a new AbstractJsonReporter instance.
182
+ * @param options Configuration options for JSON formatting and error handling
183
+ */
184
+ constructor(options = {}) {
185
+ this.errorOptions = options.error ?? {};
186
+ }
187
+ /**
188
+ * Sets a custom stringify function for object serialization.
189
+ * @param function_ The stringify function to use for serialization
190
+ */
191
+ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
192
+ setStringify(function_) {
193
+ this.stringify = function_;
194
+ }
195
+ // eslint-disable-next-line sonarjs/cognitive-complexity
196
+ log(meta) {
197
+ const { context, error, file, message, type, ...rest } = meta;
198
+ if (rest.label) {
199
+ rest.label = rest.label.trim();
200
+ }
201
+ if (file) {
202
+ rest.file = `${file.name}:${file.line}${file.column ? `:${file.column}` : ""}`;
203
+ }
204
+ if (message === EMPTY_SYMBOL) {
205
+ rest.message = void 0;
206
+ } else {
207
+ rest.message = message;
208
+ }
209
+ if (error) {
210
+ rest.error = serialize(error, this.errorOptions);
211
+ }
212
+ if (context) {
213
+ const newContext = [];
214
+ for (const item of context) {
215
+ if (item === EMPTY_SYMBOL) {
216
+ continue;
217
+ }
218
+ if (item instanceof Error) {
219
+ newContext.push(serialize(item, this.errorOptions));
220
+ } else {
221
+ newContext.push(item);
222
+ }
223
+ }
224
+ rest.context = newContext;
225
+ }
226
+ this._log(this.stringify(rest), type.level);
227
+ }
228
+ }
229
+
230
+ const compressData = async (data) => {
231
+ const CompressionStreamClass = globalThis.CompressionStream;
232
+ if (CompressionStreamClass) {
233
+ const stream = new CompressionStreamClass("gzip");
234
+ const writer = stream.writable.getWriter();
235
+ const reader = stream.readable.getReader();
236
+ const encoder = new TextEncoder();
237
+ const chunks = [];
238
+ writer.write(encoder.encode(data));
239
+ writer.close();
240
+ let done = false;
241
+ while (!done) {
242
+ const result2 = await reader.read();
243
+ done = result2.done ?? false;
244
+ if (result2.value) {
245
+ chunks.push(result2.value);
246
+ }
247
+ }
248
+ const totalLength = chunks.reduce((accumulator, chunk) => accumulator + chunk.length, 0);
249
+ const result = new Uint8Array(totalLength);
250
+ let offset = 0;
251
+ for (const chunk of chunks) {
252
+ result.set(chunk, offset);
253
+ offset += chunk.length;
254
+ }
255
+ return result;
256
+ }
257
+ return gzipSync(data);
258
+ };
259
+
260
+ class LogSizeError extends Error {
261
+ /**
262
+ * The log entry data that caused the error
263
+ */
264
+ logData;
265
+ /**
266
+ * The actual size of the log entry in bytes
267
+ */
268
+ actualSize;
269
+ /**
270
+ * The maximum allowed size in bytes
271
+ */
272
+ maxSize;
273
+ /**
274
+ * Creates a new LogSizeError instance.
275
+ * @param message Descriptive error message explaining the size violation
276
+ * @param logData The log entry data that caused the error
277
+ * @param actualSize Size of the log entry in bytes
278
+ * @param maxSize Maximum allowed size in bytes
279
+ * @example
280
+ * ```typescript
281
+ * throw new LogSizeError("Log too large", logData, 2000000, 1000000);
282
+ * ```
283
+ */
284
+ constructor(message, logData, actualSize, maxSize) {
285
+ super(message);
286
+ this.name = "LogSizeError";
287
+ this.logData = logData;
288
+ this.actualSize = actualSize;
289
+ this.maxSize = maxSize;
290
+ }
291
+ }
292
+
293
+ const calculateBackoffDelay = (baseDelay, attempt) => baseDelay * 2 ** attempt;
294
+ const sleep = (delay) => new Promise((resolve) => {
295
+ setTimeout(resolve, delay);
296
+ });
297
+ const prepareRequestBody = (body) => {
298
+ if (typeof body === "string") {
299
+ return body;
300
+ }
301
+ return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
302
+ };
303
+ const processResponse = async (response, url, method, headers, body, onDebugRequestResponse, onError) => {
304
+ const responseHeaders = {};
305
+ response.headers.forEach((value, key) => {
306
+ responseHeaders[key] = value;
307
+ });
308
+ const responseBody = await response.text();
309
+ const requestResponse = {
310
+ req: {
311
+ body,
312
+ headers,
313
+ method,
314
+ url
315
+ },
316
+ res: {
317
+ body: responseBody,
318
+ headers: responseHeaders,
319
+ status: response.status,
320
+ statusText: response.statusText
321
+ }
322
+ };
323
+ if (onDebugRequestResponse) {
324
+ onDebugRequestResponse(requestResponse);
325
+ }
326
+ if (response.ok) {
327
+ return false;
328
+ }
329
+ if (response.status < 500 && response.status !== 429) {
330
+ const error = new Error(`HTTP ${response.status}: ${response.statusText}`);
331
+ if (onError) {
332
+ onError(error);
333
+ }
334
+ throw error;
335
+ }
336
+ return true;
337
+ };
338
+ const handleRateLimit = (response, respectRateLimit, retryDelay, attempt, maxRetries) => {
339
+ if (response.status === 429 && respectRateLimit && attempt < maxRetries) {
340
+ const retryAfter = response.headers.get("retry-after");
341
+ return retryAfter ? Number.parseInt(retryAfter, 10) * 1e3 : calculateBackoffDelay(retryDelay, attempt);
342
+ }
343
+ return void 0;
344
+ };
345
+ const handleServerError = (response, retryDelay, attempt, maxRetries) => {
346
+ if (response.status >= 500 && attempt < maxRetries) {
347
+ return calculateBackoffDelay(retryDelay, attempt);
348
+ }
349
+ return void 0;
350
+ };
351
+ const calculateRetryDelay = (response, respectRateLimit, retryDelay, attempt, maxRetries) => {
352
+ const rateLimitDelay = handleRateLimit(response, respectRateLimit, retryDelay, attempt, maxRetries);
353
+ if (rateLimitDelay !== void 0) {
354
+ return rateLimitDelay;
355
+ }
356
+ return handleServerError(response, retryDelay, attempt, maxRetries);
357
+ };
358
+ const handleRetryError = async (error, attempt, maxRetries, retryDelay, onError) => {
359
+ if (attempt < maxRetries) {
360
+ const delay = calculateBackoffDelay(retryDelay, attempt);
361
+ await sleep(delay);
362
+ return true;
363
+ }
364
+ if (onError) {
365
+ onError(error);
366
+ }
367
+ return false;
368
+ };
369
+ const sendWithRetry = async (url, method, headers, body, maxRetries, retryDelay, respectRateLimit, onDebugRequestResponse, onError) => {
370
+ let attempt = 0;
371
+ while (attempt <= maxRetries) {
372
+ try {
373
+ const requestBody = prepareRequestBody(body);
374
+ const response = await fetch(url, {
375
+ body: requestBody,
376
+ headers,
377
+ method
378
+ });
379
+ const shouldRetry = await processResponse(response, url, method, headers, body, onDebugRequestResponse, onError);
380
+ if (!shouldRetry) {
381
+ return;
382
+ }
383
+ const retryDelayValue = calculateRetryDelay(response, respectRateLimit, retryDelay, attempt, maxRetries);
384
+ if (retryDelayValue !== void 0) {
385
+ await sleep(retryDelayValue);
386
+ attempt += 1;
387
+ continue;
388
+ }
389
+ if (!response.ok) {
390
+ const error = new Error(`HTTP ${response.status}: ${response.statusText}`);
391
+ if (onError) {
392
+ onError(error);
393
+ }
394
+ throw error;
395
+ }
396
+ return;
397
+ } catch (error) {
398
+ const shouldRetry = await handleRetryError(error, attempt, maxRetries, retryDelay, onError);
399
+ if (shouldRetry) {
400
+ attempt += 1;
401
+ continue;
402
+ }
403
+ throw error;
404
+ }
405
+ }
406
+ };
407
+
408
+ class AbstractHttpReporter extends AbstractJsonReporter {
409
+ url;
410
+ method;
411
+ headers;
412
+ contentType;
413
+ batchContentType;
414
+ onError;
415
+ onDebug;
416
+ onDebugRequestResponse;
417
+ payloadTemplate;
418
+ compression;
419
+ maxRetries;
420
+ retryDelay;
421
+ respectRateLimit;
422
+ enableBatchSend;
423
+ batchSize;
424
+ batchSendTimeout;
425
+ batchSendDelimiter;
426
+ batchMode;
427
+ batchFieldName;
428
+ maxLogSize;
429
+ maxPayloadSize;
430
+ edgeCompat;
431
+ // Batch management
432
+ batchQueue = [];
433
+ batchTimeout;
434
+ isProcessingBatch = false;
435
+ currentBatchSize = 0;
436
+ // Track uncompressed size of current batch
437
+ /**
438
+ * Creates a new instance of AbstractHttpReporter.
439
+ * @param config Configuration options for the reporter
440
+ */
441
+ constructor(config) {
442
+ super(config);
443
+ this.url = config.url;
444
+ this.method = config.method ?? "POST";
445
+ this.headers = config.headers ?? {};
446
+ this.contentType = config.contentType ?? "application/json";
447
+ this.batchContentType = config.batchContentType ?? "application/json";
448
+ this.onError = config.onError;
449
+ this.onDebug = config.onDebug;
450
+ this.onDebugRequestResponse = config.onDebugRequestResponse;
451
+ this.payloadTemplate = config.payloadTemplate ?? ((data) => JSON.stringify(data));
452
+ this.compression = config.compression ?? false;
453
+ this.maxRetries = config.maxRetries ?? 3;
454
+ this.retryDelay = config.retryDelay ?? 1e3;
455
+ this.respectRateLimit = config.respectRateLimit ?? true;
456
+ this.enableBatchSend = config.enableBatchSend ?? true;
457
+ this.batchSize = config.batchSize ?? 100;
458
+ this.batchSendTimeout = config.batchSendTimeout ?? 5e3;
459
+ this.batchSendDelimiter = config.batchSendDelimiter ?? "\n";
460
+ this.batchMode = config.batchMode ?? "delimiter";
461
+ this.batchFieldName = config.batchFieldName;
462
+ if (this.batchMode === "field" && !this.batchFieldName) {
463
+ throw new Error("batchFieldName is required when batchMode is 'field'");
464
+ }
465
+ this.maxLogSize = config.maxLogSize ?? 1048576;
466
+ this.maxPayloadSize = config.maxPayloadSize ?? 5242880;
467
+ this.edgeCompat = config.edgeCompat ?? false;
468
+ }
469
+ /**
470
+ * Processes and ships log entries to the HTTP endpoint.
471
+ * @param message The JSON-formatted log message
472
+ * @param logLevel The log level of the message
473
+ * @protected
474
+ */
475
+ // eslint-disable-next-line no-underscore-dangle
476
+ _log(message, logLevel) {
477
+ try {
478
+ const logData = JSON.parse(message);
479
+ const logLevelString = logLevel;
480
+ const messageString = logData.message;
481
+ const payloadTemplate = {
482
+ logLevel: logLevelString,
483
+ message: messageString ?? ""
484
+ };
485
+ if (logData.data) {
486
+ payloadTemplate["data"] = logData.data;
487
+ } else if (logData.context) {
488
+ payloadTemplate["data"] = logData.context;
489
+ } else {
490
+ const rest = { ...logData };
491
+ delete rest.message;
492
+ if (Object.keys(rest).length > 0) {
493
+ payloadTemplate["data"] = rest;
494
+ }
495
+ }
496
+ const payload = this.payloadTemplate(payloadTemplate);
497
+ if (this.onDebug) {
498
+ this.onDebug({ data: payloadTemplate.data, logLevel: logLevelString, message: messageString });
499
+ }
500
+ const logEntrySize = this.edgeCompat || typeof TextEncoder === "undefined" ? Buffer.byteLength(payload, "utf8") : new TextEncoder().encode(payload).length;
501
+ if (logEntrySize > this.maxLogSize) {
502
+ const error = new LogSizeError(
503
+ `Log entry exceeds maximum size of ${this.maxLogSize} bytes. Size: ${logEntrySize} bytes`,
504
+ { data: payloadTemplate.data, logLevel: logLevelString, message: messageString },
505
+ logEntrySize,
506
+ this.maxLogSize
507
+ );
508
+ if (this.onError) {
509
+ this.onError(error);
510
+ }
511
+ return;
512
+ }
513
+ if (this.enableBatchSend) {
514
+ this.addToBatch(payload, logEntrySize);
515
+ } else {
516
+ this.sendPayload(payload).catch((error) => {
517
+ if (this.onError) {
518
+ this.onError(error);
519
+ }
520
+ });
521
+ }
522
+ } catch (error) {
523
+ if (this.onError) {
524
+ this.onError(error);
525
+ }
526
+ }
527
+ }
528
+ /**
529
+ * Adds a payload to the batch queue and triggers sending if conditions are met.
530
+ */
531
+ addToBatch(payload, logEntrySize) {
532
+ const payloadSizeWithEntry = this.currentBatchSize + logEntrySize + this.batchSendDelimiter.length;
533
+ const payloadSizeThreshold = this.maxPayloadSize * 0.9;
534
+ if (payloadSizeWithEntry > payloadSizeThreshold && this.batchQueue.length > 0) {
535
+ this.processBatch();
536
+ }
537
+ this.batchQueue.push(payload);
538
+ this.currentBatchSize += logEntrySize + this.batchSendDelimiter.length;
539
+ if (!this.batchTimeout) {
540
+ this.batchTimeout = setTimeout(() => {
541
+ this.processBatch();
542
+ }, this.batchSendTimeout);
543
+ }
544
+ if (this.batchQueue.length >= this.batchSize) {
545
+ this.processBatch();
546
+ }
547
+ }
548
+ /**
549
+ * Processes the current batch and sends it to the HTTP endpoint.
550
+ */
551
+ async processBatch() {
552
+ if (this.isProcessingBatch || this.batchQueue.length === 0) {
553
+ return;
554
+ }
555
+ this.isProcessingBatch = true;
556
+ if (this.batchTimeout) {
557
+ clearTimeout(this.batchTimeout);
558
+ this.batchTimeout = void 0;
559
+ }
560
+ const batch = this.batchQueue.splice(0, this.batchSize);
561
+ this.currentBatchSize = 0;
562
+ for (let i = 0; i < this.batchQueue.length; i += 1) {
563
+ const payload = this.batchQueue[i];
564
+ const payloadSize = this.edgeCompat || typeof TextEncoder === "undefined" ? Buffer.byteLength(payload, "utf8") : new TextEncoder().encode(payload).length;
565
+ this.currentBatchSize += payloadSize + (i < this.batchQueue.length - 1 ? this.batchSendDelimiter.length : 0);
566
+ }
567
+ try {
568
+ await this.sendBatch(batch);
569
+ } catch (error) {
570
+ if (this.onError) {
571
+ this.onError(error);
572
+ }
573
+ } finally {
574
+ this.isProcessingBatch = false;
575
+ if (this.batchQueue.length > 0) {
576
+ this.processBatch();
577
+ }
578
+ }
579
+ }
580
+ /**
581
+ * Sends a batch of payloads to the HTTP endpoint.
582
+ */
583
+ async sendBatch(batch) {
584
+ let batchPayload;
585
+ switch (this.batchMode) {
586
+ case "array": {
587
+ const batchEntries = batch.map((payload) => JSON.parse(payload));
588
+ batchPayload = JSON.stringify(batchEntries);
589
+ break;
590
+ }
591
+ case "field": {
592
+ const fieldEntries = batch.map((payload) => JSON.parse(payload));
593
+ const batchObject = this.batchFieldName ? { [this.batchFieldName]: fieldEntries } : {};
594
+ batchPayload = JSON.stringify(batchObject);
595
+ break;
596
+ }
597
+ default: {
598
+ batchPayload = batch.join(this.batchSendDelimiter);
599
+ break;
600
+ }
601
+ }
602
+ await this.sendPayload(batchPayload, this.batchContentType);
603
+ }
604
+ /**
605
+ * Sends a single payload to the HTTP endpoint.
606
+ */
607
+ async sendPayload(payload, contentType) {
608
+ const headers = typeof this.headers === "function" ? this.headers() : { ...this.headers };
609
+ if (!headers["content-type"]) {
610
+ headers["content-type"] = contentType ?? this.contentType;
611
+ }
612
+ let finalPayload = payload;
613
+ if (this.compression && !this.edgeCompat) {
614
+ try {
615
+ finalPayload = await compressData(payload);
616
+ headers["content-encoding"] = "gzip";
617
+ } catch (error) {
618
+ if (this.onError) {
619
+ this.onError(new Error(`Compression failed: ${error}`));
620
+ }
621
+ }
622
+ }
623
+ await sendWithRetry(
624
+ this.url,
625
+ this.method,
626
+ headers,
627
+ finalPayload,
628
+ this.maxRetries,
629
+ this.retryDelay,
630
+ this.respectRateLimit,
631
+ this.onDebugRequestResponse,
632
+ this.onError
633
+ );
634
+ }
635
+ }
636
+
637
+ class HttpReporterEdgeLight extends AbstractHttpReporter {
638
+ /**
639
+ * Creates a new HTTP Reporter Edge Light instance.
640
+ * Edge compatibility mode is automatically enabled.
641
+ * @param options Configuration options for HTTP reporting
642
+ */
643
+ constructor(options) {
644
+ super({
645
+ ...options,
646
+ edgeCompat: true
647
+ });
648
+ }
649
+ }
650
+
651
+ export { HttpReporterEdgeLight as default };
@@ -0,0 +1,13 @@
1
+ import { AbstractHttpReporter } from './abstract-http-reporter.js';
2
+
3
+ class HttpReporter extends AbstractHttpReporter {
4
+ /**
5
+ * Creates a new HTTP Reporter instance.
6
+ * @param options Configuration options for HTTP reporting
7
+ */
8
+ constructor(options) {
9
+ super(options);
10
+ }
11
+ }
12
+
13
+ export { HttpReporter as default };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Compresses data using gzip compression.
3
+ * @param data The data to compress as a string
4
+ * @returns A Promise that resolves to the compressed data as Uint8Array
5
+ */
6
+ declare const compressData: (data: string) => Promise<Uint8Array>;
7
+ export default compressData;