@hot-updater/aws 1.2.0-rc.43 → 1.2.0-rc.45

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 +104 -40
  2. package/package.json +8 -8
@@ -2631,6 +2631,10 @@ const iosDevices = defineTable({
2631
2631
  eq: ["status"],
2632
2632
  sort: ["id"]
2633
2633
  },
2634
+ byRegistration: {
2635
+ eq: [],
2636
+ sort: ["registered_at_ms"]
2637
+ },
2634
2638
  all: {
2635
2639
  eq: [],
2636
2640
  sort: ["id"]
@@ -2880,6 +2884,12 @@ const WINDOW_MS$1 = 6e4;
2880
2884
  /** Log rows a compaction reads from each log table for one group. */
2881
2885
  const LOG_PAGE = 200;
2882
2886
  /**
2887
+ * How old a log row this process can't apply must be before its reads step
2888
+ * past it: older than any transaction still committing, between servers
2889
+ * whose clocks agree this closely.
2890
+ */
2891
+ const SKIP_AFTER_MS = 3e5;
2892
+ /**
2883
2893
  * Groups one compaction applies: after a commit, so the request that runs
2884
2894
  * it waits for one group; before a read; and on flush.
2885
2895
  */
@@ -3010,23 +3020,41 @@ const rewrite = (change, key, current) => {
3010
3020
  previous: current
3011
3021
  };
3012
3022
  };
3023
+ /** A shard row's place in what `readCurrent` read. */
3024
+ const rowId = (model, key) => JSON.stringify([model.table.name, key]);
3025
+ /** The shard key each change writes: shard 0, since one compaction runs at a time. */
3026
+ const keysOf = (changes) => changes.map(({ key }) => [...key.slice(0, -1), 0]);
3013
3027
  /**
3014
- * The write for each change on shard 0, since one compaction runs at a time:
3015
- * a blind increment that creates the row for counters, and a guarded rewrite
3016
- * for gauges and sketches, whose rows are read in one batch per table.
3028
+ * The current rows of the changes that rewrite theirs (gauges and sketches),
3029
+ * each once, in one batch per table.
3017
3030
  */
3018
- const compile = async (adapter, changes) => {
3019
- const keys = changes.map(({ key }) => [...key.slice(0, -1), 0]);
3031
+ const readCurrent = async (adapter, changes) => {
3032
+ const keys = keysOf(changes);
3020
3033
  const reads = /* @__PURE__ */ new Map();
3021
3034
  changes.forEach((change, at) => {
3022
- if (rewrites(change)) reads.set(change.model, [...reads.get(change.model) ?? [], at]);
3035
+ if (!rewrites(change)) return;
3036
+ const wanted = reads.get(change.model) ?? /* @__PURE__ */ new Map();
3037
+ wanted.set(rowId(change.model, keys[at]), keys[at]);
3038
+ reads.set(change.model, wanted);
3023
3039
  });
3024
3040
  const current = /* @__PURE__ */ new Map();
3025
- await Promise.all([...reads].map(async ([model, positions]) => {
3026
- const rows = await adapter.get(model.table, positions.map((at) => keys[at]));
3027
- positions.forEach((at, index) => current.set(at, rows[index] ?? null));
3041
+ await Promise.all([...reads].map(async ([model, wanted]) => {
3042
+ const keys = [...wanted.values()];
3043
+ const rows = await adapter.get(model.table, keys);
3044
+ keys.forEach((key, at) => {
3045
+ current.set(rowId(model, key), rows[at] ?? null);
3046
+ });
3028
3047
  }));
3029
- return changes.map((change, at) => current.has(at) ? rewrite(change, keys[at], current.get(at)) : increment(change, keys[at]));
3048
+ return current;
3049
+ };
3050
+ /**
3051
+ * The write for each change: a blind increment that creates the row for
3052
+ * counters, and a guarded rewrite for gauges and sketches, over the rows
3053
+ * `current` read.
3054
+ */
3055
+ const build = (changes, current) => {
3056
+ const keys = keysOf(changes);
3057
+ return changes.map((change, at) => rewrites(change) ? rewrite(change, keys[at], current.get(rowId(change.model, keys[at])) ?? null) : increment(change, keys[at]));
3030
3058
  };
3031
3059
  const isOp = (op) => op !== void 0;
3032
3060
  const sleep$2 = (attempt) => new Promise((resolve) => setTimeout(resolve, Math.min(250, 5 * 2 ** attempt) * Math.random()));
@@ -3108,6 +3136,16 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3108
3136
  let compacting;
3109
3137
  /** Log rows this process read, by `[log table's index, id]`, for the deletes after a group. */
3110
3138
  const known = /* @__PURE__ */ new Map();
3139
+ /**
3140
+ * Each log table's last row of a run at its head that this process can't
3141
+ * apply (it names an aggregate or metric this schema lacks), whose reads
3142
+ * start past it. The rows wait for a process that can apply them, and a
3143
+ * restart reads them again.
3144
+ */
3145
+ const skipped = LOG_TABLES.map(() => void 0);
3146
+ const past = (shard) => skipped[shard] === void 0 ? {} : { range: { gt: skipped[shard] } };
3147
+ /** When a log row's transaction stamped its ID. */
3148
+ const stampOf = (id) => Number.parseInt(id.slice(0, 9), 36);
3111
3149
  const idOf = (shard, id) => JSON.stringify([shard, id]);
3112
3150
  /** The lease row as stored once `set` is written over it. */
3113
3151
  const leaseOp = (held, set) => {
@@ -3223,14 +3261,22 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3223
3261
  logs: (await Promise.all(LOG_TABLES.map(async (name, shard) => {
3224
3262
  const page = await engine.reads.findMany(name, {
3225
3263
  index: "oldest",
3226
- limit: LOG_PAGE
3264
+ limit: LOG_PAGE,
3265
+ ...past(shard)
3227
3266
  });
3228
3267
  more ||= page.next !== void 0;
3229
- return (await Promise.all(page.rows.map(async (row) => ({
3268
+ const decoded = await Promise.all(page.rows.map(async (row) => ({
3230
3269
  shard,
3231
3270
  row,
3232
3271
  changes: await decode(String(row.changes))
3233
- })))).flatMap(({ changes, ...entry }) => changes === void 0 ? [] : [{
3272
+ })));
3273
+ const settled = now() - SKIP_AFTER_MS;
3274
+ for (const { row, changes } of decoded) {
3275
+ const id = String(row.id);
3276
+ if (changes !== void 0 || stampOf(id) >= settled) break;
3277
+ skipped[shard] = id;
3278
+ }
3279
+ return decoded.flatMap(({ changes, ...entry }) => changes === void 0 ? [] : [{
3234
3280
  ...entry,
3235
3281
  changes
3236
3282
  }]);
@@ -3238,7 +3284,6 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3238
3284
  more
3239
3285
  };
3240
3286
  };
3241
- const rowsFor = async (changes) => (await compile(adapter, changes)).filter(isOp);
3242
3287
  /**
3243
3288
  * Applies the oldest pending log rows whose merged changes fit in one
3244
3289
  * atomic write with the lease, which lists them as applied; the rows are
@@ -3247,12 +3292,14 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3247
3292
  * log rows it applied whole, or undefined when a conflict stopped it.
3248
3293
  */
3249
3294
  const applyGroup = async (held, logs) => {
3295
+ const current = await readCurrent(adapter, logs.flatMap((entry) => [...entry.changes.values()]));
3296
+ const rowsFor = (changes) => build(changes, current).filter(isOp);
3250
3297
  for (let count = logs.length; count > 0; count = Math.floor(count / 2)) {
3251
3298
  const batch = logs.slice(0, count);
3252
3299
  const changes = mergeAll(/* @__PURE__ */ new Map(), batch.flatMap((entry) => [...entry.changes.values()]));
3253
3300
  const applied = batch.map(({ shard, row }) => [shard, row.id]);
3254
3301
  const lease = leaseOp(held, { applied: JSON.stringify(applied) });
3255
- const ops = [...await rowsFor([...changes.values()]), lease.op];
3302
+ const ops = [...rowsFor([...changes.values()]), lease.op];
3256
3303
  if (!adapter.fits(ops)) continue;
3257
3304
  for (const { shard, row } of batch) known.set(idOf(shard, row.id), row);
3258
3305
  return await commit(ops) ? {
@@ -3274,7 +3321,7 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3274
3321
  };
3275
3322
  const lease = leaseOp(held, {});
3276
3323
  const ops = [
3277
- ...await rowsFor(entries.slice(0, take)),
3324
+ ...rowsFor(entries.slice(0, take)),
3278
3325
  kept,
3279
3326
  lease.op
3280
3327
  ];
@@ -3295,11 +3342,12 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3295
3342
  if (held === void 0) return;
3296
3343
  let pending = true;
3297
3344
  let clear = false;
3345
+ let carried;
3298
3346
  try {
3299
3347
  for (let group = 0;; group += 1) {
3300
3348
  clear = await deleteApplied(held);
3301
3349
  if (!clear || !pending || group === groups) break;
3302
- const { logs, more } = await readLogs();
3350
+ const { logs, more } = carried ?? await readLogs();
3303
3351
  if (logs.length === 0) {
3304
3352
  pending = false;
3305
3353
  break;
@@ -3307,6 +3355,11 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3307
3355
  const applied = await applyGroup(held, logs);
3308
3356
  if (applied === void 0) break;
3309
3357
  [held, clear] = [applied.held, false];
3358
+ const rest = applied.count === 0 ? [] : logs.slice(applied.count);
3359
+ carried = rest.length > 0 ? {
3360
+ logs: rest,
3361
+ more
3362
+ } : void 0;
3310
3363
  pending = more || applied.count < logs.length;
3311
3364
  }
3312
3365
  } finally {
@@ -3326,9 +3379,10 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3326
3379
  });
3327
3380
  let checking;
3328
3381
  /** Whether any log row waits; reads at once share one check. */
3329
- const hasPending = () => checking ??= Promise.all(LOG_TABLES.map((name) => engine.reads.findMany(name, {
3382
+ const hasPending = () => checking ??= Promise.all(LOG_TABLES.map((name, shard) => engine.reads.findMany(name, {
3330
3383
  index: "oldest",
3331
- limit: 1
3384
+ limit: 1,
3385
+ ...past(shard)
3332
3386
  }))).then((pages) => pages.some((page) => page.rows.length > 0)).finally(() => {
3333
3387
  checking = void 0;
3334
3388
  });
@@ -4791,7 +4845,7 @@ const createKvAdapter = ({ store, tablePrefix = "" }) => {
4791
4845
  };
4792
4846
  //#endregion
4793
4847
  //#region ../../packages/server/dist/version.mjs
4794
- const HOT_UPDATER_SERVER_VERSION = "1.2.0-rc.43";
4848
+ const HOT_UPDATER_SERVER_VERSION = "1.2.0-rc.45";
4795
4849
  //#endregion
4796
4850
  //#region ../../packages/server/dist/handlerVersionRoutes.mjs
4797
4851
  /**
@@ -5884,8 +5938,6 @@ const NAME_MAX_LENGTH = 100;
5884
5938
  const EMAIL_MAX_LENGTH = 320;
5885
5939
  /** `ios_devices.apple_issuer_id`'s bound. */
5886
5940
  const ISSUER_MAX_LENGTH = 64;
5887
- /** How many registry rows one read takes while finding its account. */
5888
- const ACCOUNT_SCAN_PAGE = 200;
5889
5941
  /** The one row of `ios_device_account`. */
5890
5942
  const ACCOUNT_ROW = "registry";
5891
5943
  /** `ios_devices.apple_error`'s bound. */
@@ -5960,25 +6012,19 @@ const createIosDeviceApi = (db, { now = Date.now } = {}) => {
5960
6012
  /**
5961
6013
  * The issuer of the registry's earliest registration among its rows, by
5962
6014
  * when and then by ID, or null when no row names one: read only while the
5963
- * registry has no account yet, a page at a time.
6015
+ * registry has no account yet, in one read of its first registration. A
6016
+ * row names an issuer exactly when it has a registration time: both are
6017
+ * set by its first registration and never cleared.
5964
6018
  */
5965
6019
  const accountOfRows = async () => {
5966
- let earliest = null;
5967
- for (let after;;) {
5968
- const { rows } = await db.findMany("ios_devices", {
5969
- index: "all",
5970
- where: {},
5971
- order: "asc",
5972
- limit: ACCOUNT_SCAN_PAGE,
5973
- ...after === void 0 ? {} : { range: { gt: after } }
5974
- });
5975
- for (const row of rows.map(pick$1)) {
5976
- if (row.apple_issuer_id === null) continue;
5977
- if (earliest === null || (row.registered_at_ms ?? 0) < (earliest.registered_at_ms ?? 0)) earliest = row;
5978
- }
5979
- if (rows.length < ACCOUNT_SCAN_PAGE) return earliest?.apple_issuer_id ?? null;
5980
- after = rows.at(-1).id;
5981
- }
6020
+ const { rows } = await db.findMany("ios_devices", {
6021
+ index: "byRegistration",
6022
+ where: {},
6023
+ order: "asc",
6024
+ limit: 1,
6025
+ range: { gte: 0 }
6026
+ });
6027
+ return (rows[0] === void 0 ? null : pick$1(rows[0]))?.apple_issuer_id ?? null;
5982
6028
  };
5983
6029
  return {
5984
6030
  async listIosDevices(input) {
@@ -6367,6 +6413,12 @@ const EXPORT_METHODS = /* @__PURE__ */ new Set([
6367
6413
  * a bundle when a build made after the bundle is the newest.
6368
6414
  */
6369
6415
  const OLDER_BUILDS_PAGE = 20;
6416
+ /**
6417
+ * How many pages of older builds that look reads before it answers null: 100
6418
+ * builds made after the bundle in one scope, where a deploy's new bundle has
6419
+ * none or a few registered while it deploys.
6420
+ */
6421
+ const OLDER_BUILDS_PAGES = 5;
6370
6422
  /** How many times a mark runs again after losing its scope's mark to another. */
6371
6423
  const MARK_ATTEMPTS = 8;
6372
6424
  /** `native_builds.build_number`'s bound in core's schema. */
@@ -6602,6 +6654,10 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
6602
6654
  const row = await db.findOne("native_builds", { id });
6603
6655
  return row === null ? null : pick(row);
6604
6656
  },
6657
+ async findNativeBuildByBuiltInBundle(bundleId) {
6658
+ const row = await db.findOne("native_builds", { min_bundle_id: bundleId });
6659
+ return row === null ? null : pick(row);
6660
+ },
6605
6661
  async findNativeBuild(scope, options) {
6606
6662
  const scopeKey = nativeBuildScopeKey(scope);
6607
6663
  const buildKey = options?.buildKey;
@@ -6650,7 +6706,8 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
6650
6706
  }).then(({ rows: [row] }) => row?.artifact_kind === artifactKind ? pick(row) : void 0) : void 0);
6651
6707
  if (newest === void 0 || !ofKind(newest)) return null;
6652
6708
  if (bundleId === void 0 || newest.min_bundle_id < bundleId) return newest;
6653
- for (let before = newest.id;;) {
6709
+ let before = newest.id;
6710
+ for (let page = 0; page < OLDER_BUILDS_PAGES; page += 1) {
6654
6711
  const { rows: older } = await read(OLDER_BUILDS_PAGE, before);
6655
6712
  const usable = older.find(({ min_bundle_id }) => min_bundle_id < bundleId);
6656
6713
  if (usable !== void 0) return ofKind(usable) ? pick(usable) : null;
@@ -6658,6 +6715,7 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
6658
6715
  if (older.length < OLDER_BUILDS_PAGE || last === void 0) return null;
6659
6716
  before = last.id;
6660
6717
  }
6718
+ return null;
6661
6719
  },
6662
6720
  async findNativeBuildSibling(build) {
6663
6721
  const artifactKind = SIBLING_KINDS[build.artifact_kind];
@@ -7871,6 +7929,7 @@ const createAdminRouteHandlers = () => ({
7871
7929
  return json$2({ data: build === null ? null : await core.findNativeBuildSibling(build) });
7872
7930
  }),
7873
7931
  getNativeBuild: onCore(async (core, _request, params) => json$2({ data: await core.getNativeBuild(requireRouteParam(params, "id")) })),
7932
+ findNativeBuildByBuiltInBundle: onCore(async (core, _request, params) => json$2({ data: await core.findNativeBuildByBuiltInBundle(requireRouteParam(params, "bundleId")) })),
7874
7933
  listNativeBuilds: onCore(async (core, request) => {
7875
7934
  const url = new URL(request.url);
7876
7935
  const input = keyset(url);
@@ -8101,6 +8160,11 @@ const ADMIN_ROUTES = [
8101
8160
  path: "/native-builds/latest",
8102
8161
  handler: "findNativeBuild"
8103
8162
  },
8163
+ {
8164
+ method: "GET",
8165
+ path: "/native-builds/built-in/:bundleId",
8166
+ handler: "findNativeBuildByBuiltInBundle"
8167
+ },
8104
8168
  {
8105
8169
  method: "GET",
8106
8170
  path: "/native-builds/:id/sibling",
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.43",
4
+ "version": "1.2.0-rc.45",
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.43",
49
- "@hot-updater/mock": "1.2.0-rc.43",
50
- "@hot-updater/test-utils": "1.2.0-rc.43"
48
+ "@hot-updater/protocol": "1.2.0-rc.45",
49
+ "@hot-updater/mock": "1.2.0-rc.45",
50
+ "@hot-updater/test-utils": "1.2.0-rc.45"
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.43",
68
- "@hot-updater/plugin-core": "1.2.0-rc.43",
69
- "@hot-updater/server": "1.2.0-rc.43",
70
- "@hot-updater/storage-s3": "1.2.0-rc.43"
67
+ "@hot-updater/plugin-core": "1.2.0-rc.45",
68
+ "@hot-updater/storage-s3": "1.2.0-rc.45",
69
+ "@hot-updater/server": "1.2.0-rc.45",
70
+ "@hot-updater/cli-tools": "1.2.0-rc.45"
71
71
  },
72
72
  "scripts": {
73
73
  "build": "tsdown",