@hot-updater/vercel 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.
@@ -2064,7 +2064,12 @@ const sqlTableShapes = (dialect, tables, tablePrefix = "") => tables.map((table)
2064
2064
  name: `${name}__${index.name}`,
2065
2065
  columns: entries,
2066
2066
  key: columns,
2067
- indexes: []
2067
+ indexes: [{
2068
+ kind: "index",
2069
+ name: shortSqlName(`${name}__${index.name}__key`),
2070
+ unique: false,
2071
+ columns: table.key
2072
+ }]
2068
2073
  }
2069
2074
  }];
2070
2075
  }
@@ -2134,9 +2139,17 @@ const pruneStatements = (dialect, table, tablePrefix, limit) => {
2134
2139
  const order = retentionOrder(table).columns.map(quote);
2135
2140
  const list = (alias, direction = "") => order.map((column) => `${alias}.${column}${direction}`).join(", ");
2136
2141
  const oldest = (before) => `SELECT ${list("i")} FROM ${name} i WHERE i.${at} <= ${before()} ORDER BY ${list("i", " ASC")} LIMIT ${limit}`;
2137
- const doomed = (alias, before) => `${alias}.${at} <= ${before()} AND (${list(alias)}) <= (SELECT ${list("b")} FROM (${oldest(before)}) b ORDER BY ${list("b", " DESC")} LIMIT 1)`;
2142
+ const doomed = (alias, before) => `(${list(alias)}) <= (SELECT ${list("b")} FROM (${oldest(before)}) b ORDER BY ${list("b", " DESC")} LIMIT 1) AND ${alias}.${at} <= ${before()}`;
2138
2143
  const entries = table.indexes.filter((index) => isMultiIndex(table, index)).map((index) => quote(`${tablePrefix}${table.name}__${index.name}`));
2139
2144
  const keyOf = (left, right) => table.key.map((column) => `${left}.${quote(column)} = ${right}.${quote(column)}`).join(" AND ");
2145
+ if (dialect === "sqlite") return [...table.indexes.filter((index) => isMultiIndex(table, index)).map((index) => {
2146
+ const columns = [...index.eq, ...indexOrderColumns(table, index)];
2147
+ const multi = columns.filter((column) => findPhysicalColumn(table, column).multi);
2148
+ const values = columns.map((column) => multi.includes(column) ? `j${multi.indexOf(column)}.${quote("value")}` : `b.${quote(column)}`);
2149
+ const each = multi.map((column, at) => `, json_each(b.${quote(column)}) j${at}`);
2150
+ const into = quote(`${tablePrefix}${table.name}__${index.name}`);
2151
+ return (before) => `DELETE FROM ${into} WHERE (${columns.map(quote).join(", ")}) IN (SELECT ${values.join(", ")} FROM ${name} b${each.join("")} WHERE ${doomed("b", before)})`;
2152
+ }), (before) => `DELETE FROM ${name} WHERE ${doomed(name, before)}`];
2140
2153
  if (dialect === "mysql") return [...entries.map((index) => (before) => `DELETE e FROM ${index} e JOIN (${oldest(before)}) d ON ${keyOf("e", "d")}`), (before) => `DELETE FROM ${name} WHERE ${at} <= ${before()} ORDER BY ${order.join(", ")} LIMIT ${limit}`];
2141
2154
  return [...entries.map((index) => (before) => `DELETE FROM ${index} WHERE EXISTS (SELECT * FROM ${name} b WHERE ${keyOf("b", index)} AND ${doomed("b", before)})`), (before) => `DELETE FROM ${name} WHERE ${doomed(name, before)}`];
2142
2155
  };
@@ -2586,6 +2599,10 @@ const iosDevices = defineTable({
2586
2599
  eq: ["status"],
2587
2600
  sort: ["id"]
2588
2601
  },
2602
+ byRegistration: {
2603
+ eq: [],
2604
+ sort: ["registered_at_ms"]
2605
+ },
2589
2606
  all: {
2590
2607
  eq: [],
2591
2608
  sort: ["id"]
@@ -2799,6 +2816,7 @@ const LEASE = { id: "compaction" };
2799
2816
  const LEASE_MS = 3e4;
2800
2817
  const WINDOW_MS$1 = 6e4;
2801
2818
  const LOG_PAGE = 200;
2819
+ const SKIP_AFTER_MS = 3e5;
2802
2820
  const GROUPS = {
2803
2821
  commit: 1,
2804
2822
  read: 8,
@@ -2905,18 +2923,30 @@ const rewrite$1 = (change, key, current) => {
2905
2923
  previous: current
2906
2924
  };
2907
2925
  };
2908
- const compile = async (adapter, changes) => {
2909
- const keys = changes.map(({ key }) => [...key.slice(0, -1), 0]);
2926
+ const rowId = (model, key) => JSON.stringify([model.table.name, key]);
2927
+ const keysOf = (changes) => changes.map(({ key }) => [...key.slice(0, -1), 0]);
2928
+ const readCurrent = async (adapter, changes) => {
2929
+ const keys = keysOf(changes);
2910
2930
  const reads = /* @__PURE__ */ new Map();
2911
2931
  changes.forEach((change, at) => {
2912
- if (rewrites(change)) reads.set(change.model, [...reads.get(change.model) ?? [], at]);
2932
+ if (!rewrites(change)) return;
2933
+ const wanted = reads.get(change.model) ?? /* @__PURE__ */ new Map();
2934
+ wanted.set(rowId(change.model, keys[at]), keys[at]);
2935
+ reads.set(change.model, wanted);
2913
2936
  });
2914
2937
  const current = /* @__PURE__ */ new Map();
2915
- await Promise.all([...reads].map(async ([model, positions]) => {
2916
- const rows = await adapter.get(model.table, positions.map((at) => keys[at]));
2917
- positions.forEach((at, index) => current.set(at, rows[index] ?? null));
2938
+ await Promise.all([...reads].map(async ([model, wanted]) => {
2939
+ const keys = [...wanted.values()];
2940
+ const rows = await adapter.get(model.table, keys);
2941
+ keys.forEach((key, at) => {
2942
+ current.set(rowId(model, key), rows[at] ?? null);
2943
+ });
2918
2944
  }));
2919
- return changes.map((change, at) => current.has(at) ? rewrite$1(change, keys[at], current.get(at)) : increment(change, keys[at]));
2945
+ return current;
2946
+ };
2947
+ const build = (changes, current) => {
2948
+ const keys = keysOf(changes);
2949
+ return changes.map((change, at) => rewrites(change) ? rewrite$1(change, keys[at], current.get(rowId(change.model, keys[at])) ?? null) : increment(change, keys[at]));
2920
2950
  };
2921
2951
  const isOp = (op) => op !== void 0;
2922
2952
  const sleep = (attempt) => new Promise((resolve) => setTimeout(resolve, Math.min(250, 5 * 2 ** attempt) * Math.random()));
@@ -2982,6 +3012,9 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
2982
3012
  let nextDue = 0;
2983
3013
  let compacting;
2984
3014
  const known = /* @__PURE__ */ new Map();
3015
+ const skipped = LOG_TABLES.map(() => void 0);
3016
+ const past = (shard) => skipped[shard] === void 0 ? {} : { range: { gt: skipped[shard] } };
3017
+ const stampOf = (id) => Number.parseInt(id.slice(0, 9), 36);
2985
3018
  const idOf = (shard, id) => JSON.stringify([shard, id]);
2986
3019
  const leaseOp = (held, set) => {
2987
3020
  const next = {
@@ -3085,14 +3118,22 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3085
3118
  logs: (await Promise.all(LOG_TABLES.map(async (name, shard) => {
3086
3119
  const page = await engine.reads.findMany(name, {
3087
3120
  index: "oldest",
3088
- limit: LOG_PAGE
3121
+ limit: LOG_PAGE,
3122
+ ...past(shard)
3089
3123
  });
3090
3124
  more ||= page.next !== void 0;
3091
- return (await Promise.all(page.rows.map(async (row) => ({
3125
+ const decoded = await Promise.all(page.rows.map(async (row) => ({
3092
3126
  shard,
3093
3127
  row,
3094
3128
  changes: await decode(String(row.changes))
3095
- })))).flatMap(({ changes, ...entry }) => changes === void 0 ? [] : [{
3129
+ })));
3130
+ const settled = now() - SKIP_AFTER_MS;
3131
+ for (const { row, changes } of decoded) {
3132
+ const id = String(row.id);
3133
+ if (changes !== void 0 || stampOf(id) >= settled) break;
3134
+ skipped[shard] = id;
3135
+ }
3136
+ return decoded.flatMap(({ changes, ...entry }) => changes === void 0 ? [] : [{
3096
3137
  ...entry,
3097
3138
  changes
3098
3139
  }]);
@@ -3100,14 +3141,15 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3100
3141
  more
3101
3142
  };
3102
3143
  };
3103
- const rowsFor = async (changes) => (await compile(adapter, changes)).filter(isOp);
3104
3144
  const applyGroup = async (held, logs) => {
3145
+ const current = await readCurrent(adapter, logs.flatMap((entry) => [...entry.changes.values()]));
3146
+ const rowsFor = (changes) => build(changes, current).filter(isOp);
3105
3147
  for (let count = logs.length; count > 0; count = Math.floor(count / 2)) {
3106
3148
  const batch = logs.slice(0, count);
3107
3149
  const changes = mergeAll(/* @__PURE__ */ new Map(), batch.flatMap((entry) => [...entry.changes.values()]));
3108
3150
  const applied = batch.map(({ shard, row }) => [shard, row.id]);
3109
3151
  const lease = leaseOp(held, { applied: JSON.stringify(applied) });
3110
- const ops = [...await rowsFor([...changes.values()]), lease.op];
3152
+ const ops = [...rowsFor([...changes.values()]), lease.op];
3111
3153
  if (!adapter.fits(ops)) continue;
3112
3154
  for (const { shard, row } of batch) known.set(idOf(shard, row.id), row);
3113
3155
  return await commit(ops) ? {
@@ -3129,7 +3171,7 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3129
3171
  };
3130
3172
  const lease = leaseOp(held, {});
3131
3173
  const ops = [
3132
- ...await rowsFor(entries.slice(0, take)),
3174
+ ...rowsFor(entries.slice(0, take)),
3133
3175
  kept,
3134
3176
  lease.op
3135
3177
  ];
@@ -3145,11 +3187,12 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3145
3187
  if (held === void 0) return;
3146
3188
  let pending = true;
3147
3189
  let clear = false;
3190
+ let carried;
3148
3191
  try {
3149
3192
  for (let group = 0;; group += 1) {
3150
3193
  clear = await deleteApplied(held);
3151
3194
  if (!clear || !pending || group === groups) break;
3152
- const { logs, more } = await readLogs();
3195
+ const { logs, more } = carried ?? await readLogs();
3153
3196
  if (logs.length === 0) {
3154
3197
  pending = false;
3155
3198
  break;
@@ -3157,6 +3200,11 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3157
3200
  const applied = await applyGroup(held, logs);
3158
3201
  if (applied === void 0) break;
3159
3202
  [held, clear] = [applied.held, false];
3203
+ const rest = applied.count === 0 ? [] : logs.slice(applied.count);
3204
+ carried = rest.length > 0 ? {
3205
+ logs: rest,
3206
+ more
3207
+ } : void 0;
3160
3208
  pending = more || applied.count < logs.length;
3161
3209
  }
3162
3210
  } finally {
@@ -3174,9 +3222,10 @@ const createAggregateBatches = ({ engine, adapter, schema, batching: { windowMs
3174
3222
  compacting = void 0;
3175
3223
  });
3176
3224
  let checking;
3177
- const hasPending = () => checking ??= Promise.all(LOG_TABLES.map((name) => engine.reads.findMany(name, {
3225
+ const hasPending = () => checking ??= Promise.all(LOG_TABLES.map((name, shard) => engine.reads.findMany(name, {
3178
3226
  index: "oldest",
3179
- limit: 1
3227
+ limit: 1,
3228
+ ...past(shard)
3180
3229
  }))).then((pages) => pages.some((page) => page.rows.length > 0)).finally(() => {
3181
3230
  checking = void 0;
3182
3231
  });
@@ -4347,19 +4396,19 @@ const createSqlAdapter = ({ executor, tablePrefix = "", maxOps = 1e3, maxParams
4347
4396
  const where = (bind, key, v) => `${keyMatch(bind, table, key)}${v === void 0 ? "" : ` AND ${version} = ${bind(v)}`}${when ? ` AND ${when(bind)}` : ""}`;
4348
4397
  const insert = (bind, into, columns, values) => `INSERT INTO ${into} (${columns.map(quote).join(", ")}) ${when ? `SELECT ${values.join(", ")} WHERE ${when(bind)}` : `VALUES (${values.join(", ")})`}`;
4349
4398
  const insertRow = (bind, row) => insert(bind, name, table.columns.map((column) => column.name), table.columns.map((column) => bind(row[column.name], column)));
4350
- const entries = (key, row, changed) => table.indexes.flatMap((index) => {
4399
+ const entries = (key, row, changed, stale = true) => table.indexes.flatMap((index) => {
4351
4400
  const columns = [...index.eq, ...indexOrderColumns(table, index)];
4352
4401
  if (!isMultiIndex(table, index) || changed && !columns.some((column) => changed.includes(column))) return [];
4353
4402
  const into = tableOf(table, index);
4354
4403
  const found = row ? indexEntries(table, index, row) : [];
4355
4404
  const order = found.length > 0 ? indexOrderTuple(table, index, row) : [];
4356
- return [sql((bind) => `DELETE FROM ${into} WHERE ${where(bind, key)}`), ...found.map((eq) => sql((bind) => {
4405
+ return [...stale ? [sql((bind) => `DELETE FROM ${into} WHERE ${where(bind, key)}`)] : [], ...found.map((eq) => sql((bind) => {
4357
4406
  const values = [...eq, ...order].map((value) => bind(value));
4358
4407
  return insert(bind, into, columns, values);
4359
4408
  }))];
4360
4409
  });
4361
4410
  switch (op.type) {
4362
- case "insert": return [sql((bind) => insertRow(bind, op.row)), ...entries(rowKey(table, op.row), op.row)];
4411
+ case "insert": return [sql((bind) => insertRow(bind, op.row)), ...entries(rowKey(table, op.row), op.row, void 0, false)];
4363
4412
  case "patch": return [sql((bind) => {
4364
4413
  const set = Object.entries(op.set).map(([column, value]) => `${quote(column)} = ${bind(value, findPhysicalColumn(table, column))}`);
4365
4414
  return `UPDATE ${name} SET ${[...set, `${version} = ${version} + 1`].join(", ")} WHERE ${where(bind, op.key, op.guard.v)}`;
@@ -17739,7 +17788,7 @@ const postgres = (config) => {
17739
17788
  };
17740
17789
  //#endregion
17741
17790
  //#region ../../packages/server/dist/version.mjs
17742
- const HOT_UPDATER_SERVER_VERSION = "1.2.0-rc.43";
17791
+ const HOT_UPDATER_SERVER_VERSION = "1.2.0-rc.45";
17743
17792
  //#endregion
17744
17793
  //#region ../../packages/server/dist/handlerVersionRoutes.mjs
17745
17794
  const createVersionRouteHandlers = (plugins = []) => {
@@ -18746,7 +18795,6 @@ const normalizeIosUdid = (value) => {
18746
18795
  const NAME_MAX_LENGTH = 100;
18747
18796
  const EMAIL_MAX_LENGTH = 320;
18748
18797
  const ISSUER_MAX_LENGTH = 64;
18749
- const ACCOUNT_SCAN_PAGE = 200;
18750
18798
  const ACCOUNT_ROW = "registry";
18751
18799
  const ERROR_MAX_LENGTH = 512;
18752
18800
  const STATUSES = [
@@ -18807,22 +18855,14 @@ const createIosDeviceApi = (db, { now = Date.now } = {}) => {
18807
18855
  return row === null ? null : pick$1(row);
18808
18856
  };
18809
18857
  const accountOfRows = async () => {
18810
- let earliest = null;
18811
- for (let after;;) {
18812
- const { rows } = await db.findMany("ios_devices", {
18813
- index: "all",
18814
- where: {},
18815
- order: "asc",
18816
- limit: ACCOUNT_SCAN_PAGE,
18817
- ...after === void 0 ? {} : { range: { gt: after } }
18818
- });
18819
- for (const row of rows.map(pick$1)) {
18820
- if (row.apple_issuer_id === null) continue;
18821
- if (earliest === null || (row.registered_at_ms ?? 0) < (earliest.registered_at_ms ?? 0)) earliest = row;
18822
- }
18823
- if (rows.length < ACCOUNT_SCAN_PAGE) return earliest?.apple_issuer_id ?? null;
18824
- after = rows.at(-1).id;
18825
- }
18858
+ const { rows } = await db.findMany("ios_devices", {
18859
+ index: "byRegistration",
18860
+ where: {},
18861
+ order: "asc",
18862
+ limit: 1,
18863
+ range: { gte: 0 }
18864
+ });
18865
+ return (rows[0] === void 0 ? null : pick$1(rows[0]))?.apple_issuer_id ?? null;
18826
18866
  };
18827
18867
  return {
18828
18868
  async listIosDevices(input) {
@@ -19171,6 +19211,7 @@ const EXPORT_METHODS = /* @__PURE__ */ new Set([
19171
19211
  "enterprise"
19172
19212
  ]);
19173
19213
  const OLDER_BUILDS_PAGE = 20;
19214
+ const OLDER_BUILDS_PAGES = 5;
19174
19215
  const MARK_ATTEMPTS = 8;
19175
19216
  const BUILD_NUMBER_MAX_LENGTH = 64;
19176
19217
  const SUBMITTED_VERSION_MAX_LENGTH = 255;
@@ -19361,6 +19402,10 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
19361
19402
  const row = await db.findOne("native_builds", { id });
19362
19403
  return row === null ? null : pick(row);
19363
19404
  },
19405
+ async findNativeBuildByBuiltInBundle(bundleId) {
19406
+ const row = await db.findOne("native_builds", { min_bundle_id: bundleId });
19407
+ return row === null ? null : pick(row);
19408
+ },
19364
19409
  async findNativeBuild(scope, options) {
19365
19410
  const scopeKey = nativeBuildScopeKey(scope);
19366
19411
  const buildKey = options?.buildKey;
@@ -19409,7 +19454,8 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
19409
19454
  }).then(({ rows: [row] }) => row?.artifact_kind === artifactKind ? pick(row) : void 0) : void 0);
19410
19455
  if (newest === void 0 || !ofKind(newest)) return null;
19411
19456
  if (bundleId === void 0 || newest.min_bundle_id < bundleId) return newest;
19412
- for (let before = newest.id;;) {
19457
+ let before = newest.id;
19458
+ for (let page = 0; page < OLDER_BUILDS_PAGES; page += 1) {
19413
19459
  const { rows: older } = await read(OLDER_BUILDS_PAGE, before);
19414
19460
  const usable = older.find(({ min_bundle_id }) => min_bundle_id < bundleId);
19415
19461
  if (usable !== void 0) return ofKind(usable) ? pick(usable) : null;
@@ -19417,6 +19463,7 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
19417
19463
  if (older.length < OLDER_BUILDS_PAGE || last === void 0) return null;
19418
19464
  before = last.id;
19419
19465
  }
19466
+ return null;
19420
19467
  },
19421
19468
  async findNativeBuildSibling(build) {
19422
19469
  const artifactKind = SIBLING_KINDS[build.artifact_kind];
@@ -20569,6 +20616,7 @@ const createAdminRouteHandlers = () => ({
20569
20616
  return json$2({ data: build === null ? null : await core.findNativeBuildSibling(build) });
20570
20617
  }),
20571
20618
  getNativeBuild: onCore(async (core, _request, params) => json$2({ data: await core.getNativeBuild(requireRouteParam(params, "id")) })),
20619
+ findNativeBuildByBuiltInBundle: onCore(async (core, _request, params) => json$2({ data: await core.findNativeBuildByBuiltInBundle(requireRouteParam(params, "bundleId")) })),
20572
20620
  listNativeBuilds: onCore(async (core, request) => {
20573
20621
  const url = new URL(request.url);
20574
20622
  const input = keyset(url);
@@ -20798,6 +20846,11 @@ const ADMIN_ROUTES = [
20798
20846
  path: "/native-builds/latest",
20799
20847
  handler: "findNativeBuild"
20800
20848
  },
20849
+ {
20850
+ method: "GET",
20851
+ path: "/native-builds/built-in/:bundleId",
20852
+ handler: "findNativeBuildByBuiltInBundle"
20853
+ },
20801
20854
  {
20802
20855
  method: "GET",
20803
20856
  path: "/native-builds/:id/sibling",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hot-updater/vercel",
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",
@@ -41,11 +41,11 @@
41
41
  "@vercel/cli-auth": "0.3.10",
42
42
  "@vercel/cli-config": "0.3.1",
43
43
  "pg": "8.13.1",
44
- "@hot-updater/plugin-core": "1.2.0-rc.43",
45
- "@hot-updater/cli-tools": "1.2.0-rc.43",
46
- "@hot-updater/server": "1.2.0-rc.43",
47
- "@hot-updater/postgres": "1.2.0-rc.43",
48
- "@hot-updater/protocol": "1.2.0-rc.43"
44
+ "@hot-updater/cli-tools": "1.2.0-rc.45",
45
+ "@hot-updater/protocol": "1.2.0-rc.45",
46
+ "@hot-updater/postgres": "1.2.0-rc.45",
47
+ "@hot-updater/plugin-core": "1.2.0-rc.45",
48
+ "@hot-updater/server": "1.2.0-rc.45"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@types/node": "^20",
@@ -54,7 +54,7 @@
54
54
  "execa": "9.5.2",
55
55
  "kysely": "0.28.17",
56
56
  "vitest": "4.1.4",
57
- "@hot-updater/test-utils": "1.2.0-rc.43"
57
+ "@hot-updater/test-utils": "1.2.0-rc.45"
58
58
  },
59
59
  "scripts": {
60
60
  "build": "tsdown",