electron-incremental-update 2.0.0-beta.9 → 2.0.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.
- package/README.md +307 -198
- package/dist/chunk-IABBXJFB.js +87 -0
- package/dist/{chunk-72ZAJ7AF.js → chunk-RCRKUKFX.js} +1 -1
- package/dist/index.cjs +91 -71
- package/dist/index.d.cts +64 -80
- package/dist/index.d.ts +64 -80
- package/dist/index.js +88 -70
- package/dist/provider.cjs +72 -52
- package/dist/provider.d.cts +47 -30
- package/dist/provider.d.ts +47 -30
- package/dist/provider.js +72 -53
- package/dist/{types-D7OK98ln.d.ts → types-BLdN9rkY.d.ts} +20 -27
- package/dist/{types-mEfMjnlV.d.cts → types-DkCn03M3.d.cts} +20 -27
- package/dist/utils.cjs +25 -17
- package/dist/utils.d.cts +37 -16
- package/dist/utils.d.ts +37 -16
- package/dist/utils.js +2 -2
- package/dist/version-BYVQ367i.d.cts +62 -0
- package/dist/version-BYVQ367i.d.ts +62 -0
- package/dist/vite.d.ts +58 -94
- package/dist/vite.js +57 -39
- package/dist/{zip-DPF5IFkK.d.ts → zip-rm9ED9nU.d.cts} +23 -0
- package/dist/{zip-DPF5IFkK.d.cts → zip-rm9ED9nU.d.ts} +23 -0
- package/package.json +10 -7
- package/dist/chunk-4MH6ZXCY.js +0 -81
- package/dist/version-DgfjJQUx.d.cts +0 -27
- package/dist/version-DgfjJQUx.d.ts +0 -27
package/dist/provider.cjs
CHANGED
|
@@ -7,6 +7,7 @@ var zlib = require('zlib');
|
|
|
7
7
|
|
|
8
8
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
9
|
|
|
10
|
+
var electron__default = /*#__PURE__*/_interopDefault(electron);
|
|
10
11
|
var crypto__default = /*#__PURE__*/_interopDefault(crypto);
|
|
11
12
|
var zlib__default = /*#__PURE__*/_interopDefault(zlib);
|
|
12
13
|
|
|
@@ -32,7 +33,7 @@ function parseVersion(version) {
|
|
|
32
33
|
ret.stageVersion = Number(_v) || -1;
|
|
33
34
|
}
|
|
34
35
|
if (Number.isNaN(major) || Number.isNaN(minor) || Number.isNaN(patch) || Number.isNaN(ret.stageVersion)) {
|
|
35
|
-
throw new TypeError(`
|
|
36
|
+
throw new TypeError(`Invalid version: ${version}`);
|
|
36
37
|
}
|
|
37
38
|
return ret;
|
|
38
39
|
}
|
|
@@ -62,18 +63,19 @@ function isUpdateJSON(json) {
|
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
// src/provider/download.ts
|
|
65
|
-
function getHeader(
|
|
66
|
-
const value =
|
|
66
|
+
function getHeader(headers, key) {
|
|
67
|
+
const value = headers[key];
|
|
67
68
|
if (Array.isArray(value)) {
|
|
68
69
|
return value.length === 0 ? null : value[value.length - 1];
|
|
69
70
|
} else {
|
|
70
71
|
return value;
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
|
-
async function downloadFn(url, headers, onResponse) {
|
|
74
|
-
await
|
|
74
|
+
async function downloadFn(url, headers, signal, onResponse) {
|
|
75
|
+
await electron__default.default.app.whenReady();
|
|
75
76
|
return new Promise((resolve, reject) => {
|
|
76
|
-
const request =
|
|
77
|
+
const request = electron__default.default.net.request({ url, method: "GET", redirect: "follow", headers, cache: "no-cache" });
|
|
78
|
+
signal.addEventListener("abort", () => request.abort(), { once: true });
|
|
77
79
|
request.on("response", (resp) => {
|
|
78
80
|
resp.on("aborted", () => reject(new Error("aborted")));
|
|
79
81
|
resp.on("error", () => reject(new Error("download error")));
|
|
@@ -83,46 +85,56 @@ async function downloadFn(url, headers, onResponse) {
|
|
|
83
85
|
request.end();
|
|
84
86
|
});
|
|
85
87
|
}
|
|
86
|
-
async function defaultDownloadUpdateJSON(url, headers) {
|
|
87
|
-
return await downloadFn(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
88
|
+
async function defaultDownloadUpdateJSON(url, headers, signal) {
|
|
89
|
+
return await downloadFn(
|
|
90
|
+
url,
|
|
91
|
+
headers,
|
|
92
|
+
signal,
|
|
93
|
+
(resp, resolve, reject) => {
|
|
94
|
+
let data = "";
|
|
95
|
+
resp.on("data", (chunk) => data += chunk);
|
|
96
|
+
resp.on("end", () => {
|
|
97
|
+
try {
|
|
98
|
+
const json = JSON.parse(data);
|
|
99
|
+
if (isUpdateJSON(json)) {
|
|
100
|
+
resolve(json);
|
|
101
|
+
} else {
|
|
102
|
+
throw Error;
|
|
103
|
+
}
|
|
104
|
+
} catch {
|
|
105
|
+
reject(new Error(`Invalid update json, "${data}"`));
|
|
97
106
|
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
});
|
|
102
|
-
});
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
);
|
|
103
110
|
}
|
|
104
|
-
async function defaultDownloadAsar(url, headers, onDownloading) {
|
|
111
|
+
async function defaultDownloadAsar(url, headers, signal, onDownloading) {
|
|
105
112
|
let transferred = 0;
|
|
106
113
|
let time = Date.now();
|
|
107
|
-
return await downloadFn(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
114
|
+
return await downloadFn(
|
|
115
|
+
url,
|
|
116
|
+
headers,
|
|
117
|
+
signal,
|
|
118
|
+
(resp, resolve) => {
|
|
119
|
+
const total = +getHeader(resp.headers, "content-length") || -1;
|
|
120
|
+
const data = [];
|
|
121
|
+
resp.on("data", (chunk) => {
|
|
122
|
+
const delta = chunk.length;
|
|
123
|
+
transferred += delta;
|
|
124
|
+
const current = Date.now();
|
|
125
|
+
onDownloading?.({
|
|
126
|
+
percent: total ? +(transferred / total).toFixed(2) * 100 : -1,
|
|
127
|
+
total,
|
|
128
|
+
transferred,
|
|
129
|
+
delta,
|
|
130
|
+
bps: delta / (current - time)
|
|
131
|
+
});
|
|
132
|
+
time = current;
|
|
133
|
+
data.push(chunk);
|
|
120
134
|
});
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
resp.on("end", () => resolve(Buffer.concat(data)));
|
|
125
|
-
});
|
|
135
|
+
resp.on("end", () => resolve(Buffer.concat(data)));
|
|
136
|
+
}
|
|
137
|
+
);
|
|
126
138
|
}
|
|
127
139
|
function hashBuffer(data, length) {
|
|
128
140
|
const hash = crypto__default.default.createHash("SHA256").update(data).digest("binary");
|
|
@@ -178,10 +190,10 @@ var GitHubProvider = class extends BaseProvider {
|
|
|
178
190
|
options;
|
|
179
191
|
/**
|
|
180
192
|
* Update Provider for Github repo
|
|
181
|
-
* - download update json from `https://
|
|
193
|
+
* - download update json from `https://github.com/{user}/{repo}/raw/HEAD/{versionPath}`
|
|
182
194
|
* - download update asar from `https://github.com/{user}/{repo}/releases/download/v{version}/{name}-{version}.asar.gz`
|
|
183
195
|
*
|
|
184
|
-
* you can setup `urlHandler` in {@link GitHubProviderOptions}
|
|
196
|
+
* you can setup `urlHandler` in {@link GitHubProviderOptions} to modify url before request
|
|
185
197
|
* @param options provider options
|
|
186
198
|
*/
|
|
187
199
|
constructor(options) {
|
|
@@ -197,23 +209,31 @@ var GitHubProvider = class extends BaseProvider {
|
|
|
197
209
|
set urlHandler(handler) {
|
|
198
210
|
this.options.urlHandler = handler;
|
|
199
211
|
}
|
|
200
|
-
async parseURL(
|
|
212
|
+
async parseURL(extraPath) {
|
|
201
213
|
const url$1 = new url.URL(
|
|
202
|
-
`/${this.options.
|
|
203
|
-
"https://
|
|
214
|
+
`/${this.options.user}/${this.options.repo}/${extraPath}`,
|
|
215
|
+
"https://github.com"
|
|
204
216
|
);
|
|
205
|
-
return (await this.urlHandler?.(url$1
|
|
217
|
+
return (await this.urlHandler?.(url$1) || url$1).toString();
|
|
206
218
|
}
|
|
207
|
-
|
|
219
|
+
/**
|
|
220
|
+
* @inheritdoc
|
|
221
|
+
*/
|
|
222
|
+
async downloadJSON(versionPath, signal) {
|
|
208
223
|
return await defaultDownloadUpdateJSON(
|
|
209
|
-
await this.parseURL(
|
|
210
|
-
{ Accept: "application/json", ...this.options.extraHeaders }
|
|
224
|
+
await this.parseURL(`raw/${this.options.branch}/${versionPath}`),
|
|
225
|
+
{ Accept: "application/json", ...this.options.extraHeaders },
|
|
226
|
+
signal
|
|
211
227
|
);
|
|
212
228
|
}
|
|
213
|
-
|
|
229
|
+
/**
|
|
230
|
+
* @inheritdoc
|
|
231
|
+
*/
|
|
232
|
+
async downloadAsar(name, info, signal, onDownloading) {
|
|
214
233
|
return await defaultDownloadAsar(
|
|
215
|
-
await this.parseURL(
|
|
234
|
+
await this.parseURL(`releases/download/v${info.version}/${name}-${info.version}.asar.gz`),
|
|
216
235
|
{ Accept: "application/octet-stream", ...this.options.extraHeaders },
|
|
236
|
+
signal,
|
|
217
237
|
onDownloading
|
|
218
238
|
);
|
|
219
239
|
}
|
package/dist/provider.d.cts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { URL } from 'node:url';
|
|
2
|
+
import { Promisable, Arrayable } from '@subframe7536/type-utils';
|
|
3
|
+
import { d as defaultIsLowerVersion, a as UpdateJSON, U as UpdateInfo } from './version-BYVQ367i.cjs';
|
|
4
|
+
import { I as IProvider, D as DownloadingInfo, O as OnDownloading } from './types-DkCn03M3.cjs';
|
|
5
|
+
import { f as defaultVerifySignature, a as defaultUnzipFile } from './zip-rm9ED9nU.cjs';
|
|
5
6
|
|
|
6
7
|
declare abstract class BaseProvider implements IProvider {
|
|
7
8
|
name: string;
|
|
@@ -20,42 +21,39 @@ declare abstract class BaseProvider implements IProvider {
|
|
|
20
21
|
/**
|
|
21
22
|
* @inheritdoc
|
|
22
23
|
*/
|
|
23
|
-
abstract downloadJSON(versionPath: string): Promise<UpdateJSON>;
|
|
24
|
+
abstract downloadJSON(versionPath: string, signal: AbortSignal): Promise<UpdateJSON>;
|
|
24
25
|
/**
|
|
25
26
|
* @inheritdoc
|
|
26
27
|
*/
|
|
27
|
-
abstract downloadAsar(name: string, info: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
|
|
28
|
+
abstract downloadAsar(name: string, info: UpdateInfo, signal: AbortSignal, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
|
|
28
29
|
}
|
|
29
30
|
|
|
31
|
+
type URLHandler = (url: URL) => Promisable<URL | string | undefined | null>;
|
|
30
32
|
interface GitHubProviderOptions {
|
|
31
33
|
/**
|
|
32
|
-
*
|
|
34
|
+
* Github user name
|
|
33
35
|
*/
|
|
34
|
-
|
|
36
|
+
user: string;
|
|
35
37
|
/**
|
|
36
|
-
*
|
|
38
|
+
* Github repo name
|
|
37
39
|
*/
|
|
38
40
|
repo: string;
|
|
39
41
|
/**
|
|
40
|
-
*
|
|
42
|
+
* Github branch name that fetch version
|
|
41
43
|
* @default 'HEAD'
|
|
42
44
|
*/
|
|
43
45
|
branch?: string;
|
|
44
46
|
/**
|
|
45
|
-
*
|
|
47
|
+
* Extra headers
|
|
46
48
|
*/
|
|
47
49
|
extraHeaders?: Record<string, string>;
|
|
48
50
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* for Github, there are some {@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L40 public CDN links}
|
|
51
|
+
* Custom url handler ({@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L40 some public CDN links})
|
|
52
52
|
* @example
|
|
53
|
-
* (url
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* return url
|
|
58
|
-
* }
|
|
53
|
+
* (url) => {
|
|
54
|
+
* url.hostname = 'mirror.ghproxy.com'
|
|
55
|
+
* url.pathname = 'https://github.com' + url.pathname
|
|
56
|
+
* return url
|
|
59
57
|
* }
|
|
60
58
|
*/
|
|
61
59
|
urlHandler?: URLHandler;
|
|
@@ -65,28 +63,47 @@ declare class GitHubProvider extends BaseProvider {
|
|
|
65
63
|
private options;
|
|
66
64
|
/**
|
|
67
65
|
* Update Provider for Github repo
|
|
68
|
-
* - download update json from `https://
|
|
66
|
+
* - download update json from `https://github.com/{user}/{repo}/raw/HEAD/{versionPath}`
|
|
69
67
|
* - download update asar from `https://github.com/{user}/{repo}/releases/download/v{version}/{name}-{version}.asar.gz`
|
|
70
68
|
*
|
|
71
|
-
* you can setup `urlHandler` in {@link GitHubProviderOptions}
|
|
69
|
+
* you can setup `urlHandler` in {@link GitHubProviderOptions} to modify url before request
|
|
72
70
|
* @param options provider options
|
|
73
71
|
*/
|
|
74
72
|
constructor(options: GitHubProviderOptions);
|
|
75
73
|
get urlHandler(): URLHandler | undefined;
|
|
76
74
|
set urlHandler(handler: URLHandler);
|
|
77
75
|
private parseURL;
|
|
78
|
-
|
|
79
|
-
|
|
76
|
+
/**
|
|
77
|
+
* @inheritdoc
|
|
78
|
+
*/
|
|
79
|
+
downloadJSON(versionPath: string, signal: AbortSignal): Promise<UpdateJSON>;
|
|
80
|
+
/**
|
|
81
|
+
* @inheritdoc
|
|
82
|
+
*/
|
|
83
|
+
downloadAsar(name: string, info: UpdateInfo, signal: AbortSignal, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
|
|
80
84
|
}
|
|
81
85
|
|
|
82
|
-
declare function getHeader(response: Record<string, Arrayable<string>>, headerKey: any): any;
|
|
83
86
|
/**
|
|
84
|
-
*
|
|
87
|
+
* Safe get value from header
|
|
88
|
+
* @param headers response header
|
|
89
|
+
* @param key target header key
|
|
90
|
+
*/
|
|
91
|
+
declare function getHeader(headers: Record<string, Arrayable<string>>, key: any): any;
|
|
92
|
+
/**
|
|
93
|
+
* Default function to download json and parse to UpdateJson
|
|
94
|
+
* @param url target url
|
|
95
|
+
* @param headers extra headers
|
|
96
|
+
* @param signal abort signal
|
|
85
97
|
*/
|
|
86
|
-
declare function defaultDownloadUpdateJSON(url: string, headers: Record<string, any
|
|
98
|
+
declare function defaultDownloadUpdateJSON(url: string, headers: Record<string, any>, signal: AbortSignal): Promise<UpdateJSON>;
|
|
87
99
|
/**
|
|
88
|
-
* download asar buffer,
|
|
100
|
+
* Default function to download asar buffer,
|
|
101
|
+
* get total size from `Content-Length` header
|
|
102
|
+
* @param url target url
|
|
103
|
+
* @param headers extra headers
|
|
104
|
+
* @param signal abort signal
|
|
105
|
+
* @param onDownloading on downloading callback
|
|
89
106
|
*/
|
|
90
|
-
declare function defaultDownloadAsar(url: string, headers: Record<string, any>, onDownloading?: OnDownloading): Promise<Buffer>;
|
|
107
|
+
declare function defaultDownloadAsar(url: string, headers: Record<string, any>, signal: AbortSignal, onDownloading?: OnDownloading): Promise<Buffer>;
|
|
91
108
|
|
|
92
|
-
export { BaseProvider, DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, URLHandler, defaultDownloadAsar, defaultDownloadUpdateJSON, getHeader };
|
|
109
|
+
export { BaseProvider, DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, type URLHandler, defaultDownloadAsar, defaultDownloadUpdateJSON, getHeader };
|
package/dist/provider.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { URL } from 'node:url';
|
|
2
|
+
import { Promisable, Arrayable } from '@subframe7536/type-utils';
|
|
3
|
+
import { d as defaultIsLowerVersion, a as UpdateJSON, U as UpdateInfo } from './version-BYVQ367i.js';
|
|
4
|
+
import { I as IProvider, D as DownloadingInfo, O as OnDownloading } from './types-BLdN9rkY.js';
|
|
5
|
+
import { f as defaultVerifySignature, a as defaultUnzipFile } from './zip-rm9ED9nU.js';
|
|
5
6
|
|
|
6
7
|
declare abstract class BaseProvider implements IProvider {
|
|
7
8
|
name: string;
|
|
@@ -20,42 +21,39 @@ declare abstract class BaseProvider implements IProvider {
|
|
|
20
21
|
/**
|
|
21
22
|
* @inheritdoc
|
|
22
23
|
*/
|
|
23
|
-
abstract downloadJSON(versionPath: string): Promise<UpdateJSON>;
|
|
24
|
+
abstract downloadJSON(versionPath: string, signal: AbortSignal): Promise<UpdateJSON>;
|
|
24
25
|
/**
|
|
25
26
|
* @inheritdoc
|
|
26
27
|
*/
|
|
27
|
-
abstract downloadAsar(name: string, info: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
|
|
28
|
+
abstract downloadAsar(name: string, info: UpdateInfo, signal: AbortSignal, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
|
|
28
29
|
}
|
|
29
30
|
|
|
31
|
+
type URLHandler = (url: URL) => Promisable<URL | string | undefined | null>;
|
|
30
32
|
interface GitHubProviderOptions {
|
|
31
33
|
/**
|
|
32
|
-
*
|
|
34
|
+
* Github user name
|
|
33
35
|
*/
|
|
34
|
-
|
|
36
|
+
user: string;
|
|
35
37
|
/**
|
|
36
|
-
*
|
|
38
|
+
* Github repo name
|
|
37
39
|
*/
|
|
38
40
|
repo: string;
|
|
39
41
|
/**
|
|
40
|
-
*
|
|
42
|
+
* Github branch name that fetch version
|
|
41
43
|
* @default 'HEAD'
|
|
42
44
|
*/
|
|
43
45
|
branch?: string;
|
|
44
46
|
/**
|
|
45
|
-
*
|
|
47
|
+
* Extra headers
|
|
46
48
|
*/
|
|
47
49
|
extraHeaders?: Record<string, string>;
|
|
48
50
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* for Github, there are some {@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L40 public CDN links}
|
|
51
|
+
* Custom url handler ({@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L40 some public CDN links})
|
|
52
52
|
* @example
|
|
53
|
-
* (url
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* return url
|
|
58
|
-
* }
|
|
53
|
+
* (url) => {
|
|
54
|
+
* url.hostname = 'mirror.ghproxy.com'
|
|
55
|
+
* url.pathname = 'https://github.com' + url.pathname
|
|
56
|
+
* return url
|
|
59
57
|
* }
|
|
60
58
|
*/
|
|
61
59
|
urlHandler?: URLHandler;
|
|
@@ -65,28 +63,47 @@ declare class GitHubProvider extends BaseProvider {
|
|
|
65
63
|
private options;
|
|
66
64
|
/**
|
|
67
65
|
* Update Provider for Github repo
|
|
68
|
-
* - download update json from `https://
|
|
66
|
+
* - download update json from `https://github.com/{user}/{repo}/raw/HEAD/{versionPath}`
|
|
69
67
|
* - download update asar from `https://github.com/{user}/{repo}/releases/download/v{version}/{name}-{version}.asar.gz`
|
|
70
68
|
*
|
|
71
|
-
* you can setup `urlHandler` in {@link GitHubProviderOptions}
|
|
69
|
+
* you can setup `urlHandler` in {@link GitHubProviderOptions} to modify url before request
|
|
72
70
|
* @param options provider options
|
|
73
71
|
*/
|
|
74
72
|
constructor(options: GitHubProviderOptions);
|
|
75
73
|
get urlHandler(): URLHandler | undefined;
|
|
76
74
|
set urlHandler(handler: URLHandler);
|
|
77
75
|
private parseURL;
|
|
78
|
-
|
|
79
|
-
|
|
76
|
+
/**
|
|
77
|
+
* @inheritdoc
|
|
78
|
+
*/
|
|
79
|
+
downloadJSON(versionPath: string, signal: AbortSignal): Promise<UpdateJSON>;
|
|
80
|
+
/**
|
|
81
|
+
* @inheritdoc
|
|
82
|
+
*/
|
|
83
|
+
downloadAsar(name: string, info: UpdateInfo, signal: AbortSignal, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
|
|
80
84
|
}
|
|
81
85
|
|
|
82
|
-
declare function getHeader(response: Record<string, Arrayable<string>>, headerKey: any): any;
|
|
83
86
|
/**
|
|
84
|
-
*
|
|
87
|
+
* Safe get value from header
|
|
88
|
+
* @param headers response header
|
|
89
|
+
* @param key target header key
|
|
90
|
+
*/
|
|
91
|
+
declare function getHeader(headers: Record<string, Arrayable<string>>, key: any): any;
|
|
92
|
+
/**
|
|
93
|
+
* Default function to download json and parse to UpdateJson
|
|
94
|
+
* @param url target url
|
|
95
|
+
* @param headers extra headers
|
|
96
|
+
* @param signal abort signal
|
|
85
97
|
*/
|
|
86
|
-
declare function defaultDownloadUpdateJSON(url: string, headers: Record<string, any
|
|
98
|
+
declare function defaultDownloadUpdateJSON(url: string, headers: Record<string, any>, signal: AbortSignal): Promise<UpdateJSON>;
|
|
87
99
|
/**
|
|
88
|
-
* download asar buffer,
|
|
100
|
+
* Default function to download asar buffer,
|
|
101
|
+
* get total size from `Content-Length` header
|
|
102
|
+
* @param url target url
|
|
103
|
+
* @param headers extra headers
|
|
104
|
+
* @param signal abort signal
|
|
105
|
+
* @param onDownloading on downloading callback
|
|
89
106
|
*/
|
|
90
|
-
declare function defaultDownloadAsar(url: string, headers: Record<string, any>, onDownloading?: OnDownloading): Promise<Buffer>;
|
|
107
|
+
declare function defaultDownloadAsar(url: string, headers: Record<string, any>, signal: AbortSignal, onDownloading?: OnDownloading): Promise<Buffer>;
|
|
91
108
|
|
|
92
|
-
export { BaseProvider, DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, URLHandler, defaultDownloadAsar, defaultDownloadUpdateJSON, getHeader };
|
|
109
|
+
export { BaseProvider, DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, type URLHandler, defaultDownloadAsar, defaultDownloadUpdateJSON, getHeader };
|
package/dist/provider.js
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
import { defaultVerifySignature, defaultUnzipFile } from './chunk-KZSYEXLO.js';
|
|
2
|
-
import { defaultIsLowerVersion, isUpdateJSON } from './chunk-
|
|
2
|
+
import { defaultIsLowerVersion, isUpdateJSON } from './chunk-RCRKUKFX.js';
|
|
3
3
|
import { URL } from 'node:url';
|
|
4
|
-
import
|
|
4
|
+
import electron from 'electron';
|
|
5
5
|
|
|
6
|
-
function getHeader(
|
|
7
|
-
const value =
|
|
6
|
+
function getHeader(headers, key) {
|
|
7
|
+
const value = headers[key];
|
|
8
8
|
if (Array.isArray(value)) {
|
|
9
9
|
return value.length === 0 ? null : value[value.length - 1];
|
|
10
10
|
} else {
|
|
11
11
|
return value;
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
-
async function downloadFn(url, headers, onResponse) {
|
|
15
|
-
await app.whenReady();
|
|
14
|
+
async function downloadFn(url, headers, signal, onResponse) {
|
|
15
|
+
await electron.app.whenReady();
|
|
16
16
|
return new Promise((resolve, reject) => {
|
|
17
|
-
const request = net.request({ url, method: "GET", redirect: "follow", headers });
|
|
17
|
+
const request = electron.net.request({ url, method: "GET", redirect: "follow", headers, cache: "no-cache" });
|
|
18
|
+
signal.addEventListener("abort", () => request.abort(), { once: true });
|
|
18
19
|
request.on("response", (resp) => {
|
|
19
20
|
resp.on("aborted", () => reject(new Error("aborted")));
|
|
20
21
|
resp.on("error", () => reject(new Error("download error")));
|
|
@@ -24,46 +25,56 @@ async function downloadFn(url, headers, onResponse) {
|
|
|
24
25
|
request.end();
|
|
25
26
|
});
|
|
26
27
|
}
|
|
27
|
-
async function defaultDownloadUpdateJSON(url, headers) {
|
|
28
|
-
return await downloadFn(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
28
|
+
async function defaultDownloadUpdateJSON(url, headers, signal) {
|
|
29
|
+
return await downloadFn(
|
|
30
|
+
url,
|
|
31
|
+
headers,
|
|
32
|
+
signal,
|
|
33
|
+
(resp, resolve, reject) => {
|
|
34
|
+
let data = "";
|
|
35
|
+
resp.on("data", (chunk) => data += chunk);
|
|
36
|
+
resp.on("end", () => {
|
|
37
|
+
try {
|
|
38
|
+
const json = JSON.parse(data);
|
|
39
|
+
if (isUpdateJSON(json)) {
|
|
40
|
+
resolve(json);
|
|
41
|
+
} else {
|
|
42
|
+
throw Error;
|
|
43
|
+
}
|
|
44
|
+
} catch {
|
|
45
|
+
reject(new Error(`Invalid update json, "${data}"`));
|
|
38
46
|
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
});
|
|
43
|
-
});
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
);
|
|
44
50
|
}
|
|
45
|
-
async function defaultDownloadAsar(url, headers, onDownloading) {
|
|
51
|
+
async function defaultDownloadAsar(url, headers, signal, onDownloading) {
|
|
46
52
|
let transferred = 0;
|
|
47
53
|
let time = Date.now();
|
|
48
|
-
return await downloadFn(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
54
|
+
return await downloadFn(
|
|
55
|
+
url,
|
|
56
|
+
headers,
|
|
57
|
+
signal,
|
|
58
|
+
(resp, resolve) => {
|
|
59
|
+
const total = +getHeader(resp.headers, "content-length") || -1;
|
|
60
|
+
const data = [];
|
|
61
|
+
resp.on("data", (chunk) => {
|
|
62
|
+
const delta = chunk.length;
|
|
63
|
+
transferred += delta;
|
|
64
|
+
const current = Date.now();
|
|
65
|
+
onDownloading?.({
|
|
66
|
+
percent: total ? +(transferred / total).toFixed(2) * 100 : -1,
|
|
67
|
+
total,
|
|
68
|
+
transferred,
|
|
69
|
+
delta,
|
|
70
|
+
bps: delta / (current - time)
|
|
71
|
+
});
|
|
72
|
+
time = current;
|
|
73
|
+
data.push(chunk);
|
|
61
74
|
});
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
resp.on("end", () => resolve(Buffer.concat(data)));
|
|
66
|
-
});
|
|
75
|
+
resp.on("end", () => resolve(Buffer.concat(data)));
|
|
76
|
+
}
|
|
77
|
+
);
|
|
67
78
|
}
|
|
68
79
|
|
|
69
80
|
// src/provider/base.ts
|
|
@@ -89,10 +100,10 @@ var GitHubProvider = class extends BaseProvider {
|
|
|
89
100
|
options;
|
|
90
101
|
/**
|
|
91
102
|
* Update Provider for Github repo
|
|
92
|
-
* - download update json from `https://
|
|
103
|
+
* - download update json from `https://github.com/{user}/{repo}/raw/HEAD/{versionPath}`
|
|
93
104
|
* - download update asar from `https://github.com/{user}/{repo}/releases/download/v{version}/{name}-{version}.asar.gz`
|
|
94
105
|
*
|
|
95
|
-
* you can setup `urlHandler` in {@link GitHubProviderOptions}
|
|
106
|
+
* you can setup `urlHandler` in {@link GitHubProviderOptions} to modify url before request
|
|
96
107
|
* @param options provider options
|
|
97
108
|
*/
|
|
98
109
|
constructor(options) {
|
|
@@ -108,23 +119,31 @@ var GitHubProvider = class extends BaseProvider {
|
|
|
108
119
|
set urlHandler(handler) {
|
|
109
120
|
this.options.urlHandler = handler;
|
|
110
121
|
}
|
|
111
|
-
async parseURL(
|
|
122
|
+
async parseURL(extraPath) {
|
|
112
123
|
const url = new URL(
|
|
113
|
-
`/${this.options.
|
|
114
|
-
"https://
|
|
124
|
+
`/${this.options.user}/${this.options.repo}/${extraPath}`,
|
|
125
|
+
"https://github.com"
|
|
115
126
|
);
|
|
116
|
-
return (await this.urlHandler?.(url
|
|
127
|
+
return (await this.urlHandler?.(url) || url).toString();
|
|
117
128
|
}
|
|
118
|
-
|
|
129
|
+
/**
|
|
130
|
+
* @inheritdoc
|
|
131
|
+
*/
|
|
132
|
+
async downloadJSON(versionPath, signal) {
|
|
119
133
|
return await defaultDownloadUpdateJSON(
|
|
120
|
-
await this.parseURL(
|
|
121
|
-
{ Accept: "application/json", ...this.options.extraHeaders }
|
|
134
|
+
await this.parseURL(`raw/${this.options.branch}/${versionPath}`),
|
|
135
|
+
{ Accept: "application/json", ...this.options.extraHeaders },
|
|
136
|
+
signal
|
|
122
137
|
);
|
|
123
138
|
}
|
|
124
|
-
|
|
139
|
+
/**
|
|
140
|
+
* @inheritdoc
|
|
141
|
+
*/
|
|
142
|
+
async downloadAsar(name, info, signal, onDownloading) {
|
|
125
143
|
return await defaultDownloadAsar(
|
|
126
|
-
await this.parseURL(
|
|
144
|
+
await this.parseURL(`releases/download/v${info.version}/${name}-${info.version}.asar.gz`),
|
|
127
145
|
{ Accept: "application/octet-stream", ...this.options.extraHeaders },
|
|
146
|
+
signal,
|
|
128
147
|
onDownloading
|
|
129
148
|
);
|
|
130
149
|
}
|