@zintrust/trace 2.0.0 → 2.1.3

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 (31) hide show
  1. package/README.md +24 -0
  2. package/dist/TraceConnection.d.ts +1 -1
  3. package/dist/build-manifest.json +71 -59
  4. package/dist/dashboard/handlers.d.ts +1 -1
  5. package/dist/dashboard/routes.d.ts +1 -6
  6. package/dist/dashboard/routes.js +4 -1
  7. package/dist/index.d.ts +1 -0
  8. package/dist/index.js +4 -0
  9. package/dist/ingest/TraceIngestGateway.d.ts +1 -1
  10. package/dist/ingest/TraceIngestGateway.js +5 -1
  11. package/dist/migrations/20260331000001_create_zin_trace_entries_table.d.ts +1 -1
  12. package/dist/migrations/20260331000001_create_zin_trace_entries_table.js +1 -1
  13. package/dist/migrations/20260331000002_create_zin_trace_entries_tags_table.d.ts +1 -1
  14. package/dist/migrations/20260331000002_create_zin_trace_entries_tags_table.js +1 -1
  15. package/dist/migrations/20260331000003_create_zin_trace_monitoring_table.d.ts +1 -1
  16. package/dist/migrations/20260331000003_create_zin_trace_monitoring_table.js +1 -1
  17. package/dist/migrations/20260407193000_widen_trace_created_at_for_sql.d.ts +1 -1
  18. package/dist/migrations/20260407193000_widen_trace_created_at_for_sql.js +1 -1
  19. package/dist/register.js +9 -1
  20. package/dist/runtime/BackgroundTaskScheduler.d.ts +38 -0
  21. package/dist/runtime/BackgroundTaskScheduler.js +105 -0
  22. package/dist/storage/ProxyTraceStorage.js +2 -1
  23. package/dist/storage/TraceServiceTag.js +1 -1
  24. package/dist/storage/TraceStorage.d.ts +1 -1
  25. package/dist/types.d.ts +3 -1
  26. package/dist/watchers/ExceptionWatcher.js +10 -2
  27. package/dist/watchers/HttpClientWatcher.js +10 -2
  28. package/dist/watchers/HttpWatcher.js +12 -4
  29. package/dist/watchers/LogWatcher.js +8 -3
  30. package/dist/watchers/QueryWatcher.js +10 -2
  31. package/package.json +1 -1
package/README.md CHANGED
@@ -91,6 +91,30 @@ import serviceManifest from './bootstrap/service-manifest';
91
91
  ProjectRuntime.set({ serviceManifest });
92
92
  ```
93
93
 
94
+ #### Cloudflare Workers waitUntil Support
95
+
96
+ For reliable trace persistence in Cloudflare Workers, set the execution context in your Worker fetch handler:
97
+
98
+ ```ts
99
+ // src/worker.ts or your Worker entrypoint
100
+ import { BackgroundTaskScheduler } from '@zintrust/trace';
101
+
102
+ export default {
103
+ async fetch(request, env, ctx) {
104
+ // Enable reliable trace persistence via ctx.waitUntil()
105
+ BackgroundTaskScheduler.setExecutionContext(ctx);
106
+
107
+ // Initialize and run your ZinTrust app
108
+ const app = createApp();
109
+ await app.boot();
110
+
111
+ return app.handle(request);
112
+ },
113
+ };
114
+ ```
115
+
116
+ This ensures trace writes complete reliably even after the HTTP response is sent. Without this, trace writes may be lost in Workers environments. See [docs/WORKER_WAITUNTIL_SUPPORT.md](./docs/WORKER_WAITUNTIL_SUPPORT.md) for details.
117
+
94
118
  Why this is the preferred path:
95
119
 
96
120
  - The plugin files are the framework-owned opt-in point that ZinTrust already auto-loads during boot.
@@ -1,4 +1,4 @@
1
- import type { IDatabase } from '@zintrust/core';
1
+ import type { IDatabase } from '@zintrust/core/database';
2
2
  type TraceErrorFactory = {
3
3
  createConfigError?(message: string, details?: unknown): Error;
4
4
  };
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@zintrust/trace",
3
- "version": "2.0.0",
4
- "buildDate": "2026-05-20T20:04:23.602Z",
3
+ "version": "2.1.3",
4
+ "buildDate": "2026-05-27T04:11:41.644Z",
5
5
  "buildEnvironment": {
6
- "node": "v20.20.2",
7
- "platform": "linux",
8
- "arch": "x64"
6
+ "node": "v22.22.1",
7
+ "platform": "darwin",
8
+ "arch": "arm64"
9
9
  },
10
10
  "git": {
11
- "commit": "be96835b",
12
- "branch": "master"
11
+ "commit": "c82f6263",
12
+ "branch": "release"
13
13
  },
14
14
  "package": {
15
15
  "engines": {
@@ -22,13 +22,17 @@
22
22
  },
23
23
  "files": {
24
24
  "TraceConnection.d.ts": {
25
- "size": 1500,
26
- "sha256": "0748601bebff011a0b3dbab736617d5e3dffe4af671f4b6f7af03012d58e5464"
25
+ "size": 1509,
26
+ "sha256": "09a6d63712628d917cd6f64550ebf527c7b419da369983a57151808d32a5a666"
27
27
  },
28
28
  "TraceConnection.js": {
29
29
  "size": 4640,
30
30
  "sha256": "c51cc312046b6b2bbe1673f1ff9508425cc7140a1d2341907f67aa36069c09f9"
31
31
  },
32
+ "build-manifest.json": {
33
+ "size": 15929,
34
+ "sha256": "be6384c7124a8d2d845db13d13bdbbafd294b758fb3f4a64af6b4bfdb26c4f86"
35
+ },
32
36
  "cli-register.d.ts": {
33
37
  "size": 255,
34
38
  "sha256": "da8d689fe5ef32e97e755f28017e4d3cb1aa63489073a71907ea41ad5761ede9"
@@ -54,20 +58,20 @@
54
58
  "sha256": "00a82a58e18fb33934f449a9fb379a2f90023570ceed1676eae0aed59b02ddeb"
55
59
  },
56
60
  "dashboard/handlers.d.ts": {
57
- "size": 993,
58
- "sha256": "430f5b294d960e13e2ec39ed5c22f6655f85c46c2de9455c222505c67298ec6a"
61
+ "size": 998,
62
+ "sha256": "788a2c71fdd77adbcef3e41bffe7ddf07f04bc1bdd10e6f19cfcd3ba6b9e858b"
59
63
  },
60
64
  "dashboard/handlers.js": {
61
65
  "size": 10011,
62
66
  "sha256": "a5f7f3d624a844df27ddc9d4908e73d91a4aac9b2aac1aefff1f391913353897"
63
67
  },
64
68
  "dashboard/routes.d.ts": {
65
- "size": 997,
66
- "sha256": "0ac87bc54e57978e8a9ac5924d248e1f984927bb6c819ea5528ab1179b0b50b8"
69
+ "size": 797,
70
+ "sha256": "988be2431f7cde857403dcbd84949f425c1ed668b3c41faa3d104dd891d6c0dc"
67
71
  },
68
72
  "dashboard/routes.js": {
69
- "size": 2501,
70
- "sha256": "06f0bf42214ed7691907443c0a1d5a4d8c3db3ce3f7b8269ba401ff421a54dfd"
73
+ "size": 2627,
74
+ "sha256": "b59d95e8bfe256b2904d6000d672036faa68983e296cd8c82ae782cb0b804fcb"
71
75
  },
72
76
  "dashboard/ui.d.ts": {
73
77
  "size": 117,
@@ -78,52 +82,52 @@
78
82
  "sha256": "6a19ca4b079944cf0070633a0694de28d034f78ff2d1f02af00707cd2434751f"
79
83
  },
80
84
  "index.d.ts": {
81
- "size": 2693,
82
- "sha256": "901aaefe88fb2cc1e7771cd541f41ebcfe6443390bd66905eacbda51f1a6f73d"
85
+ "size": 2770,
86
+ "sha256": "83fdbdc30ce9c926c35cb5a95f480797cb7d12c8a52237dd7a19ec62dfe93dac"
83
87
  },
84
88
  "index.js": {
85
- "size": 3421,
86
- "sha256": "09fa209693e5195efa6a58f18d869af4fef501a25462bc910ed7cec5c975a6bf"
89
+ "size": 3670,
90
+ "sha256": "cb7b0d00db90067076a82dbb339b942dee72803cfe730303081f5102008d1140"
87
91
  },
88
92
  "ingest/TraceIngestGateway.d.ts": {
89
- "size": 786,
90
- "sha256": "3a0a46fa5bbf5367047214533ec0ee92a171ee35a60d25c197eea1eed1ff0f65"
93
+ "size": 791,
94
+ "sha256": "7362cb112369bfa2091cb10ce3fc557bc9be5bf9e17abeba32e23bc58bbf9f57"
91
95
  },
92
96
  "ingest/TraceIngestGateway.js": {
93
- "size": 10936,
94
- "sha256": "c95f0e42f1294d8ae2e406a5910f8e74044c2b586e82e726876d75ddfafef27e"
97
+ "size": 11105,
98
+ "sha256": "d4f07c03cb9e34293058255e8ad7d7d83e2b332256f6ca3a3d2011dcb857fb37"
95
99
  },
96
100
  "migrations/20260331000001_create_zin_trace_entries_table.d.ts": {
97
- "size": 304,
98
- "sha256": "7d99b4d7c02855d6e68ad87e39dffff5fec83a406bdf2414665c23ba3ce8711d"
101
+ "size": 313,
102
+ "sha256": "be62bd700b040e7df58dc76f8282fa1400b57d19f089a63b3f4677df16e203d4"
99
103
  },
100
104
  "migrations/20260331000001_create_zin_trace_entries_table.js": {
101
- "size": 955,
102
- "sha256": "6aa54f3a6a95a78714e9b14d7f6adb4e94e9420e4e17ac467b43fef12bbc2583"
105
+ "size": 964,
106
+ "sha256": "49c5df2a7caac1da82652db591914bfa00fde7591814a9f945f861f0a705fba7"
103
107
  },
104
108
  "migrations/20260331000002_create_zin_trace_entries_tags_table.d.ts": {
105
- "size": 304,
106
- "sha256": "6d07c135b0fa34acbda2f7f0788e4db657b00ba7c7459e2b203c8ded9c55b9dc"
109
+ "size": 313,
110
+ "sha256": "95d6824268fc69ec4a8cab4a551ca600d27983d0e0b13c51ffc5c9b4e55bec39"
107
111
  },
108
112
  "migrations/20260331000002_create_zin_trace_entries_tags_table.js": {
109
- "size": 737,
110
- "sha256": "614280fc7f3a5826c3e8334e2a38d707b562217fb68a660aff0d9a6c072f7b2a"
113
+ "size": 746,
114
+ "sha256": "726375b0f404d2c6d55d195e52bf978e6f95b6ad4d241fa23ed7d8d1bb369412"
111
115
  },
112
116
  "migrations/20260331000003_create_zin_trace_monitoring_table.d.ts": {
113
- "size": 308,
114
- "sha256": "019e84f70cedf363eaf85a115d935abed841627b4a31d7fa34131352d402187a"
117
+ "size": 317,
118
+ "sha256": "d45a119cd221faaaf804fd466d2e5678e137751fc9ab3bc6bc6992b1190fb7fb"
115
119
  },
116
120
  "migrations/20260331000003_create_zin_trace_monitoring_table.js": {
117
- "size": 522,
118
- "sha256": "dd80ea1fdfe6ea7f57330b2dff530d97ee8054081436f41bbf234b814cb1df19"
121
+ "size": 531,
122
+ "sha256": "fe52d93d169ae65e6092f05535b35c728d11253580b39bdd629a64fdd94c64ab"
119
123
  },
120
124
  "migrations/20260407193000_widen_trace_created_at_for_sql.d.ts": {
121
- "size": 335,
122
- "sha256": "6e9c826a08a983927d9e801b2740f58d0cf6fbf7bff02ee5c1fd01227f73b741"
125
+ "size": 344,
126
+ "sha256": "9243e0feab40bda7bfc1e5417d93c64b28166df5326adad30bdbcf9a03a7c7fb"
123
127
  },
124
128
  "migrations/20260407193000_widen_trace_created_at_for_sql.js": {
125
- "size": 1185,
126
- "sha256": "1de9d8e4a91ffeaed4a0bc5cf942e428cd76bdb6b65bc67fa76ef62f3c99e94d"
129
+ "size": 1194,
130
+ "sha256": "ebb0d825d66b774f010f0fe689200ef7e3f1bcd74612d7090ff372daa9754a61"
127
131
  },
128
132
  "migrations/index.d.ts": {
129
133
  "size": 280,
@@ -146,16 +150,24 @@
146
150
  "sha256": "535869aa1bfcdf0ec26fad27d7f18bb8822bc586c38fcc5ffdde5274ecbea17e"
147
151
  },
148
152
  "register.js": {
149
- "size": 19680,
150
- "sha256": "042a7585e68a8a676e25f33c9a176c6f263bccb4a63090b4d2cea52b9bcdd193"
153
+ "size": 19896,
154
+ "sha256": "4516e527351016a9c2a0ea751a130cc3c42e19cd6cd817bf422663d2a613bf51"
155
+ },
156
+ "runtime/BackgroundTaskScheduler.d.ts": {
157
+ "size": 1182,
158
+ "sha256": "4ebb3549575f57210ffffce663a2f62e2d8a8ff0c56ad265259e559736ebccb2"
159
+ },
160
+ "runtime/BackgroundTaskScheduler.js": {
161
+ "size": 3254,
162
+ "sha256": "aa304666a000e6ccb0ae0a5e2bed701d610cf371955d4137a085c970fa11e230"
151
163
  },
152
164
  "storage/ProxyTraceStorage.d.ts": {
153
165
  "size": 339,
154
166
  "sha256": "9c724ff342dfe82da12e7cce95593f6623faa80c40f97593719b8e78e95f266d"
155
167
  },
156
168
  "storage/ProxyTraceStorage.js": {
157
- "size": 4330,
158
- "sha256": "672c95ed022504b2e5be62f3c2b31bac168ac06158d793dd020f7e38907e8f64"
169
+ "size": 4379,
170
+ "sha256": "697db8971137d42804d9abd375839f45b443589ef72d368a1b9f715fd8c5b8af"
159
171
  },
160
172
  "storage/TraceContentBudget.d.ts": {
161
173
  "size": 1306,
@@ -186,12 +198,12 @@
186
198
  "sha256": "35d93c82d6db80071946adaa8e40d012408b469949f1002f85ae3fd20b0a70c8"
187
199
  },
188
200
  "storage/TraceServiceTag.js": {
189
- "size": 1916,
190
- "sha256": "1ddad82563c98ddbb4033e9b8fb31901d635cffa7024d499a896f0d7a6d00900"
201
+ "size": 1923,
202
+ "sha256": "7e84325813e5399465e4794199895460aa7223c6a7b64339f0b241a7ca1295ee"
191
203
  },
192
204
  "storage/TraceStorage.d.ts": {
193
- "size": 517,
194
- "sha256": "c9c215aaa414f7b0c1fec6c82b054fc52bdf97af58f96f35c7f96672fb859c31"
205
+ "size": 526,
206
+ "sha256": "6de5990ca98178d909012e6678d95b30be1bfa29fdb0519974b37df60fbb3d20"
195
207
  },
196
208
  "storage/TraceStorage.js": {
197
209
  "size": 15322,
@@ -214,8 +226,8 @@
214
226
  "sha256": "ed5e83e108cd15f9a3976be71e966bdf0c44d8e0266d1d2dbad189f0885ad501"
215
227
  },
216
228
  "types.d.ts": {
217
- "size": 10037,
218
- "sha256": "68a2de953af2fce1a12447078177ff886f10e853c760378e13478b2558648868"
229
+ "size": 10214,
230
+ "sha256": "cc3510f7c6c859795d368a1fca2f3ec2322135387bb60bd189fd72ac833b9a7c"
219
231
  },
220
232
  "types.js": {
221
233
  "size": 696,
@@ -330,8 +342,8 @@
330
342
  "sha256": "0f58c50fd77704151399ca6cb6ec7890a9aef86afe28235951971f8cc9c1d600"
331
343
  },
332
344
  "watchers/ExceptionWatcher.js": {
333
- "size": 3816,
334
- "sha256": "52d21188c499fa409fa50691fee3d27ea08ad93b26199d9a42c08e76217d6de2"
345
+ "size": 4253,
346
+ "sha256": "7d4c63675bb78857ba8a7be91e11a770201fd80d2fa7ee857d2e78dab3665f15"
335
347
  },
336
348
  "watchers/GateWatcher.d.ts": {
337
349
  "size": 262,
@@ -346,16 +358,16 @@
346
358
  "sha256": "dfb13bba526d5338e4dcd7a5aa0f72a61a03d9e2f6a250250c0bb8f0054c9712"
347
359
  },
348
360
  "watchers/HttpClientWatcher.js": {
349
- "size": 5898,
350
- "sha256": "320b5a264f26bfeb29ba9246ffa65b79e244db715a4614d69a1425364062d5f9"
361
+ "size": 6335,
362
+ "sha256": "224b3cf85eba2e7f700a8cdcc61a0fe285ff0460a5c6550c83b0ea506e41c865"
351
363
  },
352
364
  "watchers/HttpWatcher.d.ts": {
353
365
  "size": 96,
354
366
  "sha256": "ce9a95a670f755193fd74ce721dbfa4b30f20c879a6566ebb35229b3b2435429"
355
367
  },
356
368
  "watchers/HttpWatcher.js": {
357
- "size": 6880,
358
- "sha256": "a69795a885d6b350012bb3be7a93a0a1a6e71070873cd172e3bd672b67b4ac08"
369
+ "size": 7302,
370
+ "sha256": "3bfe58db8346474da1efb6a5a050786e2d70d43ab02d437c116510254387735e"
359
371
  },
360
372
  "watchers/JobWatcher.d.ts": {
361
373
  "size": 441,
@@ -370,8 +382,8 @@
370
382
  "sha256": "f3ddc5f8b58c6c86ac6b464dd48e5a55e79ab2bf2e735feacffc7480e4ccc0c4"
371
383
  },
372
384
  "watchers/LogWatcher.js": {
373
- "size": 3290,
374
- "sha256": "0297479465b25f93ab0b11bfd6c2083afe9023fdddaa1b7841d12d68b294fa3e"
385
+ "size": 3626,
386
+ "sha256": "6b9e86824141e170a731b44c2b3af6f3dff1e06a9d09b6a7e6c9c01453b8c6cf"
375
387
  },
376
388
  "watchers/MailWatcher.d.ts": {
377
389
  "size": 244,
@@ -410,8 +422,8 @@
410
422
  "sha256": "5d5046c65e5b683369c7709f1acd09b60aec3e7f44748fd1baeb35498836465b"
411
423
  },
412
424
  "watchers/QueryWatcher.js": {
413
- "size": 2855,
414
- "sha256": "dcb9fe2049e71b5fc850286758737d80738a07dd470d96f36e418d4f54e16263"
425
+ "size": 3293,
426
+ "sha256": "f2d02707308e091ef065f1d6e264b7f2d799ed4a60b90efe664f9d9f7ad6266a"
415
427
  },
416
428
  "watchers/RedisWatcher.d.ts": {
417
429
  "size": 294,
@@ -2,7 +2,7 @@
2
2
  * TraceDashboard handlers — pure handler functions wired to ITraceStorage.
3
3
  * No auth in this layer — caller mounts middleware as needed.
4
4
  */
5
- import type { IRequest, IResponse } from '@zintrust/core';
5
+ import type { IRequest, IResponse } from '@zintrust/core/http';
6
6
  import type { ITraceStorage } from '../types';
7
7
  export declare const setHandlerStorage: (s: ITraceStorage) => void;
8
8
  export declare function listEntries(req: IRequest, res: IResponse): Promise<void>;
@@ -1,9 +1,4 @@
1
- /**
2
- * Dashboard route registrar for @zintrust/trace.
3
- * Mounts the SPA + all REST API endpoints under the configured basePath.
4
- * Auth is NOT applied here — callers add middleware via routeOptions.
5
- */
6
- import { type IRouter } from '@zintrust/core';
1
+ import { type IRouter } from '@zintrust/core/http';
7
2
  import type { ITraceStorage } from '../types';
8
3
  export type TraceDashboardOptions = {
9
4
  /** Base path for the dashboard, e.g. '/trace'. Defaults to '/trace'. */
@@ -3,7 +3,10 @@
3
3
  * Mounts the SPA + all REST API endpoints under the configured basePath.
4
4
  * Auth is NOT applied here — callers add middleware via routeOptions.
5
5
  */
6
- import { appConfig, ErrorFactory, Router, useDatabase, } from '@zintrust/core';
6
+ import { appConfig } from '@zintrust/core/config';
7
+ import { useDatabase } from '@zintrust/core/database';
8
+ import { ErrorFactory } from '@zintrust/core/errors';
9
+ import { Router } from '@zintrust/core/http';
7
10
  import { TraceConfig } from '../config.js';
8
11
  import { TraceStorage } from '../storage/index.js';
9
12
  import { assertTraceConnectionResolved, resolveDashboardTraceConnectionName, } from '../TraceConnection.js';
package/dist/index.d.ts CHANGED
@@ -11,6 +11,7 @@ export type { ITraceStorage } from './storage';
11
11
  export { TraceContentBudget } from './storage/TraceContentBudget';
12
12
  export { TraceContentRedaction } from './storage/TraceContentRedaction';
13
13
  export { TraceContext } from './context';
14
+ export { BackgroundTaskScheduler } from './runtime/BackgroundTaskScheduler';
14
15
  export { registerTraceDashboard, registerTraceRoutes } from './dashboard/routes';
15
16
  export type { TraceDashboardOptions, TraceDashboardRegistrationOptions } from './dashboard/routes';
16
17
  export { registerTraceIngestGateway, TraceIngestGateway } from './ingest/TraceIngestGateway';
package/dist/index.js CHANGED
@@ -21,6 +21,10 @@ export { TraceContentRedaction } from './storage/TraceContentRedaction.js';
21
21
  // ---------------------------------------------------------------------------
22
22
  export { TraceContext } from './context.js';
23
23
  // ---------------------------------------------------------------------------
24
+ // Runtime
25
+ // ---------------------------------------------------------------------------
26
+ export { BackgroundTaskScheduler } from './runtime/BackgroundTaskScheduler.js';
27
+ // ---------------------------------------------------------------------------
24
28
  // Dashboard
25
29
  // ---------------------------------------------------------------------------
26
30
  export { registerTraceDashboard, registerTraceRoutes } from './dashboard/routes.js';
@@ -1,4 +1,4 @@
1
- import { type IRouter } from '@zintrust/core';
1
+ import { type IRouter } from '@zintrust/core/http';
2
2
  import type { ITraceStorage } from '../types';
3
3
  type TraceIngestGatewaySettings = {
4
4
  basePath: string;
@@ -1,4 +1,8 @@
1
- import { Env, ErrorFactory, Router, SignedRequest, useDatabase, } from '@zintrust/core';
1
+ import { Env } from '@zintrust/core/config';
2
+ import { useDatabase } from '@zintrust/core/database';
3
+ import { ErrorFactory } from '@zintrust/core/errors';
4
+ import { Router, } from '@zintrust/core/http';
5
+ import { SignedRequest } from '@zintrust/core/security';
2
6
  import { TraceConfig } from '../config.js';
3
7
  import { TraceStorage } from '../storage/index.js';
4
8
  const nonces = new Map();
@@ -2,7 +2,7 @@
2
2
  * Migration: CreateZinTraceEntriesTable
3
3
  * Creates the main entries table for @zintrust/trace
4
4
  */
5
- import { type IDatabase } from '@zintrust/core';
5
+ import { type IDatabase } from '@zintrust/core/database';
6
6
  export interface Migration {
7
7
  up(db: IDatabase): Promise<void>;
8
8
  down(db: IDatabase): Promise<void>;
@@ -2,7 +2,7 @@
2
2
  * Migration: CreateZinTraceEntriesTable
3
3
  * Creates the main entries table for @zintrust/trace
4
4
  */
5
- import { MigrationSchema } from '@zintrust/core';
5
+ import { MigrationSchema } from '@zintrust/core/database';
6
6
  export const migration = {
7
7
  async up(db) {
8
8
  const schema = MigrationSchema.create(db);
@@ -2,7 +2,7 @@
2
2
  * Migration: CreateZinTraceEntriesTagsTable
3
3
  * Creates the tag join table for @zintrust/trace
4
4
  */
5
- import { type IDatabase } from '@zintrust/core';
5
+ import { type IDatabase } from '@zintrust/core/database';
6
6
  export interface Migration {
7
7
  up(db: IDatabase): Promise<void>;
8
8
  down(db: IDatabase): Promise<void>;
@@ -2,7 +2,7 @@
2
2
  * Migration: CreateZinTraceEntriesTagsTable
3
3
  * Creates the tag join table for @zintrust/trace
4
4
  */
5
- import { MigrationSchema } from '@zintrust/core';
5
+ import { MigrationSchema } from '@zintrust/core/database';
6
6
  export const migration = {
7
7
  async up(db) {
8
8
  const schema = MigrationSchema.create(db);
@@ -2,7 +2,7 @@
2
2
  * Migration: CreateZinTraceMonitoringTable
3
3
  * Creates the tag watchlist table for @zintrust/trace
4
4
  */
5
- import { type IDatabase } from '@zintrust/core';
5
+ import { type IDatabase } from '@zintrust/core/database';
6
6
  export interface Migration {
7
7
  up(db: IDatabase): Promise<void>;
8
8
  down(db: IDatabase): Promise<void>;
@@ -2,7 +2,7 @@
2
2
  * Migration: CreateZinTraceMonitoringTable
3
3
  * Creates the tag watchlist table for @zintrust/trace
4
4
  */
5
- import { MigrationSchema } from '@zintrust/core';
5
+ import { MigrationSchema } from '@zintrust/core/database';
6
6
  export const migration = {
7
7
  async up(db) {
8
8
  const schema = MigrationSchema.create(db);
@@ -2,7 +2,7 @@
2
2
  * Migration: WidenTraceCreatedAtForSql
3
3
  * Ensures SQL engines that treat INTEGER as 32-bit can store millisecond timestamps.
4
4
  */
5
- import { type IDatabase } from '@zintrust/core';
5
+ import { type IDatabase } from '@zintrust/core/database';
6
6
  export interface Migration {
7
7
  up(db: IDatabase): Promise<void>;
8
8
  down(db: IDatabase): Promise<void>;
@@ -2,7 +2,7 @@
2
2
  * Migration: WidenTraceCreatedAtForSql
3
3
  * Ensures SQL engines that treat INTEGER as 32-bit can store millisecond timestamps.
4
4
  */
5
- import { MigrationSchema } from '@zintrust/core';
5
+ import { MigrationSchema } from '@zintrust/core/database';
6
6
  const alterCreatedAt = async (db) => {
7
7
  const driver = db.getType?.() ?? 'sqlite';
8
8
  if (driver === 'sqlite' || driver === 'd1' || driver === 'd1-remote')
package/dist/register.js CHANGED
@@ -21,6 +21,7 @@
21
21
  */
22
22
  import { TraceConfig } from './config.js';
23
23
  import { TraceContext } from './context.js';
24
+ import { BackgroundTaskScheduler } from './runtime/BackgroundTaskScheduler.js';
24
25
  import { ProxyTraceStorage, TraceServiceTag, TraceStorage } from './storage/index.js';
25
26
  import { TraceContentBudget } from './storage/TraceContentBudget.js';
26
27
  import { TraceContentRedaction } from './storage/TraceContentRedaction.js';
@@ -319,7 +320,14 @@ const createTraceWatcherArgs = async (core, Env, config) => {
319
320
  connectionName: resolvedConnectionName,
320
321
  logger: core.Logger,
321
322
  });
322
- return { storage, config, db: observedDb };
323
+ return {
324
+ storage,
325
+ config,
326
+ db: observedDb,
327
+ scheduleBackgroundTask: (task) => {
328
+ BackgroundTaskScheduler.schedule(task);
329
+ },
330
+ };
323
331
  };
324
332
  const registerTraceWatchers = async (watcherArgs) => {
325
333
  const [{ HttpWatcher }, { QueryWatcher }, { LogWatcher }, { ExceptionWatcher }, { JobWatcher }, { CacheWatcher }, { ScheduleWatcher }, { MailWatcher }, { AuthWatcher }, { EventWatcher }, { ModelWatcher }, { NotificationWatcher }, { RedisWatcher }, { GateWatcher }, { MiddlewareWatcher }, { CommandWatcher }, { BatchWatcher }, { DumpWatcher }, { ViewWatcher }, { HttpClientWatcher },] = await Promise.all([
@@ -0,0 +1,38 @@
1
+ /**
2
+ * BackgroundTaskScheduler — runtime-agnostic background task scheduling.
3
+ * Maps to ctx.waitUntil() in Cloudflare Workers, normal promises in Node.
4
+ */
5
+ type ExecutionContext = {
6
+ waitUntil(promise: Promise<unknown>): void;
7
+ };
8
+ type BackgroundTaskScheduler = {
9
+ schedule(task: Promise<void>): void;
10
+ isAvailable(): boolean;
11
+ };
12
+ export declare const BackgroundTaskScheduler: Readonly<{
13
+ /**
14
+ * Set the Worker execution context for waitUntil support.
15
+ * Call this from your Cloudflare Worker fetch handler:
16
+ * BackgroundTaskScheduler.setExecutionContext(ctx);
17
+ */
18
+ setExecutionContext(ctx: ExecutionContext): void;
19
+ /**
20
+ * Get the current scheduler instance.
21
+ */
22
+ getScheduler(): BackgroundTaskScheduler;
23
+ /**
24
+ * Schedule a background task.
25
+ * In Workers: uses ctx.waitUntil() if context is set
26
+ * In Node: fire-and-forget with error suppression
27
+ */
28
+ schedule(task: Promise<void>): void;
29
+ /**
30
+ * Check if a proper scheduler is available (i.e., Workers context is set).
31
+ */
32
+ isAvailable(): boolean;
33
+ /**
34
+ * Reset the scheduler (useful for testing).
35
+ */
36
+ reset(): void;
37
+ }>;
38
+ export {};
@@ -0,0 +1,105 @@
1
+ /**
2
+ * BackgroundTaskScheduler — runtime-agnostic background task scheduling.
3
+ * Maps to ctx.waitUntil() in Cloudflare Workers, normal promises in Node.
4
+ */
5
+ let _executionContext;
6
+ let _scheduler;
7
+ const isCloudflareRuntime = () => {
8
+ const globalRef = typeof globalThis === 'undefined' ? undefined : globalThis;
9
+ if (!globalRef)
10
+ return false;
11
+ // @ts-ignore - navigator is available in workers
12
+ if (typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') {
13
+ return true;
14
+ }
15
+ return (typeof globalRef.caches !== 'undefined' ||
16
+ typeof globalRef.WebSocketPair === 'function' ||
17
+ typeof globalRef.CF !== 'undefined');
18
+ };
19
+ const createWorkerScheduler = (ctx) => {
20
+ return Object.freeze({
21
+ schedule(task) {
22
+ ctx.waitUntil(task);
23
+ },
24
+ isAvailable() {
25
+ return true;
26
+ },
27
+ });
28
+ };
29
+ const createNodeScheduler = () => {
30
+ return Object.freeze({
31
+ schedule(task) {
32
+ // In Node, fire-and-forget is acceptable since the process doesn't terminate
33
+ // like Workers do. We still catch errors to prevent unhandled rejections.
34
+ task.catch(() => {
35
+ // Silently ignore background task errors to avoid noise
36
+ // The individual watchers handle their own error logging
37
+ });
38
+ },
39
+ isAvailable() {
40
+ return true;
41
+ },
42
+ });
43
+ };
44
+ const createFallbackScheduler = () => {
45
+ return Object.freeze({
46
+ schedule(task) {
47
+ // Fallback: fire-and-forget with error suppression
48
+ task.catch(() => undefined);
49
+ },
50
+ isAvailable() {
51
+ return false;
52
+ },
53
+ });
54
+ };
55
+ export const BackgroundTaskScheduler = Object.freeze({
56
+ /**
57
+ * Set the Worker execution context for waitUntil support.
58
+ * Call this from your Cloudflare Worker fetch handler:
59
+ * BackgroundTaskScheduler.setExecutionContext(ctx);
60
+ */
61
+ setExecutionContext(ctx) {
62
+ _executionContext = ctx;
63
+ _scheduler = createWorkerScheduler(ctx);
64
+ },
65
+ /**
66
+ * Get the current scheduler instance.
67
+ */
68
+ getScheduler() {
69
+ if (_scheduler)
70
+ return _scheduler;
71
+ if (_executionContext) {
72
+ _scheduler = createWorkerScheduler(_executionContext);
73
+ return _scheduler;
74
+ }
75
+ if (isCloudflareRuntime()) {
76
+ // We're in Workers but no context was set - use fallback
77
+ _scheduler = createFallbackScheduler();
78
+ return _scheduler;
79
+ }
80
+ // Node.js or other runtime
81
+ _scheduler = createNodeScheduler();
82
+ return _scheduler;
83
+ },
84
+ /**
85
+ * Schedule a background task.
86
+ * In Workers: uses ctx.waitUntil() if context is set
87
+ * In Node: fire-and-forget with error suppression
88
+ */
89
+ schedule(task) {
90
+ this.getScheduler().schedule(task);
91
+ },
92
+ /**
93
+ * Check if a proper scheduler is available (i.e., Workers context is set).
94
+ */
95
+ isAvailable() {
96
+ return this.getScheduler().isAvailable();
97
+ },
98
+ /**
99
+ * Reset the scheduler (useful for testing).
100
+ */
101
+ reset() {
102
+ _executionContext = undefined;
103
+ _scheduler = undefined;
104
+ },
105
+ });
@@ -1,4 +1,5 @@
1
- import { ErrorFactory, RemoteSignedJson } from '@zintrust/core';
1
+ import { ErrorFactory } from '@zintrust/core/errors';
2
+ import { RemoteSignedJson } from '@zintrust/core/security';
2
3
  const ensureConfigured = (settings) => {
3
4
  if (settings.baseUrl.trim() === '') {
4
5
  throw ErrorFactory.createConfigError('TRACE_PROXY_URL is required when TRACE_PROXY=true');
@@ -1,4 +1,4 @@
1
- import { ErrorFactory } from '@zintrust/core';
1
+ import { ErrorFactory } from '@zintrust/core/errors';
2
2
  const appendServiceTag = (entry, serviceTag) => {
3
3
  const normalizedTag = serviceTag?.trim() ?? '';
4
4
  if (normalizedTag === '' || entry.tags.includes(normalizedTag)) {
@@ -3,7 +3,7 @@
3
3
  * Resolves the correct IDatabase from the app config, then delegates all
4
4
  * read/write operations to the trace storage facade.
5
5
  */
6
- import type { IDatabase } from '@zintrust/core';
6
+ import type { IDatabase } from '@zintrust/core/database';
7
7
  import type { ITraceStorage } from '../types';
8
8
  export declare const TraceStorage: Readonly<{
9
9
  resolveStorage: (db: IDatabase) => ITraceStorage;
package/dist/types.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * Types for @zintrust/trace
3
3
  * Sealed type definitions — no side effects.
4
4
  */
5
- import type { IDatabase } from '@zintrust/core';
5
+ import type { IDatabase } from '@zintrust/core/database';
6
6
  export declare const EntryType: Readonly<{
7
7
  readonly REQUEST: "request";
8
8
  readonly QUERY: "query";
@@ -272,6 +272,8 @@ export interface ITraceWatcherConfig {
272
272
  db?: IDatabase;
273
273
  /** Optional: provide to allow HttpWatcher to register as global middleware. */
274
274
  registerMiddleware?: (fn: (req: unknown, res: unknown, next: () => Promise<void>) => Promise<void>) => void;
275
+ /** Optional: schedule a background task (maps to ctx.waitUntil in Workers, normal promise in Node). */
276
+ scheduleBackgroundTask?: (task: Promise<void>) => void;
275
277
  }
276
278
  export interface ITraceWatcher {
277
279
  register(opts: ITraceWatcherConfig): () => void;
@@ -35,6 +35,7 @@ const buildContent = (err, context) => {
35
35
  };
36
36
  };
37
37
  let _storage = null;
38
+ let _scheduleBackgroundTask = null;
38
39
  let _listenerRefCount = 0;
39
40
  let _ignoreRoutes = [];
40
41
  let _ignorePaths = [];
@@ -72,7 +73,7 @@ const captureException = (err, context) => {
72
73
  const content = buildContent(err, context);
73
74
  const hash = familyHash(`${content.class}:${content.file}:${content.line}`);
74
75
  const uuid = crypto.randomUUID();
75
- storage
76
+ const writePromise = storage
76
77
  .writeEntry({
77
78
  uuid,
78
79
  batchId: context?.batchId ?? TraceContext.getBatchId(),
@@ -85,13 +86,19 @@ const captureException = (err, context) => {
85
86
  })
86
87
  .then(() => storage.markFamilyStale(hash, uuid))
87
88
  .catch(() => undefined);
89
+ // Use background task scheduler if available (Workers waitUntil support)
90
+ if (_scheduleBackgroundTask) {
91
+ _scheduleBackgroundTask(writePromise);
92
+ }
93
+ // Otherwise, the promise is already fire-and-forget with error suppression
88
94
  };
89
95
  export const ExceptionWatcher = Object.freeze({
90
96
  capture: captureException,
91
- register({ storage, config }) {
97
+ register({ storage, config, scheduleBackgroundTask }) {
92
98
  if (config.watchers.exception === false)
93
99
  return () => undefined;
94
100
  _storage = storage;
101
+ _scheduleBackgroundTask = scheduleBackgroundTask ?? null;
95
102
  _ignoreRoutes = config.ignoreRoutes;
96
103
  _ignorePaths = config.ignorePaths;
97
104
  if (_listenerRefCount === 0) {
@@ -104,6 +111,7 @@ export const ExceptionWatcher = Object.freeze({
104
111
  unregisterProcessListeners();
105
112
  }
106
113
  _storage = null;
114
+ _scheduleBackgroundTask = null;
107
115
  _ignoreRoutes = [];
108
116
  _ignorePaths = [];
109
117
  };
@@ -4,6 +4,7 @@ import { AuthTag } from '../utils/authTag.js';
4
4
  import { redactHeaders, redactUnknown } from '../utils/redact.js';
5
5
  import { RequestFilter } from '../utils/requestFilter.js';
6
6
  let _storage = null;
7
+ let _scheduleBackgroundTask = null;
7
8
  let _redactHeaderNames = [];
8
9
  let _redactBodyFields = [];
9
10
  let _ignoreRoutes = [];
@@ -126,7 +127,7 @@ const emit = ({ source, method, url, requestHeaders, responseStatus, duration, r
126
127
  responseBody,
127
128
  error,
128
129
  }, sourceRule, normalizedSource);
129
- _storage
130
+ const writePromise = _storage
130
131
  .writeEntry({
131
132
  uuid: crypto.randomUUID(),
132
133
  batchId: TraceContext.getBatchId(),
@@ -137,13 +138,19 @@ const emit = ({ source, method, url, requestHeaders, responseStatus, duration, r
137
138
  createdAt: TraceContext.now(),
138
139
  })
139
140
  .catch(() => undefined);
141
+ // Use background task scheduler if available (Workers waitUntil support)
142
+ if (_scheduleBackgroundTask) {
143
+ _scheduleBackgroundTask(writePromise);
144
+ }
145
+ // Otherwise, the promise is already fire-and-forget with error suppression
140
146
  };
141
147
  export const HttpClientWatcher = Object.freeze({
142
148
  emit,
143
- register({ storage, config }) {
149
+ register({ storage, config, scheduleBackgroundTask }) {
144
150
  if (!isWatcherEnabled(config.watchers.clientRequest))
145
151
  return () => undefined;
146
152
  _storage = storage;
153
+ _scheduleBackgroundTask = scheduleBackgroundTask ?? null;
147
154
  _clientRequestWatcher =
148
155
  typeof config.watchers.clientRequest === 'object' && config.watchers.clientRequest !== null
149
156
  ? config.watchers.clientRequest
@@ -154,6 +161,7 @@ export const HttpClientWatcher = Object.freeze({
154
161
  _ignorePaths = config.ignorePaths;
155
162
  return () => {
156
163
  _storage = null;
164
+ _scheduleBackgroundTask = null;
157
165
  _clientRequestWatcher = undefined;
158
166
  _redactBodyFields = [];
159
167
  _ignoreRoutes = [];
@@ -1,4 +1,4 @@
1
- import { Logger } from '@zintrust/core';
1
+ import { Logger } from '@zintrust/core/logger';
2
2
  import { TraceContext } from '../context.js';
3
3
  import { EntryType } from '../types.js';
4
4
  import { AuthTag } from '../utils/authTag.js';
@@ -130,7 +130,7 @@ const shouldIgnore = (req, config) => {
130
130
  };
131
131
  const isWatcherEnabled = (config) => config.watchers.request !== false;
132
132
  export const HttpWatcher = Object.freeze({
133
- register({ storage, config, registerMiddleware }) {
133
+ register({ storage, config, registerMiddleware, scheduleBackgroundTask, }) {
134
134
  if (!isWatcherEnabled(config))
135
135
  return () => undefined;
136
136
  if (!registerMiddleware)
@@ -162,14 +162,22 @@ export const HttpWatcher = Object.freeze({
162
162
  isLatest: true,
163
163
  createdAt: TraceContext.now(),
164
164
  };
165
- storage.writeEntry(entry).catch((error) => {
165
+ const writePromise = storage.writeEntry(entry).catch((error) => {
166
166
  Logger.warn('[trace] HttpWatcher writeEntry failed', {
167
167
  method: content.method,
168
168
  uri: content.uri,
169
169
  entryUuid: entry.uuid,
170
170
  error: error instanceof Error ? error.message : String(error),
171
171
  });
172
- }); // fire-and-forget
172
+ });
173
+ // Use background task scheduler if available (Workers waitUntil support)
174
+ if (scheduleBackgroundTask) {
175
+ scheduleBackgroundTask(writePromise);
176
+ }
177
+ else {
178
+ // Fallback to fire-and-forget for backward compatibility
179
+ writePromise.catch(() => undefined);
180
+ }
173
181
  };
174
182
  const completionHandler = registerCompletionHandler(response, persistEntry);
175
183
  await next();
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * LogWatcher — captures Logger output via Logger.addSink().
3
3
  */
4
- import { Logger } from '@zintrust/core';
4
+ import { Logger } from '@zintrust/core/logger';
5
5
  import { TraceContext } from '../context.js';
6
6
  import { EntryType } from '../types.js';
7
7
  import { AuthTag } from '../utils/authTag.js';
@@ -54,7 +54,7 @@ const shouldSkipTraceInfrastructureLog = (message, context) => {
54
54
  isTraceStorageQueryLog(message, context));
55
55
  };
56
56
  export const LogWatcher = Object.freeze({
57
- register({ storage, config }) {
57
+ register({ storage, config, scheduleBackgroundTask }) {
58
58
  if (config.watchers.log === false)
59
59
  return () => undefined;
60
60
  const minPriority = LEVEL_PRIORITY[config.logMinLevel] ?? 1;
@@ -75,7 +75,7 @@ export const LogWatcher = Object.freeze({
75
75
  context: context ?? undefined,
76
76
  hostname: TraceContext.getHostname(),
77
77
  };
78
- storage
78
+ const writePromise = storage
79
79
  .writeEntry({
80
80
  uuid: crypto.randomUUID(),
81
81
  batchId: TraceContext.getBatchId(),
@@ -86,6 +86,11 @@ export const LogWatcher = Object.freeze({
86
86
  createdAt: TraceContext.now(),
87
87
  })
88
88
  .catch(() => undefined);
89
+ // Use background task scheduler if available (Workers waitUntil support)
90
+ if (scheduleBackgroundTask) {
91
+ scheduleBackgroundTask(writePromise);
92
+ }
93
+ // Otherwise, the promise is already fire-and-forget with error suppression
89
94
  });
90
95
  return unsubscribe;
91
96
  },
@@ -7,6 +7,7 @@ import { EntryType } from '../types.js';
7
7
  import { AuthTag } from '../utils/authTag.js';
8
8
  let _storage = null;
9
9
  let _config = null;
10
+ let _scheduleBackgroundTask = null;
10
11
  const bindingsInterpolated = (sql, params) => {
11
12
  // Inline params for display only — safe, not for re-execution.
12
13
  let i = 0;
@@ -51,7 +52,7 @@ const emit = (query, params, duration, connection = 'default') => {
51
52
  const tags = AuthTag.append([]);
52
53
  if (slow)
53
54
  tags.push('slow');
54
- _storage
55
+ const writePromise = _storage
55
56
  .writeEntry({
56
57
  uuid: crypto.randomUUID(),
57
58
  batchId,
@@ -63,16 +64,22 @@ const emit = (query, params, duration, connection = 'default') => {
63
64
  createdAt: TraceContext.now(),
64
65
  })
65
66
  .catch(() => undefined);
67
+ // Use background task scheduler if available (Workers waitUntil support)
68
+ if (_scheduleBackgroundTask) {
69
+ _scheduleBackgroundTask(writePromise);
70
+ }
71
+ // Otherwise, the promise is already fire-and-forget with error suppression
66
72
  };
67
73
  export const QueryWatcher = Object.freeze({
68
74
  emit,
69
- register({ storage, config, db: injectedDb }) {
75
+ register({ storage, config, db: injectedDb, scheduleBackgroundTask, }) {
70
76
  if (config.watchers.query === false)
71
77
  return () => undefined;
72
78
  if (!injectedDb)
73
79
  return () => undefined; // no db available
74
80
  _storage = storage;
75
81
  _config = config;
82
+ _scheduleBackgroundTask = scheduleBackgroundTask ?? null;
76
83
  const db = injectedDb;
77
84
  const handler = (query, params, duration) => {
78
85
  emit(query, params, duration);
@@ -81,6 +88,7 @@ export const QueryWatcher = Object.freeze({
81
88
  return () => {
82
89
  _storage = null;
83
90
  _config = null;
91
+ _scheduleBackgroundTask = null;
84
92
  db.offAfterQuery?.(handler);
85
93
  };
86
94
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zintrust/trace",
3
- "version": "2.0.0",
3
+ "version": "2.1.3",
4
4
  "description": "Trace assistant for ZinTrust: logs requests, queries, exceptions, jobs, and more.",
5
5
  "private": false,
6
6
  "type": "module",