@terreno/api 57.1.0 → 57.3.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 +17 -7
- package/dist/actions.js +19 -7
- package/dist/adminTypes.d.ts +26 -0
- package/dist/api.d.ts +4 -0
- package/dist/api.js +12 -24
- package/dist/auth.d.ts +23 -10
- package/dist/auth.js +97 -32
- package/dist/authRecovery.d.ts +5 -0
- package/dist/authRecovery.js +350 -0
- package/dist/authTokens.d.ts +8 -0
- package/dist/authTokens.js +162 -0
- package/dist/betterAuth.d.ts +33 -0
- package/dist/betterAuthApp.d.ts +4 -0
- package/dist/betterAuthApp.js +26 -8
- package/dist/betterAuthSetup.d.ts +44 -2
- package/dist/betterAuthSetup.js +288 -18
- package/dist/collectionRegistry.d.ts +38 -0
- package/dist/collectionRegistry.js +72 -0
- package/dist/configurationApp.d.ts +11 -2
- package/dist/configurationApp.js +2 -4
- package/dist/docLoader.js +35 -6
- package/dist/expressServer.d.ts +26 -3
- package/dist/expressServer.js +79 -11
- package/dist/index.d.ts +16 -0
- package/dist/index.js +38 -2
- package/dist/mcp/auth.d.ts +1 -0
- package/dist/mcp/auth.js +47 -25
- package/dist/mcp/handlers.d.ts +1 -1
- package/dist/mcp/handlers.js +67 -233
- package/dist/mcp/query.d.ts +2 -2
- package/dist/mcp/registry.d.ts +1 -5
- package/dist/mcp/registry.js +31 -20
- package/dist/mcp/schemaGenerator.d.ts +2 -7
- package/dist/mcp/schemaGenerator.js +41 -116
- package/dist/mcp/server.d.ts +1 -0
- package/dist/mcp/server.js +1 -0
- package/dist/mcp/serviceTokens.d.ts +16 -0
- package/dist/mcp/serviceTokens.js +345 -0
- package/dist/mcp/types.d.ts +2 -2
- package/dist/models/mcpServiceToken.d.ts +3 -0
- package/dist/models/mcpServiceToken.js +208 -0
- package/dist/openApiBuilder.d.ts +5 -4
- package/dist/openApiBuilder.js +61 -8
- package/dist/openApiCompat.js +1 -1
- package/dist/openApiValidator.js +0 -2
- package/dist/plugins.d.ts +14 -1
- package/dist/plugins.js +17 -6
- package/dist/populate.d.ts +2 -2
- package/dist/populate.js +47 -42
- package/dist/rateLimit/applyTrustProxy.d.ts +3 -0
- package/dist/rateLimit/applyTrustProxy.js +17 -0
- package/dist/rateLimit/createStore.d.ts +2 -0
- package/dist/rateLimit/createStore.js +28 -0
- package/dist/rateLimit/memoryStore.d.ts +2 -0
- package/dist/rateLimit/memoryStore.js +108 -0
- package/dist/rateLimit/middleware.d.ts +11 -0
- package/dist/rateLimit/middleware.js +110 -0
- package/dist/rateLimit/mongoStore.d.ts +3 -0
- package/dist/rateLimit/mongoStore.js +122 -0
- package/dist/rateLimit/policies.d.ts +14 -0
- package/dist/rateLimit/policies.js +151 -0
- package/dist/rateLimit/redisStore.d.ts +3 -0
- package/dist/rateLimit/redisStore.js +155 -0
- package/dist/rateLimit/types.d.ts +54 -0
- package/dist/rateLimit/types.js +9 -0
- package/dist/rbac/access.js +5 -1
- package/dist/rbac/assertAllowed.d.ts +9 -0
- package/dist/rbac/assertAllowed.js +182 -0
- package/dist/rbac/modelRouterAccess.d.ts +1 -0
- package/dist/rbac/modelRouterAccess.js +8 -4
- package/dist/rbac/permissionUtils.js +3 -2
- package/dist/rbac/resolve.js +5 -4
- package/dist/rbac/routes.js +15 -6
- package/dist/rbac/statements.d.ts +10 -0
- package/dist/rbac/statements.js +13 -2
- package/dist/realtime/deprecation.d.ts +12 -0
- package/dist/realtime/deprecation.js +32 -0
- package/dist/realtime/realtimeApp.d.ts +1 -1
- package/dist/realtime/realtimeApp.js +10 -11
- package/dist/realtime/registry.d.ts +0 -31
- package/dist/realtime/registry.js +44 -31
- package/dist/realtime/socketAuth.d.ts +5 -5
- package/dist/realtime/socketAuth.js +11 -1
- package/dist/realtime/socketUser.d.ts +5 -1
- package/dist/realtime/types.d.ts +14 -4
- package/dist/schemaMetadata.d.ts +56 -0
- package/dist/schemaMetadata.js +513 -0
- package/dist/seedRunner.d.ts +88 -0
- package/dist/seedRunner.js +627 -0
- package/dist/sync/executors.d.ts +12 -2
- package/dist/sync/executors.js +8 -10
- package/dist/sync/loadHarness.js +28 -19
- package/dist/sync/models.js +26 -24
- package/dist/sync/mutationHandler.d.ts +9 -0
- package/dist/sync/mutationHandler.js +11 -6
- package/dist/sync/registrationSideEffects.d.ts +35 -0
- package/dist/sync/registrationSideEffects.js +151 -0
- package/dist/sync/registry.d.ts +2 -52
- package/dist/sync/registry.js +38 -187
- package/dist/sync/routes.js +3 -2
- package/dist/sync/scripts/compactTombstones.js +0 -2
- package/dist/sync/syncApp.d.ts +3 -3
- package/dist/sync/syncApp.js +4 -4
- package/dist/sync/syncBatch.js +3 -2
- package/dist/sync/syncSeqPlugin.d.ts +1 -1
- package/dist/sync/syncSeqPlugin.js +20 -16
- package/dist/terrenoApp.d.ts +19 -5
- package/dist/terrenoApp.js +66 -18
- package/dist/tests/models.d.ts +2 -0
- package/dist/tests/models.js +6 -0
- package/dist/types/authToken.d.ts +33 -0
- package/dist/types/authToken.js +2 -0
- package/dist/types/mcpServiceToken.d.ts +41 -0
- package/dist/types/mcpServiceToken.js +2 -0
- package/dist/webhooks/headerValue.d.ts +2 -0
- package/dist/webhooks/headerValue.js +14 -0
- package/dist/webhooks/idempotency/memoryStore.d.ts +10 -0
- package/dist/webhooks/idempotency/memoryStore.js +65 -0
- package/dist/webhooks/idempotency/mongoStore.d.ts +4 -0
- package/dist/webhooks/idempotency/mongoStore.js +150 -0
- package/dist/webhooks/idempotency/webhookReceipt.d.ts +62 -0
- package/dist/webhooks/idempotency/webhookReceipt.js +39 -0
- package/dist/webhooks/rawBody.d.ts +14 -0
- package/dist/webhooks/rawBody.js +22 -0
- package/dist/webhooks/timingSafeEqual.d.ts +4 -0
- package/dist/webhooks/timingSafeEqual.js +17 -0
- package/dist/webhooks/verifiers/hmac.d.ts +15 -0
- package/dist/webhooks/verifiers/hmac.js +49 -0
- package/dist/webhooks/verifiers/sendgrid.d.ts +10 -0
- package/dist/webhooks/verifiers/sendgrid.js +56 -0
- package/dist/webhooks/verifiers/stripe.d.ts +9 -0
- package/dist/webhooks/verifiers/stripe.js +99 -0
- package/dist/webhooks/verifiers/twilio.d.ts +9 -0
- package/dist/webhooks/verifiers/twilio.js +112 -0
- package/dist/webhooks/webhooksApp.d.ts +33 -0
- package/dist/webhooks/webhooksApp.js +195 -0
- package/package.json +5 -6
- package/src/__snapshots__/openApi.test.ts.snap +270 -0
- package/src/__snapshots__/openApiBuilder.test.ts.snap +30 -12
- package/src/__snapshots__/schemaMetadata.test.ts.snap +197 -0
- package/src/actions.ts +20 -13
- package/src/adminModelRouter.test.ts +133 -0
- package/src/adminTypes.ts +23 -0
- package/src/api.arrayOperations.test.ts +43 -31
- package/src/api.hooks.test.ts +38 -29
- package/src/api.query.test.ts +31 -31
- package/src/api.test.ts +95 -96
- package/src/api.ts +16 -27
- package/src/auth.test.ts +107 -51
- package/src/auth.ts +98 -29
- package/src/authRecovery.test.ts +281 -0
- package/src/authRecovery.ts +279 -0
- package/src/authTokens.test.ts +94 -0
- package/src/authTokens.ts +118 -0
- package/src/authVerification.test.ts +293 -0
- package/src/betterAuth.ts +24 -0
- package/src/betterAuthApp.test.ts +1 -0
- package/src/betterAuthApp.ts +27 -9
- package/src/betterAuthSetup.test.ts +568 -38
- package/src/betterAuthSetup.ts +260 -5
- package/src/collectionRegistry.test.ts +165 -0
- package/src/collectionRegistry.ts +115 -0
- package/src/configurationApp.ts +13 -7
- package/src/docLoader.test.ts +45 -1
- package/src/docLoader.ts +37 -6
- package/src/express.d.ts +1 -0
- package/src/expressServer.test.ts +130 -66
- package/src/expressServer.ts +75 -19
- package/src/githubAuth.test.ts +60 -55
- package/src/index.ts +58 -0
- package/src/mcp/auth.test.ts +47 -0
- package/src/mcp/auth.ts +32 -11
- package/src/mcp/handlers.ts +45 -172
- package/src/mcp/integration.test.ts +59 -9
- package/src/mcp/query.ts +2 -4
- package/src/mcp/registry.ts +29 -20
- package/src/mcp/schemaGenerator.test.ts +8 -0
- package/src/mcp/schemaGenerator.ts +49 -131
- package/src/mcp/server.ts +2 -0
- package/src/mcp/serviceTokens.test.ts +330 -0
- package/src/mcp/serviceTokens.ts +307 -0
- package/src/mcp/toolGenerator.test.ts +1 -1
- package/src/mcp/types.ts +2 -6
- package/src/models/mcpServiceToken.test.ts +129 -0
- package/src/models/mcpServiceToken.ts +158 -0
- package/src/openApi.test.ts +80 -64
- package/src/openApiBuilder.test.ts +95 -7
- package/src/openApiBuilder.ts +46 -9
- package/src/openApiCompat.ts +1 -1
- package/src/openApiValidator.test.ts +49 -54
- package/src/openApiValidator.ts +1 -3
- package/src/plugins.test.ts +14 -0
- package/src/plugins.ts +34 -9
- package/src/populate.test.ts +34 -0
- package/src/populate.ts +40 -29
- package/src/rateLimit/applyTrustProxy.ts +17 -0
- package/src/rateLimit/createStore.ts +25 -0
- package/src/rateLimit/memoryStore.ts +40 -0
- package/src/rateLimit/middleware.ts +79 -0
- package/src/rateLimit/mongoStore.test.ts +80 -0
- package/src/rateLimit/mongoStore.ts +76 -0
- package/src/rateLimit/policies.ts +130 -0
- package/src/rateLimit/rateLimit.test.ts +328 -0
- package/src/rateLimit/redisStore.ioredis.test.ts +29 -0
- package/src/rateLimit/redisStore.test.ts +114 -0
- package/src/rateLimit/redisStore.ts +85 -0
- package/src/rateLimit/types.ts +64 -0
- package/src/rbac/access.ts +5 -1
- package/src/rbac/assertAllowed.test.ts +325 -0
- package/src/rbac/assertAllowed.ts +157 -0
- package/src/rbac/backfillAdmins.test.ts +124 -0
- package/src/rbac/fieldViews.test.ts +89 -0
- package/src/rbac/modelRouterAccess.test.ts +126 -0
- package/src/rbac/modelRouterAccess.ts +5 -2
- package/src/rbac/permissionUtils.ts +3 -2
- package/src/rbac/realtimeAccess.test.ts +109 -1
- package/src/rbac/resolve.ts +5 -4
- package/src/rbac/roleManager.test.ts +169 -0
- package/src/rbac/routes.test.ts +233 -0
- package/src/rbac/routes.ts +16 -6
- package/src/rbac/statements.test.ts +2 -0
- package/src/rbac/statements.ts +12 -1
- package/src/realtime/deprecation.test.ts +60 -0
- package/src/realtime/deprecation.ts +32 -0
- package/src/realtime/realtime.test.ts +8 -4
- package/src/realtime/realtimeApp.ts +10 -11
- package/src/realtime/realtimeAppSetup.test.ts +32 -5
- package/src/realtime/registry.ts +48 -42
- package/src/realtime/sessionRevalidation.test.ts +57 -44
- package/src/realtime/socketAuth.test.ts +75 -23
- package/src/realtime/socketAuth.ts +17 -6
- package/src/realtime/socketUser.ts +5 -3
- package/src/realtime/types.ts +14 -4
- package/src/schemaMetadata.test.ts +405 -0
- package/src/schemaMetadata.ts +593 -0
- package/src/scrubAdminFields.test.ts +105 -0
- package/src/seedRunner.test.ts +591 -0
- package/src/seedRunner.ts +464 -0
- package/src/sync/docMatchesMutationData.test.ts +16 -0
- package/src/sync/ensureSyncIndexes.test.ts +54 -15
- package/src/sync/executors.test.ts +40 -0
- package/src/sync/executors.ts +24 -12
- package/src/sync/integration.test.ts +15 -7
- package/src/sync/loadHarness.ts +28 -19
- package/src/sync/models.ts +32 -18
- package/src/sync/mutationHandler.test.ts +16 -16
- package/src/sync/mutationHandler.ts +9 -7
- package/src/sync/registrationSideEffects.test.ts +126 -0
- package/src/sync/registrationSideEffects.ts +131 -0
- package/src/sync/registry.ts +45 -171
- package/src/sync/routes.ts +3 -2
- package/src/sync/scripts/compactTombstones.ts +0 -2
- package/src/sync/sessionRevalidationIntegration.test.ts +12 -11
- package/src/sync/sync.test.ts +25 -23
- package/src/sync/syncApp.ts +4 -4
- package/src/sync/syncBatch.ts +3 -2
- package/src/sync/syncFrontier.test.ts +17 -15
- package/src/sync/syncPhaseC.test.ts +54 -43
- package/src/sync/syncRoutes.test.ts +51 -36
- package/src/sync/syncSeqPlugin.ts +66 -44
- package/src/sync/syncSocket.test.ts +112 -81
- package/src/terrenoApp.mcpServiceTokens.test.ts +173 -0
- package/src/terrenoApp.ts +87 -15
- package/src/tests/models.ts +14 -1
- package/src/types/authToken.ts +43 -0
- package/src/types/mcpServiceToken.ts +54 -0
- package/src/webhooks/headerValue.test.ts +22 -0
- package/src/webhooks/headerValue.ts +12 -0
- package/src/webhooks/idempotency/memoryStore.ts +34 -0
- package/src/webhooks/idempotency/mongoStore.test.ts +274 -0
- package/src/webhooks/idempotency/mongoStore.ts +83 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +70 -0
- package/src/webhooks/rawBody.test.ts +74 -0
- package/src/webhooks/rawBody.ts +23 -0
- package/src/webhooks/timingSafeEqual.ts +16 -0
- package/src/webhooks/verifiers/hmac.test.ts +110 -0
- package/src/webhooks/verifiers/hmac.ts +54 -0
- package/src/webhooks/verifiers/providerVerifiers.test.ts +388 -0
- package/src/webhooks/verifiers/sendgrid.ts +59 -0
- package/src/webhooks/verifiers/stripe.ts +67 -0
- package/src/webhooks/verifiers/twilio.ts +69 -0
- package/src/webhooks/webhooksApp.test.ts +221 -0
- package/src/webhooks/webhooksApp.ts +144 -0
package/dist/terrenoApp.js
CHANGED
|
@@ -102,19 +102,25 @@ var cors_1 = __importDefault(require("cors"));
|
|
|
102
102
|
var express_1 = __importDefault(require("express"));
|
|
103
103
|
var qs_1 = __importDefault(require("qs"));
|
|
104
104
|
var auth_1 = require("./auth");
|
|
105
|
+
var betterAuthSetup_1 = require("./betterAuthSetup");
|
|
105
106
|
var configurationApp_1 = require("./configurationApp");
|
|
106
107
|
var errors_1 = require("./errors");
|
|
107
108
|
var expressServer_1 = require("./expressServer");
|
|
108
109
|
var githubAuth_1 = require("./githubAuth");
|
|
109
110
|
var logger_1 = require("./logger");
|
|
110
111
|
var server_1 = require("./mcp/server");
|
|
112
|
+
var serviceTokens_1 = require("./mcp/serviceTokens");
|
|
111
113
|
var middleware_1 = require("./middleware");
|
|
112
114
|
var openApiCompat_1 = require("./openApiCompat");
|
|
113
115
|
var openApiEtag_1 = require("./openApiEtag");
|
|
116
|
+
var applyTrustProxy_1 = require("./rateLimit/applyTrustProxy");
|
|
117
|
+
var createStore_1 = require("./rateLimit/createStore");
|
|
118
|
+
var middleware_2 = require("./rateLimit/middleware");
|
|
114
119
|
var realtimeApp_1 = require("./realtime/realtimeApp");
|
|
115
120
|
var requestContext_1 = require("./requestContext");
|
|
116
121
|
var registry_1 = require("./sync/registry");
|
|
117
122
|
var index_1 = __importDefault(require("./vendor/wesleytodd-openapi/index"));
|
|
123
|
+
var rawBody_1 = require("./webhooks/rawBody");
|
|
118
124
|
/**
|
|
119
125
|
* Fluent API for building Express applications with Terreno framework.
|
|
120
126
|
*
|
|
@@ -126,7 +132,7 @@ var index_1 = __importDefault(require("./vendor/wesleytodd-openapi/index"));
|
|
|
126
132
|
* 1. CORS
|
|
127
133
|
* 2. Optional `beforeJsonSetup` (configure the app before JSON parsing)
|
|
128
134
|
* 3. Custom middleware (via addMiddleware)
|
|
129
|
-
* 4. JSON body
|
|
135
|
+
* 4. JSON and urlencoded body parsers (stash `req.rawBody`)
|
|
130
136
|
* 5. Auth routes (/auth/login, /auth/signup, etc.)
|
|
131
137
|
* 6. JWT authentication setup
|
|
132
138
|
* 7. Request logging
|
|
@@ -283,10 +289,7 @@ var TerrenoApp = /** @class */ (function () {
|
|
|
283
289
|
* .start();
|
|
284
290
|
* ```
|
|
285
291
|
*/
|
|
286
|
-
TerrenoApp.prototype.configure = function (
|
|
287
|
-
// noExplicitAny: Model<any> required for invariance — consumers pass arbitrary configuration models
|
|
288
|
-
// biome-ignore lint/suspicious/noExplicitAny: Model<any> required for invariance — consumers pass arbitrary configuration models
|
|
289
|
-
model, options) {
|
|
292
|
+
TerrenoApp.prototype.configure = function (model, options) {
|
|
290
293
|
this.configurationApp = new configurationApp_1.ConfigurationApp(__assign({ model: model }, options));
|
|
291
294
|
return this;
|
|
292
295
|
};
|
|
@@ -294,8 +297,9 @@ var TerrenoApp = /** @class */ (function () {
|
|
|
294
297
|
* Build the Express application without starting the server.
|
|
295
298
|
*
|
|
296
299
|
* Configures the complete middleware stack including:
|
|
297
|
-
* - CORS, JSON parsing,
|
|
298
|
-
* -
|
|
300
|
+
* - CORS, JSON parsing, JWT decode, optional HTTP rate limiting, auth routes
|
|
301
|
+
* - Better Auth session (when BetterAuthApp is registered) before the limiter
|
|
302
|
+
* - Logging, Sentry, OpenAPI, model routers and plugins
|
|
299
303
|
* - Error handling middleware
|
|
300
304
|
*
|
|
301
305
|
* Use this method when you need the Express app instance for testing
|
|
@@ -315,7 +319,8 @@ var TerrenoApp = /** @class */ (function () {
|
|
|
315
319
|
*/
|
|
316
320
|
TerrenoApp.prototype.build = function () {
|
|
317
321
|
var e_1, _a, e_2, _b;
|
|
318
|
-
var
|
|
322
|
+
var _this = this;
|
|
323
|
+
var _c, _d, _e, _f, _g, _h;
|
|
319
324
|
(0, logger_1.setupLogging)(this.options.loggingOptions);
|
|
320
325
|
var app = (0, express_1.default)();
|
|
321
326
|
var options = this.options;
|
|
@@ -329,8 +334,8 @@ var TerrenoApp = /** @class */ (function () {
|
|
|
329
334
|
}
|
|
330
335
|
try {
|
|
331
336
|
// Apply custom middleware before JSON parsing
|
|
332
|
-
for (var
|
|
333
|
-
var fn =
|
|
337
|
+
for (var _j = __values(this.middlewareFns), _k = _j.next(); !_k.done; _k = _j.next()) {
|
|
338
|
+
var fn = _k.value;
|
|
334
339
|
if (fn.length <= 3) {
|
|
335
340
|
// express.RequestHandler (req, res, next)
|
|
336
341
|
app.use(fn);
|
|
@@ -344,18 +349,40 @@ var TerrenoApp = /** @class */ (function () {
|
|
|
344
349
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
345
350
|
finally {
|
|
346
351
|
try {
|
|
347
|
-
if (
|
|
352
|
+
if (_k && !_k.done && (_a = _j.return)) _a.call(_j);
|
|
348
353
|
}
|
|
349
354
|
finally { if (e_1) throw e_1.error; }
|
|
350
355
|
}
|
|
351
|
-
app.use(express_1.default.json(
|
|
352
|
-
|
|
353
|
-
|
|
356
|
+
app.use(express_1.default.json(rawBody_1.jsonBodyParserOptions));
|
|
357
|
+
app.use(express_1.default.urlencoded(rawBody_1.urlencodedBodyParserOptions));
|
|
358
|
+
// JWT decode before rate limiting so authenticated keys use userId.
|
|
359
|
+
// Auth routes mount after the limiter so login is in the auth bucket.
|
|
354
360
|
(0, auth_1.setupAuth)(app, options.userModel);
|
|
355
361
|
app.use(function (req, res, next) {
|
|
356
362
|
(0, requestContext_1.updateRequestContextFromRequest)(req, res);
|
|
357
363
|
next();
|
|
358
364
|
});
|
|
365
|
+
var betterAuthForLimiter = this.registrations.find(function (registration) {
|
|
366
|
+
return "getAuth" in registration &&
|
|
367
|
+
typeof registration.getAuth === "function";
|
|
368
|
+
});
|
|
369
|
+
if (betterAuthForLimiter === null || betterAuthForLimiter === void 0 ? void 0 : betterAuthForLimiter.ensureAuth) {
|
|
370
|
+
var auth = betterAuthForLimiter.ensureAuth();
|
|
371
|
+
app.use((0, betterAuthSetup_1.createBetterAuthSessionMiddleware)(auth, options.userModel));
|
|
372
|
+
(_d = betterAuthForLimiter.markSessionMiddlewareMounted) === null || _d === void 0 ? void 0 : _d.call(betterAuthForLimiter);
|
|
373
|
+
}
|
|
374
|
+
if (options.rateLimit) {
|
|
375
|
+
var betterAuthBasePath = (_e = options.rateLimit.betterAuthBasePath) !== null && _e !== void 0 ? _e : (_f = betterAuthForLimiter === null || betterAuthForLimiter === void 0 ? void 0 : betterAuthForLimiter.getBetterAuthBasePath) === null || _f === void 0 ? void 0 : _f.call(betterAuthForLimiter);
|
|
376
|
+
var rateLimit = __assign(__assign({}, options.rateLimit), (betterAuthBasePath ? { betterAuthBasePath: betterAuthBasePath } : {}));
|
|
377
|
+
(0, applyTrustProxy_1.applyRateLimitTrustProxy)(app, rateLimit);
|
|
378
|
+
var store = (0, createStore_1.createRateLimitStore)(rateLimit);
|
|
379
|
+
app.use((0, middleware_2.createRateLimitMiddleware)(store, rateLimit));
|
|
380
|
+
}
|
|
381
|
+
if (!options.authOptions) {
|
|
382
|
+
options.authOptions = {};
|
|
383
|
+
}
|
|
384
|
+
var authOptions = options.authOptions;
|
|
385
|
+
(0, auth_1.addAuthRoutes)(app, options.userModel, authOptions);
|
|
359
386
|
if (options.logRequests !== false) {
|
|
360
387
|
app.use(expressServer_1.logRequests);
|
|
361
388
|
}
|
|
@@ -411,8 +438,8 @@ var TerrenoApp = /** @class */ (function () {
|
|
|
411
438
|
}
|
|
412
439
|
try {
|
|
413
440
|
// Mount registered model routers and plugins
|
|
414
|
-
for (var
|
|
415
|
-
var registration =
|
|
441
|
+
for (var _l = __values(this.registrations), _m = _l.next(); !_m.done; _m = _l.next()) {
|
|
442
|
+
var registration = _m.value;
|
|
416
443
|
if (this.isModelRouterRegistration(registration)) {
|
|
417
444
|
var router = registration._buildWithContext({
|
|
418
445
|
accessControl: options.accessControl,
|
|
@@ -430,7 +457,7 @@ var TerrenoApp = /** @class */ (function () {
|
|
|
430
457
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
431
458
|
finally {
|
|
432
459
|
try {
|
|
433
|
-
if (
|
|
460
|
+
if (_m && !_m.done && (_b = _l.return)) _b.call(_l);
|
|
434
461
|
}
|
|
435
462
|
finally { if (e_2) throw e_2.error; }
|
|
436
463
|
}
|
|
@@ -439,8 +466,29 @@ var TerrenoApp = /** @class */ (function () {
|
|
|
439
466
|
return "getAuth" in registration &&
|
|
440
467
|
typeof registration.getAuth === "function";
|
|
441
468
|
});
|
|
469
|
+
var betterAuth = (_h = (_g = betterAuthPlugin === null || betterAuthPlugin === void 0 ? void 0 : betterAuthPlugin.ensureAuth) === null || _g === void 0 ? void 0 : _g.call(betterAuthPlugin)) !== null && _h !== void 0 ? _h : betterAuthPlugin === null || betterAuthPlugin === void 0 ? void 0 : betterAuthPlugin.getAuth();
|
|
470
|
+
if (betterAuth) {
|
|
471
|
+
authOptions.syncPasswordResetToBetterAuth = function (user, password) { return __awaiter(_this, void 0, void 0, function () {
|
|
472
|
+
return __generator(this, function (_a) {
|
|
473
|
+
switch (_a.label) {
|
|
474
|
+
case 0: return [4 /*yield*/, (0, betterAuthSetup_1.applyJwtPasswordResetToBetterAuth)(betterAuth, user, password)];
|
|
475
|
+
case 1:
|
|
476
|
+
_a.sent();
|
|
477
|
+
return [2 /*return*/];
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
}); };
|
|
481
|
+
}
|
|
482
|
+
var mcpServiceTokens = (0, serviceTokens_1.resolveMcpServiceTokensOption)(options.mcpServiceTokens);
|
|
483
|
+
if (mcpServiceTokens.enabled) {
|
|
484
|
+
(0, serviceTokens_1.addMcpServiceTokenRoutes)(app, {
|
|
485
|
+
openApi: oapi,
|
|
486
|
+
publicMcpUrl: mcpServiceTokens.publicMcpUrl,
|
|
487
|
+
});
|
|
488
|
+
}
|
|
442
489
|
(0, server_1.mountMCPServer)(app, {
|
|
443
|
-
betterAuth:
|
|
490
|
+
betterAuth: betterAuth,
|
|
491
|
+
mcpServiceTokens: mcpServiceTokens.enabled,
|
|
444
492
|
userModel: options.userModel,
|
|
445
493
|
});
|
|
446
494
|
if (options.configureApp) {
|
package/dist/tests/models.d.ts
CHANGED
package/dist/tests/models.js
CHANGED
|
@@ -52,6 +52,11 @@ var userSchema = new mongoose_1.Schema({
|
|
|
52
52
|
description: "Organization memberships",
|
|
53
53
|
type: [String],
|
|
54
54
|
},
|
|
55
|
+
tokenEpoch: {
|
|
56
|
+
default: 0,
|
|
57
|
+
description: "Incremented on password reset to invalidate outstanding refresh tokens",
|
|
58
|
+
type: Number,
|
|
59
|
+
},
|
|
55
60
|
username: { description: "The user's username", type: String },
|
|
56
61
|
});
|
|
57
62
|
userSchema.plugin(passport_local_mongoose_1.default, {
|
|
@@ -65,6 +70,7 @@ userSchema.plugin(passport_local_mongoose_1.default, {
|
|
|
65
70
|
});
|
|
66
71
|
userSchema.plugin(plugins_1.createdUpdatedPlugin);
|
|
67
72
|
userSchema.plugin(plugins_1.isDisabledPlugin);
|
|
73
|
+
userSchema.plugin(plugins_1.emailVerificationPlugin);
|
|
68
74
|
userSchema.methods.postCreate = function (body) {
|
|
69
75
|
return __awaiter(this, void 0, void 0, function () {
|
|
70
76
|
return __generator(this, function (_a) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type mongoose from "mongoose";
|
|
2
|
+
import type { FindExactlyOnePlugin, FindOneOrNonePlugin } from "../plugins";
|
|
3
|
+
export type AuthTokenType = "passwordReset" | "emailVerification";
|
|
4
|
+
export type AuthTokenMethods = Record<string, never>;
|
|
5
|
+
export interface AuthTokenStatics extends FindExactlyOnePlugin<AuthTokenDocument>, FindOneOrNonePlugin<AuthTokenDocument> {
|
|
6
|
+
consume: (this: AuthTokenModel, token: string, type: AuthTokenType) => Promise<AuthTokenDocument | null>;
|
|
7
|
+
invalidateUnusedFor: (this: AuthTokenModel, user: {
|
|
8
|
+
_id: {
|
|
9
|
+
toString(): string;
|
|
10
|
+
};
|
|
11
|
+
}, type: AuthTokenType) => Promise<void>;
|
|
12
|
+
issueFor: (this: AuthTokenModel, user: {
|
|
13
|
+
_id: {
|
|
14
|
+
toString(): string;
|
|
15
|
+
};
|
|
16
|
+
}, type: AuthTokenType) => Promise<{
|
|
17
|
+
authToken: AuthTokenDocument;
|
|
18
|
+
token: string;
|
|
19
|
+
}>;
|
|
20
|
+
}
|
|
21
|
+
export interface AuthTokenModel extends mongoose.Model<AuthTokenDocument, object, AuthTokenMethods>, AuthTokenStatics {
|
|
22
|
+
}
|
|
23
|
+
export type AuthTokenSchema = mongoose.Schema<AuthTokenDocument, AuthTokenModel, AuthTokenMethods>;
|
|
24
|
+
export interface AuthTokenDocument extends mongoose.Document {
|
|
25
|
+
_id: mongoose.Types.ObjectId;
|
|
26
|
+
userId: mongoose.Types.ObjectId;
|
|
27
|
+
tokenHash: string;
|
|
28
|
+
type: AuthTokenType;
|
|
29
|
+
expiresAt: Date;
|
|
30
|
+
consumedAt?: Date;
|
|
31
|
+
created: Date;
|
|
32
|
+
updated: Date;
|
|
33
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type mongoose from "mongoose";
|
|
2
|
+
import type { FindExactlyOnePlugin, FindOneOrNonePlugin } from "../plugins";
|
|
3
|
+
export interface McpServiceTokenIssueOptions {
|
|
4
|
+
expiresAt?: Date;
|
|
5
|
+
name: string;
|
|
6
|
+
}
|
|
7
|
+
export interface McpServiceTokenMethods {
|
|
8
|
+
deleteOne: (this: McpServiceTokenDocument) => Promise<McpServiceTokenDocument>;
|
|
9
|
+
}
|
|
10
|
+
export interface McpServiceTokenStatics extends FindExactlyOnePlugin<McpServiceTokenDocument>, FindOneOrNonePlugin<McpServiceTokenDocument> {
|
|
11
|
+
countActiveForUser: (this: McpServiceTokenModel, userId: mongoose.Types.ObjectId) => Promise<number>;
|
|
12
|
+
issueFor: (this: McpServiceTokenModel, user: {
|
|
13
|
+
_id: {
|
|
14
|
+
toString(): string;
|
|
15
|
+
};
|
|
16
|
+
}, options: McpServiceTokenIssueOptions) => Promise<{
|
|
17
|
+
mcpServiceToken: McpServiceTokenDocument;
|
|
18
|
+
token: string;
|
|
19
|
+
}>;
|
|
20
|
+
revokeForUser: (this: McpServiceTokenModel, user: {
|
|
21
|
+
_id: {
|
|
22
|
+
toString(): string;
|
|
23
|
+
};
|
|
24
|
+
}, tokenId: mongoose.Types.ObjectId) => Promise<McpServiceTokenDocument | null>;
|
|
25
|
+
verify: (this: McpServiceTokenModel, token: string) => Promise<McpServiceTokenDocument | null>;
|
|
26
|
+
}
|
|
27
|
+
export interface McpServiceTokenModel extends mongoose.Model<McpServiceTokenDocument, object, McpServiceTokenMethods>, McpServiceTokenStatics {
|
|
28
|
+
}
|
|
29
|
+
export type McpServiceTokenSchema = mongoose.Schema<McpServiceTokenDocument, McpServiceTokenModel, McpServiceTokenMethods>;
|
|
30
|
+
export interface McpServiceTokenDocument extends mongoose.Document {
|
|
31
|
+
_id: mongoose.Types.ObjectId;
|
|
32
|
+
created: Date;
|
|
33
|
+
expiresAt?: Date;
|
|
34
|
+
lastUsedAt?: Date;
|
|
35
|
+
name: string;
|
|
36
|
+
revokedAt?: Date;
|
|
37
|
+
tokenHash: string;
|
|
38
|
+
tokenPrefix: string;
|
|
39
|
+
updated: Date;
|
|
40
|
+
userId: mongoose.Types.ObjectId;
|
|
41
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.headerValue = void 0;
|
|
4
|
+
var headerValue = function (req, header) {
|
|
5
|
+
var raw = req.headers[header.toLowerCase()];
|
|
6
|
+
if (Array.isArray(raw)) {
|
|
7
|
+
return raw[0];
|
|
8
|
+
}
|
|
9
|
+
if (typeof raw === "string") {
|
|
10
|
+
return raw;
|
|
11
|
+
}
|
|
12
|
+
return undefined;
|
|
13
|
+
};
|
|
14
|
+
exports.headerValue = headerValue;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type WebhookClaimResult = "claimed" | "duplicate";
|
|
2
|
+
export interface WebhookClaimArgs {
|
|
3
|
+
eventId: string;
|
|
4
|
+
source: string;
|
|
5
|
+
}
|
|
6
|
+
export interface WebhookIdempotencyStore {
|
|
7
|
+
claim: (args: WebhookClaimArgs) => Promise<WebhookClaimResult>;
|
|
8
|
+
release: (args: WebhookClaimArgs) => Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
export declare const createMemoryIdempotencyStore: () => WebhookIdempotencyStore;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.createMemoryIdempotencyStore = void 0;
|
|
40
|
+
var claimKey = function (_a) {
|
|
41
|
+
var source = _a.source, eventId = _a.eventId;
|
|
42
|
+
return "".concat(source, "\0").concat(eventId);
|
|
43
|
+
};
|
|
44
|
+
var createMemoryIdempotencyStore = function () {
|
|
45
|
+
var claimed = new Set();
|
|
46
|
+
var claim = function (args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
47
|
+
var key;
|
|
48
|
+
return __generator(this, function (_a) {
|
|
49
|
+
key = claimKey(args);
|
|
50
|
+
if (claimed.has(key)) {
|
|
51
|
+
return [2 /*return*/, "duplicate"];
|
|
52
|
+
}
|
|
53
|
+
claimed.add(key);
|
|
54
|
+
return [2 /*return*/, "claimed"];
|
|
55
|
+
});
|
|
56
|
+
}); };
|
|
57
|
+
var release = function (args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
58
|
+
return __generator(this, function (_a) {
|
|
59
|
+
claimed.delete(claimKey(args));
|
|
60
|
+
return [2 /*return*/];
|
|
61
|
+
});
|
|
62
|
+
}); };
|
|
63
|
+
return { claim: claim, release: release };
|
|
64
|
+
};
|
|
65
|
+
exports.createMemoryIdempotencyStore = createMemoryIdempotencyStore;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.createMongoIdempotencyStore = void 0;
|
|
43
|
+
var mongoose_1 = __importDefault(require("mongoose"));
|
|
44
|
+
var errors_1 = require("../../errors");
|
|
45
|
+
var webhookReceipt_1 = require("./webhookReceipt");
|
|
46
|
+
var isDuplicateKeyError = function (error) {
|
|
47
|
+
if (!error || typeof error !== "object" || !("code" in error)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
return error.code === 11000;
|
|
51
|
+
};
|
|
52
|
+
var createMongoIdempotencyStore = function (_a) {
|
|
53
|
+
var _b = _a === void 0 ? {} : _a, _c = _b.ttlDays, ttlDays = _c === void 0 ? webhookReceipt_1.DEFAULT_WEBHOOK_RECEIPT_TTL_DAYS : _c;
|
|
54
|
+
if (!Number.isFinite(ttlDays) || ttlDays <= 0) {
|
|
55
|
+
throw new errors_1.APIError({
|
|
56
|
+
status: 500,
|
|
57
|
+
title: "Mongo webhook idempotency ttlDays must be a positive number",
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
var expireAfterSeconds = Math.round(ttlDays * 86400);
|
|
61
|
+
var indexesReady;
|
|
62
|
+
var ensureIndexes = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
63
|
+
return __generator(this, function (_a) {
|
|
64
|
+
switch (_a.label) {
|
|
65
|
+
case 0:
|
|
66
|
+
if (!indexesReady) {
|
|
67
|
+
indexesReady = (0, webhookReceipt_1.getWebhookReceiptModel)()
|
|
68
|
+
.syncIndexes()
|
|
69
|
+
.then(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
70
|
+
var db;
|
|
71
|
+
return __generator(this, function (_a) {
|
|
72
|
+
switch (_a.label) {
|
|
73
|
+
case 0:
|
|
74
|
+
db = mongoose_1.default.connection.db;
|
|
75
|
+
if (!db) {
|
|
76
|
+
return [2 /*return*/];
|
|
77
|
+
}
|
|
78
|
+
return [4 /*yield*/, db.command({
|
|
79
|
+
collMod: webhookReceipt_1.WEBHOOK_RECEIPTS_COLLECTION,
|
|
80
|
+
index: {
|
|
81
|
+
expireAfterSeconds: expireAfterSeconds,
|
|
82
|
+
keyPattern: { created: 1 },
|
|
83
|
+
},
|
|
84
|
+
})];
|
|
85
|
+
case 1:
|
|
86
|
+
_a.sent();
|
|
87
|
+
return [2 /*return*/];
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}); })
|
|
91
|
+
.catch(function (error) {
|
|
92
|
+
indexesReady = undefined;
|
|
93
|
+
throw error;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return [4 /*yield*/, indexesReady];
|
|
97
|
+
case 1:
|
|
98
|
+
_a.sent();
|
|
99
|
+
return [2 /*return*/];
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}); };
|
|
103
|
+
var claim = function (args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
104
|
+
var WebhookReceipt, error_1;
|
|
105
|
+
return __generator(this, function (_a) {
|
|
106
|
+
switch (_a.label) {
|
|
107
|
+
case 0:
|
|
108
|
+
if (!mongoose_1.default.connection.db) {
|
|
109
|
+
throw new errors_1.APIError({
|
|
110
|
+
status: 500,
|
|
111
|
+
title: "Mongo webhook idempotency store requires an open mongoose connection",
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return [4 /*yield*/, ensureIndexes()];
|
|
115
|
+
case 1:
|
|
116
|
+
_a.sent();
|
|
117
|
+
WebhookReceipt = (0, webhookReceipt_1.getWebhookReceiptModel)();
|
|
118
|
+
_a.label = 2;
|
|
119
|
+
case 2:
|
|
120
|
+
_a.trys.push([2, 4, , 5]);
|
|
121
|
+
return [4 /*yield*/, WebhookReceipt.create({ eventId: args.eventId, source: args.source })];
|
|
122
|
+
case 3:
|
|
123
|
+
_a.sent();
|
|
124
|
+
return [2 /*return*/, "claimed"];
|
|
125
|
+
case 4:
|
|
126
|
+
error_1 = _a.sent();
|
|
127
|
+
if (isDuplicateKeyError(error_1)) {
|
|
128
|
+
return [2 /*return*/, "duplicate"];
|
|
129
|
+
}
|
|
130
|
+
throw error_1;
|
|
131
|
+
case 5: return [2 /*return*/];
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}); };
|
|
135
|
+
var release = function (args) { return __awaiter(void 0, void 0, void 0, function () {
|
|
136
|
+
var WebhookReceipt;
|
|
137
|
+
return __generator(this, function (_a) {
|
|
138
|
+
switch (_a.label) {
|
|
139
|
+
case 0:
|
|
140
|
+
WebhookReceipt = (0, webhookReceipt_1.getWebhookReceiptModel)();
|
|
141
|
+
return [4 /*yield*/, WebhookReceipt.deleteOne({ eventId: args.eventId, source: args.source })];
|
|
142
|
+
case 1:
|
|
143
|
+
_a.sent();
|
|
144
|
+
return [2 /*return*/];
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}); };
|
|
148
|
+
return { claim: claim, release: release };
|
|
149
|
+
};
|
|
150
|
+
exports.createMongoIdempotencyStore = createMongoIdempotencyStore;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
import type { FindExactlyOnePlugin, FindOneOrNonePlugin } from "../../plugins";
|
|
3
|
+
export declare const WEBHOOK_RECEIPTS_COLLECTION = "webhookReceipts";
|
|
4
|
+
export declare const DEFAULT_WEBHOOK_RECEIPT_TTL_DAYS = 7;
|
|
5
|
+
export type WebhookReceiptMethods = Record<string, never>;
|
|
6
|
+
export interface WebhookReceiptStatics extends FindExactlyOnePlugin<WebhookReceiptDocument>, FindOneOrNonePlugin<WebhookReceiptDocument> {
|
|
7
|
+
}
|
|
8
|
+
export interface WebhookReceiptModel extends mongoose.Model<WebhookReceiptDocument, object, WebhookReceiptMethods>, WebhookReceiptStatics {
|
|
9
|
+
}
|
|
10
|
+
export interface WebhookReceiptDocument extends mongoose.Document {
|
|
11
|
+
_id: mongoose.Types.ObjectId;
|
|
12
|
+
created: Date;
|
|
13
|
+
eventId: string;
|
|
14
|
+
source: string;
|
|
15
|
+
}
|
|
16
|
+
export declare const webhookReceiptSchema: mongoose.Schema<WebhookReceiptDocument, WebhookReceiptModel, {}, {}, {}, {}, mongoose.DefaultSchemaOptions, WebhookReceiptDocument, mongoose.Document<unknown, {}, WebhookReceiptDocument, {
|
|
17
|
+
id: string;
|
|
18
|
+
}, mongoose.DefaultSchemaOptions> & Omit<WebhookReceiptDocument & Required<{
|
|
19
|
+
_id: mongoose.Types.ObjectId;
|
|
20
|
+
}> & {
|
|
21
|
+
__v: number;
|
|
22
|
+
}, "id"> & mongoose.HydratedDocumentOverrides<{
|
|
23
|
+
id: string;
|
|
24
|
+
}>, {
|
|
25
|
+
_id?: mongoose.SchemaDefinitionProperty<mongoose.Types.ObjectId, WebhookReceiptDocument, mongoose.Document<unknown, {}, WebhookReceiptDocument, {
|
|
26
|
+
id: string;
|
|
27
|
+
}, mongoose.DefaultSchemaOptions> & Omit<WebhookReceiptDocument & Required<{
|
|
28
|
+
_id: mongoose.Types.ObjectId;
|
|
29
|
+
}> & {
|
|
30
|
+
__v: number;
|
|
31
|
+
}, "id"> & mongoose.HydratedDocumentOverrides<{
|
|
32
|
+
id: string;
|
|
33
|
+
}>> | undefined;
|
|
34
|
+
source?: mongoose.SchemaDefinitionProperty<string, WebhookReceiptDocument, mongoose.Document<unknown, {}, WebhookReceiptDocument, {
|
|
35
|
+
id: string;
|
|
36
|
+
}, mongoose.DefaultSchemaOptions> & Omit<WebhookReceiptDocument & Required<{
|
|
37
|
+
_id: mongoose.Types.ObjectId;
|
|
38
|
+
}> & {
|
|
39
|
+
__v: number;
|
|
40
|
+
}, "id"> & mongoose.HydratedDocumentOverrides<{
|
|
41
|
+
id: string;
|
|
42
|
+
}>> | undefined;
|
|
43
|
+
created?: mongoose.SchemaDefinitionProperty<Date, WebhookReceiptDocument, mongoose.Document<unknown, {}, WebhookReceiptDocument, {
|
|
44
|
+
id: string;
|
|
45
|
+
}, mongoose.DefaultSchemaOptions> & Omit<WebhookReceiptDocument & Required<{
|
|
46
|
+
_id: mongoose.Types.ObjectId;
|
|
47
|
+
}> & {
|
|
48
|
+
__v: number;
|
|
49
|
+
}, "id"> & mongoose.HydratedDocumentOverrides<{
|
|
50
|
+
id: string;
|
|
51
|
+
}>> | undefined;
|
|
52
|
+
eventId?: mongoose.SchemaDefinitionProperty<string, WebhookReceiptDocument, mongoose.Document<unknown, {}, WebhookReceiptDocument, {
|
|
53
|
+
id: string;
|
|
54
|
+
}, mongoose.DefaultSchemaOptions> & Omit<WebhookReceiptDocument & Required<{
|
|
55
|
+
_id: mongoose.Types.ObjectId;
|
|
56
|
+
}> & {
|
|
57
|
+
__v: number;
|
|
58
|
+
}, "id"> & mongoose.HydratedDocumentOverrides<{
|
|
59
|
+
id: string;
|
|
60
|
+
}>> | undefined;
|
|
61
|
+
}, WebhookReceiptDocument>;
|
|
62
|
+
export declare const getWebhookReceiptModel: () => WebhookReceiptModel;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getWebhookReceiptModel = exports.webhookReceiptSchema = exports.DEFAULT_WEBHOOK_RECEIPT_TTL_DAYS = exports.WEBHOOK_RECEIPTS_COLLECTION = void 0;
|
|
7
|
+
var luxon_1 = require("luxon");
|
|
8
|
+
var mongoose_1 = __importDefault(require("mongoose"));
|
|
9
|
+
var plugins_1 = require("../../plugins");
|
|
10
|
+
exports.WEBHOOK_RECEIPTS_COLLECTION = "webhookReceipts";
|
|
11
|
+
exports.DEFAULT_WEBHOOK_RECEIPT_TTL_DAYS = 7;
|
|
12
|
+
var secondsPerDay = 86400;
|
|
13
|
+
exports.webhookReceiptSchema = new mongoose_1.default.Schema({
|
|
14
|
+
created: {
|
|
15
|
+
default: function () { return luxon_1.DateTime.utc().toJSDate(); },
|
|
16
|
+
description: "When this webhook event id was first claimed",
|
|
17
|
+
required: true,
|
|
18
|
+
type: Date,
|
|
19
|
+
},
|
|
20
|
+
eventId: {
|
|
21
|
+
description: "Provider event id claimed for this source",
|
|
22
|
+
required: true,
|
|
23
|
+
type: String,
|
|
24
|
+
},
|
|
25
|
+
source: {
|
|
26
|
+
description: "Webhook source label (for example stripe or sendgrid)",
|
|
27
|
+
required: true,
|
|
28
|
+
type: String,
|
|
29
|
+
},
|
|
30
|
+
}, { collection: exports.WEBHOOK_RECEIPTS_COLLECTION, strict: "throw" });
|
|
31
|
+
exports.webhookReceiptSchema.index({ eventId: 1, source: 1 }, { unique: true });
|
|
32
|
+
exports.webhookReceiptSchema.index({ created: 1 }, { expireAfterSeconds: exports.DEFAULT_WEBHOOK_RECEIPT_TTL_DAYS * secondsPerDay });
|
|
33
|
+
exports.webhookReceiptSchema.plugin(plugins_1.findOneOrNone);
|
|
34
|
+
exports.webhookReceiptSchema.plugin(plugins_1.findExactlyOne);
|
|
35
|
+
var getWebhookReceiptModel = function () {
|
|
36
|
+
var _a;
|
|
37
|
+
return ((_a = mongoose_1.default.models.WebhookReceipt) !== null && _a !== void 0 ? _a : mongoose_1.default.model("WebhookReceipt", exports.webhookReceiptSchema));
|
|
38
|
+
};
|
|
39
|
+
exports.getWebhookReceiptModel = getWebhookReceiptModel;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { IncomingMessage, OutgoingMessage } from "node:http";
|
|
2
|
+
/**
|
|
3
|
+
* Copies the parsed request buffer onto `req.rawBody`.
|
|
4
|
+
* Express may reuse `buf`; webhook signatures need the original bytes.
|
|
5
|
+
*/
|
|
6
|
+
export declare const captureRawBody: (req: IncomingMessage, _res: OutgoingMessage, buf: Buffer) => void;
|
|
7
|
+
export declare const jsonBodyParserOptions: {
|
|
8
|
+
limit: "50mb";
|
|
9
|
+
verify: (req: IncomingMessage, _res: OutgoingMessage, buf: Buffer) => void;
|
|
10
|
+
};
|
|
11
|
+
export declare const urlencodedBodyParserOptions: {
|
|
12
|
+
extended: false;
|
|
13
|
+
verify: (req: IncomingMessage, _res: OutgoingMessage, buf: Buffer) => void;
|
|
14
|
+
};
|