@terreno/api 57.3.0 → 57.4.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.
Files changed (293) hide show
  1. package/.ai/skills/mongoose-schema-safety/SKILL.md +23 -10
  2. package/README.md +6 -0
  3. package/dist/adminTypes.d.ts +7 -0
  4. package/dist/adminTypes.js +6 -1
  5. package/dist/api.d.ts +10 -0
  6. package/dist/api.js +99 -36
  7. package/dist/audit/auditApp.d.ts +30 -0
  8. package/dist/audit/auditApp.js +155 -0
  9. package/dist/audit/auditEventModel.d.ts +29 -0
  10. package/dist/audit/auditEventModel.js +102 -0
  11. package/dist/audit/cloudTasksEnqueue.d.ts +32 -0
  12. package/dist/audit/cloudTasksEnqueue.js +162 -0
  13. package/dist/audit/diff.d.ts +10 -0
  14. package/dist/audit/diff.js +230 -0
  15. package/dist/audit/processQueue.d.ts +6 -0
  16. package/dist/audit/processQueue.js +101 -0
  17. package/dist/audit/rbacSink.d.ts +2 -0
  18. package/dist/audit/rbacSink.js +82 -0
  19. package/dist/audit/record.d.ts +56 -0
  20. package/dist/audit/record.js +373 -0
  21. package/dist/betterAuth.d.ts +6 -0
  22. package/dist/betterAuthSetup.js +1 -1
  23. package/dist/collectionRegistry.d.ts +3 -1
  24. package/dist/collectionRegistry.js +25 -0
  25. package/dist/index.d.ts +26 -1
  26. package/dist/index.js +48 -2
  27. package/dist/mcp/query.d.ts +0 -13
  28. package/dist/mcp/query.js +8 -8
  29. package/dist/migrations/fixtures/all-down/20260910120000-one.d.ts +4 -0
  30. package/dist/migrations/fixtures/all-down/20260910120000-one.js +49 -0
  31. package/dist/migrations/fixtures/all-down/20260910120001-two.d.ts +4 -0
  32. package/dist/migrations/fixtures/all-down/20260910120001-two.js +49 -0
  33. package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.d.ts +3 -0
  34. package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +45 -0
  35. package/dist/migrations/fixtures/bad-name/not-a-migration.d.ts +3 -0
  36. package/dist/migrations/fixtures/bad-name/not-a-migration.js +45 -0
  37. package/dist/migrations/fixtures/missing-up/20260910120000-noup.d.ts +2 -0
  38. package/dist/migrations/fixtures/missing-up/20260910120000-noup.js +5 -0
  39. package/dist/migrations/fixtures/valid/20260910120000-alpha.d.ts +3 -0
  40. package/dist/migrations/fixtures/valid/20260910120000-alpha.js +45 -0
  41. package/dist/migrations/fixtures/valid/20260910120001-beta.d.ts +4 -0
  42. package/dist/migrations/fixtures/valid/20260910120001-beta.js +49 -0
  43. package/dist/migrations/generate.d.ts +17 -0
  44. package/dist/migrations/generate.js +277 -0
  45. package/dist/migrations/load.d.ts +20 -0
  46. package/dist/migrations/load.js +236 -0
  47. package/dist/migrations/lock.d.ts +13 -0
  48. package/dist/migrations/lock.js +145 -0
  49. package/dist/migrations/runner.d.ts +42 -0
  50. package/dist/migrations/runner.js +503 -0
  51. package/dist/migrations/schemaCatalog.d.ts +44 -0
  52. package/dist/migrations/schemaCatalog.js +326 -0
  53. package/dist/migrations/types.d.ts +87 -0
  54. package/dist/migrations/types.js +7 -0
  55. package/dist/models/notification.d.ts +2 -0
  56. package/dist/models/notification.js +62 -0
  57. package/dist/models/notificationPreference.d.ts +2 -0
  58. package/dist/models/notificationPreference.js +49 -0
  59. package/dist/notifications/notificationService.d.ts +40 -0
  60. package/dist/notifications/notificationService.js +323 -0
  61. package/dist/notifications/notificationsApp.d.ts +18 -0
  62. package/dist/notifications/notificationsApp.js +250 -0
  63. package/dist/notifications/notificationsBeforeSend.d.ts +14 -0
  64. package/dist/notifications/notificationsBeforeSend.js +77 -0
  65. package/dist/openApi.d.ts +1 -7
  66. package/dist/openApi.js +7 -6
  67. package/dist/openApiBuilder.d.ts +14 -3
  68. package/dist/openApiValidator.d.ts +13 -1
  69. package/dist/openApiValidator.js +200 -36
  70. package/dist/orgs/orgContext.d.ts +23 -0
  71. package/dist/orgs/orgContext.js +198 -0
  72. package/dist/orgs/orgPermissions.d.ts +4 -0
  73. package/dist/orgs/orgPermissions.js +77 -0
  74. package/dist/orgs/orgPlugin.d.ts +13 -0
  75. package/dist/orgs/orgPlugin.js +84 -0
  76. package/dist/orgs/organizationModel.d.ts +13 -0
  77. package/dist/orgs/organizationModel.js +301 -0
  78. package/dist/orgs/organizationSettings.d.ts +19 -0
  79. package/dist/orgs/organizationSettings.js +120 -0
  80. package/dist/orgs/orgsApp.d.ts +28 -0
  81. package/dist/orgs/orgsApp.js +678 -0
  82. package/dist/permissions.d.ts +7 -13
  83. package/dist/permissions.js +8 -31
  84. package/dist/rateLimit/middleware.d.ts +0 -8
  85. package/dist/rateLimit/middleware.js +2 -3
  86. package/dist/rateLimit/mongoStore.d.ts +1 -0
  87. package/dist/rateLimit/mongoStore.js +1 -0
  88. package/dist/rateLimit/policies.d.ts +0 -4
  89. package/dist/rateLimit/policies.js +5 -7
  90. package/dist/rateLimit/redisStore.d.ts +1 -0
  91. package/dist/rateLimit/redisStore.js +1 -0
  92. package/dist/rbac/access.js +30 -13
  93. package/dist/rbac/membershipOrgAdminSource.d.ts +6 -0
  94. package/dist/rbac/membershipOrgAdminSource.js +122 -0
  95. package/dist/rbac/modelRouterAccess.d.ts +3 -0
  96. package/dist/rbac/modelRouterAccess.js +3 -0
  97. package/dist/rbac/resolve.js +60 -31
  98. package/dist/rbac/roleModel.d.ts +2 -5
  99. package/dist/rbac/roleModel.js +18 -4
  100. package/dist/rbac/statements.d.ts +10 -1
  101. package/dist/rbac/statements.js +19 -2
  102. package/dist/rbac/types.d.ts +5 -0
  103. package/dist/realtime/changeStreamWatcher.d.ts +10 -4
  104. package/dist/realtime/changeStreamWatcher.js +110 -64
  105. package/dist/realtime/deprecation.d.ts +1 -0
  106. package/dist/realtime/deprecation.js +1 -0
  107. package/dist/realtime/realtimeApp.d.ts +1 -0
  108. package/dist/realtime/realtimeApp.js +1 -0
  109. package/dist/schemaMetadata.js +16 -5
  110. package/dist/sync/adminBroadcastScope.d.ts +40 -0
  111. package/dist/sync/adminBroadcastScope.js +194 -0
  112. package/dist/sync/adminWindowAccess.d.ts +13 -0
  113. package/dist/sync/adminWindowAccess.js +96 -0
  114. package/dist/sync/adminWindowMutation.d.ts +88 -0
  115. package/dist/sync/adminWindowMutation.js +247 -0
  116. package/dist/sync/executors.d.ts +9 -3
  117. package/dist/sync/executors.js +190 -159
  118. package/dist/sync/mutationHandler.d.ts +5 -2
  119. package/dist/sync/mutationHandler.js +191 -71
  120. package/dist/sync/registry.js +4 -0
  121. package/dist/sync/routes.d.ts +13 -0
  122. package/dist/sync/routes.js +113 -56
  123. package/dist/sync/socketHandlers.d.ts +8 -2
  124. package/dist/sync/socketHandlers.js +144 -50
  125. package/dist/sync/streams.d.ts +2 -0
  126. package/dist/sync/streams.js +7 -1
  127. package/dist/sync/syncApp.d.ts +2 -1
  128. package/dist/sync/syncApp.js +2 -1
  129. package/dist/sync/syncBatch.d.ts +5 -2
  130. package/dist/sync/syncBatch.js +4 -4
  131. package/dist/sync/types.d.ts +18 -0
  132. package/dist/terrenoApp.d.ts +29 -0
  133. package/dist/terrenoApp.js +91 -54
  134. package/dist/tests/createTestData.d.ts +1 -3
  135. package/dist/tests/createTestData.js +3 -5
  136. package/dist/tests/models.d.ts +1 -0
  137. package/dist/tests/models.js +9 -2
  138. package/dist/tests/testHelper.d.ts +0 -5
  139. package/dist/tests/testHelper.js +2 -9
  140. package/dist/types/authToken.d.ts +3 -3
  141. package/dist/types/mcpServiceToken.d.ts +2 -2
  142. package/dist/types/membership.d.ts +23 -0
  143. package/dist/types/membership.js +2 -0
  144. package/dist/types/notification.d.ts +28 -0
  145. package/dist/types/notification.js +2 -0
  146. package/dist/types/notificationPreference.d.ts +20 -0
  147. package/dist/types/notificationPreference.js +2 -0
  148. package/dist/types/organization.d.ts +36 -0
  149. package/dist/types/organization.js +2 -0
  150. package/dist/webhooks/idempotency/webhookReceipt.d.ts +4 -49
  151. package/dist/webhooks/idempotency/webhookReceipt.js +7 -7
  152. package/dist/webhooks/rawBody.d.ts +1 -0
  153. package/dist/webhooks/rawBody.js +1 -0
  154. package/package.json +31 -25
  155. package/src/__snapshots__/openApi.test.ts.snap +192 -54
  156. package/src/__snapshots__/openApiBuilder.test.ts.snap +0 -18
  157. package/src/__snapshots__/schemaMetadata.test.ts.snap +0 -1
  158. package/src/adminTypes.ts +8 -0
  159. package/src/api.test.ts +39 -2
  160. package/src/api.ts +68 -0
  161. package/src/audit/auditApp.test.ts +243 -0
  162. package/src/audit/auditApp.ts +139 -0
  163. package/src/audit/auditEventModel.ts +156 -0
  164. package/src/audit/auditRouter.test.ts +456 -0
  165. package/src/audit/cloudTasksEnqueue.test.ts +124 -0
  166. package/src/audit/cloudTasksEnqueue.ts +104 -0
  167. package/src/audit/diff.ts +163 -0
  168. package/src/audit/processQueue.ts +69 -0
  169. package/src/audit/rbacSink.test.ts +86 -0
  170. package/src/audit/rbacSink.ts +42 -0
  171. package/src/audit/record.test.ts +402 -0
  172. package/src/audit/record.ts +328 -0
  173. package/src/auth.test.ts +1 -1
  174. package/src/betterAuth.ts +7 -0
  175. package/src/betterAuthSetup.test.ts +27 -0
  176. package/src/betterAuthSetup.ts +1 -0
  177. package/src/collectionRegistry.test.ts +8 -9
  178. package/src/collectionRegistry.ts +21 -1
  179. package/src/express.d.ts +2 -0
  180. package/src/expressServer.test.ts +50 -2
  181. package/src/index.ts +69 -0
  182. package/src/mcp/handlers.test.ts +275 -0
  183. package/src/mcp/query.ts +3 -3
  184. package/src/mcp/toolGenerator.test.ts +81 -0
  185. package/src/migrations/cli.test.ts +376 -0
  186. package/src/migrations/cli.ts +283 -0
  187. package/src/migrations/fixtures/all-down/20260910120000-one.ts +6 -0
  188. package/src/migrations/fixtures/all-down/20260910120001-two.ts +6 -0
  189. package/src/migrations/fixtures/bad-id/20260910120000-mismatch.ts +4 -0
  190. package/src/migrations/fixtures/bad-name/not-a-migration.ts +4 -0
  191. package/src/migrations/fixtures/missing-up/20260910120000-noup.ts +2 -0
  192. package/src/migrations/fixtures/valid/20260910120000-alpha.ts +4 -0
  193. package/src/migrations/fixtures/valid/20260910120001-beta.ts +6 -0
  194. package/src/migrations/generate.test.ts +333 -0
  195. package/src/migrations/generate.ts +216 -0
  196. package/src/migrations/load.test.ts +103 -0
  197. package/src/migrations/load.ts +124 -0
  198. package/src/migrations/lock.test.ts +133 -0
  199. package/src/migrations/lock.ts +118 -0
  200. package/src/migrations/runner.test.ts +504 -0
  201. package/src/migrations/runner.ts +358 -0
  202. package/src/migrations/schemaCatalog.test.ts +159 -0
  203. package/src/migrations/schemaCatalog.ts +262 -0
  204. package/src/migrations/types.ts +94 -0
  205. package/src/models/notification.ts +68 -0
  206. package/src/models/notificationPreference.ts +62 -0
  207. package/src/notifications/notificationService.ts +254 -0
  208. package/src/notifications/notificationsApp.test.ts +505 -0
  209. package/src/notifications/notificationsApp.ts +200 -0
  210. package/src/notifications/notificationsBeforeSend.ts +51 -0
  211. package/src/openApi.test.ts +0 -1
  212. package/src/openApi.ts +2 -1
  213. package/src/openApiBuilder.ts +14 -3
  214. package/src/openApiValidator.test.ts +221 -2
  215. package/src/openApiValidator.ts +218 -33
  216. package/src/orgs/orgAccessOptIn.test.ts +87 -0
  217. package/src/orgs/orgContext.test.ts +259 -0
  218. package/src/orgs/orgContext.ts +151 -0
  219. package/src/orgs/orgPermissions.test.ts +105 -0
  220. package/src/orgs/orgPermissions.ts +40 -0
  221. package/src/orgs/orgPlugin.test.ts +130 -0
  222. package/src/orgs/orgPlugin.ts +70 -0
  223. package/src/orgs/orgRbac.test.ts +267 -0
  224. package/src/orgs/orgSecurityDefects.test.ts +415 -0
  225. package/src/orgs/organizationModel.test.ts +205 -0
  226. package/src/orgs/organizationModel.ts +257 -0
  227. package/src/orgs/organizationSettings.test.ts +113 -0
  228. package/src/orgs/organizationSettings.ts +126 -0
  229. package/src/orgs/orgsApp.test.ts +382 -0
  230. package/src/orgs/orgsApp.ts +563 -0
  231. package/src/permissions.test.ts +62 -36
  232. package/src/permissions.ts +8 -30
  233. package/src/rateLimit/middleware.ts +1 -1
  234. package/src/rateLimit/mongoStore.ts +1 -0
  235. package/src/rateLimit/policies.ts +2 -2
  236. package/src/rateLimit/redisStore.ts +1 -0
  237. package/src/rbac/access.ts +11 -3
  238. package/src/rbac/membershipOrgAdminSource.ts +59 -0
  239. package/src/rbac/modelRouterAccess.ts +3 -0
  240. package/src/rbac/permissionUtils.test.ts +26 -5
  241. package/src/rbac/resolve.test.ts +34 -2
  242. package/src/rbac/resolve.ts +26 -7
  243. package/src/rbac/roleModel.test.ts +34 -1
  244. package/src/rbac/roleModel.ts +17 -1
  245. package/src/rbac/statements.test.ts +19 -0
  246. package/src/rbac/statements.ts +19 -1
  247. package/src/rbac/types.ts +5 -0
  248. package/src/realtime/changeStreamWatcher.ts +73 -33
  249. package/src/realtime/deprecation.ts +1 -0
  250. package/src/realtime/realtime.test.ts +357 -185
  251. package/src/realtime/realtimeApp.ts +1 -0
  252. package/src/schemaMetadata.test.ts +14 -0
  253. package/src/schemaMetadata.ts +5 -5
  254. package/src/sync/adminBroadcastConfig.test.ts +96 -0
  255. package/src/sync/adminBroadcastScope.test.ts +155 -0
  256. package/src/sync/adminBroadcastScope.ts +123 -0
  257. package/src/sync/adminWindowAccess.test.ts +57 -0
  258. package/src/sync/adminWindowAccess.ts +32 -0
  259. package/src/sync/adminWindowMutation.test.ts +450 -0
  260. package/src/sync/adminWindowMutation.ts +283 -0
  261. package/src/sync/executors.ts +33 -6
  262. package/src/sync/mutationHandler.test.ts +277 -1
  263. package/src/sync/mutationHandler.ts +171 -29
  264. package/src/sync/registry.ts +4 -0
  265. package/src/sync/routes.ts +52 -8
  266. package/src/sync/socketHandlers.ts +154 -58
  267. package/src/sync/streams.ts +6 -0
  268. package/src/sync/sync.test.ts +3 -2
  269. package/src/sync/syncApp.ts +2 -1
  270. package/src/sync/syncBatch.ts +7 -2
  271. package/src/sync/syncRoutes.test.ts +297 -3
  272. package/src/sync/syncSocket.test.ts +229 -0
  273. package/src/sync/types.ts +20 -0
  274. package/src/terrenoApp.migrations.test.ts +114 -0
  275. package/src/terrenoApp.test.ts +41 -0
  276. package/src/terrenoApp.ts +94 -31
  277. package/src/tests/createTestData.ts +2 -2
  278. package/src/tests/models.ts +10 -2
  279. package/src/tests/testHelper.ts +1 -7
  280. package/src/types/authToken.ts +2 -4
  281. package/src/types/mcpServiceToken.ts +1 -7
  282. package/src/types/membership.ts +48 -0
  283. package/src/types/notification.ts +34 -0
  284. package/src/types/notificationPreference.ts +28 -0
  285. package/src/types/organization.ts +49 -0
  286. package/src/webhooks/idempotency/webhookReceipt.ts +4 -7
  287. package/src/webhooks/rawBody.test.ts +33 -0
  288. package/src/webhooks/rawBody.ts +1 -0
  289. package/tsconfig.json +1 -1
  290. package/tsconfig.typedoc.json +2 -1
  291. package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.d.ts +0 -2
  292. package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
  293. package/src/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
@@ -0,0 +1,124 @@
1
+ /**
2
+ * Load and validate timestamped `migrations/<YYYYMMDDHHmmss>-<slug>.ts` files
3
+ * without connecting to Mongo. Used by `terreno-migrate check` and as input to the runner.
4
+ */
5
+ import {createHash} from "node:crypto";
6
+ import {readdir, readFile} from "node:fs/promises";
7
+ import {join} from "node:path";
8
+ import {pathToFileURL} from "node:url";
9
+
10
+ import {APIError} from "../errors";
11
+ import type {LoadedMigration, MigrationModule} from "./types";
12
+
13
+ export const MIGRATION_FILENAME = /^(\d{14})-[a-z0-9-]+\.ts$/;
14
+
15
+ /** Bun compile (`bun build --compile`) puts modules on a virtual FS. */
16
+ const isBunVirtualFsPath = (value: string): boolean => {
17
+ return value.includes("$bunfs");
18
+ };
19
+
20
+ /**
21
+ * Pick a real on-disk migrations directory.
22
+ * `bun build --compile` rewrites `import.meta.url` to `$bunfs`, which is not
23
+ * scandir-able. Prefer an explicit real `dir`, then `MIGRATIONS_DIR`, then
24
+ * `<cwd>/migrations`.
25
+ */
26
+ export const resolveMigrationDir = ({
27
+ cwd = process.cwd(),
28
+ dir,
29
+ envDir = process.env.MIGRATIONS_DIR,
30
+ }: {
31
+ cwd?: string;
32
+ dir?: string;
33
+ envDir?: string;
34
+ }): string => {
35
+ if (dir && !isBunVirtualFsPath(dir)) {
36
+ return dir;
37
+ }
38
+ if (envDir) {
39
+ return envDir;
40
+ }
41
+ return join(cwd, "migrations");
42
+ };
43
+
44
+ const readMigrationFilenames = async (dir: string): Promise<string[]> => {
45
+ try {
46
+ return await readdir(dir);
47
+ } catch (error) {
48
+ const code = (error as NodeJS.ErrnoException).code;
49
+ if (code === "ENOENT") {
50
+ throw new APIError({
51
+ detail: `No directory at ${dir}`,
52
+ status: 404,
53
+ title: "Migration directory not found",
54
+ });
55
+ }
56
+ throw error;
57
+ }
58
+ };
59
+
60
+ const checksumOf = (source: string): string => {
61
+ return createHash("sha256").update(source).digest("hex");
62
+ };
63
+
64
+ const expectedIdFromFilename = (filename: string): string => {
65
+ return filename.replace(/\.ts$/, "");
66
+ };
67
+
68
+ export const checkMigrationFiles = async ({dir}: {dir: string}): Promise<LoadedMigration[]> => {
69
+ const resolvedDir = resolveMigrationDir({dir});
70
+ const entries = await readMigrationFilenames(resolvedDir);
71
+ const files = entries.filter((name) => name.endsWith(".ts")).sort();
72
+ const loaded: LoadedMigration[] = [];
73
+ const seenIds = new Set<string>();
74
+
75
+ for (const filename of files) {
76
+ if (!MIGRATION_FILENAME.test(filename)) {
77
+ throw new APIError({
78
+ detail: `Expected YYYYMMDDHHmmss-slug.ts, received ${filename}`,
79
+ status: 400,
80
+ title: "Invalid migration filename",
81
+ });
82
+ }
83
+
84
+ const source = await readFile(join(resolvedDir, filename), "utf8");
85
+ const mod = (await import(pathToFileURL(join(resolvedDir, filename)).href)) as MigrationModule;
86
+ const expectedId = expectedIdFromFilename(filename);
87
+ if (mod.id !== expectedId) {
88
+ throw new APIError({
89
+ detail: `File ${filename} exported id ${String(mod.id)}`,
90
+ status: 400,
91
+ title: "Migration id does not match filename",
92
+ });
93
+ }
94
+ if (typeof mod.up !== "function") {
95
+ throw new APIError({
96
+ detail: `${filename} must export an up function`,
97
+ status: 400,
98
+ title: "Migration missing up",
99
+ });
100
+ }
101
+ if (seenIds.has(mod.id)) {
102
+ throw new APIError({
103
+ detail: `Duplicate migration id ${mod.id}`,
104
+ status: 400,
105
+ title: "Duplicate migration id",
106
+ });
107
+ }
108
+ seenIds.add(mod.id);
109
+ loaded.push({
110
+ checksum: checksumOf(source),
111
+ down: mod.down,
112
+ id: mod.id,
113
+ schemaAfter: mod.schemaAfter,
114
+ up: mod.up,
115
+ });
116
+ }
117
+
118
+ return loaded;
119
+ };
120
+
121
+ /** Public alias for `checkMigrationFiles`; kept as a wrapper so Knip does not treat it as a duplicate export. */
122
+ export const loadMigrations = async ({dir}: {dir: string}): Promise<LoadedMigration[]> => {
123
+ return checkMigrationFiles({dir});
124
+ };
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Tests for the migrate lock: wait, heartbeat, and steal after TTL.
3
+ */
4
+ import {beforeEach, describe, expect, it} from "bun:test";
5
+ import {DateTime} from "luxon";
6
+ import mongoose from "mongoose";
7
+
8
+ import {setupDb} from "../tests";
9
+ import {withMigrationLock} from "./lock";
10
+ import {runMigrations} from "./runner";
11
+ import {type LoadedMigration, MIGRATION_LOCK_ID, MIGRATIONS_COLLECTION} from "./types";
12
+
13
+ const collection = (): mongoose.Collection => {
14
+ return mongoose.connection.collection(MIGRATIONS_COLLECTION);
15
+ };
16
+
17
+ describe("withMigrationLock", () => {
18
+ beforeEach(async () => {
19
+ await setupDb();
20
+ await collection().deleteMany({});
21
+ });
22
+
23
+ it("releases the lock in finally", async () => {
24
+ await withMigrationLock({
25
+ connection: mongoose.connection,
26
+ fn: async () => "ok",
27
+ ttlMs: 60_000,
28
+ });
29
+
30
+ const lock = await collection().findOne({_id: MIGRATION_LOCK_ID});
31
+ expect(lock).toBeNull();
32
+ });
33
+
34
+ it("steals a lock whose expiresAt is in the past", async () => {
35
+ await collection().insertOne({
36
+ _id: MIGRATION_LOCK_ID,
37
+ expiresAt: DateTime.now().minus({minutes: 11}).toJSDate(),
38
+ heartbeatAt: DateTime.now().minus({minutes: 11}).toJSDate(),
39
+ holder: "dead-holder",
40
+ });
41
+
42
+ let ran = false;
43
+ await withMigrationLock({
44
+ connection: mongoose.connection,
45
+ fn: async () => {
46
+ ran = true;
47
+ },
48
+ pollMs: 10,
49
+ ttlMs: 60_000,
50
+ });
51
+
52
+ expect(ran).toBe(true);
53
+ const lock = await collection().findOne({_id: MIGRATION_LOCK_ID});
54
+ expect(lock).toBeNull();
55
+ });
56
+
57
+ it("heartbeats the lock while the critical section runs", async () => {
58
+ await withMigrationLock({
59
+ connection: mongoose.connection,
60
+ fn: async () => {
61
+ const first = await collection().findOne({_id: MIGRATION_LOCK_ID});
62
+ await Bun.sleep(400);
63
+ const second = await collection().findOne({_id: MIGRATION_LOCK_ID});
64
+ expect(second?.heartbeatAt).toBeDefined();
65
+ const firstBeat = DateTime.fromJSDate(first?.heartbeatAt as Date);
66
+ const secondBeat = DateTime.fromJSDate(second?.heartbeatAt as Date);
67
+ expect(secondBeat.toMillis()).toBeGreaterThan(firstBeat.toMillis());
68
+ },
69
+ holder: "heartbeat-holder",
70
+ pollMs: 10,
71
+ ttlMs: 150,
72
+ });
73
+ expect(await collection().findOne({_id: MIGRATION_LOCK_ID})).toBeNull();
74
+ });
75
+
76
+ it("rethrows non-duplicate insert errors while acquiring", async () => {
77
+ const col = collection();
78
+ const originalInsertOne = col.insertOne.bind(col);
79
+ Object.assign(col, {
80
+ insertOne: async () => {
81
+ throw Object.assign(new Error("write concern"), {code: 50});
82
+ },
83
+ });
84
+ try {
85
+ await expect(
86
+ withMigrationLock({
87
+ connection: mongoose.connection,
88
+ fn: async () => "ok",
89
+ pollMs: 10,
90
+ ttlMs: 60_000,
91
+ })
92
+ ).rejects.toThrow("write concern");
93
+ } finally {
94
+ Object.assign(col, {insertOne: originalInsertOne});
95
+ }
96
+ });
97
+ });
98
+
99
+ describe("runMigrations lock", () => {
100
+ beforeEach(async () => {
101
+ await setupDb();
102
+ await collection().deleteMany({});
103
+ });
104
+
105
+ it("serializes overlapping wet runs", async () => {
106
+ let concurrent = 0;
107
+ let maxConcurrent = 0;
108
+ const migrations: LoadedMigration[] = [
109
+ {
110
+ checksum: "lock-1",
111
+ id: "20260910130000-lock",
112
+ up: async () => {
113
+ concurrent += 1;
114
+ maxConcurrent = Math.max(maxConcurrent, concurrent);
115
+ await Bun.sleep(80);
116
+ concurrent -= 1;
117
+ },
118
+ },
119
+ ];
120
+ const options = {
121
+ connection: mongoose.connection,
122
+ dryRun: false,
123
+ migrations,
124
+ mongoose,
125
+ };
126
+
127
+ await Promise.all([runMigrations(options), runMigrations(options)]);
128
+
129
+ expect(maxConcurrent).toBe(1);
130
+ const stored = await collection().findOne({_id: "20260910130000-lock"});
131
+ expect(stored?.checksum).toBe("lock-1");
132
+ });
133
+ });
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Exclusive lock for wet migrate so concurrent CLI, boot, and admin runs cannot
3
+ * double-apply. Holders heartbeat; a stale lock may be stolen after the TTL.
4
+ */
5
+ import {randomUUID} from "node:crypto";
6
+
7
+ import {DateTime} from "luxon";
8
+ import type mongoose from "mongoose";
9
+
10
+ import {MIGRATION_LOCK_ID, MIGRATIONS_COLLECTION, type MigrationStoreDoc} from "./types";
11
+
12
+ /** Default lock lifetime before another runner may steal it. */
13
+ export const MIGRATION_LOCK_TTL_MS = 10 * 60 * 1000;
14
+
15
+ export interface WithMigrationLockOptions<T> {
16
+ connection: mongoose.Connection;
17
+ fn: () => Promise<T>;
18
+ ttlMs?: number;
19
+ pollMs?: number;
20
+ holder?: string;
21
+ now?: () => DateTime;
22
+ }
23
+
24
+ const lockCollection = (
25
+ connection: mongoose.Connection
26
+ ): mongoose.Collection<MigrationStoreDoc> => {
27
+ return connection.collection<MigrationStoreDoc>(MIGRATIONS_COLLECTION);
28
+ };
29
+
30
+ const isDuplicateKey = (error: unknown): boolean => {
31
+ if (typeof error !== "object" || error === null) {
32
+ return false;
33
+ }
34
+ const code = "code" in error ? error.code : undefined;
35
+ return code === 11000;
36
+ };
37
+
38
+ const tryAcquire = async ({
39
+ col,
40
+ expiresAt,
41
+ holder,
42
+ nowJs,
43
+ }: {
44
+ col: mongoose.Collection<MigrationStoreDoc>;
45
+ expiresAt: Date;
46
+ holder: string;
47
+ nowJs: Date;
48
+ }): Promise<boolean> => {
49
+ try {
50
+ await col.insertOne({
51
+ _id: MIGRATION_LOCK_ID,
52
+ expiresAt,
53
+ heartbeatAt: nowJs,
54
+ holder,
55
+ });
56
+ return true;
57
+ } catch (error) {
58
+ if (!isDuplicateKey(error)) {
59
+ throw error;
60
+ }
61
+ }
62
+
63
+ const stolen = await col.findOneAndUpdate(
64
+ {_id: MIGRATION_LOCK_ID, expiresAt: {$lte: nowJs}},
65
+ {$set: {expiresAt, heartbeatAt: nowJs, holder}},
66
+ {returnDocument: "after"}
67
+ );
68
+ return stolen !== null && stolen.holder === holder;
69
+ };
70
+
71
+ export const withMigrationLock = async <T>({
72
+ connection,
73
+ fn,
74
+ holder = randomUUID(),
75
+ now = () => DateTime.now(),
76
+ pollMs = 1000,
77
+ ttlMs = MIGRATION_LOCK_TTL_MS,
78
+ }: WithMigrationLockOptions<T>): Promise<T> => {
79
+ const col = lockCollection(connection);
80
+
81
+ const acquire = async (): Promise<void> => {
82
+ while (true) {
83
+ const nowDt = now();
84
+ const nowJs = nowDt.toJSDate();
85
+ const expiresAt = nowDt.plus({milliseconds: ttlMs}).toJSDate();
86
+ const acquired = await tryAcquire({col, expiresAt, holder, nowJs});
87
+ if (acquired) {
88
+ return;
89
+ }
90
+ await Bun.sleep(pollMs);
91
+ }
92
+ };
93
+
94
+ await acquire();
95
+
96
+ const heartbeat = setInterval(
97
+ () => {
98
+ const nowDt = now();
99
+ void col.updateOne(
100
+ {_id: MIGRATION_LOCK_ID, holder},
101
+ {
102
+ $set: {
103
+ expiresAt: nowDt.plus({milliseconds: ttlMs}).toJSDate(),
104
+ heartbeatAt: nowDt.toJSDate(),
105
+ },
106
+ }
107
+ );
108
+ },
109
+ Math.max(50, Math.floor(ttlMs / 3))
110
+ );
111
+
112
+ try {
113
+ return await fn();
114
+ } finally {
115
+ clearInterval(heartbeat);
116
+ await col.deleteOne({_id: MIGRATION_LOCK_ID, holder});
117
+ }
118
+ };