@hot-updater/aws 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 +320 -1938
- package/dist/index.d.cts +734 -10
- package/dist/index.d.mts +734 -10
- package/dist/index.mjs +320 -1939
- package/dist/init/index.cjs +8536 -4
- package/dist/init/index.d.cts +124 -3
- package/dist/init/index.d.mts +124 -3
- package/dist/init/index.mjs +8523 -1
- package/dist/lambda/index.cjs +11617 -8579
- package/dist/lambda/index.d.cts +344 -1
- package/package.json +7 -13
- package/dist/iac/index.cjs +0 -10287
- package/dist/iac/index.d.cts +0 -19
- package/dist/iac/index.d.mts +0 -19
- package/dist/iac/index.mjs +0 -10280
package/dist/index.mjs
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CreateTableCommand, DescribeTableCommand, DescribeTimeToLiveCommand, DynamoDBClient, UpdateTimeToLiveCommand, waitUntilTableExists } from "@aws-sdk/client-dynamodb";
|
|
2
|
+
import { createEngineDatabase, createKvAdapter, createStorageAdapter, createStorageDownloadUrl, createStorageKeyBuilder, createStorageUri, migrateCoreSchema, parseStorageUri } from "@hot-updater/plugin-core";
|
|
2
3
|
import { CloudFrontClient, CreateInvalidationCommand, GetInvalidationCommand } from "@aws-sdk/client-cloudfront";
|
|
3
|
-
import {
|
|
4
|
-
import { BatchGetCommand, DynamoDBDocumentClient, GetCommand, PutCommand, QueryCommand, ScanCommand, TransactWriteCommand } from "@aws-sdk/lib-dynamodb";
|
|
5
|
-
import { createDatabasePlugin, createStorageDownloadUrl, createStorageKeyBuilder, createStoragePlugin, createStorageUri, isDatabaseMetadataObject, isInsightsMovementEvent, parseStorageUri } from "@hot-updater/plugin-core";
|
|
6
|
-
import { createDatabasePluginAdapter } from "@hot-updater/plugin-core/internal";
|
|
4
|
+
import { BatchGetCommand, BatchWriteCommand, DynamoDBDocumentClient, QueryCommand, TransactWriteCommand } from "@aws-sdk/lib-dynamodb";
|
|
7
5
|
import { SSM } from "@aws-sdk/client-ssm";
|
|
8
6
|
import { getSignedUrl } from "@aws-sdk/cloudfront-signer";
|
|
9
7
|
import { DeleteObjectCommand, DeleteObjectsCommand, GetObjectCommand, HeadObjectCommand, ListObjectsV2Command, S3Client } from "@aws-sdk/client-s3";
|
|
10
8
|
import { Upload } from "@aws-sdk/lib-storage";
|
|
9
|
+
import { apiKeys } from "@hot-updater/server/plugins/api-keys";
|
|
10
|
+
import { insights } from "@hot-updater/server/plugins/insights";
|
|
11
11
|
//#region src/cloudFrontInvalidation.ts
|
|
12
12
|
const DEFAULT_POLL_INTERVAL_MS = 2e3;
|
|
13
13
|
const DEFAULT_TIMEOUT_MS = 3e5;
|
|
14
|
-
const sleep = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
14
|
+
const sleep$1 = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
15
15
|
const invalidateCloudFront = async (client, distributionId, paths, options) => {
|
|
16
16
|
if (paths.length === 0) return;
|
|
17
17
|
const response = await client.send(new CreateInvalidationCommand({
|
|
@@ -29,7 +29,7 @@ const invalidateCloudFront = async (client, distributionId, paths, options) => {
|
|
|
29
29
|
if (!invalidationId) throw new Error("CloudFront invalidation response is missing Invalidation.Id");
|
|
30
30
|
const deadline = Date.now() + DEFAULT_TIMEOUT_MS;
|
|
31
31
|
while (Date.now() < deadline) {
|
|
32
|
-
await sleep(DEFAULT_POLL_INTERVAL_MS);
|
|
32
|
+
await sleep$1(DEFAULT_POLL_INTERVAL_MS);
|
|
33
33
|
if ((await client.send(new GetInvalidationCommand({
|
|
34
34
|
DistributionId: distributionId,
|
|
35
35
|
Id: invalidationId
|
|
@@ -37,1971 +37,328 @@ const invalidateCloudFront = async (client, distributionId, paths, options) => {
|
|
|
37
37
|
}
|
|
38
38
|
throw new Error(`Timed out waiting for CloudFront invalidation ${invalidationId}.`);
|
|
39
39
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
};
|
|
52
|
-
const boundedDynamoDBMetadataItem = (item) => {
|
|
53
|
-
const byteLength = new TextEncoder().encode(JSON.stringify(item)).byteLength;
|
|
54
|
-
if (byteLength > 8192) throw new DynamoDBMetadataItemSizeError(byteLength);
|
|
55
|
-
return item;
|
|
56
|
-
};
|
|
57
|
-
var DynamoDBCatalogItemSizeError = class extends Error {
|
|
58
|
-
byteLength;
|
|
59
|
-
name = "DynamoDBCatalogItemSizeError";
|
|
60
|
-
constructor(byteLength) {
|
|
61
|
-
super(`DynamoDB catalog item is ${byteLength} bytes; maximum is ${DYNAMODB_MAX_CATALOG_ITEM_BYTES}`);
|
|
62
|
-
this.byteLength = byteLength;
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
const boundedDynamoDBCatalogItem = (item) => {
|
|
66
|
-
const byteLength = new TextEncoder().encode(JSON.stringify(item)).byteLength;
|
|
67
|
-
if (byteLength > 409600) throw new DynamoDBCatalogItemSizeError(byteLength);
|
|
68
|
-
return item;
|
|
69
|
-
};
|
|
70
|
-
const DYNAMODB_CHANNEL_PARTITION = "channels";
|
|
71
|
-
const DYNAMODB_CHANNEL_NAME_PARTITION = "_hot-updater#channel-names";
|
|
72
|
-
var DynamoDBStoredItemError = class extends Error {
|
|
73
|
-
name = "DynamoDBStoredItemError";
|
|
74
|
-
constructor() {
|
|
75
|
-
super("DynamoDB contains an invalid Hot Updater row");
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
const field = (value, name) => Reflect.get(value, name);
|
|
79
|
-
const isNullableString = (value) => value === null || typeof value === "string";
|
|
80
|
-
const isByteSize = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
81
|
-
const withLegacyArchiveByteSize = (value) => typeof value === "object" && value !== null && field(value, "archive_byte_size") === void 0 ? {
|
|
82
|
-
...value,
|
|
83
|
-
archive_byte_size: 0
|
|
84
|
-
} : value;
|
|
85
|
-
const isBundleRow = (value) => typeof value === "object" && value !== null && typeof field(value, "id") === "string" && (field(value, "platform") === "ios" || field(value, "platform") === "android") && typeof field(value, "file_hash") === "string" && isNullableString(field(value, "git_commit_hash")) && typeof field(value, "storage_uri") === "string" && isByteSize(field(value, "archive_byte_size")) && isDatabaseMetadataObject(field(value, "metadata")) && isNullableString(field(value, "manifest_storage_uri")) && isNullableString(field(value, "manifest_file_hash")) && isNullableString(field(value, "asset_base_storage_uri"));
|
|
86
|
-
const isPatchRow = (value) => typeof value === "object" && value !== null && typeof field(value, "id") === "string" && typeof field(value, "bundle_id") === "string" && typeof field(value, "base_bundle_id") === "string" && typeof field(value, "base_file_hash") === "string" && typeof field(value, "patch_file_hash") === "string" && typeof field(value, "patch_storage_uri") === "string" && isByteSize(field(value, "byte_size")) && typeof field(value, "order_index") === "number";
|
|
87
|
-
const isChannelRow = (value) => typeof value === "object" && value !== null && typeof field(value, "id") === "string" && typeof field(value, "name") === "string";
|
|
88
|
-
const isReleaseRow = (value) => typeof value === "object" && value !== null && typeof field(value, "id") === "string" && typeof field(value, "revision") === "number" && typeof field(value, "scope_key") === "string" && typeof field(value, "channel_id") === "string" && (field(value, "platform") === "ios" || field(value, "platform") === "android") && (field(value, "kind") === "BUNDLE" || field(value, "kind") === "EMBEDDED") && isNullableString(field(value, "bundle_id")) && (field(value, "strategy") === "APP_VERSION" || field(value, "strategy") === "FINGERPRINT") && isNullableString(field(value, "target_app_version")) && isNullableString(field(value, "fingerprint_hash")) && typeof field(value, "enabled") === "boolean" && typeof field(value, "should_force_update") === "boolean" && isNullableString(field(value, "message")) && typeof field(value, "rollout_cohort_count") === "number" && Array.isArray(field(value, "target_cohorts")) && field(value, "target_cohorts").every((cohort) => typeof cohort === "string") && (field(value, "operation") === "DEPLOY" || field(value, "operation") === "PROMOTE" || field(value, "operation") === "ROLLBACK") && isNullableString(field(value, "source_release_id")) && typeof field(value, "created_at_ms") === "number" && typeof field(value, "updated_at_ms") === "number";
|
|
89
|
-
const isReleaseCatalogRow = (value) => typeof value === "object" && value !== null && typeof field(value, "scope_key") === "string" && typeof field(value, "catalog_id") === "string" && (field(value, "strategy") === "APP_VERSION" || field(value, "strategy") === "FINGERPRINT") && typeof field(value, "channel_id") === "string" && typeof field(value, "channel_key") === "string" && (field(value, "platform") === "ios" || field(value, "platform") === "android") && isNullableString(field(value, "fingerprint_hash")) && typeof field(value, "generation") === "number" && typeof field(value, "payload") === "string" && typeof field(value, "catalog_hash") === "string" && typeof field(value, "byte_size") === "number" && typeof field(value, "is_tombstone") === "boolean" && typeof field(value, "updated_at_ms") === "number";
|
|
90
|
-
const parseDynamoDBItem = (value) => {
|
|
91
|
-
const pk = value.pk;
|
|
92
|
-
const sk = value.sk;
|
|
93
|
-
const version = value.version;
|
|
94
|
-
const relationCount = value.relation_count;
|
|
95
|
-
const ownedPatchCount = value.owned_patch_count;
|
|
96
|
-
const gsi1pk = value.gsi1pk;
|
|
97
|
-
const gsi1sk = value.gsi1sk;
|
|
98
|
-
const row = pk === "bundles" ? withLegacyArchiveByteSize(value.row) : value.row;
|
|
99
|
-
if (typeof sk !== "string" || typeof version !== "number") throw new DynamoDBStoredItemError();
|
|
100
|
-
if (pk === "channels" && typeof value.reference_count === "number" && isChannelRow(row)) return {
|
|
101
|
-
pk,
|
|
102
|
-
sk,
|
|
103
|
-
version,
|
|
104
|
-
reference_count: value.reference_count,
|
|
105
|
-
row
|
|
106
|
-
};
|
|
107
|
-
if (typeof pk === "string" && pk.startsWith("release-scope#") && isReleaseRow(row)) return {
|
|
108
|
-
pk,
|
|
109
|
-
sk,
|
|
110
|
-
version,
|
|
111
|
-
row
|
|
112
|
-
};
|
|
113
|
-
if (pk === "release_catalogs" && isReleaseCatalogRow(row)) return {
|
|
114
|
-
pk,
|
|
115
|
-
sk,
|
|
116
|
-
version,
|
|
117
|
-
row
|
|
118
|
-
};
|
|
119
|
-
if (pk === "bundles" && isBundleRow(row)) {
|
|
120
|
-
if (typeof relationCount !== "number" || typeof ownedPatchCount !== "number") throw new DynamoDBStoredItemError();
|
|
121
|
-
return {
|
|
122
|
-
pk,
|
|
123
|
-
sk,
|
|
124
|
-
version,
|
|
125
|
-
relation_count: relationCount,
|
|
126
|
-
owned_patch_count: ownedPatchCount,
|
|
127
|
-
row
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
if (pk === "bundle_patches" && typeof gsi1pk === "string" && typeof gsi1sk === "string" && isPatchRow(row)) return {
|
|
131
|
-
pk,
|
|
132
|
-
sk,
|
|
133
|
-
version,
|
|
134
|
-
gsi1pk,
|
|
135
|
-
gsi1sk,
|
|
136
|
-
row
|
|
137
|
-
};
|
|
138
|
-
throw new DynamoDBStoredItemError();
|
|
139
|
-
};
|
|
140
|
-
const patchOwnerPartition = (bundleId) => `patch-owner#${bundleId}`;
|
|
141
|
-
const patchOrderKey = (row) => `${row.order_index.toString().padStart(10, "0")}#${row.id}`;
|
|
142
|
-
const toDynamoDBBundleItem = (row, version = 1, relationCount = 0, ownedPatchCount = 0) => ({
|
|
143
|
-
pk: "bundles",
|
|
144
|
-
sk: row.id,
|
|
145
|
-
version,
|
|
146
|
-
relation_count: relationCount,
|
|
147
|
-
owned_patch_count: ownedPatchCount,
|
|
148
|
-
row
|
|
149
|
-
});
|
|
150
|
-
const toDynamoDBPatchItem = (row, version = 1) => {
|
|
151
|
-
const orderKey = patchOrderKey(row);
|
|
40
|
+
/**
|
|
41
|
+
* The update-check routes' CloudFront invalidation: `invalidate` purges their
|
|
42
|
+
* cached copies and `destroy` releases the client; undefined without a
|
|
43
|
+
* distribution. A failed invalidation only warns, so the write that changed
|
|
44
|
+
* the routes still succeeds.
|
|
45
|
+
*/
|
|
46
|
+
const createUpdateRouteInvalidation = ({ apiBasePath = "/release-catalogs", cloudfrontDistributionId, shouldWaitForInvalidation = false, credentials, region }) => {
|
|
47
|
+
if (!cloudfrontDistributionId) return void 0;
|
|
48
|
+
const client = new CloudFrontClient({
|
|
49
|
+
credentials,
|
|
50
|
+
region
|
|
51
|
+
});
|
|
152
52
|
return {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
};
|
|
161
|
-
const toDynamoDBChannelItem = (row, version = 1, referenceCount = 0) => ({
|
|
162
|
-
pk: DYNAMODB_CHANNEL_PARTITION,
|
|
163
|
-
sk: row.id,
|
|
164
|
-
version,
|
|
165
|
-
reference_count: referenceCount,
|
|
166
|
-
row
|
|
167
|
-
});
|
|
168
|
-
const releaseScopePartition = (scopeKey) => `release-scope#${scopeKey}`;
|
|
169
|
-
const toDynamoDBReleaseItem = (row, version = 1) => ({
|
|
170
|
-
pk: releaseScopePartition(row.scope_key),
|
|
171
|
-
sk: row.id,
|
|
172
|
-
version,
|
|
173
|
-
row
|
|
174
|
-
});
|
|
175
|
-
const toDynamoDBReleaseCatalogItem = (row, version = 1) => ({
|
|
176
|
-
pk: "release_catalogs",
|
|
177
|
-
sk: row.scope_key,
|
|
178
|
-
version,
|
|
179
|
-
row
|
|
180
|
-
});
|
|
181
|
-
const itemKey = (model, id) => ({
|
|
182
|
-
pk: model,
|
|
183
|
-
sk: id
|
|
184
|
-
});
|
|
185
|
-
const exactDynamoDBId = (where) => {
|
|
186
|
-
if (where?.length !== 1) return void 0;
|
|
187
|
-
const condition = where[0];
|
|
188
|
-
if (Reflect.get(condition, "field") !== "id" || (Reflect.get(condition, "operator") ?? "eq") !== "eq" || Reflect.get(condition, "mode") === "insensitive") return;
|
|
189
|
-
const value = Reflect.get(condition, "value");
|
|
190
|
-
return typeof value === "string" ? value : void 0;
|
|
191
|
-
};
|
|
192
|
-
const exactDynamoDBPatchOwner = (where) => {
|
|
193
|
-
const owners = exactDynamoDBPatchOwners(where);
|
|
194
|
-
return owners?.length === 1 ? owners[0] : void 0;
|
|
195
|
-
};
|
|
196
|
-
const exactDynamoDBPatchOwners = (where) => {
|
|
197
|
-
if (!where || where.some((condition) => Reflect.get(condition, "connector") === "OR")) return;
|
|
198
|
-
const ownerConditions = where.filter((condition) => Reflect.get(condition, "field") === "bundle_id");
|
|
199
|
-
if (ownerConditions.length !== 1) return void 0;
|
|
200
|
-
const condition = ownerConditions[0];
|
|
201
|
-
if (Reflect.get(condition, "mode") === "insensitive") return;
|
|
202
|
-
const operator = Reflect.get(condition, "operator") ?? "eq";
|
|
203
|
-
const value = Reflect.get(condition, "value");
|
|
204
|
-
if (operator === "eq") return typeof value === "string" ? [value] : void 0;
|
|
205
|
-
if (operator === "in" && Array.isArray(value) && value.every((id) => typeof id === "string")) return value;
|
|
206
|
-
};
|
|
207
|
-
const exactDynamoDBBundleIds = (where) => {
|
|
208
|
-
if (!where || where.some((condition) => Reflect.get(condition, "connector") === "OR")) return;
|
|
209
|
-
for (const condition of where) {
|
|
210
|
-
if (Reflect.get(condition, "field") !== "id" || Reflect.get(condition, "mode") === "insensitive") continue;
|
|
211
|
-
const operator = Reflect.get(condition, "operator") ?? "eq";
|
|
212
|
-
const value = Reflect.get(condition, "value");
|
|
213
|
-
if (operator === "eq" && typeof value === "string") return [value];
|
|
214
|
-
if (operator === "in" && Array.isArray(value) && value.every((id) => typeof id === "string")) return value;
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
const dynamoDBBundlePageStart = (where, direction) => {
|
|
218
|
-
if ((where ?? []).some((condition) => Reflect.get(condition, "connector") === "OR")) return;
|
|
219
|
-
let result;
|
|
220
|
-
for (const condition of where ?? []) {
|
|
221
|
-
if (Reflect.get(condition, "field") !== "id") continue;
|
|
222
|
-
const operator = Reflect.get(condition, "operator") ?? "eq";
|
|
223
|
-
const value = Reflect.get(condition, "value");
|
|
224
|
-
if (!(direction === "asc" ? operator === "gt" || operator === "gte" : operator === "lt" || operator === "lte") || typeof value !== "string") continue;
|
|
225
|
-
const inclusive = operator === "gte" || operator === "lte";
|
|
226
|
-
if (result === void 0 || (direction === "asc" ? value > result.id : value < result.id) || value === result.id && !inclusive) result = {
|
|
227
|
-
id: value,
|
|
228
|
-
inclusive
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
return result;
|
|
232
|
-
};
|
|
233
|
-
const parseStoredItem = (item) => parseDynamoDBItem(item.pk === "release_catalogs" ? boundedDynamoDBCatalogItem(item) : boundedDynamoDBMetadataItem(item));
|
|
234
|
-
const queryItems = async (store, input) => {
|
|
235
|
-
let exclusiveStartKey;
|
|
236
|
-
const items = [];
|
|
237
|
-
do {
|
|
238
|
-
const sortCondition = input.minimumSortKey ? " AND #sortKey >= :minimumSortKey" : "";
|
|
239
|
-
const page = await store.client.send(new QueryCommand({
|
|
240
|
-
TableName: store.tableName,
|
|
241
|
-
...input.indexName ? { IndexName: input.indexName } : {},
|
|
242
|
-
...input.consistentRead ? { ConsistentRead: true } : {},
|
|
243
|
-
ExclusiveStartKey: exclusiveStartKey,
|
|
244
|
-
KeyConditionExpression: `#partitionKey = :partition${sortCondition}`,
|
|
245
|
-
ExpressionAttributeNames: {
|
|
246
|
-
"#partitionKey": input.indexName ? "gsi1pk" : "pk",
|
|
247
|
-
...input.minimumSortKey ? { "#sortKey": input.indexName ? "gsi1sk" : "sk" } : {}
|
|
248
|
-
},
|
|
249
|
-
ExpressionAttributeValues: {
|
|
250
|
-
":partition": input.partition,
|
|
251
|
-
...input.minimumSortKey ? { ":minimumSortKey": input.minimumSortKey } : {}
|
|
53
|
+
invalidate: async () => {
|
|
54
|
+
try {
|
|
55
|
+
await invalidateCloudFront(client, cloudfrontDistributionId, [`${apiBasePath.replace(/\/+$/, "")}/*`], { shouldWait: shouldWaitForInvalidation });
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.warn("[hot-updater/aws] CloudFront invalidation failed; continuing without cache invalidation.", {
|
|
58
|
+
distributionId: cloudfrontDistributionId,
|
|
59
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
60
|
+
});
|
|
252
61
|
}
|
|
253
|
-
}));
|
|
254
|
-
const pageItems = (page.Items ?? []).map(parseStoredItem);
|
|
255
|
-
items.push(...pageItems);
|
|
256
|
-
exclusiveStartKey = page.LastEvaluatedKey;
|
|
257
|
-
} while (exclusiveStartKey !== void 0);
|
|
258
|
-
return items;
|
|
259
|
-
};
|
|
260
|
-
const queryBundleItemsPage = async (store, input) => {
|
|
261
|
-
if (input.limit === 0) return [];
|
|
262
|
-
let exclusiveStartKey;
|
|
263
|
-
const items = [];
|
|
264
|
-
do {
|
|
265
|
-
const startOperator = input.start?.inclusive ? input.direction === "asc" ? ">=" : "<=" : input.direction === "asc" ? ">" : "<";
|
|
266
|
-
const page = await store.client.send(new QueryCommand({
|
|
267
|
-
TableName: store.tableName,
|
|
268
|
-
ExclusiveStartKey: exclusiveStartKey,
|
|
269
|
-
KeyConditionExpression: input.start ? `#pk = :pk AND #sk ${startOperator} :start` : "#pk = :pk",
|
|
270
|
-
ExpressionAttributeNames: {
|
|
271
|
-
"#pk": "pk",
|
|
272
|
-
...input.start ? { "#sk": "sk" } : {}
|
|
273
|
-
},
|
|
274
|
-
ExpressionAttributeValues: {
|
|
275
|
-
":pk": "bundles",
|
|
276
|
-
...input.start ? { ":start": input.start.id } : {}
|
|
277
|
-
},
|
|
278
|
-
Limit: Math.max(input.limit - items.length, 100),
|
|
279
|
-
ScanIndexForward: input.direction === "asc"
|
|
280
|
-
}));
|
|
281
|
-
for (const item of (page.Items ?? []).map(parseStoredItem)) {
|
|
282
|
-
if (item.pk === "bundles" && input.matches(item.row)) items.push(item);
|
|
283
|
-
if (items.length === input.limit) return items;
|
|
284
|
-
}
|
|
285
|
-
exclusiveStartKey = page.LastEvaluatedKey;
|
|
286
|
-
} while (exclusiveStartKey !== void 0);
|
|
287
|
-
return items;
|
|
288
|
-
};
|
|
289
|
-
const loadModelItems = (store, model) => queryItems(store, {
|
|
290
|
-
partition: model,
|
|
291
|
-
consistentRead: true
|
|
292
|
-
});
|
|
293
|
-
const loadModelItem = async (store, model, id) => {
|
|
294
|
-
const { Item } = await store.client.send(new GetCommand({
|
|
295
|
-
TableName: store.tableName,
|
|
296
|
-
Key: itemKey(model, id),
|
|
297
|
-
ConsistentRead: true
|
|
298
|
-
}));
|
|
299
|
-
return Item === void 0 ? void 0 : parseStoredItem(Item);
|
|
300
|
-
};
|
|
301
|
-
const loadBundleItem = async (store, id) => {
|
|
302
|
-
const item = await loadModelItem(store, "bundles", id);
|
|
303
|
-
return item?.pk === "bundles" ? item : void 0;
|
|
304
|
-
};
|
|
305
|
-
const loadPatchItem = async (store, id) => {
|
|
306
|
-
const item = await loadModelItem(store, "bundle_patches", id);
|
|
307
|
-
return item?.pk === "bundle_patches" ? item : void 0;
|
|
308
|
-
};
|
|
309
|
-
const loadChannelItem = async (store, id) => {
|
|
310
|
-
const item = await loadModelItem(store, DYNAMODB_CHANNEL_PARTITION, id);
|
|
311
|
-
return item?.pk === "channels" ? item : void 0;
|
|
312
|
-
};
|
|
313
|
-
const loadBundleItems = async (store) => {
|
|
314
|
-
return (await loadModelItems(store, "bundles")).filter((item) => item.pk === "bundles");
|
|
315
|
-
};
|
|
316
|
-
const loadPatchItems = async (store) => {
|
|
317
|
-
return (await loadModelItems(store, "bundle_patches")).filter((item) => item.pk === "bundle_patches");
|
|
318
|
-
};
|
|
319
|
-
const loadChannelItems = async (store) => {
|
|
320
|
-
return (await loadModelItems(store, DYNAMODB_CHANNEL_PARTITION)).filter((item) => item.pk === DYNAMODB_CHANNEL_PARTITION);
|
|
321
|
-
};
|
|
322
|
-
const DYNAMODB_RELEASE_ID_PARTITION = "_hot-updater#release-scope-by-id";
|
|
323
|
-
const releaseLocatorItem = (row) => ({
|
|
324
|
-
pk: DYNAMODB_RELEASE_ID_PARTITION,
|
|
325
|
-
sk: row.id,
|
|
326
|
-
scope_key: row.scope_key
|
|
327
|
-
});
|
|
328
|
-
const loadReleaseItem = async (store, id) => {
|
|
329
|
-
const locator = await store.client.send(new GetCommand({
|
|
330
|
-
TableName: store.tableName,
|
|
331
|
-
Key: {
|
|
332
|
-
pk: DYNAMODB_RELEASE_ID_PARTITION,
|
|
333
|
-
sk: id
|
|
334
62
|
},
|
|
335
|
-
|
|
336
|
-
}));
|
|
337
|
-
if (locator.Item === void 0) return void 0;
|
|
338
|
-
const scopeKey = locator.Item.scope_key;
|
|
339
|
-
if (typeof scopeKey !== "string") throw new DynamoDBStoredItemError();
|
|
340
|
-
const result = await store.client.send(new GetCommand({
|
|
341
|
-
TableName: store.tableName,
|
|
342
|
-
Key: {
|
|
343
|
-
pk: releaseScopePartition(scopeKey),
|
|
344
|
-
sk: id
|
|
345
|
-
},
|
|
346
|
-
ConsistentRead: true
|
|
347
|
-
}));
|
|
348
|
-
if (result.Item === void 0) throw new DynamoDBStoredItemError();
|
|
349
|
-
const item = parseStoredItem(result.Item);
|
|
350
|
-
if (!isReleaseRow(item.row) || item.sk !== id) throw new DynamoDBStoredItemError();
|
|
351
|
-
return item;
|
|
352
|
-
};
|
|
353
|
-
const loadReleaseItemsByScope = async (store, scopeKey) => (await queryItems(store, {
|
|
354
|
-
partition: releaseScopePartition(scopeKey),
|
|
355
|
-
consistentRead: true
|
|
356
|
-
})).filter((item) => item.pk === releaseScopePartition(scopeKey) && isReleaseRow(item.row));
|
|
357
|
-
const loadReleaseItems = async (store) => {
|
|
358
|
-
let exclusiveStartKey;
|
|
359
|
-
const items = [];
|
|
360
|
-
do {
|
|
361
|
-
const page = await store.client.send(new ScanCommand({
|
|
362
|
-
TableName: store.tableName,
|
|
363
|
-
ExclusiveStartKey: exclusiveStartKey,
|
|
364
|
-
ConsistentRead: true,
|
|
365
|
-
FilterExpression: "begins_with(#pk, :prefix)",
|
|
366
|
-
ExpressionAttributeNames: { "#pk": "pk" },
|
|
367
|
-
ExpressionAttributeValues: { ":prefix": "release-scope#" }
|
|
368
|
-
}));
|
|
369
|
-
for (const value of page.Items ?? []) {
|
|
370
|
-
const item = parseStoredItem(value);
|
|
371
|
-
if (isReleaseRow(item.row)) items.push(item);
|
|
372
|
-
}
|
|
373
|
-
exclusiveStartKey = page.LastEvaluatedKey;
|
|
374
|
-
} while (exclusiveStartKey !== void 0);
|
|
375
|
-
return items;
|
|
376
|
-
};
|
|
377
|
-
const loadReleaseCatalogItem = async (store, scopeKey) => {
|
|
378
|
-
const result = await store.client.send(new GetCommand({
|
|
379
|
-
TableName: store.tableName,
|
|
380
|
-
Key: itemKey("release_catalogs", scopeKey),
|
|
381
|
-
ConsistentRead: true
|
|
382
|
-
}));
|
|
383
|
-
if (result.Item === void 0) return void 0;
|
|
384
|
-
const item = parseStoredItem(result.Item);
|
|
385
|
-
return item.pk === "release_catalogs" ? item : void 0;
|
|
386
|
-
};
|
|
387
|
-
const loadReleaseCatalogItems = async (store) => (await queryItems(store, {
|
|
388
|
-
partition: "release_catalogs",
|
|
389
|
-
consistentRead: true
|
|
390
|
-
})).filter((item) => item.pk === "release_catalogs");
|
|
391
|
-
const loadChannelByName = async (store, name) => {
|
|
392
|
-
const { Item } = await store.client.send(new GetCommand({
|
|
393
|
-
TableName: store.tableName,
|
|
394
|
-
Key: {
|
|
395
|
-
pk: DYNAMODB_CHANNEL_NAME_PARTITION,
|
|
396
|
-
sk: name
|
|
397
|
-
},
|
|
398
|
-
ConsistentRead: true
|
|
399
|
-
}));
|
|
400
|
-
const id = Item?.channel_id;
|
|
401
|
-
if (Item === void 0) return void 0;
|
|
402
|
-
if (typeof id !== "string") throw new DynamoDBStoredItemError();
|
|
403
|
-
const channel = await loadChannelItem(store, id);
|
|
404
|
-
if (channel === void 0 || channel.row.name !== name) throw new DynamoDBStoredItemError();
|
|
405
|
-
return channel;
|
|
406
|
-
};
|
|
407
|
-
const insertDynamoDBChannel = async (store, input) => {
|
|
408
|
-
const existing = await loadChannelByName(store, input.row.name);
|
|
409
|
-
if (existing !== void 0) return {
|
|
410
|
-
row: existing.row,
|
|
411
|
-
inserted: false
|
|
63
|
+
destroy: () => client.destroy()
|
|
412
64
|
};
|
|
413
|
-
const existingId = await loadChannelItem(store, input.row.id);
|
|
414
|
-
if (existingId !== void 0) {
|
|
415
|
-
if (existingId.row.name === input.row.name) return {
|
|
416
|
-
row: existingId.row,
|
|
417
|
-
inserted: false
|
|
418
|
-
};
|
|
419
|
-
throw new DynamoDBStoredItemError();
|
|
420
|
-
}
|
|
421
|
-
try {
|
|
422
|
-
await commitDynamoDBTransaction(store, [{ Put: {
|
|
423
|
-
TableName: store.tableName,
|
|
424
|
-
Item: boundedDynamoDBMetadataItem(toDynamoDBChannelItem(input.row)),
|
|
425
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
426
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
427
|
-
} }, { Put: {
|
|
428
|
-
TableName: store.tableName,
|
|
429
|
-
Item: {
|
|
430
|
-
pk: DYNAMODB_CHANNEL_NAME_PARTITION,
|
|
431
|
-
sk: input.row.name,
|
|
432
|
-
channel_id: input.row.id
|
|
433
|
-
},
|
|
434
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
435
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
436
|
-
} }]);
|
|
437
|
-
return {
|
|
438
|
-
row: input.row,
|
|
439
|
-
inserted: true
|
|
440
|
-
};
|
|
441
|
-
} catch (error) {
|
|
442
|
-
const canonical = await loadChannelByName(store, input.row.name);
|
|
443
|
-
if (canonical !== void 0) return {
|
|
444
|
-
row: canonical.row,
|
|
445
|
-
inserted: false
|
|
446
|
-
};
|
|
447
|
-
throw error;
|
|
448
|
-
}
|
|
449
65
|
};
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
deleted: false,
|
|
492
|
-
reason: "not_found"
|
|
493
|
-
};
|
|
494
|
-
throw error;
|
|
495
|
-
}
|
|
496
|
-
};
|
|
497
|
-
const queryOwnerPatchIds = async (store, indexName, bundleId) => {
|
|
498
|
-
return (await queryItems(store, {
|
|
499
|
-
partition: patchOwnerPartition(bundleId),
|
|
500
|
-
indexName
|
|
501
|
-
})).filter((item) => item.pk === "bundle_patches").map(({ sk }) => sk);
|
|
502
|
-
};
|
|
503
|
-
var DynamoDBTransactionLimitError = class extends Error {
|
|
504
|
-
actionCount;
|
|
505
|
-
name = "DynamoDBTransactionLimitError";
|
|
506
|
-
constructor(actionCount) {
|
|
507
|
-
super(`DynamoDB transaction requires ${actionCount} actions; maximum is 100`);
|
|
508
|
-
this.actionCount = actionCount;
|
|
509
|
-
}
|
|
510
|
-
};
|
|
511
|
-
const metadataUpdate = (store, delta) => {
|
|
512
|
-
const entries = Object.entries(delta).filter(([, value]) => value !== 0);
|
|
513
|
-
if (entries.length === 0) return void 0;
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/dynamoDBStore.ts
|
|
68
|
+
/** DynamoDB's limits: 100 items and 4 MB a transaction, 400 KB an item, and 2,048- and 1,024-byte keys. */
|
|
69
|
+
const DYNAMODB_LIMITS = {
|
|
70
|
+
items: 100,
|
|
71
|
+
bytes: 4e6,
|
|
72
|
+
itemBytes: 4e5,
|
|
73
|
+
keyBytes: {
|
|
74
|
+
pk: 2048,
|
|
75
|
+
sk: 1024
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
/** BatchGetItem takes 100 keys a call, and BatchWriteItem 25 requests. */
|
|
79
|
+
const GET_BATCH = 100;
|
|
80
|
+
const WRITE_BATCH = 25;
|
|
81
|
+
/**
|
|
82
|
+
* The table's TTL attribute: epoch seconds on every item of a row that
|
|
83
|
+
* expires, which DynamoDB deletes for free. No column is named like it.
|
|
84
|
+
*/
|
|
85
|
+
const DYNAMODB_TTL_ATTRIBUTE = "_ttl";
|
|
86
|
+
/** A column named like a key attribute is stored with a leading dot, which no column name has. */
|
|
87
|
+
const attribute = (column) => column === "pk" || column === "sk" ? `.${column}` : column;
|
|
88
|
+
const renamed = (record, rename) => Object.fromEntries(Object.entries(record).map(([name, value]) => [rename(name), value]));
|
|
89
|
+
const ttl = (expiresAt) => expiresAt === void 0 ? {} : { _ttl: Math.ceil(expiresAt / 1e3) };
|
|
90
|
+
const toItem = (key, row, expiresAt) => ({
|
|
91
|
+
...renamed({
|
|
92
|
+
...row,
|
|
93
|
+
...ttl(expiresAt)
|
|
94
|
+
}, attribute),
|
|
95
|
+
pk: key.pk,
|
|
96
|
+
sk: key.sk
|
|
97
|
+
});
|
|
98
|
+
const toRow = ({ pk: _pk, sk: _sk, _ttl, ...item }) => renamed(item, (name) => name.replace(/^\./, ""));
|
|
99
|
+
const idOf = ({ pk, sk }) => JSON.stringify([pk, sk]);
|
|
100
|
+
const nameOf = (error) => error.name;
|
|
101
|
+
const sleep = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
102
|
+
/**
|
|
103
|
+
* One transaction item. Its expressions bind every column name and value, so
|
|
104
|
+
* no column clashes with a reserved word; `pk` and `sk` are none.
|
|
105
|
+
*/
|
|
106
|
+
const toTransactItem = (tableName, op) => {
|
|
514
107
|
const names = {};
|
|
515
108
|
const values = {};
|
|
516
|
-
const
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
const valueName = model === "bundles" ? "bundle" : "patch";
|
|
521
|
-
names[`#${name}`] = name;
|
|
522
|
-
values[`:${valueName}Delta`] = change;
|
|
523
|
-
updates.push(`#${name} :${valueName}Delta`);
|
|
524
|
-
if (change < 0) {
|
|
525
|
-
values[`:${valueName}Removal`] = -change;
|
|
526
|
-
conditions.push(`#${name} >= :${valueName}Removal`);
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
return { Update: {
|
|
530
|
-
TableName: store.tableName,
|
|
531
|
-
Key: {
|
|
532
|
-
pk: "_hot-updater",
|
|
533
|
-
sk: "limits.metadata"
|
|
534
|
-
},
|
|
535
|
-
UpdateExpression: `ADD ${updates.join(", ")}`,
|
|
536
|
-
...conditions.length > 0 ? { ConditionExpression: conditions.join(" AND ") } : {},
|
|
537
|
-
ExpressionAttributeNames: names,
|
|
538
|
-
ExpressionAttributeValues: values
|
|
539
|
-
} };
|
|
540
|
-
};
|
|
541
|
-
const updateBundleRelation = (store, bundleId, delta, ownedPatchDelta = 0) => {
|
|
542
|
-
const names = {
|
|
543
|
-
"#pk": "pk",
|
|
544
|
-
"#version": "version"
|
|
109
|
+
const binder = (map, prefix) => (entry) => {
|
|
110
|
+
const token = `${prefix}${Object.keys(map).length}`;
|
|
111
|
+
map[token] = entry;
|
|
112
|
+
return token;
|
|
545
113
|
};
|
|
546
|
-
const
|
|
547
|
-
const
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
condition += " AND #relationCount >= :relationRemoval";
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
if (ownedPatchDelta !== 0) {
|
|
560
|
-
names["#ownedPatchCount"] = "owned_patch_count";
|
|
561
|
-
values[":ownedPatchDelta"] = ownedPatchDelta;
|
|
562
|
-
additions.push("#ownedPatchCount :ownedPatchDelta");
|
|
563
|
-
if (ownedPatchDelta < 0) {
|
|
564
|
-
values[":ownedPatchRemoval"] = -ownedPatchDelta;
|
|
565
|
-
condition += " AND #ownedPatchCount >= :ownedPatchRemoval";
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
if (additions.length > 0) update += ` ADD ${additions.join(", ")}`;
|
|
569
|
-
return { Update: {
|
|
570
|
-
TableName: store.tableName,
|
|
571
|
-
Key: itemKey("bundles", bundleId),
|
|
572
|
-
ConditionExpression: condition,
|
|
573
|
-
ExpressionAttributeNames: names,
|
|
574
|
-
ExpressionAttributeValues: values,
|
|
575
|
-
UpdateExpression: update
|
|
576
|
-
} };
|
|
577
|
-
};
|
|
578
|
-
const commitDynamoDBTransaction = async (store, actions) => {
|
|
579
|
-
if (actions.length > 100) throw new DynamoDBTransactionLimitError(actions.length);
|
|
580
|
-
await store.client.send(new TransactWriteCommand({ TransactItems: [...actions] }));
|
|
581
|
-
};
|
|
582
|
-
const DYNAMODB_BATCH_GET_ATTEMPTS = 5;
|
|
583
|
-
const DYNAMODB_BATCH_GET_BASE_DELAY_MS = 25;
|
|
584
|
-
var DynamoDBBatchGetExhaustedError = class extends Error {
|
|
585
|
-
unprocessedKeyCount;
|
|
586
|
-
name = "DynamoDBBatchGetExhaustedError";
|
|
587
|
-
constructor(unprocessedKeyCount) {
|
|
588
|
-
super(`DynamoDB did not process ${unprocessedKeyCount} batch-get keys after ${DYNAMODB_BATCH_GET_ATTEMPTS} attempts`);
|
|
589
|
-
this.unprocessedKeyCount = unprocessedKeyCount;
|
|
590
|
-
}
|
|
591
|
-
};
|
|
592
|
-
const waitBeforeRetry = (attempt) => new Promise((resolve) => setTimeout(resolve, DYNAMODB_BATCH_GET_BASE_DELAY_MS * 2 ** attempt));
|
|
593
|
-
const batchGetDynamoDBItems = async (store, keys) => {
|
|
594
|
-
const items = [];
|
|
595
|
-
for (let offset = 0; offset < keys.length; offset += 100) {
|
|
596
|
-
let pending = keys.slice(offset, offset + 100);
|
|
597
|
-
for (let attempt = 0; attempt < DYNAMODB_BATCH_GET_ATTEMPTS && pending.length > 0; attempt++) {
|
|
598
|
-
const { Responses, UnprocessedKeys } = await store.client.send(new BatchGetCommand({ RequestItems: { [store.tableName]: {
|
|
599
|
-
ConsistentRead: true,
|
|
600
|
-
Keys: pending
|
|
601
|
-
} } }));
|
|
602
|
-
items.push(...Responses?.[store.tableName] ?? []);
|
|
603
|
-
pending = UnprocessedKeys?.[store.tableName]?.Keys ?? [];
|
|
604
|
-
if (pending.length > 0 && attempt + 1 < DYNAMODB_BATCH_GET_ATTEMPTS) await waitBeforeRetry(attempt);
|
|
605
|
-
}
|
|
606
|
-
if (pending.length > 0) throw new DynamoDBBatchGetExhaustedError(pending.length);
|
|
607
|
-
}
|
|
608
|
-
return items;
|
|
609
|
-
};
|
|
610
|
-
const parseKeyReadItem = (item) => parseDynamoDBItem(boundedDynamoDBMetadataItem(item));
|
|
611
|
-
const loadBundleItemsById = async (store, ids) => {
|
|
612
|
-
const uniqueIds = [...new Set(ids)];
|
|
613
|
-
if (uniqueIds.length === 0) return [];
|
|
614
|
-
return (await batchGetDynamoDBItems(store, uniqueIds.map((id) => itemKey("bundles", id)))).map(parseKeyReadItem).filter((item) => item.pk === "bundles");
|
|
615
|
-
};
|
|
616
|
-
const loadPatchItemsById = async (store, ids) => {
|
|
617
|
-
const uniqueIds = [...new Set(ids)];
|
|
618
|
-
if (uniqueIds.length === 0) return [];
|
|
619
|
-
return (await batchGetDynamoDBItems(store, uniqueIds.map((id) => itemKey("bundle_patches", id)))).map(parseKeyReadItem).filter((item) => item.pk === "bundle_patches");
|
|
620
|
-
};
|
|
621
|
-
const loadMetadataCount = async (store, model) => {
|
|
622
|
-
if (model !== "bundles" && model !== "bundle_patches") return void 0;
|
|
623
|
-
const field = model === "bundles" ? "bundles" : "patches";
|
|
624
|
-
const { Item } = await store.client.send(new GetCommand({
|
|
625
|
-
TableName: store.tableName,
|
|
626
|
-
Key: {
|
|
627
|
-
pk: "_hot-updater",
|
|
628
|
-
sk: "limits.metadata"
|
|
629
|
-
},
|
|
630
|
-
ConsistentRead: true,
|
|
631
|
-
ProjectionExpression: "#count",
|
|
632
|
-
ExpressionAttributeNames: { "#count": field }
|
|
633
|
-
}));
|
|
634
|
-
const count = Item?.[field];
|
|
635
|
-
return typeof count === "number" && Number.isSafeInteger(count) && count >= 0 ? count : void 0;
|
|
636
|
-
};
|
|
637
|
-
var DynamoDBPatchIndexConsistencyError = class extends Error {
|
|
638
|
-
bundleId;
|
|
639
|
-
expectedCount;
|
|
640
|
-
name = "DynamoDBPatchIndexConsistencyError";
|
|
641
|
-
constructor(bundleId, expectedCount) {
|
|
642
|
-
super(`DynamoDB patch index did not converge to ${expectedCount} rows for bundle "${bundleId}"`);
|
|
643
|
-
this.bundleId = bundleId;
|
|
644
|
-
this.expectedCount = expectedCount;
|
|
645
|
-
}
|
|
646
|
-
};
|
|
647
|
-
const waitForIndex = (attempt) => new Promise((resolve) => setTimeout(resolve, 20 * (attempt + 1)));
|
|
648
|
-
const queryCompleteOwnerPatches = async (store, indexName, bundleId) => {
|
|
649
|
-
const owner = await loadBundleItem(store, bundleId);
|
|
650
|
-
if (!owner || owner.owned_patch_count === 0) return [];
|
|
651
|
-
for (let attempt = 0; attempt < 3; attempt++) {
|
|
652
|
-
const patchIds = await queryOwnerPatchIds(store, indexName, bundleId);
|
|
653
|
-
const patchIdSet = new Set(patchIds);
|
|
654
|
-
const patches = (await loadPatchItemsById(store, patchIds)).filter(({ row }) => row.bundle_id === bundleId);
|
|
655
|
-
if (patchIds.length === owner.owned_patch_count && patchIdSet.size === owner.owned_patch_count && patches.length === owner.owned_patch_count && patches.every(({ sk }) => patchIdSet.has(sk))) return patches.map(({ row }) => row).sort((left, right) => left.order_index - right.order_index || left.id.localeCompare(right.id));
|
|
656
|
-
if (attempt < 2) await waitForIndex(attempt);
|
|
657
|
-
}
|
|
658
|
-
throw new DynamoDBPatchIndexConsistencyError(bundleId, owner.owned_patch_count);
|
|
659
|
-
};
|
|
660
|
-
const queryCompleteOwnersPatches = async (store, indexName, bundleIds) => {
|
|
661
|
-
const owners = (await loadBundleItemsById(store, bundleIds)).filter(({ owned_patch_count }) => owned_patch_count > 0);
|
|
662
|
-
const firstOwner = owners[0];
|
|
663
|
-
if (!firstOwner) return [];
|
|
664
|
-
let failedOwner = firstOwner;
|
|
665
|
-
for (let attempt = 0; attempt < 3; attempt++) {
|
|
666
|
-
const candidateIdsByOwner = await Promise.all(owners.map(({ sk }) => queryOwnerPatchIds(store, indexName, sk)));
|
|
667
|
-
const candidateIds = candidateIdsByOwner.flat();
|
|
668
|
-
const patches = await loadPatchItemsById(store, candidateIds);
|
|
669
|
-
const patchesById = new Map(patches.map((patch) => [patch.sk, patch]));
|
|
670
|
-
const mismatchedOwner = owners.find((owner, ownerIndex) => {
|
|
671
|
-
const ids = candidateIdsByOwner[ownerIndex] ?? [];
|
|
672
|
-
const uniqueIds = new Set(ids);
|
|
673
|
-
return ids.length !== owner.owned_patch_count || uniqueIds.size !== owner.owned_patch_count || ids.some((id) => patchesById.get(id)?.row.bundle_id !== owner.sk);
|
|
674
|
-
});
|
|
675
|
-
if (!mismatchedOwner) return patches.map(({ row }) => row);
|
|
676
|
-
failedOwner = mismatchedOwner;
|
|
677
|
-
if (attempt < 2) await waitForIndex(attempt);
|
|
678
|
-
}
|
|
679
|
-
throw new DynamoDBPatchIndexConsistencyError(failedOwner.sk, failedOwner.owned_patch_count);
|
|
680
|
-
};
|
|
681
|
-
const createDynamoDBBundlePatchTable = (store, indexName) => ({ findByBundleIds: (bundleIds) => bundleIds.length === 0 ? Promise.resolve([]) : queryCompleteOwnersPatches(store, indexName, bundleIds) });
|
|
682
|
-
const compare = (left, right) => {
|
|
683
|
-
if (typeof left === "number" && typeof right === "number") return left - right;
|
|
684
|
-
if (left == null) return right == null ? 0 : -1;
|
|
685
|
-
if (right == null) return 1;
|
|
686
|
-
return JSON.stringify(left).localeCompare(JSON.stringify(right));
|
|
687
|
-
};
|
|
688
|
-
const compareString = (actual, expected, mode, predicate) => {
|
|
689
|
-
if (typeof actual !== "string") return false;
|
|
690
|
-
return mode === "insensitive" ? predicate(actual.toLocaleLowerCase(), expected.toLocaleLowerCase()) : predicate(actual, expected);
|
|
691
|
-
};
|
|
692
|
-
const matchesCondition = (row, condition) => {
|
|
693
|
-
const actual = Reflect.get(row, condition.field);
|
|
694
|
-
const expected = Reflect.get(condition, "value");
|
|
695
|
-
switch (Reflect.get(condition, "operator") ?? "eq") {
|
|
696
|
-
case "eq": return typeof expected === "string" ? compareString(actual, expected, Reflect.get(condition, "mode"), (value, query) => value === query) : actual === expected;
|
|
697
|
-
case "ne":
|
|
698
|
-
if (actual == null) return false;
|
|
699
|
-
return typeof expected === "string" ? !compareString(actual, expected, Reflect.get(condition, "mode"), (value, query) => value === query) : actual !== expected;
|
|
700
|
-
case "gt": return actual != null && compare(actual, expected) > 0;
|
|
701
|
-
case "gte": return actual != null && compare(actual, expected) >= 0;
|
|
702
|
-
case "lt": return actual != null && compare(actual, expected) < 0;
|
|
703
|
-
case "lte": return actual != null && compare(actual, expected) <= 0;
|
|
704
|
-
case "in": return Array.isArray(expected) && expected.some((candidate) => candidate === actual);
|
|
705
|
-
case "not_in": return Array.isArray(expected) && (expected.length === 0 || actual != null && expected.every((candidate) => candidate !== actual));
|
|
706
|
-
case "contains": return typeof expected === "string" ? compareString(actual, expected, Reflect.get(condition, "mode"), (value, query) => value.includes(query)) : false;
|
|
707
|
-
case "starts_with": return typeof expected === "string" ? compareString(actual, expected, Reflect.get(condition, "mode"), (value, query) => value.startsWith(query)) : false;
|
|
708
|
-
case "ends_with": return typeof expected === "string" ? compareString(actual, expected, Reflect.get(condition, "mode"), (value, query) => value.endsWith(query)) : false;
|
|
709
|
-
default: return false;
|
|
710
|
-
}
|
|
711
|
-
};
|
|
712
|
-
const matchesDynamoDBWhere = (row, where) => {
|
|
713
|
-
if (!where || where.length === 0) return true;
|
|
714
|
-
let result = matchesCondition(row, where[0]);
|
|
715
|
-
for (const condition of where.slice(1)) {
|
|
716
|
-
const current = matchesCondition(row, condition);
|
|
717
|
-
result = condition.connector === "OR" ? result || current : result && current;
|
|
718
|
-
}
|
|
719
|
-
return result;
|
|
720
|
-
};
|
|
721
|
-
const compareRows = (left, right, orderBy) => {
|
|
722
|
-
for (const clause of orderBy) {
|
|
723
|
-
const leftValue = Reflect.get(left, clause.field);
|
|
724
|
-
const rightValue = Reflect.get(right, clause.field);
|
|
725
|
-
if (leftValue == null || rightValue == null) {
|
|
726
|
-
if (leftValue == null && rightValue == null) continue;
|
|
727
|
-
const nulls = clause.nulls ?? (clause.direction === "asc" ? "last" : "first");
|
|
728
|
-
const order = leftValue == null ? -1 : 1;
|
|
729
|
-
return nulls === "first" ? order : -order;
|
|
730
|
-
}
|
|
731
|
-
const order = compare(leftValue, rightValue);
|
|
732
|
-
if (order !== 0) return clause.direction === "asc" ? order : -order;
|
|
733
|
-
}
|
|
734
|
-
return 0;
|
|
735
|
-
};
|
|
736
|
-
const distinctKey = (row, distinctOn) => JSON.stringify(distinctOn.fields.map((field) => Reflect.get(row, field)));
|
|
737
|
-
const queryDynamoDBRows = (rows, input) => {
|
|
738
|
-
const filtered = rows.filter((row) => matchesDynamoDBWhere(row, input.where));
|
|
739
|
-
const orderBy = input.orderBy;
|
|
740
|
-
const ordered = orderBy ? filtered.toSorted((left, right) => compareRows(left, right, orderBy)) : filtered;
|
|
741
|
-
const distinctOn = input.distinctOn;
|
|
742
|
-
const distinct = distinctOn ? (() => {
|
|
743
|
-
const seen = /* @__PURE__ */ new Set();
|
|
744
|
-
return ordered.filter((row) => {
|
|
745
|
-
const key = distinctKey(row, distinctOn);
|
|
746
|
-
if (seen.has(key)) return false;
|
|
747
|
-
seen.add(key);
|
|
748
|
-
return true;
|
|
749
|
-
});
|
|
750
|
-
})() : ordered;
|
|
751
|
-
const offset = input.offset ?? 0;
|
|
752
|
-
return distinct.slice(offset, offset + (input.limit ?? 100));
|
|
753
|
-
};
|
|
754
|
-
const countDistinctDynamoDBRows = (rows, fields) => {
|
|
755
|
-
if (fields === void 0) return rows.length;
|
|
756
|
-
return new Set(rows.map((row) => JSON.stringify(fields.map((field) => Reflect.get(row, field))))).size;
|
|
757
|
-
};
|
|
758
|
-
const createDynamoDBBundle = async (store, row) => {
|
|
759
|
-
const counter = metadataUpdate(store, { bundles: 1 });
|
|
760
|
-
if (!counter) return;
|
|
761
|
-
await commitDynamoDBTransaction(store, [counter, { Put: {
|
|
762
|
-
TableName: store.tableName,
|
|
763
|
-
Item: boundedDynamoDBMetadataItem(toDynamoDBBundleItem(row)),
|
|
764
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
765
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
766
|
-
} }]);
|
|
767
|
-
};
|
|
768
|
-
const createDynamoDBPatch = async (store, row) => {
|
|
769
|
-
const bundleIds = [.../* @__PURE__ */ new Set([row.bundle_id, row.base_bundle_id])];
|
|
770
|
-
const counter = metadataUpdate(store, { bundle_patches: 1 });
|
|
771
|
-
if (!counter) return;
|
|
772
|
-
await commitDynamoDBTransaction(store, [
|
|
773
|
-
counter,
|
|
774
|
-
...bundleIds.map((bundleId) => updateBundleRelation(store, bundleId, 1, bundleId === row.bundle_id ? 1 : 0)),
|
|
775
|
-
{ Put: {
|
|
776
|
-
TableName: store.tableName,
|
|
777
|
-
Item: boundedDynamoDBMetadataItem(toDynamoDBPatchItem(row)),
|
|
778
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
779
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
780
|
-
} }
|
|
781
|
-
]);
|
|
782
|
-
};
|
|
783
|
-
const replaceDynamoDBBundle = async (store, current, row) => {
|
|
784
|
-
await store.client.send(new PutCommand({
|
|
785
|
-
TableName: store.tableName,
|
|
786
|
-
Item: boundedDynamoDBMetadataItem(toDynamoDBBundleItem(row, current.version + 1, current.relation_count, current.owned_patch_count)),
|
|
787
|
-
ConditionExpression: "#version = :currentVersion",
|
|
788
|
-
ExpressionAttributeNames: { "#version": "version" },
|
|
789
|
-
ExpressionAttributeValues: { ":currentVersion": current.version }
|
|
790
|
-
}));
|
|
791
|
-
};
|
|
792
|
-
const deleteAction = (store, item) => ({ Delete: {
|
|
793
|
-
TableName: store.tableName,
|
|
794
|
-
Key: {
|
|
795
|
-
pk: item.pk,
|
|
796
|
-
sk: item.sk
|
|
797
|
-
},
|
|
798
|
-
ConditionExpression: "#version = :currentVersion",
|
|
799
|
-
ExpressionAttributeNames: { "#version": "version" },
|
|
800
|
-
ExpressionAttributeValues: { ":currentVersion": item.version }
|
|
801
|
-
} });
|
|
802
|
-
const deleteDynamoDBPatch = async (store, item) => {
|
|
803
|
-
const counter = metadataUpdate(store, { bundle_patches: -1 });
|
|
804
|
-
if (!counter) return;
|
|
805
|
-
const bundleIds = [.../* @__PURE__ */ new Set([item.row.bundle_id, item.row.base_bundle_id])];
|
|
806
|
-
await commitDynamoDBTransaction(store, [
|
|
807
|
-
counter,
|
|
808
|
-
...bundleIds.map((id) => updateBundleRelation(store, id, -1, id === item.row.bundle_id ? -1 : 0)),
|
|
809
|
-
deleteAction(store, item)
|
|
810
|
-
]);
|
|
811
|
-
};
|
|
812
|
-
const deleteBundle = async (store, bundle, patches) => {
|
|
813
|
-
const counter = metadataUpdate(store, {
|
|
814
|
-
bundles: -1,
|
|
815
|
-
bundle_patches: -patches.length
|
|
114
|
+
const name = binder(names, "#n");
|
|
115
|
+
const value = binder(values, ":v");
|
|
116
|
+
const { condition } = op;
|
|
117
|
+
const guard = condition === void 0 ? void 0 : "v" in condition ? `${name("_v")} = ${value(condition.v)}` : `${condition.exists ? "attribute_exists" : "attribute_not_exists"}(pk)`;
|
|
118
|
+
const item = (fields) => ({
|
|
119
|
+
TableName: tableName,
|
|
120
|
+
ConditionExpression: guard,
|
|
121
|
+
...fields,
|
|
122
|
+
ExpressionAttributeNames: Object.keys(names).length ? names : void 0,
|
|
123
|
+
ExpressionAttributeValues: Object.keys(values).length ? values : void 0
|
|
816
124
|
});
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
if (id === bundle.sk) continue;
|
|
821
|
-
const current = otherEndpointCounts.get(id) ?? {
|
|
822
|
-
owned: 0,
|
|
823
|
-
relations: 0
|
|
824
|
-
};
|
|
825
|
-
otherEndpointCounts.set(id, {
|
|
826
|
-
owned: current.owned + (id === row.bundle_id ? 1 : 0),
|
|
827
|
-
relations: current.relations + 1
|
|
828
|
-
});
|
|
829
|
-
}
|
|
830
|
-
await commitDynamoDBTransaction(store, [
|
|
831
|
-
counter,
|
|
832
|
-
...[...otherEndpointCounts].map(([id, count]) => updateBundleRelation(store, id, -count.relations, -count.owned)),
|
|
833
|
-
...patches.map((patch) => deleteAction(store, patch)),
|
|
834
|
-
deleteAction(store, bundle)
|
|
835
|
-
]);
|
|
836
|
-
};
|
|
837
|
-
const deleteDynamoDBBundles = async (store, bundles, allPatches) => {
|
|
838
|
-
let patches = [...allPatches];
|
|
839
|
-
const versionIncrements = /* @__PURE__ */ new Map();
|
|
840
|
-
for (const originalBundle of bundles) {
|
|
841
|
-
const bundle = {
|
|
842
|
-
...originalBundle,
|
|
843
|
-
version: originalBundle.version + (versionIncrements.get(originalBundle.sk) ?? 0)
|
|
844
|
-
};
|
|
845
|
-
const related = patches.filter(({ row }) => row.bundle_id === bundle.sk || row.base_bundle_id === bundle.sk);
|
|
846
|
-
await deleteBundle(store, bundle, related);
|
|
847
|
-
const updatedBundleIds = new Set(related.flatMap(({ row }) => [row.bundle_id, row.base_bundle_id]));
|
|
848
|
-
updatedBundleIds.delete(bundle.sk);
|
|
849
|
-
for (const id of updatedBundleIds) versionIncrements.set(id, (versionIncrements.get(id) ?? 0) + 1);
|
|
850
|
-
const removedPatchIds = new Set(related.map(({ sk }) => sk));
|
|
851
|
-
patches = patches.filter(({ sk }) => !removedPatchIds.has(sk));
|
|
852
|
-
}
|
|
853
|
-
};
|
|
854
|
-
var DynamoDBUnsupportedModelError = class extends Error {
|
|
855
|
-
name = "DynamoDBUnsupportedModelError";
|
|
856
|
-
constructor() {
|
|
857
|
-
super("DynamoDB received an unsupported database model");
|
|
858
|
-
}
|
|
859
|
-
};
|
|
860
|
-
const distinctFields = (fields) => fields;
|
|
861
|
-
const exactDynamoDBField = (where, fieldName) => {
|
|
862
|
-
const conditions = (where ?? []).filter((condition) => Reflect.get(condition, "field") === fieldName);
|
|
863
|
-
if (conditions.length !== 1) return void 0;
|
|
864
|
-
const condition = conditions[0];
|
|
865
|
-
if ((Reflect.get(condition, "operator") ?? "eq") !== "eq" || Reflect.get(condition, "mode") === "insensitive") return;
|
|
866
|
-
const value = Reflect.get(condition, "value");
|
|
867
|
-
return typeof value === "string" ? value : void 0;
|
|
868
|
-
};
|
|
869
|
-
const createDynamoDBCrud = (store, updateIndexName) => ({
|
|
870
|
-
recordInsights: (input) => recordDynamoDBInsightsEvent(store, input),
|
|
871
|
-
async create(input) {
|
|
872
|
-
switch (input.model) {
|
|
873
|
-
case "bundles":
|
|
874
|
-
await createDynamoDBBundle(store, input.data);
|
|
875
|
-
return input.data;
|
|
876
|
-
case "bundle_patches":
|
|
877
|
-
await createDynamoDBPatch(store, input.data);
|
|
878
|
-
return input.data;
|
|
879
|
-
case "releases": {
|
|
880
|
-
const channel = await loadChannelItem(store, input.data.channel_id);
|
|
881
|
-
if (channel === void 0) throw new DynamoDBStoredItemError();
|
|
882
|
-
await commitDynamoDBTransaction(store, [
|
|
883
|
-
updateChannelReferenceCount(store, channel, 1),
|
|
884
|
-
{ Put: {
|
|
885
|
-
TableName: store.tableName,
|
|
886
|
-
Item: boundedDynamoDBMetadataItem(toDynamoDBReleaseItem(input.data)),
|
|
887
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
888
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
889
|
-
} },
|
|
890
|
-
{ Put: {
|
|
891
|
-
TableName: store.tableName,
|
|
892
|
-
Item: releaseLocatorItem(input.data),
|
|
893
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
894
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
895
|
-
} }
|
|
896
|
-
]);
|
|
897
|
-
return input.data;
|
|
898
|
-
}
|
|
899
|
-
case "release_catalogs":
|
|
900
|
-
await store.client.send(new PutCommand({
|
|
901
|
-
TableName: store.tableName,
|
|
902
|
-
Item: boundedDynamoDBCatalogItem(toDynamoDBReleaseCatalogItem(input.data)),
|
|
903
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
904
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
905
|
-
}));
|
|
906
|
-
return input.data;
|
|
907
|
-
case "channels": return (await insertDynamoDBChannel(store, {
|
|
908
|
-
row: input.data,
|
|
909
|
-
onConflict: "returnExisting"
|
|
910
|
-
})).row;
|
|
911
|
-
}
|
|
912
|
-
throw new DynamoDBUnsupportedModelError();
|
|
913
|
-
},
|
|
914
|
-
async update(input) {
|
|
915
|
-
if (input.model === "releases") {
|
|
916
|
-
const id = exactDynamoDBId(input.where);
|
|
917
|
-
if (id === void 0) throw new DynamoDBUnsupportedModelError();
|
|
918
|
-
const current = await loadReleaseItem(store, id);
|
|
919
|
-
if (current === void 0) return null;
|
|
920
|
-
const updated = {
|
|
921
|
-
...current.row,
|
|
922
|
-
...input.update
|
|
923
|
-
};
|
|
924
|
-
const nextPartition = releaseScopePartition(updated.scope_key);
|
|
925
|
-
const moved = current.pk !== nextPartition;
|
|
926
|
-
await commitDynamoDBTransaction(store, [
|
|
927
|
-
...moved ? [{ Delete: {
|
|
928
|
-
TableName: store.tableName,
|
|
929
|
-
Key: {
|
|
930
|
-
pk: current.pk,
|
|
931
|
-
sk: current.sk
|
|
932
|
-
},
|
|
933
|
-
ConditionExpression: "#version = :version",
|
|
934
|
-
ExpressionAttributeNames: { "#version": "version" },
|
|
935
|
-
ExpressionAttributeValues: { ":version": current.version }
|
|
936
|
-
} }] : [],
|
|
937
|
-
{ Put: {
|
|
938
|
-
TableName: store.tableName,
|
|
939
|
-
Item: boundedDynamoDBMetadataItem(toDynamoDBReleaseItem(updated, current.version + 1)),
|
|
940
|
-
ConditionExpression: moved ? "attribute_not_exists(#pk)" : "#version = :version",
|
|
941
|
-
ExpressionAttributeNames: moved ? { "#pk": "pk" } : { "#version": "version" },
|
|
942
|
-
...moved ? {} : { ExpressionAttributeValues: { ":version": current.version } }
|
|
943
|
-
} },
|
|
944
|
-
{ Put: {
|
|
945
|
-
TableName: store.tableName,
|
|
946
|
-
Item: releaseLocatorItem(updated),
|
|
947
|
-
ConditionExpression: "#scopeKey = :scopeKey",
|
|
948
|
-
ExpressionAttributeNames: { "#scopeKey": "scope_key" },
|
|
949
|
-
ExpressionAttributeValues: { ":scopeKey": current.row.scope_key }
|
|
950
|
-
} }
|
|
951
|
-
]);
|
|
952
|
-
return updated;
|
|
953
|
-
}
|
|
954
|
-
if (input.model === "release_catalogs") {
|
|
955
|
-
const scopeKey = exactDynamoDBField(input.where, "scope_key");
|
|
956
|
-
if (scopeKey === void 0) throw new DynamoDBUnsupportedModelError();
|
|
957
|
-
const current = await loadReleaseCatalogItem(store, scopeKey);
|
|
958
|
-
if (current === void 0) return null;
|
|
959
|
-
const updated = {
|
|
960
|
-
...current.row,
|
|
961
|
-
...input.update
|
|
962
|
-
};
|
|
963
|
-
await store.client.send(new PutCommand({
|
|
964
|
-
TableName: store.tableName,
|
|
965
|
-
Item: boundedDynamoDBCatalogItem(toDynamoDBReleaseCatalogItem(updated, current.version + 1)),
|
|
966
|
-
ConditionExpression: "#version = :version",
|
|
967
|
-
ExpressionAttributeNames: { "#version": "version" },
|
|
968
|
-
ExpressionAttributeValues: { ":version": current.version }
|
|
969
|
-
}));
|
|
970
|
-
return updated;
|
|
971
|
-
}
|
|
972
|
-
if (input.model !== "bundles") throw new DynamoDBUnsupportedModelError();
|
|
973
|
-
const id = exactDynamoDBId(input.where);
|
|
974
|
-
const current = id === void 0 ? (await loadBundleItems(store)).find(({ row }) => matchesDynamoDBWhere(row, input.where)) : await loadBundleItem(store, id);
|
|
975
|
-
if (!current) return null;
|
|
976
|
-
const updated = {
|
|
977
|
-
...current.row,
|
|
978
|
-
...input.update
|
|
979
|
-
};
|
|
980
|
-
await replaceDynamoDBBundle(store, current, updated);
|
|
981
|
-
return updated;
|
|
982
|
-
},
|
|
983
|
-
async delete(input) {
|
|
984
|
-
if (input.model === "channels") {
|
|
985
|
-
const id = exactDynamoDBId(input.where);
|
|
986
|
-
if (id !== void 0) await deleteDynamoDBChannel(store, { id });
|
|
987
|
-
return;
|
|
988
|
-
}
|
|
989
|
-
if (input.model === "bundle_patches") {
|
|
990
|
-
const items = (await loadPatchItems(store)).filter(({ row }) => matchesDynamoDBWhere(row, input.where));
|
|
991
|
-
for (const item of items) await deleteDynamoDBPatch(store, item);
|
|
992
|
-
return;
|
|
993
|
-
}
|
|
994
|
-
if (input.model === "releases") {
|
|
995
|
-
const items = (await loadReleaseItems(store)).filter(({ row }) => matchesDynamoDBWhere(row, input.where));
|
|
996
|
-
for (const item of items) {
|
|
997
|
-
const channel = await loadChannelItem(store, item.row.channel_id);
|
|
998
|
-
if (channel === void 0) throw new DynamoDBStoredItemError();
|
|
999
|
-
await commitDynamoDBTransaction(store, [
|
|
1000
|
-
updateChannelReferenceCount(store, channel, -1),
|
|
1001
|
-
deleteAction(store, item),
|
|
1002
|
-
{ Delete: {
|
|
1003
|
-
TableName: store.tableName,
|
|
1004
|
-
Key: {
|
|
1005
|
-
pk: DYNAMODB_RELEASE_ID_PARTITION,
|
|
1006
|
-
sk: item.sk
|
|
1007
|
-
},
|
|
1008
|
-
ConditionExpression: "#scopeKey = :scopeKey",
|
|
1009
|
-
ExpressionAttributeNames: { "#scopeKey": "scope_key" },
|
|
1010
|
-
ExpressionAttributeValues: { ":scopeKey": item.row.scope_key }
|
|
1011
|
-
} }
|
|
1012
|
-
]);
|
|
1013
|
-
}
|
|
1014
|
-
return;
|
|
1015
|
-
}
|
|
1016
|
-
if (input.model !== "bundles") throw new DynamoDBUnsupportedModelError();
|
|
1017
|
-
const bundleItems = (await loadBundleItems(store)).filter(({ row }) => matchesDynamoDBWhere(row, input.where));
|
|
1018
|
-
if (bundleItems.length === 0) return;
|
|
1019
|
-
await deleteDynamoDBBundles(store, bundleItems, await loadPatchItems(store));
|
|
1020
|
-
},
|
|
1021
|
-
async count(input) {
|
|
1022
|
-
if ((input.where === void 0 || input.where.length === 0) && input.distinct === void 0) {
|
|
1023
|
-
const count = await loadMetadataCount(store, input.model);
|
|
1024
|
-
if (count !== void 0) return count;
|
|
1025
|
-
}
|
|
1026
|
-
switch (input.model) {
|
|
1027
|
-
case "bundles": {
|
|
1028
|
-
const rows = (await loadBundleItems(store)).map(({ row }) => row).filter((row) => matchesDynamoDBWhere(row, input.where));
|
|
1029
|
-
return countDistinctDynamoDBRows(rows, distinctFields(input.distinct));
|
|
1030
|
-
}
|
|
1031
|
-
case "bundle_patches": {
|
|
1032
|
-
const ownerId = exactDynamoDBPatchOwner(input.where);
|
|
1033
|
-
const rows = (ownerId ? await queryCompleteOwnerPatches(store, updateIndexName, ownerId) : (await loadPatchItems(store)).map(({ row }) => row)).filter((row) => matchesDynamoDBWhere(row, input.where));
|
|
1034
|
-
return countDistinctDynamoDBRows(rows, distinctFields(input.distinct));
|
|
1035
|
-
}
|
|
1036
|
-
case "releases": {
|
|
1037
|
-
const scopeKey = exactDynamoDBField(input.where, "scope_key");
|
|
1038
|
-
const rows = (scopeKey === void 0 ? await loadReleaseItems(store) : await loadReleaseItemsByScope(store, scopeKey)).map(({ row }) => row).filter((row) => matchesDynamoDBWhere(row, input.where));
|
|
1039
|
-
return countDistinctDynamoDBRows(rows, distinctFields(input.distinct));
|
|
1040
|
-
}
|
|
1041
|
-
}
|
|
1042
|
-
throw new DynamoDBUnsupportedModelError();
|
|
1043
|
-
},
|
|
1044
|
-
async findOne(input) {
|
|
1045
|
-
const id = exactDynamoDBId(input.where);
|
|
1046
|
-
switch (input.model) {
|
|
1047
|
-
case "bundles":
|
|
1048
|
-
if (id !== void 0) return (await loadBundleItem(store, id))?.row ?? null;
|
|
1049
|
-
return (await loadBundleItems(store)).map(({ row }) => row).find((row) => matchesDynamoDBWhere(row, input.where)) ?? null;
|
|
1050
|
-
case "bundle_patches":
|
|
1051
|
-
if (id !== void 0) return (await loadPatchItem(store, id))?.row ?? null;
|
|
1052
|
-
{
|
|
1053
|
-
const ownerId = exactDynamoDBPatchOwner(input.where);
|
|
1054
|
-
if (ownerId === void 0) break;
|
|
1055
|
-
return (await queryCompleteOwnerPatches(store, updateIndexName, ownerId)).find((row) => matchesDynamoDBWhere(row, input.where)) ?? null;
|
|
1056
|
-
}
|
|
1057
|
-
case "channels":
|
|
1058
|
-
if (id !== void 0) return (await loadChannelItem(store, id))?.row ?? null;
|
|
1059
|
-
return (await loadChannelItems(store)).map(({ row }) => row).find((row) => matchesDynamoDBWhere(row, input.where)) ?? null;
|
|
1060
|
-
case "releases":
|
|
1061
|
-
if (id !== void 0) return (await loadReleaseItem(store, id))?.row ?? null;
|
|
1062
|
-
{
|
|
1063
|
-
const scopeKey = exactDynamoDBField(input.where, "scope_key");
|
|
1064
|
-
return (scopeKey === void 0 ? await loadReleaseItems(store) : await loadReleaseItemsByScope(store, scopeKey)).map(({ row }) => row).find((row) => matchesDynamoDBWhere(row, input.where)) ?? null;
|
|
1065
|
-
}
|
|
1066
|
-
case "release_catalogs": {
|
|
1067
|
-
const scopeKey = exactDynamoDBField(input.where, "scope_key");
|
|
1068
|
-
if (scopeKey !== void 0) return (await loadReleaseCatalogItem(store, scopeKey))?.row ?? null;
|
|
1069
|
-
return (await loadReleaseCatalogItems(store)).map(({ row }) => row).find((row) => matchesDynamoDBWhere(row, input.where)) ?? null;
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
if (input.model === "bundle_patches") return (await loadPatchItems(store)).map(({ row }) => row).find((row) => matchesDynamoDBWhere(row, input.where)) ?? null;
|
|
1073
|
-
throw new DynamoDBUnsupportedModelError();
|
|
1074
|
-
},
|
|
1075
|
-
async findMany(input) {
|
|
1076
|
-
if (input.limit === 0) return [];
|
|
1077
|
-
switch (input.model) {
|
|
1078
|
-
case "bundles": {
|
|
1079
|
-
const ids = exactDynamoDBBundleIds(input.where);
|
|
1080
|
-
if (ids !== void 0) return queryDynamoDBRows((await loadBundleItemsById(store, ids)).map(({ row }) => row), input);
|
|
1081
|
-
const orderBy = input.orderBy;
|
|
1082
|
-
const direction = orderBy?.[0]?.direction;
|
|
1083
|
-
if ((input.offset ?? 0) === 0 && input.distinctOn === void 0 && orderBy?.length === 1 && orderBy[0]?.field === "id" && (direction === "asc" || direction === "desc")) return (await queryBundleItemsPage(store, {
|
|
1084
|
-
direction,
|
|
1085
|
-
limit: input.limit ?? 100,
|
|
1086
|
-
matches: (row) => matchesDynamoDBWhere(row, input.where),
|
|
1087
|
-
start: dynamoDBBundlePageStart(input.where, direction)
|
|
1088
|
-
})).map(({ row }) => row);
|
|
1089
|
-
return queryDynamoDBRows((await loadBundleItems(store)).map(({ row }) => row), input);
|
|
1090
|
-
}
|
|
1091
|
-
case "bundle_patches": {
|
|
1092
|
-
const ownerIds = exactDynamoDBPatchOwners(input.where);
|
|
1093
|
-
if (ownerIds !== void 0) {
|
|
1094
|
-
const ownerId = ownerIds.length === 1 ? ownerIds[0] : void 0;
|
|
1095
|
-
return queryDynamoDBRows(ownerId !== void 0 ? await queryCompleteOwnerPatches(store, updateIndexName, ownerId) : await queryCompleteOwnersPatches(store, updateIndexName, ownerIds), input);
|
|
1096
|
-
}
|
|
1097
|
-
return queryDynamoDBRows((await loadPatchItems(store)).map(({ row }) => row), input);
|
|
1098
|
-
}
|
|
1099
|
-
case "channels": return queryDynamoDBRows((await loadChannelItems(store)).map(({ row }) => row), input);
|
|
1100
|
-
case "releases": {
|
|
1101
|
-
const scopeKey = exactDynamoDBField(input.where, "scope_key");
|
|
1102
|
-
return queryDynamoDBRows((scopeKey === void 0 ? await loadReleaseItems(store) : await loadReleaseItemsByScope(store, scopeKey)).map(({ row }) => row), input);
|
|
1103
|
-
}
|
|
1104
|
-
case "release_catalogs": return queryDynamoDBRows((await loadReleaseCatalogItems(store)).map(({ row }) => row), input);
|
|
1105
|
-
}
|
|
1106
|
-
throw new DynamoDBUnsupportedModelError();
|
|
1107
|
-
},
|
|
1108
|
-
insertChannel: (input) => insertDynamoDBChannel(store, input),
|
|
1109
|
-
deleteChannel: (input) => deleteDynamoDBChannel(store, input)
|
|
1110
|
-
});
|
|
1111
|
-
var DynamoDBDuplicatePatchError = class extends Error {
|
|
1112
|
-
patchId;
|
|
1113
|
-
name = "DynamoDBDuplicatePatchError";
|
|
1114
|
-
constructor(patchId) {
|
|
1115
|
-
super(`DynamoDB bundle mutation contains duplicate patch "${patchId}"`);
|
|
1116
|
-
this.patchId = patchId;
|
|
1117
|
-
}
|
|
1118
|
-
};
|
|
1119
|
-
const putNewBundle = (store, row, relationCount, ownedPatchCount = relationCount) => ({ Put: {
|
|
1120
|
-
TableName: store.tableName,
|
|
1121
|
-
Item: boundedDynamoDBMetadataItem(toDynamoDBBundleItem(row, 1, relationCount, ownedPatchCount)),
|
|
1122
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
1123
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
1124
|
-
} });
|
|
1125
|
-
const putUpdatedBundle = (store, current, row, relationCount, ownedPatchCount) => ({ Put: {
|
|
1126
|
-
TableName: store.tableName,
|
|
1127
|
-
Item: boundedDynamoDBMetadataItem(toDynamoDBBundleItem(row, current.version + 1, relationCount, ownedPatchCount)),
|
|
1128
|
-
ConditionExpression: "#version = :currentVersion",
|
|
1129
|
-
ExpressionAttributeNames: { "#version": "version" },
|
|
1130
|
-
ExpressionAttributeValues: { ":currentVersion": current.version }
|
|
1131
|
-
} });
|
|
1132
|
-
const putPatch = (store, row, current) => ({ Put: {
|
|
1133
|
-
TableName: store.tableName,
|
|
1134
|
-
Item: boundedDynamoDBMetadataItem(toDynamoDBPatchItem(row, (current?.version ?? 0) + 1)),
|
|
1135
|
-
ConditionExpression: current ? "#version = :currentVersion" : "attribute_not_exists(#pk)",
|
|
1136
|
-
ExpressionAttributeNames: current ? { "#version": "version" } : { "#pk": "pk" },
|
|
1137
|
-
...current ? { ExpressionAttributeValues: { ":currentVersion": current.version } } : {}
|
|
1138
|
-
} });
|
|
1139
|
-
const deletePatch = (store, item) => ({ Delete: {
|
|
1140
|
-
TableName: store.tableName,
|
|
1141
|
-
Key: itemKey(item.pk, item.sk),
|
|
1142
|
-
ConditionExpression: "#version = :currentVersion",
|
|
1143
|
-
ExpressionAttributeNames: { "#version": "version" },
|
|
1144
|
-
ExpressionAttributeValues: { ":currentVersion": item.version }
|
|
1145
|
-
} });
|
|
1146
|
-
var DynamoDBCommitStateError = class extends Error {
|
|
1147
|
-
name = "DynamoDBCommitStateError";
|
|
1148
|
-
constructor(message) {
|
|
1149
|
-
super(message);
|
|
1150
|
-
}
|
|
1151
|
-
};
|
|
1152
|
-
const rowsEqual = (left, right) => JSON.stringify(left) === JSON.stringify(right);
|
|
1153
|
-
const putChannel = (store, row, current, referenceCount) => ({ Put: {
|
|
1154
|
-
TableName: store.tableName,
|
|
1155
|
-
Item: boundedDynamoDBMetadataItem(toDynamoDBChannelItem(row, (current?.version ?? 0) + 1, referenceCount)),
|
|
1156
|
-
ConditionExpression: current ? "#version = :version" : "attribute_not_exists(#pk)",
|
|
1157
|
-
ExpressionAttributeNames: current ? { "#version": "version" } : { "#pk": "pk" },
|
|
1158
|
-
...current ? { ExpressionAttributeValues: { ":version": current.version } } : {}
|
|
1159
|
-
} });
|
|
1160
|
-
const deleteChannelActions = (store, current) => [{ Delete: {
|
|
1161
|
-
TableName: store.tableName,
|
|
1162
|
-
Key: itemKey(DYNAMODB_CHANNEL_PARTITION, current.sk),
|
|
1163
|
-
ConditionExpression: "#version = :version",
|
|
1164
|
-
ExpressionAttributeNames: { "#version": "version" },
|
|
1165
|
-
ExpressionAttributeValues: { ":version": current.version }
|
|
1166
|
-
} }, { Delete: {
|
|
1167
|
-
TableName: store.tableName,
|
|
1168
|
-
Key: {
|
|
1169
|
-
pk: DYNAMODB_CHANNEL_NAME_PARTITION,
|
|
1170
|
-
sk: current.row.name
|
|
1171
|
-
},
|
|
1172
|
-
ConditionExpression: "#channelId = :channelId",
|
|
1173
|
-
ExpressionAttributeNames: { "#channelId": "channel_id" },
|
|
1174
|
-
ExpressionAttributeValues: { ":channelId": current.sk }
|
|
1175
|
-
} }];
|
|
1176
|
-
const updateChannelReferenceCount = (store, current, delta) => ({ Update: {
|
|
1177
|
-
TableName: store.tableName,
|
|
1178
|
-
Key: itemKey(DYNAMODB_CHANNEL_PARTITION, current.sk),
|
|
1179
|
-
ConditionExpression: "#row.#name = :name" + (delta < 0 ? " AND #referenceCount >= :removal" : ""),
|
|
1180
|
-
UpdateExpression: "SET #version = #version + :one ADD #referenceCount :delta",
|
|
1181
|
-
ExpressionAttributeNames: {
|
|
1182
|
-
"#row": "row",
|
|
1183
|
-
"#name": "name",
|
|
1184
|
-
"#version": "version",
|
|
1185
|
-
"#referenceCount": "reference_count"
|
|
1186
|
-
},
|
|
1187
|
-
ExpressionAttributeValues: {
|
|
1188
|
-
":name": current.row.name,
|
|
1189
|
-
":one": 1,
|
|
1190
|
-
":delta": delta,
|
|
1191
|
-
...delta < 0 ? { ":removal": -delta } : {}
|
|
1192
|
-
}
|
|
1193
|
-
} });
|
|
1194
|
-
const loadApiKeyByHash = async (store, hash) => {
|
|
1195
|
-
const { Item } = await store.client.send(new GetCommand({
|
|
1196
|
-
TableName: store.tableName,
|
|
1197
|
-
Key: {
|
|
1198
|
-
pk: DYNAMODB_API_KEY_HASH_PARTITION,
|
|
1199
|
-
sk: hash
|
|
1200
|
-
},
|
|
1201
|
-
ConsistentRead: true
|
|
1202
|
-
}));
|
|
1203
|
-
if (Item === void 0) return null;
|
|
1204
|
-
const id = Item.api_key_id;
|
|
1205
|
-
if (typeof id !== "string") throw new DynamoDBStoredItemError();
|
|
1206
|
-
return loadApiKey(store, id);
|
|
1207
|
-
};
|
|
1208
|
-
const compileAndCommitDynamoDBChanges = async (store, input) => {
|
|
1209
|
-
const [originalBundleItems, originalPatchItems, originalChannelItems, originalReleaseItems, originalReleaseCatalogItems] = await Promise.all([
|
|
1210
|
-
loadBundleItems(store),
|
|
1211
|
-
loadPatchItems(store),
|
|
1212
|
-
loadChannelItems(store),
|
|
1213
|
-
loadReleaseItems(store),
|
|
1214
|
-
loadReleaseCatalogItems(store)
|
|
1215
|
-
]);
|
|
1216
|
-
const bundles = new Map(originalBundleItems.map(({ sk, row }) => [sk, row]));
|
|
1217
|
-
const patches = new Map(originalPatchItems.map(({ sk, row }) => [sk, row]));
|
|
1218
|
-
const channels = new Map(originalChannelItems.map(({ sk, row }) => [sk, row]));
|
|
1219
|
-
const channelNames = new Map(originalChannelItems.map(({ sk, row }) => [row.name, sk]));
|
|
1220
|
-
const releases = new Map(originalReleaseItems.map(({ sk, row }) => [sk, row]));
|
|
1221
|
-
const releaseCatalogs = new Map(originalReleaseCatalogItems.map(({ sk, row }) => [sk, row]));
|
|
1222
|
-
const originalApiKeys = /* @__PURE__ */ new Map();
|
|
1223
|
-
const apiKeys = /* @__PURE__ */ new Map();
|
|
1224
|
-
const apiKeyHashes = /* @__PURE__ */ new Map();
|
|
1225
|
-
const rememberApiKey = (value) => {
|
|
1226
|
-
if (value === null || apiKeys.has(value.row.id)) return;
|
|
1227
|
-
originalApiKeys.set(value.row.id, value);
|
|
1228
|
-
apiKeys.set(value.row.id, value);
|
|
1229
|
-
apiKeyHashes.set(value.row.hash, value.row.id);
|
|
1230
|
-
};
|
|
1231
|
-
const requireReleaseReferences = (row) => {
|
|
1232
|
-
if (!channels.has(row.channel_id)) throw new DynamoDBCommitStateError(`Release "${row.id}" references an invalid channel`);
|
|
1233
|
-
if (row.bundle_id !== null) {
|
|
1234
|
-
const bundle = bundles.get(row.bundle_id);
|
|
1235
|
-
if (bundle === void 0 || bundle.platform !== row.platform) throw new DynamoDBCommitStateError(`Release "${row.id}" references an invalid bundle`);
|
|
1236
|
-
}
|
|
125
|
+
const Key = {
|
|
126
|
+
pk: op.key.pk,
|
|
127
|
+
sk: op.key.sk
|
|
1237
128
|
};
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
reason: "version_conflict"
|
|
1250
|
-
}
|
|
1251
|
-
};
|
|
1252
|
-
}
|
|
1253
|
-
for (const [changeIndex, change] of input.changes.entries()) switch (change.model) {
|
|
1254
|
-
case "channels":
|
|
1255
|
-
if (change.operation === "insert") {
|
|
1256
|
-
if (channelNames.get(change.row.name) !== void 0) break;
|
|
1257
|
-
if (channels.get(change.row.id) !== void 0) throw new DynamoDBCommitStateError(`Channel id "${change.row.id}" already exists`);
|
|
1258
|
-
channels.set(change.row.id, change.row);
|
|
1259
|
-
channelNames.set(change.row.name, change.row.id);
|
|
1260
|
-
} else {
|
|
1261
|
-
const current = channels.get(change.where.id);
|
|
1262
|
-
if (current === void 0) break;
|
|
1263
|
-
if ([...releases.values()].some(({ channel_id }) => channel_id === change.where.id)) return {
|
|
1264
|
-
committed: false,
|
|
1265
|
-
conflict: {
|
|
1266
|
-
changeIndex,
|
|
1267
|
-
reason: "referenced"
|
|
1268
|
-
}
|
|
1269
|
-
};
|
|
1270
|
-
channels.delete(change.where.id);
|
|
1271
|
-
channelNames.delete(current.name);
|
|
1272
|
-
}
|
|
1273
|
-
break;
|
|
1274
|
-
case "bundles":
|
|
1275
|
-
if (change.operation === "insert") {
|
|
1276
|
-
if (bundles.has(change.row.id)) throw new DynamoDBCommitStateError(`Bundle "${change.row.id}" already exists`);
|
|
1277
|
-
bundles.set(change.row.id, change.row);
|
|
1278
|
-
} else {
|
|
1279
|
-
const current = bundles.get(change.where.id);
|
|
1280
|
-
if (current === void 0) {
|
|
1281
|
-
if (change.operation === "update") return {
|
|
1282
|
-
committed: false,
|
|
1283
|
-
conflict: {
|
|
1284
|
-
changeIndex,
|
|
1285
|
-
reason: "not_found"
|
|
1286
|
-
}
|
|
1287
|
-
};
|
|
1288
|
-
break;
|
|
1289
|
-
}
|
|
1290
|
-
if (change.operation === "update") {
|
|
1291
|
-
const row = {
|
|
1292
|
-
...current,
|
|
1293
|
-
...change.update
|
|
1294
|
-
};
|
|
1295
|
-
bundles.set(row.id, row);
|
|
1296
|
-
} else {
|
|
1297
|
-
if ([...releases.values()].some(({ bundle_id }) => bundle_id === change.where.id)) return {
|
|
1298
|
-
committed: false,
|
|
1299
|
-
conflict: {
|
|
1300
|
-
changeIndex,
|
|
1301
|
-
reason: "referenced"
|
|
1302
|
-
}
|
|
1303
|
-
};
|
|
1304
|
-
bundles.delete(change.where.id);
|
|
1305
|
-
for (const [id, patch] of patches) if (patch.bundle_id === change.where.id || patch.base_bundle_id === change.where.id) patches.delete(id);
|
|
1306
|
-
}
|
|
1307
|
-
}
|
|
1308
|
-
break;
|
|
1309
|
-
case "releases":
|
|
1310
|
-
if (change.operation === "insert") {
|
|
1311
|
-
requireReleaseReferences(change.row);
|
|
1312
|
-
if (releases.has(change.row.id)) throw new DynamoDBCommitStateError(`Release "${change.row.id}" already exists`);
|
|
1313
|
-
releases.set(change.row.id, change.row);
|
|
1314
|
-
} else {
|
|
1315
|
-
const current = releases.get(change.where.id);
|
|
1316
|
-
if (current === void 0) {
|
|
1317
|
-
if (change.operation === "update") return {
|
|
1318
|
-
committed: false,
|
|
1319
|
-
conflict: {
|
|
1320
|
-
changeIndex,
|
|
1321
|
-
reason: "not_found"
|
|
1322
|
-
}
|
|
1323
|
-
};
|
|
1324
|
-
break;
|
|
1325
|
-
}
|
|
1326
|
-
if (change.operation === "update") {
|
|
1327
|
-
const row = {
|
|
1328
|
-
...current,
|
|
1329
|
-
...change.update
|
|
1330
|
-
};
|
|
1331
|
-
requireReleaseReferences(row);
|
|
1332
|
-
releases.set(row.id, row);
|
|
1333
|
-
} else releases.delete(change.where.id);
|
|
1334
|
-
}
|
|
1335
|
-
break;
|
|
1336
|
-
case "releaseCatalogs":
|
|
1337
|
-
if (!channels.has(change.row.channel_id)) throw new DynamoDBCommitStateError(`Release catalog "${change.row.scope_key}" references an invalid channel`);
|
|
1338
|
-
releaseCatalogs.set(change.row.scope_key, change.row);
|
|
1339
|
-
break;
|
|
1340
|
-
case "bundlePatches":
|
|
1341
|
-
if (change.operation === "insert") {
|
|
1342
|
-
if (!bundles.has(change.row.bundle_id) || !bundles.has(change.row.base_bundle_id)) throw new DynamoDBCommitStateError(`Patch "${change.row.id}" references a missing bundle`);
|
|
1343
|
-
if (patches.has(change.row.id)) throw new DynamoDBDuplicatePatchError(change.row.id);
|
|
1344
|
-
patches.set(change.row.id, change.row);
|
|
1345
|
-
} else for (const [id, patch] of patches) if (patch.bundle_id === change.where.bundleId) patches.delete(id);
|
|
1346
|
-
break;
|
|
1347
|
-
case "apiKeys": if (change.operation === "insert") {
|
|
1348
|
-
rememberApiKey(await loadApiKeyByHash(store, change.row.hash));
|
|
1349
|
-
if (apiKeyHashes.has(change.row.hash)) break;
|
|
1350
|
-
rememberApiKey(await loadApiKey(store, change.row.id));
|
|
1351
|
-
if (apiKeys.has(change.row.id)) throw new DynamoDBCommitStateError(`API key id "${change.row.id}" already exists`);
|
|
1352
|
-
const inserted = {
|
|
1353
|
-
row: change.row,
|
|
1354
|
-
version: 1
|
|
129
|
+
switch (op.type) {
|
|
130
|
+
case "put": return { Put: item({ Item: toItem(op.key, op.value, op.expiresAt) }) };
|
|
131
|
+
case "delete": return { Delete: item({ Key }) };
|
|
132
|
+
case "check": return { ConditionCheck: item({
|
|
133
|
+
Key,
|
|
134
|
+
ConditionExpression: guard
|
|
135
|
+
}) };
|
|
136
|
+
case "add": {
|
|
137
|
+
const init = {
|
|
138
|
+
...op.init,
|
|
139
|
+
...ttl(op.expiresAt)
|
|
1355
140
|
};
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
}
|
|
1367
|
-
};
|
|
1368
|
-
apiKeys.set(change.where.id, {
|
|
1369
|
-
row: {
|
|
1370
|
-
...current.row,
|
|
1371
|
-
revoked_at_ms: change.update.revokedAtMs
|
|
1372
|
-
},
|
|
1373
|
-
version: current.version
|
|
1374
|
-
});
|
|
1375
|
-
}
|
|
1376
|
-
}
|
|
1377
|
-
const relationCounts = /* @__PURE__ */ new Map();
|
|
1378
|
-
const ownedPatchCounts = /* @__PURE__ */ new Map();
|
|
1379
|
-
for (const patch of patches.values()) {
|
|
1380
|
-
for (const id of /* @__PURE__ */ new Set([patch.bundle_id, patch.base_bundle_id])) relationCounts.set(id, (relationCounts.get(id) ?? 0) + 1);
|
|
1381
|
-
ownedPatchCounts.set(patch.bundle_id, (ownedPatchCounts.get(patch.bundle_id) ?? 0) + 1);
|
|
1382
|
-
}
|
|
1383
|
-
const channelReferenceCounts = /* @__PURE__ */ new Map();
|
|
1384
|
-
for (const release of releases.values()) channelReferenceCounts.set(release.channel_id, (channelReferenceCounts.get(release.channel_id) ?? 0) + 1);
|
|
1385
|
-
const actions = [];
|
|
1386
|
-
const originalChannels = new Map(originalChannelItems.map((item) => [item.sk, item]));
|
|
1387
|
-
for (const original of originalChannelItems) if (!channels.has(original.sk)) actions.push(...deleteChannelActions(store, original));
|
|
1388
|
-
for (const [id, row] of channels) {
|
|
1389
|
-
const original = originalChannels.get(id);
|
|
1390
|
-
const referenceCount = channelReferenceCounts.get(id) ?? 0;
|
|
1391
|
-
if (original === void 0) {
|
|
1392
|
-
actions.push(putChannel(store, row, original, referenceCount));
|
|
1393
|
-
actions.push({ Put: {
|
|
1394
|
-
TableName: store.tableName,
|
|
1395
|
-
Item: {
|
|
1396
|
-
pk: DYNAMODB_CHANNEL_NAME_PARTITION,
|
|
1397
|
-
sk: row.name,
|
|
1398
|
-
channel_id: id
|
|
1399
|
-
},
|
|
1400
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
1401
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
1402
|
-
} });
|
|
1403
|
-
} else {
|
|
1404
|
-
if (!rowsEqual(original.row, row)) throw new DynamoDBCommitStateError(`Channel "${id}" cannot be updated`);
|
|
1405
|
-
const delta = referenceCount - original.reference_count;
|
|
1406
|
-
if (delta !== 0) actions.push(updateChannelReferenceCount(store, original, delta));
|
|
1407
|
-
}
|
|
1408
|
-
}
|
|
1409
|
-
const originalBundles = new Map(originalBundleItems.map((item) => [item.sk, item]));
|
|
1410
|
-
for (const original of originalBundleItems) if (!bundles.has(original.sk)) actions.push(deleteAction(store, original));
|
|
1411
|
-
for (const [id, row] of bundles) {
|
|
1412
|
-
const original = originalBundles.get(id);
|
|
1413
|
-
const relationCount = relationCounts.get(id) ?? 0;
|
|
1414
|
-
const ownedPatchCount = ownedPatchCounts.get(id) ?? 0;
|
|
1415
|
-
if (original === void 0) actions.push(putNewBundle(store, row, relationCount, ownedPatchCount));
|
|
1416
|
-
else if (!rowsEqual(original.row, row)) actions.push(putUpdatedBundle(store, original, row, relationCount, ownedPatchCount));
|
|
1417
|
-
else {
|
|
1418
|
-
const relationDelta = relationCount - original.relation_count;
|
|
1419
|
-
const ownedPatchDelta = ownedPatchCount - original.owned_patch_count;
|
|
1420
|
-
if (relationDelta !== 0 || ownedPatchDelta !== 0) actions.push(updateBundleRelation(store, id, relationDelta, ownedPatchDelta));
|
|
1421
|
-
}
|
|
1422
|
-
}
|
|
1423
|
-
const originalPatches = new Map(originalPatchItems.map((item) => [item.sk, item]));
|
|
1424
|
-
for (const original of originalPatchItems) if (!patches.has(original.sk)) actions.push(deletePatch(store, original));
|
|
1425
|
-
for (const [id, row] of patches) {
|
|
1426
|
-
const original = originalPatches.get(id);
|
|
1427
|
-
if (original === void 0 || !rowsEqual(original.row, row)) actions.push(putPatch(store, row, original));
|
|
1428
|
-
}
|
|
1429
|
-
const originalReleases = new Map(originalReleaseItems.map((item) => [item.sk, item]));
|
|
1430
|
-
for (const original of originalReleaseItems) {
|
|
1431
|
-
if (releases.get(original.sk) !== void 0) continue;
|
|
1432
|
-
actions.push(deleteAction(store, original));
|
|
1433
|
-
actions.push({ Delete: {
|
|
1434
|
-
TableName: store.tableName,
|
|
1435
|
-
Key: {
|
|
1436
|
-
pk: DYNAMODB_RELEASE_ID_PARTITION,
|
|
1437
|
-
sk: original.sk
|
|
1438
|
-
},
|
|
1439
|
-
ConditionExpression: "#scopeKey = :scopeKey",
|
|
1440
|
-
ExpressionAttributeNames: { "#scopeKey": "scope_key" },
|
|
1441
|
-
ExpressionAttributeValues: { ":scopeKey": original.row.scope_key }
|
|
1442
|
-
} });
|
|
1443
|
-
}
|
|
1444
|
-
for (const [id, row] of releases) {
|
|
1445
|
-
const original = originalReleases.get(id);
|
|
1446
|
-
if (original === void 0) {
|
|
1447
|
-
actions.push({ Put: {
|
|
1448
|
-
TableName: store.tableName,
|
|
1449
|
-
Item: boundedDynamoDBMetadataItem(toDynamoDBReleaseItem(row)),
|
|
1450
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
1451
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
1452
|
-
} });
|
|
1453
|
-
actions.push({ Put: {
|
|
1454
|
-
TableName: store.tableName,
|
|
1455
|
-
Item: releaseLocatorItem(row),
|
|
1456
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
1457
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
1458
|
-
} });
|
|
1459
|
-
} else if (!rowsEqual(original.row, row)) {
|
|
1460
|
-
const moved = original.row.scope_key !== row.scope_key;
|
|
1461
|
-
if (moved) actions.push(deleteAction(store, original));
|
|
1462
|
-
actions.push({ Put: {
|
|
1463
|
-
TableName: store.tableName,
|
|
1464
|
-
Item: boundedDynamoDBMetadataItem(toDynamoDBReleaseItem(row, original.version + 1)),
|
|
1465
|
-
ConditionExpression: moved ? "attribute_not_exists(#pk)" : "#version = :version",
|
|
1466
|
-
ExpressionAttributeNames: moved ? { "#pk": "pk" } : { "#version": "version" },
|
|
1467
|
-
...moved ? {} : { ExpressionAttributeValues: { ":version": original.version } }
|
|
1468
|
-
} });
|
|
1469
|
-
if (moved) actions.push({ Put: {
|
|
1470
|
-
TableName: store.tableName,
|
|
1471
|
-
Item: releaseLocatorItem(row),
|
|
1472
|
-
ConditionExpression: "#scopeKey = :scopeKey",
|
|
1473
|
-
ExpressionAttributeNames: { "#scopeKey": "scope_key" },
|
|
1474
|
-
ExpressionAttributeValues: { ":scopeKey": original.row.scope_key }
|
|
1475
|
-
} });
|
|
1476
|
-
}
|
|
1477
|
-
}
|
|
1478
|
-
const originalReleaseCatalogs = new Map(originalReleaseCatalogItems.map((item) => [item.sk, item]));
|
|
1479
|
-
for (const [scopeKey, row] of releaseCatalogs) {
|
|
1480
|
-
const original = originalReleaseCatalogs.get(scopeKey);
|
|
1481
|
-
if (original === void 0 || !rowsEqual(original.row, row)) actions.push({ Put: {
|
|
1482
|
-
TableName: store.tableName,
|
|
1483
|
-
Item: boundedDynamoDBCatalogItem(toDynamoDBReleaseCatalogItem(row, (original?.version ?? 0) + 1)),
|
|
1484
|
-
ConditionExpression: original ? "#version = :version" : "attribute_not_exists(#pk)",
|
|
1485
|
-
ExpressionAttributeNames: original ? { "#version": "version" } : { "#pk": "pk" },
|
|
1486
|
-
...original ? { ExpressionAttributeValues: { ":version": original.version } } : {}
|
|
1487
|
-
} });
|
|
1488
|
-
}
|
|
1489
|
-
const changedReleaseIds = new Set([.../* @__PURE__ */ new Set([...originalReleases.keys(), ...releases.keys()])].filter((id) => {
|
|
1490
|
-
const original = originalReleases.get(id)?.row;
|
|
1491
|
-
const next = releases.get(id);
|
|
1492
|
-
return original === void 0 || next === void 0 || !rowsEqual(original, next);
|
|
1493
|
-
}));
|
|
1494
|
-
const changedCatalogScopes = new Set([...releaseCatalogs].flatMap(([scopeKey, row]) => {
|
|
1495
|
-
const original = originalReleaseCatalogs.get(scopeKey)?.row;
|
|
1496
|
-
return original === void 0 || !rowsEqual(original, row) ? [scopeKey] : [];
|
|
1497
|
-
}));
|
|
1498
|
-
for (const expectation of input.expectations ?? []) {
|
|
1499
|
-
if (expectation.model === "releases" && !changedReleaseIds.has(expectation.id)) {
|
|
1500
|
-
const original = originalReleases.get(expectation.id);
|
|
1501
|
-
actions.push({ ConditionCheck: {
|
|
1502
|
-
TableName: store.tableName,
|
|
1503
|
-
Key: original === void 0 ? {
|
|
1504
|
-
pk: DYNAMODB_RELEASE_ID_PARTITION,
|
|
1505
|
-
sk: expectation.id
|
|
1506
|
-
} : {
|
|
1507
|
-
pk: original.pk,
|
|
1508
|
-
sk: original.sk
|
|
1509
|
-
},
|
|
1510
|
-
ConditionExpression: original === void 0 ? "attribute_not_exists(#pk)" : "#version = :version",
|
|
1511
|
-
ExpressionAttributeNames: original === void 0 ? { "#pk": "pk" } : { "#version": "version" },
|
|
1512
|
-
...original === void 0 ? {} : { ExpressionAttributeValues: { ":version": original.version } }
|
|
1513
|
-
} });
|
|
1514
|
-
}
|
|
1515
|
-
if (expectation.model === "releaseCatalogs" && !changedCatalogScopes.has(expectation.scopeKey)) {
|
|
1516
|
-
const original = originalReleaseCatalogs.get(expectation.scopeKey);
|
|
1517
|
-
actions.push({ ConditionCheck: {
|
|
1518
|
-
TableName: store.tableName,
|
|
1519
|
-
Key: itemKey("release_catalogs", expectation.scopeKey),
|
|
1520
|
-
ConditionExpression: original === void 0 ? "attribute_not_exists(#pk)" : "#version = :version",
|
|
1521
|
-
ExpressionAttributeNames: original === void 0 ? { "#pk": "pk" } : { "#version": "version" },
|
|
1522
|
-
...original === void 0 ? {} : { ExpressionAttributeValues: { ":version": original.version } }
|
|
1523
|
-
} });
|
|
141
|
+
return { Update: item({
|
|
142
|
+
Key,
|
|
143
|
+
UpdateExpression: `SET ${Object.keys({
|
|
144
|
+
...init,
|
|
145
|
+
...op.by
|
|
146
|
+
}).map((column) => {
|
|
147
|
+
const path = name(attribute(column));
|
|
148
|
+
const by = op.by[column];
|
|
149
|
+
if (by === void 0) return `${path} = if_not_exists(${path}, ${value(init[column])})`;
|
|
150
|
+
return `${path} = if_not_exists(${path}, ${value(Number(init[column] ?? 0))}) + ${value(by)}`;
|
|
151
|
+
}).join(", ")}`
|
|
152
|
+
}) };
|
|
1524
153
|
}
|
|
1525
154
|
}
|
|
1526
|
-
const counter = metadataUpdate(store, {
|
|
1527
|
-
bundles: bundles.size - originalBundles.size,
|
|
1528
|
-
bundle_patches: patches.size - originalPatches.size
|
|
1529
|
-
});
|
|
1530
|
-
if (counter !== void 0) actions.push(counter);
|
|
1531
|
-
for (const [id, current] of apiKeys) {
|
|
1532
|
-
const original = originalApiKeys.get(id);
|
|
1533
|
-
if (original !== void 0 && rowsEqual(original.row, current.row)) continue;
|
|
1534
|
-
actions.push({ Put: {
|
|
1535
|
-
TableName: store.tableName,
|
|
1536
|
-
Item: boundedDynamoDBMetadataItem(apiKeyItem(current.row, original === void 0 ? 1 : original.version + 1)),
|
|
1537
|
-
ConditionExpression: original ? "#version = :version" : "attribute_not_exists(#pk)",
|
|
1538
|
-
ExpressionAttributeNames: original ? { "#version": "version" } : { "#pk": "pk" },
|
|
1539
|
-
...original ? { ExpressionAttributeValues: { ":version": original.version } } : {}
|
|
1540
|
-
} });
|
|
1541
|
-
if (original === void 0) actions.push({ Put: {
|
|
1542
|
-
TableName: store.tableName,
|
|
1543
|
-
Item: {
|
|
1544
|
-
pk: DYNAMODB_API_KEY_HASH_PARTITION,
|
|
1545
|
-
sk: current.row.hash,
|
|
1546
|
-
api_key_id: current.row.id
|
|
1547
|
-
},
|
|
1548
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
1549
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
1550
|
-
} });
|
|
1551
|
-
}
|
|
1552
|
-
if (actions.length > 0) await commitDynamoDBTransaction(store, actions);
|
|
1553
|
-
return { committed: true };
|
|
1554
155
|
};
|
|
1555
|
-
|
|
1556
|
-
const
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
const
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
const
|
|
1577
|
-
return (type === "UPDATE_APPLIED" || type === "RECOVERED" || type === "RELEASE_ADOPTED") && typeof fromBundleId === "string" && (updateStrategy === "fingerprint" || updateStrategy === "appVersion") || type === "UNCHANGED" && fromBundleId === null && updateStrategy === null;
|
|
1578
|
-
};
|
|
1579
|
-
const isBundleEventRow = (value) => typeof value === "object" && value !== null && typeof field(value, "id") === "string" && typeof field(value, "install_id") === "string" && isNullableString(field(value, "from_release_id")) && isNullableString(field(value, "to_release_id")) && typeof field(value, "to_bundle_id") === "string" && (field(value, "platform") === "ios" || field(value, "platform") === "android") && typeof field(value, "received_at_ms") === "number" && hasValidBundleEventShape(value);
|
|
1580
|
-
const isInsightsInstallationRow = (value) => typeof value === "object" && value !== null && typeof field(value, "id") === "string" && typeof field(value, "install_id") === "string" && isNullableString(field(value, "user_id")) && isNullableString(field(value, "username")) && typeof field(value, "to_bundle_id") === "string" && (field(value, "type") === "UPDATE_APPLIED" || field(value, "type") === "RECOVERED" || field(value, "type") === "RELEASE_ADOPTED" || field(value, "type") === "UNCHANGED") && (field(value, "platform") === "ios" || field(value, "platform") === "android") && typeof field(value, "app_version") === "string" && typeof field(value, "channel") === "string" && typeof field(value, "cohort") === "string" && typeof field(value, "received_at_ms") === "number";
|
|
1581
|
-
const isApiKeyRow = (value) => typeof value === "object" && value !== null && typeof field(value, "id") === "string" && typeof field(value, "hash") === "string" && typeof field(value, "name") === "string" && typeof field(value, "prefix") === "string" && field(value, "role") === "client" && typeof field(value, "created_at_ms") === "number" && (field(value, "revoked_at_ms") === null || typeof field(value, "revoked_at_ms") === "number");
|
|
1582
|
-
const parseOfficialRowItem = (value, partition, isRow) => {
|
|
1583
|
-
if (value.pk !== partition || typeof value.sk !== "string" || typeof value.version !== "number" || !isRow(value.row)) throw new DynamoDBStoredItemError();
|
|
156
|
+
/** Errors, and cancellation reasons, that a retry of the whole write can clear. */
|
|
157
|
+
const TRANSIENT = /* @__PURE__ */ new Set([
|
|
158
|
+
"TransactionConflict",
|
|
159
|
+
"ThrottlingError",
|
|
160
|
+
"ProvisionedThroughputExceeded",
|
|
161
|
+
"RequestLimitExceeded",
|
|
162
|
+
"TransactionInProgressException",
|
|
163
|
+
"ProvisionedThroughputExceededException",
|
|
164
|
+
"ThrottlingException",
|
|
165
|
+
"InternalServerError"
|
|
166
|
+
]);
|
|
167
|
+
/** A missing table reads as one to the schema fence, like SQL's 42P01. */
|
|
168
|
+
const missingTable = (error) => {
|
|
169
|
+
throw nameOf(error) === "ResourceNotFoundException" ? Object.assign(new Error(error.message, { cause: error }), { code: "42P01" }) : error;
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* The key-value store over one DynamoDB table keyed by `pk` and `sk`, with
|
|
173
|
+
* no secondary index. Reads are strongly consistent; a write is one
|
|
174
|
+
* `TransactWriteItems` with a client request token.
|
|
175
|
+
*/
|
|
176
|
+
const createDynamoDBStore = ({ client, tableName, nativePageSize }) => {
|
|
177
|
+
const documents = DynamoDBDocumentClient.from(client, { marshallOptions: { removeUndefinedValues: true } });
|
|
1584
178
|
return {
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
const
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
};
|
|
1602
|
-
const
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
type: row.type,
|
|
1608
|
-
fromBundleId: row.from_bundle_id
|
|
1609
|
-
} : {
|
|
1610
|
-
type: row.type,
|
|
1611
|
-
toBundleId: row.to_bundle_id
|
|
1612
|
-
}
|
|
1613
|
-
}),
|
|
1614
|
-
sk: insightsSortKey(row),
|
|
1615
|
-
version: 1,
|
|
1616
|
-
row
|
|
1617
|
-
});
|
|
1618
|
-
const toInsightsEventItem = (row) => {
|
|
1619
|
-
const orderKey = insightsSortKey(row);
|
|
1620
|
-
return boundedDynamoDBMetadataItem({
|
|
1621
|
-
pk: DYNAMODB_INSIGHTS_PARTITION,
|
|
1622
|
-
sk: orderKey,
|
|
1623
|
-
version: 1,
|
|
1624
|
-
row,
|
|
1625
|
-
...isInsightsMovementEvent(row) ? {
|
|
1626
|
-
gsi1pk: insightsMovementPartition(row.install_id),
|
|
1627
|
-
gsi1sk: orderKey
|
|
1628
|
-
} : {}
|
|
1629
|
-
});
|
|
1630
|
-
};
|
|
1631
|
-
const toInsightsInstallationItem = (row) => boundedDynamoDBMetadataItem({
|
|
1632
|
-
pk: DYNAMODB_INSIGHTS_INSTALLATIONS_PARTITION,
|
|
1633
|
-
sk: row.install_id,
|
|
1634
|
-
order_key: insightsSortKey(row),
|
|
1635
|
-
version: 1,
|
|
1636
|
-
row
|
|
1637
|
-
});
|
|
1638
|
-
const toInsightsUserItem = (row) => boundedDynamoDBMetadataItem({
|
|
1639
|
-
pk: insightsUserPartition(row.user_id),
|
|
1640
|
-
sk: row.install_id,
|
|
1641
|
-
order_key: insightsSortKey(row),
|
|
1642
|
-
version: 1,
|
|
1643
|
-
row
|
|
1644
|
-
});
|
|
1645
|
-
const parseInsightsInstallationItem = (value, partition) => {
|
|
1646
|
-
const row = value.row;
|
|
1647
|
-
if (value.pk !== partition || typeof value.sk !== "string" || typeof value.order_key !== "string" || value.version !== 1 || !isInsightsInstallationRow(row) || value.sk !== row.install_id || value.order_key !== insightsSortKey(row)) throw new DynamoDBStoredItemError();
|
|
1648
|
-
return value;
|
|
1649
|
-
};
|
|
1650
|
-
const loadInsightsInstallationItem = async (store, installId) => {
|
|
1651
|
-
const { Item } = await store.client.send(new GetCommand({
|
|
1652
|
-
TableName: store.tableName,
|
|
1653
|
-
Key: {
|
|
1654
|
-
pk: DYNAMODB_INSIGHTS_INSTALLATIONS_PARTITION,
|
|
1655
|
-
sk: installId
|
|
179
|
+
id: "dynamoDB",
|
|
180
|
+
limits: DYNAMODB_LIMITS,
|
|
181
|
+
async get(keys) {
|
|
182
|
+
const found = /* @__PURE__ */ new Map();
|
|
183
|
+
const unique = [...new Map(keys.map((key) => [idOf(key), key])).values()];
|
|
184
|
+
for (let at = 0; at < unique.length; at += GET_BATCH) {
|
|
185
|
+
let pending = unique.slice(at, at + GET_BATCH).map(({ pk, sk }) => ({
|
|
186
|
+
pk,
|
|
187
|
+
sk
|
|
188
|
+
}));
|
|
189
|
+
for (let round = 0; pending.length > 0; round += 1) {
|
|
190
|
+
if (round > 0) await sleep(2 ** round * 10);
|
|
191
|
+
const RequestItems = { [tableName]: {
|
|
192
|
+
Keys: pending,
|
|
193
|
+
ConsistentRead: true
|
|
194
|
+
} };
|
|
195
|
+
const result = await documents.send(new BatchGetCommand({ RequestItems })).catch(missingTable);
|
|
196
|
+
for (const item of result.Responses?.[tableName] ?? []) found.set(idOf(item), toRow(item));
|
|
197
|
+
pending = result.UnprocessedKeys?.[tableName]?.Keys ?? [];
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return keys.map((key) => found.get(idOf(key)) ?? null);
|
|
1656
201
|
},
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
}
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
for (let attempt = 0; attempt < DYNAMODB_INSIGHTS_RECORD_ATTEMPTS; attempt++) {
|
|
1670
|
-
const { Item: accepted } = await store.client.send(new GetCommand({
|
|
1671
|
-
TableName: store.tableName,
|
|
1672
|
-
Key: identityKey,
|
|
1673
|
-
ConsistentRead: true
|
|
1674
|
-
}));
|
|
1675
|
-
if (accepted !== void 0) return;
|
|
1676
|
-
const current = await loadInsightsInstallationItem(store, row.install_id);
|
|
1677
|
-
const actions = [
|
|
1678
|
-
{ Put: {
|
|
1679
|
-
TableName: store.tableName,
|
|
1680
|
-
Item: identityKey,
|
|
1681
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
1682
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
1683
|
-
} },
|
|
1684
|
-
{ Put: {
|
|
1685
|
-
TableName: store.tableName,
|
|
1686
|
-
Item: eventItem
|
|
1687
|
-
} },
|
|
1688
|
-
...bundleItem === null ? [] : [{ Put: {
|
|
1689
|
-
TableName: store.tableName,
|
|
1690
|
-
Item: bundleItem
|
|
1691
|
-
} }]
|
|
1692
|
-
];
|
|
1693
|
-
if (current === null || advancesInsightsInstallation(next, current.row)) {
|
|
1694
|
-
actions.push({ Put: {
|
|
1695
|
-
TableName: store.tableName,
|
|
1696
|
-
Item: toInsightsInstallationItem(next),
|
|
1697
|
-
ConditionExpression: current === null ? "attribute_not_exists(#pk)" : "#orderKey = :currentOrderKey",
|
|
1698
|
-
ExpressionAttributeNames: current === null ? { "#pk": "pk" } : { "#orderKey": "order_key" },
|
|
1699
|
-
...current === null ? {} : { ExpressionAttributeValues: { ":currentOrderKey": current.order_key } }
|
|
1700
|
-
} });
|
|
1701
|
-
if (current !== null && current.row.user_id !== null && current.row.user_id !== next.user_id) actions.push({ Delete: {
|
|
1702
|
-
TableName: store.tableName,
|
|
1703
|
-
Key: {
|
|
1704
|
-
pk: insightsUserPartition(current.row.user_id),
|
|
1705
|
-
sk: current.row.install_id
|
|
202
|
+
/** Plain BatchWriteItem deletes: 1 write unit per KB each, not a transaction's 2. */
|
|
203
|
+
async deleteConsumed(keys) {
|
|
204
|
+
for (let at = 0; at < keys.length; at += WRITE_BATCH) {
|
|
205
|
+
let pending = keys.slice(at, at + WRITE_BATCH).map(({ pk, sk }) => ({ DeleteRequest: { Key: {
|
|
206
|
+
pk,
|
|
207
|
+
sk
|
|
208
|
+
} } }));
|
|
209
|
+
for (let round = 0; pending.length > 0; round += 1) {
|
|
210
|
+
if (round > 0) await sleep(2 ** round * 10);
|
|
211
|
+
const RequestItems = { [tableName]: pending };
|
|
212
|
+
const { UnprocessedItems } = await documents.send(new BatchWriteCommand({ RequestItems })).catch(missingTable);
|
|
213
|
+
pending = UnprocessedItems?.[tableName] ?? [];
|
|
1706
214
|
}
|
|
1707
|
-
} });
|
|
1708
|
-
if (next.user_id !== null) actions.push({ Put: {
|
|
1709
|
-
TableName: store.tableName,
|
|
1710
|
-
Item: toInsightsUserItem({
|
|
1711
|
-
...next,
|
|
1712
|
-
user_id: next.user_id
|
|
1713
|
-
})
|
|
1714
|
-
} });
|
|
1715
|
-
}
|
|
1716
|
-
try {
|
|
1717
|
-
await commitDynamoDBTransaction(store, actions);
|
|
1718
|
-
return;
|
|
1719
|
-
} catch (error) {
|
|
1720
|
-
if (!isDynamoDBTransactionConflict(error)) throw error;
|
|
1721
|
-
const { Item } = await store.client.send(new GetCommand({
|
|
1722
|
-
TableName: store.tableName,
|
|
1723
|
-
Key: identityKey,
|
|
1724
|
-
ConsistentRead: true
|
|
1725
|
-
}));
|
|
1726
|
-
if (Item !== void 0) return;
|
|
1727
|
-
if (attempt === 2) throw error;
|
|
1728
|
-
}
|
|
1729
|
-
}
|
|
1730
|
-
};
|
|
1731
|
-
const insightsEventRange = (input) => {
|
|
1732
|
-
const upperExclusive = input.after ? `${timestampSortKey(input.after.receivedAtMs)}#${input.after.id}` : `${timestampSortKey(input.beforeReceivedAtMs)}#`;
|
|
1733
|
-
const upper = `${upperExclusive.slice(0, -1)}${String.fromCharCode(upperExclusive.charCodeAt(upperExclusive.length - 1) - 1)}~`;
|
|
1734
|
-
const movement = input.filter.kind === "installationMovement";
|
|
1735
|
-
const partition = movement ? insightsMovementPartition(input.filter.installId) : input.filter.kind === "bundle" ? insightsBundlePartition(input.filter) : DYNAMODB_INSIGHTS_PARTITION;
|
|
1736
|
-
return {
|
|
1737
|
-
partition,
|
|
1738
|
-
query: {
|
|
1739
|
-
...movement ? { IndexName: DYNAMODB_UPDATE_INDEX_NAME } : { ConsistentRead: true },
|
|
1740
|
-
KeyConditionExpression: "#partition = :partition AND #order BETWEEN :since AND :upper",
|
|
1741
|
-
ExpressionAttributeNames: {
|
|
1742
|
-
"#partition": movement ? "gsi1pk" : "pk",
|
|
1743
|
-
"#order": movement ? "gsi1sk" : "sk"
|
|
1744
|
-
},
|
|
1745
|
-
ExpressionAttributeValues: {
|
|
1746
|
-
":partition": partition,
|
|
1747
|
-
":since": `${timestampSortKey(input.sinceMs ?? 0)}#`,
|
|
1748
|
-
":upper": upper
|
|
1749
215
|
}
|
|
1750
|
-
}
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
const
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
do {
|
|
1761
|
-
const page = await store.client.send(new QueryCommand({
|
|
1762
|
-
TableName: store.tableName,
|
|
1763
|
-
...range.query,
|
|
1764
|
-
ExclusiveStartKey: exclusiveStartKey,
|
|
1765
|
-
Limit: input.limit - rows.length,
|
|
1766
|
-
ScanIndexForward: false
|
|
1767
|
-
}));
|
|
1768
|
-
rows.push(...(page.Items ?? []).slice(0, input.limit - rows.length).map((item) => parseOfficialRowItem(item, input.filter.kind === "bundle" ? range.partition : DYNAMODB_INSIGHTS_PARTITION, isBundleEventRow).row));
|
|
1769
|
-
exclusiveStartKey = page.LastEvaluatedKey;
|
|
1770
|
-
} while (rows.length < input.limit && exclusiveStartKey !== void 0);
|
|
1771
|
-
return rows;
|
|
1772
|
-
},
|
|
1773
|
-
async findInstallations(input) {
|
|
1774
|
-
if ("installId" in input) {
|
|
1775
|
-
const stored = await loadInsightsInstallationItem(store, input.installId);
|
|
1776
|
-
return stored === null ? [] : [stored.row];
|
|
1777
|
-
}
|
|
1778
|
-
const partition = insightsUserPartition(input.userId);
|
|
1779
|
-
const rows = [];
|
|
1780
|
-
let exclusiveStartKey;
|
|
1781
|
-
do {
|
|
1782
|
-
const page = await store.client.send(new QueryCommand({
|
|
1783
|
-
TableName: store.tableName,
|
|
1784
|
-
ConsistentRead: true,
|
|
1785
|
-
ExclusiveStartKey: exclusiveStartKey,
|
|
1786
|
-
KeyConditionExpression: input.afterInstallId === void 0 ? "#pk = :pk" : "#pk = :pk AND #sk > :after",
|
|
1787
|
-
ExpressionAttributeNames: {
|
|
1788
|
-
"#pk": "pk",
|
|
1789
|
-
...input.afterInstallId === void 0 ? {} : { "#sk": "sk" }
|
|
1790
|
-
},
|
|
216
|
+
},
|
|
217
|
+
async query({ pk, gte, lt, lte, order, limit, after }) {
|
|
218
|
+
let condition = "pk = :pk";
|
|
219
|
+
const between = gte !== void 0 && lt !== void 0;
|
|
220
|
+
if (between) condition += " AND sk BETWEEN :gte AND :lte";
|
|
221
|
+
else if (gte !== void 0) condition += " AND sk >= :gte";
|
|
222
|
+
else if (lt !== void 0) condition += " AND sk < :lt";
|
|
223
|
+
const result = await documents.send(new QueryCommand({
|
|
224
|
+
TableName: tableName,
|
|
225
|
+
KeyConditionExpression: condition,
|
|
1791
226
|
ExpressionAttributeValues: {
|
|
1792
|
-
":pk":
|
|
1793
|
-
|
|
227
|
+
":pk": pk,
|
|
228
|
+
":gte": gte,
|
|
229
|
+
...between ? { ":lte": lte ?? lt } : { ":lt": lt }
|
|
1794
230
|
},
|
|
1795
|
-
|
|
1796
|
-
ScanIndexForward: true
|
|
1797
|
-
}));
|
|
1798
|
-
for (const item of (page.Items ?? []).slice(0, input.limit - rows.length)) {
|
|
1799
|
-
const stored = parseInsightsInstallationItem(item, partition);
|
|
1800
|
-
const current = await loadInsightsInstallationItem(store, stored.row.install_id);
|
|
1801
|
-
if (current?.row.user_id === input.userId) rows.push(current.row);
|
|
1802
|
-
}
|
|
1803
|
-
exclusiveStartKey = page.LastEvaluatedKey;
|
|
1804
|
-
} while (rows.length < input.limit && exclusiveStartKey !== void 0);
|
|
1805
|
-
return rows;
|
|
1806
|
-
},
|
|
1807
|
-
async countInstallations(input) {
|
|
1808
|
-
let count = 0;
|
|
1809
|
-
let exclusiveStartKey;
|
|
1810
|
-
do {
|
|
1811
|
-
const page = await store.client.send(new QueryCommand({
|
|
1812
|
-
TableName: store.tableName,
|
|
231
|
+
ScanIndexForward: order === "asc",
|
|
1813
232
|
ConsistentRead: true,
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
},
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
":platform": input.platform,
|
|
1828
|
-
":channel": input.channel,
|
|
1829
|
-
":since": input.sinceMs,
|
|
1830
|
-
...input.bundleId === void 0 ? {} : { ":bundle": input.bundleId }
|
|
1831
|
-
},
|
|
1832
|
-
Select: "COUNT",
|
|
1833
|
-
ScanIndexForward: true
|
|
1834
|
-
}));
|
|
1835
|
-
count += page.Count ?? 0;
|
|
1836
|
-
exclusiveStartKey = page.LastEvaluatedKey;
|
|
1837
|
-
} while (exclusiveStartKey !== void 0);
|
|
1838
|
-
return count;
|
|
1839
|
-
},
|
|
1840
|
-
async countEvents(input) {
|
|
1841
|
-
if (input.sinceMs === input.beforeReceivedAtMs) return 0;
|
|
1842
|
-
const { query } = insightsEventRange({
|
|
1843
|
-
...input,
|
|
1844
|
-
filter: {
|
|
1845
|
-
kind: "bundle",
|
|
1846
|
-
...input.filter
|
|
1847
|
-
},
|
|
1848
|
-
limit: 1
|
|
1849
|
-
});
|
|
1850
|
-
let count = 0;
|
|
1851
|
-
let exclusiveStartKey;
|
|
1852
|
-
do {
|
|
1853
|
-
const page = await store.client.send(new QueryCommand({
|
|
1854
|
-
TableName: store.tableName,
|
|
1855
|
-
...query,
|
|
1856
|
-
ExclusiveStartKey: exclusiveStartKey,
|
|
1857
|
-
Select: "COUNT"
|
|
1858
|
-
}));
|
|
1859
|
-
count += page.Count ?? 0;
|
|
1860
|
-
exclusiveStartKey = page.LastEvaluatedKey;
|
|
1861
|
-
} while (exclusiveStartKey !== void 0);
|
|
1862
|
-
return count;
|
|
1863
|
-
}
|
|
1864
|
-
});
|
|
1865
|
-
const apiKeyItem = (row, version) => ({
|
|
1866
|
-
pk: DYNAMODB_API_KEY_PARTITION,
|
|
1867
|
-
sk: row.id,
|
|
1868
|
-
version,
|
|
1869
|
-
row
|
|
1870
|
-
});
|
|
1871
|
-
const loadApiKey = async (store, id) => {
|
|
1872
|
-
const result = await store.client.send(new GetCommand({
|
|
1873
|
-
TableName: store.tableName,
|
|
1874
|
-
Key: {
|
|
1875
|
-
pk: DYNAMODB_API_KEY_PARTITION,
|
|
1876
|
-
sk: id
|
|
233
|
+
Limit: Math.min(limit, nativePageSize ?? limit),
|
|
234
|
+
ExclusiveStartKey: after === void 0 ? void 0 : {
|
|
235
|
+
pk,
|
|
236
|
+
sk: after
|
|
237
|
+
}
|
|
238
|
+
})).catch(missingTable);
|
|
239
|
+
return {
|
|
240
|
+
items: (result.Items ?? []).map((item) => ({
|
|
241
|
+
sk: item.sk,
|
|
242
|
+
value: toRow(item)
|
|
243
|
+
})),
|
|
244
|
+
more: result.LastEvaluatedKey !== void 0
|
|
245
|
+
};
|
|
1877
246
|
},
|
|
1878
|
-
|
|
1879
|
-
}));
|
|
1880
|
-
return result.Item === void 0 ? null : parseOfficialRowItem(result.Item, DYNAMODB_API_KEY_PARTITION, isApiKeyRow);
|
|
1881
|
-
};
|
|
1882
|
-
const createDynamoDBApiKeyTable = (store) => {
|
|
1883
|
-
const findByHash = async (hash) => {
|
|
1884
|
-
const lookup = await store.client.send(new GetCommand({
|
|
1885
|
-
TableName: store.tableName,
|
|
1886
|
-
Key: {
|
|
1887
|
-
pk: DYNAMODB_API_KEY_HASH_PARTITION,
|
|
1888
|
-
sk: hash
|
|
1889
|
-
},
|
|
1890
|
-
ConsistentRead: true
|
|
1891
|
-
}));
|
|
1892
|
-
const id = lookup.Item?.api_key_id;
|
|
1893
|
-
if (lookup.Item === void 0) return null;
|
|
1894
|
-
if (typeof id !== "string") throw new DynamoDBStoredItemError();
|
|
1895
|
-
return (await loadApiKey(store, id))?.row ?? null;
|
|
1896
|
-
};
|
|
1897
|
-
return {
|
|
1898
|
-
async create(row) {
|
|
1899
|
-
if (await findByHash(row.hash) !== null) return "existing";
|
|
247
|
+
async write(ops) {
|
|
1900
248
|
try {
|
|
1901
|
-
await
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
} }, { Put: {
|
|
1907
|
-
TableName: store.tableName,
|
|
1908
|
-
Item: {
|
|
1909
|
-
pk: DYNAMODB_API_KEY_HASH_PARTITION,
|
|
1910
|
-
sk: row.hash,
|
|
1911
|
-
api_key_id: row.id
|
|
1912
|
-
},
|
|
1913
|
-
ConditionExpression: "attribute_not_exists(#pk)",
|
|
1914
|
-
ExpressionAttributeNames: { "#pk": "pk" }
|
|
1915
|
-
} }]);
|
|
1916
|
-
return "created";
|
|
249
|
+
await documents.send(new TransactWriteCommand({
|
|
250
|
+
TransactItems: ops.map((op) => toTransactItem(tableName, op)),
|
|
251
|
+
ClientRequestToken: crypto.randomUUID()
|
|
252
|
+
}));
|
|
253
|
+
return { ok: true };
|
|
1917
254
|
} catch (error) {
|
|
1918
|
-
|
|
1919
|
-
|
|
255
|
+
const { CancellationReasons = [] } = error;
|
|
256
|
+
const codes = CancellationReasons.map(({ Code }) => Code);
|
|
257
|
+
const failed = codes.indexOf("ConditionalCheckFailed");
|
|
258
|
+
if (failed !== -1) return {
|
|
259
|
+
ok: false,
|
|
260
|
+
failedOp: failed
|
|
261
|
+
};
|
|
262
|
+
return [nameOf(error), ...codes].some((code) => TRANSIENT.has(code)) ? {
|
|
263
|
+
ok: false,
|
|
264
|
+
retry: true
|
|
265
|
+
} : missingTable(error);
|
|
1920
266
|
}
|
|
1921
267
|
},
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
268
|
+
migrations: {
|
|
269
|
+
/**
|
|
270
|
+
* Creates the table for local and test runs, and turns on its TTL,
|
|
271
|
+
* which every table the store runs on needs; `hot-updater init`
|
|
272
|
+
* creates it in AWS.
|
|
273
|
+
*/
|
|
274
|
+
async apply() {
|
|
275
|
+
try {
|
|
276
|
+
await client.send(new DescribeTableCommand({ TableName: tableName }));
|
|
277
|
+
} catch (error) {
|
|
278
|
+
if (nameOf(error) !== "ResourceNotFoundException") throw error;
|
|
279
|
+
await client.send(new CreateTableCommand({
|
|
280
|
+
TableName: tableName,
|
|
281
|
+
AttributeDefinitions: [{
|
|
282
|
+
AttributeName: "pk",
|
|
283
|
+
AttributeType: "S"
|
|
284
|
+
}, {
|
|
285
|
+
AttributeName: "sk",
|
|
286
|
+
AttributeType: "S"
|
|
287
|
+
}],
|
|
288
|
+
KeySchema: [{
|
|
289
|
+
AttributeName: "pk",
|
|
290
|
+
KeyType: "HASH"
|
|
291
|
+
}, {
|
|
292
|
+
AttributeName: "sk",
|
|
293
|
+
KeyType: "RANGE"
|
|
294
|
+
}],
|
|
295
|
+
BillingMode: "PAY_PER_REQUEST"
|
|
1934
296
|
}));
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
297
|
+
await waitUntilTableExists({
|
|
298
|
+
client,
|
|
299
|
+
maxWaitTime: 120,
|
|
300
|
+
minDelay: 1,
|
|
301
|
+
maxDelay: 1
|
|
302
|
+
}, { TableName: tableName });
|
|
303
|
+
}
|
|
304
|
+
const { TimeToLiveDescription: ttl } = await client.send(new DescribeTimeToLiveCommand({ TableName: tableName }));
|
|
305
|
+
if (ttl?.TimeToLiveStatus?.startsWith("ENABL")) return;
|
|
306
|
+
const input = {
|
|
307
|
+
TableName: tableName,
|
|
308
|
+
TimeToLiveSpecification: {
|
|
309
|
+
AttributeName: DYNAMODB_TTL_ATTRIBUTE,
|
|
310
|
+
Enabled: true
|
|
311
|
+
}
|
|
1946
312
|
};
|
|
1947
|
-
await
|
|
1948
|
-
|
|
1949
|
-
Item: boundedDynamoDBMetadataItem(apiKeyItem(row, current.version + 1)),
|
|
1950
|
-
ConditionExpression: "#version = :currentVersion",
|
|
1951
|
-
ExpressionAttributeNames: { "#version": "version" },
|
|
1952
|
-
ExpressionAttributeValues: { ":currentVersion": current.version }
|
|
1953
|
-
}));
|
|
1954
|
-
return row;
|
|
1955
|
-
}
|
|
313
|
+
await client.send(new UpdateTimeToLiveCommand(input));
|
|
314
|
+
} }
|
|
1956
315
|
};
|
|
1957
316
|
};
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
const
|
|
1963
|
-
|
|
1964
|
-
region: clientConfig.region
|
|
1965
|
-
}) : null;
|
|
1966
|
-
const store = {
|
|
317
|
+
//#endregion
|
|
318
|
+
//#region src/dynamoDB.ts
|
|
319
|
+
/** The DynamoDB client ignores the CloudFront and batching settings. */
|
|
320
|
+
const adapterOf = ({ tableName, ...clientConfig }) => {
|
|
321
|
+
const client = new DynamoDBClient(clientConfig);
|
|
322
|
+
const store = createDynamoDBStore({
|
|
1967
323
|
client,
|
|
1968
324
|
tableName
|
|
325
|
+
});
|
|
326
|
+
return {
|
|
327
|
+
client,
|
|
328
|
+
adapter: createKvAdapter({ store })
|
|
1969
329
|
};
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
330
|
+
};
|
|
331
|
+
/**
|
|
332
|
+
* Creates the table when it is missing and writes the schema settings of
|
|
333
|
+
* core and `plugins`, the plugins the server runs, which the database checks
|
|
334
|
+
* before its first read. `hot-updater init` runs it in AWS with the managed
|
|
335
|
+
* server's plugins.
|
|
336
|
+
*/
|
|
337
|
+
const migrateDynamoDB = (config, plugins = []) => {
|
|
338
|
+
const { client, adapter } = adapterOf(config);
|
|
339
|
+
return migrateCoreSchema(adapter, "dynamoDB", plugins).finally(() => client.destroy());
|
|
340
|
+
};
|
|
341
|
+
/**
|
|
342
|
+
* Hot Updater's database on one DynamoDB table, through the storage engine.
|
|
343
|
+
* With a distribution, it gives core the CloudFront invalidation core runs
|
|
344
|
+
* after a write that changes what the update-check routes answer.
|
|
345
|
+
*/
|
|
346
|
+
const dynamoDB = (config) => {
|
|
347
|
+
const { client, adapter } = adapterOf(config);
|
|
348
|
+
const cloudFront = createUpdateRouteInvalidation(config);
|
|
349
|
+
return {
|
|
350
|
+
...createEngineDatabase({
|
|
351
|
+
name: "dynamoDB",
|
|
352
|
+
adapter,
|
|
353
|
+
onCachedRoutesChange: cloudFront?.invalidate,
|
|
354
|
+
aggregateBatching: config.aggregateBatching ?? {}
|
|
355
|
+
}),
|
|
356
|
+
async dispose() {
|
|
357
|
+
await adapter.dispose?.();
|
|
1986
358
|
client.destroy();
|
|
1987
359
|
cloudFront?.destroy();
|
|
1988
360
|
}
|
|
1989
|
-
}
|
|
1990
|
-
return createDatabasePlugin({
|
|
1991
|
-
name: "dynamoDB",
|
|
1992
|
-
models: {
|
|
1993
|
-
...adapter.models,
|
|
1994
|
-
bundlePatches: createDynamoDBBundlePatchTable(store, DYNAMODB_UPDATE_INDEX_NAME),
|
|
1995
|
-
insights: createDynamoDBInsightsTable(store),
|
|
1996
|
-
apiKeys: createDynamoDBApiKeyTable(store)
|
|
1997
|
-
},
|
|
1998
|
-
async commit(input) {
|
|
1999
|
-
const result = await adapter.commit(input);
|
|
2000
|
-
if (result.committed && input.changes.some(({ model }) => model === "bundles" || model === "bundlePatches")) await invalidateUpdateRoutes();
|
|
2001
|
-
return result;
|
|
2002
|
-
},
|
|
2003
|
-
dispose: adapter.dispose
|
|
2004
|
-
});
|
|
361
|
+
};
|
|
2005
362
|
};
|
|
2006
363
|
//#endregion
|
|
2007
364
|
//#region src/runtimeAwsConfig.ts
|
|
@@ -2091,6 +448,21 @@ function s3Storage(config) {
|
|
|
2091
448
|
const value = [normalizedBasePath, normalizedPrefix].filter(Boolean).join("/");
|
|
2092
449
|
return value ? `${value}/` : "";
|
|
2093
450
|
};
|
|
451
|
+
/**
|
|
452
|
+
* The URI `put` returns for the key, or null for a key no `put` writes,
|
|
453
|
+
* such as a folder marker that ends in "/".
|
|
454
|
+
*/
|
|
455
|
+
const getListedStorageUri = (key) => {
|
|
456
|
+
try {
|
|
457
|
+
return createStorageUri({
|
|
458
|
+
bucket: bucketName,
|
|
459
|
+
key,
|
|
460
|
+
protocol: "s3"
|
|
461
|
+
});
|
|
462
|
+
} catch {
|
|
463
|
+
return null;
|
|
464
|
+
}
|
|
465
|
+
};
|
|
2094
466
|
const getRelativeKey = (key) => {
|
|
2095
467
|
if (!normalizedBasePath) return key;
|
|
2096
468
|
const basePrefix = `${normalizedBasePath}/`;
|
|
@@ -2101,7 +473,7 @@ function s3Storage(config) {
|
|
|
2101
473
|
if (parsed.bucket !== bucketName) throw new Error(`Bucket name mismatch: expected "${bucketName}", but found "${parsed.bucket}".`);
|
|
2102
474
|
return parsed;
|
|
2103
475
|
};
|
|
2104
|
-
return
|
|
476
|
+
return createStorageAdapter({
|
|
2105
477
|
name: "s3Storage",
|
|
2106
478
|
protocol: "s3",
|
|
2107
479
|
async listObjects(prefix) {
|
|
@@ -2115,11 +487,13 @@ function s3Storage(config) {
|
|
|
2115
487
|
}));
|
|
2116
488
|
for (const object of response.Contents ?? []) {
|
|
2117
489
|
if (!object.Key) continue;
|
|
490
|
+
const storageUri = getListedStorageUri(object.Key);
|
|
491
|
+
if (storageUri === null) continue;
|
|
2118
492
|
objects.push({
|
|
2119
493
|
key: getRelativeKey(object.Key),
|
|
2120
494
|
lastModifiedAt: object.LastModified,
|
|
2121
495
|
size: object.Size ?? 0,
|
|
2122
|
-
storageUri
|
|
496
|
+
storageUri
|
|
2123
497
|
});
|
|
2124
498
|
}
|
|
2125
499
|
continuationToken = response.NextContinuationToken;
|
|
@@ -2206,4 +580,11 @@ function s3Storage(config) {
|
|
|
2206
580
|
});
|
|
2207
581
|
}
|
|
2208
582
|
//#endregion
|
|
2209
|
-
|
|
583
|
+
//#region src/plugins.ts
|
|
584
|
+
/**
|
|
585
|
+
* The plugins the managed AWS server runs: Insights, and API keys on every
|
|
586
|
+
* client route. Its Lambda@Edge function imports this list.
|
|
587
|
+
*/
|
|
588
|
+
const plugins = [insights(), apiKeys()];
|
|
589
|
+
//#endregion
|
|
590
|
+
export { cloudFrontDownloadUrl, dynamoDB, migrateDynamoDB, plugins, s3Storage };
|