@hot-updater/server 1.0.0-rc.2 → 1.0.0-rc.21

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 (328) hide show
  1. package/LICENSE +12 -14
  2. package/dist/adapters/drizzle.cjs +32 -75
  3. package/dist/adapters/drizzle.d.cts +13 -5
  4. package/dist/adapters/drizzle.d.mts +13 -5
  5. package/dist/adapters/drizzle.mjs +32 -77
  6. package/dist/adapters/drizzleExecutor.cjs +114 -0
  7. package/dist/adapters/drizzleExecutor.d.cts +9 -0
  8. package/dist/adapters/drizzleExecutor.d.mts +9 -0
  9. package/dist/adapters/drizzleExecutor.mjs +112 -0
  10. package/dist/adapters/kysely.cjs +22 -31
  11. package/dist/adapters/kysely.d.cts +10 -6
  12. package/dist/adapters/kysely.d.mts +10 -6
  13. package/dist/adapters/kysely.mjs +23 -33
  14. package/dist/adapters/kyselyExecutor.cjs +32 -0
  15. package/dist/adapters/kyselyExecutor.d.cts +11 -0
  16. package/dist/adapters/kyselyExecutor.d.mts +11 -0
  17. package/dist/adapters/kyselyExecutor.mjs +32 -0
  18. package/dist/adapters/mongodb.cjs +18 -54
  19. package/dist/adapters/mongodb.d.cts +10 -5
  20. package/dist/adapters/mongodb.d.mts +10 -5
  21. package/dist/adapters/mongodb.mjs +19 -56
  22. package/dist/adapters/mongodbAdapter.cjs +192 -0
  23. package/dist/adapters/mongodbAdapter.d.cts +9 -0
  24. package/dist/adapters/mongodbAdapter.d.mts +9 -0
  25. package/dist/adapters/mongodbAdapter.mjs +191 -0
  26. package/dist/adapters/prisma.cjs +35 -267
  27. package/dist/adapters/prisma.d.cts +12 -7
  28. package/dist/adapters/prisma.d.mts +12 -7
  29. package/dist/adapters/prisma.mjs +36 -268
  30. package/dist/adapters/prismaExecutor.cjs +63 -0
  31. package/dist/adapters/prismaExecutor.mjs +63 -0
  32. package/dist/adapters/sqlProviders.cjs +14 -0
  33. package/dist/adapters/sqlProviders.mjs +14 -0
  34. package/dist/assembly/assemblePlugins.cjs +144 -0
  35. package/dist/assembly/assemblePlugins.d.cts +5 -0
  36. package/dist/assembly/assemblePlugins.d.mts +5 -0
  37. package/dist/assembly/assemblePlugins.mjs +139 -0
  38. package/dist/assembly/clientPlugins.cjs +88 -0
  39. package/dist/assembly/clientPlugins.mjs +88 -0
  40. package/dist/assembly/configError.d.cts +2 -0
  41. package/dist/assembly/configError.d.mts +2 -0
  42. package/dist/assembly/configError.mjs +2 -0
  43. package/dist/core/api.cjs +51 -0
  44. package/dist/core/api.d.cts +54 -0
  45. package/dist/core/api.d.mts +54 -0
  46. package/dist/core/api.mjs +51 -0
  47. package/dist/core/baseCandidates.cjs +28 -0
  48. package/dist/core/baseCandidates.mjs +28 -0
  49. package/dist/core/operations.cjs +351 -0
  50. package/dist/core/operations.mjs +351 -0
  51. package/dist/core/reads.cjs +273 -0
  52. package/dist/core/reads.d.cts +9 -0
  53. package/dist/core/reads.d.mts +9 -0
  54. package/dist/core/reads.mjs +267 -0
  55. package/dist/core/releases.cjs +193 -0
  56. package/dist/core/releases.mjs +190 -0
  57. package/dist/core/writes.cjs +96 -0
  58. package/dist/core/writes.mjs +91 -0
  59. package/dist/createHotUpdaterCore.cjs +72 -91
  60. package/dist/createHotUpdaterCore.d.cts +106 -38
  61. package/dist/createHotUpdaterCore.d.mts +106 -38
  62. package/dist/createHotUpdaterCore.mjs +74 -89
  63. package/dist/db/engineDrizzleSchema.cjs +53 -0
  64. package/dist/db/engineDrizzleSchema.mjs +53 -0
  65. package/dist/db/enginePrismaSchema.cjs +84 -0
  66. package/dist/db/enginePrismaSchema.d.cts +5 -0
  67. package/dist/db/enginePrismaSchema.d.mts +5 -0
  68. package/dist/db/enginePrismaSchema.mjs +83 -0
  69. package/dist/db/releaseCatalog.cjs +24 -47
  70. package/dist/db/releaseCatalog.mjs +20 -44
  71. package/dist/db/updateArtifacts.cjs +43 -72
  72. package/dist/db/updateArtifacts.mjs +40 -69
  73. package/dist/handler.cjs +59 -43
  74. package/dist/handler.d.cts +3 -5
  75. package/dist/handler.d.mts +3 -5
  76. package/dist/handler.mjs +61 -43
  77. package/dist/handlerAdminRoutes.cjs +374 -0
  78. package/dist/handlerAdminRoutes.mjs +372 -0
  79. package/dist/handlerParameters.cjs +6 -11
  80. package/dist/handlerParameters.mjs +6 -10
  81. package/dist/handlerReleaseCatalogRoutes.cjs +32 -15
  82. package/dist/handlerReleaseCatalogRoutes.mjs +32 -15
  83. package/dist/handlerTypes.d.cts +2 -55
  84. package/dist/handlerTypes.d.mts +2 -55
  85. package/dist/handlerVersionRoutes.cjs +20 -4
  86. package/dist/handlerVersionRoutes.mjs +20 -4
  87. package/dist/index.cjs +9 -9
  88. package/dist/index.d.cts +8 -9
  89. package/dist/index.d.mts +8 -9
  90. package/dist/index.mjs +3 -4
  91. package/dist/node.cjs +3 -6
  92. package/dist/node.d.cts +2 -2
  93. package/dist/node.d.mts +2 -2
  94. package/dist/node.mjs +4 -4
  95. package/dist/package.cjs +1 -1
  96. package/dist/package.mjs +1 -1
  97. package/dist/plugins/api-keys/index.cjs +9 -0
  98. package/dist/plugins/api-keys/index.d.cts +1 -0
  99. package/dist/plugins/api-keys/index.d.mts +1 -0
  100. package/dist/plugins/api-keys/index.mjs +2 -0
  101. package/dist/plugins/definePlugin.d.cts +2 -0
  102. package/dist/plugins/definePlugin.d.mts +2 -0
  103. package/dist/plugins/insights/index.cjs +9 -0
  104. package/dist/plugins/insights/index.d.cts +1 -0
  105. package/dist/plugins/insights/index.d.mts +1 -0
  106. package/dist/plugins/insights/index.mjs +2 -0
  107. package/dist/plugins/official.cjs +30 -0
  108. package/dist/plugins/official.mjs +29 -0
  109. package/dist/storageAccess.cjs +13 -10
  110. package/dist/storageAccess.mjs +13 -10
  111. package/dist/types/index.d.cts +2 -28
  112. package/dist/types/index.d.mts +2 -28
  113. package/package.json +26 -14
  114. package/dist/_virtual/_rolldown/runtime.cjs +0 -23
  115. package/dist/adapters/databaseConstraintErrors.cjs +0 -22
  116. package/dist/adapters/databaseConstraintErrors.mjs +0 -21
  117. package/dist/adapters/databasePluginUtils.cjs +0 -116
  118. package/dist/adapters/databasePluginUtils.mjs +0 -106
  119. package/dist/adapters/drizzleCrud.cjs +0 -311
  120. package/dist/adapters/drizzleCrud.mjs +0 -308
  121. package/dist/adapters/drizzleLazyDB.cjs +0 -106
  122. package/dist/adapters/drizzleLazyDB.mjs +0 -106
  123. package/dist/adapters/drizzleQuery.cjs +0 -69
  124. package/dist/adapters/drizzleQuery.mjs +0 -69
  125. package/dist/adapters/insightsTransaction.cjs +0 -22
  126. package/dist/adapters/insightsTransaction.mjs +0 -22
  127. package/dist/adapters/kyselyCrud.cjs +0 -305
  128. package/dist/adapters/kyselyCrud.mjs +0 -304
  129. package/dist/adapters/kyselyQuery.cjs +0 -56
  130. package/dist/adapters/kyselyQuery.mjs +0 -56
  131. package/dist/adapters/mongodbCollections.cjs +0 -38
  132. package/dist/adapters/mongodbCollections.mjs +0 -32
  133. package/dist/adapters/mongodbQuery.cjs +0 -94
  134. package/dist/adapters/mongodbQuery.mjs +0 -86
  135. package/dist/adapters/mongodbReads.cjs +0 -210
  136. package/dist/adapters/mongodbReads.mjs +0 -210
  137. package/dist/adapters/mongodbWrites.cjs +0 -148
  138. package/dist/adapters/mongodbWrites.mjs +0 -148
  139. package/dist/adapters/prismaQuery.cjs +0 -57
  140. package/dist/adapters/prismaQuery.mjs +0 -56
  141. package/dist/adapters/prismaRows.cjs +0 -219
  142. package/dist/adapters/prismaRows.mjs +0 -209
  143. package/dist/apiKeys.cjs +0 -143
  144. package/dist/apiKeys.d.cts +0 -49
  145. package/dist/apiKeys.d.mts +0 -49
  146. package/dist/apiKeys.mjs +0 -133
  147. package/dist/db/createBundleDiff.cjs +0 -144
  148. package/dist/db/createBundleDiff.d.cts +0 -16
  149. package/dist/db/createBundleDiff.d.mts +0 -16
  150. package/dist/db/createBundleDiff.mjs +0 -141
  151. package/dist/db/databasePluginCore.cjs +0 -137
  152. package/dist/db/databasePluginCore.mjs +0 -137
  153. package/dist/db/fixedMigrator.cjs +0 -79
  154. package/dist/db/fixedMigrator.mjs +0 -78
  155. package/dist/db/fixedMigratorMongo.cjs +0 -321
  156. package/dist/db/fixedMigratorMongo.mjs +0 -321
  157. package/dist/db/fixedMigratorShared.cjs +0 -23
  158. package/dist/db/fixedMigratorShared.mjs +0 -18
  159. package/dist/db/index.cjs +0 -27
  160. package/dist/db/index.d.cts +0 -12
  161. package/dist/db/index.d.mts +0 -12
  162. package/dist/db/index.mjs +0 -22
  163. package/dist/db/mongoMigrationExecution.cjs +0 -11
  164. package/dist/db/mongoMigrationExecution.mjs +0 -11
  165. package/dist/db/releaseCatalog.d.cts +0 -16
  166. package/dist/db/releaseCatalog.d.mts +0 -16
  167. package/dist/db/schema/insightsCollation.cjs +0 -14
  168. package/dist/db/schema/insightsCollation.mjs +0 -14
  169. package/dist/db/schema/mongodb.cjs +0 -25
  170. package/dist/db/schema/mongodb.mjs +0 -25
  171. package/dist/db/schema/registry.cjs +0 -21
  172. package/dist/db/schema/registry.mjs +0 -17
  173. package/dist/db/schema/sql.cjs +0 -105
  174. package/dist/db/schema/sql.mjs +0 -97
  175. package/dist/db/schema/sqlOperations.cjs +0 -27
  176. package/dist/db/schema/sqlOperations.mjs +0 -26
  177. package/dist/db/schemaGenerators.cjs +0 -275
  178. package/dist/db/schemaGenerators.mjs +0 -273
  179. package/dist/db/schemaReadiness.cjs +0 -26
  180. package/dist/db/schemaReadiness.d.cts +0 -9
  181. package/dist/db/schemaReadiness.d.mts +0 -9
  182. package/dist/db/schemaReadiness.mjs +0 -25
  183. package/dist/db/sqlMigrationExecution.cjs +0 -67
  184. package/dist/db/sqlMigrationExecution.mjs +0 -67
  185. package/dist/db/types.cjs +0 -22
  186. package/dist/db/types.d.cts +0 -118
  187. package/dist/db/types.d.mts +0 -118
  188. package/dist/db/types.mjs +0 -19
  189. package/dist/handlerBundleRoutes.cjs +0 -155
  190. package/dist/handlerBundleRoutes.mjs +0 -155
  191. package/dist/handlerReleaseManagementRoutes.cjs +0 -124
  192. package/dist/handlerReleaseManagementRoutes.mjs +0 -124
  193. package/dist/insights/domain.d.cts +0 -89
  194. package/dist/insights/domain.d.mts +0 -89
  195. package/dist/insights/errors.cjs +0 -15
  196. package/dist/insights/errors.mjs +0 -14
  197. package/dist/insights/eventInput.cjs +0 -155
  198. package/dist/insights/eventInput.mjs +0 -153
  199. package/dist/insights/provider.cjs +0 -314
  200. package/dist/insights/provider.d.cts +0 -6
  201. package/dist/insights/provider.d.mts +0 -6
  202. package/dist/insights/provider.mjs +0 -314
  203. package/dist/insights/queryInput.cjs +0 -93
  204. package/dist/insights/queryInput.mjs +0 -91
  205. package/dist/insights/routes.cjs +0 -58
  206. package/dist/insights/routes.mjs +0 -56
  207. package/dist/insights/types.d.cts +0 -32
  208. package/dist/insights/types.d.mts +0 -32
  209. package/dist/schema/dsl.cjs +0 -103
  210. package/dist/schema/dsl.mjs +0 -87
  211. package/dist/schema/index.cjs +0 -20
  212. package/dist/schema/index.mjs +0 -7
  213. package/dist/schema/settings.cjs +0 -9
  214. package/dist/schema/settings.mjs +0 -9
  215. package/dist/schema/types.cjs +0 -8
  216. package/dist/schema/types.mjs +0 -6
  217. package/dist/schema/v1_0_0.cjs +0 -406
  218. package/dist/schema/v1_0_0.mjs +0 -398
  219. package/src/adapters/databaseConstraintErrors.ts +0 -25
  220. package/src/adapters/databasePluginTestDatabase.ts +0 -120
  221. package/src/adapters/databasePluginUtils.spec.ts +0 -29
  222. package/src/adapters/databasePluginUtils.ts +0 -214
  223. package/src/adapters/drizzle.spec.ts +0 -336
  224. package/src/adapters/drizzle.ts +0 -162
  225. package/src/adapters/drizzleCrud.ts +0 -571
  226. package/src/adapters/drizzleLazyDB.ts +0 -271
  227. package/src/adapters/drizzleQuery.ts +0 -164
  228. package/src/adapters/insightsTransaction.ts +0 -32
  229. package/src/adapters/kysely.spec.ts +0 -174
  230. package/src/adapters/kysely.ts +0 -110
  231. package/src/adapters/kyselyCrud.ts +0 -709
  232. package/src/adapters/kyselyQuery.ts +0 -128
  233. package/src/adapters/mongodb.integration.spec.ts +0 -210
  234. package/src/adapters/mongodb.spec.ts +0 -209
  235. package/src/adapters/mongodb.ts +0 -138
  236. package/src/adapters/mongodbCollections.ts +0 -67
  237. package/src/adapters/mongodbQuery.ts +0 -255
  238. package/src/adapters/mongodbReads.ts +0 -380
  239. package/src/adapters/mongodbRows.spec.ts +0 -64
  240. package/src/adapters/mongodbRows.ts +0 -118
  241. package/src/adapters/mongodbTestClient.ts +0 -377
  242. package/src/adapters/mongodbTestFilter.ts +0 -169
  243. package/src/adapters/mongodbWrites.ts +0 -287
  244. package/src/adapters/prisma.spec.ts +0 -293
  245. package/src/adapters/prisma.ts +0 -565
  246. package/src/adapters/prismaQuery.ts +0 -129
  247. package/src/adapters/prismaRows.spec.ts +0 -98
  248. package/src/adapters/prismaRows.ts +0 -337
  249. package/src/adapters/prismaTestClient.ts +0 -413
  250. package/src/apiKeys.integration.spec.ts +0 -192
  251. package/src/apiKeys.spec.ts +0 -162
  252. package/src/apiKeys.ts +0 -220
  253. package/src/calculatePagination.ts +0 -34
  254. package/src/createHotUpdaterCore.ts +0 -246
  255. package/src/db/bundleRows.spec.ts +0 -164
  256. package/src/db/bundleRows.ts +0 -10
  257. package/src/db/createBundleDiff.spec.ts +0 -450
  258. package/src/db/createBundleDiff.ts +0 -364
  259. package/src/db/databasePluginCore.spec.ts +0 -62
  260. package/src/db/databasePluginCore.testFixtures.ts +0 -76
  261. package/src/db/databasePluginCore.ts +0 -185
  262. package/src/db/databasePluginUpdateCheck.bench.ts +0 -110
  263. package/src/db/fixedMigrator.spec.ts +0 -296
  264. package/src/db/fixedMigrator.ts +0 -147
  265. package/src/db/fixedMigratorMongo.spec.ts +0 -239
  266. package/src/db/fixedMigratorMongo.ts +0 -425
  267. package/src/db/fixedMigratorShared.ts +0 -36
  268. package/src/db/hotUpdaterSchema.ts +0 -5
  269. package/src/db/index.spec.ts +0 -1269
  270. package/src/db/index.ts +0 -47
  271. package/src/db/mongoMigrationExecution.ts +0 -23
  272. package/src/db/node-sqlite.d.ts +0 -25
  273. package/src/db/releaseCatalog.ts +0 -148
  274. package/src/db/schema/definitions.ts +0 -1
  275. package/src/db/schema/insightsCollation.ts +0 -21
  276. package/src/db/schema/mongodb.ts +0 -40
  277. package/src/db/schema/registry.ts +0 -55
  278. package/src/db/schema/sql.ts +0 -244
  279. package/src/db/schema/sqlOperations.ts +0 -63
  280. package/src/db/schema/types.ts +0 -1
  281. package/src/db/schemaGenerators.ts +0 -494
  282. package/src/db/schemaReadiness.ts +0 -34
  283. package/src/db/sqlMigrationExecution.spec.ts +0 -65
  284. package/src/db/sqlMigrationExecution.ts +0 -108
  285. package/src/db/types.spec.ts +0 -63
  286. package/src/db/types.ts +0 -228
  287. package/src/db/updateArtifacts.spec.ts +0 -359
  288. package/src/db/updateArtifacts.ts +0 -541
  289. package/src/handler-standalone.integration.spec.ts +0 -528
  290. package/src/handler.spec.ts +0 -190
  291. package/src/handler.testFixtures.ts +0 -30
  292. package/src/handler.ts +0 -245
  293. package/src/handlerAuth.integration.spec.ts +0 -33
  294. package/src/handlerBundleRoutes.ts +0 -259
  295. package/src/handlerErrors.ts +0 -7
  296. package/src/handlerManagement.spec.ts +0 -262
  297. package/src/handlerParameters.ts +0 -105
  298. package/src/handlerReleaseCatalogRoutes.ts +0 -154
  299. package/src/handlerReleaseManagementRoutes.ts +0 -213
  300. package/src/handlerTypes.ts +0 -96
  301. package/src/handlerVersionRoutes.ts +0 -12
  302. package/src/index.ts +0 -30
  303. package/src/insights/domain.ts +0 -103
  304. package/src/insights/errors.ts +0 -11
  305. package/src/insights/eventInput.ts +0 -225
  306. package/src/insights/insights.integration.spec.ts +0 -381
  307. package/src/insights/provider.spec.ts +0 -385
  308. package/src/insights/provider.ts +0 -591
  309. package/src/insights/queryInput.ts +0 -141
  310. package/src/insights/routes.ts +0 -104
  311. package/src/insights/types.ts +0 -55
  312. package/src/internalRouter.ts +0 -104
  313. package/src/node.spec.ts +0 -119
  314. package/src/node.ts +0 -111
  315. package/src/releaseCatalogRoutes.integration.spec.ts +0 -238
  316. package/src/runtime.spec.ts +0 -194
  317. package/src/runtime.testFixtures.ts +0 -71
  318. package/src/schema/dsl-all-versions.spec.ts +0 -37
  319. package/src/schema/dsl.ts +0 -187
  320. package/src/schema/index.ts +0 -10
  321. package/src/schema/settings.ts +0 -15
  322. package/src/schema/types.ts +0 -79
  323. package/src/schema/v1_0_0.spec.ts +0 -220
  324. package/src/schema/v1_0_0.ts +0 -529
  325. package/src/storageAccess.spec.ts +0 -152
  326. package/src/storageAccess.ts +0 -141
  327. package/src/types/index.ts +0 -34
  328. package/src/version.ts +0 -3
@@ -1,64 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import {
4
- createBundlePatchRowFixture,
5
- createBundleRowFixture,
6
- } from "../../../test-utils/src/databaseTestFixtures";
7
- import { parseMongoBundleRow, parseMongoPatchRow } from "./mongodbRows";
8
-
9
- describe("parseMongoBundleRow", () => {
10
- it("rejects a missing metadata field", () => {
11
- const { metadata: _metadata, ...row } =
12
- createBundleRowFixture("missing-metadata");
13
-
14
- expect(() => parseMongoBundleRow(row)).toThrow(
15
- "Invalid MongoDB plugin data",
16
- );
17
- });
18
-
19
- it("rejects explicit null metadata", () => {
20
- expect(() =>
21
- parseMongoBundleRow({
22
- ...createBundleRowFixture("null-metadata"),
23
- metadata: null,
24
- }),
25
- ).toThrow("Invalid MongoDB plugin data");
26
- });
27
-
28
- it("preserves safe archive sizes above 2 GiB", () => {
29
- expect(parseMongoBundleRow(createBundleRowFixture("large"))).toMatchObject({
30
- archive_byte_size: 3_000_000_001,
31
- });
32
- });
33
-
34
- it.each([-1, 1.5, Number.MAX_SAFE_INTEGER + 1, Number.NaN])(
35
- "rejects invalid archive size %s",
36
- (archiveByteSize) => {
37
- expect(() =>
38
- parseMongoBundleRow({
39
- ...createBundleRowFixture("invalid-size"),
40
- archive_byte_size: archiveByteSize,
41
- }),
42
- ).toThrow("Invalid MongoDB plugin data");
43
- },
44
- );
45
- });
46
-
47
- describe("parseMongoPatchRow", () => {
48
- const row = createBundlePatchRowFixture("large", "bundle", "base");
49
-
50
- it("preserves safe patch sizes above 2 GiB", () => {
51
- expect(parseMongoPatchRow(row)).toMatchObject({
52
- byte_size: 3_000_000_002,
53
- });
54
- });
55
-
56
- it.each([-1, 1.5, Number.MAX_SAFE_INTEGER + 1, Number.NaN])(
57
- "rejects invalid patch size %s",
58
- (patchByteSize) => {
59
- expect(() =>
60
- parseMongoPatchRow({ ...row, byte_size: patchByteSize }),
61
- ).toThrow("Invalid MongoDB plugin data");
62
- },
63
- );
64
- });
@@ -1,118 +0,0 @@
1
- import {
2
- isDatabaseMetadataObject,
3
- type BundlePatchRow,
4
- type BundleRow,
5
- type ChannelRow,
6
- } from "@hot-updater/plugin-core";
7
-
8
- export class MongoAdapterDataError extends Error {
9
- readonly name = "MongoAdapterDataError";
10
-
11
- constructor(readonly source: string) {
12
- super(`Invalid MongoDB plugin data at "${source}".`);
13
- }
14
- }
15
-
16
- const isRecord = (value: unknown): value is Record<string, unknown> =>
17
- typeof value === "object" && value !== null && !Array.isArray(value);
18
-
19
- const record = (value: unknown, source: string): Record<string, unknown> => {
20
- if (!isRecord(value)) {
21
- throw new MongoAdapterDataError(source);
22
- }
23
- return value;
24
- };
25
-
26
- const string = (value: unknown, source: string): string => {
27
- if (typeof value !== "string") throw new MongoAdapterDataError(source);
28
- return value;
29
- };
30
-
31
- const nullableString = (value: unknown, source: string): string | null => {
32
- if (value === null || value === undefined) return null;
33
- return string(value, source);
34
- };
35
-
36
- const integer = (
37
- value: unknown,
38
- source: string,
39
- maximum = Number.POSITIVE_INFINITY,
40
- ): number => {
41
- if (
42
- typeof value !== "number" ||
43
- !Number.isInteger(value) ||
44
- value < 0 ||
45
- value > maximum
46
- ) {
47
- throw new MongoAdapterDataError(source);
48
- }
49
- return value;
50
- };
51
-
52
- const platform = (value: unknown, source: string): "android" | "ios" => {
53
- if (value !== "android" && value !== "ios") {
54
- throw new MongoAdapterDataError(source);
55
- }
56
- return value;
57
- };
58
-
59
- const metadata = (value: unknown, source: string) => {
60
- if (!isDatabaseMetadataObject(value)) {
61
- throw new MongoAdapterDataError(source);
62
- }
63
- return value;
64
- };
65
-
66
- export const parseMongoBundleRow = (
67
- value: unknown,
68
- source = "bundles",
69
- ): BundleRow => {
70
- const input = record(value, source);
71
- return {
72
- id: string(input["id"], source),
73
- platform: platform(input["platform"], source),
74
- file_hash: string(input["file_hash"], source),
75
- git_commit_hash: nullableString(input["git_commit_hash"], source),
76
- storage_uri: string(input["storage_uri"], source),
77
- archive_byte_size: integer(
78
- input["archive_byte_size"],
79
- source,
80
- Number.MAX_SAFE_INTEGER,
81
- ),
82
- metadata: metadata(input["metadata"], source),
83
- manifest_storage_uri: nullableString(input["manifest_storage_uri"], source),
84
- manifest_file_hash: nullableString(input["manifest_file_hash"], source),
85
- asset_base_storage_uri: nullableString(
86
- input["asset_base_storage_uri"],
87
- source,
88
- ),
89
- };
90
- };
91
-
92
- export const parseMongoChannelRow = (
93
- value: unknown,
94
- source = "channels",
95
- ): ChannelRow => {
96
- const input = record(value, source);
97
- return {
98
- id: string(input["id"], source),
99
- name: string(input["name"], source),
100
- };
101
- };
102
-
103
- export const parseMongoPatchRow = (
104
- value: unknown,
105
- source = "bundle_patches",
106
- ): BundlePatchRow => {
107
- const input = record(value, source);
108
- return {
109
- id: string(input["id"], source),
110
- bundle_id: string(input["bundle_id"], source),
111
- base_bundle_id: string(input["base_bundle_id"], source),
112
- base_file_hash: string(input["base_file_hash"], source),
113
- patch_file_hash: string(input["patch_file_hash"], source),
114
- patch_storage_uri: string(input["patch_storage_uri"], source),
115
- byte_size: integer(input["byte_size"], source, Number.MAX_SAFE_INTEGER),
116
- order_index: integer(input["order_index"], source),
117
- };
118
- };
@@ -1,377 +0,0 @@
1
- import type { BundlePatchRow, BundleRow } from "@hot-updater/plugin-core";
2
- import type { ClientSession } from "mongodb";
3
- import { MongoClient } from "mongodb";
4
-
5
- import {
6
- matchesMongoTestFilter,
7
- type MongoTestRow,
8
- sortMongoTestRows,
9
- } from "./mongodbTestFilter";
10
-
11
- type Tables = {
12
- bundle_patches: MongoTestRow[];
13
- bundles: MongoTestRow[];
14
- bundle_events: MongoTestRow[];
15
- bundle_installations: MongoTestRow[];
16
- channels: MongoTestRow[];
17
- api_keys: MongoTestRow[];
18
- releases: MongoTestRow[];
19
- release_catalogs: MongoTestRow[];
20
- };
21
-
22
- type FindOptions = { readonly projection?: unknown };
23
- type UpdateInput =
24
- | { readonly $set: Readonly<Record<string, unknown>> }
25
- | { readonly $setOnInsert: MongoTestRow };
26
- type MongoTestHooks = {
27
- beforeBundlePatchInsert?: () => Promise<void>;
28
- failNextBundleTombstone: boolean;
29
- failNextInstallationWrite: boolean;
30
- operationCount: number;
31
- };
32
-
33
- class MongoTestConstraintError extends Error {
34
- readonly name = "MongoTestConstraintError";
35
- }
36
-
37
- class MongoTestCursor {
38
- private offset = 0;
39
- private maximum = Number.POSITIVE_INFINITY;
40
- private sortSpecification: unknown;
41
-
42
- constructor(
43
- private readonly rows: MongoTestRow[],
44
- private readonly projection?: unknown,
45
- ) {}
46
-
47
- limit(value: number): this {
48
- this.maximum = value;
49
- return this;
50
- }
51
-
52
- project(): this {
53
- return this;
54
- }
55
-
56
- skip(value: number): this {
57
- this.offset = value;
58
- return this;
59
- }
60
-
61
- sort(value: unknown): this {
62
- this.sortSpecification = value;
63
- return this;
64
- }
65
-
66
- async toArray(): Promise<MongoTestRow[]> {
67
- const rows = sortMongoTestRows(this.rows, this.sortSpecification);
68
- return structuredClone(
69
- rows
70
- .slice(this.offset, this.offset + this.maximum)
71
- .map((row) => projectMongoTestRow(row, this.projection)),
72
- );
73
- }
74
- }
75
-
76
- const projectMongoTestRow = (
77
- row: MongoTestRow,
78
- projection: unknown,
79
- ): MongoTestRow => {
80
- if (typeof projection !== "object" || projection === null) return row;
81
- const projected = structuredClone(row);
82
- for (const field of Object.keys(projected)) {
83
- if (Reflect.get(projection, field) === 0) {
84
- Reflect.deleteProperty(projected, field);
85
- }
86
- }
87
- return projected;
88
- };
89
-
90
- const createCollection = (
91
- tables: Tables,
92
- model: keyof Tables,
93
- hooks: MongoTestHooks,
94
- ) => ({
95
- countDocuments: async (filter?: unknown): Promise<number> => {
96
- hooks.operationCount += 1;
97
- return tables[model].filter((row) => matchesMongoTestFilter(row, filter))
98
- .length;
99
- },
100
- deleteMany: async (filter?: unknown): Promise<void> => {
101
- hooks.operationCount += 1;
102
- tables[model] = tables[model].filter(
103
- (row) => !matchesMongoTestFilter(row, filter),
104
- );
105
- },
106
- distinct: async (field: string, filter?: unknown): Promise<unknown[]> => {
107
- hooks.operationCount += 1;
108
- return Array.from(
109
- new Set(
110
- tables[model]
111
- .filter((row) => matchesMongoTestFilter(row, filter))
112
- .map((row) => Reflect.get(row, field)),
113
- ),
114
- );
115
- },
116
- find: (filter?: unknown, options?: FindOptions): MongoTestCursor => {
117
- hooks.operationCount += 1;
118
- return new MongoTestCursor(
119
- tables[model].filter((row) => matchesMongoTestFilter(row, filter)),
120
- options?.projection,
121
- );
122
- },
123
- findOne: async (
124
- filter?: unknown,
125
- options?: FindOptions,
126
- ): Promise<MongoTestRow | null> => {
127
- hooks.operationCount += 1;
128
- const row = tables[model].find((candidate) =>
129
- matchesMongoTestFilter(candidate, filter),
130
- );
131
- return row === undefined
132
- ? null
133
- : structuredClone(projectMongoTestRow(row, options?.projection));
134
- },
135
- findOneAndUpdate: async (
136
- filter: unknown,
137
- update: UpdateInput,
138
- ): Promise<MongoTestRow | null> => {
139
- hooks.operationCount += 1;
140
- const index = tables[model].findIndex((row) =>
141
- matchesMongoTestFilter(row, filter),
142
- );
143
- const current = tables[model][index];
144
- if (current === undefined) return null;
145
- if (!("$set" in update)) {
146
- throw new MongoTestConstraintError("findOneAndUpdate requires $set");
147
- }
148
- const updated = { ...current, ...update.$set } as MongoTestRow;
149
- tables[model][index] = updated;
150
- return structuredClone(updated);
151
- },
152
- insertOne: async (row: MongoTestRow): Promise<void> => {
153
- hooks.operationCount += 1;
154
- if (model === "bundle_installations" && hooks.failNextInstallationWrite) {
155
- hooks.failNextInstallationWrite = false;
156
- throw new MongoTestConstraintError("injected installation write failure");
157
- }
158
- if (model === "bundle_patches") await hooks.beforeBundlePatchInsert?.();
159
- const key =
160
- model === "bundle_installations" && "install_id" in row
161
- ? row.install_id
162
- : "id" in row
163
- ? row.id
164
- : row.scope_key;
165
- if (
166
- tables[model].some((candidate) =>
167
- model === "bundle_installations" && "install_id" in candidate
168
- ? candidate.install_id === key
169
- : "id" in candidate
170
- ? candidate.id === key
171
- : candidate.scope_key === key,
172
- )
173
- ) {
174
- throw new MongoTestConstraintError("duplicate id");
175
- }
176
- if (
177
- model === "channels" &&
178
- "name" in row &&
179
- tables.channels.some(
180
- (candidate) => "name" in candidate && candidate.name === row.name,
181
- )
182
- ) {
183
- throw new MongoTestConstraintError("duplicate channel name");
184
- }
185
- if (
186
- model === "api_keys" &&
187
- "hash" in row &&
188
- tables.api_keys.some(
189
- (candidate) => "hash" in candidate && candidate.hash === row.hash,
190
- )
191
- ) {
192
- throw new MongoTestConstraintError("duplicate hash");
193
- }
194
- tables[model].push(structuredClone(row));
195
- },
196
- updateMany: async (filter: unknown, update: UpdateInput): Promise<void> => {
197
- hooks.operationCount += 1;
198
- const values = "$set" in update ? update.$set : {};
199
- tables[model] = tables[model].map((row) =>
200
- matchesMongoTestFilter(row, filter)
201
- ? ({ ...row, ...values } as MongoTestRow)
202
- : row,
203
- );
204
- if (model === "bundles" && hooks.failNextBundleTombstone) {
205
- hooks.failNextBundleTombstone = false;
206
- throw new MongoTestConstraintError("injected tombstone failure");
207
- }
208
- },
209
- updateOne: async (
210
- filter: unknown,
211
- update: UpdateInput,
212
- options?: { readonly upsert?: boolean },
213
- ): Promise<{ matchedCount: number; upsertedCount: number }> => {
214
- hooks.operationCount += 1;
215
- if (model === "bundle_installations" && hooks.failNextInstallationWrite) {
216
- hooks.failNextInstallationWrite = false;
217
- throw new MongoTestConstraintError("injected installation write failure");
218
- }
219
- const index = tables[model].findIndex((row) =>
220
- matchesMongoTestFilter(row, filter),
221
- );
222
- if (index >= 0) {
223
- if ("$set" in update) {
224
- tables[model][index] = {
225
- ...tables[model][index],
226
- ...update.$set,
227
- } as MongoTestRow;
228
- }
229
- return { matchedCount: 1, upsertedCount: 0 };
230
- }
231
- if (options?.upsert === true && "$setOnInsert" in update) {
232
- const key =
233
- model === "bundle_installations" && "install_id" in update.$setOnInsert
234
- ? update.$setOnInsert.install_id
235
- : "id" in update.$setOnInsert
236
- ? update.$setOnInsert.id
237
- : update.$setOnInsert.scope_key;
238
- if (
239
- tables[model].some((row) =>
240
- model === "bundle_installations" && "install_id" in row
241
- ? row.install_id === key
242
- : "id" in row
243
- ? row.id === key
244
- : row.scope_key === key,
245
- )
246
- ) {
247
- throw new MongoTestConstraintError("duplicate id");
248
- }
249
- tables[model].push(structuredClone(update.$setOnInsert));
250
- return { matchedCount: 0, upsertedCount: 1 };
251
- }
252
- return { matchedCount: 0, upsertedCount: 0 };
253
- },
254
- });
255
-
256
- const createDatabase = (tables: Tables, hooks: MongoTestHooks) => ({
257
- collection: (name: string) => {
258
- switch (name) {
259
- case "bundles":
260
- return createCollection(tables, "bundles", hooks);
261
- case "bundle_patches":
262
- return createCollection(tables, "bundle_patches", hooks);
263
- case "bundle_events":
264
- return createCollection(tables, "bundle_events", hooks);
265
- case "bundle_installations":
266
- return createCollection(tables, "bundle_installations", hooks);
267
- case "channels":
268
- return createCollection(tables, "channels", hooks);
269
- case "api_keys":
270
- return createCollection(tables, "api_keys", hooks);
271
- case "releases":
272
- return createCollection(tables, "releases", hooks);
273
- case "release_catalogs":
274
- return createCollection(tables, "release_catalogs", hooks);
275
- default:
276
- throw new MongoTestConstraintError(`unknown collection: ${name}`);
277
- }
278
- },
279
- });
280
-
281
- export const createMongoTestHarness = () => {
282
- const tables: Tables = {
283
- bundle_patches: [],
284
- bundles: [],
285
- bundle_events: [],
286
- bundle_installations: [],
287
- channels: [],
288
- api_keys: [],
289
- releases: [],
290
- release_catalogs: [],
291
- };
292
- const hooks: MongoTestHooks = {
293
- failNextBundleTombstone: false,
294
- failNextInstallationWrite: false,
295
- operationCount: 0,
296
- };
297
- let activeTables = tables;
298
- let transactionQueue = Promise.resolve();
299
- const client = new MongoClient("mongodb://127.0.0.1:27017/hot_updater_test");
300
- Object.defineProperty(client, "db", {
301
- value: () => createDatabase(activeTables, hooks),
302
- });
303
- Object.defineProperty(client, "withSession", {
304
- value: async (
305
- callback: (session: ClientSession) => Promise<unknown>,
306
- ): Promise<unknown> => {
307
- const session = {
308
- withTransaction: async (transaction: () => Promise<unknown>) => {
309
- const previous = transactionQueue;
310
- let release!: () => void;
311
- transactionQueue = new Promise<void>((resolve) => {
312
- release = resolve;
313
- });
314
- await previous;
315
- try {
316
- const staged = structuredClone(tables);
317
- activeTables = staged;
318
- const result = await transaction();
319
- tables.bundle_patches = staged.bundle_patches;
320
- tables.bundles = staged.bundles;
321
- tables.bundle_events = staged.bundle_events;
322
- tables.bundle_installations = staged.bundle_installations;
323
- tables.channels = staged.channels;
324
- tables.api_keys = staged.api_keys;
325
- tables.releases = staged.releases;
326
- tables.release_catalogs = staged.release_catalogs;
327
- return result;
328
- } finally {
329
- activeTables = tables;
330
- release();
331
- }
332
- },
333
- } as unknown as ClientSession;
334
- return callback(session);
335
- },
336
- });
337
- return {
338
- client,
339
- close: () => client.close(),
340
- reset: (): void => {
341
- hooks.failNextBundleTombstone = false;
342
- hooks.failNextInstallationWrite = false;
343
- hooks.operationCount = 0;
344
- transactionQueue = Promise.resolve();
345
- tables.bundle_patches = [];
346
- tables.bundles = [];
347
- tables.bundle_events = [];
348
- tables.bundle_installations = [];
349
- tables.channels = [];
350
- tables.api_keys = [];
351
- tables.releases = [];
352
- tables.release_catalogs = [];
353
- },
354
- getOperationCount: (): number => hooks.operationCount,
355
- setBeforeBundlePatchInsert: (
356
- hook: MongoTestHooks["beforeBundlePatchInsert"],
357
- ): void => {
358
- hooks.beforeBundlePatchInsert = hook;
359
- },
360
- failNextBundleTombstone: (): void => {
361
- hooks.failNextBundleTombstone = true;
362
- },
363
- failNextInstallationWrite: (): void => {
364
- hooks.failNextInstallationWrite = true;
365
- },
366
- setBundleField: (id: string, field: string, value: unknown): void => {
367
- const row = tables.bundles.find(
368
- (candidate) => "id" in candidate && candidate.id === id,
369
- );
370
- if (row === undefined) throw new MongoTestConstraintError("missing row");
371
- Reflect.set(row, field, value);
372
- },
373
- };
374
- };
375
-
376
- export type MongoTestBundleRow = BundleRow;
377
- export type MongoTestBundlePatchRow = BundlePatchRow;