@hot-updater/cloudflare 0.29.1 → 0.29.3

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.
@@ -3138,11 +3138,6 @@ init_performance2();
3138
3138
  init_virtual_unenv_global_polyfill_cloudflare_unenv_preset_node_process();
3139
3139
  init_virtual_unenv_global_polyfill_cloudflare_unenv_preset_node_console();
3140
3140
  init_performance2();
3141
-
3142
- // ../../packages/core/dist/rollout.mjs
3143
- init_virtual_unenv_global_polyfill_cloudflare_unenv_preset_node_process();
3144
- init_virtual_unenv_global_polyfill_cloudflare_unenv_preset_node_console();
3145
- init_performance2();
3146
3141
  var NUMERIC_COHORT_SIZE = 1e3;
3147
3142
  var DEFAULT_ROLLOUT_COHORT_COUNT = NUMERIC_COHORT_SIZE;
3148
3143
  function parseNumericCohortValue(cohort) {
@@ -3243,11 +3238,6 @@ function isCohortEligibleForUpdate(bundleId, cohort, rolloutCohortCount, targetC
3243
3238
  return getNumericCohortRolloutPosition(bundleId, numericCohort) < normalizedRolloutCount;
3244
3239
  }
3245
3240
  __name(isCohortEligibleForUpdate, "isCohortEligibleForUpdate");
3246
-
3247
- // ../../packages/core/dist/uuid.mjs
3248
- init_virtual_unenv_global_polyfill_cloudflare_unenv_preset_node_process();
3249
- init_virtual_unenv_global_polyfill_cloudflare_unenv_preset_node_console();
3250
- init_performance2();
3251
3241
  var NIL_UUID = "00000000-0000-0000-0000-000000000000";
3252
3242
 
3253
3243
  // ../plugin-core/dist/index.mjs
@@ -3336,12 +3326,12 @@ function mergeBundleUpdate(baseBundle, patch) {
3336
3326
  __name(mergeBundleUpdate, "mergeBundleUpdate");
3337
3327
  function createDatabasePlugin(options) {
3338
3328
  return (config2, hooks) => {
3329
+ let cachedMethods = null;
3330
+ const getMethods = /* @__PURE__ */ __name(() => {
3331
+ if (!cachedMethods) cachedMethods = options.factory(config2);
3332
+ return cachedMethods;
3333
+ }, "getMethods");
3339
3334
  return () => {
3340
- let cachedMethods = null;
3341
- const getMethods = /* @__PURE__ */ __name(() => {
3342
- if (!cachedMethods) cachedMethods = options.factory(config2);
3343
- return cachedMethods;
3344
- }, "getMethods");
3345
3335
  const changedMap = /* @__PURE__ */ new Map();
3346
3336
  const markChanged = /* @__PURE__ */ __name((operation, data) => {
3347
3337
  changedMap.set(data.id, {
@@ -3372,8 +3362,8 @@ function createDatabasePlugin(options) {
3372
3362
  const params = { changedSets: Array.from(changedMap.values()) };
3373
3363
  if (context2 === void 0) await methods.commitBundle(params);
3374
3364
  else await methods.commitBundle(params, context2);
3375
- await hooks?.onDatabaseUpdated?.();
3376
3365
  changedMap.clear();
3366
+ await hooks?.onDatabaseUpdated?.();
3377
3367
  },
3378
3368
  async updateBundle(targetBundleId, newBundle, context2) {
3379
3369
  const pendingChange = changedMap.get(targetBundleId);
@@ -3470,18 +3460,28 @@ var INIT_BUNDLE_ROLLBACK_UPDATE_INFO = {
3470
3460
  storageUri: null,
3471
3461
  fileHash: null
3472
3462
  };
3473
- function createPluginDatabaseCore(plugin, resolveFileUrl) {
3474
- const getSortedBundlePage = /* @__PURE__ */ __name(async (options, context2) => {
3475
- const result = await plugin.getBundles(
3463
+ function createPluginDatabaseCore(getPlugin, resolveFileUrl, options) {
3464
+ const runWithMutationPlugin = /* @__PURE__ */ __name(async (operation) => {
3465
+ const plugin = options?.createMutationPlugin?.() ?? getPlugin();
3466
+ try {
3467
+ return await operation(plugin);
3468
+ } finally {
3469
+ if (options?.createMutationPlugin) {
3470
+ await options.cleanupMutationPlugin?.(plugin);
3471
+ }
3472
+ }
3473
+ }, "runWithMutationPlugin");
3474
+ const getSortedBundlePage = /* @__PURE__ */ __name(async (options2, context2) => {
3475
+ const result = await getPlugin().getBundles(
3476
3476
  {
3477
- ...options,
3478
- orderBy: options.orderBy ?? DESC_ORDER
3477
+ ...options2,
3478
+ orderBy: options2.orderBy ?? DESC_ORDER
3479
3479
  },
3480
3480
  context2
3481
3481
  );
3482
3482
  return {
3483
3483
  ...result,
3484
- data: sortBundles(result.data, options.orderBy ?? DESC_ORDER)
3484
+ data: sortBundles(result.data, options2.orderBy ?? DESC_ORDER)
3485
3485
  };
3486
3486
  }, "getSortedBundlePage");
3487
3487
  const isEligibleForUpdate = /* @__PURE__ */ __name((bundle, cohort) => {
@@ -3561,7 +3561,7 @@ function createPluginDatabaseCore(plugin, resolveFileUrl) {
3561
3561
  }), "getBaseWhere");
3562
3562
  const api = {
3563
3563
  async getBundleById(id, context2) {
3564
- return plugin.getBundleById(id, context2);
3564
+ return getPlugin().getBundleById(id, context2);
3565
3565
  },
3566
3566
  async getUpdateInfo(args, context2) {
3567
3567
  const channel2 = args.channel ?? "production";
@@ -3605,31 +3605,37 @@ function createPluginDatabaseCore(plugin, resolveFileUrl) {
3605
3605
  return { ...rest, fileUrl };
3606
3606
  },
3607
3607
  async getChannels(context2) {
3608
- return plugin.getChannels(context2);
3608
+ return getPlugin().getChannels(context2);
3609
3609
  },
3610
- async getBundles(options, context2) {
3611
- return plugin.getBundles(options, context2);
3610
+ async getBundles(options2, context2) {
3611
+ return getPlugin().getBundles(options2, context2);
3612
3612
  },
3613
3613
  async insertBundle(bundle, context2) {
3614
- await plugin.appendBundle(bundle, context2);
3615
- await plugin.commitBundle(context2);
3614
+ await runWithMutationPlugin(async (plugin) => {
3615
+ await plugin.appendBundle(bundle, context2);
3616
+ await plugin.commitBundle(context2);
3617
+ });
3616
3618
  },
3617
3619
  async updateBundleById(bundleId, newBundle, context2) {
3618
- await plugin.updateBundle(bundleId, newBundle, context2);
3619
- await plugin.commitBundle(context2);
3620
+ await runWithMutationPlugin(async (plugin) => {
3621
+ await plugin.updateBundle(bundleId, newBundle, context2);
3622
+ await plugin.commitBundle(context2);
3623
+ });
3620
3624
  },
3621
3625
  async deleteBundleById(bundleId, context2) {
3622
- const bundle = await plugin.getBundleById(bundleId, context2);
3623
- if (!bundle) {
3624
- return;
3625
- }
3626
- await plugin.deleteBundle(bundle, context2);
3627
- await plugin.commitBundle(context2);
3626
+ await runWithMutationPlugin(async (plugin) => {
3627
+ const bundle = await plugin.getBundleById(bundleId, context2);
3628
+ if (!bundle) {
3629
+ return;
3630
+ }
3631
+ await plugin.deleteBundle(bundle, context2);
3632
+ await plugin.commitBundle(context2);
3633
+ });
3628
3634
  }
3629
3635
  };
3630
3636
  return {
3631
3637
  api,
3632
- adapterName: plugin.name,
3638
+ adapterName: getPlugin().name,
3633
3639
  createMigrator: /* @__PURE__ */ __name(() => {
3634
3640
  throw new Error(
3635
3641
  "createMigrator is only available for Kysely/Prisma/Drizzle database adapters."
@@ -3866,7 +3872,7 @@ var handleGetBundles = /* @__PURE__ */ __name(async (_params, request, api, cont
3866
3872
  },
3867
3873
  context2
3868
3874
  );
3869
- return new Response(JSON.stringify(result.data), {
3875
+ return new Response(JSON.stringify(result), {
3870
3876
  status: 200,
3871
3877
  headers: { "Content-Type": "application/json" }
3872
3878
  });
@@ -3903,7 +3909,12 @@ var handleDeleteBundle = /* @__PURE__ */ __name(async (params, _request, api, co
3903
3909
  }, "handleDeleteBundle");
3904
3910
  var handleGetChannels = /* @__PURE__ */ __name(async (_params, _request, api, context2) => {
3905
3911
  const channels = await api.getChannels(context2);
3906
- return new Response(JSON.stringify({ channels }), {
3912
+ const response = {
3913
+ data: {
3914
+ channels
3915
+ }
3916
+ };
3917
+ return new Response(JSON.stringify(response), {
3907
3918
  status: 200,
3908
3919
  headers: { "Content-Type": "application/json" }
3909
3920
  });
@@ -4016,6 +4027,7 @@ var normalizeBasePath = /* @__PURE__ */ __name((basePath) => {
4016
4027
 
4017
4028
  // ../../packages/server/src/runtime.ts
4018
4029
  function createHotUpdater(options) {
4030
+ const database = options.database;
4019
4031
  const basePath = normalizeBasePath(options.basePath ?? "/api");
4020
4032
  const storagePlugins = (options.storages ?? options.storagePlugins ?? []).map(
4021
4033
  (plugin2) => typeof plugin2 === "function" ? plugin2() : plugin2
@@ -4041,15 +4053,18 @@ function createHotUpdater(options) {
4041
4053
  }
4042
4054
  return fileUrl;
4043
4055
  }, "resolveStoragePluginUrl");
4044
- if (!isDatabasePluginFactory(options.database) && !isDatabasePlugin(options.database)) {
4056
+ if (!isDatabasePluginFactory(database) && !isDatabasePlugin(database)) {
4045
4057
  throw new Error(
4046
4058
  "@hot-updater/server/runtime only supports database plugins."
4047
4059
  );
4048
4060
  }
4049
- const plugin = isDatabasePluginFactory(options.database) ? options.database() : options.database;
4061
+ const plugin = isDatabasePluginFactory(database) ? database() : database;
4050
4062
  const core = createPluginDatabaseCore(
4051
- plugin,
4052
- resolveStoragePluginUrl
4063
+ () => plugin,
4064
+ resolveStoragePluginUrl,
4065
+ isDatabasePluginFactory(database) ? {
4066
+ createMutationPlugin: /* @__PURE__ */ __name(() => database(), "createMutationPlugin")
4067
+ } : void 0
4053
4068
  );
4054
4069
  const api = {
4055
4070
  ...core.api,
@@ -8838,7 +8853,6 @@ var resolveDbFromContext = /* @__PURE__ */ __name((context2) => {
8838
8853
  var d1WorkerDatabase = /* @__PURE__ */ __name(() => createDatabasePlugin({
8839
8854
  name: "d1WorkerDatabase",
8840
8855
  factory: /* @__PURE__ */ __name((config2) => {
8841
- let bundles = [];
8842
8856
  const queryAll = /* @__PURE__ */ __name(async (sql, params = [], context2) => {
8843
8857
  const result = await config2.getDb(context2).prepare(sql).bind(...params).all();
8844
8858
  return result.results ?? [];
@@ -8849,10 +8863,6 @@ var d1WorkerDatabase = /* @__PURE__ */ __name(() => createDatabasePlugin({
8849
8863
  }, "queryFirst");
8850
8864
  return {
8851
8865
  async getBundleById(bundleId, context2) {
8852
- const found = bundles.find((bundle) => bundle.id === bundleId);
8853
- if (found) {
8854
- return found;
8855
- }
8856
8866
  const row = await queryFirst(
8857
8867
  "SELECT * FROM bundles WHERE id = ? LIMIT 1",
8858
8868
  [bundleId],
@@ -8875,7 +8885,7 @@ var d1WorkerDatabase = /* @__PURE__ */ __name(() => createDatabasePlugin({
8875
8885
  [...params, limit, offset],
8876
8886
  context2
8877
8887
  );
8878
- bundles = rows.map(transformRowToBundle);
8888
+ const bundles = rows.map(transformRowToBundle);
8879
8889
  const paginationOptions = { limit, offset };
8880
8890
  return {
8881
8891
  data: bundles,
@@ -8898,9 +8908,6 @@ var d1WorkerDatabase = /* @__PURE__ */ __name(() => createDatabasePlugin({
8898
8908
  for (const operation of changedSets) {
8899
8909
  if (operation.operation === "delete") {
8900
8910
  await db.prepare("DELETE FROM bundles WHERE id = ?").bind(operation.data.id).run();
8901
- bundles = bundles.filter(
8902
- (bundle2) => bundle2.id !== operation.data.id
8903
- );
8904
8911
  continue;
8905
8912
  }
8906
8913
  const bundle = operation.data;