@visulima/pail 4.0.0-alpha.12 → 4.0.0-alpha.14

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 (85) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/LICENSE.md +4 -1
  3. package/dist/error.js +5 -76
  4. package/dist/index.browser.js +19 -1499
  5. package/dist/index.server.js +31 -2804
  6. package/dist/middleware/elysia.d.ts +3 -3
  7. package/dist/middleware/elysia.js +1 -70
  8. package/dist/middleware/express.d.ts +3 -3
  9. package/dist/middleware/express.js +1 -29
  10. package/dist/middleware/fastify.d.ts +3 -3
  11. package/dist/middleware/fastify.js +1 -46
  12. package/dist/middleware/hono.d.ts +3 -3
  13. package/dist/middleware/hono.js +1 -33
  14. package/dist/middleware/next/handler.d.ts +5 -5
  15. package/dist/middleware/next/handler.js +1 -53
  16. package/dist/middleware/sveltekit.d.ts +3 -3
  17. package/dist/middleware/sveltekit.js +1 -43
  18. package/dist/object-tree.js +2 -89
  19. package/dist/packem_shared/AbstractJsonReporter-D_Pb6kpB.js +1 -0
  20. package/dist/packem_shared/AbstractJsonReporter-PVC4ONf2.js +1 -0
  21. package/dist/packem_shared/JsonReporter-BEEmb0W4.js +2 -0
  22. package/dist/packem_shared/JsonReporter-CE-lw7R0.js +1 -0
  23. package/dist/packem_shared/PrettyReporter-Br5njEEy.js +5 -0
  24. package/dist/packem_shared/PrettyReporter-N7FvHzl5.js +44 -0
  25. package/dist/packem_shared/{abstract-json-reporter.d-BAgznjyU.d.ts → abstract-json-reporter.d-Boj5w068.d.ts} +1 -1
  26. package/dist/packem_shared/abstract-pretty-reporter-Bag7lQKf.js +33 -0
  27. package/dist/packem_shared/constants-DKfCaSUR.js +1 -0
  28. package/dist/packem_shared/constants-ep2nsfdy.js +1 -0
  29. package/dist/packem_shared/{create-middleware-logger.d-DheMh8q4.d.ts → create-middleware-logger.d-CMQdVeVE.d.ts} +2 -2
  30. package/dist/packem_shared/createPailError-53jAfQ-T.js +5 -0
  31. package/dist/packem_shared/headers-S8WyX-2m.js +1 -0
  32. package/dist/packem_shared/index-Bzp4LnJ5.js +1 -0
  33. package/dist/packem_shared/pailMiddleware-CWOJRmJq.js +1 -0
  34. package/dist/packem_shared/storage-DHM1EP2P.js +1 -0
  35. package/dist/packem_shared/{types.d-BeLumqgD.d.ts → types.d-Ck_RSjLg.d.ts} +1 -1
  36. package/dist/packem_shared/useLogger-qyZrjy4D.js +1 -0
  37. package/dist/packem_shared/{wide-event.d-B-t8ZnhI.d.ts → wide-event.d-DA-6dTPc.d.ts} +1 -1
  38. package/dist/packem_shared/write-console-log-based-on-level-DfD64owL.js +1 -0
  39. package/dist/packem_shared/write-stream-Dd4_PTcm.js +1 -0
  40. package/dist/processor/caller/caller-processor.d.ts +1 -1
  41. package/dist/processor/caller/caller-processor.js +1 -59
  42. package/dist/processor/environment-processor.d.ts +1 -1
  43. package/dist/processor/environment-processor.js +1 -82
  44. package/dist/processor/message-formatter-processor.d.ts +1 -1
  45. package/dist/processor/message-formatter-processor.js +1 -715
  46. package/dist/processor/opentelemetry-processor.d.ts +1 -1
  47. package/dist/processor/opentelemetry-processor.js +1 -52
  48. package/dist/processor/redact-processor.d.ts +1 -1
  49. package/dist/processor/redact-processor.js +1 -31
  50. package/dist/processor/sampling-processor.d.ts +1 -1
  51. package/dist/processor/sampling-processor.js +1 -59
  52. package/dist/reporter/file/json-file-reporter.d.ts +2 -2
  53. package/dist/reporter/file/json-file-reporter.js +2 -136
  54. package/dist/reporter/http/abstract-http-reporter.d.ts +2 -2
  55. package/dist/reporter/http/abstract-http-reporter.js +2 -432
  56. package/dist/reporter/http/http-reporter.d.ts +2 -2
  57. package/dist/reporter/http/http-reporter.edge-light.js +2 -728
  58. package/dist/reporter/http/http-reporter.js +1 -13
  59. package/dist/reporter/json/index.browser.js +1 -2
  60. package/dist/reporter/json/index.d.ts +2 -2
  61. package/dist/reporter/json/index.js +1 -2
  62. package/dist/reporter/pretty/index.browser.js +1 -1
  63. package/dist/reporter/pretty/index.d.ts +1 -1
  64. package/dist/reporter/pretty/index.js +1 -1
  65. package/dist/reporter/simple/simple-reporter.server.js +8 -183
  66. package/dist/wide-event.d.ts +2 -2
  67. package/dist/wide-event.js +1 -284
  68. package/package.json +24 -24
  69. package/dist/packem_shared/AbstractJsonReporter-BO8Calb4.js +0 -284
  70. package/dist/packem_shared/AbstractJsonReporter-nOj0Ft1F.js +0 -284
  71. package/dist/packem_shared/JsonReporter-CCmj7oYL.js +0 -28
  72. package/dist/packem_shared/JsonReporter-Ck2PIAEw.js +0 -58
  73. package/dist/packem_shared/PrettyReporter-CIbrmjUV.js +0 -213
  74. package/dist/packem_shared/PrettyReporter-CNJEO9g7.js +0 -2723
  75. package/dist/packem_shared/abstract-pretty-reporter-CkqCt5hg.js +0 -2637
  76. package/dist/packem_shared/constants-B1RjD_ps.js +0 -99
  77. package/dist/packem_shared/constants-omsTHUWB.js +0 -119
  78. package/dist/packem_shared/createPailError-B_sgL0nF.js +0 -76
  79. package/dist/packem_shared/headers-BxHWM6KI.js +0 -127
  80. package/dist/packem_shared/index-6cG1Kp0t.js +0 -658
  81. package/dist/packem_shared/pailMiddleware-Ci88geIF.js +0 -24
  82. package/dist/packem_shared/storage-D0vqz8OX.js +0 -36
  83. package/dist/packem_shared/useLogger-D0rU3lcX.js +0 -33
  84. package/dist/packem_shared/write-console-log-based-on-level-ree2lDPw.js +0 -15
  85. package/dist/packem_shared/write-stream-MDqyXmc_.js +0 -6
@@ -1,432 +1,2 @@
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
- import { AbstractJsonReporter } from '../../packem_shared/AbstractJsonReporter-nOj0Ft1F.js';
24
- const {
25
- gzipSync
26
- } = __cjs_getBuiltinModule("node:zlib");
27
-
28
- const compressData = async (data) => {
29
- const CompressionStreamClass = globalThis.CompressionStream;
30
- if (CompressionStreamClass) {
31
- const stream = new CompressionStreamClass("gzip");
32
- const writer = stream.writable.getWriter();
33
- const reader = stream.readable.getReader();
34
- const encoder = new TextEncoder();
35
- const chunks = [];
36
- await writer.write(encoder.encode(data));
37
- await writer.close();
38
- let done = false;
39
- while (!done) {
40
- const result = await reader.read();
41
- done = result.done;
42
- if (result.value) {
43
- chunks.push(result.value);
44
- }
45
- }
46
- const totalLength = chunks.reduce((accumulator, chunk) => accumulator + chunk.length, 0);
47
- const resultBuffer = new Uint8Array(totalLength);
48
- let offset = 0;
49
- for (let i = 0; i < chunks.length; i += 1) {
50
- const chunk = chunks[i];
51
- resultBuffer.set(chunk, offset);
52
- offset += chunk.length;
53
- }
54
- return resultBuffer;
55
- }
56
- return gzipSync(data);
57
- };
58
-
59
- class LogSizeError extends Error {
60
- /**
61
- * The log entry data that caused the error
62
- */
63
- logData;
64
- /**
65
- * The actual size of the log entry in bytes
66
- */
67
- actualSize;
68
- /**
69
- * The maximum allowed size in bytes
70
- */
71
- maxSize;
72
- /**
73
- * Creates a new LogSizeError instance.
74
- * @param message Descriptive error message explaining the size violation
75
- * @param logData The log entry data that caused the error
76
- * @param actualSize Size of the log entry in bytes
77
- * @param maxSize Maximum allowed size in bytes
78
- * @example
79
- * ```typescript
80
- * throw new LogSizeError("Log too large", logData, 2000000, 1000000);
81
- * ```
82
- */
83
- constructor(message, logData, actualSize, maxSize) {
84
- super(message);
85
- this.name = "LogSizeError";
86
- this.logData = logData;
87
- this.actualSize = actualSize;
88
- this.maxSize = maxSize;
89
- }
90
- }
91
-
92
- const calculateBackoffDelay = (baseDelay, attempt) => baseDelay * 2 ** attempt;
93
- const sleep = (delay) => new Promise((resolve) => {
94
- setTimeout(resolve, delay);
95
- });
96
- const prepareRequestBody = (body) => {
97
- if (typeof body === "string") {
98
- return body;
99
- }
100
- return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
101
- };
102
- const processResponse = async (response, url, method, headers, body, onDebugRequestResponse, onError) => {
103
- const responseHeaders = {};
104
- response.headers.forEach((value, key) => {
105
- responseHeaders[key] = value;
106
- });
107
- const responseBody = await response.text();
108
- const requestResponse = {
109
- req: {
110
- body,
111
- headers,
112
- method,
113
- url
114
- },
115
- res: {
116
- body: responseBody,
117
- headers: responseHeaders,
118
- status: response.status,
119
- statusText: response.statusText
120
- }
121
- };
122
- if (onDebugRequestResponse) {
123
- onDebugRequestResponse(requestResponse);
124
- }
125
- if (response.ok) {
126
- return false;
127
- }
128
- if (response.status < 500 && response.status !== 429) {
129
- const error = new Error(`HTTP ${String(response.status)}: ${response.statusText}`);
130
- if (onError) {
131
- onError(error);
132
- }
133
- throw error;
134
- }
135
- return true;
136
- };
137
- const handleRateLimit = (response, respectRateLimit, retryDelay, attempt, maxRetries) => {
138
- if (response.status === 429 && respectRateLimit && attempt < maxRetries) {
139
- const retryAfter = response.headers.get("retry-after");
140
- return retryAfter ? Number.parseInt(retryAfter, 10) * 1e3 : calculateBackoffDelay(retryDelay, attempt);
141
- }
142
- return void 0;
143
- };
144
- const handleServerError = (response, retryDelay, attempt, maxRetries) => {
145
- if (response.status >= 500 && attempt < maxRetries) {
146
- return calculateBackoffDelay(retryDelay, attempt);
147
- }
148
- return void 0;
149
- };
150
- const calculateRetryDelay = (response, respectRateLimit, retryDelay, attempt, maxRetries) => {
151
- const rateLimitDelay = handleRateLimit(response, respectRateLimit, retryDelay, attempt, maxRetries);
152
- if (rateLimitDelay !== void 0) {
153
- return rateLimitDelay;
154
- }
155
- return handleServerError(response, retryDelay, attempt, maxRetries);
156
- };
157
- const handleRetryError = async (error, attempt, maxRetries, retryDelay, onError) => {
158
- if (attempt < maxRetries) {
159
- const delay = calculateBackoffDelay(retryDelay, attempt);
160
- await sleep(delay);
161
- return true;
162
- }
163
- if (onError) {
164
- onError(error);
165
- }
166
- return false;
167
- };
168
- const sendWithRetry = async (url, method, headers, body, maxRetries, retryDelay, respectRateLimit, onDebugRequestResponse, onError) => {
169
- let attempt = 0;
170
- while (attempt <= maxRetries) {
171
- try {
172
- const requestBody = prepareRequestBody(body);
173
- const response = await fetch(url, {
174
- body: requestBody,
175
- headers,
176
- method
177
- });
178
- const shouldRetry = await processResponse(response, url, method, headers, body, onDebugRequestResponse, onError);
179
- if (!shouldRetry) {
180
- return;
181
- }
182
- const retryDelayValue = calculateRetryDelay(response, respectRateLimit, retryDelay, attempt, maxRetries);
183
- if (retryDelayValue !== void 0) {
184
- await sleep(retryDelayValue);
185
- attempt += 1;
186
- continue;
187
- }
188
- if (!response.ok) {
189
- const error = new Error(`HTTP ${String(response.status)}: ${response.statusText}`);
190
- if (onError) {
191
- onError(error);
192
- }
193
- throw error;
194
- }
195
- return;
196
- } catch (error) {
197
- const shouldRetry = await handleRetryError(error, attempt, maxRetries, retryDelay, onError);
198
- if (shouldRetry) {
199
- attempt += 1;
200
- continue;
201
- }
202
- throw error;
203
- }
204
- }
205
- };
206
-
207
- class AbstractHttpReporter extends AbstractJsonReporter {
208
- url;
209
- method;
210
- headers;
211
- contentType;
212
- batchContentType;
213
- onError;
214
- onDebug;
215
- onDebugRequestResponse;
216
- payloadTemplate;
217
- compression;
218
- maxRetries;
219
- retryDelay;
220
- respectRateLimit;
221
- enableBatchSend;
222
- batchSize;
223
- batchSendTimeout;
224
- batchSendDelimiter;
225
- batchMode;
226
- batchFieldName;
227
- maxLogSize;
228
- maxPayloadSize;
229
- edgeCompat;
230
- // Batch management
231
- batchQueue = [];
232
- batchTimeout;
233
- isProcessingBatch = false;
234
- currentBatchSize = 0;
235
- // Track uncompressed size of current batch
236
- /**
237
- * Creates a new instance of AbstractHttpReporter.
238
- * @param config Configuration options for the reporter
239
- */
240
- constructor(config) {
241
- super(config);
242
- this.url = config.url;
243
- this.method = config.method ?? "POST";
244
- this.headers = config.headers ?? {};
245
- this.contentType = config.contentType ?? "application/json";
246
- this.batchContentType = config.batchContentType ?? "application/json";
247
- this.onError = config.onError;
248
- this.onDebug = config.onDebug;
249
- this.onDebugRequestResponse = config.onDebugRequestResponse;
250
- this.payloadTemplate = config.payloadTemplate ?? ((data) => JSON.stringify(data));
251
- this.compression = config.compression ?? false;
252
- this.maxRetries = config.maxRetries ?? 3;
253
- this.retryDelay = config.retryDelay ?? 1e3;
254
- this.respectRateLimit = config.respectRateLimit ?? true;
255
- this.enableBatchSend = config.enableBatchSend ?? true;
256
- this.batchSize = config.batchSize ?? 100;
257
- this.batchSendTimeout = config.batchSendTimeout ?? 5e3;
258
- this.batchSendDelimiter = config.batchSendDelimiter ?? "\n";
259
- this.batchMode = config.batchMode ?? "delimiter";
260
- this.batchFieldName = config.batchFieldName;
261
- if (this.batchMode === "field" && !this.batchFieldName) {
262
- throw new Error("batchFieldName is required when batchMode is 'field'");
263
- }
264
- this.maxLogSize = config.maxLogSize ?? 1048576;
265
- this.maxPayloadSize = config.maxPayloadSize ?? 5242880;
266
- this.edgeCompat = config.edgeCompat ?? false;
267
- }
268
- /**
269
- * Processes and ships log entries to the HTTP endpoint.
270
- * @param message The JSON-formatted log message
271
- * @param logLevel The log level of the message
272
- * @protected
273
- */
274
- // eslint-disable-next-line no-underscore-dangle
275
- _log(message, logLevel) {
276
- try {
277
- const logData = JSON.parse(message);
278
- const logLevelString = logLevel;
279
- const messageString = logData.message;
280
- const payloadTemplate = {
281
- logLevel: logLevelString,
282
- message: messageString ?? ""
283
- };
284
- if (logData.data) {
285
- payloadTemplate["data"] = logData.data;
286
- } else if (logData.context) {
287
- payloadTemplate["data"] = logData.context;
288
- } else {
289
- const rest = { ...logData };
290
- delete rest.message;
291
- if (Object.keys(rest).length > 0) {
292
- payloadTemplate["data"] = rest;
293
- }
294
- }
295
- const payload = this.payloadTemplate(payloadTemplate);
296
- if (this.onDebug) {
297
- this.onDebug({ data: payloadTemplate.data, logLevel: logLevelString, message: messageString });
298
- }
299
- const logEntrySize = this.edgeCompat || typeof TextEncoder === "undefined" ? Buffer.byteLength(payload, "utf8") : new TextEncoder().encode(payload).length;
300
- if (logEntrySize > this.maxLogSize) {
301
- const error = new LogSizeError(
302
- `Log entry exceeds maximum size of ${String(this.maxLogSize)} bytes. Size: ${String(logEntrySize)} bytes`,
303
- { data: payloadTemplate.data, logLevel: logLevelString, message: messageString },
304
- logEntrySize,
305
- this.maxLogSize
306
- );
307
- if (this.onError) {
308
- this.onError(error);
309
- }
310
- return;
311
- }
312
- if (this.enableBatchSend) {
313
- this.addToBatch(payload, logEntrySize);
314
- } else {
315
- this.sendPayload(payload).catch((error) => {
316
- if (this.onError) {
317
- this.onError(error);
318
- }
319
- });
320
- }
321
- } catch (error) {
322
- if (this.onError) {
323
- this.onError(error);
324
- }
325
- }
326
- }
327
- /**
328
- * Adds a payload to the batch queue and triggers sending if conditions are met.
329
- */
330
- addToBatch(payload, logEntrySize) {
331
- const payloadSizeWithEntry = this.currentBatchSize + logEntrySize + this.batchSendDelimiter.length;
332
- const payloadSizeThreshold = this.maxPayloadSize * 0.9;
333
- if (payloadSizeWithEntry > payloadSizeThreshold && this.batchQueue.length > 0) {
334
- void this.processBatch();
335
- }
336
- this.batchQueue.push(payload);
337
- this.currentBatchSize += logEntrySize + this.batchSendDelimiter.length;
338
- this.batchTimeout ??= setTimeout(() => {
339
- void this.processBatch();
340
- }, this.batchSendTimeout);
341
- if (this.batchQueue.length >= this.batchSize) {
342
- void this.processBatch();
343
- }
344
- }
345
- /**
346
- * Processes the current batch and sends it to the HTTP endpoint.
347
- */
348
- async processBatch() {
349
- if (this.isProcessingBatch || this.batchQueue.length === 0) {
350
- return;
351
- }
352
- this.isProcessingBatch = true;
353
- if (this.batchTimeout) {
354
- clearTimeout(this.batchTimeout);
355
- this.batchTimeout = void 0;
356
- }
357
- const batch = this.batchQueue.splice(0, this.batchSize);
358
- this.currentBatchSize = 0;
359
- for (let i = 0; i < this.batchQueue.length; i += 1) {
360
- const payload = this.batchQueue[i];
361
- const payloadSize = this.edgeCompat || typeof TextEncoder === "undefined" ? Buffer.byteLength(payload, "utf8") : new TextEncoder().encode(payload).length;
362
- this.currentBatchSize += payloadSize + (i < this.batchQueue.length - 1 ? this.batchSendDelimiter.length : 0);
363
- }
364
- try {
365
- await this.sendBatch(batch);
366
- } catch (error) {
367
- if (this.onError) {
368
- this.onError(error);
369
- }
370
- } finally {
371
- this.isProcessingBatch = false;
372
- if (this.batchQueue.length > 0) {
373
- void this.processBatch();
374
- }
375
- }
376
- }
377
- /**
378
- * Sends a batch of payloads to the HTTP endpoint.
379
- */
380
- async sendBatch(batch) {
381
- let batchPayload;
382
- switch (this.batchMode) {
383
- case "array": {
384
- const batchEntries = batch.map((payload) => JSON.parse(payload));
385
- batchPayload = JSON.stringify(batchEntries);
386
- break;
387
- }
388
- case "field": {
389
- const fieldEntries = batch.map((payload) => JSON.parse(payload));
390
- const batchObject = this.batchFieldName ? { [this.batchFieldName]: fieldEntries } : {};
391
- batchPayload = JSON.stringify(batchObject);
392
- break;
393
- }
394
- default: {
395
- batchPayload = batch.join(this.batchSendDelimiter);
396
- break;
397
- }
398
- }
399
- await this.sendPayload(batchPayload, this.batchContentType);
400
- }
401
- /**
402
- * Sends a single payload to the HTTP endpoint.
403
- */
404
- async sendPayload(payload, contentType) {
405
- const headers = typeof this.headers === "function" ? this.headers() : { ...this.headers };
406
- headers["content-type"] ??= contentType ?? this.contentType;
407
- let finalPayload = payload;
408
- if (this.compression && !this.edgeCompat) {
409
- try {
410
- finalPayload = await compressData(payload);
411
- headers["content-encoding"] = "gzip";
412
- } catch (error) {
413
- if (this.onError) {
414
- this.onError(new Error(`Compression failed: ${String(error)}`));
415
- }
416
- }
417
- }
418
- await sendWithRetry(
419
- this.url,
420
- this.method,
421
- headers,
422
- finalPayload,
423
- this.maxRetries,
424
- this.retryDelay,
425
- this.respectRateLimit,
426
- this.onDebugRequestResponse,
427
- this.onError
428
- );
429
- }
430
- }
431
-
432
- export { AbstractHttpReporter };
1
+ var w=Object.defineProperty;var m=(t,e)=>w(t,"name",{value:e,configurable:!0});import{createRequire as x}from"node:module";import{AbstractJsonReporter as E}from"../../packem_shared/AbstractJsonReporter-PVC4ONf2.js";const z=x(import.meta.url),p=typeof globalThis<"u"&&typeof globalThis.process<"u"?globalThis.process:process,S=m(t=>{if(typeof p<"u"&&p.versions&&p.versions.node){const[e,s]=p.versions.node.split(".").map(Number);if(e>22||e===22&&s>=3||e===20&&s>=16)return p.getBuiltinModule(t)}return z(t)},"__cjs_getBuiltinModule"),{Buffer:y}=S("node:buffer"),{gzipSync:R}=S("node:zlib");var B=Object.defineProperty,v=m((t,e)=>B(t,"name",{value:e,configurable:!0}),"d");const D=v(async t=>{const e=globalThis.CompressionStream;if(e){const s=new e("gzip"),a=s.writable.getWriter(),i=s.readable.getReader(),r=new TextEncoder,h=[];await a.write(r.encode(t)),await a.close();let u=!1;for(;!u;){const n=await i.read();u=n.done,n.value&&h.push(n.value)}const c=h.reduce((n,b)=>n+b.length,0),o=new Uint8Array(c);let l=0;for(let n=0;n<h.length;n+=1){const b=h[n];o.set(b,l),l+=b.length}return o}return R(t)},"compressData");var P=Object.defineProperty,L=m((t,e)=>P(t,"name",{value:e,configurable:!0}),"n");class N extends Error{static{m(this,"u")}static{L(this,"LogSizeError")}logData;actualSize;maxSize;constructor(e,s,a,i){super(e),this.name="LogSizeError",this.logData=s,this.actualSize=a,this.maxSize=i}}var j=Object.defineProperty,d=m((t,e)=>j(t,"name",{value:e,configurable:!0}),"o");const g=d((t,e)=>t*2**e,"calculateBackoffDelay"),T=d(t=>new Promise(e=>{setTimeout(e,t)}),"sleep"),O=d(t=>typeof t=="string"?t:new Uint8Array(t.buffer,t.byteOffset,t.byteLength),"prepareRequestBody"),_=d(async(t,e,s,a,i,r,h)=>{const u={};t.headers.forEach((l,n)=>{u[n]=l});const c=await t.text(),o={req:{body:i,headers:a,method:s,url:e},res:{body:c,headers:u,status:t.status,statusText:t.statusText}};if(r&&r(o),t.ok)return!1;if(t.status<500&&t.status!==429){const l=new Error(`HTTP ${String(t.status)}: ${t.statusText}`);throw h&&h(l),l}return!0},"processResponse"),C=d((t,e,s,a,i)=>{if(t.status===429&&e&&a<i){const r=t.headers.get("retry-after");return r?Number.parseInt(r,10)*1e3:g(s,a)}},"handleRateLimit"),q=d((t,e,s,a)=>{if(t.status>=500&&s<a)return g(e,s)},"handleServerError"),Q=d((t,e,s,a,i)=>{const r=C(t,e,s,a,i);return r!==void 0?r:q(t,s,a,i)},"calculateRetryDelay"),k=d(async(t,e,s,a,i)=>{if(e<s){const r=g(a,e);return await T(r),!0}return i&&i(t),!1},"handleRetryError"),M=d(async(t,e,s,a,i,r,h,u,c)=>{let o=0;for(;o<=i;)try{const l=O(a),n=await fetch(t,{body:l,headers:s,method:e});if(!await _(n,t,e,s,a,u,c))return;const b=Q(n,h,r,o,i);if(b!==void 0){await T(b),o+=1;continue}if(!n.ok){const f=new Error(`HTTP ${String(n.status)}: ${n.statusText}`);throw c&&c(f),f}return}catch(l){if(await k(l,o,i,r,c)){o+=1;continue}throw l}},"sendWithRetry");var F=Object.defineProperty,J=m((t,e)=>F(t,"name",{value:e,configurable:!0}),"c");class U extends E{static{m(this,"AbstractHttpReporter")}static{J(this,"AbstractHttpReporter")}url;method;headers;contentType;batchContentType;onError;onDebug;onDebugRequestResponse;payloadTemplate;compression;maxRetries;retryDelay;respectRateLimit;enableBatchSend;batchSize;batchSendTimeout;batchSendDelimiter;batchMode;batchFieldName;maxLogSize;maxPayloadSize;edgeCompat;batchQueue=[];batchTimeout;isProcessingBatch=!1;currentBatchSize=0;constructor(e){if(super(e),this.url=e.url,this.method=e.method??"POST",this.headers=e.headers??{},this.contentType=e.contentType??"application/json",this.batchContentType=e.batchContentType??"application/json",this.onError=e.onError,this.onDebug=e.onDebug,this.onDebugRequestResponse=e.onDebugRequestResponse,this.payloadTemplate=e.payloadTemplate??(s=>JSON.stringify(s)),this.compression=e.compression??!1,this.maxRetries=e.maxRetries??3,this.retryDelay=e.retryDelay??1e3,this.respectRateLimit=e.respectRateLimit??!0,this.enableBatchSend=e.enableBatchSend??!0,this.batchSize=e.batchSize??100,this.batchSendTimeout=e.batchSendTimeout??5e3,this.batchSendDelimiter=e.batchSendDelimiter??`
2
+ `,this.batchMode=e.batchMode??"delimiter",this.batchFieldName=e.batchFieldName,this.batchMode==="field"&&!this.batchFieldName)throw new Error("batchFieldName is required when batchMode is 'field'");this.maxLogSize=e.maxLogSize??1048576,this.maxPayloadSize=e.maxPayloadSize??5242880,this.edgeCompat=e.edgeCompat??!1}_log(e,s){try{const a=JSON.parse(e),i=s,r=a.message,h={logLevel:i,message:r??""};if(a.data)h.data=a.data;else if(a.context)h.data=a.context;else{const o={...a};delete o.message,Object.keys(o).length>0&&(h.data=o)}const u=this.payloadTemplate(h);this.onDebug&&this.onDebug({data:h.data,logLevel:i,message:r});const c=this.edgeCompat||typeof TextEncoder>"u"?y.byteLength(u,"utf8"):new TextEncoder().encode(u).length;if(c>this.maxLogSize){const o=new N(`Log entry exceeds maximum size of ${String(this.maxLogSize)} bytes. Size: ${String(c)} bytes`,{data:h.data,logLevel:i,message:r},c,this.maxLogSize);this.onError&&this.onError(o);return}this.enableBatchSend?this.addToBatch(u,c):this.sendPayload(u).catch(o=>{this.onError&&this.onError(o)})}catch(a){this.onError&&this.onError(a)}}addToBatch(e,s){const a=this.currentBatchSize+s+this.batchSendDelimiter.length,i=this.maxPayloadSize*.9;a>i&&this.batchQueue.length>0&&this.processBatch(),this.batchQueue.push(e),this.currentBatchSize+=s+this.batchSendDelimiter.length,this.batchTimeout??=setTimeout(()=>{this.processBatch()},this.batchSendTimeout),this.batchQueue.length>=this.batchSize&&this.processBatch()}async processBatch(){if(this.isProcessingBatch||this.batchQueue.length===0)return;this.isProcessingBatch=!0,this.batchTimeout&&(clearTimeout(this.batchTimeout),this.batchTimeout=void 0);const e=this.batchQueue.splice(0,this.batchSize);this.currentBatchSize=0;for(let s=0;s<this.batchQueue.length;s+=1){const a=this.batchQueue[s],i=this.edgeCompat||typeof TextEncoder>"u"?y.byteLength(a,"utf8"):new TextEncoder().encode(a).length;this.currentBatchSize+=i+(s<this.batchQueue.length-1?this.batchSendDelimiter.length:0)}try{await this.sendBatch(e)}catch(s){this.onError&&this.onError(s)}finally{this.isProcessingBatch=!1,this.batchQueue.length>0&&this.processBatch()}}async sendBatch(e){let s;switch(this.batchMode){case"array":{const a=e.map(i=>JSON.parse(i));s=JSON.stringify(a);break}case"field":{const a=e.map(r=>JSON.parse(r)),i=this.batchFieldName?{[this.batchFieldName]:a}:{};s=JSON.stringify(i);break}default:{s=e.join(this.batchSendDelimiter);break}}await this.sendPayload(s,this.batchContentType)}async sendPayload(e,s){const a=typeof this.headers=="function"?this.headers():{...this.headers};a["content-type"]??=s??this.contentType;let i=e;if(this.compression&&!this.edgeCompat)try{i=await D(e),a["content-encoding"]="gzip"}catch(r){this.onError&&this.onError(new Error(`Compression failed: ${String(r)}`))}await M(this.url,this.method,a,i,this.maxRetries,this.retryDelay,this.respectRateLimit,this.onDebugRequestResponse,this.onError)}}export{U as AbstractHttpReporter};
@@ -1,8 +1,8 @@
1
1
  import { AbstractHttpReporterOptions, AbstractHttpReporter } from "./abstract-http-reporter.js";
2
- import "../../packem_shared/types.d-BeLumqgD.js";
2
+ import "../../packem_shared/types.d-Ck_RSjLg.js";
3
3
  import '@visulima/colorize';
4
4
  import '@visulima/interactive-manager';
5
- import "../../packem_shared/abstract-json-reporter.d-BAgznjyU.js";
5
+ import "../../packem_shared/abstract-json-reporter.d-Boj5w068.js";
6
6
  import 'safe-stable-stringify';
7
7
  /**
8
8
  * HTTP Reporter.