electron-incremental-update 1.3.0 → 2.0.0-beta.10

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/README.md CHANGED
@@ -10,6 +10,8 @@ The new `${electron.app.name}.asar`, which can download from remote or load from
10
10
 
11
11
  All **native modules** should be packaged into `app.asar` to reduce `${electron.app.name}.asar` file size, [see usage](#use-native-modules). Therefore, auto upgrade of portable app is possible.
12
12
 
13
+ Support bytecode protection, [see details](#bytecode-protection)
14
+
13
15
  No `vite-plugin-electron-renderer` config
14
16
 
15
17
  - inspired by [Obsidian](https://obsidian.md/)'s upgrade strategy
@@ -179,7 +181,7 @@ initApp({
179
181
  in `electron/main/index.ts`
180
182
 
181
183
  ```ts
182
- import { startupWithUpdater } from 'electron-incremental-update'
184
+ import { UpdaterError, startupWithUpdater } from 'electron-incremental-update'
183
185
  import { getPathFromAppNameAsar, getVersions } from 'electron-incremental-update/utils'
184
186
  import { app } from 'electron'
185
187
 
@@ -196,10 +198,12 @@ export default startupWithUpdater((updater) => {
196
198
  console.log(percent)
197
199
  }
198
200
  updater.logger = console
201
+ updater.receiveBeta = true
202
+
199
203
  updater.checkUpdate().then(async (result) => {
200
204
  if (result === undefined) {
201
205
  console.log('Update Unavailable')
202
- } else if (result instanceof Error) {
206
+ } else if (result instanceof UpdaterError) {
203
207
  console.error(result)
204
208
  } else {
205
209
  console.log('new version: ', result.version)
@@ -272,7 +276,7 @@ import Database from 'better-sqlite3'
272
276
 
273
277
  const db = new Database(':memory:', { nativeBinding: './better_sqlite3.node' })
274
278
 
275
- export function test() {
279
+ export function test(): void {
276
280
  db.exec(
277
281
  'DROP TABLE IF EXISTS employees; '
278
282
  + 'CREATE TABLE IF NOT EXISTS employees (name TEXT, salary INTEGER)',
@@ -0,0 +1,81 @@
1
+ import { __require } from './chunk-72ZAJ7AF.js';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { app } from 'electron';
5
+
6
+ var isDev = __EIU_IS_DEV__;
7
+ var isWin = process.platform === "win32";
8
+ var isMac = process.platform === "darwin";
9
+ var isLinux = process.platform === "linux";
10
+ function getPathFromAppNameAsar(...paths) {
11
+ return isDev ? "DEV.asar" : path.join(path.dirname(app.getAppPath()), `${app.name}.asar`, ...paths);
12
+ }
13
+ function getAppVersion() {
14
+ return isDev ? getEntryVersion() : fs.readFileSync(getPathFromAppNameAsar("version"), "utf-8");
15
+ }
16
+ function getEntryVersion() {
17
+ return app.getVersion();
18
+ }
19
+ function requireNative(moduleName) {
20
+ return __require(path.join(app.getAppPath(), __EIU_ENTRY_DIST_PATH__, moduleName));
21
+ }
22
+ function restartApp() {
23
+ app.relaunch();
24
+ app.quit();
25
+ }
26
+ function setAppUserModelId(id) {
27
+ if (isWin) {
28
+ app.setAppUserModelId(id ?? `org.${app.name}`);
29
+ }
30
+ }
31
+ function disableHWAccForWin7() {
32
+ if (__require("node:os").release().startsWith("6.1")) {
33
+ app.disableHardwareAcceleration();
34
+ }
35
+ }
36
+ function singleInstance(window) {
37
+ const result = app.requestSingleInstanceLock();
38
+ if (result) {
39
+ app.on("second-instance", () => {
40
+ if (window) {
41
+ window.show();
42
+ if (window.isMinimized()) {
43
+ window.restore();
44
+ }
45
+ window.focus();
46
+ }
47
+ });
48
+ } else {
49
+ app.quit();
50
+ }
51
+ return result;
52
+ }
53
+ function setPortableAppDataPath(dirName = "data") {
54
+ const portablePath = path.join(path.dirname(app.getPath("exe")), dirName);
55
+ if (!fs.existsSync(portablePath)) {
56
+ fs.mkdirSync(portablePath);
57
+ }
58
+ app.setPath("appData", portablePath);
59
+ }
60
+ function loadPage(win, htmlFilePath = "index.html") {
61
+ if (isDev) {
62
+ win.loadURL(process.env.VITE_DEV_SERVER_URL + htmlFilePath);
63
+ } else {
64
+ win.loadFile(getPathFromAppNameAsar("renderer", htmlFilePath));
65
+ }
66
+ }
67
+ function getPathFromPreload(...paths) {
68
+ return isDev ? path.join(app.getAppPath(), __EIU_ELECTRON_DIST_PATH__, "preload", ...paths) : getPathFromAppNameAsar("preload", ...paths);
69
+ }
70
+ function getPathFromPublic(...paths) {
71
+ return isDev ? path.join(app.getAppPath(), "public", ...paths) : getPathFromAppNameAsar("renderer", ...paths);
72
+ }
73
+ function getPathFromEntryAsar(...paths) {
74
+ return path.join(app.getAppPath(), __EIU_ENTRY_DIST_PATH__, ...paths);
75
+ }
76
+ function handleUnexpectedErrors(callback) {
77
+ process.on("uncaughtException", callback);
78
+ process.on("unhandledRejection", callback);
79
+ }
80
+
81
+ export { disableHWAccForWin7, getAppVersion, getEntryVersion, getPathFromAppNameAsar, getPathFromEntryAsar, getPathFromPreload, getPathFromPublic, handleUnexpectedErrors, isDev, isLinux, isMac, isWin, loadPage, requireNative, restartApp, setAppUserModelId, setPortableAppDataPath, singleInstance };
@@ -0,0 +1,70 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ // src/utils/version.ts
9
+ function parseVersion(version) {
10
+ const match = /^(\d+)\.(\d+)\.(\d+)(?:-([a-z0-9.-]+))?/i.exec(version);
11
+ if (!match) {
12
+ throw new TypeError(`invalid version: ${version}`);
13
+ }
14
+ const [major, minor, patch] = match.slice(1, 4).map(Number);
15
+ const ret = {
16
+ major,
17
+ minor,
18
+ patch,
19
+ stage: "",
20
+ stageVersion: -1
21
+ };
22
+ if (match[4]) {
23
+ let [stage, _v] = match[4].split(".");
24
+ ret.stage = stage;
25
+ ret.stageVersion = Number(_v) || -1;
26
+ }
27
+ if (Number.isNaN(major) || Number.isNaN(minor) || Number.isNaN(patch) || Number.isNaN(ret.stageVersion)) {
28
+ throw new TypeError(`invalid version: ${version}`);
29
+ }
30
+ return ret;
31
+ }
32
+ function defaultIsLowerVersion(oldVer, newVer) {
33
+ const oldV = parseVersion(oldVer);
34
+ const newV = parseVersion(newVer);
35
+ function compareStrings(str1, str2) {
36
+ if (str1 === "") {
37
+ return str2 !== "";
38
+ } else if (str2 === "") {
39
+ return true;
40
+ }
41
+ return str1 < str2;
42
+ }
43
+ for (let key of Object.keys(oldV)) {
44
+ if (key === "stage" && compareStrings(oldV[key], newV[key])) {
45
+ return true;
46
+ } else if (oldV[key] !== newV[key]) {
47
+ return oldV[key] < newV[key];
48
+ }
49
+ }
50
+ return false;
51
+ }
52
+ function isUpdateJSON(json) {
53
+ const is = (j) => !!(j && j.minimumVersion && j.signature && j.version);
54
+ return is(json) && is(json?.beta);
55
+ }
56
+ function defaultVersionJsonGenerator(existingJson, signature, version, minimumVersion) {
57
+ existingJson.beta = {
58
+ version,
59
+ minimumVersion,
60
+ signature
61
+ };
62
+ if (!parseVersion(version).stage) {
63
+ existingJson.version = version;
64
+ existingJson.minimumVersion = minimumVersion;
65
+ existingJson.signature = signature;
66
+ }
67
+ return existingJson;
68
+ }
69
+
70
+ export { __require, defaultIsLowerVersion, defaultVersionJsonGenerator, isUpdateJSON, parseVersion };
@@ -0,0 +1,55 @@
1
+ import zlib from 'node:zlib';
2
+ import crypto from 'node:crypto';
3
+
4
+ // src/utils/zip.ts
5
+ async function defaultZipFile(buffer) {
6
+ return new Promise((resolve, reject) => {
7
+ zlib.brotliCompress(buffer, (err, buffer2) => {
8
+ if (err) {
9
+ reject(err);
10
+ } else {
11
+ resolve(buffer2);
12
+ }
13
+ });
14
+ });
15
+ }
16
+ async function defaultUnzipFile(buffer) {
17
+ return new Promise((resolve, reject) => {
18
+ zlib.brotliDecompress(buffer, (err, buffer2) => {
19
+ if (err) {
20
+ reject(err);
21
+ } else {
22
+ resolve(buffer2);
23
+ }
24
+ });
25
+ });
26
+ }
27
+ function hashBuffer(data, length) {
28
+ const hash = crypto.createHash("SHA256").update(data).digest("binary");
29
+ return Buffer.from(hash).subarray(0, length);
30
+ }
31
+ function aesEncrypt(plainText, key, iv) {
32
+ const cipher = crypto.createCipheriv("aes-256-cbc", key, iv);
33
+ return cipher.update(plainText, "utf8", "base64url") + cipher.final("base64url");
34
+ }
35
+ function defaultSignature(buffer, privateKey, cert, version) {
36
+ const sig = crypto.createSign("RSA-SHA256").update(buffer).sign(crypto.createPrivateKey(privateKey), "base64");
37
+ return aesEncrypt(`${sig}%${version}`, hashBuffer(cert, 32), hashBuffer(buffer, 16));
38
+ }
39
+ function aesDecrypt(encryptedText, key, iv) {
40
+ const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv);
41
+ return decipher.update(encryptedText, "base64url", "utf8") + decipher.final("utf8");
42
+ }
43
+ function defaultVerifySignature(buffer, version, signature, cert) {
44
+ try {
45
+ const [sig, ver] = aesDecrypt(signature, hashBuffer(cert, 32), hashBuffer(buffer, 16)).split("%");
46
+ if (ver !== version) {
47
+ return false;
48
+ }
49
+ return crypto.createVerify("RSA-SHA256").update(buffer).verify(cert, sig, "base64");
50
+ } catch {
51
+ return false;
52
+ }
53
+ }
54
+
55
+ export { aesDecrypt, aesEncrypt, defaultSignature, defaultUnzipFile, defaultVerifySignature, defaultZipFile, hashBuffer };