@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,413 +0,0 @@
1
- import type {
2
- BundleEventRow,
3
- BundlePatchRow,
4
- BundleRow,
5
- ChannelRow,
6
- ApiKeyRow,
7
- InsightsInstallationRow,
8
- ReleaseCatalogRow,
9
- ReleaseRow,
10
- } from "@hot-updater/plugin-core";
11
- import { compareInsightsText } from "@hot-updater/plugin-core";
12
-
13
- type Row =
14
- | BundleEventRow
15
- | BundlePatchRow
16
- | BundleRow
17
- | ChannelRow
18
- | ApiKeyRow
19
- | InsightsInstallationRow
20
- | ReleaseRow
21
- | ReleaseCatalogRow;
22
- type Table = Row[];
23
- type Tables = {
24
- bundle_patches: Table;
25
- bundles: Table;
26
- bundle_events: Table;
27
- bundle_installations: Table;
28
- channels: Table;
29
- api_keys: Table;
30
- releases: Table;
31
- release_catalogs: Table;
32
- };
33
-
34
- type Hooks = {
35
- beforeNextBundleUpdateMany: ((tables: Tables) => void) | undefined;
36
- failNextBundleDelete: boolean;
37
- transactionOptions: ({ readonly isolationLevel?: string } | undefined)[];
38
- };
39
-
40
- type QueryArgs = {
41
- readonly orderBy?: unknown;
42
- readonly skip?: number;
43
- readonly take?: number;
44
- readonly where?: unknown;
45
- };
46
- type CreateArgs = QueryArgs & { readonly data: Row };
47
- type UpdateArgs = QueryArgs & {
48
- readonly data: Readonly<Record<string, unknown>>;
49
- };
50
- type UpsertArgs = QueryArgs & {
51
- readonly create: Row;
52
- readonly update: Partial<Row>;
53
- };
54
-
55
- class PrismaTestConstraintError extends Error {
56
- readonly name = "PrismaTestConstraintError";
57
- }
58
-
59
- const isRecord = (value: unknown): value is Record<string, unknown> =>
60
- typeof value === "object" && value !== null;
61
-
62
- const compare = (left: unknown, right: unknown): number => {
63
- if (typeof left === "number" && typeof right === "number") {
64
- return left - right;
65
- }
66
- if (typeof left === "string" && typeof right === "string") {
67
- return compareInsightsText(left, right);
68
- }
69
- return 0;
70
- };
71
-
72
- const normalize = (value: unknown, insensitive: boolean): unknown =>
73
- insensitive && typeof value === "string" ? value.toLocaleLowerCase() : value;
74
-
75
- const readField = (row: Row, field: string): unknown =>
76
- Object.entries(row).find(([key]) => key === field)?.[1];
77
-
78
- const rowKey = (model: keyof Tables, row: Row): string =>
79
- model === "bundle_installations" && "install_id" in row
80
- ? row.install_id
81
- : "id" in row
82
- ? row.id
83
- : row.scope_key;
84
-
85
- const matchesCondition = (current: unknown, condition: unknown): boolean => {
86
- if (!isRecord(condition)) return Object.is(current, condition);
87
- const insensitive = condition["mode"] === "insensitive";
88
- const value = normalize(current, insensitive);
89
- if ("equals" in condition) {
90
- return Object.is(value, normalize(condition["equals"], insensitive));
91
- }
92
- if ("not" in condition) {
93
- return (
94
- value !== null &&
95
- value !== undefined &&
96
- !Object.is(value, normalize(condition["not"], insensitive))
97
- );
98
- }
99
- if (Array.isArray(condition["in"])) {
100
- return condition["in"].some((item) => Object.is(value, item));
101
- }
102
- if (Array.isArray(condition["notIn"])) {
103
- return (
104
- condition["notIn"].length === 0 ||
105
- (value !== null &&
106
- value !== undefined &&
107
- condition["notIn"].every((item) => !Object.is(value, item)))
108
- );
109
- }
110
- if (value === null || value === undefined) return false;
111
- if ("gt" in condition) return compare(value, condition["gt"]) > 0;
112
- if ("gte" in condition) return compare(value, condition["gte"]) >= 0;
113
- if ("lt" in condition) return compare(value, condition["lt"]) < 0;
114
- if ("lte" in condition) return compare(value, condition["lte"]) <= 0;
115
- if (typeof value !== "string") return false;
116
- if (typeof condition["contains"] === "string") {
117
- const target = normalize(condition["contains"], insensitive);
118
- return typeof target === "string" && value.includes(target);
119
- }
120
- if (typeof condition["startsWith"] === "string") {
121
- const target = normalize(condition["startsWith"], insensitive);
122
- return typeof target === "string" && value.startsWith(target);
123
- }
124
- if (typeof condition["endsWith"] === "string") {
125
- const target = normalize(condition["endsWith"], insensitive);
126
- return typeof target === "string" && value.endsWith(target);
127
- }
128
- return false;
129
- };
130
-
131
- const matchesWhere = (row: Row, where: unknown): boolean => {
132
- if (!isRecord(where)) return true;
133
- const conjunction = where["AND"];
134
- if (Array.isArray(conjunction)) {
135
- return conjunction.every((item) => matchesWhere(row, item));
136
- }
137
- const disjunction = where["OR"];
138
- if (Array.isArray(disjunction)) {
139
- return disjunction.some((item) => matchesWhere(row, item));
140
- }
141
- return Object.entries(where).every(([field, condition]) =>
142
- matchesCondition(readField(row, field), condition),
143
- );
144
- };
145
-
146
- const sortRows = (rows: Row[], orderBy: unknown): Row[] => {
147
- const clauses = Array.isArray(orderBy)
148
- ? orderBy.filter(isRecord)
149
- : isRecord(orderBy)
150
- ? [orderBy]
151
- : [];
152
- if (clauses.length === 0) return rows;
153
- return rows.toSorted((left, right) => {
154
- for (const clause of clauses) {
155
- const entry = Object.entries(clause)[0];
156
- if (entry === undefined) continue;
157
- const [field, direction] = entry;
158
- const result = compare(readField(left, field), readField(right, field));
159
- if (result !== 0) {
160
- return direction === "desc" ? -result : result;
161
- }
162
- }
163
- return 0;
164
- });
165
- };
166
-
167
- const assertReferences = (
168
- tables: Tables,
169
- model: keyof Tables,
170
- row: Row,
171
- ): void => {
172
- if (model === "bundle_patches" && "base_bundle_id" in row) {
173
- const bundleIds = new Set(
174
- tables.bundles.flatMap((bundle) => ("id" in bundle ? [bundle.id] : [])),
175
- );
176
- if (!bundleIds.has(row.bundle_id) || !bundleIds.has(row.base_bundle_id)) {
177
- throw new PrismaTestConstraintError("missing patch reference");
178
- }
179
- }
180
- if (model === "releases" && "kind" in row) {
181
- if (
182
- !tables.channels.some(
183
- (channel) => "id" in channel && channel.id === row.channel_id,
184
- ) ||
185
- (row.bundle_id !== null &&
186
- !tables.bundles.some(
187
- (bundle) =>
188
- "file_hash" in bundle &&
189
- bundle.id === row.bundle_id &&
190
- bundle.platform === row.platform,
191
- ))
192
- ) {
193
- throw new PrismaTestConstraintError("missing Release reference");
194
- }
195
- }
196
- };
197
-
198
- const createDelegate = (tables: Tables, model: keyof Tables, hooks: Hooks) => ({
199
- count: async (args?: QueryArgs): Promise<number> =>
200
- tables[model].filter((row) => matchesWhere(row, args?.where)).length,
201
- create: async ({ data }: CreateArgs): Promise<Row> => {
202
- if (
203
- tables[model].some((row) => rowKey(model, row) === rowKey(model, data))
204
- ) {
205
- throw new PrismaTestConstraintError("duplicate id");
206
- }
207
- if (
208
- model === "channels" &&
209
- "name" in data &&
210
- tables.channels.some((row) => "name" in row && row.name === data.name)
211
- ) {
212
- throw new PrismaTestConstraintError("duplicate channel name");
213
- }
214
- if (
215
- model === "api_keys" &&
216
- "hash" in data &&
217
- tables.api_keys.some((row) => "hash" in row && row.hash === data.hash)
218
- ) {
219
- throw new PrismaTestConstraintError("duplicate hash");
220
- }
221
- assertReferences(tables, model, data);
222
- tables[model].push(structuredClone(data));
223
- return structuredClone(data);
224
- },
225
- deleteMany: async (args?: QueryArgs): Promise<void> => {
226
- if (model === "bundles" && hooks.failNextBundleDelete) {
227
- hooks.failNextBundleDelete = false;
228
- throw new PrismaTestConstraintError("injected bundle delete failure");
229
- }
230
- const selected = tables[model].filter((row) =>
231
- matchesWhere(row, args?.where),
232
- );
233
- if (model === "bundles") {
234
- const ids = new Set(selected.map((row) => rowKey(model, row)));
235
- if (
236
- tables.releases.some(
237
- (row) =>
238
- "bundle_id" in row &&
239
- row.bundle_id !== null &&
240
- ids.has(row.bundle_id),
241
- )
242
- ) {
243
- throw new PrismaTestConstraintError("referenced bundle");
244
- }
245
- tables.bundle_patches = tables.bundle_patches.filter(
246
- (row) =>
247
- !("base_bundle_id" in row) ||
248
- (!ids.has(row.bundle_id) && !ids.has(row.base_bundle_id)),
249
- );
250
- }
251
- tables[model] = tables[model].filter(
252
- (row) => !matchesWhere(row, args?.where),
253
- );
254
- },
255
- findFirst: async (args?: QueryArgs): Promise<Row | null> =>
256
- structuredClone(
257
- tables[model].find((row) => matchesWhere(row, args?.where)) ?? null,
258
- ),
259
- findMany: async (args?: QueryArgs): Promise<Row[]> => {
260
- const filtered = tables[model].filter((row) =>
261
- matchesWhere(row, args?.where),
262
- );
263
- const sorted = sortRows(filtered, args?.orderBy);
264
- const start = args?.skip ?? 0;
265
- const end = start + (args?.take ?? sorted.length);
266
- return structuredClone(sorted.slice(start, end));
267
- },
268
- update: async ({ data, where }: UpdateArgs): Promise<Row> => {
269
- const index = tables[model].findIndex((row) => matchesWhere(row, where));
270
- const current = tables[model][index];
271
- if (current === undefined) {
272
- throw new PrismaTestConstraintError("missing update row");
273
- }
274
- const updated = { ...current, ...data } as Row;
275
- assertReferences(tables, model, updated);
276
- tables[model][index] = updated;
277
- return structuredClone(updated);
278
- },
279
- updateMany: async ({
280
- data,
281
- where,
282
- }: UpdateArgs): Promise<{ count: number }> => {
283
- if (model === "bundles") {
284
- const hook = hooks.beforeNextBundleUpdateMany;
285
- hooks.beforeNextBundleUpdateMany = undefined;
286
- hook?.(tables);
287
- }
288
- let count = 0;
289
- tables[model] = tables[model].map((row) => {
290
- if (!matchesWhere(row, where)) return row;
291
- const updated = { ...row, ...data } as Row;
292
- assertReferences(tables, model, updated);
293
- count += 1;
294
- return updated;
295
- });
296
- return { count };
297
- },
298
- upsert: async ({ create, update, where }: UpsertArgs): Promise<Row> => {
299
- const index = tables[model].findIndex((row) => matchesWhere(row, where));
300
- const current = tables[model][index];
301
- if (current !== undefined) {
302
- const updated = { ...current, ...update } as Row;
303
- assertReferences(tables, model, updated);
304
- tables[model][index] = updated;
305
- return structuredClone(updated);
306
- }
307
- if (
308
- tables[model].some((row) => rowKey(model, row) === rowKey(model, create))
309
- ) {
310
- throw new PrismaTestConstraintError("duplicate id");
311
- }
312
- assertReferences(tables, model, create);
313
- tables[model].push(structuredClone(create));
314
- return structuredClone(create);
315
- },
316
- });
317
-
318
- const createClient = (tables: Tables, hooks: Hooks) => ({
319
- bundle_events: createDelegate(tables, "bundle_events", hooks),
320
- bundle_installations: createDelegate(tables, "bundle_installations", hooks),
321
- bundle_patches: createDelegate(tables, "bundle_patches", hooks),
322
- bundles: createDelegate(tables, "bundles", hooks),
323
- channels: createDelegate(tables, "channels", hooks),
324
- api_keys: createDelegate(tables, "api_keys", hooks),
325
- releases: createDelegate(tables, "releases", hooks),
326
- release_catalogs: createDelegate(tables, "release_catalogs", hooks),
327
- });
328
-
329
- export const createPrismaTestHarness = () => {
330
- let tables: Tables = {
331
- bundle_patches: [],
332
- bundles: [],
333
- bundle_events: [],
334
- bundle_installations: [],
335
- channels: [],
336
- api_keys: [],
337
- releases: [],
338
- release_catalogs: [],
339
- };
340
- const hooks: Hooks = {
341
- beforeNextBundleUpdateMany: undefined,
342
- failNextBundleDelete: false,
343
- transactionOptions: [],
344
- };
345
- let transactionQueue = Promise.resolve();
346
- const client = {
347
- ...createClient(tables, hooks),
348
- $transaction: async <TResult>(
349
- callback: (transaction: object) => Promise<TResult>,
350
- options?: { readonly isolationLevel?: string },
351
- ): Promise<TResult> => {
352
- hooks.transactionOptions.push(options);
353
- const previous = transactionQueue;
354
- let release!: () => void;
355
- transactionQueue = new Promise<void>((resolve) => {
356
- release = resolve;
357
- });
358
- await previous;
359
- try {
360
- const transactionTables = structuredClone(tables);
361
- const result = await callback(createClient(transactionTables, hooks));
362
- tables.bundle_patches = transactionTables.bundle_patches;
363
- tables.bundles = transactionTables.bundles;
364
- tables.bundle_events = transactionTables.bundle_events;
365
- tables.bundle_installations = transactionTables.bundle_installations;
366
- tables.channels = transactionTables.channels;
367
- tables.api_keys = transactionTables.api_keys;
368
- tables.releases = transactionTables.releases;
369
- tables.release_catalogs = transactionTables.release_catalogs;
370
- return result;
371
- } finally {
372
- release();
373
- }
374
- },
375
- };
376
- return {
377
- client,
378
- clearTargetBeforeNextBundleUpdate: (
379
- id: string,
380
- field: "fingerprint_hash" | "target_app_version",
381
- ): void => {
382
- hooks.beforeNextBundleUpdateMany = (transactionTables) => {
383
- for (const currentTables of new Set([tables, transactionTables])) {
384
- const index = currentTables.bundles.findIndex(
385
- (candidate) => "id" in candidate && candidate.id === id,
386
- );
387
- const row = currentTables.bundles[index];
388
- if (row !== undefined && "target_app_version" in row) {
389
- currentTables.bundles[index] = { ...row, [field]: null };
390
- }
391
- }
392
- };
393
- },
394
- failNextBundleDelete: (): void => {
395
- hooks.failNextBundleDelete = true;
396
- },
397
- getTransactionOptions: () => structuredClone(hooks.transactionOptions),
398
- reset: (): void => {
399
- hooks.failNextBundleDelete = false;
400
- hooks.beforeNextBundleUpdateMany = undefined;
401
- hooks.transactionOptions.length = 0;
402
- transactionQueue = Promise.resolve();
403
- tables.bundle_patches = [];
404
- tables.bundles = [];
405
- tables.bundle_events = [];
406
- tables.bundle_installations = [];
407
- tables.channels = [];
408
- tables.api_keys = [];
409
- tables.releases = [];
410
- tables.release_catalogs = [];
411
- },
412
- };
413
- };
@@ -1,192 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
-
3
- import { createInMemoryDatabasePlugin } from "../../test-utils/test/inMemoryDatabasePlugin";
4
- import { registerApiKey } from "./apiKeys";
5
- import { API_KEY_HEADER_NAME, createHotUpdater } from "./index";
6
-
7
- const API_KEY = "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE";
8
- const updateUrl =
9
- "https://example.com/release-catalogs/app-version/ios/" +
10
- "cHJvZHVjdGlvbg/1.0.0";
11
-
12
- const withApiKey = (url: string, init?: RequestInit) =>
13
- new Request(url, {
14
- ...init,
15
- headers: {
16
- ...Object.fromEntries(new Headers(init?.headers)),
17
- [API_KEY_HEADER_NAME]: API_KEY,
18
- },
19
- });
20
-
21
- describe("createHotUpdater API keys", () => {
22
- it("rejects an invalid API key header name", () => {
23
- expect(() =>
24
- createHotUpdater({
25
- clientAccess: {
26
- headerName: "invalid header",
27
- type: "api-key",
28
- },
29
- database: createInMemoryDatabasePlugin(),
30
- }),
31
- ).toThrow("clientAccess.headerName must be a valid header name.");
32
- });
33
-
34
- it("protects only client OTA and Insights write routes", async () => {
35
- const database = createInMemoryDatabasePlugin();
36
- await registerApiKey({
37
- apiKeys: database.models.apiKeys,
38
- apiKey: API_KEY,
39
- name: "App",
40
- });
41
- const hotUpdater = createHotUpdater({
42
- clientAccess: { type: "api-key" },
43
- database,
44
- });
45
-
46
- expect(
47
- (await hotUpdater.handlers.client(new Request(updateUrl))).status,
48
- ).toBe(401);
49
- expect(
50
- (await hotUpdater.handlers.client(withApiKey(updateUrl))).status,
51
- ).toBe(404);
52
- expect(
53
- (
54
- await hotUpdater.handlers.client(
55
- new Request("https://example.com/version"),
56
- )
57
- ).status,
58
- ).toBe(200);
59
- expect(
60
- (
61
- await hotUpdater.handlers.admin(
62
- withApiKey("https://example.com/events"),
63
- )
64
- ).status,
65
- ).toBe(200);
66
- });
67
-
68
- it("creates, lists, and revokes API keys without exposing hashes", async () => {
69
- const hotUpdater = createHotUpdater({
70
- clientAccess: { type: "public" },
71
- database: createInMemoryDatabasePlugin(),
72
- });
73
-
74
- const created = await hotUpdater.apiKeys.create({ name: "Production" });
75
- expect(created.apiKey).toMatch(/^[A-Za-z0-9_-]{43}$/u);
76
- expect(created.record).not.toHaveProperty("hash");
77
-
78
- await expect(hotUpdater.apiKeys.list()).resolves.toEqual([created.record]);
79
- const revoked = await hotUpdater.apiKeys.revoke({
80
- id: created.record.id,
81
- });
82
- expect(revoked).toMatchObject({
83
- id: created.record.id,
84
- revoked_at_ms: expect.any(Number),
85
- });
86
- expect(revoked).not.toHaveProperty("hash");
87
- });
88
-
89
- it("authenticates before parsing Insights event bodies", async () => {
90
- const database = createInMemoryDatabasePlugin();
91
- await registerApiKey({
92
- apiKeys: database.models.apiKeys,
93
- apiKey: API_KEY,
94
- name: "App",
95
- });
96
- const hotUpdater = createHotUpdater({
97
- clientAccess: { type: "api-key" },
98
- database,
99
- });
100
- const invalidBody = {
101
- method: "POST",
102
- headers: { "content-type": "application/json" },
103
- body: "not-json",
104
- } satisfies RequestInit;
105
-
106
- expect(
107
- (
108
- await hotUpdater.handlers.client(
109
- new Request("https://example.com/events", invalidBody),
110
- )
111
- ).status,
112
- ).toBe(401);
113
- expect(
114
- (
115
- await hotUpdater.handlers.client(
116
- withApiKey("https://example.com/events", invalidBody),
117
- )
118
- ).status,
119
- ).toBe(400);
120
- });
121
-
122
- it("returns 503 when credential storage is unavailable", async () => {
123
- const database = createInMemoryDatabasePlugin();
124
- vi.spyOn(database.models.apiKeys, "findByHash").mockRejectedValue(
125
- new Error("database offline"),
126
- );
127
- const hotUpdater = createHotUpdater({
128
- clientAccess: { type: "api-key" },
129
- database,
130
- });
131
-
132
- const response = await hotUpdater.handlers.client(withApiKey(updateUrl));
133
-
134
- expect(response.status).toBe(503);
135
- await expect(response.json()).resolves.toEqual({
136
- error: "Service unavailable",
137
- });
138
- });
139
-
140
- it("keeps client routes public with the public access policy", async () => {
141
- const hotUpdater = createHotUpdater({
142
- clientAccess: { type: "public" },
143
- database: createInMemoryDatabasePlugin(),
144
- });
145
-
146
- expect(
147
- (await hotUpdater.handlers.client(new Request(updateUrl))).status,
148
- ).toBe(404);
149
- });
150
-
151
- it("reads API keys only from the configured header", async () => {
152
- const database = createInMemoryDatabasePlugin();
153
- await registerApiKey({
154
- apiKeys: database.models.apiKeys,
155
- apiKey: API_KEY,
156
- name: "App",
157
- });
158
- const hotUpdater = createHotUpdater({
159
- clientAccess: {
160
- headerName: "X-Hot-Updater-Key",
161
- type: "api-key",
162
- },
163
- database,
164
- });
165
- const invalidBody = {
166
- method: "POST",
167
- headers: { "content-type": "application/json" },
168
- body: "not-json",
169
- } satisfies RequestInit;
170
-
171
- expect(
172
- (
173
- await hotUpdater.handlers.client(
174
- withApiKey("https://example.com/events", invalidBody),
175
- )
176
- ).status,
177
- ).toBe(401);
178
- expect(
179
- (
180
- await hotUpdater.handlers.client(
181
- new Request("https://example.com/events", {
182
- ...invalidBody,
183
- headers: {
184
- ...invalidBody.headers,
185
- "x-hot-updater-key": API_KEY,
186
- },
187
- }),
188
- )
189
- ).status,
190
- ).toBe(400);
191
- });
192
- });