@hot-updater/aws 1.0.0-rc.0 → 1.0.0-rc.2

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 CHANGED
@@ -1,4 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let node_crypto = require("node:crypto");
2
3
  let _aws_sdk_client_cloudfront = require("@aws-sdk/client-cloudfront");
3
4
  let _aws_sdk_client_dynamodb = require("@aws-sdk/client-dynamodb");
4
5
  let _aws_sdk_lib_dynamodb = require("@aws-sdk/lib-dynamodb");
@@ -10,7 +11,7 @@ let _aws_sdk_client_s3 = require("@aws-sdk/client-s3");
10
11
  let _aws_sdk_lib_storage = require("@aws-sdk/lib-storage");
11
12
  //#region src/cloudFrontInvalidation.ts
12
13
  const DEFAULT_POLL_INTERVAL_MS = 2e3;
13
- const DEFAULT_TIMEOUT_MS = 300 * 1e3;
14
+ const DEFAULT_TIMEOUT_MS = 3e5;
14
15
  const sleep = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
15
16
  const invalidateCloudFront = async (client, distributionId, paths, options) => {
16
17
  if (paths.length === 0) return;
@@ -39,9 +40,10 @@ const invalidateCloudFront = async (client, distributionId, paths, options) => {
39
40
  };
40
41
  //#endregion
41
42
  //#region src/dynamoDB.ts
42
- const DYNAMODB_MAX_METADATA_ITEM_BYTES = 8 * 1024;
43
- const DYNAMODB_MAX_CATALOG_ITEM_BYTES = 400 * 1024;
43
+ const DYNAMODB_MAX_METADATA_ITEM_BYTES = 8192;
44
+ const DYNAMODB_MAX_CATALOG_ITEM_BYTES = 409600;
44
45
  var DynamoDBMetadataItemSizeError = class extends Error {
46
+ byteLength;
45
47
  name = "DynamoDBMetadataItemSizeError";
46
48
  constructor(byteLength) {
47
49
  super(`DynamoDB metadata item is ${byteLength} bytes; maximum is ${DYNAMODB_MAX_METADATA_ITEM_BYTES}`);
@@ -54,6 +56,7 @@ const boundedDynamoDBMetadataItem = (item) => {
54
56
  return item;
55
57
  };
56
58
  var DynamoDBCatalogItemSizeError = class extends Error {
59
+ byteLength;
57
60
  name = "DynamoDBCatalogItemSizeError";
58
61
  constructor(byteLength) {
59
62
  super(`DynamoDB catalog item is ${byteLength} bytes; maximum is ${DYNAMODB_MAX_CATALOG_ITEM_BYTES}`);
@@ -114,7 +117,6 @@ const parseDynamoDBItem = (value) => {
114
117
  version,
115
118
  row
116
119
  };
117
- if (typeof gsi1pk !== "string" || typeof gsi1sk !== "string") throw new DynamoDBStoredItemError();
118
120
  if (pk === "bundles" && isBundleRow(row)) {
119
121
  if (typeof relationCount !== "number" || typeof ownedPatchCount !== "number") throw new DynamoDBStoredItemError();
120
122
  return {
@@ -123,12 +125,10 @@ const parseDynamoDBItem = (value) => {
123
125
  version,
124
126
  relation_count: relationCount,
125
127
  owned_patch_count: ownedPatchCount,
126
- gsi1pk,
127
- gsi1sk,
128
128
  row
129
129
  };
130
130
  }
131
- if (pk === "bundle_patches" && isPatchRow(row)) return {
131
+ if (pk === "bundle_patches" && typeof gsi1pk === "string" && typeof gsi1sk === "string" && isPatchRow(row)) return {
132
132
  pk,
133
133
  sk,
134
134
  version,
@@ -138,7 +138,6 @@ const parseDynamoDBItem = (value) => {
138
138
  };
139
139
  throw new DynamoDBStoredItemError();
140
140
  };
141
- const bundlePlatformPartition = (row) => `bundle#${row.platform}`;
142
141
  const patchOwnerPartition = (bundleId) => `patch-owner#${bundleId}`;
143
142
  const patchOrderKey = (row) => `${row.order_index.toString().padStart(10, "0")}#${row.id}`;
144
143
  const toDynamoDBBundleItem = (row, version = 1, relationCount = 0, ownedPatchCount = 0) => ({
@@ -147,8 +146,6 @@ const toDynamoDBBundleItem = (row, version = 1, relationCount = 0, ownedPatchCou
147
146
  version,
148
147
  relation_count: relationCount,
149
148
  owned_patch_count: ownedPatchCount,
150
- gsi1pk: bundlePlatformPartition(row),
151
- gsi1sk: row.id,
152
149
  row
153
150
  });
154
151
  const toDynamoDBPatchItem = (row, version = 1) => {
@@ -505,6 +502,7 @@ const queryOwnerPatchIds = async (store, indexName, bundleId) => {
505
502
  })).filter((item) => item.pk === "bundle_patches").map(({ sk }) => sk);
506
503
  };
507
504
  var DynamoDBTransactionLimitError = class extends Error {
505
+ actionCount;
508
506
  name = "DynamoDBTransactionLimitError";
509
507
  constructor(actionCount) {
510
508
  super(`DynamoDB transaction requires ${actionCount} actions; maximum is 100`);
@@ -585,6 +583,7 @@ const commitDynamoDBTransaction = async (store, actions) => {
585
583
  const DYNAMODB_BATCH_GET_ATTEMPTS = 5;
586
584
  const DYNAMODB_BATCH_GET_BASE_DELAY_MS = 25;
587
585
  var DynamoDBBatchGetExhaustedError = class extends Error {
586
+ unprocessedKeyCount;
588
587
  name = "DynamoDBBatchGetExhaustedError";
589
588
  constructor(unprocessedKeyCount) {
590
589
  super(`DynamoDB did not process ${unprocessedKeyCount} batch-get keys after ${DYNAMODB_BATCH_GET_ATTEMPTS} attempts`);
@@ -637,6 +636,8 @@ const loadMetadataCount = async (store, model) => {
637
636
  return typeof count === "number" && Number.isSafeInteger(count) && count >= 0 ? count : void 0;
638
637
  };
639
638
  var DynamoDBPatchIndexConsistencyError = class extends Error {
639
+ bundleId;
640
+ expectedCount;
640
641
  name = "DynamoDBPatchIndexConsistencyError";
641
642
  constructor(bundleId, expectedCount) {
642
643
  super(`DynamoDB patch index did not converge to ${expectedCount} rows for bundle "${bundleId}"`);
@@ -664,7 +665,8 @@ const queryCompleteOwnersPatches = async (store, indexName, bundleIds) => {
664
665
  let failedOwner = firstOwner;
665
666
  for (let attempt = 0; attempt < 3; attempt++) {
666
667
  const candidateIdsByOwner = await Promise.all(owners.map(({ sk }) => queryOwnerPatchIds(store, indexName, sk)));
667
- const patches = await loadPatchItemsById(store, candidateIdsByOwner.flat());
668
+ const candidateIds = candidateIdsByOwner.flat();
669
+ const patches = await loadPatchItemsById(store, candidateIds);
668
670
  const patchesById = new Map(patches.map((patch) => [patch.sk, patch]));
669
671
  const mismatchedOwner = owners.find((owner, ownerIndex) => {
670
672
  const ids = candidateIdsByOwner[ownerIndex] ?? [];
@@ -765,7 +767,7 @@ const createDynamoDBBundle = async (store, row) => {
765
767
  } }]);
766
768
  };
767
769
  const createDynamoDBPatch = async (store, row) => {
768
- const bundleIds = [...new Set([row.bundle_id, row.base_bundle_id])];
770
+ const bundleIds = [.../* @__PURE__ */ new Set([row.bundle_id, row.base_bundle_id])];
769
771
  const counter = metadataUpdate(store, { bundle_patches: 1 });
770
772
  if (!counter) return;
771
773
  await commitDynamoDBTransaction(store, [
@@ -801,9 +803,10 @@ const deleteAction = (store, item) => ({ Delete: {
801
803
  const deleteDynamoDBPatch = async (store, item) => {
802
804
  const counter = metadataUpdate(store, { bundle_patches: -1 });
803
805
  if (!counter) return;
806
+ const bundleIds = [.../* @__PURE__ */ new Set([item.row.bundle_id, item.row.base_bundle_id])];
804
807
  await commitDynamoDBTransaction(store, [
805
808
  counter,
806
- ...[...new Set([item.row.bundle_id, item.row.base_bundle_id])].map((id) => updateBundleRelation(store, id, -1, id === item.row.bundle_id ? -1 : 0)),
809
+ ...bundleIds.map((id) => updateBundleRelation(store, id, -1, id === item.row.bundle_id ? -1 : 0)),
807
810
  deleteAction(store, item)
808
811
  ]);
809
812
  };
@@ -814,7 +817,7 @@ const deleteBundle = async (store, bundle, patches) => {
814
817
  });
815
818
  if (!counter) return;
816
819
  const otherEndpointCounts = /* @__PURE__ */ new Map();
817
- for (const { row } of patches) for (const id of new Set([row.bundle_id, row.base_bundle_id])) {
820
+ for (const { row } of patches) for (const id of /* @__PURE__ */ new Set([row.bundle_id, row.base_bundle_id])) {
818
821
  if (id === bundle.sk) continue;
819
822
  const current = otherEndpointCounts.get(id) ?? {
820
823
  owned: 0,
@@ -865,6 +868,7 @@ const exactDynamoDBField = (where, fieldName) => {
865
868
  return typeof value === "string" ? value : void 0;
866
869
  };
867
870
  const createDynamoDBCrud = (store, updateIndexName) => ({
871
+ recordInsights: (input) => recordDynamoDBInsightsEvent(store, input),
868
872
  async create(input) {
869
873
  switch (input.model) {
870
874
  case "bundles":
@@ -1021,14 +1025,19 @@ const createDynamoDBCrud = (store, updateIndexName) => ({
1021
1025
  if (count !== void 0) return count;
1022
1026
  }
1023
1027
  switch (input.model) {
1024
- case "bundles": return countDistinctDynamoDBRows((await loadBundleItems(store)).map(({ row }) => row).filter((row) => matchesDynamoDBWhere(row, input.where)), distinctFields(input.distinct));
1028
+ case "bundles": {
1029
+ const rows = (await loadBundleItems(store)).map(({ row }) => row).filter((row) => matchesDynamoDBWhere(row, input.where));
1030
+ return countDistinctDynamoDBRows(rows, distinctFields(input.distinct));
1031
+ }
1025
1032
  case "bundle_patches": {
1026
1033
  const ownerId = exactDynamoDBPatchOwner(input.where);
1027
- return countDistinctDynamoDBRows((ownerId ? await queryCompleteOwnerPatches(store, updateIndexName, ownerId) : (await loadPatchItems(store)).map(({ row }) => row)).filter((row) => matchesDynamoDBWhere(row, input.where)), distinctFields(input.distinct));
1034
+ const rows = (ownerId ? await queryCompleteOwnerPatches(store, updateIndexName, ownerId) : (await loadPatchItems(store)).map(({ row }) => row)).filter((row) => matchesDynamoDBWhere(row, input.where));
1035
+ return countDistinctDynamoDBRows(rows, distinctFields(input.distinct));
1028
1036
  }
1029
1037
  case "releases": {
1030
1038
  const scopeKey = exactDynamoDBField(input.where, "scope_key");
1031
- return countDistinctDynamoDBRows((scopeKey === void 0 ? await loadReleaseItems(store) : await loadReleaseItemsByScope(store, scopeKey)).map(({ row }) => row).filter((row) => matchesDynamoDBWhere(row, input.where)), distinctFields(input.distinct));
1039
+ const rows = (scopeKey === void 0 ? await loadReleaseItems(store) : await loadReleaseItemsByScope(store, scopeKey)).map(({ row }) => row).filter((row) => matchesDynamoDBWhere(row, input.where));
1040
+ return countDistinctDynamoDBRows(rows, distinctFields(input.distinct));
1032
1041
  }
1033
1042
  }
1034
1043
  throw new DynamoDBUnsupportedModelError();
@@ -1046,7 +1055,6 @@ const createDynamoDBCrud = (store, updateIndexName) => ({
1046
1055
  if (ownerId === void 0) break;
1047
1056
  return (await queryCompleteOwnerPatches(store, updateIndexName, ownerId)).find((row) => matchesDynamoDBWhere(row, input.where)) ?? null;
1048
1057
  }
1049
- break;
1050
1058
  case "channels":
1051
1059
  if (id !== void 0) return (await loadChannelItem(store, id))?.row ?? null;
1052
1060
  return (await loadChannelItems(store)).map(({ row }) => row).find((row) => matchesDynamoDBWhere(row, input.where)) ?? null;
@@ -1102,6 +1110,7 @@ const createDynamoDBCrud = (store, updateIndexName) => ({
1102
1110
  deleteChannel: (input) => deleteDynamoDBChannel(store, input)
1103
1111
  });
1104
1112
  var DynamoDBDuplicatePatchError = class extends Error {
1113
+ patchId;
1105
1114
  name = "DynamoDBDuplicatePatchError";
1106
1115
  constructor(patchId) {
1107
1116
  super(`DynamoDB bundle mutation contains duplicate patch "${patchId}"`);
@@ -1214,7 +1223,6 @@ const compileAndCommitDynamoDBChanges = async (store, input) => {
1214
1223
  const originalApiKeys = /* @__PURE__ */ new Map();
1215
1224
  const apiKeys = /* @__PURE__ */ new Map();
1216
1225
  const apiKeyHashes = /* @__PURE__ */ new Map();
1217
- const analytics = /* @__PURE__ */ new Map();
1218
1226
  const rememberApiKey = (value) => {
1219
1227
  if (value === null || apiKeys.has(value.row.id)) return;
1220
1228
  originalApiKeys.set(value.row.id, value);
@@ -1337,48 +1345,40 @@ const compileAndCommitDynamoDBChanges = async (store, input) => {
1337
1345
  patches.set(change.row.id, change.row);
1338
1346
  } else for (const [id, patch] of patches) if (patch.bundle_id === change.where.bundleId) patches.delete(id);
1339
1347
  break;
1340
- case "analytics": {
1341
- const key = analyticsSortKey(change.row);
1342
- if (analytics.has(key)) throw new DynamoDBCommitStateError(`Analytics event "${change.row.id}" is duplicated`);
1343
- analytics.set(key, change.row);
1344
- break;
1348
+ case "apiKeys": if (change.operation === "insert") {
1349
+ rememberApiKey(await loadApiKeyByHash(store, change.row.hash));
1350
+ if (apiKeyHashes.has(change.row.hash)) break;
1351
+ rememberApiKey(await loadApiKey(store, change.row.id));
1352
+ if (apiKeys.has(change.row.id)) throw new DynamoDBCommitStateError(`API key id "${change.row.id}" already exists`);
1353
+ const inserted = {
1354
+ row: change.row,
1355
+ version: 1
1356
+ };
1357
+ apiKeys.set(change.row.id, inserted);
1358
+ apiKeyHashes.set(change.row.hash, change.row.id);
1359
+ } else {
1360
+ rememberApiKey(await loadApiKey(store, change.where.id));
1361
+ const current = apiKeys.get(change.where.id);
1362
+ if (current === void 0) return {
1363
+ committed: false,
1364
+ conflict: {
1365
+ changeIndex,
1366
+ reason: "not_found"
1367
+ }
1368
+ };
1369
+ apiKeys.set(change.where.id, {
1370
+ row: {
1371
+ ...current.row,
1372
+ revoked_at_ms: change.update.revokedAtMs
1373
+ },
1374
+ version: current.version
1375
+ });
1345
1376
  }
1346
- case "apiKeys":
1347
- 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
1355
- };
1356
- apiKeys.set(change.row.id, inserted);
1357
- apiKeyHashes.set(change.row.hash, change.row.id);
1358
- } else {
1359
- rememberApiKey(await loadApiKey(store, change.where.id));
1360
- const current = apiKeys.get(change.where.id);
1361
- if (current === void 0) return {
1362
- committed: false,
1363
- conflict: {
1364
- changeIndex,
1365
- reason: "not_found"
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
- break;
1377
1377
  }
1378
1378
  const relationCounts = /* @__PURE__ */ new Map();
1379
1379
  const ownedPatchCounts = /* @__PURE__ */ new Map();
1380
1380
  for (const patch of patches.values()) {
1381
- for (const id of new Set([patch.bundle_id, patch.base_bundle_id])) relationCounts.set(id, (relationCounts.get(id) ?? 0) + 1);
1381
+ for (const id of /* @__PURE__ */ new Set([patch.bundle_id, patch.base_bundle_id])) relationCounts.set(id, (relationCounts.get(id) ?? 0) + 1);
1382
1382
  ownedPatchCounts.set(patch.bundle_id, (ownedPatchCounts.get(patch.bundle_id) ?? 0) + 1);
1383
1383
  }
1384
1384
  const channelReferenceCounts = /* @__PURE__ */ new Map();
@@ -1487,7 +1487,7 @@ const compileAndCommitDynamoDBChanges = async (store, input) => {
1487
1487
  ...original ? { ExpressionAttributeValues: { ":version": original.version } } : {}
1488
1488
  } });
1489
1489
  }
1490
- const changedReleaseIds = new Set([...new Set([...originalReleases.keys(), ...releases.keys()])].filter((id) => {
1490
+ const changedReleaseIds = new Set([.../* @__PURE__ */ new Set([...originalReleases.keys(), ...releases.keys()])].filter((id) => {
1491
1491
  const original = originalReleases.get(id)?.row;
1492
1492
  const next = releases.get(id);
1493
1493
  return original === void 0 || next === void 0 || !rowsEqual(original, next);
@@ -1529,17 +1529,6 @@ const compileAndCommitDynamoDBChanges = async (store, input) => {
1529
1529
  bundle_patches: patches.size - originalPatches.size
1530
1530
  });
1531
1531
  if (counter !== void 0) actions.push(counter);
1532
- for (const [key, row] of analytics) actions.push({ Put: {
1533
- TableName: store.tableName,
1534
- Item: boundedDynamoDBMetadataItem({
1535
- pk: DYNAMODB_ANALYTICS_PARTITION,
1536
- sk: key,
1537
- version: 1,
1538
- row
1539
- }),
1540
- ConditionExpression: "attribute_not_exists(#pk)",
1541
- ExpressionAttributeNames: { "#pk": "pk" }
1542
- } });
1543
1532
  for (const [id, current] of apiKeys) {
1544
1533
  const original = originalApiKeys.get(id);
1545
1534
  if (original !== void 0 && rowsEqual(original.row, current.row)) continue;
@@ -1573,9 +1562,15 @@ const createDynamoDBCommit = (store) => async (input) => {
1573
1562
  return compileAndCommitDynamoDBChanges(store, input);
1574
1563
  }
1575
1564
  };
1576
- const DYNAMODB_ANALYTICS_PARTITION = "bundle_events";
1565
+ const DYNAMODB_INSIGHTS_PARTITION = "bundle_events";
1566
+ const DYNAMODB_INSIGHTS_INSTALLATIONS_PARTITION = "_hot-updater#insights-installations";
1567
+ const DYNAMODB_INSIGHTS_EVENT_IDS_PARTITION = "_hot-updater#insights-event-ids";
1568
+ const DYNAMODB_INSIGHTS_BUNDLE_PREFIX = "_hot-updater#insights-bundle#";
1577
1569
  const DYNAMODB_API_KEY_PARTITION = "api_keys";
1578
1570
  const DYNAMODB_API_KEY_HASH_PARTITION = "_hot-updater#api-key-hashes";
1571
+ const DYNAMODB_INSIGHTS_MOVEMENT_PREFIX = "_hot-updater#insights-movement#";
1572
+ const DYNAMODB_INSIGHTS_USER_PREFIX = "_hot-updater#insights-user#";
1573
+ const DYNAMODB_INSIGHTS_RECORD_ATTEMPTS = 3;
1579
1574
  const hasValidBundleEventShape = (value) => {
1580
1575
  const type = field(value, "type");
1581
1576
  const fromBundleId = field(value, "from_bundle_id");
@@ -1583,6 +1578,7 @@ const hasValidBundleEventShape = (value) => {
1583
1578
  return (type === "UPDATE_APPLIED" || type === "RECOVERED" || type === "RELEASE_ADOPTED") && typeof fromBundleId === "string" && (updateStrategy === "fingerprint" || updateStrategy === "appVersion") || type === "UNCHANGED" && fromBundleId === null && updateStrategy === null;
1584
1579
  };
1585
1580
  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);
1581
+ 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";
1586
1582
  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");
1587
1583
  const parseOfficialRowItem = (value, partition, isRow) => {
1588
1584
  if (value.pk !== partition || typeof value.sk !== "string" || typeof value.version !== "number" || !isRow(value.row)) throw new DynamoDBStoredItemError();
@@ -1592,52 +1588,279 @@ const parseOfficialRowItem = (value, partition, isRow) => {
1592
1588
  };
1593
1589
  };
1594
1590
  const timestampSortKey = (timestampMs) => Math.trunc(timestampMs).toString().padStart(16, "0");
1595
- const analyticsSortKey = (row) => `${timestampSortKey(row.received_at_ms)}#${row.id}`;
1596
- const createDynamoDBAnalyticsTable = (store) => ({
1597
- async append(row) {
1598
- await store.client.send(new _aws_sdk_lib_dynamodb.PutCommand({
1591
+ const insightsSortKey = (row) => `${timestampSortKey(row.received_at_ms)}#${row.id}`;
1592
+ const insightsMovementPartition = (installId) => `${DYNAMODB_INSIGHTS_MOVEMENT_PREFIX}${installId}`;
1593
+ const insightsUserPartition = (userId) => `${DYNAMODB_INSIGHTS_USER_PREFIX}${userId}`;
1594
+ const insightsBundlePartition = (filter) => {
1595
+ const scope = JSON.stringify([
1596
+ filter.platform,
1597
+ filter.channel,
1598
+ filter.type,
1599
+ filter.type === "RECOVERED" ? filter.fromBundleId : filter.toBundleId
1600
+ ]);
1601
+ return `${DYNAMODB_INSIGHTS_BUNDLE_PREFIX}${(0, node_crypto.createHash)("sha256").update(scope, "utf8").digest("hex")}`;
1602
+ };
1603
+ const toInsightsBundleItem = (row) => row.type === "UNCHANGED" ? null : boundedDynamoDBMetadataItem({
1604
+ pk: insightsBundlePartition({
1605
+ platform: row.platform,
1606
+ channel: row.channel,
1607
+ ...row.type === "RECOVERED" ? {
1608
+ type: row.type,
1609
+ fromBundleId: row.from_bundle_id
1610
+ } : {
1611
+ type: row.type,
1612
+ toBundleId: row.to_bundle_id
1613
+ }
1614
+ }),
1615
+ sk: insightsSortKey(row),
1616
+ version: 1,
1617
+ row
1618
+ });
1619
+ const toInsightsEventItem = (row) => {
1620
+ const orderKey = insightsSortKey(row);
1621
+ return boundedDynamoDBMetadataItem({
1622
+ pk: DYNAMODB_INSIGHTS_PARTITION,
1623
+ sk: orderKey,
1624
+ version: 1,
1625
+ row,
1626
+ ...(0, _hot_updater_plugin_core.isInsightsMovementEvent)(row) ? {
1627
+ gsi1pk: insightsMovementPartition(row.install_id),
1628
+ gsi1sk: orderKey
1629
+ } : {}
1630
+ });
1631
+ };
1632
+ const toInsightsInstallationItem = (row) => boundedDynamoDBMetadataItem({
1633
+ pk: DYNAMODB_INSIGHTS_INSTALLATIONS_PARTITION,
1634
+ sk: row.install_id,
1635
+ order_key: insightsSortKey(row),
1636
+ version: 1,
1637
+ row
1638
+ });
1639
+ const toInsightsUserItem = (row) => boundedDynamoDBMetadataItem({
1640
+ pk: insightsUserPartition(row.user_id),
1641
+ sk: row.install_id,
1642
+ order_key: insightsSortKey(row),
1643
+ version: 1,
1644
+ row
1645
+ });
1646
+ const parseInsightsInstallationItem = (value, partition) => {
1647
+ const row = value.row;
1648
+ 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();
1649
+ return value;
1650
+ };
1651
+ const loadInsightsInstallationItem = async (store, installId) => {
1652
+ const { Item } = await store.client.send(new _aws_sdk_lib_dynamodb.GetCommand({
1653
+ TableName: store.tableName,
1654
+ Key: {
1655
+ pk: DYNAMODB_INSIGHTS_INSTALLATIONS_PARTITION,
1656
+ sk: installId
1657
+ },
1658
+ ConsistentRead: true
1659
+ }));
1660
+ return Item === void 0 ? null : parseInsightsInstallationItem(Item, DYNAMODB_INSIGHTS_INSTALLATIONS_PARTITION);
1661
+ };
1662
+ const advancesInsightsInstallation = (row, current) => row.received_at_ms > current.received_at_ms || row.received_at_ms === current.received_at_ms && row.id > current.id;
1663
+ const recordDynamoDBInsightsEvent = async (store, { event: row, installation: next }) => {
1664
+ const eventItem = toInsightsEventItem(row);
1665
+ const bundleItem = toInsightsBundleItem(row);
1666
+ const identityKey = {
1667
+ pk: DYNAMODB_INSIGHTS_EVENT_IDS_PARTITION,
1668
+ sk: row.id
1669
+ };
1670
+ for (let attempt = 0; attempt < DYNAMODB_INSIGHTS_RECORD_ATTEMPTS; attempt++) {
1671
+ const { Item: accepted } = await store.client.send(new _aws_sdk_lib_dynamodb.GetCommand({
1599
1672
  TableName: store.tableName,
1600
- Item: boundedDynamoDBMetadataItem({
1601
- pk: DYNAMODB_ANALYTICS_PARTITION,
1602
- sk: analyticsSortKey(row),
1603
- version: 1,
1604
- row
1605
- }),
1606
- ConditionExpression: "attribute_not_exists(#pk)",
1607
- ExpressionAttributeNames: { "#pk": "pk" }
1673
+ Key: identityKey,
1674
+ ConsistentRead: true
1608
1675
  }));
1676
+ if (accepted !== void 0) return;
1677
+ const current = await loadInsightsInstallationItem(store, row.install_id);
1678
+ const actions = [
1679
+ { Put: {
1680
+ TableName: store.tableName,
1681
+ Item: identityKey,
1682
+ ConditionExpression: "attribute_not_exists(#pk)",
1683
+ ExpressionAttributeNames: { "#pk": "pk" }
1684
+ } },
1685
+ { Put: {
1686
+ TableName: store.tableName,
1687
+ Item: eventItem
1688
+ } },
1689
+ ...bundleItem === null ? [] : [{ Put: {
1690
+ TableName: store.tableName,
1691
+ Item: bundleItem
1692
+ } }]
1693
+ ];
1694
+ if (current === null || advancesInsightsInstallation(next, current.row)) {
1695
+ actions.push({ Put: {
1696
+ TableName: store.tableName,
1697
+ Item: toInsightsInstallationItem(next),
1698
+ ConditionExpression: current === null ? "attribute_not_exists(#pk)" : "#orderKey = :currentOrderKey",
1699
+ ExpressionAttributeNames: current === null ? { "#pk": "pk" } : { "#orderKey": "order_key" },
1700
+ ...current === null ? {} : { ExpressionAttributeValues: { ":currentOrderKey": current.order_key } }
1701
+ } });
1702
+ if (current !== null && current.row.user_id !== null && current.row.user_id !== next.user_id) actions.push({ Delete: {
1703
+ TableName: store.tableName,
1704
+ Key: {
1705
+ pk: insightsUserPartition(current.row.user_id),
1706
+ sk: current.row.install_id
1707
+ }
1708
+ } });
1709
+ if (next.user_id !== null) actions.push({ Put: {
1710
+ TableName: store.tableName,
1711
+ Item: toInsightsUserItem({
1712
+ ...next,
1713
+ user_id: next.user_id
1714
+ })
1715
+ } });
1716
+ }
1717
+ try {
1718
+ await commitDynamoDBTransaction(store, actions);
1719
+ return;
1720
+ } catch (error) {
1721
+ if (!isDynamoDBTransactionConflict(error)) throw error;
1722
+ const { Item } = await store.client.send(new _aws_sdk_lib_dynamodb.GetCommand({
1723
+ TableName: store.tableName,
1724
+ Key: identityKey,
1725
+ ConsistentRead: true
1726
+ }));
1727
+ if (Item !== void 0) return;
1728
+ if (attempt === 2) throw error;
1729
+ }
1730
+ }
1731
+ };
1732
+ const insightsEventRange = (input) => {
1733
+ const upperExclusive = input.after ? `${timestampSortKey(input.after.receivedAtMs)}#${input.after.id}` : `${timestampSortKey(input.beforeReceivedAtMs)}#`;
1734
+ const upper = `${upperExclusive.slice(0, -1)}${String.fromCharCode(upperExclusive.charCodeAt(upperExclusive.length - 1) - 1)}~`;
1735
+ const movement = input.filter.kind === "installationMovement";
1736
+ const partition = movement ? insightsMovementPartition(input.filter.installId) : input.filter.kind === "bundle" ? insightsBundlePartition(input.filter) : DYNAMODB_INSIGHTS_PARTITION;
1737
+ return {
1738
+ partition,
1739
+ query: {
1740
+ ...movement ? { IndexName: DYNAMODB_UPDATE_INDEX_NAME } : { ConsistentRead: true },
1741
+ KeyConditionExpression: "#partition = :partition AND #order BETWEEN :since AND :upper",
1742
+ ExpressionAttributeNames: {
1743
+ "#partition": movement ? "gsi1pk" : "pk",
1744
+ "#order": movement ? "gsi1sk" : "sk"
1745
+ },
1746
+ ExpressionAttributeValues: {
1747
+ ":partition": partition,
1748
+ ":since": `${timestampSortKey(input.sinceMs ?? 0)}#`,
1749
+ ":upper": upper
1750
+ }
1751
+ }
1752
+ };
1753
+ };
1754
+ const createDynamoDBInsightsTable = (store) => ({
1755
+ record: (input) => recordDynamoDBInsightsEvent(store, input),
1756
+ async listEvents(input) {
1757
+ if ((input.sinceMs ?? 0) === input.beforeReceivedAtMs) return [];
1758
+ const range = insightsEventRange(input);
1759
+ const rows = [];
1760
+ let exclusiveStartKey;
1761
+ do {
1762
+ const page = await store.client.send(new _aws_sdk_lib_dynamodb.QueryCommand({
1763
+ TableName: store.tableName,
1764
+ ...range.query,
1765
+ ExclusiveStartKey: exclusiveStartKey,
1766
+ Limit: input.limit - rows.length,
1767
+ ScanIndexForward: false
1768
+ }));
1769
+ 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));
1770
+ exclusiveStartKey = page.LastEvaluatedKey;
1771
+ } while (rows.length < input.limit && exclusiveStartKey !== void 0);
1772
+ return rows;
1609
1773
  },
1610
- async scan(input) {
1611
- if (input.limit <= 0) return [];
1612
- if (input.after !== void 0 && input.after.receivedAtMs >= input.beforeReceivedAtMs) return [];
1774
+ async findInstallations(input) {
1775
+ if ("installId" in input) {
1776
+ const stored = await loadInsightsInstallationItem(store, input.installId);
1777
+ return stored === null ? [] : [stored.row];
1778
+ }
1779
+ const partition = insightsUserPartition(input.userId);
1613
1780
  const rows = [];
1614
- const afterKey = input.after === void 0 ? void 0 : `${timestampSortKey(input.after.receivedAtMs)}#${input.after.id}`;
1615
1781
  let exclusiveStartKey;
1616
1782
  do {
1617
1783
  const page = await store.client.send(new _aws_sdk_lib_dynamodb.QueryCommand({
1618
1784
  TableName: store.tableName,
1785
+ ConsistentRead: true,
1619
1786
  ExclusiveStartKey: exclusiveStartKey,
1620
- KeyConditionExpression: afterKey === void 0 ? "#pk = :pk AND #sk < :before" : "#pk = :pk AND #sk BETWEEN :after AND :before",
1787
+ KeyConditionExpression: input.afterInstallId === void 0 ? "#pk = :pk" : "#pk = :pk AND #sk > :after",
1621
1788
  ExpressionAttributeNames: {
1622
1789
  "#pk": "pk",
1623
- "#sk": "sk"
1790
+ ...input.afterInstallId === void 0 ? {} : { "#sk": "sk" }
1624
1791
  },
1625
1792
  ExpressionAttributeValues: {
1626
- ":before": `${timestampSortKey(input.beforeReceivedAtMs)}#`,
1627
- ":pk": DYNAMODB_ANALYTICS_PARTITION,
1628
- ...afterKey === void 0 ? {} : { ":after": afterKey }
1793
+ ":pk": partition,
1794
+ ...input.afterInstallId === void 0 ? {} : { ":after": input.afterInstallId }
1629
1795
  },
1630
- Limit: input.limit - rows.length + (afterKey === void 0 ? 0 : 1),
1796
+ Limit: input.limit - rows.length,
1631
1797
  ScanIndexForward: true
1632
1798
  }));
1633
- for (const item of page.Items ?? []) {
1634
- if (item.sk === afterKey) continue;
1635
- rows.push(parseOfficialRowItem(item, DYNAMODB_ANALYTICS_PARTITION, isBundleEventRow).row);
1636
- if (rows.length === input.limit) return rows;
1799
+ for (const item of (page.Items ?? []).slice(0, input.limit - rows.length)) {
1800
+ const stored = parseInsightsInstallationItem(item, partition);
1801
+ const current = await loadInsightsInstallationItem(store, stored.row.install_id);
1802
+ if (current?.row.user_id === input.userId) rows.push(current.row);
1637
1803
  }
1638
1804
  exclusiveStartKey = page.LastEvaluatedKey;
1639
- } while (exclusiveStartKey !== void 0);
1805
+ } while (rows.length < input.limit && exclusiveStartKey !== void 0);
1640
1806
  return rows;
1807
+ },
1808
+ async countInstallations(input) {
1809
+ let count = 0;
1810
+ let exclusiveStartKey;
1811
+ do {
1812
+ const page = await store.client.send(new _aws_sdk_lib_dynamodb.QueryCommand({
1813
+ TableName: store.tableName,
1814
+ ConsistentRead: true,
1815
+ ExclusiveStartKey: exclusiveStartKey,
1816
+ KeyConditionExpression: "#pk = :pk",
1817
+ FilterExpression: "#row.#platform = :platform AND #row.#channel = :channel AND #row.#received >= :since" + (input.bundleId === void 0 ? "" : " AND #row.#bundle = :bundle"),
1818
+ ExpressionAttributeNames: {
1819
+ "#pk": "pk",
1820
+ "#row": "row",
1821
+ "#platform": "platform",
1822
+ "#channel": "channel",
1823
+ "#received": "received_at_ms",
1824
+ ...input.bundleId === void 0 ? {} : { "#bundle": "to_bundle_id" }
1825
+ },
1826
+ ExpressionAttributeValues: {
1827
+ ":pk": DYNAMODB_INSIGHTS_INSTALLATIONS_PARTITION,
1828
+ ":platform": input.platform,
1829
+ ":channel": input.channel,
1830
+ ":since": input.sinceMs,
1831
+ ...input.bundleId === void 0 ? {} : { ":bundle": input.bundleId }
1832
+ },
1833
+ Select: "COUNT",
1834
+ ScanIndexForward: true
1835
+ }));
1836
+ count += page.Count ?? 0;
1837
+ exclusiveStartKey = page.LastEvaluatedKey;
1838
+ } while (exclusiveStartKey !== void 0);
1839
+ return count;
1840
+ },
1841
+ async countEvents(input) {
1842
+ if (input.sinceMs === input.beforeReceivedAtMs) return 0;
1843
+ const { query } = insightsEventRange({
1844
+ ...input,
1845
+ filter: {
1846
+ kind: "bundle",
1847
+ ...input.filter
1848
+ },
1849
+ limit: 1
1850
+ });
1851
+ let count = 0;
1852
+ let exclusiveStartKey;
1853
+ do {
1854
+ const page = await store.client.send(new _aws_sdk_lib_dynamodb.QueryCommand({
1855
+ TableName: store.tableName,
1856
+ ...query,
1857
+ ExclusiveStartKey: exclusiveStartKey,
1858
+ Select: "COUNT"
1859
+ }));
1860
+ count += page.Count ?? 0;
1861
+ exclusiveStartKey = page.LastEvaluatedKey;
1862
+ } while (exclusiveStartKey !== void 0);
1863
+ return count;
1641
1864
  }
1642
1865
  });
1643
1866
  const apiKeyItem = (row, version) => ({
@@ -1770,7 +1993,7 @@ const dynamoDB = (config) => {
1770
1993
  models: {
1771
1994
  ...adapter.models,
1772
1995
  bundlePatches: createDynamoDBBundlePatchTable(store, DYNAMODB_UPDATE_INDEX_NAME),
1773
- analytics: createDynamoDBAnalyticsTable(store),
1996
+ insights: createDynamoDBInsightsTable(store),
1774
1997
  apiKeys: createDynamoDBApiKeyTable(store)
1775
1998
  },
1776
1999
  async commit(input) {
@@ -1783,7 +2006,7 @@ const dynamoDB = (config) => {
1783
2006
  };
1784
2007
  //#endregion
1785
2008
  //#region src/runtimeAwsConfig.ts
1786
- const truthyValues = new Set([
2009
+ const truthyValues = /* @__PURE__ */ new Set([
1787
2010
  "1",
1788
2011
  "true",
1789
2012
  "yes",
@@ -1818,7 +2041,7 @@ const applySsmRuntimeAwsConfig = (config) => {
1818
2041
  };
1819
2042
  //#endregion
1820
2043
  //#region src/cloudFrontDownloadUrl.ts
1821
- const ONE_YEAR_IN_SECONDS = 3600 * 24 * 365;
2044
+ const ONE_YEAR_IN_SECONDS = 31536e3;
1822
2045
  const privateKeyCache = /* @__PURE__ */ new Map();
1823
2046
  const getPrivateKeyFromSsm = async (region, parameterName) => {
1824
2047
  const value = (await new _aws_sdk_client_ssm.SSM(applySsmRuntimeAwsConfig({ region })).getParameter({
@@ -1865,7 +2088,8 @@ function s3Storage(config) {
1865
2088
  const normalizedBasePath = basePath?.replace(/^\/+|\/+$/g, "") ?? "";
1866
2089
  const getStorageKey = (0, _hot_updater_plugin_core.createStorageKeyBuilder)(normalizedBasePath);
1867
2090
  const getListPrefix = (prefix = "") => {
1868
- const value = [normalizedBasePath, prefix.replace(/^\/+|\/+$/g, "")].filter(Boolean).join("/");
2091
+ const normalizedPrefix = prefix.replace(/^\/+|\/+$/g, "");
2092
+ const value = [normalizedBasePath, normalizedPrefix].filter(Boolean).join("/");
1869
2093
  return value ? `${value}/` : "";
1870
2094
  };
1871
2095
  const getRelativeKey = (key) => {