@lvce-editor/update-worker 1.2.0 → 1.4.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.
@@ -910,6 +910,21 @@ const confirmPrompt = async message => {
910
910
  return true;
911
911
  };
912
912
 
913
+ const getBaseName = downloadUrl => {
914
+ const lastSlashIndex = downloadUrl.lastIndexOf('/');
915
+ return downloadUrl.slice(lastSlashIndex + 1);
916
+ };
917
+ const downloadToDisk = async (downloadUrl, response) => {
918
+ // @ts-ignore
919
+ const cacheDir = await invoke('PlatformPaths.getCacheDir');
920
+ const baseName = getBaseName(downloadUrl);
921
+ const diskPath = `${cacheDir}/${baseName}`;
922
+ const blob = await response.blob();
923
+ // @ts-ignore
924
+ await invoke('FileSystem.writeBlob', diskPath, blob);
925
+ return diskPath;
926
+ };
927
+
913
928
  const downloadUpdate = async updateUrl => {
914
929
  const response = await fetch(updateUrl, {
915
930
  priority: 'low',
@@ -921,21 +936,47 @@ const downloadUpdate = async updateUrl => {
921
936
  return response;
922
937
  };
923
938
 
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');
939
+ const putInCache = async (url, response, cache) => {
932
940
  await cache.put(url, response);
933
941
  };
934
942
 
935
- const downloadUpdateToCache = async (repository, version) => {
936
- const downloadUrl = getUpdateUrl(repository, version);
943
+ const downloadUpdateToCache = async (downloadUrl, cache) => {
937
944
  const response = await downloadUpdate(downloadUrl);
938
- await putInCache(downloadUrl, response);
945
+ await putInCache(downloadUrl, response, cache);
946
+ };
947
+
948
+ /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
949
+
950
+ const cachedCaches = Object.create(null);
951
+ const noopCache = {
952
+ async match() {
953
+ return undefined;
954
+ },
955
+ async put() {}
956
+ };
957
+ const supportsStorageBuckets = () => {
958
+ // @ts-ignore
959
+ return Boolean(navigator.storageBuckets);
960
+ };
961
+ const getCacheInternal = async (bucketName, cacheName) => {
962
+ if (!supportsStorageBuckets()) {
963
+ return noopCache;
964
+ }
965
+ const twoWeeks = 14 * 24 * 60 * 60 * 1000;
966
+ // @ts-ignore
967
+ const bucket = await navigator.storageBuckets.open(bucketName, {
968
+ quota: 1000 * 1024 * 1024,
969
+ // 1 GB
970
+ expires: Date.now() + twoWeeks
971
+ });
972
+ const cache = await bucket.caches.open(cacheName);
973
+ return cache;
974
+ };
975
+ const getCache = (bucketName, cacheName) => {
976
+ if (!(cacheName in cachedCaches)) {
977
+ cachedCaches[cacheName] = getCacheInternal(bucketName, cacheName);
978
+ }
979
+ return cachedCaches[cacheName];
939
980
  };
940
981
 
941
982
  const parseVersionFromUrl = (url, repository) => {
@@ -968,6 +1009,12 @@ const getLatestReleaseVersion = async repository => {
968
1009
  }
969
1010
  };
970
1011
 
1012
+ const getUpdateUrl = (repository, version) => {
1013
+ const fileName = `Lvce-Setup-v${version}-x64.exe`;
1014
+ const url = `https://github.com/${repository}/releases/download/v${version}/${fileName}`;
1015
+ return url;
1016
+ };
1017
+
971
1018
  const exec = async (path, args, options) => {
972
1019
  // TODO
973
1020
  return {
@@ -1001,6 +1048,14 @@ const installAndRestart = async downloadPath => {
1001
1048
  }
1002
1049
  };
1003
1050
 
1051
+ const isCached = async (url, cache) => {
1052
+ const match = await cache.match(url);
1053
+ if (match) {
1054
+ return true;
1055
+ }
1056
+ return false;
1057
+ };
1058
+
1004
1059
  const shouldUpdate = async (updateSetting, version) => {
1005
1060
  if (updateSetting && updateSetting !== 'none') {
1006
1061
  return true;
@@ -1012,6 +1067,8 @@ const shouldUpdate = async (updateSetting, version) => {
1012
1067
 
1013
1068
  const checkForUpdates = async (updateSetting, repository) => {
1014
1069
  try {
1070
+ const bucketName = 'electron-updates';
1071
+ const cacheName = 'electron-updates';
1015
1072
  const info = await getLatestReleaseVersion(repository);
1016
1073
  if (!info || !info.version) {
1017
1074
  return {
@@ -1019,18 +1076,23 @@ const checkForUpdates = async (updateSetting, repository) => {
1019
1076
  error: undefined
1020
1077
  };
1021
1078
  }
1079
+ const cache = await getCache(bucketName, cacheName);
1022
1080
  if (!(await shouldUpdate(updateSetting, info.version))) {
1023
1081
  return {
1024
1082
  updated: false,
1025
1083
  error: undefined
1026
1084
  };
1027
1085
  }
1086
+ const downloadUrl = getUpdateUrl(repository, info.version);
1087
+
1028
1088
  // @ts-ignore
1029
1089
  await invoke('Layout.setUpdateState', {
1030
1090
  state: 'downloading',
1031
1091
  progress: 0
1032
1092
  });
1033
- await downloadUpdateToCache(repository, info.version);
1093
+ if (!(await isCached(downloadUrl, cache))) {
1094
+ await downloadUpdateToCache(downloadUrl, cache);
1095
+ }
1034
1096
  const messageRestart = promptRestart();
1035
1097
  const shouldRestart = await confirmPrompt(messageRestart);
1036
1098
  if (!shouldRestart) {
@@ -1039,8 +1101,15 @@ const checkForUpdates = async (updateSetting, repository) => {
1039
1101
  error: undefined
1040
1102
  };
1041
1103
  }
1042
- const downloadPath = ''; // TODO get it from cache to disk somehow
1043
- await installAndRestart(downloadPath);
1104
+ const response = await cache.match(downloadUrl);
1105
+ if (!response) {
1106
+ return {
1107
+ updated: false,
1108
+ error: undefined
1109
+ };
1110
+ }
1111
+ const diskPath = await downloadToDisk(downloadUrl, response);
1112
+ await installAndRestart(diskPath);
1044
1113
  return {
1045
1114
  updated: true,
1046
1115
  error: undefined
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.4.0",
4
4
  "description": "Update Worker",
5
5
  "repository": {
6
6
  "type": "git",