@terreno/api 57.4.0 → 57.6.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/README.md +5 -5
- package/bunfig.toml +1 -1
- package/dist/actions.js +197 -368
- package/dist/adminModelRouter.js +88 -173
- package/dist/api.js +598 -852
- package/dist/audit/auditApp.js +48 -89
- package/dist/audit/auditEventModel.js +15 -16
- package/dist/audit/cloudTasksEnqueue.js +56 -149
- package/dist/audit/diff.js +70 -160
- package/dist/audit/processQueue.js +25 -70
- package/dist/audit/rbacSink.js +25 -66
- package/dist/audit/record.js +129 -251
- package/dist/auth.js +432 -681
- package/dist/authRecovery.js +155 -269
- package/dist/authTokens.js +43 -118
- package/dist/betterAuthApp.js +25 -26
- package/dist/betterAuthSetup.js +258 -413
- package/dist/browserLogsRoute.d.ts +6 -0
- package/dist/browserLogsRoute.js +97 -0
- package/dist/collectionRegistry.js +32 -47
- package/dist/config.js +53 -122
- package/dist/configurationApp.js +180 -361
- package/dist/configurationPlugin.js +130 -302
- package/dist/consentApp.js +276 -421
- package/dist/docLoader.js +78 -126
- package/dist/envConfigurationPlugin.js +25 -147
- package/dist/errors.js +151 -247
- package/dist/example.js +28 -34
- package/dist/expressServer.d.ts +10 -0
- package/dist/expressServer.js +109 -255
- package/dist/frameworkModelNames.d.ts +24 -0
- package/dist/frameworkModelNames.js +31 -0
- package/dist/githubAuth.js +152 -216
- package/dist/httpClient.js +176 -264
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -2
- package/dist/logger.js +145 -278
- package/dist/mcp/auth.js +76 -128
- package/dist/mcp/createMCPRequest.js +4 -5
- package/dist/mcp/handlers.js +268 -480
- package/dist/mcp/query.js +56 -150
- package/dist/mcp/registry.js +17 -30
- package/dist/mcp/schemaGenerator.js +95 -181
- package/dist/mcp/server.js +28 -100
- package/dist/mcp/serviceTokens.js +102 -191
- package/dist/mcp/toolGenerator.js +78 -191
- package/dist/middleware.js +15 -27
- package/dist/migrations/fixtures/all-down/20260910120000-one.js +2 -42
- package/dist/migrations/fixtures/all-down/20260910120001-two.js +2 -42
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +1 -39
- package/dist/migrations/fixtures/bad-name/not-a-migration.js +1 -39
- package/dist/migrations/fixtures/valid/20260910120000-alpha.js +1 -39
- package/dist/migrations/fixtures/valid/20260910120001-beta.js +2 -42
- package/dist/migrations/generate.js +107 -229
- package/dist/migrations/load.js +75 -201
- package/dist/migrations/lock.js +54 -126
- package/dist/migrations/runner.js +192 -446
- package/dist/migrations/schemaCatalog.js +122 -263
- package/dist/models/consentForm.js +6 -6
- package/dist/models/consentResponse.js +5 -5
- package/dist/models/mcpServiceToken.js +66 -156
- package/dist/models/notification.js +6 -6
- package/dist/models/notificationPreference.js +6 -6
- package/dist/models/versionConfig.js +5 -5
- package/dist/notifications/notificationService.js +133 -273
- package/dist/notifications/notificationsApp.js +85 -179
- package/dist/notifications/notificationsBeforeSend.js +19 -64
- package/dist/notifiers/googleChatNotifier.js +36 -117
- package/dist/notifiers/slackNotifier.d.ts +31 -2
- package/dist/notifiers/slackNotifier.js +170 -119
- package/dist/notifiers/zoomNotifier.js +60 -111
- package/dist/openApi.js +107 -122
- package/dist/openApiBuilder.js +117 -159
- package/dist/openApiCompat.js +81 -148
- package/dist/openApiEtag.js +7 -7
- package/dist/openApiValidator.js +177 -286
- package/dist/orgs/orgContext.js +87 -157
- package/dist/orgs/orgPermissions.js +29 -68
- package/dist/orgs/orgPlugin.js +9 -27
- package/dist/orgs/organizationModel.js +65 -166
- package/dist/orgs/organizationSettings.js +29 -36
- package/dist/orgs/orgsApp.js +346 -608
- package/dist/permissions.js +88 -181
- package/dist/plugins.js +125 -218
- package/dist/populate.js +130 -240
- package/dist/rateLimit/applyTrustProxy.js +1 -1
- package/dist/rateLimit/createStore.js +7 -8
- package/dist/rateLimit/memoryStore.js +22 -100
- package/dist/rateLimit/middleware.js +48 -99
- package/dist/rateLimit/mongoStore.js +54 -112
- package/dist/rateLimit/policies.js +41 -75
- package/dist/rateLimit/redisStore.js +39 -133
- package/dist/rbac/access.js +119 -267
- package/dist/rbac/assertAllowed.js +95 -171
- package/dist/rbac/auditModel.js +14 -58
- package/dist/rbac/backfillAdmins.js +24 -130
- package/dist/rbac/fieldViews.js +46 -141
- package/dist/rbac/membershipOrgAdminSource.js +28 -99
- package/dist/rbac/middleware.js +30 -96
- package/dist/rbac/modelRouterAccess.js +130 -307
- package/dist/rbac/permissionUtils.js +40 -179
- package/dist/rbac/realtimeAccess.js +81 -147
- package/dist/rbac/resolve.d.ts +1 -1
- package/dist/rbac/resolve.js +148 -342
- package/dist/rbac/roleManager.js +318 -700
- package/dist/rbac/roleModel.js +44 -171
- package/dist/rbac/routes.js +93 -196
- package/dist/rbac/scopes.js +39 -133
- package/dist/rbac/statements.js +23 -91
- package/dist/rbac/userPlugin.js +3 -29
- package/dist/realtime/changeStreamWatcher.js +489 -800
- package/dist/realtime/deprecation.js +8 -10
- package/dist/realtime/queryMatcher.js +110 -191
- package/dist/realtime/queryStore.js +35 -118
- package/dist/realtime/realtimeApp.js +247 -401
- package/dist/realtime/registry.js +19 -30
- package/dist/realtime/sessionRevalidation.js +132 -358
- package/dist/realtime/socketAuth.js +89 -208
- package/dist/realtime/socketUser.js +10 -56
- package/dist/requestContext.js +93 -139
- package/dist/schemaMetadata.js +179 -274
- package/dist/scriptRunner.js +96 -266
- package/dist/scrubAdminFields.js +36 -108
- package/dist/secretProviders.js +130 -299
- package/dist/seedRunner.js +221 -514
- package/dist/sync/adminBroadcastScope.js +58 -170
- package/dist/sync/adminWindowAccess.js +15 -86
- package/dist/sync/adminWindowMutation.js +110 -181
- package/dist/sync/executors.js +425 -629
- package/dist/sync/loadHarness.js +392 -689
- package/dist/sync/models.js +181 -341
- package/dist/sync/mutationHandler.js +533 -801
- package/dist/sync/registrationSideEffects.js +41 -95
- package/dist/sync/registry.js +29 -49
- package/dist/sync/routes.js +484 -817
- package/dist/sync/scripts/compactTombstones.js +83 -228
- package/dist/sync/serialize.js +18 -58
- package/dist/sync/socketHandlers.js +253 -478
- package/dist/sync/streams.js +49 -117
- package/dist/sync/syncApp.js +12 -15
- package/dist/sync/syncBatch.js +63 -168
- package/dist/sync/syncSeqPlugin.js +430 -702
- package/dist/syncConsents.js +113 -253
- package/dist/terrenoApp.js +166 -276
- package/dist/tests/createTestData.js +146 -256
- package/dist/tests/models.js +14 -54
- package/dist/tests/mongoTestSetup.js +42 -103
- package/dist/tests/testHelper.js +31 -93
- package/dist/tests.js +12 -51
- package/dist/transformers.js +61 -164
- package/dist/utils.js +31 -93
- package/dist/vendor/wesleytodd-openapi/index.js +37 -51
- package/dist/vendor/wesleytodd-openapi/lib/generate-doc.js +32 -40
- package/dist/vendor/wesleytodd-openapi/lib/layer-schema.js +3 -3
- package/dist/vendor/wesleytodd-openapi/lib/ui.js +50 -19
- package/dist/vendor/wesleytodd-openapi/lib/validate.js +19 -47
- package/dist/versionCheckPlugin.js +52 -100
- package/dist/webhooks/headerValue.js +2 -2
- package/dist/webhooks/idempotency/memoryStore.js +16 -59
- package/dist/webhooks/idempotency/mongoStore.js +55 -130
- package/dist/webhooks/idempotency/webhookReceipt.js +9 -9
- package/dist/webhooks/rawBody.js +1 -1
- package/dist/webhooks/timingSafeEqual.js +4 -5
- package/dist/webhooks/verifiers/hmac.js +17 -18
- package/dist/webhooks/verifiers/sendgrid.js +20 -22
- package/dist/webhooks/verifiers/stripe.js +31 -65
- package/dist/webhooks/verifiers/twilio.js +23 -73
- package/dist/webhooks/webhooksApp.js +78 -177
- package/dist/zodOpenApi.js +2 -2
- package/package.json +6 -6
- package/src/__tests__/browserLogsRoute.test.ts +165 -0
- package/src/audit/auditApp.test.ts +5 -5
- package/src/audit/auditEventModel.ts +7 -3
- package/src/audit/auditRouter.test.ts +5 -5
- package/src/audit/rbacSink.test.ts +2 -2
- package/src/audit/record.test.ts +2 -2
- package/src/audit/record.ts +3 -2
- package/src/auth.ts +1 -1
- package/src/authRecovery.test.ts +43 -0
- package/src/authRecovery.ts +16 -10
- package/src/authTokens.ts +6 -2
- package/src/authVerification.test.ts +22 -0
- package/src/browserLogsRoute.ts +110 -0
- package/src/configurationPlugin.test.ts +1 -11
- package/src/expressServer.ts +10 -0
- package/src/frameworkModelNames.test.ts +62 -0
- package/src/frameworkModelNames.ts +33 -0
- package/src/index.ts +7 -0
- package/src/isolated/migrateCliConnect.isolated.ts +51 -0
- package/src/mcp/auth.test.ts +34 -1
- package/src/migrations/cli.test.ts +0 -23
- package/src/models/mcpServiceToken.ts +4 -3
- package/src/models/notification.ts +3 -2
- package/src/models/notificationPreference.ts +1 -1
- package/src/notifications/notificationsApp.test.ts +24 -1
- package/src/notifiers/slackNotifier.test.ts +186 -1
- package/src/notifiers/slackNotifier.ts +167 -8
- package/src/orgs/orgPlugin.ts +1 -1
- package/src/orgs/organizationModel.ts +11 -6
- package/src/plugins.test.ts +5 -0
- package/src/plugins.ts +3 -0
- package/src/realtime/realtimeApp.ts +1 -1
- package/src/sync/integration.test.ts +2 -2
- package/src/terrenoApp.ts +3 -0
- package/src/types.d.ts +1 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +4 -3
- package/tsconfig.json +13 -54
- package/tsconfig.typedoc.json +2 -0
package/dist/requestContext.js
CHANGED
|
@@ -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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
-
|
|
97
|
+
const parseGoogleCloudTraceContext = (headerValue) => {
|
|
136
98
|
if (!headerValue) {
|
|
137
99
|
return undefined;
|
|
138
100
|
}
|
|
139
|
-
|
|
140
|
-
|
|
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
|
|
146
|
-
traceId
|
|
107
|
+
spanId,
|
|
108
|
+
traceId,
|
|
147
109
|
traceSampled: options === "o=1",
|
|
148
110
|
};
|
|
149
111
|
};
|
|
150
|
-
|
|
112
|
+
const parseTraceParent = (headerValue) => {
|
|
151
113
|
if (!headerValue) {
|
|
152
114
|
return undefined;
|
|
153
115
|
}
|
|
154
|
-
|
|
116
|
+
const [_version, traceId, spanId, flags] = headerValue.split("-");
|
|
155
117
|
if (!traceId) {
|
|
156
118
|
return undefined;
|
|
157
119
|
}
|
|
158
120
|
return {
|
|
159
|
-
spanId
|
|
160
|
-
traceId
|
|
121
|
+
spanId,
|
|
122
|
+
traceId,
|
|
161
123
|
traceSampled: flags === "01",
|
|
162
124
|
};
|
|
163
125
|
};
|
|
164
|
-
|
|
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
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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:
|
|
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:
|
|
220
|
-
traceId:
|
|
221
|
-
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
|
-
|
|
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
|
-
|
|
240
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
268
|
-
|
|
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
|
-
|
|
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
|
-
|
|
294
|
-
|
|
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
|
-
|
|
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
|
-
|
|
314
|
-
|
|
315
|
-
|
|
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
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
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
|
-
|
|
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
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
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
|
|
327
|
+
res?.setHeader("X-Job-ID", jobId);
|
|
374
328
|
}
|
|
375
329
|
if (sessionId) {
|
|
376
330
|
reqWithContext.sessionId = sessionId;
|
|
377
|
-
res
|
|
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
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
jobId
|
|
398
|
-
requestId
|
|
399
|
-
sessionId
|
|
400
|
-
spanId: traceContext
|
|
401
|
-
traceId: traceContext
|
|
402
|
-
traceSampled: traceContext
|
|
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
|
-
|
|
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,
|
|
367
|
+
requestContextStorage.run(context, () => {
|
|
414
368
|
(0, exports.updateRequestContextFromRequest)(req, res);
|
|
415
369
|
next();
|
|
416
370
|
});
|