electron-incremental-update 1.2.0 → 2.0.0-beta.1

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.
@@ -0,0 +1,227 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/provider/index.ts
21
+ var provider_exports = {};
22
+ __export(provider_exports, {
23
+ GitHubProvider: () => GitHubProvider,
24
+ downloadAsarBufferDefault: () => downloadAsarBufferDefault,
25
+ downloadUpdateJSONDefault: () => downloadUpdateJSONDefault
26
+ });
27
+ module.exports = __toCommonJS(provider_exports);
28
+
29
+ // src/utils/electron.ts
30
+ var import_node_fs = require("fs");
31
+ var import_node_path = require("path");
32
+ var import_electron = require("electron");
33
+ var isDev = __EIU_IS_DEV__;
34
+ var isWin = process.platform === "win32";
35
+ var isMac = process.platform === "darwin";
36
+ var isLinux = process.platform === "linux";
37
+ function waitAppReady(timeout = 1e3) {
38
+ return import_electron.app.isReady() ? Promise.resolve() : new Promise((resolve, reject) => {
39
+ const _ = setTimeout(() => {
40
+ reject(new Error("app is not ready"));
41
+ }, timeout);
42
+ import_electron.app.whenReady().then(() => {
43
+ clearTimeout(_);
44
+ resolve();
45
+ });
46
+ });
47
+ }
48
+
49
+ // src/utils/zip.ts
50
+ var import_node_fs2 = require("fs");
51
+ var import_node_zlib = require("zlib");
52
+
53
+ // src/utils/unzip.ts
54
+ var import_node_fs3 = require("fs");
55
+ var import_node_zlib2 = require("zlib");
56
+
57
+ // src/utils/version.ts
58
+ function parseVersion(version) {
59
+ const match = /^(\d+)\.(\d+)\.(\d+)(?:-([a-z0-9.-]+))?/i.exec(version);
60
+ if (!match) {
61
+ throw new TypeError(`invalid version: ${version}`);
62
+ }
63
+ const [major, minor, patch] = match.slice(1, 4).map(Number);
64
+ const ret = {
65
+ major,
66
+ minor,
67
+ patch,
68
+ stage: "",
69
+ stageVersion: -1
70
+ };
71
+ if (match[4]) {
72
+ let [stage, _v] = match[4].split(".");
73
+ ret.stage = stage;
74
+ ret.stageVersion = Number(_v) || -1;
75
+ }
76
+ if (Number.isNaN(major) || Number.isNaN(minor) || Number.isNaN(patch) || Number.isNaN(ret.stageVersion)) {
77
+ throw new TypeError(`invalid version: ${version}`);
78
+ }
79
+ return ret;
80
+ }
81
+ function isLowerVersionDefault(oldVer, newVer) {
82
+ const oldV = parseVersion(oldVer);
83
+ const newV = parseVersion(newVer);
84
+ function compareStrings(str1, str2) {
85
+ if (str1 === "") {
86
+ return str2 !== "";
87
+ } else if (str2 === "") {
88
+ return true;
89
+ }
90
+ return str1 < str2;
91
+ }
92
+ for (let key of Object.keys(oldV)) {
93
+ if (key === "stage" && compareStrings(oldV[key], newV[key])) {
94
+ return true;
95
+ } else if (oldV[key] !== newV[key]) {
96
+ return oldV[key] < newV[key];
97
+ }
98
+ }
99
+ return false;
100
+ }
101
+ function isUpdateJSON(json) {
102
+ const is = (j) => !!(j && j.minimumVersion && j.signature && j.size && j.version);
103
+ return is(json) && is(json?.beta);
104
+ }
105
+
106
+ // src/utils/crypto/decrypt.ts
107
+ var import_node_crypto2 = require("crypto");
108
+
109
+ // src/utils/crypto/utils.ts
110
+ var import_node_crypto = require("crypto");
111
+ function hashString(data, length) {
112
+ const hash = (0, import_node_crypto.createHash)("SHA256").update(data).digest("binary");
113
+ return Buffer.from(hash).subarray(0, length);
114
+ }
115
+
116
+ // src/utils/crypto/decrypt.ts
117
+ function decrypt(encryptedText, key, iv) {
118
+ const decipher = (0, import_node_crypto2.createDecipheriv)("aes-256-cbc", key, iv);
119
+ let decrypted = decipher.update(encryptedText, "base64url", "utf8");
120
+ decrypted += decipher.final("utf8");
121
+ return decrypted;
122
+ }
123
+ function verifySignatureDefault(buffer, signature, cert) {
124
+ try {
125
+ const [sig, version] = decrypt(signature, hashString(cert, 32), hashString(buffer, 16)).split("%");
126
+ const result = (0, import_node_crypto2.createVerify)("RSA-SHA256").update(buffer).verify(cert, sig, "base64");
127
+ return result ? version : void 0;
128
+ } catch (error) {
129
+ return void 0;
130
+ }
131
+ }
132
+
133
+ // src/utils/crypto/encrypt.ts
134
+ var import_node_crypto3 = require("crypto");
135
+
136
+ // src/provider/download.ts
137
+ var import_electron2 = require("electron");
138
+ async function downlaodFn(url, headers, onResponse) {
139
+ await waitAppReady();
140
+ return new Promise((resolve, reject) => {
141
+ const request = import_electron2.net.request({ url, method: "GET", redirect: "follow" });
142
+ Object.keys(headers).forEach((key) => request.setHeader(key, headers[key]));
143
+ request.on("response", (resp) => {
144
+ resp.on("aborted", () => reject(new Error("aborted")));
145
+ resp.on("error", () => reject(new Error("download error")));
146
+ onResponse(resp, resolve, reject);
147
+ });
148
+ request.on("error", reject);
149
+ request.end();
150
+ });
151
+ }
152
+ async function downloadUpdateJSONDefault(url, headers) {
153
+ return await downlaodFn(url, headers, (resp, resolve, reject) => {
154
+ let data = "";
155
+ resp.on("data", (chunk) => data += chunk);
156
+ resp.on("end", () => {
157
+ try {
158
+ const json = JSON.parse(data);
159
+ if (isUpdateJSON(json)) {
160
+ resolve(json);
161
+ } else {
162
+ throw Error;
163
+ }
164
+ } catch (ignore) {
165
+ reject(new Error("invalid update json"));
166
+ }
167
+ });
168
+ });
169
+ }
170
+ async function downloadAsarBufferDefault(url, headers, total, onDownloading) {
171
+ let current = 0;
172
+ return await downlaodFn(url, headers, (resp, resolve) => {
173
+ let data = [];
174
+ resp.on("data", (chunk) => {
175
+ current += chunk.length;
176
+ onDownloading?.({ percent: `${+(current / total).toFixed(2) * 100}%`, total, current });
177
+ data.push(chunk);
178
+ });
179
+ resp.on("end", () => resolve(Buffer.concat(data)));
180
+ });
181
+ }
182
+
183
+ // src/provider/github.ts
184
+ var GitHubProvider = class {
185
+ ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36";
186
+ name = "GithubProvider";
187
+ urlHandler;
188
+ url;
189
+ extraHeaders;
190
+ constructor(options) {
191
+ this.url = new URL(options.url);
192
+ this.extraHeaders = options.extraHeaders;
193
+ this.urlHandler = options.urlHandler;
194
+ if (this.url.host !== "github.com") {
195
+ throw new Error(`${this.name}: invalid github url: ${options.url}`);
196
+ }
197
+ if (!this.url.pathname.endsWith("/")) {
198
+ this.url.pathname += "/";
199
+ }
200
+ }
201
+ parseURL(isDownloadAsar, path) {
202
+ const url = this.url.href + path;
203
+ return this.urlHandler ? this.urlHandler(url, isDownloadAsar) : url;
204
+ }
205
+ isLowerVersion = isLowerVersionDefault;
206
+ verifySignaure = verifySignatureDefault;
207
+ async downloadJSON(versionPath) {
208
+ return await downloadUpdateJSONDefault(
209
+ this.parseURL(false, `HEAD/${versionPath}`),
210
+ { userAgent: this.ua, accept: "application/json", ...this.extraHeaders }
211
+ );
212
+ }
213
+ async downloadBuffer(name, { version, size }, onDownloading) {
214
+ return await downloadAsarBufferDefault(
215
+ this.parseURL(true, `releases/download/v${version}/${name}-${version}.asar.gz`),
216
+ { userAgent: this.ua, accept: "application/octet-stream", ...this.extraHeaders },
217
+ size,
218
+ onDownloading
219
+ );
220
+ }
221
+ };
222
+ // Annotate the CommonJS export names for ESM import in node:
223
+ 0 && (module.exports = {
224
+ GitHubProvider,
225
+ downloadAsarBufferDefault,
226
+ downloadUpdateJSONDefault
227
+ });
@@ -0,0 +1,37 @@
1
+ import { i as isLowerVersionDefault, U as UpdateJSON, a as UpdateInfo } from './version-CffZWDhZ.cjs';
2
+ import { v as verifySignatureDefault } from './decrypt-BNBcodiO.cjs';
3
+ import { U as URLHandler, I as IProvider, D as DownloadingInfo, O as OnDownloading } from './types-DxPmQmaq.cjs';
4
+ import '@subframe7536/type-utils';
5
+
6
+ interface GitHubProviderOptions {
7
+ /**
8
+ * github repo root url
9
+ * @example 'https://github.com/electron/electron'
10
+ */
11
+ url: string;
12
+ extraHeaders?: Record<string, string>;
13
+ /**
14
+ * custom url handler
15
+ *
16
+ * for Github, there are some {@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L34 public CDN links}
17
+ */
18
+ urlHandler?: URLHandler;
19
+ }
20
+ declare class GitHubProvider implements IProvider {
21
+ private ua;
22
+ name: string;
23
+ urlHandler?: URLHandler;
24
+ private url;
25
+ private extraHeaders?;
26
+ constructor(options: GitHubProviderOptions);
27
+ private parseURL;
28
+ isLowerVersion: typeof isLowerVersionDefault;
29
+ verifySignaure: typeof verifySignatureDefault;
30
+ downloadJSON(versionPath: string): Promise<UpdateJSON>;
31
+ downloadBuffer(name: string, { version, size }: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
32
+ }
33
+
34
+ declare function downloadUpdateJSONDefault(url: string, headers: Record<string, any>): Promise<UpdateJSON>;
35
+ declare function downloadAsarBufferDefault(url: string, headers: Record<string, any>, total: number, onDownloading?: OnDownloading): Promise<Buffer>;
36
+
37
+ export { DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, URLHandler, downloadAsarBufferDefault, downloadUpdateJSONDefault };
@@ -0,0 +1,37 @@
1
+ import { i as isLowerVersionDefault, U as UpdateJSON, a as UpdateInfo } from './version-CffZWDhZ.js';
2
+ import { v as verifySignatureDefault } from './decrypt-BNBcodiO.js';
3
+ import { U as URLHandler, I as IProvider, D as DownloadingInfo, O as OnDownloading } from './types-seJf3Wbc.js';
4
+ import '@subframe7536/type-utils';
5
+
6
+ interface GitHubProviderOptions {
7
+ /**
8
+ * github repo root url
9
+ * @example 'https://github.com/electron/electron'
10
+ */
11
+ url: string;
12
+ extraHeaders?: Record<string, string>;
13
+ /**
14
+ * custom url handler
15
+ *
16
+ * for Github, there are some {@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L34 public CDN links}
17
+ */
18
+ urlHandler?: URLHandler;
19
+ }
20
+ declare class GitHubProvider implements IProvider {
21
+ private ua;
22
+ name: string;
23
+ urlHandler?: URLHandler;
24
+ private url;
25
+ private extraHeaders?;
26
+ constructor(options: GitHubProviderOptions);
27
+ private parseURL;
28
+ isLowerVersion: typeof isLowerVersionDefault;
29
+ verifySignaure: typeof verifySignatureDefault;
30
+ downloadJSON(versionPath: string): Promise<UpdateJSON>;
31
+ downloadBuffer(name: string, { version, size }: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
32
+ }
33
+
34
+ declare function downloadUpdateJSONDefault(url: string, headers: Record<string, any>): Promise<UpdateJSON>;
35
+ declare function downloadAsarBufferDefault(url: string, headers: Record<string, any>, total: number, onDownloading?: OnDownloading): Promise<Buffer>;
36
+
37
+ export { DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, URLHandler, downloadAsarBufferDefault, downloadUpdateJSONDefault };
@@ -0,0 +1,98 @@
1
+ import {
2
+ isLowerVersionDefault,
3
+ isUpdateJSON,
4
+ verifySignatureDefault,
5
+ waitAppReady
6
+ } from "./chunk-RSLOPAIZ.js";
7
+
8
+ // src/provider/download.ts
9
+ import { net } from "electron";
10
+ async function downlaodFn(url, headers, onResponse) {
11
+ await waitAppReady();
12
+ return new Promise((resolve, reject) => {
13
+ const request = net.request({ url, method: "GET", redirect: "follow" });
14
+ Object.keys(headers).forEach((key) => request.setHeader(key, headers[key]));
15
+ request.on("response", (resp) => {
16
+ resp.on("aborted", () => reject(new Error("aborted")));
17
+ resp.on("error", () => reject(new Error("download error")));
18
+ onResponse(resp, resolve, reject);
19
+ });
20
+ request.on("error", reject);
21
+ request.end();
22
+ });
23
+ }
24
+ async function downloadUpdateJSONDefault(url, headers) {
25
+ return await downlaodFn(url, headers, (resp, resolve, reject) => {
26
+ let data = "";
27
+ resp.on("data", (chunk) => data += chunk);
28
+ resp.on("end", () => {
29
+ try {
30
+ const json = JSON.parse(data);
31
+ if (isUpdateJSON(json)) {
32
+ resolve(json);
33
+ } else {
34
+ throw Error;
35
+ }
36
+ } catch (ignore) {
37
+ reject(new Error("invalid update json"));
38
+ }
39
+ });
40
+ });
41
+ }
42
+ async function downloadAsarBufferDefault(url, headers, total, onDownloading) {
43
+ let current = 0;
44
+ return await downlaodFn(url, headers, (resp, resolve) => {
45
+ let data = [];
46
+ resp.on("data", (chunk) => {
47
+ current += chunk.length;
48
+ onDownloading?.({ percent: `${+(current / total).toFixed(2) * 100}%`, total, current });
49
+ data.push(chunk);
50
+ });
51
+ resp.on("end", () => resolve(Buffer.concat(data)));
52
+ });
53
+ }
54
+
55
+ // src/provider/github.ts
56
+ var GitHubProvider = class {
57
+ ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36";
58
+ name = "GithubProvider";
59
+ urlHandler;
60
+ url;
61
+ extraHeaders;
62
+ constructor(options) {
63
+ this.url = new URL(options.url);
64
+ this.extraHeaders = options.extraHeaders;
65
+ this.urlHandler = options.urlHandler;
66
+ if (this.url.host !== "github.com") {
67
+ throw new Error(`${this.name}: invalid github url: ${options.url}`);
68
+ }
69
+ if (!this.url.pathname.endsWith("/")) {
70
+ this.url.pathname += "/";
71
+ }
72
+ }
73
+ parseURL(isDownloadAsar, path) {
74
+ const url = this.url.href + path;
75
+ return this.urlHandler ? this.urlHandler(url, isDownloadAsar) : url;
76
+ }
77
+ isLowerVersion = isLowerVersionDefault;
78
+ verifySignaure = verifySignatureDefault;
79
+ async downloadJSON(versionPath) {
80
+ return await downloadUpdateJSONDefault(
81
+ this.parseURL(false, `HEAD/${versionPath}`),
82
+ { userAgent: this.ua, accept: "application/json", ...this.extraHeaders }
83
+ );
84
+ }
85
+ async downloadBuffer(name, { version, size }, onDownloading) {
86
+ return await downloadAsarBufferDefault(
87
+ this.parseURL(true, `releases/download/v${version}/${name}-${version}.asar.gz`),
88
+ { userAgent: this.ua, accept: "application/octet-stream", ...this.extraHeaders },
89
+ size,
90
+ onDownloading
91
+ );
92
+ }
93
+ };
94
+ export {
95
+ GitHubProvider,
96
+ downloadAsarBufferDefault,
97
+ downloadUpdateJSONDefault
98
+ };
@@ -0,0 +1,61 @@
1
+ import { Promisable } from '@subframe7536/type-utils';
2
+ import { U as UpdateJSON, a as UpdateInfo } from './version-CffZWDhZ.cjs';
3
+
4
+ type URLHandler = (url: string, isDownloadAsar: boolean) => string;
5
+ type OnDownloading = (progress: DownloadingInfo) => void;
6
+ interface DownloadingInfo {
7
+ /**
8
+ * downloaded percent, 0% - 100%
9
+ */
10
+ percent: `${number}%`;
11
+ /**
12
+ * total size
13
+ */
14
+ total: number;
15
+ /**
16
+ * downloaded size
17
+ */
18
+ current: number;
19
+ }
20
+ interface IProvider {
21
+ /**
22
+ * provider name
23
+ */
24
+ name: string;
25
+ /**
26
+ * custom url handler
27
+ *
28
+ * for Github, there are some {@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L34 public CDN links}
29
+ */
30
+ urlHandler?: URLHandler;
31
+ onDownloading?: OnDownloading;
32
+ /**
33
+ * download update json
34
+ * @param versionPath parsed version path
35
+ */
36
+ downloadJSON: (versionPath: string) => Promise<UpdateJSON>;
37
+ /**
38
+ * download update asar
39
+ * @param name app name
40
+ * @param updateInfo existing update info
41
+ * @param onDownloading hook for on downloading
42
+ */
43
+ downloadBuffer: (name: string, updateInfo: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void) => Promise<Buffer>;
44
+ /**
45
+ * compare version
46
+ * @param oldVer old version string
47
+ * @param newVer new version string
48
+ * @returns if version1 < version2
49
+ */
50
+ isLowerVersion: (oldVer: string, newVer: string) => Promisable<boolean>;
51
+ /**
52
+ * verify asar signature
53
+ * @param buffer file buffer
54
+ * @param signature signature
55
+ * @param cert certificate
56
+ * @returns if signature is valid, returns the version, otherwise returns `undefined`
57
+ */
58
+ verifySignaure: (buffer: Buffer, signature: string, cert: string) => Promisable<string | undefined>;
59
+ }
60
+
61
+ export type { DownloadingInfo as D, IProvider as I, OnDownloading as O, URLHandler as U };
@@ -0,0 +1,61 @@
1
+ import { Promisable } from '@subframe7536/type-utils';
2
+ import { U as UpdateJSON, a as UpdateInfo } from './version-CffZWDhZ.js';
3
+
4
+ type URLHandler = (url: string, isDownloadAsar: boolean) => string;
5
+ type OnDownloading = (progress: DownloadingInfo) => void;
6
+ interface DownloadingInfo {
7
+ /**
8
+ * downloaded percent, 0% - 100%
9
+ */
10
+ percent: `${number}%`;
11
+ /**
12
+ * total size
13
+ */
14
+ total: number;
15
+ /**
16
+ * downloaded size
17
+ */
18
+ current: number;
19
+ }
20
+ interface IProvider {
21
+ /**
22
+ * provider name
23
+ */
24
+ name: string;
25
+ /**
26
+ * custom url handler
27
+ *
28
+ * for Github, there are some {@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L34 public CDN links}
29
+ */
30
+ urlHandler?: URLHandler;
31
+ onDownloading?: OnDownloading;
32
+ /**
33
+ * download update json
34
+ * @param versionPath parsed version path
35
+ */
36
+ downloadJSON: (versionPath: string) => Promise<UpdateJSON>;
37
+ /**
38
+ * download update asar
39
+ * @param name app name
40
+ * @param updateInfo existing update info
41
+ * @param onDownloading hook for on downloading
42
+ */
43
+ downloadBuffer: (name: string, updateInfo: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void) => Promise<Buffer>;
44
+ /**
45
+ * compare version
46
+ * @param oldVer old version string
47
+ * @param newVer new version string
48
+ * @returns if version1 < version2
49
+ */
50
+ isLowerVersion: (oldVer: string, newVer: string) => Promisable<boolean>;
51
+ /**
52
+ * verify asar signature
53
+ * @param buffer file buffer
54
+ * @param signature signature
55
+ * @param cert certificate
56
+ * @returns if signature is valid, returns the version, otherwise returns `undefined`
57
+ */
58
+ verifySignaure: (buffer: Buffer, signature: string, cert: string) => Promisable<string | undefined>;
59
+ }
60
+
61
+ export type { DownloadingInfo as D, IProvider as I, OnDownloading as O, URLHandler as U };