@lvce-editor/update-worker 1.2.0 → 1.3.0

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.
@@ -921,21 +921,47 @@ const downloadUpdate = async updateUrl => {
921
921
  return response;
922
922
  };
923
923
 
924
- const getUpdateUrl = (repository, version) => {
925
- const fileName = `Lvce-Setup-v${version}-x64.exe`;
926
- const url = `https://github.com/${repository}/releases/download/v${version}/${fileName}`;
927
- return url;
928
- };
929
-
930
- const putInCache = async (url, response) => {
931
- const cache = await caches.open('electron-updates');
924
+ const putInCache = async (url, response, cache) => {
932
925
  await cache.put(url, response);
933
926
  };
934
927
 
935
- const downloadUpdateToCache = async (repository, version) => {
936
- const downloadUrl = getUpdateUrl(repository, version);
928
+ const downloadUpdateToCache = async (downloadUrl, cache) => {
937
929
  const response = await downloadUpdate(downloadUrl);
938
- await putInCache(downloadUrl, response);
930
+ await putInCache(downloadUrl, response, cache);
931
+ };
932
+
933
+ /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
934
+
935
+ const cachedCaches = Object.create(null);
936
+ const noopCache = {
937
+ async match() {
938
+ return undefined;
939
+ },
940
+ async put() {}
941
+ };
942
+ const supportsStorageBuckets = () => {
943
+ // @ts-ignore
944
+ return Boolean(navigator.storageBuckets);
945
+ };
946
+ const getCacheInternal = async (bucketName, cacheName) => {
947
+ if (!supportsStorageBuckets()) {
948
+ return noopCache;
949
+ }
950
+ const twoWeeks = 14 * 24 * 60 * 60 * 1000;
951
+ // @ts-ignore
952
+ const bucket = await navigator.storageBuckets.open(bucketName, {
953
+ quota: 1000 * 1024 * 1024,
954
+ // 1 GB
955
+ expires: Date.now() + twoWeeks
956
+ });
957
+ const cache = await bucket.caches.open(cacheName);
958
+ return cache;
959
+ };
960
+ const getCache = (bucketName, cacheName) => {
961
+ if (!(cacheName in cachedCaches)) {
962
+ cachedCaches[cacheName] = getCacheInternal(bucketName, cacheName);
963
+ }
964
+ return cachedCaches[cacheName];
939
965
  };
940
966
 
941
967
  const parseVersionFromUrl = (url, repository) => {
@@ -968,6 +994,12 @@ const getLatestReleaseVersion = async repository => {
968
994
  }
969
995
  };
970
996
 
997
+ const getUpdateUrl = (repository, version) => {
998
+ const fileName = `Lvce-Setup-v${version}-x64.exe`;
999
+ const url = `https://github.com/${repository}/releases/download/v${version}/${fileName}`;
1000
+ return url;
1001
+ };
1002
+
971
1003
  const exec = async (path, args, options) => {
972
1004
  // TODO
973
1005
  return {
@@ -1001,6 +1033,14 @@ const installAndRestart = async downloadPath => {
1001
1033
  }
1002
1034
  };
1003
1035
 
1036
+ const isCached = async (url, cache) => {
1037
+ const match = await cache.match(url);
1038
+ if (match) {
1039
+ return true;
1040
+ }
1041
+ return false;
1042
+ };
1043
+
1004
1044
  const shouldUpdate = async (updateSetting, version) => {
1005
1045
  if (updateSetting && updateSetting !== 'none') {
1006
1046
  return true;
@@ -1012,6 +1052,8 @@ const shouldUpdate = async (updateSetting, version) => {
1012
1052
 
1013
1053
  const checkForUpdates = async (updateSetting, repository) => {
1014
1054
  try {
1055
+ const bucketName = 'electron-updates';
1056
+ const cacheName = 'electron-updates';
1015
1057
  const info = await getLatestReleaseVersion(repository);
1016
1058
  if (!info || !info.version) {
1017
1059
  return {
@@ -1019,18 +1061,23 @@ const checkForUpdates = async (updateSetting, repository) => {
1019
1061
  error: undefined
1020
1062
  };
1021
1063
  }
1064
+ const cache = await getCache(bucketName, cacheName);
1022
1065
  if (!(await shouldUpdate(updateSetting, info.version))) {
1023
1066
  return {
1024
1067
  updated: false,
1025
1068
  error: undefined
1026
1069
  };
1027
1070
  }
1071
+ const downloadUrl = getUpdateUrl(repository, info.version);
1072
+
1028
1073
  // @ts-ignore
1029
1074
  await invoke('Layout.setUpdateState', {
1030
1075
  state: 'downloading',
1031
1076
  progress: 0
1032
1077
  });
1033
- await downloadUpdateToCache(repository, info.version);
1078
+ if (!(await isCached(downloadUrl, cache))) {
1079
+ await downloadUpdateToCache(downloadUrl, cache);
1080
+ }
1034
1081
  const messageRestart = promptRestart();
1035
1082
  const shouldRestart = await confirmPrompt(messageRestart);
1036
1083
  if (!shouldRestart) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/update-worker",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Update Worker",
5
5
  "repository": {
6
6
  "type": "git",