@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
|
@@ -1,76 +1,4 @@
|
|
|
1
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 __values = (this && this.__values) || function(o) {
|
|
39
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
40
|
-
if (m) return m.call(o);
|
|
41
|
-
if (o && typeof o.length === "number") return {
|
|
42
|
-
next: function () {
|
|
43
|
-
if (o && i >= o.length) o = void 0;
|
|
44
|
-
return { value: o && o[i++], done: !o };
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
48
|
-
};
|
|
49
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
50
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
51
|
-
if (!m) return o;
|
|
52
|
-
var i = m.call(o), r, ar = [], e;
|
|
53
|
-
try {
|
|
54
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
55
|
-
}
|
|
56
|
-
catch (error) { e = { error: error }; }
|
|
57
|
-
finally {
|
|
58
|
-
try {
|
|
59
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
60
|
-
}
|
|
61
|
-
finally { if (e) throw e.error; }
|
|
62
|
-
}
|
|
63
|
-
return ar;
|
|
64
|
-
};
|
|
65
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
66
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
67
|
-
if (ar || !(i in from)) {
|
|
68
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
69
|
-
ar[i] = from[i];
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
73
|
-
};
|
|
74
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
75
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
76
4
|
};
|
|
@@ -81,304 +9,169 @@ exports.exerciseReversibleMigrations = exports.runStartupMigrations = exports.as
|
|
|
81
9
|
* owns the production wet-apply gate, the TerrenoApp `runOnStart` hook, and the
|
|
82
10
|
* CI helper that applies then reverses reversible files.
|
|
83
11
|
*/
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
12
|
+
const luxon_1 = require("luxon");
|
|
13
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
14
|
+
const errors_1 = require("../errors");
|
|
15
|
+
const logger_1 = require("../logger");
|
|
16
|
+
const load_1 = require("./load");
|
|
17
|
+
const lock_1 = require("./lock");
|
|
18
|
+
const types_1 = require("./types");
|
|
19
|
+
const historyCollection = (connection) => {
|
|
92
20
|
return connection.collection(types_1.MIGRATIONS_COLLECTION);
|
|
93
21
|
};
|
|
94
|
-
|
|
22
|
+
const toJsDate = (value) => {
|
|
95
23
|
if (value instanceof Date) {
|
|
96
|
-
|
|
97
|
-
if (!
|
|
24
|
+
const parsed = luxon_1.DateTime.fromJSDate(value, { zone: "utc" });
|
|
25
|
+
if (!parsed.isValid) {
|
|
98
26
|
throw new errors_1.APIError({ status: 500, title: "Invalid migration timestamp" });
|
|
99
27
|
}
|
|
100
|
-
return
|
|
28
|
+
return parsed.toJSDate();
|
|
101
29
|
}
|
|
102
|
-
|
|
30
|
+
const parsed = luxon_1.DateTime.fromISO(String(value ?? ""), { zone: "utc" });
|
|
103
31
|
if (!parsed.isValid) {
|
|
104
32
|
throw new errors_1.APIError({ status: 500, title: "Invalid migration timestamp" });
|
|
105
33
|
}
|
|
106
34
|
return parsed.toJSDate();
|
|
107
35
|
};
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
132
|
-
finally {
|
|
133
|
-
try {
|
|
134
|
-
if (docs_1_1 && !docs_1_1.done && (_a = docs_1.return)) _a.call(docs_1);
|
|
135
|
-
}
|
|
136
|
-
finally { if (e_1) throw e_1.error; }
|
|
137
|
-
}
|
|
138
|
-
return [2 /*return*/, applied];
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
}); };
|
|
142
|
-
var recordApplied = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
143
|
-
var connection = _b.connection, migration = _b.migration;
|
|
144
|
-
return __generator(this, function (_c) {
|
|
145
|
-
switch (_c.label) {
|
|
146
|
-
case 0: return [4 /*yield*/, historyCollection(connection).insertOne({
|
|
147
|
-
_id: migration.id,
|
|
148
|
-
appliedAt: luxon_1.DateTime.utc().toJSDate(),
|
|
149
|
-
checksum: migration.checksum,
|
|
150
|
-
id: migration.id,
|
|
151
|
-
})];
|
|
152
|
-
case 1:
|
|
153
|
-
_c.sent();
|
|
154
|
-
return [2 /*return*/];
|
|
155
|
-
}
|
|
36
|
+
const loadApplied = async (connection) => {
|
|
37
|
+
const docs = await historyCollection(connection)
|
|
38
|
+
.find({ _id: { $ne: types_1.MIGRATION_LOCK_ID } })
|
|
39
|
+
.toArray();
|
|
40
|
+
const applied = new Map();
|
|
41
|
+
for (const doc of docs) {
|
|
42
|
+
const id = String(doc.id ?? doc._id);
|
|
43
|
+
applied.set(id, {
|
|
44
|
+
appliedAt: toJsDate(doc.appliedAt),
|
|
45
|
+
checksum: String(doc.checksum ?? ""),
|
|
46
|
+
id,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return applied;
|
|
50
|
+
};
|
|
51
|
+
const recordApplied = async ({ connection, migration, }) => {
|
|
52
|
+
await historyCollection(connection).insertOne({
|
|
53
|
+
_id: migration.id,
|
|
54
|
+
appliedAt: luxon_1.DateTime.utc().toJSDate(),
|
|
55
|
+
checksum: migration.checksum,
|
|
56
|
+
id: migration.id,
|
|
156
57
|
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
_b.label = 2;
|
|
173
|
-
case 2:
|
|
174
|
-
_b.trys.push([2, 9, 10, 11]);
|
|
175
|
-
migrations_1 = __values(migrations), migrations_1_1 = migrations_1.next();
|
|
176
|
-
_b.label = 3;
|
|
177
|
-
case 3:
|
|
178
|
-
if (!!migrations_1_1.done) return [3 /*break*/, 8];
|
|
179
|
-
migration = migrations_1_1.value;
|
|
180
|
-
existing = appliedRecords.get(migration.id);
|
|
181
|
-
if (existing) {
|
|
182
|
-
if (existing.checksum !== migration.checksum) {
|
|
183
|
-
throw new errors_1.APIError({
|
|
184
|
-
detail: "Migration ".concat(migration.id, " was applied with checksum ").concat(existing.checksum, " but the current file is ").concat(migration.checksum),
|
|
185
|
-
status: 409,
|
|
186
|
-
title: "Migration checksum mismatch",
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
skipped.push(migration.id);
|
|
190
|
-
return [3 /*break*/, 7];
|
|
191
|
-
}
|
|
192
|
-
ctx = {
|
|
193
|
-
addLog: addLog,
|
|
194
|
-
checkCancellation: checkCancellation,
|
|
195
|
-
dryRun: dryRun,
|
|
196
|
-
logger: logger,
|
|
197
|
-
mongoose: mongooseNs,
|
|
198
|
-
};
|
|
199
|
-
return [4 /*yield*/, (checkCancellation === null || checkCancellation === void 0 ? void 0 : checkCancellation())];
|
|
200
|
-
case 4:
|
|
201
|
-
_b.sent();
|
|
202
|
-
return [4 /*yield*/, migration.up(ctx)];
|
|
203
|
-
case 5:
|
|
204
|
-
_b.sent();
|
|
205
|
-
applied.push(migration.id);
|
|
206
|
-
if (!!dryRun) return [3 /*break*/, 7];
|
|
207
|
-
return [4 /*yield*/, recordApplied({ connection: connection, migration: migration })];
|
|
208
|
-
case 6:
|
|
209
|
-
_b.sent();
|
|
210
|
-
_b.label = 7;
|
|
211
|
-
case 7:
|
|
212
|
-
migrations_1_1 = migrations_1.next();
|
|
213
|
-
return [3 /*break*/, 3];
|
|
214
|
-
case 8: return [3 /*break*/, 11];
|
|
215
|
-
case 9:
|
|
216
|
-
e_2_1 = _b.sent();
|
|
217
|
-
e_2 = { error: e_2_1 };
|
|
218
|
-
return [3 /*break*/, 11];
|
|
219
|
-
case 10:
|
|
220
|
-
try {
|
|
221
|
-
if (migrations_1_1 && !migrations_1_1.done && (_a = migrations_1.return)) _a.call(migrations_1);
|
|
222
|
-
}
|
|
223
|
-
finally { if (e_2) throw e_2.error; }
|
|
224
|
-
return [7 /*endfinally*/];
|
|
225
|
-
case 11: return [2 /*return*/, { applied: applied, dryRun: dryRun, skipped: skipped }];
|
|
58
|
+
};
|
|
59
|
+
const runMigrations = async ({ addLog, checkCancellation, connection, dryRun, lockPollMs, lockTtlMs, logger = logger_1.logger, migrations, mongoose: mongooseNs, }) => {
|
|
60
|
+
const execute = async () => {
|
|
61
|
+
const appliedRecords = await loadApplied(connection);
|
|
62
|
+
const applied = [];
|
|
63
|
+
const skipped = [];
|
|
64
|
+
for (const migration of migrations) {
|
|
65
|
+
const existing = appliedRecords.get(migration.id);
|
|
66
|
+
if (existing) {
|
|
67
|
+
if (existing.checksum !== migration.checksum) {
|
|
68
|
+
throw new errors_1.APIError({
|
|
69
|
+
detail: `Migration ${migration.id} was applied with checksum ${existing.checksum} but the current file is ${migration.checksum}`,
|
|
70
|
+
status: 409,
|
|
71
|
+
title: "Migration checksum mismatch",
|
|
72
|
+
});
|
|
226
73
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
case 1:
|
|
244
|
-
_c.sent();
|
|
245
|
-
return [2 /*return*/];
|
|
74
|
+
skipped.push(migration.id);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const ctx = {
|
|
78
|
+
addLog,
|
|
79
|
+
checkCancellation,
|
|
80
|
+
dryRun,
|
|
81
|
+
logger,
|
|
82
|
+
mongoose: mongooseNs,
|
|
83
|
+
};
|
|
84
|
+
await checkCancellation?.();
|
|
85
|
+
await migration.up(ctx);
|
|
86
|
+
applied.push(migration.id);
|
|
87
|
+
if (!dryRun) {
|
|
88
|
+
await recordApplied({ connection, migration });
|
|
89
|
+
}
|
|
246
90
|
}
|
|
91
|
+
return { applied, dryRun, skipped };
|
|
92
|
+
};
|
|
93
|
+
return (0, lock_1.withMigrationLock)({
|
|
94
|
+
connection,
|
|
95
|
+
fn: execute,
|
|
96
|
+
pollMs: lockPollMs,
|
|
97
|
+
ttlMs: lockTtlMs,
|
|
247
98
|
});
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
for (migrations_2 = __values(migrations), migrations_2_1 = migrations_2.next(); !migrations_2_1.done; migrations_2_1 = migrations_2.next()) {
|
|
263
|
-
migration = migrations_2_1.value;
|
|
264
|
-
existing = appliedRecords.get(migration.id);
|
|
265
|
-
if (existing) {
|
|
266
|
-
applied.push(existing);
|
|
267
|
-
continue;
|
|
268
|
-
}
|
|
269
|
-
pending.push({ checksum: migration.checksum, id: migration.id });
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
273
|
-
finally {
|
|
274
|
-
try {
|
|
275
|
-
if (migrations_2_1 && !migrations_2_1.done && (_c = migrations_2.return)) _c.call(migrations_2);
|
|
276
|
-
}
|
|
277
|
-
finally { if (e_3) throw e_3.error; }
|
|
278
|
-
}
|
|
279
|
-
collection = historyCollection(connection);
|
|
280
|
-
return [4 /*yield*/, collection.findOne({ _id: types_1.MIGRATION_LOCK_ID })];
|
|
281
|
-
case 2:
|
|
282
|
-
lockDoc = _e.sent();
|
|
283
|
-
lock = lockDoc == null
|
|
284
|
-
? null
|
|
285
|
-
: {
|
|
286
|
-
expiresAt: toJsDate(lockDoc.expiresAt),
|
|
287
|
-
holder: String((_d = lockDoc.holder) !== null && _d !== void 0 ? _d : ""),
|
|
288
|
-
};
|
|
289
|
-
return [2 /*return*/, { applied: applied, lock: lock, pending: pending }];
|
|
99
|
+
};
|
|
100
|
+
exports.runMigrations = runMigrations;
|
|
101
|
+
const removeApplied = async ({ connection, id, }) => {
|
|
102
|
+
await historyCollection(connection).deleteOne({ _id: id });
|
|
103
|
+
};
|
|
104
|
+
const getMigrationStatus = async ({ connection, migrations, }) => {
|
|
105
|
+
const appliedRecords = await loadApplied(connection);
|
|
106
|
+
const applied = [];
|
|
107
|
+
const pending = [];
|
|
108
|
+
for (const migration of migrations) {
|
|
109
|
+
const existing = appliedRecords.get(migration.id);
|
|
110
|
+
if (existing) {
|
|
111
|
+
applied.push(existing);
|
|
112
|
+
continue;
|
|
290
113
|
}
|
|
291
|
-
|
|
292
|
-
}
|
|
114
|
+
pending.push({ checksum: migration.checksum, id: migration.id });
|
|
115
|
+
}
|
|
116
|
+
const collection = historyCollection(connection);
|
|
117
|
+
const lockDoc = await collection.findOne({ _id: types_1.MIGRATION_LOCK_ID });
|
|
118
|
+
const lock = lockDoc == null
|
|
119
|
+
? null
|
|
120
|
+
: {
|
|
121
|
+
expiresAt: toJsDate(lockDoc.expiresAt),
|
|
122
|
+
holder: String(lockDoc.holder ?? ""),
|
|
123
|
+
};
|
|
124
|
+
return { applied, lock, pending };
|
|
125
|
+
};
|
|
293
126
|
exports.getMigrationStatus = getMigrationStatus;
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
127
|
+
const runDownMigrations = async ({ connection, dryRun, lockPollMs, lockTtlMs, logger = logger_1.logger, migrations, mongoose: mongooseNs, steps, }) => {
|
|
128
|
+
if (!Number.isInteger(steps) || steps < 1) {
|
|
129
|
+
throw new errors_1.APIError({
|
|
130
|
+
detail: "down --steps must be a positive integer",
|
|
131
|
+
status: 400,
|
|
132
|
+
title: "Invalid down steps",
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
const execute = async () => {
|
|
136
|
+
const appliedRecords = await loadApplied(connection);
|
|
137
|
+
const appliedInOrder = migrations.filter((migration) => appliedRecords.has(migration.id));
|
|
138
|
+
const targets = appliedInOrder.slice(-steps).reverse();
|
|
139
|
+
const reversed = [];
|
|
140
|
+
const reversible = targets.map((migration) => {
|
|
141
|
+
if (typeof migration.down !== "function") {
|
|
142
|
+
throw new errors_1.APIError({
|
|
143
|
+
detail: `Migration ${migration.id} has no down. Rollback stopped.`,
|
|
144
|
+
status: 400,
|
|
145
|
+
title: "Migration has no down",
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return { down: migration.down, id: migration.id };
|
|
149
|
+
});
|
|
150
|
+
for (const migration of reversible) {
|
|
151
|
+
const ctx = {
|
|
152
|
+
dryRun,
|
|
153
|
+
logger,
|
|
154
|
+
mongoose: mongooseNs,
|
|
155
|
+
};
|
|
156
|
+
await ctx.checkCancellation?.();
|
|
157
|
+
await migration.down(ctx);
|
|
158
|
+
reversed.push(migration.id);
|
|
159
|
+
if (!dryRun) {
|
|
160
|
+
await removeApplied({ connection, id: migration.id });
|
|
161
|
+
}
|
|
304
162
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
case 1:
|
|
313
|
-
appliedRecords = _c.sent();
|
|
314
|
-
appliedInOrder = migrations.filter(function (migration) { return appliedRecords.has(migration.id); });
|
|
315
|
-
targets = appliedInOrder.slice(-steps).reverse();
|
|
316
|
-
reversed = [];
|
|
317
|
-
reversible = targets.map(function (migration) {
|
|
318
|
-
if (typeof migration.down !== "function") {
|
|
319
|
-
throw new errors_1.APIError({
|
|
320
|
-
detail: "Migration ".concat(migration.id, " has no down. Rollback stopped."),
|
|
321
|
-
status: 400,
|
|
322
|
-
title: "Migration has no down",
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
return { down: migration.down, id: migration.id };
|
|
326
|
-
});
|
|
327
|
-
_c.label = 2;
|
|
328
|
-
case 2:
|
|
329
|
-
_c.trys.push([2, 9, 10, 11]);
|
|
330
|
-
reversible_1 = __values(reversible), reversible_1_1 = reversible_1.next();
|
|
331
|
-
_c.label = 3;
|
|
332
|
-
case 3:
|
|
333
|
-
if (!!reversible_1_1.done) return [3 /*break*/, 8];
|
|
334
|
-
migration = reversible_1_1.value;
|
|
335
|
-
ctx = {
|
|
336
|
-
dryRun: dryRun,
|
|
337
|
-
logger: logger,
|
|
338
|
-
mongoose: mongooseNs,
|
|
339
|
-
};
|
|
340
|
-
return [4 /*yield*/, ((_b = ctx.checkCancellation) === null || _b === void 0 ? void 0 : _b.call(ctx))];
|
|
341
|
-
case 4:
|
|
342
|
-
_c.sent();
|
|
343
|
-
return [4 /*yield*/, migration.down(ctx)];
|
|
344
|
-
case 5:
|
|
345
|
-
_c.sent();
|
|
346
|
-
reversed.push(migration.id);
|
|
347
|
-
if (!!dryRun) return [3 /*break*/, 7];
|
|
348
|
-
return [4 /*yield*/, removeApplied({ connection: connection, id: migration.id })];
|
|
349
|
-
case 6:
|
|
350
|
-
_c.sent();
|
|
351
|
-
_c.label = 7;
|
|
352
|
-
case 7:
|
|
353
|
-
reversible_1_1 = reversible_1.next();
|
|
354
|
-
return [3 /*break*/, 3];
|
|
355
|
-
case 8: return [3 /*break*/, 11];
|
|
356
|
-
case 9:
|
|
357
|
-
e_4_1 = _c.sent();
|
|
358
|
-
e_4 = { error: e_4_1 };
|
|
359
|
-
return [3 /*break*/, 11];
|
|
360
|
-
case 10:
|
|
361
|
-
try {
|
|
362
|
-
if (reversible_1_1 && !reversible_1_1.done && (_a = reversible_1.return)) _a.call(reversible_1);
|
|
363
|
-
}
|
|
364
|
-
finally { if (e_4) throw e_4.error; }
|
|
365
|
-
return [7 /*endfinally*/];
|
|
366
|
-
case 11: return [2 /*return*/, { dryRun: dryRun, reversed: reversed }];
|
|
367
|
-
}
|
|
368
|
-
});
|
|
369
|
-
}); };
|
|
370
|
-
return [2 /*return*/, (0, lock_1.withMigrationLock)({
|
|
371
|
-
connection: connection,
|
|
372
|
-
fn: execute,
|
|
373
|
-
pollMs: lockPollMs,
|
|
374
|
-
ttlMs: lockTtlMs,
|
|
375
|
-
})];
|
|
163
|
+
return { dryRun, reversed };
|
|
164
|
+
};
|
|
165
|
+
return (0, lock_1.withMigrationLock)({
|
|
166
|
+
connection,
|
|
167
|
+
fn: execute,
|
|
168
|
+
pollMs: lockPollMs,
|
|
169
|
+
ttlMs: lockTtlMs,
|
|
376
170
|
});
|
|
377
|
-
}
|
|
171
|
+
};
|
|
378
172
|
exports.runDownMigrations = runDownMigrations;
|
|
379
173
|
/** Fail closed for production wet apply unless ALLOW_MIGRATIONS and force (CLI `--force` or boot/admin Apply). */
|
|
380
|
-
|
|
381
|
-
var allowEnv = _a.allowEnv, dryRun = _a.dryRun, force = _a.force, isProduction = _a.isProduction;
|
|
174
|
+
const assertMigrationsAllowed = ({ allowEnv, dryRun, force, isProduction, }) => {
|
|
382
175
|
if (dryRun) {
|
|
383
176
|
return;
|
|
384
177
|
}
|
|
@@ -388,116 +181,69 @@ var assertMigrationsAllowed = function (_a) {
|
|
|
388
181
|
if (allowEnv && force) {
|
|
389
182
|
return;
|
|
390
183
|
}
|
|
391
|
-
|
|
184
|
+
const detail = allowEnv
|
|
392
185
|
? "Production wet migrations also require --force (or an equivalent boot/admin Apply)."
|
|
393
186
|
: "Set ALLOW_MIGRATIONS=true for production wet migrations.";
|
|
394
187
|
throw new errors_1.APIError({
|
|
395
|
-
detail
|
|
188
|
+
detail,
|
|
396
189
|
status: 403,
|
|
397
190
|
title: "Migrations not allowed",
|
|
398
191
|
});
|
|
399
192
|
};
|
|
400
193
|
exports.assertMigrationsAllowed = assertMigrationsAllowed;
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
case 1:
|
|
418
|
-
loaded = _d.sent();
|
|
419
|
-
return [4 /*yield*/, (0, exports.runMigrations)({
|
|
420
|
-
connection: mongooseNs.connection,
|
|
421
|
-
dryRun: false,
|
|
422
|
-
migrations: loaded,
|
|
423
|
-
mongoose: mongooseNs,
|
|
424
|
-
})];
|
|
425
|
-
case 2:
|
|
426
|
-
result = _d.sent();
|
|
427
|
-
return [2 /*return*/, { applied: result.applied, ran: true }];
|
|
428
|
-
}
|
|
194
|
+
const runStartupMigrations = async ({ env = process.env, migrations, mongoose: mongooseNs, }) => {
|
|
195
|
+
if (!migrations?.runOnStart) {
|
|
196
|
+
return { applied: [], ran: false };
|
|
197
|
+
}
|
|
198
|
+
(0, exports.assertMigrationsAllowed)({
|
|
199
|
+
allowEnv: env.ALLOW_MIGRATIONS === "true",
|
|
200
|
+
dryRun: false,
|
|
201
|
+
force: true,
|
|
202
|
+
isProduction: env.NODE_ENV === "production",
|
|
203
|
+
});
|
|
204
|
+
const loaded = await (0, load_1.checkMigrationFiles)({ dir: migrations.dir });
|
|
205
|
+
const result = await (0, exports.runMigrations)({
|
|
206
|
+
connection: mongooseNs.connection,
|
|
207
|
+
dryRun: false,
|
|
208
|
+
migrations: loaded,
|
|
209
|
+
mongoose: mongooseNs,
|
|
429
210
|
});
|
|
430
|
-
|
|
211
|
+
return { applied: result.applied, ran: true };
|
|
212
|
+
};
|
|
431
213
|
exports.runStartupMigrations = runStartupMigrations;
|
|
432
214
|
/**
|
|
433
215
|
* CI helper: apply every file, then roll back in reverse until a file without
|
|
434
216
|
* `down` stops the chain. The irreversible id is recorded; earlier files stay applied.
|
|
435
217
|
*/
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
connection = _f.sent();
|
|
445
|
-
return [4 /*yield*/, (0, load_1.checkMigrationFiles)({ dir: dir })];
|
|
446
|
-
case 2:
|
|
447
|
-
migrations = _f.sent();
|
|
448
|
-
return [4 /*yield*/, (0, exports.runMigrations)({
|
|
449
|
-
connection: connection,
|
|
450
|
-
dryRun: false,
|
|
451
|
-
migrations: migrations,
|
|
452
|
-
mongoose: mongoose_1.default,
|
|
453
|
-
})];
|
|
454
|
-
case 3:
|
|
455
|
-
upResult = _f.sent();
|
|
456
|
-
reversed = [];
|
|
457
|
-
skippedIrreversible = null;
|
|
458
|
-
_f.label = 4;
|
|
459
|
-
case 4:
|
|
460
|
-
_f.trys.push([4, 9, 10, 11]);
|
|
461
|
-
_c = __values(__spreadArray([], __read(migrations), false).reverse()), _d = _c.next();
|
|
462
|
-
_f.label = 5;
|
|
463
|
-
case 5:
|
|
464
|
-
if (!!_d.done) return [3 /*break*/, 8];
|
|
465
|
-
migration = _d.value;
|
|
466
|
-
if (typeof migration.down !== "function") {
|
|
467
|
-
skippedIrreversible = migration.id;
|
|
468
|
-
return [3 /*break*/, 8];
|
|
469
|
-
}
|
|
470
|
-
return [4 /*yield*/, (0, exports.runDownMigrations)({
|
|
471
|
-
connection: connection,
|
|
472
|
-
dryRun: false,
|
|
473
|
-
migrations: migrations,
|
|
474
|
-
mongoose: mongoose_1.default,
|
|
475
|
-
steps: 1,
|
|
476
|
-
})];
|
|
477
|
-
case 6:
|
|
478
|
-
downResult = _f.sent();
|
|
479
|
-
reversed.push.apply(reversed, __spreadArray([], __read(downResult.reversed), false));
|
|
480
|
-
_f.label = 7;
|
|
481
|
-
case 7:
|
|
482
|
-
_d = _c.next();
|
|
483
|
-
return [3 /*break*/, 5];
|
|
484
|
-
case 8: return [3 /*break*/, 11];
|
|
485
|
-
case 9:
|
|
486
|
-
e_5_1 = _f.sent();
|
|
487
|
-
e_5 = { error: e_5_1 };
|
|
488
|
-
return [3 /*break*/, 11];
|
|
489
|
-
case 10:
|
|
490
|
-
try {
|
|
491
|
-
if (_d && !_d.done && (_e = _c.return)) _e.call(_c);
|
|
492
|
-
}
|
|
493
|
-
finally { if (e_5) throw e_5.error; }
|
|
494
|
-
return [7 /*endfinally*/];
|
|
495
|
-
case 11: return [2 /*return*/, {
|
|
496
|
-
applied: __spreadArray(__spreadArray([], __read(upResult.applied), false), __read(upResult.skipped), false),
|
|
497
|
-
reversed: reversed,
|
|
498
|
-
skippedIrreversible: skippedIrreversible,
|
|
499
|
-
}];
|
|
500
|
-
}
|
|
218
|
+
const exerciseReversibleMigrations = async ({ connect, dir, }) => {
|
|
219
|
+
const connection = await connect();
|
|
220
|
+
const migrations = await (0, load_1.checkMigrationFiles)({ dir });
|
|
221
|
+
const upResult = await (0, exports.runMigrations)({
|
|
222
|
+
connection,
|
|
223
|
+
dryRun: false,
|
|
224
|
+
migrations,
|
|
225
|
+
mongoose: mongoose_1.default,
|
|
501
226
|
});
|
|
502
|
-
|
|
227
|
+
const reversed = [];
|
|
228
|
+
let skippedIrreversible = null;
|
|
229
|
+
for (const migration of [...migrations].reverse()) {
|
|
230
|
+
if (typeof migration.down !== "function") {
|
|
231
|
+
skippedIrreversible = migration.id;
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
const downResult = await (0, exports.runDownMigrations)({
|
|
235
|
+
connection,
|
|
236
|
+
dryRun: false,
|
|
237
|
+
migrations,
|
|
238
|
+
mongoose: mongoose_1.default,
|
|
239
|
+
steps: 1,
|
|
240
|
+
});
|
|
241
|
+
reversed.push(...downResult.reversed);
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
applied: [...upResult.applied, ...upResult.skipped],
|
|
245
|
+
reversed,
|
|
246
|
+
skippedIrreversible,
|
|
247
|
+
};
|
|
248
|
+
};
|
|
503
249
|
exports.exerciseReversibleMigrations = exerciseReversibleMigrations;
|