@senzops/apm-node 1.2.6 → 1.2.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@senzops/apm-node",
3
- "version": "1.2.6",
3
+ "version": "1.2.7",
4
4
  "description": "Universal APM SDK for Senzor",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,7 +22,8 @@ export class Transport {
22
22
  private taskErrorQueue: SenzorError[] = [];
23
23
  private taskLogQueue: SenzorLog[] = [];
24
24
 
25
- private timer: NodeJS.Timeout | null = null;
25
+ private timer: ReturnType<typeof setInterval> | null = null;
26
+ private timerStarted = false;
26
27
  private apmEndpoint: string;
27
28
  private taskEndpoint: string;
28
29
  private isFlushing = false;
@@ -38,15 +39,26 @@ export class Transport {
38
39
  ? baseEndpoint.replace('/apm', '/task')
39
40
  : `${baseEndpoint}/api/ingest/task`;
40
41
 
41
- if (typeof setInterval !== 'undefined') {
42
- this.timer = setInterval(
43
- () => void this.flush(),
44
- config.flushInterval || 10000
45
- );
46
- if (this.timer && typeof this.timer.unref === 'function') {
47
- this.timer.unref();
42
+ // Timer and shutdown flush are deferred to first enqueue.
43
+ // Cloudflare Workers forbids setInterval / process access in global scope,
44
+ // and init() may be called at module evaluation time (e.g. Nitro plugins).
45
+ }
46
+
47
+ private ensureTimer() {
48
+ if (this.timerStarted) return;
49
+ this.timerStarted = true;
50
+
51
+ try {
52
+ if (typeof setInterval !== 'undefined') {
53
+ this.timer = setInterval(
54
+ () => void this.flush(),
55
+ this.config.flushInterval || 10000
56
+ );
57
+ if (this.timer && typeof (this.timer as any).unref === 'function') {
58
+ (this.timer as any).unref();
59
+ }
48
60
  }
49
- }
61
+ } catch {}
50
62
 
51
63
  this.installShutdownFlush();
52
64
  }
@@ -78,6 +90,7 @@ export class Transport {
78
90
  }
79
91
 
80
92
  private enqueue<T>(queue: T[], item: T) {
93
+ this.ensureTimer();
81
94
  queue.push(item);
82
95
 
83
96
  const maxQueueSize = this.config.maxQueueSize ?? 10000;