vedatrace 0.1.9 → 0.2.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.d.cts CHANGED
@@ -1,77 +1,132 @@
1
- import { b as VedaTraceTransport, B as BatcherConfig, I as InternalLogEntry, V as VedaTraceLoggerInterface, R as RuntimeType$1, a as VedaTraceConfig, L as LogMetadata, c as RedactionConfig } from './types-DUNRMOTv.cjs';
2
- export { d as VedaTraceLevel, e as VedaTraceLog } from './types-DUNRMOTv.cjs';
3
- export { ConsoleFormat, ConsoleTransportConfig, HttpTransportConfig, VedaTraceConsoleTransport, VedaTraceHttpTransport } from './transports/index.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-BU0UESs9.cjs';
2
+ export { e as VedaTraceLevel, f as VedaTraceLog } from './types-BU0UESs9.cjs';
3
+ export { ConsoleFormat, ConsoleTransportConfig, HttpTransportConfig, VedaTraceConsoleTransport, VedaTraceHttpTransport, VedaTraceHttpTransportBrowser } from './transports/index.cjs';
4
4
 
5
5
  /**
6
- * Log batcher with background flushing
7
- * Handles queueing, batching, and retry logic
6
+ * VedaTrace Batcher - Context-Aware for Cloudflare Workers
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
15
  */
9
16
 
10
17
  declare class VedaTraceBatcher {
11
18
  private transports;
12
19
  private config;
13
- private onError?;
14
- private onSuccess?;
15
20
  private immediateFlush;
16
21
  private queue;
17
22
  private flushTimer;
23
+ private flushDebounceTimer;
18
24
  private isFlushing;
19
25
  private pendingFlush;
20
- constructor(transports: VedaTraceTransport[], config: BatcherConfig, onError?: ((error: Error) => void) | undefined, onSuccess?: (() => void) | undefined, immediateFlush?: boolean, autoStart?: boolean);
21
- /** Add log to queue */
26
+ private context;
27
+ constructor(transports: VedaTraceTransport[], config: BatcherConfig, immediateFlush?: boolean);
28
+ /** Attach execution context after initialization */
29
+ setContext(ctx: VedaTraceEdgeContext): void;
30
+ /** Get current context */
31
+ getContext(): VedaTraceEdgeContext | undefined;
32
+ /** Add log to queue with context-aware flush */
22
33
  add(log: InternalLogEntry): void;
23
- /** Flush logs to all transports */
34
+ /** Debounced flush - prevents rapid-fire flushes */
35
+ private debouncedFlush;
36
+ /** Flush logs to all transports with waitUntil protection */
24
37
  flush(): Promise<void>;
25
- /** Send logs with retry logic */
26
38
  private sendWithRetry;
27
- /** Start the flush interval timer */
28
39
  private startFlushTimer;
29
- /** Stop the flush timer */
30
40
  stop(): void;
31
- /** Start the flush timer (for manual control in edge runtimes) */
32
41
  start(): void;
33
- /** Delay helper */
34
42
  private delay;
35
- /** Get current queue size */
36
43
  getQueueSize(): number;
44
+ setExecutionContext(ctx: VedaTraceEdgeContext): void;
37
45
  }
38
46
 
39
47
  /**
40
48
  * Core VedaTrace Logger implementation
49
+ * Supports Cloudflare Workers / Pages via withContext() integration
41
50
  */
42
51
 
43
52
  declare class VedaTraceLogger implements VedaTraceLoggerInterface {
44
53
  private batcher;
45
54
  runtime: RuntimeType$1;
46
55
  private config;
47
- private childDefaults;
56
+ private readonly childDefaults;
57
+ private _context;
48
58
  constructor(config?: VedaTraceConfig, childDefaults?: LogMetadata);
49
- /** Initialize the batcher with transports */
50
59
  private initializeBatcher;
51
- /** Set batcher (called from factory function) */
52
60
  setBatcher(batcher: VedaTraceBatcher): void;
53
- /** Log at debug level */
61
+ /** Attach execution context for waitUntil support (Cloudflare Workers / Pages) */
62
+ withContext(ctx: VedaTraceEdgeContext): this;
63
+ /** Check if context is attached */
64
+ hasContext(): boolean;
65
+ /** Get current execution context */
66
+ getContext(): VedaTraceEdgeContext | undefined;
54
67
  debug(message: string, metadata?: LogMetadata): void;
55
- /** Log at info level */
56
68
  info(message: string, metadata?: LogMetadata): void;
57
- /** Log at warn level */
58
69
  warn(message: string, metadata?: LogMetadata): void;
59
- /** Log at error level */
60
70
  error(message: string | Error, metadata?: LogMetadata): void;
61
- /** Log at fatal level */
62
71
  fatal(message: string | Error, metadata?: LogMetadata): void;
63
- /** Internal log method */
64
72
  private log;
65
- /** Create a child logger with default metadata */
66
73
  child(defaults: LogMetadata): VedaTraceLoggerInterface;
67
- /** Flush pending logs */
68
74
  flush(): Promise<void>;
69
- /** Stop the batcher and flush timer */
70
75
  stop(): void;
71
- /** Start the flush timer (for manual control in edge runtimes) */
72
76
  start(): void;
73
- /** Detect runtime environment (legacy, kept for compatibility) */
74
- private detectEnvironment;
77
+ }
78
+
79
+ /**
80
+ * Browser lifecycle management for VedaTrace
81
+ *
82
+ * Handles:
83
+ * - visibilitychange: flush when page becomes hidden
84
+ * - pagehide: final flush when user leaves the page
85
+ * - beforeunload: backup flush before page unload
86
+ *
87
+ * Uses fetch with keepalive: true for final flush to ensure
88
+ * the request completes after the tab is closed.
89
+ */
90
+
91
+ interface BrowserLifecycleConfig {
92
+ transports: VedaTraceTransport[];
93
+ flush(): Promise<void>;
94
+ debug?: boolean;
95
+ }
96
+ /**
97
+ * Browser lifecycle manager
98
+ * Attaches event listeners and handles graceful flush on page exit
99
+ */
100
+ declare class BrowserLifecycle {
101
+ private config;
102
+ private boundVisibilityHandler;
103
+ private boundPageHideHandler;
104
+ private boundBeforeUnloadHandler;
105
+ private boundUnloadHandler;
106
+ private isAttached;
107
+ private pendingFlush;
108
+ constructor(config: BrowserLifecycleConfig);
109
+ /** Start listening for browser lifecycle events */
110
+ attach(): void;
111
+ /** Stop listening for browser lifecycle events */
112
+ detach(): void;
113
+ /** Handle visibility change - flush when page becomes hidden */
114
+ private handleVisibilityChange;
115
+ /** Handle pagehide event - primary flush handler for Safari */
116
+ private handlePageHide;
117
+ /** Handle beforeunload - backup flush mechanism */
118
+ private handleBeforeUnload;
119
+ /** Handle unload - fallback for older browsers */
120
+ private handleUnload;
121
+ /** Schedule a debounced flush (for visibility change) */
122
+ private scheduleFlush;
123
+ /**
124
+ * Final flush using keepalive fetch
125
+ * For sending logs after the page context is destroyed
126
+ */
127
+ private finalFlush;
128
+ /** Check if handlers are attached */
129
+ isActive(): boolean;
75
130
  }
76
131
 
77
132
  /**
@@ -83,46 +138,55 @@ declare class VedaTraceLogger implements VedaTraceLoggerInterface {
83
138
  */
84
139
  declare function redact(obj: unknown, config?: RedactionConfig): unknown;
85
140
 
141
+ type RuntimeType = "node" | "browser" | "cloudflare" | "deno" | "bun" | "edge";
86
142
  /**
87
143
  * Runtime environment detection utility
88
144
  * Detects Node.js, Browser, Cloudflare Workers, Deno, Bun
89
145
  */
90
- type RuntimeType = "node" | "browser" | "cloudflare" | "deno" | "bun" | "edge";
91
146
  declare function detectRuntime(): RuntimeType;
92
147
  declare function isEdgeRuntime(): boolean;
148
+ declare function isServerless(): boolean;
149
+ declare function isLongRunning(): boolean;
150
+ declare function isBrowser(): boolean;
93
151
 
94
152
  /**
95
153
  * VedaTrace SDK - Universal JavaScript logging
96
154
  *
97
- * Import examples:
98
- * import { vedatrace } from 'vedatrace'
99
- * import { VedaTraceHttpTransport } from 'vedatrace/transports'
100
- * import { vedaTraceMiddleware } from 'vedatrace/express'
155
+ * Supports all JavaScript environments:
156
+ * - Cloudflare Workers / Pages: waitUntil() for background flush
157
+ * - Node.js: standard batching with process event handlers
158
+ * - Bun: standard batching with unref timers
159
+ * - Deno: standard batching with unref timers
160
+ * - Browser: batching with visibility lifecycle handling
161
+ * - Generic Edge: immediate flush fallback
162
+ *
163
+ * @example
164
+ * // Cloudflare Worker
165
+ * const logger = vedatrace({ apiKey: 'key', service: 'app' }).withContext(ctx)
166
+ *
167
+ * @example
168
+ * // Node.js / Express
169
+ * const logger = vedatrace({ apiKey: 'key', service: 'app' })
170
+ * // Automatic shutdown handlers via process.on()
171
+ *
172
+ * @example
173
+ * // Browser
174
+ * const logger = vedatrace({ apiKey: 'key', service: 'app' })
175
+ * // Automatic flush on visibilitychange / pagehide
101
176
  */
102
177
 
103
178
  /**
104
179
  * Create a VedaTrace logger instance
105
180
  *
106
- * @example
107
- * ```typescript
108
- * const logger = vedatrace({
109
- * apiKey: 'your-api-key',
110
- * service: 'my-service'
111
- * })
112
- *
113
- * logger.info('Hello world')
114
- * logger.error('Something went wrong', { error: err })
115
- * ```
181
+ * Handles all runtimes with environment-appropriate strategies:
182
+ * 1. Cloudflare/Edge: immediate flush with waitUntil if context available
183
+ * 2. Node/Bun/Deno: standard batching with process/unref timers
184
+ * 3. Browser: batching with visibility lifecycle handlers
116
185
  */
117
186
  declare function vedatrace(config?: VedaTraceConfig): VedaTraceLoggerInterface;
118
187
  /**
119
188
  * Create a console-only logger for development
120
- *
121
- * @example
122
- * ```typescript
123
- * const logger = devVedatrace({ service: 'my-service' })
124
- * ```
125
189
  */
126
190
  declare function devVedatrace(config?: Omit<VedaTraceConfig, "apiKey" | "transports">): VedaTraceLoggerInterface;
127
191
 
128
- export { BatcherConfig, InternalLogEntry, LogMetadata, RedactionConfig, RuntimeType$1 as RuntimeType, VedaTraceBatcher, VedaTraceConfig, VedaTraceLogger, VedaTraceLoggerInterface, VedaTraceTransport, vedatrace as default, detectRuntime, devVedatrace, isEdgeRuntime, redact, vedatrace };
192
+ export { BatcherConfig, BrowserLifecycle, InternalLogEntry, LogMetadata, RedactionConfig, RuntimeType$1 as RuntimeType, VedaTraceBatcher, VedaTraceConfig, VedaTraceEdgeContext, VedaTraceLogger, VedaTraceLoggerInterface, VedaTraceTransport, vedatrace as default, detectRuntime, devVedatrace, isBrowser, isEdgeRuntime, isLongRunning, isServerless, redact, vedatrace };
package/dist/index.d.mts CHANGED
@@ -1,77 +1,132 @@
1
- import { b as VedaTraceTransport, B as BatcherConfig, I as InternalLogEntry, V as VedaTraceLoggerInterface, R as RuntimeType$1, a as VedaTraceConfig, L as LogMetadata, c as RedactionConfig } from './types-DUNRMOTv.mjs';
2
- export { d as VedaTraceLevel, e as VedaTraceLog } from './types-DUNRMOTv.mjs';
3
- export { ConsoleFormat, ConsoleTransportConfig, HttpTransportConfig, VedaTraceConsoleTransport, VedaTraceHttpTransport } from './transports/index.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-BU0UESs9.mjs';
2
+ export { e as VedaTraceLevel, f as VedaTraceLog } from './types-BU0UESs9.mjs';
3
+ export { ConsoleFormat, ConsoleTransportConfig, HttpTransportConfig, VedaTraceConsoleTransport, VedaTraceHttpTransport, VedaTraceHttpTransportBrowser } from './transports/index.mjs';
4
4
 
5
5
  /**
6
- * Log batcher with background flushing
7
- * Handles queueing, batching, and retry logic
6
+ * VedaTrace Batcher - Context-Aware for Cloudflare Workers
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
15
  */
9
16
 
10
17
  declare class VedaTraceBatcher {
11
18
  private transports;
12
19
  private config;
13
- private onError?;
14
- private onSuccess?;
15
20
  private immediateFlush;
16
21
  private queue;
17
22
  private flushTimer;
23
+ private flushDebounceTimer;
18
24
  private isFlushing;
19
25
  private pendingFlush;
20
- constructor(transports: VedaTraceTransport[], config: BatcherConfig, onError?: ((error: Error) => void) | undefined, onSuccess?: (() => void) | undefined, immediateFlush?: boolean, autoStart?: boolean);
21
- /** Add log to queue */
26
+ private context;
27
+ constructor(transports: VedaTraceTransport[], config: BatcherConfig, immediateFlush?: boolean);
28
+ /** Attach execution context after initialization */
29
+ setContext(ctx: VedaTraceEdgeContext): void;
30
+ /** Get current context */
31
+ getContext(): VedaTraceEdgeContext | undefined;
32
+ /** Add log to queue with context-aware flush */
22
33
  add(log: InternalLogEntry): void;
23
- /** Flush logs to all transports */
34
+ /** Debounced flush - prevents rapid-fire flushes */
35
+ private debouncedFlush;
36
+ /** Flush logs to all transports with waitUntil protection */
24
37
  flush(): Promise<void>;
25
- /** Send logs with retry logic */
26
38
  private sendWithRetry;
27
- /** Start the flush interval timer */
28
39
  private startFlushTimer;
29
- /** Stop the flush timer */
30
40
  stop(): void;
31
- /** Start the flush timer (for manual control in edge runtimes) */
32
41
  start(): void;
33
- /** Delay helper */
34
42
  private delay;
35
- /** Get current queue size */
36
43
  getQueueSize(): number;
44
+ setExecutionContext(ctx: VedaTraceEdgeContext): void;
37
45
  }
38
46
 
39
47
  /**
40
48
  * Core VedaTrace Logger implementation
49
+ * Supports Cloudflare Workers / Pages via withContext() integration
41
50
  */
42
51
 
43
52
  declare class VedaTraceLogger implements VedaTraceLoggerInterface {
44
53
  private batcher;
45
54
  runtime: RuntimeType$1;
46
55
  private config;
47
- private childDefaults;
56
+ private readonly childDefaults;
57
+ private _context;
48
58
  constructor(config?: VedaTraceConfig, childDefaults?: LogMetadata);
49
- /** Initialize the batcher with transports */
50
59
  private initializeBatcher;
51
- /** Set batcher (called from factory function) */
52
60
  setBatcher(batcher: VedaTraceBatcher): void;
53
- /** Log at debug level */
61
+ /** Attach execution context for waitUntil support (Cloudflare Workers / Pages) */
62
+ withContext(ctx: VedaTraceEdgeContext): this;
63
+ /** Check if context is attached */
64
+ hasContext(): boolean;
65
+ /** Get current execution context */
66
+ getContext(): VedaTraceEdgeContext | undefined;
54
67
  debug(message: string, metadata?: LogMetadata): void;
55
- /** Log at info level */
56
68
  info(message: string, metadata?: LogMetadata): void;
57
- /** Log at warn level */
58
69
  warn(message: string, metadata?: LogMetadata): void;
59
- /** Log at error level */
60
70
  error(message: string | Error, metadata?: LogMetadata): void;
61
- /** Log at fatal level */
62
71
  fatal(message: string | Error, metadata?: LogMetadata): void;
63
- /** Internal log method */
64
72
  private log;
65
- /** Create a child logger with default metadata */
66
73
  child(defaults: LogMetadata): VedaTraceLoggerInterface;
67
- /** Flush pending logs */
68
74
  flush(): Promise<void>;
69
- /** Stop the batcher and flush timer */
70
75
  stop(): void;
71
- /** Start the flush timer (for manual control in edge runtimes) */
72
76
  start(): void;
73
- /** Detect runtime environment (legacy, kept for compatibility) */
74
- private detectEnvironment;
77
+ }
78
+
79
+ /**
80
+ * Browser lifecycle management for VedaTrace
81
+ *
82
+ * Handles:
83
+ * - visibilitychange: flush when page becomes hidden
84
+ * - pagehide: final flush when user leaves the page
85
+ * - beforeunload: backup flush before page unload
86
+ *
87
+ * Uses fetch with keepalive: true for final flush to ensure
88
+ * the request completes after the tab is closed.
89
+ */
90
+
91
+ interface BrowserLifecycleConfig {
92
+ transports: VedaTraceTransport[];
93
+ flush(): Promise<void>;
94
+ debug?: boolean;
95
+ }
96
+ /**
97
+ * Browser lifecycle manager
98
+ * Attaches event listeners and handles graceful flush on page exit
99
+ */
100
+ declare class BrowserLifecycle {
101
+ private config;
102
+ private boundVisibilityHandler;
103
+ private boundPageHideHandler;
104
+ private boundBeforeUnloadHandler;
105
+ private boundUnloadHandler;
106
+ private isAttached;
107
+ private pendingFlush;
108
+ constructor(config: BrowserLifecycleConfig);
109
+ /** Start listening for browser lifecycle events */
110
+ attach(): void;
111
+ /** Stop listening for browser lifecycle events */
112
+ detach(): void;
113
+ /** Handle visibility change - flush when page becomes hidden */
114
+ private handleVisibilityChange;
115
+ /** Handle pagehide event - primary flush handler for Safari */
116
+ private handlePageHide;
117
+ /** Handle beforeunload - backup flush mechanism */
118
+ private handleBeforeUnload;
119
+ /** Handle unload - fallback for older browsers */
120
+ private handleUnload;
121
+ /** Schedule a debounced flush (for visibility change) */
122
+ private scheduleFlush;
123
+ /**
124
+ * Final flush using keepalive fetch
125
+ * For sending logs after the page context is destroyed
126
+ */
127
+ private finalFlush;
128
+ /** Check if handlers are attached */
129
+ isActive(): boolean;
75
130
  }
76
131
 
77
132
  /**
@@ -83,46 +138,55 @@ declare class VedaTraceLogger implements VedaTraceLoggerInterface {
83
138
  */
84
139
  declare function redact(obj: unknown, config?: RedactionConfig): unknown;
85
140
 
141
+ type RuntimeType = "node" | "browser" | "cloudflare" | "deno" | "bun" | "edge";
86
142
  /**
87
143
  * Runtime environment detection utility
88
144
  * Detects Node.js, Browser, Cloudflare Workers, Deno, Bun
89
145
  */
90
- type RuntimeType = "node" | "browser" | "cloudflare" | "deno" | "bun" | "edge";
91
146
  declare function detectRuntime(): RuntimeType;
92
147
  declare function isEdgeRuntime(): boolean;
148
+ declare function isServerless(): boolean;
149
+ declare function isLongRunning(): boolean;
150
+ declare function isBrowser(): boolean;
93
151
 
94
152
  /**
95
153
  * VedaTrace SDK - Universal JavaScript logging
96
154
  *
97
- * Import examples:
98
- * import { vedatrace } from 'vedatrace'
99
- * import { VedaTraceHttpTransport } from 'vedatrace/transports'
100
- * import { vedaTraceMiddleware } from 'vedatrace/express'
155
+ * Supports all JavaScript environments:
156
+ * - Cloudflare Workers / Pages: waitUntil() for background flush
157
+ * - Node.js: standard batching with process event handlers
158
+ * - Bun: standard batching with unref timers
159
+ * - Deno: standard batching with unref timers
160
+ * - Browser: batching with visibility lifecycle handling
161
+ * - Generic Edge: immediate flush fallback
162
+ *
163
+ * @example
164
+ * // Cloudflare Worker
165
+ * const logger = vedatrace({ apiKey: 'key', service: 'app' }).withContext(ctx)
166
+ *
167
+ * @example
168
+ * // Node.js / Express
169
+ * const logger = vedatrace({ apiKey: 'key', service: 'app' })
170
+ * // Automatic shutdown handlers via process.on()
171
+ *
172
+ * @example
173
+ * // Browser
174
+ * const logger = vedatrace({ apiKey: 'key', service: 'app' })
175
+ * // Automatic flush on visibilitychange / pagehide
101
176
  */
102
177
 
103
178
  /**
104
179
  * Create a VedaTrace logger instance
105
180
  *
106
- * @example
107
- * ```typescript
108
- * const logger = vedatrace({
109
- * apiKey: 'your-api-key',
110
- * service: 'my-service'
111
- * })
112
- *
113
- * logger.info('Hello world')
114
- * logger.error('Something went wrong', { error: err })
115
- * ```
181
+ * Handles all runtimes with environment-appropriate strategies:
182
+ * 1. Cloudflare/Edge: immediate flush with waitUntil if context available
183
+ * 2. Node/Bun/Deno: standard batching with process/unref timers
184
+ * 3. Browser: batching with visibility lifecycle handlers
116
185
  */
117
186
  declare function vedatrace(config?: VedaTraceConfig): VedaTraceLoggerInterface;
118
187
  /**
119
188
  * Create a console-only logger for development
120
- *
121
- * @example
122
- * ```typescript
123
- * const logger = devVedatrace({ service: 'my-service' })
124
- * ```
125
189
  */
126
190
  declare function devVedatrace(config?: Omit<VedaTraceConfig, "apiKey" | "transports">): VedaTraceLoggerInterface;
127
191
 
128
- export { BatcherConfig, InternalLogEntry, LogMetadata, RedactionConfig, RuntimeType$1 as RuntimeType, VedaTraceBatcher, VedaTraceConfig, VedaTraceLogger, VedaTraceLoggerInterface, VedaTraceTransport, vedatrace as default, detectRuntime, devVedatrace, isEdgeRuntime, redact, vedatrace };
192
+ export { BatcherConfig, BrowserLifecycle, InternalLogEntry, LogMetadata, RedactionConfig, RuntimeType$1 as RuntimeType, VedaTraceBatcher, VedaTraceConfig, VedaTraceEdgeContext, VedaTraceLogger, VedaTraceLoggerInterface, VedaTraceTransport, vedatrace as default, detectRuntime, devVedatrace, isBrowser, isEdgeRuntime, isLongRunning, isServerless, redact, vedatrace };