@visulima/pail 4.0.0-alpha.11 → 4.0.0-alpha.13

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 (127) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/LICENSE.md +131 -900
  3. package/dist/error.d.ts +95 -96
  4. package/dist/error.js +5 -76
  5. package/dist/index.browser.d.ts +557 -47
  6. package/dist/index.browser.js +19 -1499
  7. package/dist/index.server.d.ts +722 -61
  8. package/dist/index.server.js +31 -2803
  9. package/dist/middleware/elysia.d.ts +65 -63
  10. package/dist/middleware/elysia.js +1 -70
  11. package/dist/middleware/express.d.ts +73 -71
  12. package/dist/middleware/express.js +1 -29
  13. package/dist/middleware/fastify.d.ts +69 -67
  14. package/dist/middleware/fastify.js +1 -46
  15. package/dist/middleware/hono.d.ts +74 -72
  16. package/dist/middleware/hono.js +1 -33
  17. package/dist/middleware/next/handler.d.ts +101 -34
  18. package/dist/middleware/next/handler.js +1 -53
  19. package/dist/middleware/sveltekit.d.ts +100 -98
  20. package/dist/middleware/sveltekit.js +1 -43
  21. package/dist/object-tree.d.ts +67 -70
  22. package/dist/object-tree.js +2 -89
  23. package/dist/packem_shared/AbstractJsonReporter-BawNFYxI.js +1 -0
  24. package/dist/packem_shared/AbstractJsonReporter-Dt5-5x2R.js +1 -0
  25. package/dist/packem_shared/JsonReporter-15ln04rX.js +2 -0
  26. package/dist/packem_shared/JsonReporter-CerNvhg-.js +1 -0
  27. package/dist/packem_shared/PrettyReporter-Br5njEEy.js +5 -0
  28. package/dist/packem_shared/PrettyReporter-CgXnwIip.js +44 -0
  29. package/dist/packem_shared/abstract-json-reporter.d-Bz3lZEDi.d.ts +61 -0
  30. package/dist/packem_shared/abstract-pretty-reporter-BiHgKxG_.js +33 -0
  31. package/dist/packem_shared/constants-DKfCaSUR.js +1 -0
  32. package/dist/packem_shared/constants-ep2nsfdy.js +1 -0
  33. package/dist/packem_shared/create-middleware-logger.d-DeNLii5l.d.ts +47 -0
  34. package/dist/packem_shared/createPailError-53jAfQ-T.js +5 -0
  35. package/dist/packem_shared/headers-S8WyX-2m.js +1 -0
  36. package/dist/packem_shared/index-Bzp4LnJ5.js +1 -0
  37. package/dist/packem_shared/pailMiddleware-CWOJRmJq.js +1 -0
  38. package/dist/packem_shared/storage-DHM1EP2P.js +1 -0
  39. package/dist/packem_shared/types.d-ByY2ZuYG.d.ts +246 -0
  40. package/dist/packem_shared/types.d-C51XNfQz.d.ts +194 -0
  41. package/dist/packem_shared/types.d-CM5ie2qm.d.ts +236 -0
  42. package/dist/packem_shared/useLogger-qyZrjy4D.js +1 -0
  43. package/dist/packem_shared/wide-event.d-Ke44zirU.d.ts +704 -0
  44. package/dist/packem_shared/write-console-log-based-on-level-DfD64owL.js +1 -0
  45. package/dist/packem_shared/write-stream-Dd4_PTcm.js +1 -0
  46. package/dist/processor/caller/caller-processor.d.ts +49 -47
  47. package/dist/processor/caller/caller-processor.js +1 -59
  48. package/dist/processor/environment-processor.d.ts +113 -113
  49. package/dist/processor/environment-processor.js +1 -89
  50. package/dist/processor/message-formatter-processor.d.ts +51 -49
  51. package/dist/processor/message-formatter-processor.js +1 -715
  52. package/dist/processor/opentelemetry-processor.d.ts +69 -66
  53. package/dist/processor/opentelemetry-processor.js +1 -52
  54. package/dist/processor/redact-processor.d.ts +60 -41
  55. package/dist/processor/redact-processor.js +1 -31
  56. package/dist/processor/sampling-processor.d.ts +102 -101
  57. package/dist/processor/sampling-processor.js +1 -59
  58. package/dist/reporter/file/json-file-reporter.d.ts +91 -42
  59. package/dist/reporter/file/json-file-reporter.js +2 -136
  60. package/dist/reporter/http/abstract-http-reporter.d.ts +213 -211
  61. package/dist/reporter/http/abstract-http-reporter.js +2 -432
  62. package/dist/reporter/http/http-reporter.d.ts +40 -36
  63. package/dist/reporter/http/http-reporter.edge-light.d.ts +430 -38
  64. package/dist/reporter/http/http-reporter.edge-light.js +2 -728
  65. package/dist/reporter/http/http-reporter.js +1 -13
  66. package/dist/reporter/json/index.browser.d.ts +99 -3
  67. package/dist/reporter/json/index.browser.js +1 -2
  68. package/dist/reporter/json/index.d.ts +51 -3
  69. package/dist/reporter/json/index.js +1 -2
  70. package/dist/reporter/pretty/index.browser.d.ts +106 -2
  71. package/dist/reporter/pretty/index.browser.js +1 -1
  72. package/dist/reporter/pretty/index.d.ts +186 -2
  73. package/dist/reporter/pretty/index.js +1 -1
  74. package/dist/reporter/simple/simple-reporter.server.d.ts +168 -18
  75. package/dist/reporter/simple/simple-reporter.server.js +8 -182
  76. package/dist/wide-event.d.ts +5 -300
  77. package/dist/wide-event.js +1 -284
  78. package/package.json +29 -30
  79. package/dist/constants.d.ts +0 -37
  80. package/dist/middleware/next/middleware.d.ts +0 -59
  81. package/dist/middleware/next/storage.d.ts +0 -14
  82. package/dist/middleware/shared/create-middleware-logger.d.ts +0 -82
  83. package/dist/middleware/shared/headers.d.ts +0 -14
  84. package/dist/middleware/shared/routes.d.ts +0 -30
  85. package/dist/middleware/shared/storage.d.ts +0 -29
  86. package/dist/packem_shared/AbstractJsonReporter-BO8Calb4.js +0 -284
  87. package/dist/packem_shared/AbstractJsonReporter-nOj0Ft1F.js +0 -284
  88. package/dist/packem_shared/JsonReporter-CCmj7oYL.js +0 -28
  89. package/dist/packem_shared/JsonReporter-Ck2PIAEw.js +0 -58
  90. package/dist/packem_shared/PrettyReporter-BCvyNzXO.js +0 -2720
  91. package/dist/packem_shared/PrettyReporter-BtTr13Ha.js +0 -213
  92. package/dist/packem_shared/abstract-pretty-reporter-CXAKYCb8.js +0 -2635
  93. package/dist/packem_shared/constants-B1RjD_ps.js +0 -99
  94. package/dist/packem_shared/constants-omsTHUWB.js +0 -119
  95. package/dist/packem_shared/createPailError-B_sgL0nF.js +0 -76
  96. package/dist/packem_shared/headers-BxHWM6KI.js +0 -127
  97. package/dist/packem_shared/index-Bx3-C0j9.js +0 -658
  98. package/dist/packem_shared/pailMiddleware-Ci88geIF.js +0 -24
  99. package/dist/packem_shared/storage-D0vqz8OX.js +0 -36
  100. package/dist/packem_shared/useLogger-D0rU3lcX.js +0 -33
  101. package/dist/packem_shared/write-console-log-based-on-level-ree2lDPw.js +0 -15
  102. package/dist/packem_shared/write-stream-MDqyXmc_.js +0 -6
  103. package/dist/pail.browser.d.ts +0 -412
  104. package/dist/pail.server.d.ts +0 -158
  105. package/dist/processor/caller/get-caller-filename.d.ts +0 -23
  106. package/dist/reporter/file/utils/rotating-file-stream.d.ts +0 -48
  107. package/dist/reporter/http/utils/compression.d.ts +0 -7
  108. package/dist/reporter/http/utils/log-size-error.d.ts +0 -30
  109. package/dist/reporter/http/utils/retry.d.ts +0 -27
  110. package/dist/reporter/json/abstract-json-reporter.d.ts +0 -61
  111. package/dist/reporter/json/json-reporter.browser.d.ts +0 -40
  112. package/dist/reporter/json/json-reporter.server.d.ts +0 -50
  113. package/dist/reporter/pretty/abstract-pretty-reporter.d.ts +0 -83
  114. package/dist/reporter/pretty/pretty-reporter.browser.d.ts +0 -36
  115. package/dist/reporter/pretty/pretty-reporter.server.d.ts +0 -70
  116. package/dist/reporter/raw/raw-reporter.browser.d.ts +0 -5
  117. package/dist/reporter/raw/raw-reporter.server.d.ts +0 -13
  118. package/dist/reporter/utils/default-inspector-config.d.ts +0 -3
  119. package/dist/reporter/utils/format-label.d.ts +0 -3
  120. package/dist/types.d.ts +0 -241
  121. package/dist/utils/arrayify.d.ts +0 -2
  122. package/dist/utils/get-longest-badge.d.ts +0 -4
  123. package/dist/utils/get-longest-label.d.ts +0 -4
  124. package/dist/utils/merge-types.d.ts +0 -4
  125. package/dist/utils/stream/safe-stream-handler.d.ts +0 -21
  126. package/dist/utils/write-console-log-based-on-level.d.ts +0 -4
  127. package/dist/utils/write-stream.d.ts +0 -2
@@ -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-BawNFYxI.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,39 +1,43 @@
1
- import type { AbstractHttpReporterOptions } from "./abstract-http-reporter.d.ts";
2
- import { AbstractHttpReporter } from "./abstract-http-reporter.d.ts";
1
+ import { AbstractHttpReporterOptions, AbstractHttpReporter } from "./abstract-http-reporter.js";
2
+ import "../../packem_shared/types.d-ByY2ZuYG.js";
3
+ import '@visulima/colorize';
4
+ import '@visulima/interactive-manager';
5
+ import "../../packem_shared/abstract-json-reporter.d-Bz3lZEDi.js";
6
+ import 'safe-stable-stringify';
3
7
  /**
4
- * HTTP Reporter.
5
- *
6
- * A reporter that sends logs to HTTP endpoints.
7
- * Supports batching, compression, retries, and rate limiting.
8
- * Works in both Node.js server and browser environments.
9
- * @template L - The log level type
10
- * @example
11
- * ```typescript
12
- * import { createPail } from "@visulima/pail";
13
- * import { HttpReporter } from "@visulima/pail/reporter/http";
14
- *
15
- * const logger = createPail({
16
- * reporters: [
17
- * new HttpReporter({
18
- * url: "https://api.example.com/logs",
19
- * method: "POST",
20
- * headers: {
21
- * "Authorization": "Bearer token"
22
- * },
23
- * enableBatchSend: true,
24
- * batchSize: 100
25
- * })
26
- * ]
27
- * });
28
- *
29
- * logger.info("Application started", { version: "1.0.0" });
30
- * ```
31
- */
8
+ * HTTP Reporter.
9
+ *
10
+ * A reporter that sends logs to HTTP endpoints.
11
+ * Supports batching, compression, retries, and rate limiting.
12
+ * Works in both Node.js server and browser environments.
13
+ * @template L - The log level type
14
+ * @example
15
+ * ```typescript
16
+ * import { createPail } from "@visulima/pail";
17
+ * import { HttpReporter } from "@visulima/pail/reporter/http";
18
+ *
19
+ * const logger = createPail({
20
+ * reporters: [
21
+ * new HttpReporter({
22
+ * url: "https://api.example.com/logs",
23
+ * method: "POST",
24
+ * headers: {
25
+ * "Authorization": "Bearer token"
26
+ * },
27
+ * enableBatchSend: true,
28
+ * batchSize: 100
29
+ * })
30
+ * ]
31
+ * });
32
+ *
33
+ * logger.info("Application started", { version: "1.0.0" });
34
+ * ```
35
+ */
32
36
  declare class HttpReporter<L extends string = string> extends AbstractHttpReporter<L> {
33
- /**
34
- * Creates a new HTTP Reporter instance.
35
- * @param options Configuration options for HTTP reporting
36
- */
37
- constructor(options: AbstractHttpReporterOptions);
37
+ /**
38
+ * Creates a new HTTP Reporter instance.
39
+ * @param options Configuration options for HTTP reporting
40
+ */
41
+ constructor(options: AbstractHttpReporterOptions);
38
42
  }
39
- export default HttpReporter;
43
+ export { HttpReporter as default };