@hot-updater/cloudflare 0.12.7 → 0.13.1

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.
package/dist/index.cjs CHANGED
@@ -815,49 +815,147 @@ __export(index_exports, {
815
815
  module.exports = __toCommonJS(index_exports);
816
816
 
817
817
  // src/d1Database.ts
818
- var import_pg_minify = __toESM(require_lib(), 1);
818
+ var import_plugin_core = require("@hot-updater/plugin-core");
819
819
  var import_cloudflare = __toESM(require("cloudflare"), 1);
820
- var d1Database = (config, hooks) => (_) => {
820
+ var import_pg_minify = __toESM(require_lib(), 1);
821
+ async function resolvePage(singlePage) {
822
+ const results = [];
823
+ for await (const page of singlePage.iterPages()) {
824
+ const data = page.result.flatMap((r) => r.results);
825
+ results.push(...data);
826
+ }
827
+ return results;
828
+ }
829
+ var d1Database = (config, hooks) => {
821
830
  const cf = new import_cloudflare.default({
822
831
  apiToken: config.cloudflareApiToken
823
832
  });
824
833
  let bundles = [];
825
- const changedIds = /* @__PURE__ */ new Set();
826
- function markChanged(id) {
827
- changedIds.add(id);
828
- }
829
- return {
830
- name: "d1Database",
831
- async commitBundle() {
832
- if (changedIds.size === 0) {
833
- return;
834
- }
835
- const changedBundles = bundles.filter((b) => changedIds.has(b.id));
836
- if (changedBundles.length === 0) {
837
- return;
838
- }
839
- const params = [];
840
- const valuesSql = changedBundles.map((b) => {
841
- params.push(
842
- b.id,
843
- b.enabled ? 1 : 0,
844
- b.fileUrl,
845
- b.shouldForceUpdate ? 1 : 0,
846
- b.fileHash,
847
- b.gitCommitHash || null,
848
- b.message || null,
849
- b.platform,
850
- b.targetAppVersion
834
+ return (0, import_plugin_core.createDatabasePlugin)(
835
+ "d1Database",
836
+ {
837
+ async getBundleById(bundleId) {
838
+ const found = bundles.find((b) => b.id === bundleId);
839
+ if (found) {
840
+ return found;
841
+ }
842
+ const sql = (0, import_pg_minify.default)(
843
+ /* sql */
844
+ `
845
+ SELECT * FROM bundles WHERE id = ? LIMIT 1`
851
846
  );
852
- return "(?, ?, ?, ?, ?, ?, ?, ?, ?)";
853
- }).join(",\n");
854
- const sql = (0, import_pg_minify.default)(
855
- /* sql */
847
+ const singlePage = await cf.d1.database.query(config.databaseId, {
848
+ account_id: config.accountId,
849
+ sql,
850
+ params: [bundleId]
851
+ });
852
+ const rows = await resolvePage(singlePage);
853
+ if (rows.length === 0) {
854
+ return null;
855
+ }
856
+ const row = rows[0];
857
+ return {
858
+ channel: row.channel,
859
+ enabled: Boolean(row.enabled),
860
+ shouldForceUpdate: Boolean(row.should_force_update),
861
+ fileHash: row.file_hash,
862
+ gitCommitHash: row.git_commit_hash,
863
+ id: row.id,
864
+ message: row.message,
865
+ platform: row.platform,
866
+ targetAppVersion: row.target_app_version
867
+ };
868
+ },
869
+ async getBundles(options) {
870
+ const { where, limit, offset = 0 } = options ?? {};
871
+ let sql = "SELECT * FROM bundles";
872
+ const params = [];
873
+ const conditions = [];
874
+ if (where?.channel) {
875
+ conditions.push("channel = ?");
876
+ params.push(where.channel);
877
+ }
878
+ if (where?.platform) {
879
+ conditions.push("platform = ?");
880
+ params.push(where.platform);
881
+ }
882
+ if (conditions.length > 0) {
883
+ sql += ` WHERE ${conditions.join(" AND ")}`;
884
+ }
885
+ sql += " ORDER BY id DESC";
886
+ if (limit) {
887
+ sql += " LIMIT ?";
888
+ params.push(limit);
889
+ }
890
+ if (offset) {
891
+ sql += " OFFSET ?";
892
+ params.push(offset);
893
+ }
894
+ const singlePage = await cf.d1.database.query(config.databaseId, {
895
+ account_id: config.accountId,
896
+ sql: (0, import_pg_minify.default)(sql),
897
+ params
898
+ });
899
+ const rows = await resolvePage(singlePage);
900
+ if (rows.length === 0) {
901
+ bundles = [];
902
+ } else {
903
+ bundles = rows.map((row) => ({
904
+ id: row.id,
905
+ channel: row.channel,
906
+ enabled: Boolean(row.enabled),
907
+ shouldForceUpdate: Boolean(row.should_force_update),
908
+ fileHash: row.file_hash,
909
+ gitCommitHash: row.git_commit_hash,
910
+ message: row.message,
911
+ platform: row.platform,
912
+ targetAppVersion: row.target_app_version
913
+ }));
914
+ }
915
+ return bundles;
916
+ },
917
+ async getChannels() {
918
+ const sql = (0, import_pg_minify.default)(
919
+ /* sql */
920
+ `
921
+ SELECT channel FROM bundles GROUP BY channel
856
922
  `
923
+ );
924
+ const singlePage = await cf.d1.database.query(config.databaseId, {
925
+ account_id: config.accountId,
926
+ sql,
927
+ params: []
928
+ });
929
+ const rows = await resolvePage(singlePage);
930
+ return rows.map((row) => row.channel);
931
+ },
932
+ async commitBundle({ changedSets }) {
933
+ if (changedSets.length === 0) {
934
+ return;
935
+ }
936
+ const bundles2 = changedSets.map((op) => op.data);
937
+ const params = [];
938
+ const valuesSql = bundles2.map((b) => {
939
+ params.push(
940
+ b.id,
941
+ b.channel,
942
+ b.enabled ? 1 : 0,
943
+ b.shouldForceUpdate ? 1 : 0,
944
+ b.fileHash,
945
+ b.gitCommitHash || null,
946
+ b.message || null,
947
+ b.platform,
948
+ b.targetAppVersion
949
+ );
950
+ return "(?, ?, ?, ?, ?, ?, ?, ?, ?)";
951
+ }).join(",\n");
952
+ const sql = (0, import_pg_minify.default)(
953
+ /* sql */
954
+ `
857
955
  INSERT OR REPLACE INTO bundles (
858
956
  id,
957
+ channel,
859
958
  enabled,
860
- file_url,
861
959
  should_force_update,
862
960
  file_hash,
863
961
  git_commit_hash,
@@ -867,113 +965,16 @@ var d1Database = (config, hooks) => (_) => {
867
965
  )
868
966
  VALUES
869
967
  ${valuesSql};`
870
- );
871
- await cf.d1.database.query(config.databaseId, {
872
- account_id: config.accountId,
873
- sql,
874
- // why this type is string[] ?
875
- params
876
- });
877
- changedIds.clear();
878
- hooks?.onDatabaseUpdated?.();
879
- },
880
- async updateBundle(targetBundleId, newBundle) {
881
- const index = bundles.findIndex((b) => b.id === targetBundleId);
882
- if (index === -1) {
883
- throw new Error(`Cannot find bundle with id ${targetBundleId}`);
884
- }
885
- Object.assign(bundles[index], newBundle);
886
- markChanged(targetBundleId);
887
- },
888
- async appendBundle(inputBundle) {
889
- bundles.unshift(inputBundle);
890
- markChanged(inputBundle.id);
891
- },
892
- async getBundleById(bundleId) {
893
- const found = bundles.find((b) => b.id === bundleId);
894
- if (found) {
895
- return found;
896
- }
897
- const sql = (0, import_pg_minify.default)(
898
- /* sql */
899
- `
900
- SELECT * FROM bundles WHERE id = ? LIMIT 1`
901
- );
902
- const singlePage = await cf.d1.database.query(config.databaseId, {
903
- account_id: config.accountId,
904
- sql,
905
- params: [bundleId]
906
- });
907
- const rows = [];
908
- for await (const page of singlePage.iterPages()) {
909
- const data = page.result.flatMap((r) => r.results);
910
- rows.push(...data);
911
- }
912
- if (rows.length === 0) {
913
- return null;
968
+ );
969
+ await cf.d1.database.query(config.databaseId, {
970
+ account_id: config.accountId,
971
+ sql,
972
+ params
973
+ });
914
974
  }
915
- const row = rows[0];
916
- return {
917
- enabled: Boolean(row.enabled),
918
- fileUrl: row.file_url,
919
- shouldForceUpdate: Boolean(row.should_force_update),
920
- fileHash: row.file_hash,
921
- gitCommitHash: row.git_commit_hash,
922
- id: row.id,
923
- message: row.message,
924
- platform: row.platform,
925
- targetAppVersion: row.target_app_version
926
- };
927
975
  },
928
- async getBundles(refresh = false) {
929
- if (bundles.length > 0 && !refresh) {
930
- return bundles;
931
- }
932
- const sql = (0, import_pg_minify.default)(
933
- /* sql */
934
- `
935
- SELECT
936
- id,
937
- enabled,
938
- file_url,
939
- should_force_update,
940
- file_hash,
941
- git_commit_hash,
942
- message,
943
- platform,
944
- target_app_version
945
- FROM bundles
946
- ORDER BY id DESC
947
- `
948
- );
949
- const singlePage = await cf.d1.database.query(config.databaseId, {
950
- account_id: config.accountId,
951
- sql,
952
- params: []
953
- });
954
- const rows = [];
955
- for await (const page of singlePage.iterPages()) {
956
- const data = page.result.flatMap((r) => r.results);
957
- rows.push(...data);
958
- }
959
- if (rows.length === 0) {
960
- bundles = [];
961
- } else {
962
- bundles = rows.map((row) => ({
963
- id: row.id,
964
- enabled: Boolean(row.enabled),
965
- fileUrl: row.file_url,
966
- shouldForceUpdate: Boolean(row.should_force_update),
967
- fileHash: row.file_hash,
968
- gitCommitHash: row.git_commit_hash,
969
- message: row.message,
970
- platform: row.platform,
971
- targetAppVersion: row.target_app_version
972
- }));
973
- }
974
- return bundles;
975
- }
976
- };
976
+ hooks
977
+ );
977
978
  };
978
979
 
979
980
  // src/r2Storage.ts
@@ -7919,16 +7920,9 @@ var r2Storage = (config, hooks) => (_) => {
7919
7920
  throw error;
7920
7921
  }
7921
7922
  hooks?.onStorageUploaded?.();
7922
- if (hooks?.transformFileUrl) {
7923
- return {
7924
- fileUrl: hooks.transformFileUrl(Key)
7925
- };
7926
- }
7927
- const publicUrl = await cf.r2.buckets.domains.managed.list(bucketName, {
7928
- account_id: accountId
7929
- });
7930
7923
  return {
7931
- fileUrl: `https://${publicUrl.domain}/${Key}`
7924
+ bucketName,
7925
+ key: Key
7932
7926
  };
7933
7927
  }
7934
7928
  };
package/dist/index.d.cts CHANGED
@@ -1,11 +1,12 @@
1
- import { DatabasePluginHooks, BasePluginArgs, DatabasePlugin, StoragePluginHooks, StoragePlugin } from '@hot-updater/plugin-core';
1
+ import * as _hot_updater_plugin_core from '@hot-updater/plugin-core';
2
+ import { DatabasePluginHooks, StoragePluginHooks, BasePluginArgs, StoragePlugin } from '@hot-updater/plugin-core';
2
3
 
3
4
  interface D1DatabaseConfig {
4
5
  databaseId: string;
5
6
  accountId: string;
6
7
  cloudflareApiToken: string;
7
8
  }
8
- declare const d1Database: (config: D1DatabaseConfig, hooks?: DatabasePluginHooks) => (_: BasePluginArgs) => DatabasePlugin;
9
+ declare const d1Database: (config: D1DatabaseConfig, hooks?: DatabasePluginHooks) => (options: _hot_updater_plugin_core.BasePluginArgs) => _hot_updater_plugin_core.DatabasePlugin;
9
10
 
10
11
  interface R2StorageConfig {
11
12
  cloudflareApiToken: string;
package/dist/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
- import { DatabasePluginHooks, BasePluginArgs, DatabasePlugin, StoragePluginHooks, StoragePlugin } from '@hot-updater/plugin-core';
1
+ import * as _hot_updater_plugin_core from '@hot-updater/plugin-core';
2
+ import { DatabasePluginHooks, StoragePluginHooks, BasePluginArgs, StoragePlugin } from '@hot-updater/plugin-core';
2
3
 
3
4
  interface D1DatabaseConfig {
4
5
  databaseId: string;
5
6
  accountId: string;
6
7
  cloudflareApiToken: string;
7
8
  }
8
- declare const d1Database: (config: D1DatabaseConfig, hooks?: DatabasePluginHooks) => (_: BasePluginArgs) => DatabasePlugin;
9
+ declare const d1Database: (config: D1DatabaseConfig, hooks?: DatabasePluginHooks) => (options: _hot_updater_plugin_core.BasePluginArgs) => _hot_updater_plugin_core.DatabasePlugin;
9
10
 
10
11
  interface R2StorageConfig {
11
12
  cloudflareApiToken: string;