@terreno/api 57.4.0 → 57.6.1

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 (210) hide show
  1. package/README.md +5 -5
  2. package/bunfig.toml +1 -1
  3. package/dist/actions.js +197 -368
  4. package/dist/adminModelRouter.js +88 -173
  5. package/dist/api.js +617 -852
  6. package/dist/audit/auditApp.js +48 -89
  7. package/dist/audit/auditEventModel.js +15 -16
  8. package/dist/audit/cloudTasksEnqueue.js +56 -149
  9. package/dist/audit/diff.js +70 -160
  10. package/dist/audit/processQueue.js +25 -70
  11. package/dist/audit/rbacSink.js +25 -66
  12. package/dist/audit/record.js +129 -251
  13. package/dist/auth.js +432 -681
  14. package/dist/authRecovery.js +155 -269
  15. package/dist/authTokens.js +43 -118
  16. package/dist/betterAuthApp.js +25 -26
  17. package/dist/betterAuthSetup.js +258 -413
  18. package/dist/browserLogsRoute.d.ts +6 -0
  19. package/dist/browserLogsRoute.js +97 -0
  20. package/dist/collectionRegistry.js +32 -47
  21. package/dist/config.js +53 -122
  22. package/dist/configurationApp.js +180 -361
  23. package/dist/configurationPlugin.js +130 -302
  24. package/dist/consentApp.js +276 -421
  25. package/dist/docLoader.js +78 -126
  26. package/dist/envConfigurationPlugin.js +25 -147
  27. package/dist/errors.js +151 -247
  28. package/dist/example.js +28 -34
  29. package/dist/expressServer.d.ts +10 -0
  30. package/dist/expressServer.js +109 -255
  31. package/dist/frameworkModelNames.d.ts +24 -0
  32. package/dist/frameworkModelNames.js +31 -0
  33. package/dist/githubAuth.js +152 -216
  34. package/dist/httpClient.js +176 -264
  35. package/dist/index.d.ts +2 -0
  36. package/dist/index.js +7 -2
  37. package/dist/logger.js +145 -278
  38. package/dist/mcp/auth.js +76 -128
  39. package/dist/mcp/createMCPRequest.js +4 -5
  40. package/dist/mcp/handlers.js +268 -480
  41. package/dist/mcp/query.js +56 -150
  42. package/dist/mcp/registry.js +17 -30
  43. package/dist/mcp/schemaGenerator.js +95 -181
  44. package/dist/mcp/server.js +28 -100
  45. package/dist/mcp/serviceTokens.js +102 -191
  46. package/dist/mcp/toolGenerator.js +78 -191
  47. package/dist/middleware.js +15 -27
  48. package/dist/migrations/fixtures/all-down/20260910120000-one.js +2 -42
  49. package/dist/migrations/fixtures/all-down/20260910120001-two.js +2 -42
  50. package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +1 -39
  51. package/dist/migrations/fixtures/bad-name/not-a-migration.js +1 -39
  52. package/dist/migrations/fixtures/valid/20260910120000-alpha.js +1 -39
  53. package/dist/migrations/fixtures/valid/20260910120001-beta.js +2 -42
  54. package/dist/migrations/generate.js +107 -229
  55. package/dist/migrations/load.js +75 -201
  56. package/dist/migrations/lock.js +54 -126
  57. package/dist/migrations/runner.js +192 -446
  58. package/dist/migrations/schemaCatalog.js +122 -263
  59. package/dist/models/consentForm.js +6 -6
  60. package/dist/models/consentResponse.js +5 -5
  61. package/dist/models/mcpServiceToken.js +66 -156
  62. package/dist/models/notification.js +6 -6
  63. package/dist/models/notificationPreference.js +6 -6
  64. package/dist/models/versionConfig.js +5 -5
  65. package/dist/notifications/notificationService.js +133 -273
  66. package/dist/notifications/notificationsApp.js +85 -179
  67. package/dist/notifications/notificationsBeforeSend.js +19 -64
  68. package/dist/notifiers/googleChatNotifier.js +36 -117
  69. package/dist/notifiers/slackNotifier.d.ts +31 -2
  70. package/dist/notifiers/slackNotifier.js +170 -119
  71. package/dist/notifiers/zoomNotifier.js +60 -111
  72. package/dist/openApi.js +107 -122
  73. package/dist/openApiBuilder.js +117 -159
  74. package/dist/openApiCompat.js +81 -148
  75. package/dist/openApiEtag.js +7 -7
  76. package/dist/openApiValidator.js +177 -286
  77. package/dist/orgs/orgContext.js +87 -157
  78. package/dist/orgs/orgPermissions.js +29 -68
  79. package/dist/orgs/orgPlugin.js +9 -27
  80. package/dist/orgs/organizationModel.js +65 -166
  81. package/dist/orgs/organizationSettings.js +29 -36
  82. package/dist/orgs/orgsApp.js +346 -608
  83. package/dist/permissions.js +88 -181
  84. package/dist/plugins.js +125 -218
  85. package/dist/populate.js +130 -240
  86. package/dist/rateLimit/applyTrustProxy.js +1 -1
  87. package/dist/rateLimit/createStore.js +7 -8
  88. package/dist/rateLimit/memoryStore.js +22 -100
  89. package/dist/rateLimit/middleware.js +48 -99
  90. package/dist/rateLimit/mongoStore.js +54 -112
  91. package/dist/rateLimit/policies.js +41 -75
  92. package/dist/rateLimit/redisStore.js +39 -133
  93. package/dist/rbac/access.js +119 -267
  94. package/dist/rbac/assertAllowed.js +95 -171
  95. package/dist/rbac/auditModel.js +14 -58
  96. package/dist/rbac/backfillAdmins.js +24 -130
  97. package/dist/rbac/fieldViews.js +46 -141
  98. package/dist/rbac/membershipOrgAdminSource.js +28 -99
  99. package/dist/rbac/middleware.js +30 -96
  100. package/dist/rbac/modelRouterAccess.js +130 -307
  101. package/dist/rbac/permissionUtils.js +40 -179
  102. package/dist/rbac/realtimeAccess.js +81 -147
  103. package/dist/rbac/resolve.d.ts +1 -1
  104. package/dist/rbac/resolve.js +148 -342
  105. package/dist/rbac/roleManager.js +318 -700
  106. package/dist/rbac/roleModel.js +44 -171
  107. package/dist/rbac/routes.js +93 -196
  108. package/dist/rbac/scopes.js +39 -133
  109. package/dist/rbac/statements.js +23 -91
  110. package/dist/rbac/userPlugin.js +3 -29
  111. package/dist/realtime/changeStreamWatcher.js +489 -800
  112. package/dist/realtime/deprecation.js +8 -10
  113. package/dist/realtime/queryMatcher.js +110 -191
  114. package/dist/realtime/queryStore.js +35 -118
  115. package/dist/realtime/realtimeApp.js +247 -401
  116. package/dist/realtime/registry.js +19 -30
  117. package/dist/realtime/sessionRevalidation.js +132 -358
  118. package/dist/realtime/socketAuth.js +89 -208
  119. package/dist/realtime/socketUser.js +10 -56
  120. package/dist/requestContext.js +93 -139
  121. package/dist/schemaMetadata.js +179 -274
  122. package/dist/scriptRunner.js +96 -266
  123. package/dist/scrubAdminFields.js +36 -108
  124. package/dist/secretProviders.js +130 -299
  125. package/dist/seedRunner.js +221 -514
  126. package/dist/sync/adminBroadcastScope.js +58 -170
  127. package/dist/sync/adminWindowAccess.js +15 -86
  128. package/dist/sync/adminWindowMutation.js +110 -181
  129. package/dist/sync/executors.js +425 -629
  130. package/dist/sync/loadHarness.js +392 -689
  131. package/dist/sync/models.js +181 -341
  132. package/dist/sync/mutationHandler.js +533 -801
  133. package/dist/sync/registrationSideEffects.js +41 -95
  134. package/dist/sync/registry.js +29 -49
  135. package/dist/sync/routes.js +484 -817
  136. package/dist/sync/scripts/compactTombstones.js +83 -228
  137. package/dist/sync/serialize.js +18 -58
  138. package/dist/sync/socketHandlers.js +253 -478
  139. package/dist/sync/streams.js +49 -117
  140. package/dist/sync/syncApp.js +12 -15
  141. package/dist/sync/syncBatch.js +63 -168
  142. package/dist/sync/syncSeqPlugin.js +430 -702
  143. package/dist/syncConsents.js +113 -253
  144. package/dist/terrenoApp.js +166 -276
  145. package/dist/tests/createTestData.js +146 -256
  146. package/dist/tests/models.js +14 -54
  147. package/dist/tests/mongoTestSetup.js +42 -103
  148. package/dist/tests/testHelper.js +31 -93
  149. package/dist/tests.js +12 -51
  150. package/dist/transformers.js +61 -164
  151. package/dist/utils.js +31 -93
  152. package/dist/vendor/wesleytodd-openapi/index.js +37 -51
  153. package/dist/vendor/wesleytodd-openapi/lib/generate-doc.js +32 -40
  154. package/dist/vendor/wesleytodd-openapi/lib/layer-schema.js +3 -3
  155. package/dist/vendor/wesleytodd-openapi/lib/ui.js +50 -19
  156. package/dist/vendor/wesleytodd-openapi/lib/validate.js +19 -47
  157. package/dist/versionCheckPlugin.js +52 -100
  158. package/dist/webhooks/headerValue.js +2 -2
  159. package/dist/webhooks/idempotency/memoryStore.js +16 -59
  160. package/dist/webhooks/idempotency/mongoStore.js +55 -130
  161. package/dist/webhooks/idempotency/webhookReceipt.js +9 -9
  162. package/dist/webhooks/rawBody.js +1 -1
  163. package/dist/webhooks/timingSafeEqual.js +4 -5
  164. package/dist/webhooks/verifiers/hmac.js +17 -18
  165. package/dist/webhooks/verifiers/sendgrid.js +20 -22
  166. package/dist/webhooks/verifiers/stripe.js +31 -65
  167. package/dist/webhooks/verifiers/twilio.js +23 -73
  168. package/dist/webhooks/webhooksApp.js +78 -177
  169. package/dist/zodOpenApi.js +2 -2
  170. package/package.json +6 -6
  171. package/src/__tests__/browserLogsRoute.test.ts +165 -0
  172. package/src/api.arrayOperations.test.ts +44 -0
  173. package/src/api.ts +64 -39
  174. package/src/audit/auditApp.test.ts +5 -5
  175. package/src/audit/auditEventModel.ts +7 -3
  176. package/src/audit/auditRouter.test.ts +5 -5
  177. package/src/audit/rbacSink.test.ts +2 -2
  178. package/src/audit/record.test.ts +2 -2
  179. package/src/audit/record.ts +3 -2
  180. package/src/auth.ts +1 -1
  181. package/src/authRecovery.test.ts +43 -0
  182. package/src/authRecovery.ts +16 -10
  183. package/src/authTokens.ts +6 -2
  184. package/src/authVerification.test.ts +22 -0
  185. package/src/browserLogsRoute.ts +110 -0
  186. package/src/configurationPlugin.test.ts +1 -11
  187. package/src/expressServer.ts +10 -0
  188. package/src/frameworkModelNames.test.ts +62 -0
  189. package/src/frameworkModelNames.ts +33 -0
  190. package/src/index.ts +7 -0
  191. package/src/isolated/migrateCliConnect.isolated.ts +51 -0
  192. package/src/mcp/auth.test.ts +34 -1
  193. package/src/migrations/cli.test.ts +0 -23
  194. package/src/models/mcpServiceToken.ts +4 -3
  195. package/src/models/notification.ts +3 -2
  196. package/src/models/notificationPreference.ts +1 -1
  197. package/src/notifications/notificationsApp.test.ts +24 -1
  198. package/src/notifiers/slackNotifier.test.ts +186 -1
  199. package/src/notifiers/slackNotifier.ts +167 -8
  200. package/src/orgs/orgPlugin.ts +1 -1
  201. package/src/orgs/organizationModel.ts +11 -6
  202. package/src/plugins.test.ts +5 -0
  203. package/src/plugins.ts +3 -0
  204. package/src/realtime/realtimeApp.ts +1 -1
  205. package/src/sync/integration.test.ts +2 -2
  206. package/src/terrenoApp.ts +3 -0
  207. package/src/types.d.ts +1 -0
  208. package/src/webhooks/idempotency/webhookReceipt.ts +4 -3
  209. package/tsconfig.json +13 -54
  210. package/tsconfig.typedoc.json +2 -0
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
3
  if (k2 === undefined) k2 = k;
15
4
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -43,33 +32,6 @@ var __importStar = (this && this.__importStar) || (function () {
43
32
  return result;
44
33
  };
45
34
  })();
46
- var __read = (this && this.__read) || function (o, n) {
47
- var m = typeof Symbol === "function" && o[Symbol.iterator];
48
- if (!m) return o;
49
- var i = m.call(o), r, ar = [], e;
50
- try {
51
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
52
- }
53
- catch (error) { e = { error: error }; }
54
- finally {
55
- try {
56
- if (r && !r.done && (m = i["return"])) m.call(i);
57
- }
58
- finally { if (e) throw e.error; }
59
- }
60
- return ar;
61
- };
62
- var __values = (this && this.__values) || function(o) {
63
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
64
- if (m) return m.call(o);
65
- if (o && typeof o.length === "number") return {
66
- next: function () {
67
- if (o && i >= o.length) o = void 0;
68
- return { value: o && o[i++], done: !o };
69
- }
70
- };
71
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
72
- };
73
35
  Object.defineProperty(exports, "__esModule", { value: true });
74
36
  exports.requestContextMiddleware = exports.updateRequestContextFromRequest = exports.runWithRequestContextAttributes = exports.runWithRequestContext = exports.getCurrentRequestContextAttributes = exports.setRequestContext = exports.applyRequestContextToSentry = exports.getCurrentLogContext = exports.getCurrentRequestContext = exports.getRequestContextFromAttributes = exports.getSessionIdFromJwtPayload = exports.REQUEST_CONTEXT_ATTRIBUTE_NAMES = void 0;
75
37
  /**
@@ -97,18 +59,18 @@ exports.requestContextMiddleware = exports.updateRequestContextFromRequest = exp
97
59
  * @see {@link getCurrentLogContext}
98
60
  * @module requestContext
99
61
  */
100
- var node_async_hooks_1 = require("node:async_hooks");
101
- var node_crypto_1 = require("node:crypto");
102
- var Sentry = __importStar(require("@sentry/bun"));
103
- var CLOUD_TRACE_CONTEXT_HEADER = "x-cloud-trace-context";
104
- var JOB_ID_HEADER = "x-job-id";
105
- var REQUEST_ID_HEADERS = ["x-request-id", "x-correlation-id", "x-transaction-id"];
106
- var SESSION_ID_HEADER = "x-session-id";
107
- var SPAN_ID_HEADER = "x-span-id";
108
- var TRACE_ID_HEADER = "x-trace-id";
109
- var TRACE_PARENT_HEADER = "traceparent";
110
- var TRACE_SAMPLED_HEADER = "x-trace-sampled";
111
- var USER_ID_HEADER = "x-user-id";
62
+ const node_async_hooks_1 = require("node:async_hooks");
63
+ const node_crypto_1 = require("node:crypto");
64
+ const Sentry = __importStar(require("@sentry/bun"));
65
+ const CLOUD_TRACE_CONTEXT_HEADER = "x-cloud-trace-context";
66
+ const JOB_ID_HEADER = "x-job-id";
67
+ const REQUEST_ID_HEADERS = ["x-request-id", "x-correlation-id", "x-transaction-id"];
68
+ const SESSION_ID_HEADER = "x-session-id";
69
+ const SPAN_ID_HEADER = "x-span-id";
70
+ const TRACE_ID_HEADER = "x-trace-id";
71
+ const TRACE_PARENT_HEADER = "traceparent";
72
+ const TRACE_SAMPLED_HEADER = "x-trace-sampled";
73
+ const USER_ID_HEADER = "x-user-id";
112
74
  /**
113
75
  * Canonical HTTP header names for each correlation field. Use these to propagate context to
114
76
  * downstream services (pair with {@link getCurrentRequestContextAttributes}) or to read it from an
@@ -124,44 +86,44 @@ exports.REQUEST_CONTEXT_ATTRIBUTE_NAMES = {
124
86
  traceSampled: TRACE_SAMPLED_HEADER,
125
87
  userId: USER_ID_HEADER,
126
88
  };
127
- var requestContextStorage = new node_async_hooks_1.AsyncLocalStorage();
128
- var getHeader = function (req, headerName) {
129
- var value = req.header(headerName);
89
+ const requestContextStorage = new node_async_hooks_1.AsyncLocalStorage();
90
+ const getHeader = (req, headerName) => {
91
+ const value = req.header(headerName);
130
92
  if (!Array.isArray(value)) {
131
93
  return value;
132
94
  }
133
95
  return value[0];
134
96
  };
135
- var parseGoogleCloudTraceContext = function (headerValue) {
97
+ const parseGoogleCloudTraceContext = (headerValue) => {
136
98
  if (!headerValue) {
137
99
  return undefined;
138
100
  }
139
- var _a = __read(headerValue.split(";"), 2), traceAndSpan = _a[0], options = _a[1];
140
- var _b = __read(traceAndSpan.split("/"), 2), traceId = _b[0], spanId = _b[1];
101
+ const [traceAndSpan, options] = headerValue.split(";");
102
+ const [traceId, spanId] = traceAndSpan.split("/");
141
103
  if (!traceId) {
142
104
  return undefined;
143
105
  }
144
106
  return {
145
- spanId: spanId,
146
- traceId: traceId,
107
+ spanId,
108
+ traceId,
147
109
  traceSampled: options === "o=1",
148
110
  };
149
111
  };
150
- var parseTraceParent = function (headerValue) {
112
+ const parseTraceParent = (headerValue) => {
151
113
  if (!headerValue) {
152
114
  return undefined;
153
115
  }
154
- var _a = __read(headerValue.split("-"), 4), _version = _a[0], traceId = _a[1], spanId = _a[2], flags = _a[3];
116
+ const [_version, traceId, spanId, flags] = headerValue.split("-");
155
117
  if (!traceId) {
156
118
  return undefined;
157
119
  }
158
120
  return {
159
- spanId: spanId,
160
- traceId: traceId,
121
+ spanId,
122
+ traceId,
161
123
  traceSampled: flags === "01",
162
124
  };
163
125
  };
164
- var parseTraceSampled = function (value) {
126
+ const parseTraceSampled = (value) => {
165
127
  if (typeof value === "boolean") {
166
128
  return value;
167
129
  }
@@ -173,30 +135,19 @@ var parseTraceSampled = function (value) {
173
135
  }
174
136
  return undefined;
175
137
  };
176
- var getIncomingRequestId = function (req, traceContext) {
177
- var e_1, _a;
178
- try {
179
- for (var REQUEST_ID_HEADERS_1 = __values(REQUEST_ID_HEADERS), REQUEST_ID_HEADERS_1_1 = REQUEST_ID_HEADERS_1.next(); !REQUEST_ID_HEADERS_1_1.done; REQUEST_ID_HEADERS_1_1 = REQUEST_ID_HEADERS_1.next()) {
180
- var headerName = REQUEST_ID_HEADERS_1_1.value;
181
- var headerValue = getHeader(req, headerName);
182
- if (headerValue) {
183
- return headerValue;
184
- }
138
+ const getIncomingRequestId = (req, traceContext) => {
139
+ for (const headerName of REQUEST_ID_HEADERS) {
140
+ const headerValue = getHeader(req, headerName);
141
+ if (headerValue) {
142
+ return headerValue;
185
143
  }
186
144
  }
187
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
188
- finally {
189
- try {
190
- if (REQUEST_ID_HEADERS_1_1 && !REQUEST_ID_HEADERS_1_1.done && (_a = REQUEST_ID_HEADERS_1.return)) _a.call(REQUEST_ID_HEADERS_1);
191
- }
192
- finally { if (e_1) throw e_1.error; }
193
- }
194
- if (traceContext === null || traceContext === void 0 ? void 0 : traceContext.traceId) {
145
+ if (traceContext?.traceId) {
195
146
  return traceContext.traceId;
196
147
  }
197
148
  return (0, node_crypto_1.randomUUID)();
198
149
  };
199
- var getSessionIdFromJwtPayload = function (payload) {
150
+ const getSessionIdFromJwtPayload = (payload) => {
200
151
  if (!payload) {
201
152
  return undefined;
202
153
  }
@@ -206,19 +157,21 @@ var getSessionIdFromJwtPayload = function (payload) {
206
157
  return payload.sessionId;
207
158
  };
208
159
  exports.getSessionIdFromJwtPayload = getSessionIdFromJwtPayload;
209
- var getRequestContextFromAttributes = function (attributes) {
210
- var _a, _b, _c, _d, _e, _f, _g;
211
- if (attributes === void 0) { attributes = {}; }
212
- var cloudTraceContext = parseGoogleCloudTraceContext(attributes[CLOUD_TRACE_CONTEXT_HEADER]);
213
- var traceParentContext = parseTraceParent(attributes[TRACE_PARENT_HEADER]);
214
- var traceContext = cloudTraceContext !== null && cloudTraceContext !== void 0 ? cloudTraceContext : traceParentContext;
160
+ const getRequestContextFromAttributes = (attributes = {}) => {
161
+ const cloudTraceContext = parseGoogleCloudTraceContext(attributes[CLOUD_TRACE_CONTEXT_HEADER]);
162
+ const traceParentContext = parseTraceParent(attributes[TRACE_PARENT_HEADER]);
163
+ const traceContext = cloudTraceContext ?? traceParentContext;
215
164
  return {
216
165
  jobId: attributes[JOB_ID_HEADER],
217
- requestId: (_d = (_c = (_b = (_a = attributes[exports.REQUEST_CONTEXT_ATTRIBUTE_NAMES.requestId]) !== null && _a !== void 0 ? _a : attributes["x-correlation-id"]) !== null && _b !== void 0 ? _b : attributes["x-transaction-id"]) !== null && _c !== void 0 ? _c : traceContext === null || traceContext === void 0 ? void 0 : traceContext.traceId) !== null && _d !== void 0 ? _d : (0, node_crypto_1.randomUUID)(),
166
+ requestId: attributes[exports.REQUEST_CONTEXT_ATTRIBUTE_NAMES.requestId] ??
167
+ attributes["x-correlation-id"] ??
168
+ attributes["x-transaction-id"] ??
169
+ traceContext?.traceId ??
170
+ (0, node_crypto_1.randomUUID)(),
218
171
  sessionId: attributes[SESSION_ID_HEADER],
219
- spanId: (_e = attributes[SPAN_ID_HEADER]) !== null && _e !== void 0 ? _e : traceContext === null || traceContext === void 0 ? void 0 : traceContext.spanId,
220
- traceId: (_f = attributes[TRACE_ID_HEADER]) !== null && _f !== void 0 ? _f : traceContext === null || traceContext === void 0 ? void 0 : traceContext.traceId,
221
- traceSampled: (_g = parseTraceSampled(attributes[TRACE_SAMPLED_HEADER])) !== null && _g !== void 0 ? _g : traceContext === null || traceContext === void 0 ? void 0 : traceContext.traceSampled,
172
+ spanId: attributes[SPAN_ID_HEADER] ?? traceContext?.spanId,
173
+ traceId: attributes[TRACE_ID_HEADER] ?? traceContext?.traceId,
174
+ traceSampled: parseTraceSampled(attributes[TRACE_SAMPLED_HEADER]) ?? traceContext?.traceSampled,
222
175
  userId: attributes[USER_ID_HEADER],
223
176
  };
224
177
  };
@@ -227,7 +180,7 @@ exports.getRequestContextFromAttributes = getRequestContextFromAttributes;
227
180
  * Returns the full {@link RequestContext} for the active AsyncLocalStorage scope, or `undefined`
228
181
  * when called outside any request/job scope. The logger uses this to enrich each line.
229
182
  */
230
- var getCurrentRequestContext = function () {
183
+ const getCurrentRequestContext = () => {
231
184
  return requestContextStorage.getStore();
232
185
  };
233
186
  exports.getCurrentRequestContext = getCurrentRequestContext;
@@ -236,8 +189,8 @@ exports.getCurrentRequestContext = getCurrentRequestContext;
236
189
  * shape attached to Sentry log attributes and is handy when you need to log or forward the current
237
190
  * context yourself.
238
191
  */
239
- var getCurrentLogContext = function () {
240
- var context = (0, exports.getCurrentRequestContext)();
192
+ const getCurrentLogContext = () => {
193
+ const context = (0, exports.getCurrentRequestContext)();
241
194
  if (!context) {
242
195
  return {};
243
196
  }
@@ -252,21 +205,20 @@ var getCurrentLogContext = function () {
252
205
  };
253
206
  };
254
207
  exports.getCurrentLogContext = getCurrentLogContext;
255
- var setSentryTag = function (scope, name, value) {
208
+ const setSentryTag = (scope, name, value) => {
256
209
  if (typeof value === "undefined") {
257
210
  return;
258
211
  }
259
212
  scope.setTag(name, String(value));
260
213
  };
261
- var setSentryContextValue = function (context, name, value) {
214
+ const setSentryContextValue = (context, name, value) => {
262
215
  if (typeof value === "undefined") {
263
216
  return;
264
217
  }
265
218
  context[name] = value;
266
219
  };
267
- var applyRequestContextToSentry = function (context) {
268
- if (context === void 0) { context = (0, exports.getCurrentLogContext)(); }
269
- var scope = Sentry.getCurrentScope();
220
+ const applyRequestContextToSentry = (context = (0, exports.getCurrentLogContext)()) => {
221
+ const scope = Sentry.getCurrentScope();
270
222
  setSentryTag(scope, "request_id", context.requestId);
271
223
  setSentryTag(scope, "session_id", context.sessionId);
272
224
  setSentryTag(scope, "job_id", context.jobId);
@@ -277,7 +229,7 @@ var applyRequestContextToSentry = function (context) {
277
229
  if (context.userId) {
278
230
  scope.setUser({ id: context.userId });
279
231
  }
280
- var sentryContext = {};
232
+ const sentryContext = {};
281
233
  setSentryContextValue(sentryContext, "requestId", context.requestId);
282
234
  setSentryContextValue(sentryContext, "sessionId", context.sessionId);
283
235
  setSentryContextValue(sentryContext, "jobId", context.jobId);
@@ -290,8 +242,8 @@ var applyRequestContextToSentry = function (context) {
290
242
  }
291
243
  };
292
244
  exports.applyRequestContextToSentry = applyRequestContextToSentry;
293
- var setRequestContext = function (updates) {
294
- var context = (0, exports.getCurrentRequestContext)();
245
+ const setRequestContext = (updates) => {
246
+ const context = (0, exports.getCurrentRequestContext)();
295
247
  if (!context) {
296
248
  return;
297
249
  }
@@ -299,7 +251,7 @@ var setRequestContext = function (updates) {
299
251
  (0, exports.applyRequestContextToSentry)(context);
300
252
  };
301
253
  exports.setRequestContext = setRequestContext;
302
- var setAttribute = function (attributes, name, value) {
254
+ const setAttribute = (attributes, name, value) => {
303
255
  if (typeof value === "undefined") {
304
256
  return;
305
257
  }
@@ -310,10 +262,9 @@ var setAttribute = function (attributes, name, value) {
310
262
  * {@link REQUEST_CONTEXT_ATTRIBUTE_NAMES}) so it can be propagated on outbound requests to other
311
263
  * services, keeping the same `requestId`/`traceId` across service boundaries.
312
264
  */
313
- var getCurrentRequestContextAttributes = function (overrides) {
314
- if (overrides === void 0) { overrides = {}; }
315
- var context = __assign(__assign({}, (0, exports.getCurrentLogContext)()), overrides);
316
- var attributes = {};
265
+ const getCurrentRequestContextAttributes = (overrides = {}) => {
266
+ const context = { ...(0, exports.getCurrentLogContext)(), ...overrides };
267
+ const attributes = {};
317
268
  setAttribute(attributes, exports.REQUEST_CONTEXT_ATTRIBUTE_NAMES.requestId, context.requestId);
318
269
  setAttribute(attributes, exports.REQUEST_CONTEXT_ATTRIBUTE_NAMES.sessionId, context.sessionId);
319
270
  setAttribute(attributes, exports.REQUEST_CONTEXT_ATTRIBUTE_NAMES.jobId, context.jobId);
@@ -341,10 +292,12 @@ exports.getCurrentRequestContextAttributes = getCurrentRequestContextAttributes;
341
292
  * });
342
293
  * ```
343
294
  */
344
- var runWithRequestContext = function (context, callback) {
345
- var _a;
346
- var nextContext = __assign(__assign({}, context), { requestId: (_a = context.requestId) !== null && _a !== void 0 ? _a : (0, node_crypto_1.randomUUID)() });
347
- return requestContextStorage.run(nextContext, function () {
295
+ const runWithRequestContext = (context, callback) => {
296
+ const nextContext = {
297
+ ...context,
298
+ requestId: context.requestId ?? (0, node_crypto_1.randomUUID)(),
299
+ };
300
+ return requestContextStorage.run(nextContext, () => {
348
301
  (0, exports.applyRequestContextToSentry)(nextContext);
349
302
  return callback();
350
303
  });
@@ -355,26 +308,27 @@ exports.runWithRequestContext = runWithRequestContext;
355
308
  * those received on an incoming message or forwarded by another service). Parses Cloud Trace / W3C
356
309
  * `traceparent` into `traceId`/`spanId` via {@link getRequestContextFromAttributes}.
357
310
  */
358
- var runWithRequestContextAttributes = function (attributes, callback) {
359
- if (attributes === void 0) { attributes = {}; }
311
+ const runWithRequestContextAttributes = (attributes = {}, callback) => {
360
312
  return (0, exports.runWithRequestContext)((0, exports.getRequestContextFromAttributes)(attributes), callback);
361
313
  };
362
314
  exports.runWithRequestContextAttributes = runWithRequestContextAttributes;
363
- var updateRequestContextFromRequest = function (req, res) {
364
- var _a, _b, _c, _d, _e, _f, _g;
365
- var reqWithContext = req;
366
- var jobId = (_a = reqWithContext.jobId) !== null && _a !== void 0 ? _a : getHeader(req, JOB_ID_HEADER);
367
- var sessionId = (_f = (_e = (_b = (0, exports.getSessionIdFromJwtPayload)(reqWithContext.authTokenPayload)) !== null && _b !== void 0 ? _b : (_d = (_c = reqWithContext.betterAuthSession) === null || _c === void 0 ? void 0 : _c.session) === null || _d === void 0 ? void 0 : _d.id) !== null && _e !== void 0 ? _e : reqWithContext.sessionId) !== null && _f !== void 0 ? _f : getHeader(req, SESSION_ID_HEADER);
368
- var user = req.user;
369
- var userId = (_g = user === null || user === void 0 ? void 0 : user.id) !== null && _g !== void 0 ? _g : ((user === null || user === void 0 ? void 0 : user._id) ? String(user._id) : undefined);
370
- (0, exports.setRequestContext)({ jobId: jobId, sessionId: sessionId, userId: userId });
315
+ const updateRequestContextFromRequest = (req, res) => {
316
+ const reqWithContext = req;
317
+ const jobId = reqWithContext.jobId ?? getHeader(req, JOB_ID_HEADER);
318
+ const sessionId = (0, exports.getSessionIdFromJwtPayload)(reqWithContext.authTokenPayload) ??
319
+ reqWithContext.betterAuthSession?.session?.id ??
320
+ reqWithContext.sessionId ??
321
+ getHeader(req, SESSION_ID_HEADER);
322
+ const user = req.user;
323
+ const userId = user?.id ?? (user?._id ? String(user._id) : undefined);
324
+ (0, exports.setRequestContext)({ jobId, sessionId, userId });
371
325
  if (jobId) {
372
326
  reqWithContext.jobId = jobId;
373
- res === null || res === void 0 ? void 0 : res.setHeader("X-Job-ID", jobId);
327
+ res?.setHeader("X-Job-ID", jobId);
374
328
  }
375
329
  if (sessionId) {
376
330
  reqWithContext.sessionId = sessionId;
377
- res === null || res === void 0 ? void 0 : res.setHeader("X-Session-ID", sessionId);
331
+ res?.setHeader("X-Session-ID", sessionId);
378
332
  }
379
333
  };
380
334
  exports.updateRequestContextFromRequest = updateRequestContextFromRequest;
@@ -386,22 +340,22 @@ exports.updateRequestContextFromRequest = updateRequestContextFromRequest;
386
340
  * downstream logs are correlated. A later auth-aware pass ({@link updateRequestContextFromRequest})
387
341
  * fills in `userId`/`sessionId`.
388
342
  */
389
- var requestContextMiddleware = function (req, res, next) {
390
- var cloudTraceContext = parseGoogleCloudTraceContext(getHeader(req, CLOUD_TRACE_CONTEXT_HEADER));
391
- var traceParentContext = parseTraceParent(getHeader(req, TRACE_PARENT_HEADER));
392
- var traceContext = cloudTraceContext !== null && cloudTraceContext !== void 0 ? cloudTraceContext : traceParentContext;
393
- var requestId = getIncomingRequestId(req, traceContext);
394
- var jobId = getHeader(req, JOB_ID_HEADER);
395
- var sessionId = getHeader(req, SESSION_ID_HEADER);
396
- var context = {
397
- jobId: jobId,
398
- requestId: requestId,
399
- sessionId: sessionId,
400
- spanId: traceContext === null || traceContext === void 0 ? void 0 : traceContext.spanId,
401
- traceId: traceContext === null || traceContext === void 0 ? void 0 : traceContext.traceId,
402
- traceSampled: traceContext === null || traceContext === void 0 ? void 0 : traceContext.traceSampled,
343
+ const requestContextMiddleware = (req, res, next) => {
344
+ const cloudTraceContext = parseGoogleCloudTraceContext(getHeader(req, CLOUD_TRACE_CONTEXT_HEADER));
345
+ const traceParentContext = parseTraceParent(getHeader(req, TRACE_PARENT_HEADER));
346
+ const traceContext = cloudTraceContext ?? traceParentContext;
347
+ const requestId = getIncomingRequestId(req, traceContext);
348
+ const jobId = getHeader(req, JOB_ID_HEADER);
349
+ const sessionId = getHeader(req, SESSION_ID_HEADER);
350
+ const context = {
351
+ jobId,
352
+ requestId,
353
+ sessionId,
354
+ spanId: traceContext?.spanId,
355
+ traceId: traceContext?.traceId,
356
+ traceSampled: traceContext?.traceSampled,
403
357
  };
404
- var reqWithContext = req;
358
+ const reqWithContext = req;
405
359
  if (jobId) {
406
360
  reqWithContext.jobId = jobId;
407
361
  }
@@ -410,7 +364,7 @@ var requestContextMiddleware = function (req, res, next) {
410
364
  reqWithContext.sessionId = sessionId;
411
365
  }
412
366
  res.setHeader("X-Request-ID", requestId);
413
- requestContextStorage.run(context, function () {
367
+ requestContextStorage.run(context, () => {
414
368
  (0, exports.updateRequestContextFromRequest)(req, res);
415
369
  next();
416
370
  });