@hot-updater/aws 1.2.0-rc.51 → 1.2.0-rc.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/lambda/index.cjs +113 -16
  2. package/package.json +8 -8
@@ -1356,6 +1356,76 @@ function canonicalStringify(value) {
1356
1356
  const record = value;
1357
1357
  return `{${Object.keys(record).sort().map((key) => `${JSON.stringify(key)}:${canonicalStringify(record[key])}`).join(",")}}`;
1358
1358
  }
1359
+ const BOUND_SHAPE = {
1360
+ inclusive: null,
1361
+ version: null
1362
+ };
1363
+ /**
1364
+ * Every key this release's compiler writes in a catalog payload, where it
1365
+ * writes it: the envelope's, a segment's and its bounds', and a release
1366
+ * descriptor's. A key outside it was written by a later release.
1367
+ */
1368
+ const COMPILED_CATALOG_SHAPE = {
1369
+ fallbackPolicy: null,
1370
+ schemaVersion: null,
1371
+ strategy: null,
1372
+ releaseIndexes: null,
1373
+ rollbackReleaseIndexes: null,
1374
+ segments: {
1375
+ lower: BOUND_SHAPE,
1376
+ upper: BOUND_SHAPE,
1377
+ releaseIndexes: null,
1378
+ rollbackReleaseIndexes: null
1379
+ },
1380
+ releaseDescriptors: {
1381
+ releaseId: null,
1382
+ kind: null,
1383
+ bundleId: null,
1384
+ rolloutCohortCount: null,
1385
+ targetCohorts: null,
1386
+ shouldForceUpdate: null,
1387
+ message: null,
1388
+ label: null
1389
+ }
1390
+ };
1391
+ /**
1392
+ * A stored catalog payload as this release reads it: canonical, with the
1393
+ * keys a later release added left out, and whether it had any, a later
1394
+ * schema version included. Server core compares a catalog by the fields
1395
+ * its compiler writes, and refuses to rewrite one a later release wrote,
1396
+ * which would drop what that release added.
1397
+ */
1398
+ function readCompiledCatalogPayload(payload) {
1399
+ let fromLaterRelease = false;
1400
+ const known = (value, shape) => {
1401
+ if (Array.isArray(value)) return value.map((item) => known(item, shape));
1402
+ if (value === null || typeof value !== "object") return value;
1403
+ const entries = Object.entries(value).filter(([key]) => {
1404
+ if (Object.hasOwn(shape, key)) return true;
1405
+ fromLaterRelease = true;
1406
+ return false;
1407
+ });
1408
+ return Object.fromEntries(entries.map(([key, entry]) => {
1409
+ const inner = shape[key];
1410
+ return [key, inner === null ? entry : known(entry, inner)];
1411
+ }));
1412
+ };
1413
+ let parsed;
1414
+ try {
1415
+ parsed = JSON.parse(payload);
1416
+ } catch {
1417
+ parsed = null;
1418
+ }
1419
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return {
1420
+ canonicalPayload: payload,
1421
+ fromLaterRelease: false
1422
+ };
1423
+ if ("schemaVersion" in parsed && typeof parsed.schemaVersion === "number" && parsed.schemaVersion > 1) fromLaterRelease = true;
1424
+ return {
1425
+ canonicalPayload: canonicalStringify(known(parsed, COMPILED_CATALOG_SHAPE)),
1426
+ fromLaterRelease
1427
+ };
1428
+ }
1359
1429
  async function compileReleaseCatalog(input) {
1360
1430
  const releases = validateReleases(input.releases, input.strategy);
1361
1431
  const targetCohorts = new Set(releases.flatMap((release) => release.targetCohorts));
@@ -1943,6 +2013,23 @@ const SETTINGS_TABLE = {
1943
2013
  /** The engine's storage-layout generation; it changes only with a migration. */
1944
2014
  const ENGINE_SCHEMA_KEY = "schema.engine";
1945
2015
  const CORE_SCHEMA_KEY = "schema.core";
2016
+ /** Compares dotted numeric versions, such as "1" and "1.2.0"; NaN when either is not one. */
2017
+ const compareVersions = (left, right) => {
2018
+ const parse = (version) => /^\d+(?:\.\d+)*$/u.test(version) ? version.split(".").map(Number) : [NaN];
2019
+ const [a, b] = [parse(left), parse(right)];
2020
+ if ([...a, ...b].some(Number.isNaN)) return NaN;
2021
+ for (let index = 0; index < Math.max(a.length, b.length); index += 1) {
2022
+ const difference = (a[index] ?? 0) - (b[index] ?? 0);
2023
+ if (difference !== 0) return difference;
2024
+ }
2025
+ return 0;
2026
+ };
2027
+ /**
2028
+ * The `schema.core` a newer release recorded, which `hot-updater db migrate`
2029
+ * never moves back: the fix is to upgrade, not to migrate.
2030
+ */
2031
+ const newerCore = (settings) => settings.find(({ key, expected, found }) => key === CORE_SCHEMA_KEY && found !== null && compareVersions(found, expected) > 0);
2032
+ const newerMessage = (adapterName, { key, expected, found }) => `Hot Updater schema setting "${key}" for ${adapterName} is "${found}"; expected "${expected}". The database was migrated by Hot Updater ${found}: upgrade hot-updater and the @hot-updater packages to ${found} or later.`;
1946
2033
  const stored = ({ key, expected, found }) => `"${key}" is ${found === null ? "missing" : `"${found}"`}; expected "${expected}"`;
1947
2034
  /**
1948
2035
  * How to migrate a database the fence refused: doctor names the command for
@@ -1956,7 +2043,10 @@ const pluginOf = ({ key }) => key.startsWith("schema.") && key !== "schema.engin
1956
2043
  const listed = (items) => items.length < 2 ? items.join("") : `${items.slice(0, -1).join(", ")} and ${items.at(-1)}`;
1957
2044
  const settingsMessage = (adapterName, settings) => `Hot Updater schema settings for ${adapterName} are not migrated: ${settings.map(stored).join(", and ")}. ${MIGRATE_SCHEMA_FIX}`;
1958
2045
  const pluginsMessage = (adapterName, plugins, settings, fix) => `The tables of ${plugins.length === 1 ? "plugin" : "plugins"} ${listed(plugins.map((id) => `"${id}"`))} are not migrated on ${adapterName}: schema ${settings.length === 1 ? "setting" : "settings"} ${settings.map(stored).join(", and ")}. ${fix}`;
1959
- /** The database needs `hot-updater db migrate`, or a new database; the handler answers 503. */
2046
+ /**
2047
+ * The database needs `hot-updater db migrate`, a new database, or a newer
2048
+ * Hot Updater; the handler answers 503.
2049
+ */
1960
2050
  var HotUpdaterSchemaMigrationRequiredError = class extends Error {
1961
2051
  adapterName;
1962
2052
  currentVersion;
@@ -1971,7 +2061,8 @@ var HotUpdaterSchemaMigrationRequiredError = class extends Error {
1971
2061
  constructor(adapterName, currentVersion, setting, options) {
1972
2062
  const settings = options?.settings ?? (setting === void 0 ? [] : [setting]);
1973
2063
  const plugins = settings.flatMap((mismatch) => pluginOf(mismatch) ?? []);
1974
- super(setting === void 0 ? currentVersion === void 0 ? `Hot Updater database schema is not initialized for ${adapterName}. ${MIGRATE_SCHEMA_FIX}` : `Hot Updater v1 cannot migrate schema ${currentVersion} in place. Create a new empty database and run \`hot-updater db migrate\`.` : plugins.length > 0 && plugins.length === settings.length ? pluginsMessage(adapterName, plugins, settings, options?.fix ?? MIGRATE_SCHEMA_FIX) : settings.length > 1 && !(setting.key === "schema.engine" && setting.found === null) ? settingsMessage(adapterName, settings) : settingMessage(adapterName, setting), options?.cause === void 0 ? void 0 : { cause: options.cause });
2064
+ const newer = newerCore(settings);
2065
+ super(newer !== void 0 ? newerMessage(adapterName, newer) : setting === void 0 ? currentVersion === void 0 ? `Hot Updater database schema is not initialized for ${adapterName}. ${MIGRATE_SCHEMA_FIX}` : `Hot Updater v1 cannot migrate schema ${currentVersion} in place. Create a new empty database and run \`hot-updater db migrate\`.` : plugins.length > 0 && plugins.length === settings.length ? pluginsMessage(adapterName, plugins, settings, options?.fix ?? MIGRATE_SCHEMA_FIX) : settings.length > 1 && !(setting.key === "schema.engine" && setting.found === null) ? settingsMessage(adapterName, settings) : settingMessage(adapterName, setting), options?.cause === void 0 ? void 0 : { cause: options.cause });
1975
2066
  this.adapterName = adapterName;
1976
2067
  this.currentVersion = currentVersion;
1977
2068
  this.setting = setting;
@@ -2758,17 +2849,6 @@ const refuseV0Database = (adapterName, stored) => {
2758
2849
  if (typeof version !== "string" || stored.has("schema.engine")) return;
2759
2850
  throw new HotUpdaterSchemaMigrationRequiredError(adapterName, version);
2760
2851
  };
2761
- /** Compares dotted numeric versions, such as "1" and "1.2.0"; NaN when either is not one. */
2762
- const compareVersions = (left, right) => {
2763
- const parse = (version) => /^\d+(?:\.\d+)*$/u.test(version) ? version.split(".").map(Number) : [NaN];
2764
- const [a, b] = [parse(left), parse(right)];
2765
- if ([...a, ...b].some(Number.isNaN)) return NaN;
2766
- for (let index = 0; index < Math.max(a.length, b.length); index += 1) {
2767
- const difference = (a[index] ?? 0) - (b[index] ?? 0);
2768
- if (difference !== 0) return difference;
2769
- }
2770
- return 0;
2771
- };
2772
2852
  /**
2773
2853
  * The settings whose versions only move forward when a `ToolingTarget` names
2774
2854
  * none. `schema.engine` is a storage layout, not a release: a store refuses
@@ -5146,7 +5226,7 @@ const createKvAdapter = ({ store, tablePrefix = "" }) => {
5146
5226
  };
5147
5227
  //#endregion
5148
5228
  //#region ../../packages/server/dist/version.mjs
5149
- const HOT_UPDATER_SERVER_VERSION = "1.2.0-rc.51";
5229
+ const HOT_UPDATER_SERVER_VERSION = "1.2.0-rc.53";
5150
5230
  //#endregion
5151
5231
  //#region ../../packages/server/dist/handlerVersionRoutes.mjs
5152
5232
  /**
@@ -7613,6 +7693,13 @@ const readCatalog = async (tx, scopeKey) => {
7613
7693
  catalog: row === null ? null : toCatalogRow(row)
7614
7694
  };
7615
7695
  };
7696
+ /**
7697
+ * Refuses to rewrite a catalog a later release compiled: this release's
7698
+ * compiler would drop the fields that release added.
7699
+ */
7700
+ const refuseLaterCatalog = (scope, catalog) => {
7701
+ if (catalog !== null && readCompiledCatalogPayload(catalog.payload).fromLaterRelease) fail$1("CATALOG_FROM_LATER_RELEASE", `The ${scope.platform} release catalog of channel "${scope.channelName}" was written by a later Hot Updater.\nUpgrade hot-updater, then try again.`);
7702
+ };
7616
7703
  /** A scope with releases and no compiled catalog lost its history. */
7617
7704
  const assertHistory = (stored, hasReleases) => {
7618
7705
  if (stored.catalog === null && hasReleases) fail$1("CATALOG_IDENTITY_MISSING", "Catalog history is missing. Restore its catalog row from backup before rebuilding or deploying.");
@@ -7626,6 +7713,7 @@ const assertHistory = (stored, hasReleases) => {
7626
7713
  const changeRelease = async (tx, { scope, change, bundle, updatedAtMs }, labels) => {
7627
7714
  checkScope(scope);
7628
7715
  const stored = await readCatalog(tx, scope.scopeKey);
7716
+ refuseLaterCatalog(scope, stored.catalog);
7629
7717
  let enabled = await enabledReleases(tx, scope.scopeKey);
7630
7718
  if (bundle !== void 0) {
7631
7719
  insertBundle(tx, bundle.row);
@@ -7698,8 +7786,16 @@ const changeReleases = (db, inputs) => {
7698
7786
  return results;
7699
7787
  });
7700
7788
  };
7701
- /** Whether `current` is what the scope's enabled releases compile to. */
7702
- const isCurrent = (scope, current, compilation) => current.strategy === scope.strategy && current.channel_id === scope.channelId && current.channel_key === encodeChannelKey(scope.channelName) && current.platform === scope.platform && current.fingerprint_hash === scope.fingerprintHash && current.catalog_hash === compilation.catalogHash;
7789
+ /**
7790
+ * Whether `current` is what the scope's enabled releases compile to, in the
7791
+ * fields this release compiles: a later release's catalog may add others.
7792
+ */
7793
+ const isCurrent = (scope, current, compilation) => {
7794
+ if (current.strategy !== scope.strategy || current.channel_id !== scope.channelId || current.channel_key !== encodeChannelKey(scope.channelName) || current.platform !== scope.platform || current.fingerprint_hash !== scope.fingerprintHash) return false;
7795
+ if (current.catalog_hash === compilation.catalogHash) return true;
7796
+ const read = readCompiledCatalogPayload(current.payload);
7797
+ return read.fromLaterRelease && read.canonicalPayload === compilation.canonicalPayload;
7798
+ };
7703
7799
  /** A compiled payload with its descriptors' labels left out. */
7704
7800
  const withoutLabels = (payload) => {
7705
7801
  const parsed = JSON.parse(payload);
@@ -7761,6 +7857,7 @@ const rebuildScope = async (tx, scope, updatedAtMs) => {
7761
7857
  changed: false,
7762
7858
  diagnostics: compilation.diagnostics
7763
7859
  };
7860
+ refuseLaterCatalog(scope, current);
7764
7861
  const written = await writeCatalog(tx, scope, stored, enabled, updatedAtMs);
7765
7862
  return {
7766
7863
  catalog: written.catalog,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hot-updater/aws",
3
3
  "type": "module",
4
- "version": "1.2.0-rc.51",
4
+ "version": "1.2.0-rc.53",
5
5
  "description": "React Native OTA solution for self-hosted",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.mjs",
@@ -45,9 +45,9 @@
45
45
  "es-toolkit": "^1.32.0",
46
46
  "execa": "9.5.2",
47
47
  "mime": "^4.0.4",
48
- "@hot-updater/protocol": "1.2.0-rc.51",
49
- "@hot-updater/mock": "1.2.0-rc.51",
50
- "@hot-updater/test-utils": "1.2.0-rc.51"
48
+ "@hot-updater/protocol": "1.2.0-rc.53",
49
+ "@hot-updater/mock": "1.2.0-rc.53",
50
+ "@hot-updater/test-utils": "1.2.0-rc.53"
51
51
  },
52
52
  "dependencies": {
53
53
  "@aws-sdk/client-cloudfront": "3.1066.0",
@@ -64,10 +64,10 @@
64
64
  "@aws-sdk/lib-dynamodb": "3.1066.0",
65
65
  "@aws-sdk/s3-request-presigner": "3.1066.0",
66
66
  "hono": "4.12.34",
67
- "@hot-updater/cli-tools": "1.2.0-rc.51",
68
- "@hot-updater/plugin-core": "1.2.0-rc.51",
69
- "@hot-updater/server": "1.2.0-rc.51",
70
- "@hot-updater/storage-s3": "1.2.0-rc.51"
67
+ "@hot-updater/cli-tools": "1.2.0-rc.53",
68
+ "@hot-updater/plugin-core": "1.2.0-rc.53",
69
+ "@hot-updater/server": "1.2.0-rc.53",
70
+ "@hot-updater/storage-s3": "1.2.0-rc.53"
71
71
  },
72
72
  "scripts": {
73
73
  "build": "tsdown",