electron-incremental-update 0.1.2 → 0.1.3

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
@@ -1,25 +1,37 @@
1
1
  ## electron incremental updater
2
2
 
3
- inspired by Obsidian's update strategy, using RSA + Signature to sign the update asar and replace the old one when verified
3
+ provider a vite plugin and useful functions to generate updater and split entry file and real app
4
+
5
+ ### principle
6
+
7
+ using two asar, `app.asar` and `main.asar` (if "main" is your app's name)
8
+
9
+ the `app.asar` is used to load `main.asar` and initialize the updater
10
+
11
+ using RSA + Signature to sign the new `main.asar` downloaded from remote and replace the old one when verified
12
+
13
+ - inspired by Obsidian's update strategy
14
+
15
+ ### notice
4
16
 
5
17
  develop with [vite-plugin-electron](https://github.com/electron-vite/vite-plugin-electron), and may be effect in other electron vite frameworks
6
18
 
7
- ### install
19
+ ## install
8
20
 
9
- #### npm
21
+ ### npm
10
22
  ```bash
11
23
  npm install electron-incremental-update
12
24
  ```
13
- #### yarn
25
+ ### yarn
14
26
  ```bash
15
27
  yarn add electron-incremental-update
16
28
  ```
17
- #### pnpm
29
+ ### pnpm
18
30
  ```bash
19
31
  pnpm add electron-incremental-update
20
32
  ```
21
33
 
22
- ### usage
34
+ ## usage
23
35
 
24
36
  base on [electron-vite-vue](https://github.com/electron-vite/electron-vite-vue)
25
37
 
@@ -36,7 +48,7 @@ src
36
48
  └── ...
37
49
  ```
38
50
 
39
- #### setup app
51
+ ### setup app
40
52
 
41
53
  ```ts
42
54
  // electron/app.ts
@@ -53,7 +65,7 @@ const updater = createUpdater({
53
65
  initApp(name, updater)
54
66
  ```
55
67
 
56
- #### setup main
68
+ ### setup main
57
69
 
58
70
  ```ts
59
71
  // electron/main/index.ts
@@ -103,7 +115,7 @@ export default function (updater: Updater) {
103
115
  }
104
116
  ```
105
117
 
106
- #### use native modules
118
+ ### use native modules
107
119
 
108
120
  ```ts
109
121
  // db.ts
@@ -128,7 +140,7 @@ console.log(r)
128
140
  db.close()
129
141
  ```
130
142
 
131
- #### setup vite.config.ts
143
+ ### setup vite.config.ts
132
144
 
133
145
  ```ts
134
146
  import { rmSync } from 'node:fs'
@@ -189,7 +201,7 @@ export default defineConfig(({ command }) => {
189
201
  })
190
202
  ```
191
203
 
192
- ##### option
204
+ #### option
193
205
 
194
206
  ```ts
195
207
  type Options = {
@@ -213,6 +225,9 @@ type Options = {
213
225
  * Whether to minify
214
226
  */
215
227
  minify?: boolean
228
+ /**
229
+ * path config
230
+ */
216
231
  paths?: {
217
232
  /**
218
233
  * Path to app entry file
@@ -225,7 +240,7 @@ type Options = {
225
240
  */
226
241
  entryOutputPath?: string
227
242
  /**
228
- * Path to app entry file
243
+ * Path to asar file
229
244
  * @default `release/${ProductName}.asar`
230
245
  */
231
246
  asarOutputPath?: string
@@ -239,7 +254,15 @@ type Options = {
239
254
  * @default `dist`
240
255
  */
241
256
  rendererDistPath?: string
257
+ /**
258
+ * Path to version info output
259
+ * @default `version.json`
260
+ */
261
+ versionPath?: string
242
262
  }
263
+ /**
264
+ * signature config
265
+ */
243
266
  keys?: {
244
267
  /**
245
268
  * Path to the pem file that contains private key
@@ -262,7 +285,7 @@ type Options = {
262
285
  }
263
286
  ```
264
287
 
265
- #### electron-builder config
288
+ ### electron-builder config
266
289
 
267
290
  ```js
268
291
  const { name } = require('./package.json')
package/dist/index.cjs CHANGED
@@ -89,55 +89,68 @@ var import_node_fs2 = require("fs");
89
89
  var import_promises = require("fs/promises");
90
90
  var import_node_https = __toESM(require("https"), 1);
91
91
  var import_electron2 = require("electron");
92
+ function downloadJSONDefault(url, updater, headers) {
93
+ return new Promise((resolve2, reject) => {
94
+ import_node_https.default.get(url, (res) => {
95
+ let data = "";
96
+ res.setEncoding("utf8");
97
+ res.headers = headers;
98
+ res.on("data", (chunk) => data += chunk);
99
+ res.on("end", () => {
100
+ updater.emit("downloadEnd", true);
101
+ const json = JSON.parse(data);
102
+ if ("signature" in json && "version" in json && "size" in json) {
103
+ resolve2(json);
104
+ } else {
105
+ throw new Error("invalid update json");
106
+ }
107
+ });
108
+ }).on("error", (e) => {
109
+ e && updater.emit("donwnloadError", e);
110
+ updater.emit("downloadEnd", false);
111
+ reject(e);
112
+ });
113
+ });
114
+ }
115
+ function downloadBufferDefault(url, updater, headers) {
116
+ return new Promise((resolve2, reject) => {
117
+ import_node_https.default.get(url, (res) => {
118
+ let data = [];
119
+ res.headers = headers;
120
+ res.on("data", (chunk) => {
121
+ updater.emit("downloading", chunk.length);
122
+ data.push(chunk);
123
+ });
124
+ res.on("end", () => {
125
+ updater.emit("downloadEnd", true);
126
+ resolve2(import_node_buffer.Buffer.concat(data));
127
+ });
128
+ }).on("error", (e) => {
129
+ e && updater.emit("donwnloadError", e);
130
+ updater.emit("downloadEnd", false);
131
+ reject(e);
132
+ });
133
+ });
134
+ }
92
135
  function createUpdater({
93
136
  SIGNATURE_PUB,
94
137
  repository,
95
138
  productName,
96
139
  releaseAsarURL: _release,
97
140
  updateJsonURL: _update,
98
- userAgent,
99
- extraHeader
141
+ downloadConfig
100
142
  }) {
101
143
  const updater = new import_node_events.EventEmitter();
144
+ const { downloadBuffer, downloadJSON, extraHeader, userAgent } = downloadConfig || {};
102
145
  async function download(url, format) {
103
146
  const ua = userAgent || "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36";
104
- const commonHeader = {
147
+ const headers = {
148
+ Accept: `application/${format === "json" ? "json" : "octet-stream"}`,
105
149
  UserAgent: ua,
106
150
  ...extraHeader
107
151
  };
108
- return await new Promise((resolve2, reject) => {
109
- import_node_https.default.get(url, (res) => {
110
- if (format === "json") {
111
- let data = "";
112
- res.setEncoding("utf8");
113
- res.headers = {
114
- Accept: "application/json",
115
- ...commonHeader
116
- };
117
- res.on("data", (chunk) => data += chunk);
118
- res.on("end", () => {
119
- resolve2(JSON.parse(data));
120
- });
121
- } else if (format === "buffer") {
122
- let data = [];
123
- res.headers = {
124
- Accept: "application/octet-stream",
125
- ...commonHeader
126
- };
127
- res.on("data", (chunk) => {
128
- updater.emit("downloading", chunk.length);
129
- data.push(chunk);
130
- });
131
- res.on("end", () => {
132
- updater.emit("downloadEnd", true);
133
- resolve2(import_node_buffer.Buffer.concat(data));
134
- });
135
- }
136
- }).on("error", (e) => {
137
- e && updater.emit("donwnloadError", e);
138
- reject(e);
139
- });
140
- });
152
+ const downloadFn = format === "json" ? downloadJSON ?? downloadJSONDefault : downloadBuffer ?? downloadBufferDefault;
153
+ return await downloadFn(url, updater, headers);
141
154
  }
142
155
  async function extractFile(gzipFilePath) {
143
156
  if (!gzipFilePath.endsWith(".asar.gz") || !(0, import_node_fs2.existsSync)(gzipFilePath)) {
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Buffer } from 'node:buffer';
2
+
1
3
  type CheckResultType = 'success' | 'fail' | 'unavailable';
2
4
  type UpdateEvents = {
3
5
  check: null;
@@ -7,6 +9,11 @@ type UpdateEvents = {
7
9
  downloadEnd: [success: boolean];
8
10
  donwnloadError: [error: unknown];
9
11
  };
12
+ type UpdateJSON = {
13
+ signature: string;
14
+ version: string;
15
+ size: number;
16
+ };
10
17
  type MaybeArray<T> = T extends undefined | null | never ? [] : T extends any[] ? T['length'] extends 1 ? [data: T[0]] : T : [data: T];
11
18
  interface UpdateOption {
12
19
  /**
@@ -53,29 +60,47 @@ interface Options extends UpdateOption {
53
60
  /**
54
61
  * product name
55
62
  *
56
- * you can use the `name` in package.json
63
+ * you can use the `name` in `package.json`
57
64
  */
58
65
  productName: string;
59
66
  /**
60
67
  * repository url, e.g. `https://github.com/electron/electron`
61
68
  *
62
- * you can use the `repository` in package.json
69
+ * you can use the `repository` in `package.json`
63
70
  *
64
71
  * if `updateJsonURL` or `releaseAsarURL` are absent,
65
72
  * `repository` will be used to determine the url
66
73
  */
67
74
  repository?: string;
68
- /**
69
- * download user agent
70
- * @default 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
71
- */
72
- userAgent?: string;
73
- /**
74
- * extra download header, `accept` and `user-agent` is set by default
75
- */
76
- extraHeader?: Record<string, string>;
75
+ downloadConfig?: {
76
+ /**
77
+ * download user agent
78
+ * @default 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
79
+ */
80
+ userAgent?: string;
81
+ /**
82
+ * extra download header, `accept` and `user-agent` is set by default
83
+ */
84
+ extraHeader?: Record<string, string>;
85
+ /**
86
+ * download JSON function
87
+ * @param url download url
88
+ * @param updater updater, emit events
89
+ * @param header download header
90
+ * @returns `UpdateJSON`
91
+ */
92
+ downloadJSON?: (url: string, updater: Updater, headers: Record<string, any>) => Promise<UpdateJSON>;
93
+ /**
94
+ * download buffer function
95
+ * @param url download url
96
+ * @param updater updater, emit events
97
+ * @param header download header
98
+ * @returns `Buffer`
99
+ */
100
+ downloadBuffer?: (url: string, updater: Updater, headers: Record<string, any>) => Promise<Buffer>;
101
+ };
77
102
  }
78
- declare function createUpdater({ SIGNATURE_PUB, repository, productName, releaseAsarURL: _release, updateJsonURL: _update, userAgent, extraHeader, }: Options): Updater;
103
+ declare function createUpdater({ SIGNATURE_PUB, repository, productName, releaseAsarURL: _release, updateJsonURL: _update, downloadConfig, }: Options): Updater;
79
104
 
80
105
  declare function getAppAsarPath(name: string): string;
81
106
  declare function getElectronVersion(): string;
package/dist/index.mjs CHANGED
@@ -53,55 +53,68 @@ import { createReadStream, createWriteStream, existsSync } from "node:fs";
53
53
  import { rm, writeFile } from "node:fs/promises";
54
54
  import https from "node:https";
55
55
  import { app as app2 } from "electron";
56
+ function downloadJSONDefault(url, updater, headers) {
57
+ return new Promise((resolve2, reject) => {
58
+ https.get(url, (res) => {
59
+ let data = "";
60
+ res.setEncoding("utf8");
61
+ res.headers = headers;
62
+ res.on("data", (chunk) => data += chunk);
63
+ res.on("end", () => {
64
+ updater.emit("downloadEnd", true);
65
+ const json = JSON.parse(data);
66
+ if ("signature" in json && "version" in json && "size" in json) {
67
+ resolve2(json);
68
+ } else {
69
+ throw new Error("invalid update json");
70
+ }
71
+ });
72
+ }).on("error", (e) => {
73
+ e && updater.emit("donwnloadError", e);
74
+ updater.emit("downloadEnd", false);
75
+ reject(e);
76
+ });
77
+ });
78
+ }
79
+ function downloadBufferDefault(url, updater, headers) {
80
+ return new Promise((resolve2, reject) => {
81
+ https.get(url, (res) => {
82
+ let data = [];
83
+ res.headers = headers;
84
+ res.on("data", (chunk) => {
85
+ updater.emit("downloading", chunk.length);
86
+ data.push(chunk);
87
+ });
88
+ res.on("end", () => {
89
+ updater.emit("downloadEnd", true);
90
+ resolve2(Buffer.concat(data));
91
+ });
92
+ }).on("error", (e) => {
93
+ e && updater.emit("donwnloadError", e);
94
+ updater.emit("downloadEnd", false);
95
+ reject(e);
96
+ });
97
+ });
98
+ }
56
99
  function createUpdater({
57
100
  SIGNATURE_PUB,
58
101
  repository,
59
102
  productName,
60
103
  releaseAsarURL: _release,
61
104
  updateJsonURL: _update,
62
- userAgent,
63
- extraHeader
105
+ downloadConfig
64
106
  }) {
65
107
  const updater = new EventEmitter();
108
+ const { downloadBuffer, downloadJSON, extraHeader, userAgent } = downloadConfig || {};
66
109
  async function download(url, format) {
67
110
  const ua = userAgent || "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36";
68
- const commonHeader = {
111
+ const headers = {
112
+ Accept: `application/${format === "json" ? "json" : "octet-stream"}`,
69
113
  UserAgent: ua,
70
114
  ...extraHeader
71
115
  };
72
- return await new Promise((resolve2, reject) => {
73
- https.get(url, (res) => {
74
- if (format === "json") {
75
- let data = "";
76
- res.setEncoding("utf8");
77
- res.headers = {
78
- Accept: "application/json",
79
- ...commonHeader
80
- };
81
- res.on("data", (chunk) => data += chunk);
82
- res.on("end", () => {
83
- resolve2(JSON.parse(data));
84
- });
85
- } else if (format === "buffer") {
86
- let data = [];
87
- res.headers = {
88
- Accept: "application/octet-stream",
89
- ...commonHeader
90
- };
91
- res.on("data", (chunk) => {
92
- updater.emit("downloading", chunk.length);
93
- data.push(chunk);
94
- });
95
- res.on("end", () => {
96
- updater.emit("downloadEnd", true);
97
- resolve2(Buffer.concat(data));
98
- });
99
- }
100
- }).on("error", (e) => {
101
- e && updater.emit("donwnloadError", e);
102
- reject(e);
103
- });
104
- });
116
+ const downloadFn = format === "json" ? downloadJSON ?? downloadJSONDefault : downloadBuffer ?? downloadBufferDefault;
117
+ return await downloadFn(url, updater, headers);
105
118
  }
106
119
  async function extractFile(gzipFilePath) {
107
120
  if (!gzipFilePath.endsWith(".asar.gz") || !existsSync(gzipFilePath)) {
package/dist/vite.d.ts CHANGED
@@ -8,19 +8,22 @@ type Options = {
8
8
  /**
9
9
  * the name of you application
10
10
  *
11
- * you can set as 'name' in package.json
11
+ * you can set as 'name' in `package.json`
12
12
  */
13
13
  productName: string;
14
14
  /**
15
15
  * the version of you application
16
16
  *
17
- * you can set as 'version' in package.json
17
+ * you can set as 'version' in `package.json`
18
18
  */
19
19
  version: string;
20
20
  /**
21
- * Whether to minify
21
+ * Whether to minify entry file
22
22
  */
23
23
  minify?: boolean;
24
+ /**
25
+ * paths config
26
+ */
24
27
  paths?: {
25
28
  /**
26
29
  * Path to app entry file
@@ -33,7 +36,7 @@ type Options = {
33
36
  */
34
37
  entryOutputPath?: string;
35
38
  /**
36
- * Path to app entry file
39
+ * Path to asar file
37
40
  * @default `release/${ProductName}.asar`
38
41
  */
39
42
  asarOutputPath?: string;
@@ -53,6 +56,9 @@ type Options = {
53
56
  */
54
57
  versionPath?: string;
55
58
  };
59
+ /**
60
+ * signature config
61
+ */
56
62
  keys?: {
57
63
  /**
58
64
  * Path to the pem file that contains private key
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electron-incremental-update",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "electron incremental update tools, powered by vite",
5
5
  "scripts": {
6
6
  "build": "tsup",