@hot-updater/server 0.36.7 → 1.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/databaseConstraintErrors.cjs +23 -0
- package/dist/adapters/databaseConstraintErrors.mjs +21 -0
- package/dist/adapters/databasePluginUtils.cjs +117 -0
- package/dist/adapters/databasePluginUtils.mjs +106 -0
- package/dist/adapters/drizzle.cjs +53 -176
- package/dist/adapters/drizzle.d.cts +6 -4
- package/dist/adapters/drizzle.d.mts +6 -4
- package/dist/adapters/drizzle.mjs +54 -177
- package/dist/adapters/drizzleCrud.cjs +250 -0
- package/dist/adapters/drizzleCrud.mjs +249 -0
- package/dist/adapters/drizzleLazyDB.cjs +84 -36
- package/dist/adapters/drizzleLazyDB.mjs +84 -36
- package/dist/adapters/drizzleQuery.cjs +68 -0
- package/dist/adapters/drizzleQuery.mjs +67 -0
- package/dist/adapters/kysely.cjs +18 -138
- package/dist/adapters/kysely.d.cts +3 -12
- package/dist/adapters/kysely.d.mts +3 -12
- package/dist/adapters/kysely.mjs +19 -139
- package/dist/adapters/kyselyCrud.cjs +254 -0
- package/dist/adapters/kyselyCrud.mjs +253 -0
- package/dist/adapters/kyselyQuery.cjs +56 -0
- package/dist/adapters/kyselyQuery.mjs +55 -0
- package/dist/adapters/mongodb.cjs +21 -174
- package/dist/adapters/mongodb.d.cts +3 -2
- package/dist/adapters/mongodb.d.mts +3 -2
- package/dist/adapters/mongodb.mjs +21 -174
- package/dist/adapters/mongodbCollections.cjs +36 -0
- package/dist/adapters/mongodbCollections.mjs +30 -0
- package/dist/adapters/mongodbQuery.cjs +77 -0
- package/dist/adapters/mongodbQuery.mjs +70 -0
- package/dist/adapters/mongodbReads.cjs +148 -0
- package/dist/adapters/mongodbReads.mjs +147 -0
- package/dist/adapters/mongodbWrites.cjs +134 -0
- package/dist/adapters/mongodbWrites.mjs +133 -0
- package/dist/adapters/prisma.cjs +205 -180
- package/dist/adapters/prisma.d.cts +3 -3
- package/dist/adapters/prisma.d.mts +3 -3
- package/dist/adapters/prisma.mjs +206 -181
- package/dist/adapters/prismaQuery.cjs +58 -0
- package/dist/adapters/prismaQuery.mjs +56 -0
- package/dist/adapters/prismaRows.cjs +198 -0
- package/dist/adapters/prismaRows.mjs +188 -0
- package/dist/apiKeys.cjs +139 -0
- package/dist/apiKeys.d.cts +50 -0
- package/dist/apiKeys.d.mts +50 -0
- package/dist/apiKeys.mjs +133 -0
- package/dist/createHotUpdaterCore.cjs +48 -26
- package/dist/createHotUpdaterCore.d.cts +49 -17
- package/dist/createHotUpdaterCore.d.mts +49 -17
- package/dist/createHotUpdaterCore.mjs +51 -29
- package/dist/db/createBundleDiff.cjs +45 -68
- package/dist/db/createBundleDiff.d.cts +3 -3
- package/dist/db/createBundleDiff.d.mts +3 -3
- package/dist/db/createBundleDiff.mjs +47 -68
- package/dist/db/databasePluginCore.cjs +138 -0
- package/dist/db/databasePluginCore.mjs +137 -0
- package/dist/db/fixedMigrator.cjs +36 -93
- package/dist/db/fixedMigrator.mjs +36 -93
- package/dist/db/fixedMigratorMongo.cjs +319 -0
- package/dist/db/fixedMigratorMongo.mjs +319 -0
- package/dist/db/fixedMigratorShared.cjs +23 -0
- package/dist/db/fixedMigratorShared.mjs +18 -0
- package/dist/db/mongoMigrationExecution.cjs +11 -0
- package/dist/db/mongoMigrationExecution.mjs +11 -0
- package/dist/db/releaseCatalog.cjs +66 -0
- package/dist/db/releaseCatalog.d.cts +17 -0
- package/dist/db/releaseCatalog.d.mts +17 -0
- package/dist/db/releaseCatalog.mjs +64 -0
- package/dist/db/schema/mongodb.cjs +14 -6
- package/dist/db/schema/mongodb.mjs +14 -6
- package/dist/db/schema/registry.cjs +0 -2
- package/dist/db/schema/registry.mjs +1 -2
- package/dist/db/schema/sql.cjs +31 -14
- package/dist/db/schema/sql.mjs +32 -13
- package/dist/db/schema/sqlOperations.cjs +4 -4
- package/dist/db/schema/sqlOperations.mjs +5 -5
- package/dist/db/schemaGenerators.cjs +52 -18
- package/dist/db/schemaGenerators.mjs +52 -18
- package/dist/db/schemaReadiness.cjs +4 -3
- package/dist/db/schemaReadiness.mjs +4 -3
- package/dist/db/sqlMigrationExecution.cjs +67 -0
- package/dist/db/sqlMigrationExecution.mjs +66 -0
- package/dist/db/types.cjs +2 -6
- package/dist/db/types.d.cts +60 -16
- package/dist/db/types.d.mts +60 -16
- package/dist/db/types.mjs +3 -6
- package/dist/db/updateArtifacts.cjs +114 -60
- package/dist/db/updateArtifacts.mjs +117 -62
- package/dist/handler.cjs +108 -282
- package/dist/handler.d.cts +3 -46
- package/dist/handler.d.mts +3 -46
- package/dist/handler.mjs +107 -281
- package/dist/handlerBundleRoutes.cjs +155 -0
- package/dist/handlerBundleRoutes.mjs +155 -0
- package/dist/handlerErrors.cjs +9 -0
- package/dist/handlerErrors.mjs +9 -0
- package/dist/handlerParameters.cjs +39 -0
- package/dist/handlerParameters.mjs +34 -0
- package/dist/handlerReleaseCatalogRoutes.cjs +111 -0
- package/dist/handlerReleaseCatalogRoutes.mjs +110 -0
- package/dist/handlerReleaseManagementRoutes.cjs +125 -0
- package/dist/handlerReleaseManagementRoutes.mjs +124 -0
- package/dist/handlerTypes.d.cts +63 -0
- package/dist/handlerTypes.d.mts +63 -0
- package/dist/handlerVersionRoutes.cjs +10 -0
- package/dist/handlerVersionRoutes.d.cts +4 -0
- package/dist/handlerVersionRoutes.d.mts +4 -0
- package/dist/handlerVersionRoutes.mjs +9 -0
- package/dist/index.cjs +10 -1
- package/dist/index.d.cts +9 -3
- package/dist/index.d.mts +9 -3
- package/dist/index.mjs +5 -2
- package/dist/insights/bounded/activeOverview.cjs +87 -0
- package/dist/insights/bounded/activeOverview.mjs +85 -0
- package/dist/insights/bounded/installationSearch.cjs +42 -0
- package/dist/insights/bounded/installationSearch.mjs +42 -0
- package/dist/insights/bounded/persistence.cjs +39 -0
- package/dist/insights/bounded/persistence.mjs +38 -0
- package/dist/insights/bounded/provider.cjs +170 -0
- package/dist/insights/bounded/provider.d.cts +7 -0
- package/dist/insights/bounded/provider.d.mts +7 -0
- package/dist/insights/bounded/provider.mjs +170 -0
- package/dist/insights/bounded/scan.cjs +144 -0
- package/dist/insights/bounded/scan.mjs +138 -0
- package/dist/insights/domain.d.cts +121 -0
- package/dist/insights/domain.d.mts +121 -0
- package/dist/insights/errors.cjs +22 -0
- package/dist/insights/errors.mjs +20 -0
- package/dist/insights/eventInput.cjs +111 -0
- package/dist/insights/eventInput.mjs +111 -0
- package/dist/insights/persistence.d.cts +6 -0
- package/dist/insights/persistence.d.mts +6 -0
- package/dist/insights/queryInput.cjs +58 -0
- package/dist/insights/queryInput.mjs +55 -0
- package/dist/insights/routes.cjs +63 -0
- package/dist/insights/routes.mjs +61 -0
- package/dist/insights/types.d.cts +17 -0
- package/dist/insights/types.d.mts +17 -0
- package/dist/node.cjs +19 -12
- package/dist/node.d.cts +15 -10
- package/dist/node.d.mts +15 -10
- package/dist/node.mjs +19 -13
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/schema/dsl.cjs +19 -2
- package/dist/schema/dsl.mjs +17 -3
- package/dist/schema/index.cjs +1 -8
- package/dist/schema/index.mjs +2 -8
- package/dist/schema/types.cjs +3 -1
- package/dist/schema/types.mjs +3 -2
- package/dist/schema/v1_0_0.cjs +364 -0
- package/dist/schema/v1_0_0.mjs +357 -0
- package/dist/storageAccess.cjs +69 -25
- package/dist/storageAccess.mjs +68 -25
- package/dist/types/index.d.cts +4 -3
- package/dist/types/index.d.mts +5 -4
- package/package.json +8 -7
- package/src/adapters/databaseConstraintErrors.ts +25 -0
- package/src/adapters/databasePluginTestDatabase.ts +106 -0
- package/src/adapters/databasePluginUtils.spec.ts +29 -0
- package/src/adapters/databasePluginUtils.ts +214 -0
- package/src/adapters/drizzle.spec.ts +208 -47
- package/src/adapters/drizzle.ts +106 -293
- package/src/adapters/drizzleCrud.ts +419 -0
- package/src/adapters/drizzleLazyDB.ts +206 -100
- package/src/adapters/drizzleQuery.ts +164 -0
- package/src/adapters/kysely.spec.ts +126 -88
- package/src/adapters/kysely.ts +76 -334
- package/src/adapters/kyselyCrud.ts +588 -0
- package/src/adapters/kyselyQuery.ts +128 -0
- package/src/adapters/mongodb.spec.ts +141 -0
- package/src/adapters/mongodb.ts +60 -288
- package/src/adapters/mongodbCollections.ts +62 -0
- package/src/adapters/mongodbQuery.ts +226 -0
- package/src/adapters/mongodbReads.ts +311 -0
- package/src/adapters/mongodbRows.spec.ts +64 -0
- package/src/adapters/mongodbRows.ts +118 -0
- package/src/adapters/mongodbTestClient.ts +342 -0
- package/src/adapters/mongodbTestFilter.ts +166 -0
- package/src/adapters/mongodbWrites.ts +260 -0
- package/src/adapters/prisma.spec.ts +196 -0
- package/src/adapters/prisma.ts +417 -292
- package/src/adapters/prismaQuery.ts +129 -0
- package/src/adapters/prismaRows.spec.ts +98 -0
- package/src/adapters/prismaRows.ts +301 -0
- package/src/adapters/prismaTestClient.ts +396 -0
- package/src/apiKeys.integration.spec.ts +192 -0
- package/src/apiKeys.spec.ts +162 -0
- package/src/apiKeys.ts +220 -0
- package/src/createHotUpdaterCore.ts +164 -100
- package/src/db/bundleRows.spec.ts +164 -0
- package/src/db/bundleRows.ts +10 -140
- package/src/db/createBundleDiff.spec.ts +172 -127
- package/src/db/createBundleDiff.ts +91 -110
- package/src/db/databasePluginCore.spec.ts +62 -0
- package/src/db/databasePluginCore.testFixtures.ts +76 -0
- package/src/db/databasePluginCore.ts +185 -0
- package/src/db/databasePluginUpdateCheck.bench.ts +107 -0
- package/src/db/fixedMigrator.spec.ts +184 -42
- package/src/db/fixedMigrator.ts +64 -205
- package/src/db/fixedMigratorMongo.spec.ts +192 -0
- package/src/db/fixedMigratorMongo.ts +425 -0
- package/src/db/fixedMigratorShared.ts +36 -0
- package/src/db/hotUpdaterSchema.ts +0 -1
- package/src/db/index.spec.ts +333 -1025
- package/src/db/mongoMigrationExecution.ts +23 -0
- package/src/db/node-sqlite.d.ts +25 -0
- package/src/db/releaseCatalog.ts +148 -0
- package/src/db/schema/mongodb.ts +20 -6
- package/src/db/schema/sql.ts +52 -11
- package/src/db/schema/sqlOperations.ts +5 -4
- package/src/db/schemaGenerators.ts +107 -23
- package/src/db/schemaReadiness.ts +2 -1
- package/src/db/sqlMigrationExecution.spec.ts +65 -0
- package/src/db/sqlMigrationExecution.ts +108 -0
- package/src/db/types.spec.ts +55 -0
- package/src/db/types.ts +129 -65
- package/src/db/updateArtifacts.spec.ts +359 -0
- package/src/db/updateArtifacts.ts +290 -126
- package/src/handler-standalone.integration.spec.ts +401 -364
- package/src/handler.spec.ts +137 -582
- package/src/handler.testFixtures.ts +30 -0
- package/src/handler.ts +218 -616
- package/src/handlerAuth.integration.spec.ts +33 -0
- package/src/handlerBundleRoutes.ts +259 -0
- package/src/handlerErrors.ts +7 -0
- package/src/handlerManagement.spec.ts +262 -0
- package/src/handlerParameters.ts +105 -0
- package/src/handlerReleaseCatalogRoutes.ts +154 -0
- package/src/handlerReleaseManagementRoutes.ts +213 -0
- package/src/handlerTypes.ts +96 -0
- package/src/handlerVersionRoutes.ts +12 -0
- package/src/index.ts +22 -2
- package/src/insights/bounded/activeOverview.spec.ts +189 -0
- package/src/insights/bounded/activeOverview.ts +148 -0
- package/src/insights/bounded/installationSearch.spec.ts +130 -0
- package/src/insights/bounded/installationSearch.ts +69 -0
- package/src/insights/bounded/persistence.ts +43 -0
- package/src/insights/bounded/provider.spec.ts +219 -0
- package/src/insights/bounded/provider.ts +229 -0
- package/src/insights/bounded/scan.ts +235 -0
- package/src/insights/domain.ts +139 -0
- package/src/insights/errors.ts +18 -0
- package/src/insights/eventInput.ts +168 -0
- package/src/insights/insights.integration.spec.ts +144 -0
- package/src/insights/persistence.ts +10 -0
- package/src/insights/queryInput.ts +102 -0
- package/src/insights/routes.ts +123 -0
- package/src/insights/types.ts +44 -0
- package/src/node.spec.ts +79 -10
- package/src/node.ts +20 -17
- package/src/releaseCatalogRoutes.integration.spec.ts +238 -0
- package/src/runtime.spec.ts +126 -959
- package/src/runtime.testFixtures.ts +71 -0
- package/src/schema/dsl-all-versions.spec.ts +26 -17
- package/src/schema/dsl.ts +41 -2
- package/src/schema/index.ts +3 -9
- package/src/schema/types.ts +9 -3
- package/src/schema/v1_0_0.spec.ts +146 -0
- package/src/schema/v1_0_0.ts +465 -0
- package/src/storageAccess.spec.ts +127 -32
- package/src/storageAccess.ts +106 -55
- package/src/types/index.ts +2 -1
- package/dist/db/bundleRows.cjs +0 -82
- package/dist/db/bundleRows.d.cts +0 -33
- package/dist/db/bundleRows.d.mts +0 -33
- package/dist/db/bundleRows.mjs +0 -79
- package/dist/db/pluginCore.cjs +0 -242
- package/dist/db/pluginCore.mjs +0 -241
- package/dist/db/schema/sqlMigrations.cjs +0 -74
- package/dist/db/schema/sqlMigrations.mjs +0 -73
- package/dist/db/schemaEnhancements.cjs +0 -20
- package/dist/db/schemaEnhancements.mjs +0 -19
- package/dist/route.cjs +0 -7
- package/dist/route.mjs +0 -7
- package/dist/schema/v0_21_0.cjs +0 -38
- package/dist/schema/v0_21_0.mjs +0 -37
- package/dist/schema/v0_29_0.cjs +0 -45
- package/dist/schema/v0_29_0.mjs +0 -44
- package/dist/schema/v0_31_0.cjs +0 -82
- package/dist/schema/v0_31_0.mjs +0 -80
- package/src/db/pluginCore.spec.ts +0 -1095
- package/src/db/pluginCore.ts +0 -475
- package/src/db/pluginUpdateCheck.bench.ts +0 -254
- package/src/db/schema/sqlMigrations.ts +0 -219
- package/src/db/schemaEnhancements.ts +0 -55
- package/src/route.ts +0 -7
- package/src/schema/v0_21_0.ts +0 -52
- package/src/schema/v0_29_0.ts +0 -62
- package/src/schema/v0_31_0.spec.ts +0 -73
- package/src/schema/v0_31_0.ts +0 -120
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BundleEventRow,
|
|
3
|
+
BundlePatchRow,
|
|
4
|
+
BundleRow,
|
|
5
|
+
ChannelRow,
|
|
6
|
+
ApiKeyRow,
|
|
7
|
+
ReleaseCatalogRow,
|
|
8
|
+
ReleaseRow,
|
|
9
|
+
} from "@hot-updater/plugin-core";
|
|
10
|
+
|
|
11
|
+
type Row =
|
|
12
|
+
| BundleEventRow
|
|
13
|
+
| BundlePatchRow
|
|
14
|
+
| BundleRow
|
|
15
|
+
| ChannelRow
|
|
16
|
+
| ApiKeyRow
|
|
17
|
+
| ReleaseRow
|
|
18
|
+
| ReleaseCatalogRow;
|
|
19
|
+
type Table = Row[];
|
|
20
|
+
type Tables = {
|
|
21
|
+
bundle_patches: Table;
|
|
22
|
+
bundles: Table;
|
|
23
|
+
bundle_events: Table;
|
|
24
|
+
channels: Table;
|
|
25
|
+
api_keys: Table;
|
|
26
|
+
releases: Table;
|
|
27
|
+
release_catalogs: Table;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
type Hooks = {
|
|
31
|
+
beforeNextBundleUpdateMany: ((tables: Tables) => void) | undefined;
|
|
32
|
+
failNextBundleDelete: boolean;
|
|
33
|
+
transactionOptions: ({ readonly isolationLevel?: string } | undefined)[];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
type QueryArgs = {
|
|
37
|
+
readonly orderBy?: unknown;
|
|
38
|
+
readonly skip?: number;
|
|
39
|
+
readonly take?: number;
|
|
40
|
+
readonly where?: unknown;
|
|
41
|
+
};
|
|
42
|
+
type CreateArgs = QueryArgs & { readonly data: Row };
|
|
43
|
+
type UpdateArgs = QueryArgs & {
|
|
44
|
+
readonly data: Readonly<Record<string, unknown>>;
|
|
45
|
+
};
|
|
46
|
+
type UpsertArgs = QueryArgs & {
|
|
47
|
+
readonly create: Row;
|
|
48
|
+
readonly update: Partial<Row>;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
class PrismaTestConstraintError extends Error {
|
|
52
|
+
readonly name = "PrismaTestConstraintError";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
56
|
+
typeof value === "object" && value !== null;
|
|
57
|
+
|
|
58
|
+
const compare = (left: unknown, right: unknown): number => {
|
|
59
|
+
if (typeof left === "number" && typeof right === "number") {
|
|
60
|
+
return left - right;
|
|
61
|
+
}
|
|
62
|
+
if (typeof left === "string" && typeof right === "string") {
|
|
63
|
+
return left.localeCompare(right);
|
|
64
|
+
}
|
|
65
|
+
return 0;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const normalize = (value: unknown, insensitive: boolean): unknown =>
|
|
69
|
+
insensitive && typeof value === "string" ? value.toLocaleLowerCase() : value;
|
|
70
|
+
|
|
71
|
+
const readField = (row: Row, field: string): unknown =>
|
|
72
|
+
Object.entries(row).find(([key]) => key === field)?.[1];
|
|
73
|
+
|
|
74
|
+
const rowKey = (row: Row): string => ("id" in row ? row.id : row.scope_key);
|
|
75
|
+
|
|
76
|
+
const matchesCondition = (current: unknown, condition: unknown): boolean => {
|
|
77
|
+
if (!isRecord(condition)) return Object.is(current, condition);
|
|
78
|
+
const insensitive = condition["mode"] === "insensitive";
|
|
79
|
+
const value = normalize(current, insensitive);
|
|
80
|
+
if ("equals" in condition) {
|
|
81
|
+
return Object.is(value, normalize(condition["equals"], insensitive));
|
|
82
|
+
}
|
|
83
|
+
if ("not" in condition) {
|
|
84
|
+
return (
|
|
85
|
+
value !== null &&
|
|
86
|
+
value !== undefined &&
|
|
87
|
+
!Object.is(value, normalize(condition["not"], insensitive))
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
if (Array.isArray(condition["in"])) {
|
|
91
|
+
return condition["in"].some((item) => Object.is(value, item));
|
|
92
|
+
}
|
|
93
|
+
if (Array.isArray(condition["notIn"])) {
|
|
94
|
+
return (
|
|
95
|
+
condition["notIn"].length === 0 ||
|
|
96
|
+
(value !== null &&
|
|
97
|
+
value !== undefined &&
|
|
98
|
+
condition["notIn"].every((item) => !Object.is(value, item)))
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
if (value === null || value === undefined) return false;
|
|
102
|
+
if ("gt" in condition) return compare(value, condition["gt"]) > 0;
|
|
103
|
+
if ("gte" in condition) return compare(value, condition["gte"]) >= 0;
|
|
104
|
+
if ("lt" in condition) return compare(value, condition["lt"]) < 0;
|
|
105
|
+
if ("lte" in condition) return compare(value, condition["lte"]) <= 0;
|
|
106
|
+
if (typeof value !== "string") return false;
|
|
107
|
+
if (typeof condition["contains"] === "string") {
|
|
108
|
+
const target = normalize(condition["contains"], insensitive);
|
|
109
|
+
return typeof target === "string" && value.includes(target);
|
|
110
|
+
}
|
|
111
|
+
if (typeof condition["startsWith"] === "string") {
|
|
112
|
+
const target = normalize(condition["startsWith"], insensitive);
|
|
113
|
+
return typeof target === "string" && value.startsWith(target);
|
|
114
|
+
}
|
|
115
|
+
if (typeof condition["endsWith"] === "string") {
|
|
116
|
+
const target = normalize(condition["endsWith"], insensitive);
|
|
117
|
+
return typeof target === "string" && value.endsWith(target);
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const matchesWhere = (row: Row, where: unknown): boolean => {
|
|
123
|
+
if (!isRecord(where)) return true;
|
|
124
|
+
const conjunction = where["AND"];
|
|
125
|
+
if (Array.isArray(conjunction)) {
|
|
126
|
+
return conjunction.every((item) => matchesWhere(row, item));
|
|
127
|
+
}
|
|
128
|
+
const disjunction = where["OR"];
|
|
129
|
+
if (Array.isArray(disjunction)) {
|
|
130
|
+
return disjunction.some((item) => matchesWhere(row, item));
|
|
131
|
+
}
|
|
132
|
+
return Object.entries(where).every(([field, condition]) =>
|
|
133
|
+
matchesCondition(readField(row, field), condition),
|
|
134
|
+
);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const sortRows = (rows: Row[], orderBy: unknown): Row[] => {
|
|
138
|
+
const clauses = Array.isArray(orderBy)
|
|
139
|
+
? orderBy.filter(isRecord)
|
|
140
|
+
: isRecord(orderBy)
|
|
141
|
+
? [orderBy]
|
|
142
|
+
: [];
|
|
143
|
+
if (clauses.length === 0) return rows;
|
|
144
|
+
return rows.toSorted((left, right) => {
|
|
145
|
+
for (const clause of clauses) {
|
|
146
|
+
const entry = Object.entries(clause)[0];
|
|
147
|
+
if (entry === undefined) continue;
|
|
148
|
+
const [field, direction] = entry;
|
|
149
|
+
const result = compare(readField(left, field), readField(right, field));
|
|
150
|
+
if (result !== 0) {
|
|
151
|
+
return direction === "desc" ? -result : result;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return 0;
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const assertReferences = (
|
|
159
|
+
tables: Tables,
|
|
160
|
+
model: keyof Tables,
|
|
161
|
+
row: Row,
|
|
162
|
+
): void => {
|
|
163
|
+
if (model === "bundle_patches" && "base_bundle_id" in row) {
|
|
164
|
+
const bundleIds = new Set(
|
|
165
|
+
tables.bundles.flatMap((bundle) => ("id" in bundle ? [bundle.id] : [])),
|
|
166
|
+
);
|
|
167
|
+
if (!bundleIds.has(row.bundle_id) || !bundleIds.has(row.base_bundle_id)) {
|
|
168
|
+
throw new PrismaTestConstraintError("missing patch reference");
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (model === "releases" && "kind" in row) {
|
|
172
|
+
if (
|
|
173
|
+
!tables.channels.some(
|
|
174
|
+
(channel) => "id" in channel && channel.id === row.channel_id,
|
|
175
|
+
) ||
|
|
176
|
+
(row.bundle_id !== null &&
|
|
177
|
+
!tables.bundles.some(
|
|
178
|
+
(bundle) =>
|
|
179
|
+
"file_hash" in bundle &&
|
|
180
|
+
bundle.id === row.bundle_id &&
|
|
181
|
+
bundle.platform === row.platform,
|
|
182
|
+
))
|
|
183
|
+
) {
|
|
184
|
+
throw new PrismaTestConstraintError("missing Release reference");
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const createDelegate = (tables: Tables, model: keyof Tables, hooks: Hooks) => ({
|
|
190
|
+
count: async (args?: QueryArgs): Promise<number> =>
|
|
191
|
+
tables[model].filter((row) => matchesWhere(row, args?.where)).length,
|
|
192
|
+
create: async ({ data }: CreateArgs): Promise<Row> => {
|
|
193
|
+
if (tables[model].some((row) => rowKey(row) === rowKey(data))) {
|
|
194
|
+
throw new PrismaTestConstraintError("duplicate id");
|
|
195
|
+
}
|
|
196
|
+
if (
|
|
197
|
+
model === "channels" &&
|
|
198
|
+
"name" in data &&
|
|
199
|
+
tables.channels.some((row) => "name" in row && row.name === data.name)
|
|
200
|
+
) {
|
|
201
|
+
throw new PrismaTestConstraintError("duplicate channel name");
|
|
202
|
+
}
|
|
203
|
+
if (
|
|
204
|
+
model === "api_keys" &&
|
|
205
|
+
"hash" in data &&
|
|
206
|
+
tables.api_keys.some((row) => "hash" in row && row.hash === data.hash)
|
|
207
|
+
) {
|
|
208
|
+
throw new PrismaTestConstraintError("duplicate hash");
|
|
209
|
+
}
|
|
210
|
+
assertReferences(tables, model, data);
|
|
211
|
+
tables[model].push(structuredClone(data));
|
|
212
|
+
return structuredClone(data);
|
|
213
|
+
},
|
|
214
|
+
deleteMany: async (args?: QueryArgs): Promise<void> => {
|
|
215
|
+
if (model === "bundles" && hooks.failNextBundleDelete) {
|
|
216
|
+
hooks.failNextBundleDelete = false;
|
|
217
|
+
throw new PrismaTestConstraintError("injected bundle delete failure");
|
|
218
|
+
}
|
|
219
|
+
const selected = tables[model].filter((row) =>
|
|
220
|
+
matchesWhere(row, args?.where),
|
|
221
|
+
);
|
|
222
|
+
if (model === "bundles") {
|
|
223
|
+
const ids = new Set(selected.map(rowKey));
|
|
224
|
+
if (
|
|
225
|
+
tables.releases.some(
|
|
226
|
+
(row) =>
|
|
227
|
+
"bundle_id" in row &&
|
|
228
|
+
row.bundle_id !== null &&
|
|
229
|
+
ids.has(row.bundle_id),
|
|
230
|
+
)
|
|
231
|
+
) {
|
|
232
|
+
throw new PrismaTestConstraintError("referenced bundle");
|
|
233
|
+
}
|
|
234
|
+
tables.bundle_patches = tables.bundle_patches.filter(
|
|
235
|
+
(row) =>
|
|
236
|
+
!("base_bundle_id" in row) ||
|
|
237
|
+
(!ids.has(row.bundle_id) && !ids.has(row.base_bundle_id)),
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
tables[model] = tables[model].filter(
|
|
241
|
+
(row) => !matchesWhere(row, args?.where),
|
|
242
|
+
);
|
|
243
|
+
},
|
|
244
|
+
findFirst: async (args?: QueryArgs): Promise<Row | null> =>
|
|
245
|
+
structuredClone(
|
|
246
|
+
tables[model].find((row) => matchesWhere(row, args?.where)) ?? null,
|
|
247
|
+
),
|
|
248
|
+
findMany: async (args?: QueryArgs): Promise<Row[]> => {
|
|
249
|
+
const filtered = tables[model].filter((row) =>
|
|
250
|
+
matchesWhere(row, args?.where),
|
|
251
|
+
);
|
|
252
|
+
const sorted = sortRows(filtered, args?.orderBy);
|
|
253
|
+
const start = args?.skip ?? 0;
|
|
254
|
+
const end = start + (args?.take ?? sorted.length);
|
|
255
|
+
return structuredClone(sorted.slice(start, end));
|
|
256
|
+
},
|
|
257
|
+
update: async ({ data, where }: UpdateArgs): Promise<Row> => {
|
|
258
|
+
const index = tables[model].findIndex((row) => matchesWhere(row, where));
|
|
259
|
+
const current = tables[model][index];
|
|
260
|
+
if (current === undefined) {
|
|
261
|
+
throw new PrismaTestConstraintError("missing update row");
|
|
262
|
+
}
|
|
263
|
+
const updated = { ...current, ...data } as Row;
|
|
264
|
+
assertReferences(tables, model, updated);
|
|
265
|
+
tables[model][index] = updated;
|
|
266
|
+
return structuredClone(updated);
|
|
267
|
+
},
|
|
268
|
+
updateMany: async ({
|
|
269
|
+
data,
|
|
270
|
+
where,
|
|
271
|
+
}: UpdateArgs): Promise<{ count: number }> => {
|
|
272
|
+
if (model === "bundles") {
|
|
273
|
+
const hook = hooks.beforeNextBundleUpdateMany;
|
|
274
|
+
hooks.beforeNextBundleUpdateMany = undefined;
|
|
275
|
+
hook?.(tables);
|
|
276
|
+
}
|
|
277
|
+
let count = 0;
|
|
278
|
+
tables[model] = tables[model].map((row) => {
|
|
279
|
+
if (!matchesWhere(row, where)) return row;
|
|
280
|
+
const updated = { ...row, ...data } as Row;
|
|
281
|
+
assertReferences(tables, model, updated);
|
|
282
|
+
count += 1;
|
|
283
|
+
return updated;
|
|
284
|
+
});
|
|
285
|
+
return { count };
|
|
286
|
+
},
|
|
287
|
+
upsert: async ({ create, update, where }: UpsertArgs): Promise<Row> => {
|
|
288
|
+
const index = tables[model].findIndex((row) => matchesWhere(row, where));
|
|
289
|
+
const current = tables[model][index];
|
|
290
|
+
if (current !== undefined) {
|
|
291
|
+
const updated = { ...current, ...update } as Row;
|
|
292
|
+
assertReferences(tables, model, updated);
|
|
293
|
+
tables[model][index] = updated;
|
|
294
|
+
return structuredClone(updated);
|
|
295
|
+
}
|
|
296
|
+
if (tables[model].some((row) => rowKey(row) === rowKey(create))) {
|
|
297
|
+
throw new PrismaTestConstraintError("duplicate id");
|
|
298
|
+
}
|
|
299
|
+
assertReferences(tables, model, create);
|
|
300
|
+
tables[model].push(structuredClone(create));
|
|
301
|
+
return structuredClone(create);
|
|
302
|
+
},
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
const createClient = (tables: Tables, hooks: Hooks) => ({
|
|
306
|
+
bundle_events: createDelegate(tables, "bundle_events", hooks),
|
|
307
|
+
bundle_patches: createDelegate(tables, "bundle_patches", hooks),
|
|
308
|
+
bundles: createDelegate(tables, "bundles", hooks),
|
|
309
|
+
channels: createDelegate(tables, "channels", hooks),
|
|
310
|
+
api_keys: createDelegate(tables, "api_keys", hooks),
|
|
311
|
+
releases: createDelegate(tables, "releases", hooks),
|
|
312
|
+
release_catalogs: createDelegate(tables, "release_catalogs", hooks),
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
export const createPrismaTestHarness = () => {
|
|
316
|
+
let tables: Tables = {
|
|
317
|
+
bundle_patches: [],
|
|
318
|
+
bundles: [],
|
|
319
|
+
bundle_events: [],
|
|
320
|
+
channels: [],
|
|
321
|
+
api_keys: [],
|
|
322
|
+
releases: [],
|
|
323
|
+
release_catalogs: [],
|
|
324
|
+
};
|
|
325
|
+
const hooks: Hooks = {
|
|
326
|
+
beforeNextBundleUpdateMany: undefined,
|
|
327
|
+
failNextBundleDelete: false,
|
|
328
|
+
transactionOptions: [],
|
|
329
|
+
};
|
|
330
|
+
let transactionQueue = Promise.resolve();
|
|
331
|
+
const client = {
|
|
332
|
+
...createClient(tables, hooks),
|
|
333
|
+
$transaction: async <TResult>(
|
|
334
|
+
callback: (transaction: object) => Promise<TResult>,
|
|
335
|
+
options?: { readonly isolationLevel?: string },
|
|
336
|
+
): Promise<TResult> => {
|
|
337
|
+
hooks.transactionOptions.push(options);
|
|
338
|
+
const previous = transactionQueue;
|
|
339
|
+
let release!: () => void;
|
|
340
|
+
transactionQueue = new Promise<void>((resolve) => {
|
|
341
|
+
release = resolve;
|
|
342
|
+
});
|
|
343
|
+
await previous;
|
|
344
|
+
try {
|
|
345
|
+
const transactionTables = structuredClone(tables);
|
|
346
|
+
const result = await callback(createClient(transactionTables, hooks));
|
|
347
|
+
tables.bundle_patches = transactionTables.bundle_patches;
|
|
348
|
+
tables.bundles = transactionTables.bundles;
|
|
349
|
+
tables.bundle_events = transactionTables.bundle_events;
|
|
350
|
+
tables.channels = transactionTables.channels;
|
|
351
|
+
tables.api_keys = transactionTables.api_keys;
|
|
352
|
+
tables.releases = transactionTables.releases;
|
|
353
|
+
tables.release_catalogs = transactionTables.release_catalogs;
|
|
354
|
+
return result;
|
|
355
|
+
} finally {
|
|
356
|
+
release();
|
|
357
|
+
}
|
|
358
|
+
},
|
|
359
|
+
};
|
|
360
|
+
return {
|
|
361
|
+
client,
|
|
362
|
+
clearTargetBeforeNextBundleUpdate: (
|
|
363
|
+
id: string,
|
|
364
|
+
field: "fingerprint_hash" | "target_app_version",
|
|
365
|
+
): void => {
|
|
366
|
+
hooks.beforeNextBundleUpdateMany = (transactionTables) => {
|
|
367
|
+
for (const currentTables of new Set([tables, transactionTables])) {
|
|
368
|
+
const index = currentTables.bundles.findIndex(
|
|
369
|
+
(candidate) => "id" in candidate && candidate.id === id,
|
|
370
|
+
);
|
|
371
|
+
const row = currentTables.bundles[index];
|
|
372
|
+
if (row !== undefined && "target_app_version" in row) {
|
|
373
|
+
currentTables.bundles[index] = { ...row, [field]: null };
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
},
|
|
378
|
+
failNextBundleDelete: (): void => {
|
|
379
|
+
hooks.failNextBundleDelete = true;
|
|
380
|
+
},
|
|
381
|
+
getTransactionOptions: () => structuredClone(hooks.transactionOptions),
|
|
382
|
+
reset: (): void => {
|
|
383
|
+
hooks.failNextBundleDelete = false;
|
|
384
|
+
hooks.beforeNextBundleUpdateMany = undefined;
|
|
385
|
+
hooks.transactionOptions.length = 0;
|
|
386
|
+
transactionQueue = Promise.resolve();
|
|
387
|
+
tables.bundle_patches = [];
|
|
388
|
+
tables.bundles = [];
|
|
389
|
+
tables.bundle_events = [];
|
|
390
|
+
tables.channels = [];
|
|
391
|
+
tables.api_keys = [];
|
|
392
|
+
tables.releases = [];
|
|
393
|
+
tables.release_catalogs = [];
|
|
394
|
+
},
|
|
395
|
+
};
|
|
396
|
+
};
|
|
@@ -0,0 +1,192 @@
|
|
|
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/installations/overview"),
|
|
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
|
+
});
|