vedatrace 0.2.0 → 0.3.0

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.
package/dist/index.cjs CHANGED
@@ -10,40 +10,43 @@ class VedaTraceBatcher {
10
10
  this.config = config;
11
11
  this.immediateFlush = immediateFlush;
12
12
  this.context = config.executionContext;
13
+ this.waitUntilFn = config.waitUntil;
13
14
  }
14
15
  queue = [];
15
16
  flushTimer = null;
16
- flushDebounceTimer = null;
17
+ flushQueued = false;
17
18
  isFlushing = false;
18
19
  pendingFlush = null;
19
20
  context;
20
- /** Attach execution context after initialization */
21
+ waitUntilFn;
21
22
  setContext(ctx) {
22
23
  this.context = ctx;
23
24
  }
24
- /** Get current context */
25
25
  getContext() {
26
26
  return this.context;
27
27
  }
28
- /** Add log to queue with context-aware flush */
29
28
  add(log) {
30
29
  this.queue.push(log);
31
- if (!this.flushTimer && !this.immediateFlush) {
30
+ if (this.context || this.waitUntilFn) {
31
+ this.debouncedFlush();
32
+ return;
33
+ }
34
+ if (this.immediateFlush) {
35
+ this.flush();
36
+ return;
37
+ }
38
+ if (!this.flushTimer) {
32
39
  this.startFlushTimer();
33
40
  }
34
- if (this.immediateFlush || this.context) {
35
- this.debouncedFlush();
36
- } else if (this.queue.length >= this.config.batchSize) {
41
+ if (this.queue.length >= this.config.batchSize) {
37
42
  this.flush();
38
43
  }
39
44
  }
40
- /** Debounced flush - prevents rapid-fire flushes */
41
45
  debouncedFlush() {
42
- if (this.flushDebounceTimer) {
43
- clearTimeout(this.flushDebounceTimer);
44
- }
45
- this.flushDebounceTimer = setTimeout(() => {
46
- this.flushDebounceTimer = null;
46
+ if (this.flushQueued) return;
47
+ this.flushQueued = true;
48
+ queueMicrotask(() => {
49
+ this.flushQueued = false;
47
50
  this.flush().catch((error) => {
48
51
  if (this.config.onError) {
49
52
  this.config.onError(
@@ -56,12 +59,11 @@ class VedaTraceBatcher {
56
59
  );
57
60
  }
58
61
  });
59
- }, 100);
62
+ });
60
63
  }
61
- /** Flush logs to all transports with waitUntil protection */
62
64
  async flush() {
63
65
  if (this.isFlushing) {
64
- return this.pendingFlush ?? Promise.resolve();
66
+ return this.pendingFlush ?? await Promise.resolve();
65
67
  }
66
68
  if (this.queue.length === 0) {
67
69
  return Promise.resolve();
@@ -74,8 +76,10 @@ class VedaTraceBatcher {
74
76
  this.pendingFlush = null;
75
77
  });
76
78
  this.pendingFlush = flushPromise;
77
- if (this.context) {
79
+ if (this.context && typeof this.context.waitUntil === "function") {
78
80
  this.context.waitUntil(flushPromise);
81
+ } else if (typeof this.waitUntilFn === "function") {
82
+ this.waitUntilFn(flushPromise);
79
83
  }
80
84
  return flushPromise;
81
85
  }
@@ -136,10 +140,7 @@ class VedaTraceBatcher {
136
140
  clearInterval(this.flushTimer);
137
141
  this.flushTimer = null;
138
142
  }
139
- if (this.flushDebounceTimer) {
140
- clearTimeout(this.flushDebounceTimer);
141
- this.flushDebounceTimer = null;
142
- }
143
+ this.flushQueued = false;
143
144
  }
144
145
  start() {
145
146
  if (!this.flushTimer && !this.immediateFlush) {
@@ -546,7 +547,7 @@ function vedatrace(config = {}) {
546
547
  const HttpTransport = isBrowserEnv ? transports_index.VedaTraceHttpTransportBrowser : transports_index.VedaTraceHttpTransport;
547
548
  const httpConfig = {
548
549
  apiKey: config.apiKey,
549
- keepalive: isBrowserEnv
550
+ keepalive: isBrowserEnv || isServerlessEnv
550
551
  };
551
552
  if (config.endpoint) httpConfig.endpoint = config.endpoint;
552
553
  const httpTransport = new HttpTransport(httpConfig);
@@ -571,12 +572,15 @@ function vedatrace(config = {}) {
571
572
  unrefTimer: config.unrefTimer ?? shouldUnrefTimer,
572
573
  executionContext: config.executionContext,
573
574
  onError: config.onError,
574
- onSuccess: config.onSuccess
575
+ onSuccess: config.onSuccess,
576
+ debug: config.debug,
577
+ waitUntil: config.waitUntil
575
578
  },
576
579
  immediateFlush
577
580
  );
578
581
  logger.setBatcher(batcher);
579
- if (typeof process !== "undefined" && isLongRunningEnv) {
582
+ const isNodeRuntime = typeof process !== "undefined" && process.versions?.node;
583
+ if (typeof process !== "undefined" && (isLongRunningEnv || isNodeRuntime)) {
580
584
  const flushLogs = async () => {
581
585
  await batcher.flush();
582
586
  };
package/dist/index.d.cts CHANGED
@@ -1,17 +1,14 @@
1
- import { b as VedaTraceTransport, B as BatcherConfig, c as VedaTraceEdgeContext, I as InternalLogEntry, V as VedaTraceLoggerInterface, R as RuntimeType$1, a as VedaTraceConfig, L as LogMetadata, d as RedactionConfig } from './types-BU0UESs9.cjs';
2
- export { e as VedaTraceLevel, f as VedaTraceLog } from './types-BU0UESs9.cjs';
1
+ import { b as VedaTraceTransport, B as BatcherConfig, c as VedaTraceEdgeContext, I as InternalLogEntry, V as VedaTraceLoggerInterface, R as RuntimeType$1, a as VedaTraceConfig, L as LogMetadata, d as RedactionConfig } from './types-CSBzeGyK.cjs';
2
+ export { e as VedaTraceLevel, f as VedaTraceLog } from './types-CSBzeGyK.cjs';
3
3
  export { ConsoleFormat, ConsoleTransportConfig, HttpTransportConfig, VedaTraceConsoleTransport, VedaTraceHttpTransport, VedaTraceHttpTransportBrowser } from './transports/index.cjs';
4
4
 
5
5
  /**
6
6
  * VedaTrace Batcher - Context-Aware for Cloudflare Workers
7
7
  *
8
- * Key features:
9
- * 1. Stores EdgeContext for waitUntil() integration
10
- * 2. setContext() method for post-initialization context attachment
11
- * 3. Fire-and-forget flush wrapped in ctx.waitUntil() when context is available
12
- * 4. Debounced flush to avoid excessive network calls
13
- * 5. Automatic flush on each log entry when context is present
14
- * 6. All callbacks moved into BatcherConfig for cleaner API
8
+ * Three flush modes:
9
+ * 1. Reliable mode (context or waitUntilFn): debounced flush + waitUntil for guaranteed delivery
10
+ * 2. Immediate mode: flush() called directly from add() during handler execution
11
+ * 3. Batch mode: periodic/batch-size flush for long-running environments
15
12
  */
16
13
 
17
14
  declare class VedaTraceBatcher {
@@ -20,20 +17,16 @@ declare class VedaTraceBatcher {
20
17
  private immediateFlush;
21
18
  private queue;
22
19
  private flushTimer;
23
- private flushDebounceTimer;
20
+ private flushQueued;
24
21
  private isFlushing;
25
22
  private pendingFlush;
26
23
  private context;
24
+ private waitUntilFn;
27
25
  constructor(transports: VedaTraceTransport[], config: BatcherConfig, immediateFlush?: boolean);
28
- /** Attach execution context after initialization */
29
26
  setContext(ctx: VedaTraceEdgeContext): void;
30
- /** Get current context */
31
27
  getContext(): VedaTraceEdgeContext | undefined;
32
- /** Add log to queue with context-aware flush */
33
28
  add(log: InternalLogEntry): void;
34
- /** Debounced flush - prevents rapid-fire flushes */
35
29
  private debouncedFlush;
36
- /** Flush logs to all transports with waitUntil protection */
37
30
  flush(): Promise<void>;
38
31
  private sendWithRetry;
39
32
  private startFlushTimer;
package/dist/index.d.mts CHANGED
@@ -1,17 +1,14 @@
1
- import { b as VedaTraceTransport, B as BatcherConfig, c as VedaTraceEdgeContext, I as InternalLogEntry, V as VedaTraceLoggerInterface, R as RuntimeType$1, a as VedaTraceConfig, L as LogMetadata, d as RedactionConfig } from './types-BU0UESs9.mjs';
2
- export { e as VedaTraceLevel, f as VedaTraceLog } from './types-BU0UESs9.mjs';
1
+ import { b as VedaTraceTransport, B as BatcherConfig, c as VedaTraceEdgeContext, I as InternalLogEntry, V as VedaTraceLoggerInterface, R as RuntimeType$1, a as VedaTraceConfig, L as LogMetadata, d as RedactionConfig } from './types-CSBzeGyK.mjs';
2
+ export { e as VedaTraceLevel, f as VedaTraceLog } from './types-CSBzeGyK.mjs';
3
3
  export { ConsoleFormat, ConsoleTransportConfig, HttpTransportConfig, VedaTraceConsoleTransport, VedaTraceHttpTransport, VedaTraceHttpTransportBrowser } from './transports/index.mjs';
4
4
 
5
5
  /**
6
6
  * VedaTrace Batcher - Context-Aware for Cloudflare Workers
7
7
  *
8
- * Key features:
9
- * 1. Stores EdgeContext for waitUntil() integration
10
- * 2. setContext() method for post-initialization context attachment
11
- * 3. Fire-and-forget flush wrapped in ctx.waitUntil() when context is available
12
- * 4. Debounced flush to avoid excessive network calls
13
- * 5. Automatic flush on each log entry when context is present
14
- * 6. All callbacks moved into BatcherConfig for cleaner API
8
+ * Three flush modes:
9
+ * 1. Reliable mode (context or waitUntilFn): debounced flush + waitUntil for guaranteed delivery
10
+ * 2. Immediate mode: flush() called directly from add() during handler execution
11
+ * 3. Batch mode: periodic/batch-size flush for long-running environments
15
12
  */
16
13
 
17
14
  declare class VedaTraceBatcher {
@@ -20,20 +17,16 @@ declare class VedaTraceBatcher {
20
17
  private immediateFlush;
21
18
  private queue;
22
19
  private flushTimer;
23
- private flushDebounceTimer;
20
+ private flushQueued;
24
21
  private isFlushing;
25
22
  private pendingFlush;
26
23
  private context;
24
+ private waitUntilFn;
27
25
  constructor(transports: VedaTraceTransport[], config: BatcherConfig, immediateFlush?: boolean);
28
- /** Attach execution context after initialization */
29
26
  setContext(ctx: VedaTraceEdgeContext): void;
30
- /** Get current context */
31
27
  getContext(): VedaTraceEdgeContext | undefined;
32
- /** Add log to queue with context-aware flush */
33
28
  add(log: InternalLogEntry): void;
34
- /** Debounced flush - prevents rapid-fire flushes */
35
29
  private debouncedFlush;
36
- /** Flush logs to all transports with waitUntil protection */
37
30
  flush(): Promise<void>;
38
31
  private sendWithRetry;
39
32
  private startFlushTimer;
package/dist/index.mjs CHANGED
@@ -6,40 +6,43 @@ class VedaTraceBatcher {
6
6
  this.config = config;
7
7
  this.immediateFlush = immediateFlush;
8
8
  this.context = config.executionContext;
9
+ this.waitUntilFn = config.waitUntil;
9
10
  }
10
11
  queue = [];
11
12
  flushTimer = null;
12
- flushDebounceTimer = null;
13
+ flushQueued = false;
13
14
  isFlushing = false;
14
15
  pendingFlush = null;
15
16
  context;
16
- /** Attach execution context after initialization */
17
+ waitUntilFn;
17
18
  setContext(ctx) {
18
19
  this.context = ctx;
19
20
  }
20
- /** Get current context */
21
21
  getContext() {
22
22
  return this.context;
23
23
  }
24
- /** Add log to queue with context-aware flush */
25
24
  add(log) {
26
25
  this.queue.push(log);
27
- if (!this.flushTimer && !this.immediateFlush) {
26
+ if (this.context || this.waitUntilFn) {
27
+ this.debouncedFlush();
28
+ return;
29
+ }
30
+ if (this.immediateFlush) {
31
+ this.flush();
32
+ return;
33
+ }
34
+ if (!this.flushTimer) {
28
35
  this.startFlushTimer();
29
36
  }
30
- if (this.immediateFlush || this.context) {
31
- this.debouncedFlush();
32
- } else if (this.queue.length >= this.config.batchSize) {
37
+ if (this.queue.length >= this.config.batchSize) {
33
38
  this.flush();
34
39
  }
35
40
  }
36
- /** Debounced flush - prevents rapid-fire flushes */
37
41
  debouncedFlush() {
38
- if (this.flushDebounceTimer) {
39
- clearTimeout(this.flushDebounceTimer);
40
- }
41
- this.flushDebounceTimer = setTimeout(() => {
42
- this.flushDebounceTimer = null;
42
+ if (this.flushQueued) return;
43
+ this.flushQueued = true;
44
+ queueMicrotask(() => {
45
+ this.flushQueued = false;
43
46
  this.flush().catch((error) => {
44
47
  if (this.config.onError) {
45
48
  this.config.onError(
@@ -52,12 +55,11 @@ class VedaTraceBatcher {
52
55
  );
53
56
  }
54
57
  });
55
- }, 100);
58
+ });
56
59
  }
57
- /** Flush logs to all transports with waitUntil protection */
58
60
  async flush() {
59
61
  if (this.isFlushing) {
60
- return this.pendingFlush ?? Promise.resolve();
62
+ return this.pendingFlush ?? await Promise.resolve();
61
63
  }
62
64
  if (this.queue.length === 0) {
63
65
  return Promise.resolve();
@@ -70,8 +72,10 @@ class VedaTraceBatcher {
70
72
  this.pendingFlush = null;
71
73
  });
72
74
  this.pendingFlush = flushPromise;
73
- if (this.context) {
75
+ if (this.context && typeof this.context.waitUntil === "function") {
74
76
  this.context.waitUntil(flushPromise);
77
+ } else if (typeof this.waitUntilFn === "function") {
78
+ this.waitUntilFn(flushPromise);
75
79
  }
76
80
  return flushPromise;
77
81
  }
@@ -132,10 +136,7 @@ class VedaTraceBatcher {
132
136
  clearInterval(this.flushTimer);
133
137
  this.flushTimer = null;
134
138
  }
135
- if (this.flushDebounceTimer) {
136
- clearTimeout(this.flushDebounceTimer);
137
- this.flushDebounceTimer = null;
138
- }
139
+ this.flushQueued = false;
139
140
  }
140
141
  start() {
141
142
  if (!this.flushTimer && !this.immediateFlush) {
@@ -542,7 +543,7 @@ function vedatrace(config = {}) {
542
543
  const HttpTransport = isBrowserEnv ? VedaTraceHttpTransportBrowser : VedaTraceHttpTransport;
543
544
  const httpConfig = {
544
545
  apiKey: config.apiKey,
545
- keepalive: isBrowserEnv
546
+ keepalive: isBrowserEnv || isServerlessEnv
546
547
  };
547
548
  if (config.endpoint) httpConfig.endpoint = config.endpoint;
548
549
  const httpTransport = new HttpTransport(httpConfig);
@@ -567,12 +568,15 @@ function vedatrace(config = {}) {
567
568
  unrefTimer: config.unrefTimer ?? shouldUnrefTimer,
568
569
  executionContext: config.executionContext,
569
570
  onError: config.onError,
570
- onSuccess: config.onSuccess
571
+ onSuccess: config.onSuccess,
572
+ debug: config.debug,
573
+ waitUntil: config.waitUntil
571
574
  },
572
575
  immediateFlush
573
576
  );
574
577
  logger.setBatcher(batcher);
575
- if (typeof process !== "undefined" && isLongRunningEnv) {
578
+ const isNodeRuntime = typeof process !== "undefined" && process.versions?.node;
579
+ if (typeof process !== "undefined" && (isLongRunningEnv || isNodeRuntime)) {
576
580
  const flushLogs = async () => {
577
581
  await batcher.flush();
578
582
  };
@@ -1,5 +1,5 @@
1
1
  import { Request, Response, NextFunction } from 'express';
2
- import { V as VedaTraceLoggerInterface, a as VedaTraceConfig, L as LogMetadata } from '../types-BU0UESs9.cjs';
2
+ import { V as VedaTraceLoggerInterface, a as VedaTraceConfig, L as LogMetadata } from '../types-CSBzeGyK.cjs';
3
3
 
4
4
  /**
5
5
  * Express.js middleware integration for VedaTrace
@@ -1,5 +1,5 @@
1
1
  import { Request, Response, NextFunction } from 'express';
2
- import { V as VedaTraceLoggerInterface, a as VedaTraceConfig, L as LogMetadata } from '../types-BU0UESs9.mjs';
2
+ import { V as VedaTraceLoggerInterface, a as VedaTraceConfig, L as LogMetadata } from '../types-CSBzeGyK.mjs';
3
3
 
4
4
  /**
5
5
  * Express.js middleware integration for VedaTrace
@@ -1,4 +1,4 @@
1
- import { a as VedaTraceConfig, L as LogMetadata, V as VedaTraceLoggerInterface } from '../types-BU0UESs9.cjs';
1
+ import { a as VedaTraceConfig, L as LogMetadata, V as VedaTraceLoggerInterface } from '../types-CSBzeGyK.cjs';
2
2
 
3
3
  /**
4
4
  * Next.js integration for VedaTrace
@@ -1,4 +1,4 @@
1
- import { a as VedaTraceConfig, L as LogMetadata, V as VedaTraceLoggerInterface } from '../types-BU0UESs9.mjs';
1
+ import { a as VedaTraceConfig, L as LogMetadata, V as VedaTraceLoggerInterface } from '../types-CSBzeGyK.mjs';
2
2
 
3
3
  /**
4
4
  * Next.js integration for VedaTrace
@@ -1,5 +1,5 @@
1
1
  import { ReactNode, ReactElement } from 'react';
2
- import { a as VedaTraceConfig, V as VedaTraceLoggerInterface, L as LogMetadata } from '../types-BU0UESs9.cjs';
2
+ import { a as VedaTraceConfig, V as VedaTraceLoggerInterface, L as LogMetadata } from '../types-CSBzeGyK.cjs';
3
3
 
4
4
  /**
5
5
  * React integration for VedaTrace
@@ -1,5 +1,5 @@
1
1
  import { ReactNode, ReactElement } from 'react';
2
- import { a as VedaTraceConfig, V as VedaTraceLoggerInterface, L as LogMetadata } from '../types-BU0UESs9.mjs';
2
+ import { a as VedaTraceConfig, V as VedaTraceLoggerInterface, L as LogMetadata } from '../types-CSBzeGyK.mjs';
3
3
 
4
4
  /**
5
5
  * React integration for VedaTrace
@@ -1,4 +1,4 @@
1
- import { e as VedaTraceLevel, b as VedaTraceTransport, I as InternalLogEntry } from '../types-BU0UESs9.cjs';
1
+ import { e as VedaTraceLevel, b as VedaTraceTransport, I as InternalLogEntry } from '../types-CSBzeGyK.cjs';
2
2
 
3
3
  /**
4
4
  * Console transport for development/debugging
@@ -1,4 +1,4 @@
1
- import { e as VedaTraceLevel, b as VedaTraceTransport, I as InternalLogEntry } from '../types-BU0UESs9.mjs';
1
+ import { e as VedaTraceLevel, b as VedaTraceTransport, I as InternalLogEntry } from '../types-CSBzeGyK.mjs';
2
2
 
3
3
  /**
4
4
  * Console transport for development/debugging
@@ -42,6 +42,12 @@ interface VedaTraceConfig {
42
42
  runtime?: RuntimeType;
43
43
  /** Cloudflare Workers ExecutionContext - enables waitUntil support */
44
44
  executionContext?: VedaTraceEdgeContext;
45
+ /**
46
+ * Global waitUntil function (e.g. from "cloudflare:workers").
47
+ * When provided, the SDK uses the reliable debounced-flush+waitUntil path
48
+ * instead of fire-and-forget, ensuring logs complete before worker termination.
49
+ */
50
+ waitUntil?: (promise: Promise<unknown>) => void;
45
51
  }
46
52
  interface RedactionConfig {
47
53
  paths?: string[];
@@ -78,6 +84,8 @@ interface BatcherConfig {
78
84
  executionContext?: VedaTraceEdgeContext;
79
85
  onError?: (error: Error) => void;
80
86
  onSuccess?: () => void;
87
+ debug?: boolean;
88
+ waitUntil?: (promise: Promise<unknown>) => void;
81
89
  }
82
90
  interface VedaTraceLoggerInterface {
83
91
  debug(message: string, metadata?: LogMetadata): void;
@@ -42,6 +42,12 @@ interface VedaTraceConfig {
42
42
  runtime?: RuntimeType;
43
43
  /** Cloudflare Workers ExecutionContext - enables waitUntil support */
44
44
  executionContext?: VedaTraceEdgeContext;
45
+ /**
46
+ * Global waitUntil function (e.g. from "cloudflare:workers").
47
+ * When provided, the SDK uses the reliable debounced-flush+waitUntil path
48
+ * instead of fire-and-forget, ensuring logs complete before worker termination.
49
+ */
50
+ waitUntil?: (promise: Promise<unknown>) => void;
45
51
  }
46
52
  interface RedactionConfig {
47
53
  paths?: string[];
@@ -78,6 +84,8 @@ interface BatcherConfig {
78
84
  executionContext?: VedaTraceEdgeContext;
79
85
  onError?: (error: Error) => void;
80
86
  onSuccess?: () => void;
87
+ debug?: boolean;
88
+ waitUntil?: (promise: Promise<unknown>) => void;
81
89
  }
82
90
  interface VedaTraceLoggerInterface {
83
91
  debug(message: string, metadata?: LogMetadata): void;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vedatrace",
3
3
  "description": "Universal JavaScript logging SDK for VedaTrace - type-safe, lightweight, and developer-friendly",
4
- "version": "0.2.0",
4
+ "version": "0.3.0",
5
5
  "scripts": {
6
6
  "setup": "rm -rf node_modules && npm i && git init && husky",
7
7
  "prepublishOnly": "npm run build",