@hot-updater/plugin-core 0.29.5 → 0.29.6

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.
@@ -2,6 +2,7 @@ import { calculatePagination } from "./calculatePagination.mjs";
2
2
  import { createDatabasePlugin } from "./createDatabasePlugin.mjs";
3
3
  import { filterCompatibleAppVersions } from "./filterCompatibleAppVersions.mjs";
4
4
  import { bundleMatchesQueryWhere, sortBundles } from "./queryBundles.mjs";
5
+ import { paginateBundles } from "./paginateBundles.mjs";
5
6
  import { getUpdateInfo } from "@hot-updater/js";
6
7
  import { orderBy } from "es-toolkit";
7
8
  import semver from "semver";
@@ -46,6 +47,135 @@ function resolveStorageTarget({ targetAppVersion, fingerprintHash }) {
46
47
  if (!target) throw new Error("target not found");
47
48
  return target;
48
49
  }
50
+ const DEFAULT_DESC_ORDER = {
51
+ field: "id",
52
+ direction: "desc"
53
+ };
54
+ const MANAGEMENT_INDEX_PREFIX = "_index";
55
+ const MANAGEMENT_INDEX_VERSION = 1;
56
+ const DEFAULT_MANAGEMENT_INDEX_PAGE_SIZE = 128;
57
+ const ALL_SCOPE_CACHE_KEY = "*|*";
58
+ function resolveManagementIndexPageSize(config) {
59
+ const pageSize = config.managementIndexPageSize ?? DEFAULT_MANAGEMENT_INDEX_PAGE_SIZE;
60
+ if (!Number.isInteger(pageSize) || pageSize < 1) throw new Error("managementIndexPageSize must be a positive integer.");
61
+ return pageSize;
62
+ }
63
+ function sortManagedBundles(bundles, orderBy = DEFAULT_DESC_ORDER) {
64
+ return sortBundles(bundles, orderBy);
65
+ }
66
+ function isDefaultManagementOrder(orderBy) {
67
+ return orderBy === void 0 || orderBy.field === DEFAULT_DESC_ORDER.field && orderBy.direction === DEFAULT_DESC_ORDER.direction;
68
+ }
69
+ function hasUnsupportedManagementFilters(where) {
70
+ if (!where) return false;
71
+ return Boolean(where.enabled !== void 0 || where.id !== void 0 || where.targetAppVersion !== void 0 || where.targetAppVersionIn !== void 0 || where.targetAppVersionNotNull !== void 0 || where.fingerprintHash !== void 0);
72
+ }
73
+ function getSupportedManagementScope(where, orderBy) {
74
+ if (!isDefaultManagementOrder(orderBy) || hasUnsupportedManagementFilters(where)) return null;
75
+ return {
76
+ channel: where?.channel,
77
+ platform: where?.platform
78
+ };
79
+ }
80
+ function encodeScopePart(value) {
81
+ return encodeURIComponent(value);
82
+ }
83
+ function getManagementScopeCacheKey({ channel, platform }) {
84
+ return `${channel ?? "*"}|${platform ?? "*"}`;
85
+ }
86
+ function getManagementScopePrefix({ channel, platform }) {
87
+ if (channel && platform) return `${MANAGEMENT_INDEX_PREFIX}/channel/${encodeScopePart(channel)}/platform/${platform}`;
88
+ if (channel) return `${MANAGEMENT_INDEX_PREFIX}/channel/${encodeScopePart(channel)}`;
89
+ if (platform) return `${MANAGEMENT_INDEX_PREFIX}/platform/${platform}`;
90
+ return `${MANAGEMENT_INDEX_PREFIX}/all`;
91
+ }
92
+ function getManagementRootKey(scope) {
93
+ return `${getManagementScopePrefix(scope)}/root.json`;
94
+ }
95
+ function getManagementPageKey(scope, pageIndex) {
96
+ return `${getManagementScopePrefix(scope)}/pages/${String(pageIndex).padStart(4, "0")}.json`;
97
+ }
98
+ function createBundleWithUpdateJsonKey(bundle) {
99
+ const target = resolveStorageTarget(bundle);
100
+ return {
101
+ ...bundle,
102
+ _updateJsonKey: `${bundle.channel}/${bundle.platform}/${target}/update.json`
103
+ };
104
+ }
105
+ function getPageStartOffsets(pages) {
106
+ const startOffsets = [];
107
+ let offset = 0;
108
+ for (const page of pages) {
109
+ startOffsets.push(offset);
110
+ offset += page.count;
111
+ }
112
+ return startOffsets;
113
+ }
114
+ function createEmptyManagementResult(limit) {
115
+ return {
116
+ data: [],
117
+ pagination: calculatePagination(0, {
118
+ limit,
119
+ offset: 0
120
+ })
121
+ };
122
+ }
123
+ function buildManagementIndexArtifacts(allBundles, pageSize) {
124
+ const sortedAllBundles = sortManagedBundles(allBundles);
125
+ const pages = /* @__PURE__ */ new Map();
126
+ const scopes = [];
127
+ const channels = [...new Set(sortedAllBundles.map((bundle) => bundle.channel))].sort();
128
+ const addScope = (scope, scopeBundles, options) => {
129
+ if (!options?.includeChannels && scopeBundles.length === 0) return;
130
+ const pageKeys = [];
131
+ const pageDescriptors = [];
132
+ for (let pageIndex = 0; pageIndex * pageSize < scopeBundles.length; pageIndex++) {
133
+ const page = scopeBundles.slice(pageIndex * pageSize, (pageIndex + 1) * pageSize);
134
+ const key = getManagementPageKey(scope, pageIndex);
135
+ pages.set(key, page);
136
+ pageKeys.push(key);
137
+ pageDescriptors.push({
138
+ key,
139
+ count: page.length,
140
+ firstId: page[0].id,
141
+ lastId: page.at(-1).id
142
+ });
143
+ }
144
+ const root = {
145
+ version: MANAGEMENT_INDEX_VERSION,
146
+ pageSize,
147
+ total: scopeBundles.length,
148
+ pages: pageDescriptors,
149
+ ...options?.includeChannels ? { channels } : {}
150
+ };
151
+ scopes.push({
152
+ cacheKey: getManagementScopeCacheKey(scope),
153
+ rootKey: getManagementRootKey(scope),
154
+ root,
155
+ pageKeys
156
+ });
157
+ };
158
+ addScope({}, sortedAllBundles, { includeChannels: true });
159
+ for (const channel of channels) {
160
+ const channelBundles = sortedAllBundles.filter((bundle) => bundle.channel === channel);
161
+ addScope({ channel }, channelBundles);
162
+ for (const platform of ["ios", "android"]) {
163
+ const scopedBundles = channelBundles.filter((bundle) => bundle.platform === platform);
164
+ addScope({
165
+ channel,
166
+ platform
167
+ }, scopedBundles);
168
+ }
169
+ }
170
+ for (const platform of ["ios", "android"]) {
171
+ const platformBundles = sortedAllBundles.filter((bundle) => bundle.platform === platform);
172
+ addScope({ platform }, platformBundles);
173
+ }
174
+ return {
175
+ pages,
176
+ scopes
177
+ };
178
+ }
49
179
  /**
50
180
  * Creates a blob storage-based database plugin with lazy initialization.
51
181
  *
@@ -55,22 +185,282 @@ function resolveStorageTarget({ targetAppVersion, fingerprintHash }) {
55
185
  */
56
186
  const createBlobDatabasePlugin = ({ name, factory }) => {
57
187
  return (config, hooks) => {
188
+ const managementIndexPageSize = resolveManagementIndexPageSize(config);
58
189
  const { listObjects, loadObject, uploadObject, deleteObject, invalidatePaths, apiBasePath } = factory(config);
59
190
  const bundlesMap = /* @__PURE__ */ new Map();
60
191
  const pendingBundlesMap = /* @__PURE__ */ new Map();
192
+ const managementRootCache = /* @__PURE__ */ new Map();
61
193
  const PLATFORMS = ["ios", "android"];
194
+ const getAllManagementArtifact = (artifacts) => {
195
+ const allArtifact = artifacts.scopes.find((scope) => scope.cacheKey === ALL_SCOPE_CACHE_KEY);
196
+ if (!allArtifact) throw new Error("all-bundles management index artifact not found");
197
+ return allArtifact;
198
+ };
199
+ const replaceManagementRootCache = (artifacts) => {
200
+ managementRootCache.clear();
201
+ for (const scope of artifacts.scopes) managementRootCache.set(scope.cacheKey, scope.root);
202
+ };
203
+ const createHydratedBundle = (bundle) => {
204
+ const hydratedBundle = createBundleWithUpdateJsonKey(bundle);
205
+ bundlesMap.set(hydratedBundle.id, hydratedBundle);
206
+ return hydratedBundle;
207
+ };
208
+ const loadStoredManagementRoot = async (scope) => {
209
+ const cacheKey = getManagementScopeCacheKey(scope);
210
+ const storedRoot = await loadObject(getManagementRootKey(scope));
211
+ if (storedRoot) {
212
+ managementRootCache.set(cacheKey, storedRoot);
213
+ return storedRoot;
214
+ }
215
+ managementRootCache.delete(cacheKey);
216
+ return null;
217
+ };
218
+ const loadManagementPage = async (descriptor, pageCache) => {
219
+ if (pageCache?.has(descriptor.key)) return pageCache.get(descriptor.key) ?? null;
220
+ const page = await loadObject(descriptor.key);
221
+ pageCache?.set(descriptor.key, page);
222
+ return page;
223
+ };
224
+ const loadBundleFromManagementRoot = async (root, bundleId) => {
225
+ const pageIndex = findPageIndexContainingId(root.pages, bundleId);
226
+ if (pageIndex < 0) return null;
227
+ const descriptor = root.pages[pageIndex];
228
+ const page = await loadManagementPage(descriptor);
229
+ if (!page) return null;
230
+ return page.find((item) => item.id === bundleId) ?? null;
231
+ };
232
+ const loadAllBundlesFromRoot = async (root) => {
233
+ const allBundles = [];
234
+ const pageCache = /* @__PURE__ */ new Map();
235
+ for (const descriptor of root.pages) {
236
+ const page = await loadManagementPage(descriptor, pageCache);
237
+ if (!page) return null;
238
+ allBundles.push(...page);
239
+ }
240
+ return allBundles;
241
+ };
242
+ const persistManagementIndexArtifacts = async (nextArtifacts, previousArtifacts) => {
243
+ for (const [key, page] of nextArtifacts.pages.entries()) await uploadObject(key, page);
244
+ for (const scope of nextArtifacts.scopes) await uploadObject(scope.rootKey, scope.root);
245
+ if (!previousArtifacts) return;
246
+ const nextPageKeys = new Set(nextArtifacts.pages.keys());
247
+ const nextRootKeys = new Set(nextArtifacts.scopes.map((scope) => scope.rootKey));
248
+ for (const [key] of previousArtifacts.pages.entries()) if (!nextPageKeys.has(key)) await deleteObject(key).catch(() => {});
249
+ for (const scope of previousArtifacts.scopes) if (!nextRootKeys.has(scope.rootKey)) await deleteObject(scope.rootKey).catch(() => {});
250
+ };
251
+ const ensureAllManagementRoot = async () => {
252
+ const storedAllRoot = await loadStoredManagementRoot({});
253
+ if (storedAllRoot && storedAllRoot.pageSize === managementIndexPageSize) return storedAllRoot;
254
+ const rebuiltBundles = sortManagedBundles((await reloadBundles()).map((bundle) => removeBundleInternalKeys(bundle)));
255
+ const nextArtifacts = buildManagementIndexArtifacts(rebuiltBundles, managementIndexPageSize);
256
+ await persistManagementIndexArtifacts(nextArtifacts, storedAllRoot ? buildManagementIndexArtifacts(rebuiltBundles, storedAllRoot.pageSize) : void 0);
257
+ replaceManagementRootCache(nextArtifacts);
258
+ return getAllManagementArtifact(nextArtifacts).root;
259
+ };
260
+ const loadManagementScopeRoot = async (scope) => {
261
+ const cacheKey = getManagementScopeCacheKey(scope);
262
+ if (cacheKey === ALL_SCOPE_CACHE_KEY) return ensureAllManagementRoot();
263
+ const storedRoot = await loadStoredManagementRoot(scope);
264
+ if (storedRoot) return storedRoot;
265
+ await ensureAllManagementRoot();
266
+ const storedScopedRoot = await loadStoredManagementRoot(scope);
267
+ if (storedScopedRoot) return storedScopedRoot;
268
+ managementRootCache.set(cacheKey, null);
269
+ return null;
270
+ };
271
+ const loadAllBundlesForManagementFallback = async () => {
272
+ const allRoot = await loadManagementScopeRoot({});
273
+ if (allRoot) {
274
+ const pagedBundles = await loadAllBundlesFromRoot(allRoot);
275
+ if (pagedBundles) return pagedBundles;
276
+ }
277
+ return sortManagedBundles((await reloadBundles()).map((bundle) => removeBundleInternalKeys(bundle)));
278
+ };
279
+ const loadCurrentBundlesForIndexRebuild = async () => {
280
+ return loadAllBundlesForManagementFallback();
281
+ };
282
+ const findPageIndexContainingId = (pages, id) => {
283
+ return pages.findIndex((page) => id.localeCompare(page.firstId) <= 0 && id.localeCompare(page.lastId) >= 0);
284
+ };
285
+ const readPagedBundles = async ({ root, limit, offset, cursor }) => {
286
+ if (root.total === 0 || root.pages.length === 0) return createEmptyManagementResult(limit);
287
+ const pageStartOffsets = getPageStartOffsets(root.pages);
288
+ const pageCache = /* @__PURE__ */ new Map();
289
+ if (offset !== void 0) {
290
+ const normalizedOffset = Math.max(0, offset);
291
+ if (normalizedOffset >= root.total) return {
292
+ data: [],
293
+ pagination: calculatePagination(root.total, {
294
+ limit,
295
+ offset: normalizedOffset
296
+ })
297
+ };
298
+ let pageIndex = 0;
299
+ for (let index = pageStartOffsets.length - 1; index >= 0; index--) if ((pageStartOffsets[index] ?? 0) <= normalizedOffset) {
300
+ pageIndex = index;
301
+ break;
302
+ }
303
+ const startInPage = normalizedOffset - (pageStartOffsets[pageIndex] ?? 0);
304
+ const data = [];
305
+ for (let currentPageIndex = pageIndex; currentPageIndex < root.pages.length && (limit <= 0 || data.length < limit); currentPageIndex++) {
306
+ const descriptor = root.pages[currentPageIndex];
307
+ const page = await loadManagementPage(descriptor, pageCache);
308
+ if (!page) return paginateBundles({
309
+ bundles: await loadAllBundlesForManagementFallback(),
310
+ limit,
311
+ offset: normalizedOffset
312
+ });
313
+ data.push(...currentPageIndex === pageIndex ? page.slice(startInPage) : page);
314
+ }
315
+ const paginatedData = limit > 0 ? data.slice(0, limit) : data;
316
+ return {
317
+ data: paginatedData,
318
+ pagination: {
319
+ ...calculatePagination(root.total, {
320
+ limit,
321
+ offset: normalizedOffset
322
+ }),
323
+ ...paginatedData.length > 0 && normalizedOffset + paginatedData.length < root.total ? { nextCursor: paginatedData.at(-1)?.id } : {},
324
+ ...paginatedData.length > 0 && normalizedOffset > 0 ? { previousCursor: paginatedData[0]?.id } : {}
325
+ }
326
+ };
327
+ }
328
+ if (cursor?.after) {
329
+ let pageIndex = root.pages.findIndex((page) => {
330
+ const containsCursor = cursor.after.localeCompare(page.firstId) <= 0 && cursor.after.localeCompare(page.lastId) >= 0;
331
+ const wholePageEligible = cursor.after.localeCompare(page.firstId) > 0;
332
+ return containsCursor || wholePageEligible;
333
+ });
334
+ if (pageIndex < 0) return {
335
+ data: [],
336
+ pagination: {
337
+ ...calculatePagination(root.total, {
338
+ limit,
339
+ offset: root.total
340
+ }),
341
+ previousCursor: cursor.after
342
+ }
343
+ };
344
+ const data = [];
345
+ let startIndex = null;
346
+ while (pageIndex < root.pages.length && (limit <= 0 || data.length < limit)) {
347
+ const descriptor = root.pages[pageIndex];
348
+ const page = await loadManagementPage(descriptor, pageCache);
349
+ if (!page) return paginateBundles({
350
+ bundles: await loadAllBundlesForManagementFallback(),
351
+ limit,
352
+ cursor
353
+ });
354
+ const containsCursor = cursor.after.localeCompare(descriptor.firstId) <= 0 && cursor.after.localeCompare(descriptor.lastId) >= 0;
355
+ let eligiblePageBundles = page;
356
+ if (containsCursor) {
357
+ const startInPage = page.findIndex((bundle) => bundle.id.localeCompare(cursor.after) < 0);
358
+ if (startInPage < 0) eligiblePageBundles = [];
359
+ else {
360
+ eligiblePageBundles = page.slice(startInPage);
361
+ startIndex ??= (pageStartOffsets[pageIndex] ?? 0) + startInPage;
362
+ }
363
+ } else if (eligiblePageBundles.length > 0) startIndex ??= pageStartOffsets[pageIndex] ?? 0;
364
+ data.push(...eligiblePageBundles);
365
+ if (limit > 0 && data.length >= limit) break;
366
+ pageIndex += 1;
367
+ }
368
+ const paginatedData = limit > 0 ? data.slice(0, limit) : data;
369
+ const resolvedStartIndex = startIndex ?? root.total;
370
+ return {
371
+ data: paginatedData,
372
+ pagination: {
373
+ ...calculatePagination(root.total, {
374
+ limit,
375
+ offset: resolvedStartIndex
376
+ }),
377
+ ...paginatedData.length > 0 && resolvedStartIndex + paginatedData.length < root.total ? { nextCursor: paginatedData.at(-1)?.id } : {},
378
+ ...paginatedData.length > 0 && resolvedStartIndex > 0 ? { previousCursor: paginatedData[0]?.id } : {}
379
+ }
380
+ };
381
+ }
382
+ if (cursor?.before) {
383
+ let pageIndex = -1;
384
+ for (let index = root.pages.length - 1; index >= 0; index--) {
385
+ const page = root.pages[index];
386
+ const containsCursor = cursor.before.localeCompare(page.firstId) <= 0 && cursor.before.localeCompare(page.lastId) >= 0;
387
+ const wholePageEligible = cursor.before.localeCompare(page.lastId) < 0;
388
+ if (containsCursor || wholePageEligible) {
389
+ pageIndex = index;
390
+ break;
391
+ }
392
+ }
393
+ if (pageIndex < 0) return createEmptyManagementResult(limit);
394
+ let startIndex = null;
395
+ let collected = [];
396
+ while (pageIndex >= 0 && (limit <= 0 || collected.length < limit)) {
397
+ const descriptor = root.pages[pageIndex];
398
+ const page = await loadManagementPage(descriptor, pageCache);
399
+ if (!page) return paginateBundles({
400
+ bundles: await loadAllBundlesForManagementFallback(),
401
+ limit,
402
+ cursor
403
+ });
404
+ const eligiblePageBundles = cursor.before.localeCompare(descriptor.firstId) <= 0 && cursor.before.localeCompare(descriptor.lastId) >= 0 ? page.filter((bundle) => bundle.id.localeCompare(cursor.before) > 0) : page;
405
+ collected = [...eligiblePageBundles, ...collected];
406
+ if (eligiblePageBundles.length > 0) startIndex = pageStartOffsets[pageIndex] ?? 0;
407
+ if (limit > 0 && collected.length >= limit) break;
408
+ pageIndex -= 1;
409
+ }
410
+ if (startIndex === null || collected.length === 0) return createEmptyManagementResult(limit);
411
+ let paginatedData = collected;
412
+ if (limit > 0 && collected.length > limit) {
413
+ const dropCount = collected.length - limit;
414
+ paginatedData = collected.slice(dropCount);
415
+ startIndex += dropCount;
416
+ }
417
+ const pagination = calculatePagination(root.total, {
418
+ limit,
419
+ offset: startIndex
420
+ });
421
+ return {
422
+ data: paginatedData,
423
+ pagination: {
424
+ ...pagination,
425
+ ...paginatedData.length > 0 && startIndex + paginatedData.length < root.total ? { nextCursor: paginatedData.at(-1)?.id } : {},
426
+ ...paginatedData.length > 0 && startIndex > 0 ? { previousCursor: paginatedData[0]?.id } : {}
427
+ }
428
+ };
429
+ }
430
+ const pageIndex = 0;
431
+ const startInPage = 0;
432
+ const data = [];
433
+ for (let currentPageIndex = pageIndex; currentPageIndex < root.pages.length && (limit <= 0 || data.length < limit); currentPageIndex++) {
434
+ const descriptor = root.pages[currentPageIndex];
435
+ const page = await loadManagementPage(descriptor, pageCache);
436
+ if (!page) return paginateBundles({
437
+ bundles: await loadAllBundlesForManagementFallback(),
438
+ limit,
439
+ cursor
440
+ });
441
+ data.push(...currentPageIndex === pageIndex ? page.slice(startInPage) : page);
442
+ }
443
+ const paginatedData = limit > 0 ? data.slice(0, limit) : data;
444
+ return {
445
+ data: paginatedData,
446
+ pagination: {
447
+ ...calculatePagination(root.total, {
448
+ limit,
449
+ offset: 0
450
+ }),
451
+ ...paginatedData.length > 0 && paginatedData.length < root.total ? { nextCursor: paginatedData.at(-1)?.id } : {}
452
+ }
453
+ };
454
+ };
62
455
  async function reloadBundles() {
63
456
  bundlesMap.clear();
64
- const platformPromises = PLATFORMS.map(async (platform) => {
65
- const filePromises = (await listUpdateJsonKeys(platform)).map(async (key) => {
66
- return (await loadObject(key) ?? []).map((bundle) => ({
67
- ...bundle,
68
- _updateJsonKey: key
69
- }));
70
- });
71
- return (await Promise.all(filePromises)).flat();
457
+ const filePromises = (await listObjects("")).filter((key) => /^[^/]+\/(?:ios|android)\/[^/]+\/update\.json$/.test(key)).map(async (key) => {
458
+ return (await loadObject(key) ?? []).map((bundle) => ({
459
+ ...bundle,
460
+ _updateJsonKey: key
461
+ }));
72
462
  });
73
- const allBundles = (await Promise.all(platformPromises)).flat();
463
+ const allBundles = (await Promise.all(filePromises)).flat();
74
464
  for (const bundle of allBundles) bundlesMap.set(bundle.id, bundle);
75
465
  for (const [id, bundle] of pendingBundlesMap.entries()) bundlesMap.set(id, bundle);
76
466
  return orderBy(allBundles, [(v) => v.id], ["desc"]);
@@ -105,17 +495,6 @@ const createBlobDatabasePlugin = ({ name, factory }) => {
105
495
  if (JSON.stringify(oldTargetVersions) !== JSON.stringify(newTargetVersions)) await uploadObject(targetKey, newTargetVersions);
106
496
  }
107
497
  }
108
- /**
109
- * Lists update.json keys for a given platform.
110
- *
111
- * - If a channel is provided, only that channel's update.json files are listed.
112
- * - Otherwise, all channels for the given platform are returned.
113
- */
114
- async function listUpdateJsonKeys(platform, channel) {
115
- const prefix = channel ? platform ? `${channel}/${platform}/` : `${channel}/` : "";
116
- const pattern = channel ? platform ? new RegExp(`^${channel}/${platform}/[^/]+/update\\.json$`) : new RegExp(`^${channel}/[^/]+/[^/]+/update\\.json$`) : platform ? new RegExp(`^[^/]+/${platform}/[^/]+/update\\.json$`) : /^[^/]+\/[^/]+\/[^/]+\/update\.json$/;
117
- return listObjects(prefix).then((keys) => keys.filter((key) => pattern.test(key)));
118
- }
119
498
  const getAppVersionUpdateInfo = async ({ appVersion, bundleId, channel = "production", cohort, minBundleId, platform }) => {
120
499
  const matchingVersions = filterCompatibleAppVersions(await loadObject(`${channel}/${platform}/target-app-versions.json`) ?? [], appVersion);
121
500
  return getUpdateInfo((await Promise.allSettled(matchingVersions.map(async (targetAppVersion) => {
@@ -163,41 +542,56 @@ const createBlobDatabasePlugin = ({ name, factory }) => {
163
542
  return createDatabasePlugin({
164
543
  name,
165
544
  factory: () => ({
545
+ supportsCursorPagination: true,
166
546
  async getBundleById(bundleId) {
167
547
  const pendingBundle = pendingBundlesMap.get(bundleId);
168
548
  if (pendingBundle) return removeBundleInternalKeys(pendingBundle);
169
549
  const bundle = bundlesMap.get(bundleId);
170
550
  if (bundle) return removeBundleInternalKeys(bundle);
171
- return (await reloadBundles()).find((bundle) => bundle.id === bundleId) ?? null;
551
+ const allRoot = await loadManagementScopeRoot({});
552
+ if (allRoot) {
553
+ const matchedBundle = await loadBundleFromManagementRoot(allRoot, bundleId);
554
+ if (matchedBundle) return removeBundleInternalKeys(createHydratedBundle(matchedBundle));
555
+ managementRootCache.delete(ALL_SCOPE_CACHE_KEY);
556
+ const refreshedAllRoot = await loadStoredManagementRoot({});
557
+ if (refreshedAllRoot) {
558
+ const refreshedBundle = await loadBundleFromManagementRoot(refreshedAllRoot, bundleId);
559
+ if (refreshedBundle) return removeBundleInternalKeys(createHydratedBundle(refreshedBundle));
560
+ }
561
+ }
562
+ const matchedBundle = (await reloadBundles()).find((item) => item.id === bundleId);
563
+ if (!matchedBundle) return null;
564
+ return removeBundleInternalKeys(matchedBundle);
172
565
  },
173
566
  async getUpdateInfo(args) {
174
567
  if (args._updateStrategy === "appVersion") return getAppVersionUpdateInfo(args);
175
568
  return getFingerprintUpdateInfo(args);
176
569
  },
177
570
  async getBundles(options) {
178
- let allBundles = await reloadBundles();
179
- const { where, limit, offset, orderBy } = options;
180
- if (where) allBundles = allBundles.filter((bundle) => bundleMatchesQueryWhere(bundle, where));
181
- const cleanBundles = sortBundles(allBundles.map(removeBundleInternalKeys), orderBy);
182
- const total = cleanBundles.length;
183
- let paginatedData = cleanBundles;
184
- if (offset > 0) paginatedData = paginatedData.slice(offset);
185
- if (limit) paginatedData = paginatedData.slice(0, limit);
186
- return {
187
- data: paginatedData,
188
- pagination: calculatePagination(total, {
571
+ const { where, limit, offset, orderBy, cursor } = options;
572
+ const scope = getSupportedManagementScope(where, orderBy);
573
+ if (scope) {
574
+ const root = await loadManagementScopeRoot(scope);
575
+ if (!root) return createEmptyManagementResult(limit);
576
+ return readPagedBundles({
577
+ root,
189
578
  limit,
190
- offset
191
- })
192
- };
579
+ offset,
580
+ cursor
581
+ });
582
+ }
583
+ let allBundles = await loadAllBundlesForManagementFallback();
584
+ if (where) allBundles = allBundles.filter((bundle) => bundleMatchesQueryWhere(bundle, where));
585
+ return paginateBundles({
586
+ bundles: allBundles,
587
+ limit,
588
+ offset,
589
+ cursor,
590
+ orderBy
591
+ });
193
592
  },
194
593
  async getChannels() {
195
- const total = (await reloadBundles()).length;
196
- const result = await this.getBundles({
197
- limit: total,
198
- offset: 0
199
- });
200
- return [...new Set(result.data.map((bundle) => bundle.channel))];
594
+ return (await loadManagementScopeRoot({}))?.channels ?? [];
201
595
  },
202
596
  async commitBundle({ changedSets }) {
203
597
  if (changedSets.length === 0) return;
@@ -294,6 +688,20 @@ const createBlobDatabasePlugin = ({ name, factory }) => {
294
688
  await uploadObject(key, currentBundles);
295
689
  })();
296
690
  if (isTargetAppVersionChanged || isChannelChanged) for (const platform of PLATFORMS) await updateTargetVersionsForPlatform(platform);
691
+ const currentIndexBundles = await loadCurrentBundlesForIndexRebuild();
692
+ const nextIndexMap = new Map(currentIndexBundles.map((bundle) => [bundle.id, bundle]));
693
+ for (const { operation, data } of changedSets) {
694
+ if (operation === "delete") {
695
+ nextIndexMap.delete(data.id);
696
+ continue;
697
+ }
698
+ nextIndexMap.set(data.id, data);
699
+ }
700
+ const nextIndexBundles = sortManagedBundles(Array.from(nextIndexMap.values()));
701
+ const previousArtifacts = buildManagementIndexArtifacts(currentIndexBundles, managementIndexPageSize);
702
+ const nextArtifacts = buildManagementIndexArtifacts(nextIndexBundles, managementIndexPageSize);
703
+ await persistManagementIndexArtifacts(nextArtifacts, previousArtifacts);
704
+ replaceManagementRootCache(nextArtifacts);
297
705
  const encondedPaths = /* @__PURE__ */ new Set();
298
706
  for (const path of pathsToInvalidate) encondedPaths.add(encodeURI(path));
299
707
  await invalidatePaths(Array.from(encondedPaths));