electron-incremental-update 2.0.0-beta.1 → 2.0.0-beta.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.
@@ -1,37 +1,63 @@
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';
1
+ import { d as defaultIsLowerVersion, U as UpdateJSON, a as UpdateInfo } from './version-CemSHimT.cjs';
2
+ import { I as IProvider, D as DownloadingInfo, U as URLHandler, O as OnDownloading } from './types-Tequ_V2o.cjs';
3
+ import { e as defaultVerify, d as defaultUnzipFile } from './unzip-JjYLjJkH.cjs';
4
4
  import '@subframe7536/type-utils';
5
5
 
6
+ declare abstract class BaseProvider implements IProvider {
7
+ name: string;
8
+ isLowerVersion: typeof defaultIsLowerVersion;
9
+ verifySignaure: typeof defaultVerify;
10
+ unzipFile: typeof defaultUnzipFile;
11
+ abstract downloadJSON(versionPath: string): Promise<UpdateJSON>;
12
+ abstract downloadAsar(name: string, info: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
13
+ }
14
+
6
15
  interface GitHubProviderOptions {
7
16
  /**
8
17
  * github repo root url
9
- * @example 'https://github.com/electron/electron'
18
+ * @example 'https://github.com/electron/electron/'
10
19
  */
11
20
  url: string;
21
+ /**
22
+ * extra headers
23
+ */
12
24
  extraHeaders?: Record<string, string>;
13
25
  /**
14
26
  * custom url handler
15
27
  *
16
- * for Github, there are some {@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L34 public CDN links}
28
+ * for Github, there are some {@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L40 public CDN links}
29
+ * @example
30
+ * (url, isDownloadAsar) => {
31
+ * if (isDownloadAsar) {
32
+ * url.hostname = 'mirror.ghproxy.com'
33
+ * url.pathname = 'https://github.com' + url.pathname
34
+ * return url
35
+ * }
36
+ * }
17
37
  */
18
38
  urlHandler?: URLHandler;
19
39
  }
20
- declare class GitHubProvider implements IProvider {
40
+ declare class GitHubProvider extends BaseProvider {
21
41
  private ua;
22
42
  name: string;
23
43
  urlHandler?: URLHandler;
24
44
  private url;
25
45
  private extraHeaders?;
46
+ /**
47
+ * Update Provider for Github repo
48
+ * - download update json from `https://raw.githubusercontent.com/{user}/{repo}/HEAD/{versionPath}`
49
+ * - download update asar from `https://github.com/{user}/{repo}/releases/download/v{version}/{name}-{version}.asar.gz`
50
+ *
51
+ * you can setup `urlHandler` in {@link GitHubProviderOptions} or `Updater` to modify url before request
52
+ * @param options provider options
53
+ */
26
54
  constructor(options: GitHubProviderOptions);
27
55
  private parseURL;
28
- isLowerVersion: typeof isLowerVersionDefault;
29
- verifySignaure: typeof verifySignatureDefault;
30
56
  downloadJSON(versionPath: string): Promise<UpdateJSON>;
31
- downloadBuffer(name: string, { version, size }: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
57
+ downloadAsar(name: string, { version, size }: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
32
58
  }
33
59
 
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>;
60
+ declare function defaultDownloadUpdateJSON(url: string, headers: Record<string, any>): Promise<UpdateJSON>;
61
+ declare function defaultDownloadAsar(url: string, headers: Record<string, any>, total: number, onDownloading?: OnDownloading): Promise<Buffer>;
36
62
 
37
- export { DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, URLHandler, downloadAsarBufferDefault, downloadUpdateJSONDefault };
63
+ export { BaseProvider, DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, URLHandler, defaultDownloadAsar, defaultDownloadUpdateJSON };
@@ -1,37 +1,63 @@
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';
1
+ import { d as defaultIsLowerVersion, U as UpdateJSON, a as UpdateInfo } from './version-CemSHimT.js';
2
+ import { I as IProvider, D as DownloadingInfo, U as URLHandler, O as OnDownloading } from './types-DxhNaNgR.js';
3
+ import { e as defaultVerify, d as defaultUnzipFile } from './unzip-JjYLjJkH.js';
4
4
  import '@subframe7536/type-utils';
5
5
 
6
+ declare abstract class BaseProvider implements IProvider {
7
+ name: string;
8
+ isLowerVersion: typeof defaultIsLowerVersion;
9
+ verifySignaure: typeof defaultVerify;
10
+ unzipFile: typeof defaultUnzipFile;
11
+ abstract downloadJSON(versionPath: string): Promise<UpdateJSON>;
12
+ abstract downloadAsar(name: string, info: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
13
+ }
14
+
6
15
  interface GitHubProviderOptions {
7
16
  /**
8
17
  * github repo root url
9
- * @example 'https://github.com/electron/electron'
18
+ * @example 'https://github.com/electron/electron/'
10
19
  */
11
20
  url: string;
21
+ /**
22
+ * extra headers
23
+ */
12
24
  extraHeaders?: Record<string, string>;
13
25
  /**
14
26
  * custom url handler
15
27
  *
16
- * for Github, there are some {@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L34 public CDN links}
28
+ * for Github, there are some {@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L40 public CDN links}
29
+ * @example
30
+ * (url, isDownloadAsar) => {
31
+ * if (isDownloadAsar) {
32
+ * url.hostname = 'mirror.ghproxy.com'
33
+ * url.pathname = 'https://github.com' + url.pathname
34
+ * return url
35
+ * }
36
+ * }
17
37
  */
18
38
  urlHandler?: URLHandler;
19
39
  }
20
- declare class GitHubProvider implements IProvider {
40
+ declare class GitHubProvider extends BaseProvider {
21
41
  private ua;
22
42
  name: string;
23
43
  urlHandler?: URLHandler;
24
44
  private url;
25
45
  private extraHeaders?;
46
+ /**
47
+ * Update Provider for Github repo
48
+ * - download update json from `https://raw.githubusercontent.com/{user}/{repo}/HEAD/{versionPath}`
49
+ * - download update asar from `https://github.com/{user}/{repo}/releases/download/v{version}/{name}-{version}.asar.gz`
50
+ *
51
+ * you can setup `urlHandler` in {@link GitHubProviderOptions} or `Updater` to modify url before request
52
+ * @param options provider options
53
+ */
26
54
  constructor(options: GitHubProviderOptions);
27
55
  private parseURL;
28
- isLowerVersion: typeof isLowerVersionDefault;
29
- verifySignaure: typeof verifySignatureDefault;
30
56
  downloadJSON(versionPath: string): Promise<UpdateJSON>;
31
- downloadBuffer(name: string, { version, size }: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
57
+ downloadAsar(name: string, { version, size }: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
32
58
  }
33
59
 
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>;
60
+ declare function defaultDownloadUpdateJSON(url: string, headers: Record<string, any>): Promise<UpdateJSON>;
61
+ declare function defaultDownloadAsar(url: string, headers: Record<string, any>, total: number, onDownloading?: OnDownloading): Promise<Buffer>;
36
62
 
37
- export { DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, URLHandler, downloadAsarBufferDefault, downloadUpdateJSONDefault };
63
+ export { BaseProvider, DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, URLHandler, defaultDownloadAsar, defaultDownloadUpdateJSON };
package/dist/provider.js CHANGED
@@ -1,14 +1,9 @@
1
- import {
2
- isLowerVersionDefault,
3
- isUpdateJSON,
4
- verifySignatureDefault,
5
- waitAppReady
6
- } from "./chunk-RSLOPAIZ.js";
1
+ import { defaultVerify, defaultUnzipFile } from './chunk-5WFXC5GU.js';
2
+ import { defaultIsLowerVersion, isUpdateJSON } from './chunk-BVFQWBLK.js';
3
+ import { app, net } from 'electron';
7
4
 
8
- // src/provider/download.ts
9
- import { net } from "electron";
10
- async function downlaodFn(url, headers, onResponse) {
11
- await waitAppReady();
5
+ async function downloadFn(url, headers, onResponse) {
6
+ await app.whenReady();
12
7
  return new Promise((resolve, reject) => {
13
8
  const request = net.request({ url, method: "GET", redirect: "follow" });
14
9
  Object.keys(headers).forEach((key) => request.setHeader(key, headers[key]));
@@ -21,8 +16,8 @@ async function downlaodFn(url, headers, onResponse) {
21
16
  request.end();
22
17
  });
23
18
  }
24
- async function downloadUpdateJSONDefault(url, headers) {
25
- return await downlaodFn(url, headers, (resp, resolve, reject) => {
19
+ async function defaultDownloadUpdateJSON(url, headers) {
20
+ return await downloadFn(url, headers, (resp, resolve, reject) => {
26
21
  let data = "";
27
22
  resp.on("data", (chunk) => data += chunk);
28
23
  resp.on("end", () => {
@@ -33,66 +28,89 @@ async function downloadUpdateJSONDefault(url, headers) {
33
28
  } else {
34
29
  throw Error;
35
30
  }
36
- } catch (ignore) {
31
+ } catch {
37
32
  reject(new Error("invalid update json"));
38
33
  }
39
34
  });
40
35
  });
41
36
  }
42
- async function downloadAsarBufferDefault(url, headers, total, onDownloading) {
43
- let current = 0;
44
- return await downlaodFn(url, headers, (resp, resolve) => {
37
+ async function defaultDownloadAsar(url, headers, total, onDownloading) {
38
+ let transferred = 0;
39
+ let time = Date.now();
40
+ return await downloadFn(url, headers, (resp, resolve) => {
45
41
  let data = [];
46
42
  resp.on("data", (chunk) => {
47
- current += chunk.length;
48
- onDownloading?.({ percent: `${+(current / total).toFixed(2) * 100}%`, total, current });
43
+ transferred += chunk.length;
44
+ const current = Date.now();
45
+ onDownloading?.({
46
+ percent: +(transferred / total).toFixed(2) * 100,
47
+ total,
48
+ transferred,
49
+ delta: chunk.length,
50
+ bps: chunk.length / ((current - time) * 1e3)
51
+ });
52
+ time = current;
49
53
  data.push(chunk);
50
54
  });
51
55
  resp.on("end", () => resolve(Buffer.concat(data)));
52
56
  });
53
57
  }
54
58
 
59
+ // src/provider/base.ts
60
+ var BaseProvider = class {
61
+ name = "BaseProvider";
62
+ isLowerVersion = defaultIsLowerVersion;
63
+ verifySignaure = defaultVerify;
64
+ unzipFile = defaultUnzipFile;
65
+ };
66
+
55
67
  // src/provider/github.ts
56
- var GitHubProvider = class {
68
+ var GitHubProvider = class extends BaseProvider {
57
69
  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
70
  name = "GithubProvider";
59
71
  urlHandler;
60
72
  url;
61
73
  extraHeaders;
74
+ /**
75
+ * Update Provider for Github repo
76
+ * - download update json from `https://raw.githubusercontent.com/{user}/{repo}/HEAD/{versionPath}`
77
+ * - download update asar from `https://github.com/{user}/{repo}/releases/download/v{version}/{name}-{version}.asar.gz`
78
+ *
79
+ * you can setup `urlHandler` in {@link GitHubProviderOptions} or `Updater` to modify url before request
80
+ * @param options provider options
81
+ */
62
82
  constructor(options) {
63
- this.url = new URL(options.url);
83
+ super();
64
84
  this.extraHeaders = options.extraHeaders;
65
85
  this.urlHandler = options.urlHandler;
66
- if (this.url.host !== "github.com") {
86
+ if (!options.url.startsWith("https://github.com")) {
67
87
  throw new Error(`${this.name}: invalid github url: ${options.url}`);
68
88
  }
69
- if (!this.url.pathname.endsWith("/")) {
70
- this.url.pathname += "/";
89
+ this.url = options.url;
90
+ if (!this.url.endsWith("/")) {
91
+ this.url += "/";
71
92
  }
72
93
  }
73
- parseURL(isDownloadAsar, path) {
74
- const url = this.url.href + path;
75
- return this.urlHandler ? this.urlHandler(url, isDownloadAsar) : url;
94
+ async parseURL(isDownloadAsar, extraPath) {
95
+ const _url = new URL(this.url);
96
+ _url.hostname = isDownloadAsar ? "github.com" : "raw.githubusercontent.com";
97
+ _url.pathname += extraPath;
98
+ return (await this.urlHandler?.(_url, isDownloadAsar) || _url).toString();
76
99
  }
77
- isLowerVersion = isLowerVersionDefault;
78
- verifySignaure = verifySignatureDefault;
79
100
  async downloadJSON(versionPath) {
80
- return await downloadUpdateJSONDefault(
81
- this.parseURL(false, `HEAD/${versionPath}`),
101
+ return await defaultDownloadUpdateJSON(
102
+ await this.parseURL(false, `HEAD/${versionPath}`),
82
103
  { userAgent: this.ua, accept: "application/json", ...this.extraHeaders }
83
104
  );
84
105
  }
85
- async downloadBuffer(name, { version, size }, onDownloading) {
86
- return await downloadAsarBufferDefault(
87
- this.parseURL(true, `releases/download/v${version}/${name}-${version}.asar.gz`),
106
+ async downloadAsar(name, { version, size }, onDownloading) {
107
+ return await defaultDownloadAsar(
108
+ await this.parseURL(true, `releases/download/v${version}/${name}-${version}.asar.gz`),
88
109
  { userAgent: this.ua, accept: "application/octet-stream", ...this.extraHeaders },
89
110
  size,
90
111
  onDownloading
91
112
  );
92
113
  }
93
114
  };
94
- export {
95
- GitHubProvider,
96
- downloadAsarBufferDefault,
97
- downloadUpdateJSONDefault
98
- };
115
+
116
+ export { BaseProvider, GitHubProvider, defaultDownloadAsar, defaultDownloadUpdateJSON };
@@ -1,13 +1,17 @@
1
1
  import { Promisable } from '@subframe7536/type-utils';
2
- import { U as UpdateJSON, a as UpdateInfo } from './version-CffZWDhZ.js';
2
+ import { U as UpdateJSON, a as UpdateInfo } from './version-CemSHimT.js';
3
3
 
4
- type URLHandler = (url: string, isDownloadAsar: boolean) => string;
4
+ type URLHandler = (url: URL, isDownloadAsar: boolean) => Promisable<URL | string | undefined | null>;
5
5
  type OnDownloading = (progress: DownloadingInfo) => void;
6
6
  interface DownloadingInfo {
7
7
  /**
8
- * downloaded percent, 0% - 100%
8
+ * download delta
9
9
  */
10
- percent: `${number}%`;
10
+ delta: number;
11
+ /**
12
+ * downloaded percent, 0 ~ 100
13
+ */
14
+ percent: number;
11
15
  /**
12
16
  * total size
13
17
  */
@@ -15,7 +19,11 @@ interface DownloadingInfo {
15
19
  /**
16
20
  * downloaded size
17
21
  */
18
- current: number;
22
+ transferred: number;
23
+ /**
24
+ * download speed, bytes per second
25
+ */
26
+ bps: number;
19
27
  }
20
28
  interface IProvider {
21
29
  /**
@@ -40,14 +48,20 @@ interface IProvider {
40
48
  * @param updateInfo existing update info
41
49
  * @param onDownloading hook for on downloading
42
50
  */
43
- downloadBuffer: (name: string, updateInfo: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void) => Promise<Buffer>;
51
+ downloadAsar: (name: string, updateInfo: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void) => Promise<Buffer>;
44
52
  /**
45
53
  * compare version
46
54
  * @param oldVer old version string
47
55
  * @param newVer new version string
48
56
  * @returns if version1 < version2
49
57
  */
50
- isLowerVersion: (oldVer: string, newVer: string) => Promisable<boolean>;
58
+ isLowerVersion: (oldVer: string, newVer: string) => boolean;
59
+ /**
60
+ * unzip file
61
+ * @param buffer source buffer
62
+ * @param targetFilePath target file path
63
+ */
64
+ unzipFile: (buffer: Buffer, targetFilePath: string) => Promise<void>;
51
65
  /**
52
66
  * verify asar signature
53
67
  * @param buffer file buffer
@@ -1,13 +1,17 @@
1
1
  import { Promisable } from '@subframe7536/type-utils';
2
- import { U as UpdateJSON, a as UpdateInfo } from './version-CffZWDhZ.cjs';
2
+ import { U as UpdateJSON, a as UpdateInfo } from './version-CemSHimT.cjs';
3
3
 
4
- type URLHandler = (url: string, isDownloadAsar: boolean) => string;
4
+ type URLHandler = (url: URL, isDownloadAsar: boolean) => Promisable<URL | string | undefined | null>;
5
5
  type OnDownloading = (progress: DownloadingInfo) => void;
6
6
  interface DownloadingInfo {
7
7
  /**
8
- * downloaded percent, 0% - 100%
8
+ * download delta
9
9
  */
10
- percent: `${number}%`;
10
+ delta: number;
11
+ /**
12
+ * downloaded percent, 0 ~ 100
13
+ */
14
+ percent: number;
11
15
  /**
12
16
  * total size
13
17
  */
@@ -15,7 +19,11 @@ interface DownloadingInfo {
15
19
  /**
16
20
  * downloaded size
17
21
  */
18
- current: number;
22
+ transferred: number;
23
+ /**
24
+ * download speed, bytes per second
25
+ */
26
+ bps: number;
19
27
  }
20
28
  interface IProvider {
21
29
  /**
@@ -40,14 +48,20 @@ interface IProvider {
40
48
  * @param updateInfo existing update info
41
49
  * @param onDownloading hook for on downloading
42
50
  */
43
- downloadBuffer: (name: string, updateInfo: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void) => Promise<Buffer>;
51
+ downloadAsar: (name: string, updateInfo: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void) => Promise<Buffer>;
44
52
  /**
45
53
  * compare version
46
54
  * @param oldVer old version string
47
55
  * @param newVer new version string
48
56
  * @returns if version1 < version2
49
57
  */
50
- isLowerVersion: (oldVer: string, newVer: string) => Promisable<boolean>;
58
+ isLowerVersion: (oldVer: string, newVer: string) => boolean;
59
+ /**
60
+ * unzip file
61
+ * @param buffer source buffer
62
+ * @param targetFilePath target file path
63
+ */
64
+ unzipFile: (buffer: Buffer, targetFilePath: string) => Promise<void>;
51
65
  /**
52
66
  * verify asar signature
53
67
  * @param buffer file buffer
@@ -0,0 +1,9 @@
1
+ declare function hashBuffer(data: string | Buffer, length: number): Buffer;
2
+ declare function aesEncrypt(plainText: string, key: Buffer, iv: Buffer): string;
3
+ declare function defaultSignature(buffer: Buffer, privateKey: string, cert: string, version: string): string;
4
+ declare function aesDecrypt(encryptedText: string, key: Buffer, iv: Buffer): string;
5
+ declare function defaultVerify(buffer: Buffer, signature: string, cert: string): string | undefined;
6
+
7
+ declare function defaultUnzipFile(buffer: Buffer, targetFilePath: string): Promise<void>;
8
+
9
+ export { aesEncrypt as a, defaultSignature as b, aesDecrypt as c, defaultUnzipFile as d, defaultVerify as e, hashBuffer as h };
@@ -0,0 +1,9 @@
1
+ declare function hashBuffer(data: string | Buffer, length: number): Buffer;
2
+ declare function aesEncrypt(plainText: string, key: Buffer, iv: Buffer): string;
3
+ declare function defaultSignature(buffer: Buffer, privateKey: string, cert: string, version: string): string;
4
+ declare function aesDecrypt(encryptedText: string, key: Buffer, iv: Buffer): string;
5
+ declare function defaultVerify(buffer: Buffer, signature: string, cert: string): string | undefined;
6
+
7
+ declare function defaultUnzipFile(buffer: Buffer, targetFilePath: string): Promise<void>;
8
+
9
+ export { aesEncrypt as a, defaultSignature as b, aesDecrypt as c, defaultUnzipFile as d, defaultVerify as e, hashBuffer as h };