@hot-updater/cloudflare 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.
- package/dist/index.cjs +95 -870
- package/dist/index.d.cts +724 -6
- package/dist/index.d.mts +724 -6
- package/dist/index.mjs +93 -870
- package/dist/init/index.cjs +11768 -0
- package/dist/init/index.d.cts +5 -1
- package/dist/init/index.d.mts +5 -1
- package/dist/init/index.mjs +11761 -1
- package/dist/worker/index.cjs +59 -859
- package/dist/worker/index.d.cts +725 -11
- package/dist/worker/index.d.mts +725 -11
- package/dist/worker/index.mjs +62 -863
- package/package.json +9 -13
- package/sql/bundles.sql +109 -190
- package/src/d1Database.ts +61 -40
- package/src/d1Executor.ts +84 -0
- package/src/d1Schema.ts +26 -0
- package/src/index.ts +1 -0
- package/src/plugins.ts +8 -0
- package/src/r2S3Storage.ts +4 -4
- package/src/r2Storage.ts +2 -2
- package/src/r2WorkerStorage.ts +4 -4
- package/src/worker/d1Database.ts +21 -27
- package/src/worker/index.ts +1 -0
- package/worker/dist/README.md +1 -1
- package/worker/dist/index.js +9682 -6206
- package/worker/dist/index.js.map +4 -4
- package/worker/migrations/0001_hot-updater_1.0.0.sql +109 -205
- package/worker/src/index.ts +2 -2
- package/dist/iac/index.cjs +0 -12566
- package/dist/iac/index.d.cts +0 -9
- package/dist/iac/index.d.mts +0 -9
- package/dist/iac/index.mjs +0 -12558
- package/src/d1Database.spec.ts +0 -178
- package/src/d1Implementation.spec.ts +0 -252
- package/src/d1Implementation.ts +0 -742
- package/src/d1Query.spec.ts +0 -46
- package/src/d1Query.ts +0 -89
- package/src/d1Rows.spec.ts +0 -111
- package/src/d1Rows.ts +0 -326
- package/src/d1Sql.spec.ts +0 -30
- package/src/d1Sql.ts +0 -175
- package/src/r2Storage.spec.ts +0 -346
- package/src/r2WorkerStorage.spec.ts +0 -184
- package/src/utils/createWrangler.spec.ts +0 -75
- package/src/worker/d1Database.spec.ts +0 -33
- package/worker/src/databaseMigration.integration.spec.ts +0 -130
- package/worker/src/databasePlugins.integration.spec.ts +0 -292
- package/worker/src/index.integration.spec.ts +0 -190
- package/worker/src/storagePlugin.integration.spec.ts +0 -59
- package/worker/src/testWorker.ts +0 -5
package/dist/worker/index.cjs
CHANGED
|
@@ -1,873 +1,72 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let _hot_updater_plugin_core = require("@hot-updater/plugin-core");
|
|
3
|
-
let
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
constructor(
|
|
9
|
-
super(
|
|
10
|
-
this.model = model;
|
|
3
|
+
let _hot_updater_server_plugins_api_keys = require("@hot-updater/server/plugins/api-keys");
|
|
4
|
+
let _hot_updater_server_plugins_insights = require("@hot-updater/server/plugins/insights");
|
|
5
|
+
//#region src/d1Executor.ts
|
|
6
|
+
var D1ExecutionError = class extends Error {
|
|
7
|
+
name = "D1ExecutionError";
|
|
8
|
+
constructor() {
|
|
9
|
+
super("D1 did not successfully execute every requested statement");
|
|
11
10
|
}
|
|
12
11
|
};
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
const value = row[field];
|
|
16
|
-
if (typeof value !== "string") throw new InvalidD1RowError(model);
|
|
17
|
-
return value;
|
|
18
|
-
};
|
|
19
|
-
const nullableString = (row, field, model) => {
|
|
20
|
-
const value = row[field];
|
|
21
|
-
if (value === null) return null;
|
|
22
|
-
if (typeof value !== "string") throw new InvalidD1RowError(model);
|
|
23
|
-
return value;
|
|
24
|
-
};
|
|
25
|
-
const numberValue = (row, field, model) => {
|
|
26
|
-
const value = row[field];
|
|
27
|
-
if (typeof value !== "number") throw new InvalidD1RowError(model);
|
|
28
|
-
return value;
|
|
29
|
-
};
|
|
30
|
-
const byteSizeValue = (row, field, model) => {
|
|
31
|
-
const value = row[field];
|
|
32
|
-
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) throw new InvalidD1RowError(model);
|
|
33
|
-
return value;
|
|
34
|
-
};
|
|
35
|
-
const booleanValue = (row, field, model) => {
|
|
36
|
-
const value = row[field];
|
|
37
|
-
if (value === true || value === 1) return true;
|
|
38
|
-
if (value === false || value === 0) return false;
|
|
39
|
-
throw new InvalidD1RowError(model);
|
|
40
|
-
};
|
|
41
|
-
const jsonValue = (value, model) => {
|
|
42
|
-
if (typeof value !== "string") throw new InvalidD1RowError(model);
|
|
43
|
-
try {
|
|
44
|
-
return JSON.parse(value);
|
|
45
|
-
} catch (error) {
|
|
46
|
-
if (error instanceof SyntaxError) throw new InvalidD1RowError(model);
|
|
47
|
-
throw error;
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
const metadata = (row) => {
|
|
51
|
-
const value = jsonValue(row["metadata"], "bundles");
|
|
52
|
-
if (!(0, _hot_updater_plugin_core.isDatabaseMetadataObject)(value)) throw new InvalidD1RowError("bundles");
|
|
53
|
-
return value;
|
|
54
|
-
};
|
|
55
|
-
const bundleRow = (row) => {
|
|
56
|
-
const platform = stringValue(row, "platform", "bundles");
|
|
57
|
-
if (platform !== "ios" && platform !== "android") throw new InvalidD1RowError("bundles");
|
|
58
|
-
return {
|
|
59
|
-
id: stringValue(row, "id", "bundles"),
|
|
60
|
-
platform,
|
|
61
|
-
file_hash: stringValue(row, "file_hash", "bundles"),
|
|
62
|
-
git_commit_hash: nullableString(row, "git_commit_hash", "bundles"),
|
|
63
|
-
storage_uri: stringValue(row, "storage_uri", "bundles"),
|
|
64
|
-
archive_byte_size: row["archive_byte_size"] === void 0 ? 0 : byteSizeValue(row, "archive_byte_size", "bundles"),
|
|
65
|
-
metadata: metadata(row),
|
|
66
|
-
manifest_storage_uri: nullableString(row, "manifest_storage_uri", "bundles"),
|
|
67
|
-
manifest_file_hash: nullableString(row, "manifest_file_hash", "bundles"),
|
|
68
|
-
asset_base_storage_uri: nullableString(row, "asset_base_storage_uri", "bundles")
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
const patchRow = (row) => ({
|
|
72
|
-
id: stringValue(row, "id", "bundle_patches"),
|
|
73
|
-
bundle_id: stringValue(row, "bundle_id", "bundle_patches"),
|
|
74
|
-
base_bundle_id: stringValue(row, "base_bundle_id", "bundle_patches"),
|
|
75
|
-
base_file_hash: stringValue(row, "base_file_hash", "bundle_patches"),
|
|
76
|
-
patch_file_hash: stringValue(row, "patch_file_hash", "bundle_patches"),
|
|
77
|
-
patch_storage_uri: stringValue(row, "patch_storage_uri", "bundle_patches"),
|
|
78
|
-
byte_size: byteSizeValue(row, "byte_size", "bundle_patches"),
|
|
79
|
-
order_index: numberValue(row, "order_index", "bundle_patches")
|
|
80
|
-
});
|
|
81
|
-
const channelRow = (row) => ({
|
|
82
|
-
id: stringValue(row, "id", "channels"),
|
|
83
|
-
name: stringValue(row, "name", "channels")
|
|
84
|
-
});
|
|
85
|
-
const eventRow = (row) => {
|
|
86
|
-
const type = stringValue(row, "type", "bundle_events");
|
|
87
|
-
const platform = stringValue(row, "platform", "bundle_events");
|
|
88
|
-
const fromBundleId = nullableString(row, "from_bundle_id", "bundle_events");
|
|
89
|
-
const updateStrategy = nullableString(row, "update_strategy", "bundle_events");
|
|
90
|
-
if (platform !== "ios" && platform !== "android" || !((type === "UPDATE_APPLIED" || type === "RECOVERED" || type === "RELEASE_ADOPTED") && typeof fromBundleId === "string" && (updateStrategy === "fingerprint" || updateStrategy === "appVersion") || type === "UNCHANGED" && fromBundleId === null && updateStrategy === null)) throw new InvalidD1RowError("bundle_events");
|
|
91
|
-
return {
|
|
92
|
-
id: stringValue(row, "id", "bundle_events"),
|
|
93
|
-
type,
|
|
94
|
-
install_id: stringValue(row, "install_id", "bundle_events"),
|
|
95
|
-
user_id: nullableString(row, "user_id", "bundle_events"),
|
|
96
|
-
username: nullableString(row, "username", "bundle_events"),
|
|
97
|
-
from_release_id: nullableString(row, "from_release_id", "bundle_events"),
|
|
98
|
-
from_bundle_id: fromBundleId,
|
|
99
|
-
to_release_id: nullableString(row, "to_release_id", "bundle_events"),
|
|
100
|
-
to_bundle_id: stringValue(row, "to_bundle_id", "bundle_events"),
|
|
101
|
-
platform,
|
|
102
|
-
app_version: stringValue(row, "app_version", "bundle_events"),
|
|
103
|
-
channel: stringValue(row, "channel", "bundle_events"),
|
|
104
|
-
cohort: stringValue(row, "cohort", "bundle_events"),
|
|
105
|
-
update_strategy: updateStrategy,
|
|
106
|
-
fingerprint_hash: nullableString(row, "fingerprint_hash", "bundle_events"),
|
|
107
|
-
sdk_version: nullableString(row, "sdk_version", "bundle_events"),
|
|
108
|
-
received_at_ms: numberValue(row, "received_at_ms", "bundle_events")
|
|
109
|
-
};
|
|
110
|
-
};
|
|
111
|
-
const apiKeyRow = (row) => {
|
|
112
|
-
const role = stringValue(row, "role", "api_keys");
|
|
113
|
-
if (role !== "client") throw new InvalidD1RowError("api_keys");
|
|
114
|
-
const revokedAt = row["revoked_at_ms"];
|
|
12
|
+
const toSqlResult = (result) => {
|
|
13
|
+
if (result.success === false) throw new D1ExecutionError();
|
|
115
14
|
return {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
name: stringValue(row, "name", "api_keys"),
|
|
119
|
-
prefix: stringValue(row, "prefix", "api_keys"),
|
|
120
|
-
role,
|
|
121
|
-
created_at_ms: numberValue(row, "created_at_ms", "api_keys"),
|
|
122
|
-
revoked_at_ms: revokedAt === null ? null : numberValue(row, "revoked_at_ms", "api_keys")
|
|
15
|
+
rows: result.results ?? [],
|
|
16
|
+
changes: result.meta?.changes ?? 0
|
|
123
17
|
};
|
|
124
18
|
};
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) throw new InvalidD1RowError("releases");
|
|
128
|
-
return value.filter((item) => typeof item === "string");
|
|
129
|
-
};
|
|
130
|
-
const releaseRow = (row) => {
|
|
131
|
-
const platform = stringValue(row, "platform", "releases");
|
|
132
|
-
const kind = stringValue(row, "kind", "releases");
|
|
133
|
-
const strategy = stringValue(row, "strategy", "releases");
|
|
134
|
-
const operation = stringValue(row, "operation", "releases");
|
|
135
|
-
if (platform !== "ios" && platform !== "android" || kind !== "BUNDLE" && kind !== "EMBEDDED" || strategy !== "APP_VERSION" && strategy !== "FINGERPRINT" || operation !== "DEPLOY" && operation !== "PROMOTE" && operation !== "ROLLBACK") throw new InvalidD1RowError("releases");
|
|
136
|
-
return {
|
|
137
|
-
id: stringValue(row, "id", "releases"),
|
|
138
|
-
revision: numberValue(row, "revision", "releases"),
|
|
139
|
-
scope_key: stringValue(row, "scope_key", "releases"),
|
|
140
|
-
channel_id: stringValue(row, "channel_id", "releases"),
|
|
141
|
-
platform,
|
|
142
|
-
kind,
|
|
143
|
-
bundle_id: nullableString(row, "bundle_id", "releases"),
|
|
144
|
-
strategy,
|
|
145
|
-
target_app_version: nullableString(row, "target_app_version", "releases"),
|
|
146
|
-
fingerprint_hash: nullableString(row, "fingerprint_hash", "releases"),
|
|
147
|
-
enabled: booleanValue(row, "enabled", "releases"),
|
|
148
|
-
should_force_update: booleanValue(row, "should_force_update", "releases"),
|
|
149
|
-
message: nullableString(row, "message", "releases"),
|
|
150
|
-
rollout_cohort_count: numberValue(row, "rollout_cohort_count", "releases"),
|
|
151
|
-
target_cohorts: releaseTargetCohorts(row),
|
|
152
|
-
operation,
|
|
153
|
-
source_release_id: nullableString(row, "source_release_id", "releases"),
|
|
154
|
-
created_at_ms: numberValue(row, "created_at_ms", "releases"),
|
|
155
|
-
updated_at_ms: numberValue(row, "updated_at_ms", "releases")
|
|
156
|
-
};
|
|
157
|
-
};
|
|
158
|
-
const releaseCatalogRow = (row) => {
|
|
159
|
-
const platform = stringValue(row, "platform", "release_catalogs");
|
|
160
|
-
const strategy = stringValue(row, "strategy", "release_catalogs");
|
|
161
|
-
if (platform !== "ios" && platform !== "android" || strategy !== "APP_VERSION" && strategy !== "FINGERPRINT") throw new InvalidD1RowError("release_catalogs");
|
|
162
|
-
return {
|
|
163
|
-
scope_key: stringValue(row, "scope_key", "release_catalogs"),
|
|
164
|
-
catalog_id: stringValue(row, "catalog_id", "release_catalogs"),
|
|
165
|
-
strategy,
|
|
166
|
-
channel_id: stringValue(row, "channel_id", "release_catalogs"),
|
|
167
|
-
channel_key: stringValue(row, "channel_key", "release_catalogs"),
|
|
168
|
-
platform,
|
|
169
|
-
fingerprint_hash: nullableString(row, "fingerprint_hash", "release_catalogs"),
|
|
170
|
-
generation: numberValue(row, "generation", "release_catalogs"),
|
|
171
|
-
payload: stringValue(row, "payload", "release_catalogs"),
|
|
172
|
-
catalog_hash: stringValue(row, "catalog_hash", "release_catalogs"),
|
|
173
|
-
byte_size: numberValue(row, "byte_size", "release_catalogs"),
|
|
174
|
-
is_tombstone: booleanValue(row, "is_tombstone", "release_catalogs"),
|
|
175
|
-
updated_at_ms: numberValue(row, "updated_at_ms", "release_catalogs")
|
|
176
|
-
};
|
|
177
|
-
};
|
|
178
|
-
function parseD1Row(model, value) {
|
|
179
|
-
if (!isRecord(value)) throw new InvalidD1RowError(model);
|
|
180
|
-
switch (model) {
|
|
181
|
-
case "bundles": return bundleRow(value);
|
|
182
|
-
case "bundle_patches": return patchRow(value);
|
|
183
|
-
case "channels": return channelRow(value);
|
|
184
|
-
case "bundle_events": return eventRow(value);
|
|
185
|
-
case "api_keys": return apiKeyRow(value);
|
|
186
|
-
case "releases": return releaseRow(value);
|
|
187
|
-
case "release_catalogs": return releaseCatalogRow(value);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
//#endregion
|
|
191
|
-
//#region src/d1Sql.ts
|
|
192
|
-
var InvalidD1PredicateError = class extends Error {
|
|
193
|
-
name = "InvalidD1PredicateError";
|
|
194
|
-
};
|
|
195
|
-
const encodeD1Value = (value) => {
|
|
196
|
-
const encoded = JSON.stringify(value);
|
|
197
|
-
if (encoded === void 0) throw new TypeError("D1 values must be JSON-serializable.");
|
|
198
|
-
return encoded;
|
|
199
|
-
};
|
|
200
|
-
const bind = (value) => ({
|
|
201
|
-
sql: "json_extract(?, '$')",
|
|
202
|
-
params: [encodeD1Value(value)]
|
|
203
|
-
});
|
|
204
|
-
const escapeLikePattern = (value) => value.replaceAll("\\", "\\\\").replaceAll("%", "\\%").replaceAll("_", "\\_");
|
|
205
|
-
const stringPredicate = (condition, operator) => {
|
|
206
|
-
if (typeof condition.value !== "string") throw new InvalidD1PredicateError();
|
|
207
|
-
if (condition.mode !== "insensitive") {
|
|
208
|
-
const parameter = bind(condition.value);
|
|
209
|
-
const column = `${condition.field} COLLATE BINARY`;
|
|
210
|
-
const value = `${parameter.sql} COLLATE BINARY`;
|
|
211
|
-
if (operator === "contains" || operator === "starts_with") return {
|
|
212
|
-
sql: `instr(${column}, ${value}) ${operator === "contains" ? "> 0" : "= 1"}`,
|
|
213
|
-
params: parameter.params
|
|
214
|
-
};
|
|
215
|
-
return {
|
|
216
|
-
sql: `substr(${column}, length(${condition.field}) - length(${parameter.sql}) + 1) = ${value}`,
|
|
217
|
-
params: [...parameter.params, ...parameter.params]
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
const literal = escapeLikePattern(condition.value);
|
|
221
|
-
const parameter = bind(operator === "contains" ? `%${literal}%` : operator === "starts_with" ? `${literal}%` : `%${literal}`);
|
|
222
|
-
return {
|
|
223
|
-
sql: `lower(${condition.field}) LIKE lower(${parameter.sql}) ESCAPE '\\'`,
|
|
224
|
-
params: parameter.params
|
|
225
|
-
};
|
|
226
|
-
};
|
|
227
|
-
const predicate = (condition) => {
|
|
228
|
-
const operator = condition.operator ?? "eq";
|
|
229
|
-
switch (operator) {
|
|
230
|
-
case "eq":
|
|
231
|
-
case "ne": {
|
|
232
|
-
if (condition.value === null) return {
|
|
233
|
-
sql: `${condition.field} IS ${operator === "ne" ? "NOT " : ""}NULL`,
|
|
234
|
-
params: []
|
|
235
|
-
};
|
|
236
|
-
const parameter = bind(condition.value);
|
|
237
|
-
const insensitive = "mode" in condition && condition.mode === "insensitive";
|
|
238
|
-
const column = insensitive ? `lower(${condition.field})` : condition.field;
|
|
239
|
-
const value = insensitive ? `lower(${parameter.sql})` : parameter.sql;
|
|
240
|
-
return {
|
|
241
|
-
sql: `${column} ${operator === "eq" ? "=" : "<>"} ${value}`,
|
|
242
|
-
params: parameter.params
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
case "gt":
|
|
246
|
-
case "gte":
|
|
247
|
-
case "lt":
|
|
248
|
-
case "lte": {
|
|
249
|
-
const sqlOperator = {
|
|
250
|
-
gt: ">",
|
|
251
|
-
gte: ">=",
|
|
252
|
-
lt: "<",
|
|
253
|
-
lte: "<="
|
|
254
|
-
}[operator];
|
|
255
|
-
const parameter = bind(condition.value);
|
|
256
|
-
return {
|
|
257
|
-
sql: `${condition.field} ${sqlOperator} ${parameter.sql}`,
|
|
258
|
-
params: parameter.params
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
case "in":
|
|
262
|
-
case "not_in":
|
|
263
|
-
if (!Array.isArray(condition.value)) throw new InvalidD1PredicateError();
|
|
264
|
-
if (condition.value.length === 0) return {
|
|
265
|
-
sql: operator === "not_in" ? "1 = 1" : "1 = 0",
|
|
266
|
-
params: []
|
|
267
|
-
};
|
|
268
|
-
return {
|
|
269
|
-
sql: `${condition.field} ${operator === "not_in" ? "NOT " : ""}IN (SELECT value FROM json_each(?))`,
|
|
270
|
-
params: [encodeD1Value(condition.value)]
|
|
271
|
-
};
|
|
272
|
-
case "contains":
|
|
273
|
-
case "starts_with":
|
|
274
|
-
case "ends_with": return stringPredicate(condition, operator);
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
const buildD1Where = (where) => {
|
|
278
|
-
const [first, ...rest] = where ?? [];
|
|
279
|
-
if (first === void 0) return {
|
|
280
|
-
sql: "",
|
|
281
|
-
params: []
|
|
282
|
-
};
|
|
283
|
-
const initial = predicate(first);
|
|
284
|
-
let sql = initial.sql;
|
|
285
|
-
const params = [...initial.params];
|
|
286
|
-
for (const condition of rest) {
|
|
287
|
-
const next = predicate(condition);
|
|
288
|
-
sql = `(${sql} ${condition.connector === "OR" ? "OR" : "AND"} ${next.sql})`;
|
|
289
|
-
params.push(...next.params);
|
|
290
|
-
}
|
|
291
|
-
return {
|
|
292
|
-
sql: ` WHERE ${sql}`,
|
|
293
|
-
params
|
|
294
|
-
};
|
|
295
|
-
};
|
|
296
|
-
const buildD1Order = (orderBy) => orderBy === void 0 ? "" : ` ORDER BY ${orderBy.map((clause) => `${clause.field} ${clause.direction.toUpperCase()}${clause.nulls ? ` NULLS ${clause.nulls.toUpperCase()}` : ""}`).join(", ")}`;
|
|
297
|
-
const d1Placeholders = (count) => Array.from({ length: count }, () => "json_extract(?, '$')").join(", ");
|
|
298
|
-
const encodeD1Values = (values) => values.map(encodeD1Value);
|
|
299
|
-
//#endregion
|
|
300
|
-
//#region src/d1Query.ts
|
|
301
|
-
const d1TableNames = {
|
|
302
|
-
bundles: "bundles",
|
|
303
|
-
bundle_patches: "bundle_patches",
|
|
304
|
-
channels: "channels",
|
|
305
|
-
bundle_events: "bundle_events",
|
|
306
|
-
api_keys: "api_keys",
|
|
307
|
-
release_catalogs: "release_catalogs",
|
|
308
|
-
releases: "releases"
|
|
309
|
-
};
|
|
310
|
-
var D1QueryResultError = class extends Error {
|
|
311
|
-
name = "D1QueryResultError";
|
|
312
|
-
operation = "count";
|
|
313
|
-
constructor(result) {
|
|
314
|
-
super("D1 count query returned a malformed first row");
|
|
315
|
-
this.result = result;
|
|
316
|
-
}
|
|
317
|
-
};
|
|
318
|
-
const countD1Rows = async (executor, input) => {
|
|
319
|
-
const where = buildD1Where(input.where);
|
|
320
|
-
const table = d1TableNames[input.model];
|
|
321
|
-
const source = input.distinct === void 0 ? `${table}${where.sql}` : `(SELECT DISTINCT ${input.distinct.join(", ")} FROM ${table}${where.sql}) AS distinct_rows`;
|
|
322
|
-
const row = (await executor.query(`SELECT COUNT(*) AS count FROM ${source}`, where.params))[0];
|
|
323
|
-
if (typeof row !== "object" || row === null || Array.isArray(row)) throw new D1QueryResultError(row);
|
|
324
|
-
const count = Reflect.get(row, "count");
|
|
325
|
-
if (typeof count !== "number" || !Number.isFinite(count)) throw new D1QueryResultError(row);
|
|
326
|
-
return count;
|
|
327
|
-
};
|
|
328
|
-
const findManyD1Rows = async (executor, input) => {
|
|
329
|
-
const where = buildD1Where(input.where);
|
|
330
|
-
const orderBy = input.orderBy;
|
|
331
|
-
const order = buildD1Order(orderBy);
|
|
332
|
-
const table = d1TableNames[input.model];
|
|
333
|
-
const source = input.distinctOn === void 0 ? `SELECT * FROM ${table}${where.sql}${order}` : `SELECT * FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY ${input.distinctOn.fields.join(", ")}${order}) AS __hot_updater_rank FROM ${table}${where.sql}) AS distinct_rows WHERE __hot_updater_rank = 1${order}`;
|
|
334
|
-
const pageParams = encodeD1Values([input.limit, input.offset]);
|
|
335
|
-
const rows = await executor.query(`${source} LIMIT json_extract(?, '$') OFFSET json_extract(?, '$')`, [...where.params, ...pageParams]);
|
|
336
|
-
switch (input.model) {
|
|
337
|
-
case "bundles": return rows.map((row) => parseD1Row("bundles", row));
|
|
338
|
-
case "bundle_patches": return rows.map((row) => parseD1Row("bundle_patches", row));
|
|
339
|
-
case "channels": return rows.map((row) => parseD1Row("channels", row));
|
|
340
|
-
case "bundle_events": return rows.map((row) => parseD1Row("bundle_events", row));
|
|
341
|
-
case "api_keys": return rows.map((row) => parseD1Row("api_keys", row));
|
|
342
|
-
case "releases": return rows.map((row) => parseD1Row("releases", row));
|
|
343
|
-
case "release_catalogs": return rows.map((row) => parseD1Row("release_catalogs", row));
|
|
344
|
-
}
|
|
345
|
-
};
|
|
346
|
-
//#endregion
|
|
347
|
-
//#region src/d1Implementation.ts
|
|
348
|
-
const EXPECTATION_CONFLICT_MARKER = "HOT_UPDATER_COMMIT_EXPECTATION_CONFLICT";
|
|
349
|
-
const expectationGuard = (expectations) => {
|
|
350
|
-
const clauses = [];
|
|
351
|
-
const params = [];
|
|
352
|
-
for (const expectation of expectations) {
|
|
353
|
-
const isRelease = expectation.model === "releases";
|
|
354
|
-
const table = isRelease ? "releases" : "release_catalogs";
|
|
355
|
-
const keyField = isRelease ? "id" : "scope_key";
|
|
356
|
-
const versionField = isRelease ? "revision" : "generation";
|
|
357
|
-
const key = isRelease ? expectation.id : expectation.scopeKey;
|
|
358
|
-
const version = isRelease ? expectation.revision : expectation.generation;
|
|
359
|
-
if (version === null) {
|
|
360
|
-
clauses.push(`NOT EXISTS (SELECT 1 FROM ${table} WHERE ${keyField} = json_extract(?, '$'))`);
|
|
361
|
-
params.push(...encodeD1Values([key]));
|
|
362
|
-
} else {
|
|
363
|
-
clauses.push(`EXISTS (SELECT 1 FROM ${table} WHERE ${keyField} = json_extract(?, '$') AND ${versionField} = json_extract(?, '$'))`);
|
|
364
|
-
params.push(...encodeD1Values([key, version]));
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
return {
|
|
368
|
-
sql: clauses.join(" AND ") || "1",
|
|
369
|
-
params
|
|
370
|
-
};
|
|
371
|
-
};
|
|
372
|
-
const readVersion = (row, field) => {
|
|
373
|
-
if (typeof row !== "object" || row === null) return null;
|
|
374
|
-
const value = Reflect.get(row, field);
|
|
375
|
-
return typeof value === "number" ? value : null;
|
|
376
|
-
};
|
|
377
|
-
const expectationConflict = async (executor, expectations) => {
|
|
378
|
-
for (const expectation of expectations) {
|
|
379
|
-
const isRelease = expectation.model === "releases";
|
|
380
|
-
const table = isRelease ? "releases" : "release_catalogs";
|
|
381
|
-
const keyField = isRelease ? "id" : "scope_key";
|
|
382
|
-
const versionField = isRelease ? "revision" : "generation";
|
|
383
|
-
const key = isRelease ? expectation.id : expectation.scopeKey;
|
|
384
|
-
const expectedVersion = isRelease ? expectation.revision : expectation.generation;
|
|
385
|
-
const actualVersion = readVersion((await executor.query(`SELECT ${versionField} FROM ${table} WHERE ${keyField} = json_extract(?, '$') LIMIT 1`, encodeD1Values([key])))[0], versionField);
|
|
386
|
-
if (actualVersion !== expectedVersion) return {
|
|
387
|
-
committed: false,
|
|
388
|
-
conflict: {
|
|
389
|
-
actualVersion,
|
|
390
|
-
changeIndex: -1,
|
|
391
|
-
expectedVersion,
|
|
392
|
-
key,
|
|
393
|
-
model: expectation.model,
|
|
394
|
-
reason: "version_conflict"
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
}
|
|
398
|
-
return null;
|
|
399
|
-
};
|
|
400
|
-
const isExpectationConflictError = (error) => {
|
|
19
|
+
/** D1 reports SQLite errors in the message; the SQL core classifies codes. */
|
|
20
|
+
const withSqliteCode = (error) => {
|
|
401
21
|
const message = error instanceof Error ? error.message : String(error);
|
|
402
|
-
|
|
403
|
-
};
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
];
|
|
426
|
-
const channelValues = (row) => [row.id, row.name];
|
|
427
|
-
const insertQuery = (input, guard, conflictMode = "returnExisting") => {
|
|
428
|
-
let columns;
|
|
429
|
-
let values;
|
|
430
|
-
switch (input.model) {
|
|
431
|
-
case "bundles":
|
|
432
|
-
columns = [
|
|
433
|
-
"id",
|
|
434
|
-
"platform",
|
|
435
|
-
"file_hash",
|
|
436
|
-
"git_commit_hash",
|
|
437
|
-
"storage_uri",
|
|
438
|
-
"archive_byte_size",
|
|
439
|
-
"metadata",
|
|
440
|
-
"manifest_storage_uri",
|
|
441
|
-
"manifest_file_hash",
|
|
442
|
-
"asset_base_storage_uri"
|
|
443
|
-
];
|
|
444
|
-
values = bundleValues(input.data);
|
|
445
|
-
break;
|
|
446
|
-
case "bundle_patches":
|
|
447
|
-
columns = [
|
|
448
|
-
"id",
|
|
449
|
-
"bundle_id",
|
|
450
|
-
"base_bundle_id",
|
|
451
|
-
"base_file_hash",
|
|
452
|
-
"patch_file_hash",
|
|
453
|
-
"patch_storage_uri",
|
|
454
|
-
"byte_size",
|
|
455
|
-
"order_index"
|
|
456
|
-
];
|
|
457
|
-
values = patchValues(input.data);
|
|
458
|
-
break;
|
|
459
|
-
case "channels":
|
|
460
|
-
columns = ["id", "name"];
|
|
461
|
-
values = channelValues(input.data);
|
|
462
|
-
break;
|
|
463
|
-
case "bundle_events":
|
|
464
|
-
case "api_keys":
|
|
465
|
-
case "release_catalogs":
|
|
466
|
-
case "releases":
|
|
467
|
-
columns = Object.keys(input.data);
|
|
468
|
-
values = Object.values(input.data);
|
|
469
|
-
break;
|
|
470
|
-
}
|
|
471
|
-
const conflict = input.onConflict !== "ignore" ? "" : input.model === "channels" ? conflictMode === "ignore" ? " ON CONFLICT(name) DO NOTHING" : " ON CONFLICT(name) DO UPDATE SET name = excluded.name" : input.model === "api_keys" ? conflictMode === "ignore" ? " ON CONFLICT(hash) DO NOTHING" : " ON CONFLICT(hash) DO UPDATE SET hash = excluded.hash" : "";
|
|
472
|
-
return {
|
|
473
|
-
sql: `INSERT INTO ${d1TableNames[input.model]} (${columns.join(", ")}) ${guard === void 0 ? `VALUES (${d1Placeholders(values.length)})` : `SELECT ${d1Placeholders(values.length)} WHERE ${guard.sql}`}${conflict} RETURNING *`,
|
|
474
|
-
params: [...encodeD1Values(values), ...guard?.params ?? []]
|
|
475
|
-
};
|
|
476
|
-
};
|
|
477
|
-
const updateEntries = (update) => Object.entries(update);
|
|
478
|
-
const updateQuery = (input, guard) => {
|
|
479
|
-
const entries = updateEntries(input.update);
|
|
480
|
-
const where = buildD1Where(input.where);
|
|
481
|
-
if (entries.length === 0) return {
|
|
482
|
-
sql: `SELECT * FROM ${d1TableNames[input.model]}${where.sql}${guard ? ` AND ${guard.sql}` : ""} LIMIT 1`,
|
|
483
|
-
params: [...where.params, ...guard?.params ?? []]
|
|
484
|
-
};
|
|
485
|
-
const assignments = entries.map(([field]) => `${field} = json_extract(?, '$')`).join(", ");
|
|
486
|
-
return {
|
|
487
|
-
sql: `UPDATE ${d1TableNames[input.model]} SET ${assignments}${where.sql}${guard ? ` AND ${guard.sql}` : ""} RETURNING *`,
|
|
488
|
-
params: [
|
|
489
|
-
...encodeD1Values(entries.map(([, value]) => value)),
|
|
490
|
-
...where.params,
|
|
491
|
-
...guard?.params ?? []
|
|
492
|
-
]
|
|
493
|
-
};
|
|
494
|
-
};
|
|
495
|
-
const deleteQuery = (input, guard) => {
|
|
496
|
-
const where = buildD1Where(input.where);
|
|
497
|
-
return {
|
|
498
|
-
sql: `DELETE FROM ${d1TableNames[input.model]}${where.sql}${guard ? ` AND ${guard.sql}` : ""}`,
|
|
499
|
-
params: [...where.params, ...guard?.params ?? []]
|
|
500
|
-
};
|
|
501
|
-
};
|
|
502
|
-
const insertedKey = (change) => {
|
|
503
|
-
if (change.operation !== "insert") return void 0;
|
|
504
|
-
if (change.model === "bundles") return `bundles:${change.row.id}`;
|
|
505
|
-
if (change.model === "apiKeys") return `api_keys:${change.row.id}`;
|
|
506
|
-
if (change.model === "releases") return `releases:${change.row.id}`;
|
|
507
|
-
};
|
|
508
|
-
const requiredRow = (change, changeIndex) => {
|
|
509
|
-
if (change.operation !== "update") return void 0;
|
|
510
|
-
if (change.model === "bundles") return {
|
|
511
|
-
model: "bundles",
|
|
512
|
-
id: change.where.id,
|
|
513
|
-
changeIndex
|
|
514
|
-
};
|
|
515
|
-
if (change.model === "apiKeys") return {
|
|
516
|
-
model: "api_keys",
|
|
517
|
-
id: change.where.id,
|
|
518
|
-
changeIndex
|
|
519
|
-
};
|
|
520
|
-
if (change.model === "releases") return {
|
|
521
|
-
model: "releases",
|
|
522
|
-
id: change.where.id,
|
|
523
|
-
changeIndex
|
|
524
|
-
};
|
|
525
|
-
};
|
|
526
|
-
const channelDeletePrecondition = (changes, changeIndex, channelId) => {
|
|
527
|
-
const releaseEffects = /* @__PURE__ */ new Map();
|
|
528
|
-
for (const change of changes.slice(0, changeIndex)) {
|
|
529
|
-
if (change.model !== "releases") continue;
|
|
530
|
-
switch (change.operation) {
|
|
531
|
-
case "insert":
|
|
532
|
-
releaseEffects.set(change.row.id, change.row.channel_id);
|
|
533
|
-
break;
|
|
534
|
-
case "update": break;
|
|
535
|
-
case "delete":
|
|
536
|
-
releaseEffects.set(change.where.id, null);
|
|
537
|
-
break;
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
return {
|
|
541
|
-
id: channelId,
|
|
542
|
-
excludedReleaseIds: [...releaseEffects.keys()],
|
|
543
|
-
addedReferenceReleaseIds: [...releaseEffects].filter(([, finalChannelId]) => finalChannelId === channelId).map(([releaseId]) => releaseId)
|
|
544
|
-
};
|
|
545
|
-
};
|
|
546
|
-
const changeQuery = (change, guard) => {
|
|
547
|
-
switch (change.model) {
|
|
548
|
-
case "bundles": switch (change.operation) {
|
|
549
|
-
case "insert": return insertQuery({
|
|
550
|
-
model: "bundles",
|
|
551
|
-
data: change.row
|
|
552
|
-
}, guard);
|
|
553
|
-
case "update": return updateQuery({
|
|
554
|
-
model: "bundles",
|
|
555
|
-
where: [{
|
|
556
|
-
field: "id",
|
|
557
|
-
value: change.where.id
|
|
558
|
-
}],
|
|
559
|
-
update: change.update
|
|
560
|
-
}, guard);
|
|
561
|
-
case "delete": return deleteQuery({
|
|
562
|
-
model: "bundles",
|
|
563
|
-
where: [{
|
|
564
|
-
field: "id",
|
|
565
|
-
value: change.where.id
|
|
566
|
-
}]
|
|
567
|
-
}, guard);
|
|
568
|
-
}
|
|
569
|
-
case "bundlePatches": return change.operation === "insert" ? insertQuery({
|
|
570
|
-
model: "bundle_patches",
|
|
571
|
-
data: change.row
|
|
572
|
-
}, guard) : deleteQuery({
|
|
573
|
-
model: "bundle_patches",
|
|
574
|
-
where: [{
|
|
575
|
-
field: "bundle_id",
|
|
576
|
-
value: change.where.bundleId
|
|
577
|
-
}]
|
|
578
|
-
}, guard);
|
|
579
|
-
case "releases": switch (change.operation) {
|
|
580
|
-
case "insert": return insertQuery({
|
|
581
|
-
model: "releases",
|
|
582
|
-
data: change.row
|
|
583
|
-
}, guard);
|
|
584
|
-
case "update": return updateQuery({
|
|
585
|
-
model: "releases",
|
|
586
|
-
where: [{
|
|
587
|
-
field: "id",
|
|
588
|
-
value: change.where.id
|
|
589
|
-
}],
|
|
590
|
-
update: change.update
|
|
591
|
-
}, guard);
|
|
592
|
-
case "delete": return deleteQuery({
|
|
593
|
-
model: "releases",
|
|
594
|
-
where: [{
|
|
595
|
-
field: "id",
|
|
596
|
-
value: change.where.id
|
|
597
|
-
}]
|
|
598
|
-
}, guard);
|
|
599
|
-
}
|
|
600
|
-
case "releaseCatalogs": return {
|
|
601
|
-
sql: `INSERT INTO release_catalogs (${Object.keys(change.row).join(", ")}) SELECT ${d1Placeholders(Object.keys(change.row).length)} WHERE ${guard.sql} ON CONFLICT(scope_key) DO UPDATE SET ${Object.keys(change.row).filter((field) => field !== "scope_key").map((field) => `${field} = excluded.${field}`).join(", ")} RETURNING *`,
|
|
602
|
-
params: [...encodeD1Values(Object.values(change.row)), ...guard.params]
|
|
603
|
-
};
|
|
604
|
-
case "channels": return change.operation === "insert" ? insertQuery({
|
|
605
|
-
model: "channels",
|
|
606
|
-
data: change.row,
|
|
607
|
-
onConflict: change.onConflict
|
|
608
|
-
}, guard, "ignore") : {
|
|
609
|
-
sql: `DELETE FROM channels WHERE id = json_extract(?, '$') AND NOT EXISTS (SELECT 1 FROM releases WHERE channel_id = json_extract(?, '$')) AND ${guard.sql}`,
|
|
610
|
-
params: [...encodeD1Values([change.where.id, change.where.id]), ...guard.params]
|
|
611
|
-
};
|
|
612
|
-
case "insights": return insertQuery({
|
|
613
|
-
model: "bundle_events",
|
|
614
|
-
data: change.row
|
|
615
|
-
}, guard);
|
|
616
|
-
case "apiKeys": return change.operation === "insert" ? insertQuery({
|
|
617
|
-
model: "api_keys",
|
|
618
|
-
data: change.row,
|
|
619
|
-
onConflict: change.onConflict
|
|
620
|
-
}, guard, "ignore") : updateQuery({
|
|
621
|
-
model: "api_keys",
|
|
622
|
-
where: [{
|
|
623
|
-
field: "id",
|
|
624
|
-
value: change.where.id
|
|
625
|
-
}],
|
|
626
|
-
update: { revoked_at_ms: change.update.revokedAtMs }
|
|
627
|
-
}, guard);
|
|
628
|
-
}
|
|
629
|
-
};
|
|
630
|
-
const createCommitPlan = (input) => {
|
|
631
|
-
const checks = [];
|
|
632
|
-
const statements = [];
|
|
633
|
-
const expectations = input.expectations ?? [];
|
|
634
|
-
if (expectations.length > 0) {
|
|
635
|
-
const guard = expectationGuard(expectations);
|
|
636
|
-
statements.push({
|
|
637
|
-
sql: `SELECT CASE WHEN ${guard.sql} THEN 1 ELSE json_extract('${EXPECTATION_CONFLICT_MARKER}', '$') END AS expectation_guard`,
|
|
638
|
-
params: guard.params
|
|
639
|
-
});
|
|
640
|
-
}
|
|
641
|
-
const requiredRows = [];
|
|
642
|
-
const channelDeletes = [];
|
|
643
|
-
const inserted = /* @__PURE__ */ new Set();
|
|
644
|
-
for (const [changeIndex, change] of input.changes.entries()) {
|
|
645
|
-
const required = requiredRow(change, changeIndex);
|
|
646
|
-
if (required !== void 0) {
|
|
647
|
-
if (!inserted.has(`${required.model}:${required.id}`)) {
|
|
648
|
-
requiredRows.push(required);
|
|
649
|
-
checks.push({
|
|
650
|
-
changeIndex,
|
|
651
|
-
resultIndex: statements.length,
|
|
652
|
-
conflictWhen: "empty",
|
|
653
|
-
reason: "not_found"
|
|
654
|
-
});
|
|
655
|
-
statements.push({
|
|
656
|
-
sql: `SELECT id FROM ${d1TableNames[required.model]} WHERE id = json_extract(?, '$') LIMIT 1`,
|
|
657
|
-
params: encodeD1Values([required.id])
|
|
658
|
-
});
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
if (change.model === "channels" && change.operation === "delete") {
|
|
662
|
-
const precondition = channelDeletePrecondition(input.changes, changeIndex, change.where.id);
|
|
663
|
-
channelDeletes.push(precondition);
|
|
664
|
-
checks.push({
|
|
665
|
-
changeIndex,
|
|
666
|
-
resultIndex: statements.length,
|
|
667
|
-
conflictWhen: "nonempty",
|
|
668
|
-
reason: "referenced"
|
|
669
|
-
});
|
|
670
|
-
statements.push({
|
|
671
|
-
sql: "SELECT id FROM releases WHERE channel_id = json_extract(?, '$') AND id NOT IN (SELECT value FROM json_each(?)) UNION ALL SELECT value AS id FROM json_each(?) LIMIT 1",
|
|
672
|
-
params: encodeD1Values([
|
|
673
|
-
precondition.id,
|
|
674
|
-
precondition.excludedReleaseIds,
|
|
675
|
-
precondition.addedReferenceReleaseIds
|
|
676
|
-
])
|
|
677
|
-
});
|
|
678
|
-
}
|
|
679
|
-
const key = insertedKey(change);
|
|
680
|
-
if (key !== void 0) inserted.add(key);
|
|
681
|
-
}
|
|
682
|
-
const guard = {
|
|
683
|
-
sql: `NOT EXISTS (
|
|
684
|
-
SELECT 1 FROM json_each(?) AS required
|
|
685
|
-
WHERE NOT EXISTS (
|
|
686
|
-
SELECT 1 FROM bundles
|
|
687
|
-
WHERE json_extract(required.value, '$.model') = 'bundles'
|
|
688
|
-
AND bundles.id = json_extract(required.value, '$.id')
|
|
689
|
-
UNION ALL
|
|
690
|
-
SELECT 1 FROM api_keys
|
|
691
|
-
WHERE json_extract(required.value, '$.model') = 'api_keys'
|
|
692
|
-
AND api_keys.id = json_extract(required.value, '$.id')
|
|
693
|
-
UNION ALL
|
|
694
|
-
SELECT 1 FROM channels
|
|
695
|
-
WHERE json_extract(required.value, '$.model') = 'channels'
|
|
696
|
-
AND channels.id = json_extract(required.value, '$.id')
|
|
697
|
-
UNION ALL
|
|
698
|
-
SELECT 1 FROM releases
|
|
699
|
-
WHERE json_extract(required.value, '$.model') = 'releases'
|
|
700
|
-
AND releases.id = json_extract(required.value, '$.id')
|
|
701
|
-
)
|
|
702
|
-
) AND NOT EXISTS (
|
|
703
|
-
SELECT 1 FROM json_each(?) AS channel_delete
|
|
704
|
-
WHERE EXISTS (
|
|
705
|
-
SELECT 1 FROM releases
|
|
706
|
-
WHERE releases.channel_id = json_extract(channel_delete.value, '$.id')
|
|
707
|
-
AND releases.id NOT IN (
|
|
708
|
-
SELECT value FROM json_each(
|
|
709
|
-
json_extract(channel_delete.value, '$.excludedReleaseIds')
|
|
710
|
-
)
|
|
711
|
-
)
|
|
712
|
-
UNION ALL
|
|
713
|
-
SELECT value FROM json_each(
|
|
714
|
-
json_extract(channel_delete.value, '$.addedReferenceReleaseIds')
|
|
715
|
-
)
|
|
716
|
-
)
|
|
717
|
-
)`,
|
|
718
|
-
params: encodeD1Values([requiredRows.map(({ model, id }) => ({
|
|
719
|
-
model,
|
|
720
|
-
id
|
|
721
|
-
})), channelDeletes])
|
|
722
|
-
};
|
|
723
|
-
statements.push(...input.changes.map((change) => changeQuery(change, guard)));
|
|
724
|
-
return {
|
|
725
|
-
checks,
|
|
726
|
-
statements
|
|
727
|
-
};
|
|
728
|
-
};
|
|
729
|
-
const resultForPlan = (plan, results) => {
|
|
730
|
-
const missing = plan.checks.find(({ conflictWhen, resultIndex }) => conflictWhen === "empty" ? (results[resultIndex]?.length ?? 0) === 0 : (results[resultIndex]?.length ?? 0) > 0);
|
|
731
|
-
return missing === void 0 ? { committed: true } : {
|
|
732
|
-
committed: false,
|
|
733
|
-
conflict: {
|
|
734
|
-
changeIndex: missing.changeIndex,
|
|
735
|
-
reason: missing.reason
|
|
736
|
-
}
|
|
737
|
-
};
|
|
738
|
-
};
|
|
739
|
-
const insertChannel = async (executor, input) => {
|
|
740
|
-
const insert = insertQuery({
|
|
741
|
-
model: "channels",
|
|
742
|
-
data: input.row,
|
|
743
|
-
onConflict: "ignore"
|
|
744
|
-
}, void 0, "ignore");
|
|
745
|
-
const select = {
|
|
746
|
-
sql: "SELECT id, name FROM channels WHERE name = json_extract(?, '$') LIMIT 1",
|
|
747
|
-
params: encodeD1Values([input.row.name])
|
|
748
|
-
};
|
|
749
|
-
const [insertedRows = [], canonicalRows = []] = await executor.batch([insert, select]);
|
|
750
|
-
return {
|
|
751
|
-
row: parseD1Row("channels", canonicalRows[0]),
|
|
752
|
-
inserted: insertedRows.length > 0
|
|
753
|
-
};
|
|
754
|
-
};
|
|
755
|
-
const deleteChannel = async (executor, input) => {
|
|
756
|
-
const [channelRows = [], referenceRows = [], deletedRows = []] = await executor.batch([
|
|
757
|
-
{
|
|
758
|
-
sql: "SELECT id FROM channels WHERE id = json_extract(?, '$') LIMIT 1",
|
|
759
|
-
params: encodeD1Values([input.id])
|
|
760
|
-
},
|
|
761
|
-
{
|
|
762
|
-
sql: "SELECT id FROM releases WHERE channel_id = json_extract(?, '$') LIMIT 1",
|
|
763
|
-
params: encodeD1Values([input.id])
|
|
764
|
-
},
|
|
765
|
-
{
|
|
766
|
-
sql: "DELETE FROM channels WHERE id = json_extract(?, '$') AND NOT EXISTS (SELECT 1 FROM releases WHERE channel_id = json_extract(?, '$')) RETURNING id",
|
|
767
|
-
params: encodeD1Values([input.id, input.id])
|
|
768
|
-
}
|
|
769
|
-
]);
|
|
770
|
-
if (channelRows.length === 0) return {
|
|
771
|
-
deleted: false,
|
|
772
|
-
reason: "not_found"
|
|
773
|
-
};
|
|
774
|
-
if (referenceRows.length > 0) return {
|
|
775
|
-
deleted: false,
|
|
776
|
-
reason: "not_empty"
|
|
777
|
-
};
|
|
778
|
-
return deletedRows.length > 0 ? { deleted: true } : {
|
|
779
|
-
deleted: false,
|
|
780
|
-
reason: "not_empty"
|
|
781
|
-
};
|
|
782
|
-
};
|
|
783
|
-
const createD1Implementation = (executor) => ({
|
|
784
|
-
async create(input) {
|
|
785
|
-
const query = insertQuery(input);
|
|
786
|
-
const rows = await executor.query(query.sql, query.params);
|
|
787
|
-
switch (input.model) {
|
|
788
|
-
case "bundles": return parseD1Row("bundles", rows[0]);
|
|
789
|
-
case "bundle_patches": return parseD1Row("bundle_patches", rows[0]);
|
|
790
|
-
case "channels": return parseD1Row("channels", rows[0]);
|
|
791
|
-
case "bundle_events": return parseD1Row("bundle_events", rows[0]);
|
|
792
|
-
case "api_keys": return parseD1Row("api_keys", rows[0]);
|
|
793
|
-
case "releases": return parseD1Row("releases", rows[0]);
|
|
794
|
-
case "release_catalogs": return parseD1Row("release_catalogs", rows[0]);
|
|
795
|
-
}
|
|
796
|
-
},
|
|
797
|
-
async update(input) {
|
|
798
|
-
const query = updateQuery(input);
|
|
799
|
-
const rows = await executor.query(query.sql, query.params);
|
|
800
|
-
if (rows[0] === void 0) return null;
|
|
801
|
-
switch (input.model) {
|
|
802
|
-
case "bundles": return parseD1Row("bundles", rows[0]);
|
|
803
|
-
case "api_keys": return parseD1Row("api_keys", rows[0]);
|
|
804
|
-
case "releases": return parseD1Row("releases", rows[0]);
|
|
805
|
-
case "release_catalogs": return parseD1Row("release_catalogs", rows[0]);
|
|
806
|
-
}
|
|
807
|
-
},
|
|
808
|
-
async delete(input) {
|
|
809
|
-
const query = deleteQuery(input);
|
|
810
|
-
await executor.query(query.sql, query.params);
|
|
811
|
-
},
|
|
812
|
-
count: (input) => countD1Rows(executor, input),
|
|
813
|
-
async findOne(input) {
|
|
814
|
-
const where = buildD1Where(input.where);
|
|
815
|
-
const rows = await executor.query(`SELECT * FROM ${d1TableNames[input.model]}${where.sql} LIMIT 1`, where.params);
|
|
816
|
-
if (rows[0] === void 0) return null;
|
|
817
|
-
switch (input.model) {
|
|
818
|
-
case "bundles": return parseD1Row("bundles", rows[0]);
|
|
819
|
-
case "bundle_patches": return parseD1Row("bundle_patches", rows[0]);
|
|
820
|
-
case "channels": return parseD1Row("channels", rows[0]);
|
|
821
|
-
case "api_keys": return parseD1Row("api_keys", rows[0]);
|
|
822
|
-
case "releases": return parseD1Row("releases", rows[0]);
|
|
823
|
-
case "release_catalogs": return parseD1Row("release_catalogs", rows[0]);
|
|
824
|
-
}
|
|
825
|
-
},
|
|
826
|
-
findMany: (input) => findManyD1Rows(executor, input),
|
|
827
|
-
insertChannel: (input) => insertChannel(executor, input),
|
|
828
|
-
deleteChannel: (input) => deleteChannel(executor, input),
|
|
829
|
-
async commit(input) {
|
|
830
|
-
if (input.changes.length === 0) return { committed: true };
|
|
831
|
-
const expectations = input.expectations ?? [];
|
|
832
|
-
const conflict = await expectationConflict(executor, expectations);
|
|
833
|
-
if (conflict !== null) return conflict;
|
|
834
|
-
const plan = createCommitPlan(input);
|
|
835
|
-
try {
|
|
836
|
-
return resultForPlan(plan, await executor.batch(plan.statements));
|
|
837
|
-
} catch (error) {
|
|
838
|
-
if (expectations.length === 0 || !isExpectationConflictError(error)) throw error;
|
|
839
|
-
return await expectationConflict(executor, expectations) ?? {
|
|
840
|
-
committed: false,
|
|
841
|
-
conflict: {
|
|
842
|
-
actualVersion: expectations[0].model === "releases" ? expectations[0].revision : expectations[0].generation,
|
|
843
|
-
changeIndex: -1,
|
|
844
|
-
expectedVersion: expectations[0].model === "releases" ? expectations[0].revision : expectations[0].generation,
|
|
845
|
-
key: expectations[0].model === "releases" ? expectations[0].id : expectations[0].scopeKey,
|
|
846
|
-
model: expectations[0].model,
|
|
847
|
-
reason: "version_conflict"
|
|
22
|
+
const code = /UNIQUE constraint failed|constraint failed: .*PRIMARY/iu.test(message) ? "SQLITE_CONSTRAINT_UNIQUE" : /SQLITE_BUSY|database is locked/iu.test(message) ? "SQLITE_BUSY" : void 0;
|
|
23
|
+
return code === void 0 ? error : Object.assign(new Error(message, { cause: error }), { code });
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Hot Updater's database on D1: the storage engine through the shared SQL
|
|
27
|
+
* core, fenced by the schema settings. D1 has no interactive transactions,
|
|
28
|
+
* so each write is one atomic batch that evaluates its guards first
|
|
29
|
+
* (`_hu_write`).
|
|
30
|
+
*/
|
|
31
|
+
const createD1Database = (runner) => {
|
|
32
|
+
return (0, _hot_updater_plugin_core.createEngineDatabase)({
|
|
33
|
+
name: "d1Database",
|
|
34
|
+
adapter: (0, _hot_updater_plugin_core.createSqlAdapter)({
|
|
35
|
+
executor: {
|
|
36
|
+
dialect: "sqlite",
|
|
37
|
+
execute: (statement) => runner.query(statement).catch((error) => {
|
|
38
|
+
throw withSqliteCode(error);
|
|
39
|
+
}),
|
|
40
|
+
batch: (statements) => runner.batch(statements).catch((error) => {
|
|
41
|
+
throw withSqliteCode(error);
|
|
42
|
+
}),
|
|
43
|
+
transaction: () => {
|
|
44
|
+
throw new Error("D1 has no interactive transactions; writes are batches.");
|
|
848
45
|
}
|
|
849
|
-
}
|
|
850
|
-
|
|
46
|
+
},
|
|
47
|
+
maxOps: 450,
|
|
48
|
+
maxParams: 100
|
|
49
|
+
})
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/worker/d1Database.ts
|
|
54
|
+
/** Hot Updater's database on a D1 binding, inside a Worker. */
|
|
55
|
+
const d1Database = (database) => createD1Database({
|
|
56
|
+
query: async ({ sql, params }) => toSqlResult(await database.prepare(sql).bind(...params).all()),
|
|
57
|
+
async batch(statements) {
|
|
58
|
+
const results = await database.batch(statements.map(({ sql, params }) => database.prepare(sql).bind(...params)));
|
|
59
|
+
if (results.length !== statements.length) throw new D1ExecutionError();
|
|
60
|
+
return results.map(toSqlResult);
|
|
851
61
|
}
|
|
852
62
|
});
|
|
853
63
|
//#endregion
|
|
854
|
-
//#region src/
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
async batch(statements) {
|
|
861
|
-
return (await database.batch(statements.map(({ sql, params }) => database.prepare(sql).bind(...params)))).map(({ results }) => results ?? []);
|
|
862
|
-
}
|
|
863
|
-
}));
|
|
864
|
-
return (0, _hot_updater_plugin_core.createDatabasePlugin)({
|
|
865
|
-
name: "d1Database",
|
|
866
|
-
models: adapter.models,
|
|
867
|
-
commit: adapter.commit,
|
|
868
|
-
...adapter.dispose ? { dispose: adapter.dispose } : {}
|
|
869
|
-
});
|
|
870
|
-
};
|
|
64
|
+
//#region src/plugins.ts
|
|
65
|
+
/**
|
|
66
|
+
* The plugins the managed Cloudflare server runs: Insights, and API keys on every
|
|
67
|
+
* client route. Its Worker imports this list.
|
|
68
|
+
*/
|
|
69
|
+
const plugins = [(0, _hot_updater_server_plugins_insights.insights)(), (0, _hot_updater_server_plugins_api_keys.apiKeys)()];
|
|
871
70
|
//#endregion
|
|
872
71
|
//#region src/r2WorkerStorage.ts
|
|
873
72
|
const r2WorkerStorage = (config) => {
|
|
@@ -878,7 +77,7 @@ const r2WorkerStorage = (config) => {
|
|
|
878
77
|
if (parsed.bucket !== config.bucketName) throw new Error(`Bucket name mismatch: expected "${config.bucketName}", but found "${parsed.bucket}".`);
|
|
879
78
|
return parsed;
|
|
880
79
|
};
|
|
881
|
-
return (0, _hot_updater_plugin_core.
|
|
80
|
+
return (0, _hot_updater_plugin_core.createStorageAdapter)({
|
|
882
81
|
name: "r2Storage",
|
|
883
82
|
protocol: "r2",
|
|
884
83
|
async put({ key, body, contentLength, contentType }) {
|
|
@@ -927,4 +126,5 @@ const r2WorkerStorage = (config) => {
|
|
|
927
126
|
};
|
|
928
127
|
//#endregion
|
|
929
128
|
exports.d1Database = d1Database;
|
|
129
|
+
exports.plugins = plugins;
|
|
930
130
|
exports.r2Storage = r2WorkerStorage;
|