@lvce-editor/update-worker 1.1.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.
@@ -903,6 +903,13 @@ const promptRestart = () => {
903
903
  return i18nString(UiStrings.DoYouWantToRestart);
904
904
  };
905
905
 
906
+ // TODO avoid js prompt in elctron, since it crashes electron
907
+
908
+ const confirmPrompt = async message => {
909
+ // TODO
910
+ return true;
911
+ };
912
+
906
913
  const downloadUpdate = async updateUrl => {
907
914
  const response = await fetch(updateUrl, {
908
915
  priority: 'low',
@@ -914,21 +921,47 @@ const downloadUpdate = async updateUrl => {
914
921
  return response;
915
922
  };
916
923
 
917
- const getUpdateUrl = (repository, version) => {
918
- const fileName = `Lvce-Setup-v${version}-x64.exe`;
919
- const url = `https://github.com/${repository}/releases/download/v${version}/${fileName}`;
920
- return url;
921
- };
922
-
923
- const putInCache = async (url, response) => {
924
- const cache = await caches.open('electron-updates');
924
+ const putInCache = async (url, response, cache) => {
925
925
  await cache.put(url, response);
926
926
  };
927
927
 
928
- const downloadUpdateToCache = async (repository, version) => {
929
- const downloadUrl = getUpdateUrl(repository, version);
928
+ const downloadUpdateToCache = async (downloadUrl, cache) => {
930
929
  const response = await downloadUpdate(downloadUrl);
931
- 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];
932
965
  };
933
966
 
934
967
  const parseVersionFromUrl = (url, repository) => {
@@ -951,7 +984,7 @@ const getLatestReleaseVersion = async repository => {
951
984
  const finalUrlResponse = await fetch(url, {
952
985
  method: 'HEAD'
953
986
  });
954
- const finalUrl = await finalUrlResponse.text();
987
+ const finalUrl = finalUrlResponse.url;
955
988
  const version = parseVersionFromUrl(finalUrl, repository);
956
989
  return {
957
990
  version
@@ -961,6 +994,12 @@ const getLatestReleaseVersion = async repository => {
961
994
  }
962
995
  };
963
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
+
964
1003
  const exec = async (path, args, options) => {
965
1004
  // TODO
966
1005
  return {
@@ -994,42 +1033,72 @@ const installAndRestart = async downloadPath => {
994
1033
  }
995
1034
  };
996
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
+
997
1044
  const shouldUpdate = async (updateSetting, version) => {
998
1045
  if (updateSetting && updateSetting !== 'none') {
999
1046
  return true;
1000
1047
  }
1001
- const message = promptMessage(version);
1002
- const shouldUpdate = await invoke('ConfirmPrompt.prompt', message, {
1003
- confirmMessage: '',
1004
- title: ''
1005
- });
1048
+ promptMessage(version);
1049
+ const shouldUpdate = await confirmPrompt();
1006
1050
  return shouldUpdate;
1007
1051
  };
1008
1052
 
1009
1053
  const checkForUpdates = async (updateSetting, repository) => {
1010
- const info = await getLatestReleaseVersion(repository);
1011
- if (!info || !info.version) {
1012
- return;
1013
- }
1014
- if (!(await shouldUpdate(updateSetting, info.version))) {
1015
- return;
1016
- }
1017
- // @ts-ignore
1018
- await invoke('Layout.setUpdateState', {
1019
- state: 'downloading',
1020
- progress: 0
1021
- });
1022
- await downloadUpdateToCache(repository, info.version);
1023
- const downloadPath = ''; // TODO get it from cache to disk somehow
1024
- const messageRestart = promptRestart();
1025
- const shouldRestart = await invoke('ConfirmPrompt.prompt', messageRestart, {
1026
- confirmMessage: '',
1027
- title: ''
1028
- });
1029
- if (!shouldRestart) {
1030
- return;
1054
+ try {
1055
+ const bucketName = 'electron-updates';
1056
+ const cacheName = 'electron-updates';
1057
+ const info = await getLatestReleaseVersion(repository);
1058
+ if (!info || !info.version) {
1059
+ return {
1060
+ updated: false,
1061
+ error: undefined
1062
+ };
1063
+ }
1064
+ const cache = await getCache(bucketName, cacheName);
1065
+ if (!(await shouldUpdate(updateSetting, info.version))) {
1066
+ return {
1067
+ updated: false,
1068
+ error: undefined
1069
+ };
1070
+ }
1071
+ const downloadUrl = getUpdateUrl(repository, info.version);
1072
+
1073
+ // @ts-ignore
1074
+ await invoke('Layout.setUpdateState', {
1075
+ state: 'downloading',
1076
+ progress: 0
1077
+ });
1078
+ if (!(await isCached(downloadUrl, cache))) {
1079
+ await downloadUpdateToCache(downloadUrl, cache);
1080
+ }
1081
+ const messageRestart = promptRestart();
1082
+ const shouldRestart = await confirmPrompt(messageRestart);
1083
+ if (!shouldRestart) {
1084
+ return {
1085
+ updated: false,
1086
+ error: undefined
1087
+ };
1088
+ }
1089
+ const downloadPath = ''; // TODO get it from cache to disk somehow
1090
+ await installAndRestart(downloadPath);
1091
+ return {
1092
+ updated: true,
1093
+ error: undefined
1094
+ };
1095
+ } catch (error) {
1096
+ console.error(error);
1097
+ return {
1098
+ updated: false,
1099
+ error: undefined
1100
+ };
1031
1101
  }
1032
- await installAndRestart(downloadPath);
1033
1102
  };
1034
1103
 
1035
1104
  const commandMap = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/update-worker",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Update Worker",
5
5
  "repository": {
6
6
  "type": "git",