electron-incremental-update 2.0.0-beta.3 → 2.0.0-beta.5

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/dist/index.d.ts CHANGED
@@ -1,139 +1,9 @@
1
- import { EventEmitter } from 'node:events';
2
- import { U as UpdateJSON, a as UpdateInfo } from './version-CemSHimT.js';
3
- import { D as DownloadingInfo, I as IProvider, U as URLHandler } from './types-DxhNaNgR.js';
1
+ import { U as Updater, a as UpdaterOption, L as Logger } from './core-CXETH_bb.js';
2
+ export { C as CheckResult, D as DownloadResult, E as ErrorInfo, b as UpdaterError } from './core-CXETH_bb.js';
3
+ import { I as IProvider } from './types-C5P0h_bB.js';
4
+ import 'node:events';
4
5
  import '@subframe7536/type-utils';
5
6
 
6
- declare const ErrorInfo: {
7
- readonly download: "Download failed";
8
- readonly validate: "Validate failed";
9
- readonly param: "Missing params";
10
- readonly network: "Network error";
11
- };
12
- declare class UpdaterError extends Error {
13
- code: keyof typeof ErrorInfo;
14
- constructor(msg: keyof typeof ErrorInfo, info: string);
15
- }
16
- type CheckResult<T extends UpdateJSON> = {
17
- success: true;
18
- data: Omit<T, 'beta'>;
19
- } | {
20
- success: false;
21
- /**
22
- * minimal version that can update
23
- */
24
- data: string;
25
- } | {
26
- success: false;
27
- data: UpdaterError;
28
- };
29
- type DownloadResult = {
30
- success: true;
31
- } | {
32
- success: false;
33
- data: UpdaterError;
34
- };
35
- interface Logger {
36
- info: (msg: string) => void;
37
- debug: (msg: string) => void;
38
- warn: (msg: string) => void;
39
- error: (msg: string, e?: unknown) => void;
40
- }
41
- interface UpdaterOption {
42
- /**
43
- * public key of signature, which will be auto generated by plugin,
44
- * generate by `selfsigned` if not set
45
- */
46
- SIGNATURE_CERT?: string;
47
- /**
48
- * whether to receive beta update
49
- */
50
- receiveBeta?: boolean;
51
- logger?: Logger;
52
- }
53
-
54
- declare class Updater extends EventEmitter<{
55
- 'checking': any;
56
- 'update-available': [data: UpdateInfo];
57
- 'update-unavailable': [reason: string];
58
- 'error': [error: UpdaterError];
59
- 'download-progress': [info: DownloadingInfo];
60
- 'update-downloaded': any;
61
- }> {
62
- private CERT;
63
- private info?;
64
- private asarPath;
65
- private tmpFilePath;
66
- private provider;
67
- /**
68
- * updater logger
69
- */
70
- logger?: Logger;
71
- /**
72
- * whether to receive beta update
73
- */
74
- receiveBeta?: boolean;
75
- /**
76
- * whether force update in DEV
77
- */
78
- forceUpdate?: boolean;
79
- /**
80
- * initialize incremental updater
81
- * @param provider update provider
82
- * @param option UpdaterOption
83
- */
84
- constructor(provider: IProvider, option?: UpdaterOption);
85
- /**
86
- * this function is used to parse download data.
87
- * - if format is `'json'`
88
- * - if data is `UpdateJSON`, return it
89
- * - if data is string or absent, download URL data and return it
90
- * - if format is `'buffer'`
91
- * - if data is `Buffer`, return it
92
- * - if data is string or absent, download URL data and return it
93
- * @param format 'json' or 'buffer'
94
- * @param data download URL or update json or buffer
95
- */
96
- private fetch;
97
- /**
98
- * handle error message and emit error event
99
- */
100
- private err;
101
- /**
102
- * check update info using default options
103
- */
104
- checkUpdate(): Promise<boolean>;
105
- /**
106
- * check update info using existing update json
107
- * @param data existing update json
108
- */
109
- checkUpdate(data: UpdateJSON): Promise<boolean>;
110
- /**
111
- * download update using default options
112
- */
113
- download(): Promise<boolean>;
114
- /**
115
- * download update using existing `asar.gz` buffer and signature
116
- * @param data existing `asar.gz` buffer
117
- * @param sig signature
118
- */
119
- download(data: Uint8Array | Buffer, sig: string): Promise<boolean>;
120
- /**
121
- * quit App and install
122
- */
123
- quitAndInstall(): void;
124
- /**
125
- * setup provider URL handler
126
- *
127
- * @example
128
- * updater.setURLHandler((url, isDownloadingAsar) => {
129
- * if (isDownloadingAsar) {
130
- * return url.replace('https://raw.githubusercontent.com', 'https://cdn.jsdelivr.net/gh')
131
- * }
132
- * })
133
- */
134
- setURLHandler(handler: URLHandler): void;
135
- }
136
-
137
7
  type Promisable<T> = T | Promise<T>;
138
8
  /**
139
9
  * hooks on rename temp asar path to `${app.name}.asar`
@@ -205,4 +75,4 @@ declare function startupWithUpdater(fn: (updater: Updater) => Promisable<void>):
205
75
  */
206
76
  declare function initApp(appOptions: AppOption): Promise<void>;
207
77
 
208
- export { type AppOption, type CheckResult, type DownloadResult, ErrorInfo, type Logger, Updater, UpdaterError, type UpdaterOption, initApp, startupWithUpdater };
78
+ export { type AppOption, Logger, Updater, UpdaterOption, initApp, startupWithUpdater };
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { isDev, getPathFromAppNameAsar, getEntryVersion, getAppVersion, restartApp } from './chunk-PNYRQYFC.js';
2
- import { isUpdateJSON, __require } from './chunk-BVFQWBLK.js';
1
+ import { isDev, getEntryVersion, getAppVersion, getPathFromAppNameAsar, restartApp } from './chunk-DFNDKSE6.js';
2
+ import { isUpdateJSON, __require } from './chunk-72ZAJ7AF.js';
3
3
  import { join } from 'node:path';
4
- import { existsSync, rmSync, renameSync } from 'node:fs';
4
+ import { writeFileSync, existsSync, renameSync } from 'node:fs';
5
5
  import { app } from 'electron';
6
6
  import { EventEmitter } from 'node:events';
7
7
 
@@ -24,8 +24,6 @@ var UpdaterError = class extends Error {
24
24
  var Updater = class extends EventEmitter {
25
25
  CERT = __EIU_SIGNATURE_CERT__;
26
26
  info;
27
- asarPath;
28
- tmpFilePath;
29
27
  provider;
30
28
  /**
31
29
  * updater logger
@@ -63,14 +61,8 @@ var Updater = class extends EventEmitter {
63
61
  };
64
62
  this.logger.info("no logger set, enable dev-only logger");
65
63
  }
66
- this.asarPath = getPathFromAppNameAsar();
67
- this.tmpFilePath = `${this.asarPath}.tmp`;
68
64
  }
69
65
  async fetch(format, data) {
70
- if (existsSync(this.tmpFilePath)) {
71
- this.logger?.warn(`remove tmp file: ${this.tmpFilePath}`);
72
- rmSync(this.tmpFilePath);
73
- }
74
66
  if (typeof data === "object") {
75
67
  if (format === "json" && isUpdateJSON(data) || format === "buffer" && Buffer.isBuffer(data)) {
76
68
  return data;
@@ -106,14 +98,13 @@ var Updater = class extends EventEmitter {
106
98
  if (!_data) {
107
99
  return emitUnavailable("failed to get update info");
108
100
  }
109
- let { signature, size, version, minimumVersion, beta } = _data;
101
+ let { signature, version, minimumVersion, beta } = _data;
110
102
  if (this.receiveBeta) {
111
103
  version = beta.version;
112
104
  signature = beta.signature;
113
105
  minimumVersion = beta.minimumVersion;
114
- size = beta.size;
115
106
  }
116
- this.logger?.debug(`checked update, version: ${version}, size: ${size}, signature: ${signature}`);
107
+ this.logger?.debug(`checked update, version: ${version}, signature: ${signature}`);
117
108
  if (isDev && !this.forceUpdate && !data) {
118
109
  return emitUnavailable("skip check update in dev mode, to force update, set `updater.forceUpdate` to true or call checkUpdate with UpdateJSON");
119
110
  }
@@ -128,11 +119,11 @@ var Updater = class extends EventEmitter {
128
119
  return emitUnavailable(`current version (${appVersion}) < new version (${version})`);
129
120
  }
130
121
  this.logger?.info(`update available: ${version}`);
131
- this.info = { signature, minimumVersion, version, size };
122
+ this.info = { signature, minimumVersion, version };
132
123
  this.emit("update-available", this.info);
133
124
  return true;
134
125
  }
135
- async download(data, sig) {
126
+ async downloadUpdate(data, sig) {
136
127
  const _sig = sig ?? this.info?.signature;
137
128
  if (!_sig) {
138
129
  this.err("download failed", "param", "no update signature, please call `checkUpdate` first");
@@ -151,8 +142,9 @@ var Updater = class extends EventEmitter {
151
142
  }
152
143
  this.logger?.debug("verify success");
153
144
  try {
154
- this.logger?.debug(`extract to ${this.tmpFilePath}`);
155
- await this.provider.unzipFile(buffer, this.tmpFilePath);
145
+ const tmpFilePath = getPathFromAppNameAsar() + ".tmp";
146
+ this.logger?.debug(`install to ${tmpFilePath}`);
147
+ writeFileSync(tmpFilePath, await this.provider.unzipFile(buffer));
156
148
  this.logger?.info(`download success, version: ${_ver}`);
157
149
  this.info = void 0;
158
150
  this.emit("update-downloaded");
@@ -175,7 +167,8 @@ var Updater = class extends EventEmitter {
175
167
  * @example
176
168
  * updater.setURLHandler((url, isDownloadingAsar) => {
177
169
  * if (isDownloadingAsar) {
178
- * return url.replace('https://raw.githubusercontent.com', 'https://cdn.jsdelivr.net/gh')
170
+ * url.hostname = 'https://cdn.jsdelivr.net/gh'
171
+ * return url
179
172
  * }
180
173
  * })
181
174
  */
package/dist/provider.cjs CHANGED
@@ -1,11 +1,11 @@
1
1
  'use strict';
2
2
 
3
+ var url = require('url');
3
4
  var electron = require('electron');
4
5
  var crypto = require('crypto');
5
- var fs = require('fs');
6
6
  var zlib = require('zlib');
7
7
 
8
- // src/provider/download.ts
8
+ // src/provider/github.ts
9
9
 
10
10
  // src/utils/version.ts
11
11
  function parseVersion(version) {
@@ -52,11 +52,19 @@ function defaultIsLowerVersion(oldVer, newVer) {
52
52
  return false;
53
53
  }
54
54
  function isUpdateJSON(json) {
55
- const is = (j) => !!(j && j.minimumVersion && j.signature && j.size && j.version);
55
+ const is = (j) => !!(j && j.minimumVersion && j.signature && j.version);
56
56
  return is(json) && is(json?.beta);
57
57
  }
58
58
 
59
59
  // src/provider/download.ts
60
+ function getHeader(response, headerKey) {
61
+ const value = response.headers[headerKey];
62
+ if (Array.isArray(value)) {
63
+ return value.length === 0 ? null : value[value.length - 1];
64
+ } else {
65
+ return value;
66
+ }
67
+ }
60
68
  async function downloadFn(url, headers, onResponse) {
61
69
  await electron.app.whenReady();
62
70
  return new Promise((resolve, reject) => {
@@ -89,10 +97,11 @@ async function defaultDownloadUpdateJSON(url, headers) {
89
97
  });
90
98
  });
91
99
  }
92
- async function defaultDownloadAsar(url, headers, total, onDownloading) {
100
+ async function defaultDownloadAsar(url, headers, onDownloading) {
93
101
  let transferred = 0;
94
102
  let time = Date.now();
95
103
  return await downloadFn(url, headers, (resp, resolve) => {
104
+ const total = getHeader(resp.headers, "content-length") || -1;
96
105
  let data = [];
97
106
  resp.on("data", (chunk) => {
98
107
  transferred += chunk.length;
@@ -127,14 +136,14 @@ function defaultVerify(buffer, signature, cert) {
127
136
  return void 0;
128
137
  }
129
138
  }
130
- async function defaultUnzipFile(buffer, targetFilePath) {
139
+ async function defaultUnzipFile(buffer) {
131
140
  return new Promise((resolve, reject) => {
132
141
  zlib.brotliDecompress(buffer, (err, buffer2) => {
133
142
  if (err) {
134
143
  reject(err);
144
+ } else {
145
+ resolve(buffer2);
135
146
  }
136
- fs.writeFileSync(targetFilePath, buffer2);
137
- resolve();
138
147
  });
139
148
  });
140
149
  }
@@ -149,11 +158,8 @@ var BaseProvider = class {
149
158
 
150
159
  // src/provider/github.ts
151
160
  var GitHubProvider = class extends BaseProvider {
152
- ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36";
153
161
  name = "GithubProvider";
154
- urlHandler;
155
- url;
156
- extraHeaders;
162
+ options;
157
163
  /**
158
164
  * Update Provider for Github repo
159
165
  * - download update json from `https://raw.githubusercontent.com/{user}/{repo}/HEAD/{versionPath}`
@@ -164,33 +170,31 @@ var GitHubProvider = class extends BaseProvider {
164
170
  */
165
171
  constructor(options) {
166
172
  super();
167
- this.extraHeaders = options.extraHeaders;
168
- this.urlHandler = options.urlHandler;
169
- if (!options.url.startsWith("https://github.com")) {
170
- throw new Error(`${this.name}: invalid github url: ${options.url}`);
171
- }
172
- this.url = options.url;
173
- if (!this.url.endsWith("/")) {
174
- this.url += "/";
175
- }
173
+ this.options = options;
174
+ }
175
+ get urlHandler() {
176
+ return this.options.urlHandler;
177
+ }
178
+ set urlHandler(handler) {
179
+ this.options.urlHandler = handler;
176
180
  }
177
181
  async parseURL(isDownloadAsar, extraPath) {
178
- const _url = new URL(this.url);
179
- _url.hostname = isDownloadAsar ? "github.com" : "raw.githubusercontent.com";
180
- _url.pathname += extraPath;
181
- return (await this.urlHandler?.(_url, isDownloadAsar) || _url).toString();
182
+ const url$1 = new url.URL(
183
+ `/${this.options.username}/${this.options.repo}/${extraPath}`,
184
+ "https://" + (isDownloadAsar ? "github.com" : "raw.githubusercontent.com")
185
+ );
186
+ return (await this.urlHandler?.(url$1, isDownloadAsar) || url$1).toString();
182
187
  }
183
188
  async downloadJSON(versionPath) {
184
189
  return await defaultDownloadUpdateJSON(
185
190
  await this.parseURL(false, `HEAD/${versionPath}`),
186
- { userAgent: this.ua, accept: "application/json", ...this.extraHeaders }
191
+ { accept: "application/json", ...this.options.extraHeaders }
187
192
  );
188
193
  }
189
- async downloadAsar(name, { version, size }, onDownloading) {
194
+ async downloadAsar(name, info, onDownloading) {
190
195
  return await defaultDownloadAsar(
191
- await this.parseURL(true, `releases/download/v${version}/${name}-${version}.asar.gz`),
192
- { userAgent: this.ua, accept: "application/octet-stream", ...this.extraHeaders },
193
- size,
196
+ await this.parseURL(true, `releases/download/v${info.version}/${name}-${info.version}.asar.gz`),
197
+ { accept: "application/octet-stream", ...this.options.extraHeaders },
194
198
  onDownloading
195
199
  );
196
200
  }
@@ -200,3 +204,4 @@ exports.BaseProvider = BaseProvider;
200
204
  exports.GitHubProvider = GitHubProvider;
201
205
  exports.defaultDownloadAsar = defaultDownloadAsar;
202
206
  exports.defaultDownloadUpdateJSON = defaultDownloadUpdateJSON;
207
+ exports.getHeader = getHeader;
@@ -1,7 +1,6 @@
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
- import '@subframe7536/type-utils';
1
+ import { I as IProvider, d as defaultIsLowerVersion, a as UpdateJSON, U as UpdateInfo, D as DownloadingInfo, c as URLHandler, O as OnDownloading } from './types-C5P0h_bB.cjs';
2
+ import { f as defaultVerify, a as defaultUnzipFile } from './zip-WRrEMkgp.cjs';
3
+ import { Arrayable } from '@subframe7536/type-utils';
5
4
 
6
5
  declare abstract class BaseProvider implements IProvider {
7
6
  name: string;
@@ -14,10 +13,13 @@ declare abstract class BaseProvider implements IProvider {
14
13
 
15
14
  interface GitHubProviderOptions {
16
15
  /**
17
- * github repo root url
18
- * @example 'https://github.com/electron/electron/'
16
+ * github user name
19
17
  */
20
- url: string;
18
+ username: string;
19
+ /**
20
+ * github repo name
21
+ */
22
+ repo: string;
21
23
  /**
22
24
  * extra headers
23
25
  */
@@ -38,11 +40,8 @@ interface GitHubProviderOptions {
38
40
  urlHandler?: URLHandler;
39
41
  }
40
42
  declare class GitHubProvider extends BaseProvider {
41
- private ua;
42
43
  name: string;
43
- urlHandler?: URLHandler;
44
- private url;
45
- private extraHeaders?;
44
+ private options;
46
45
  /**
47
46
  * Update Provider for Github repo
48
47
  * - download update json from `https://raw.githubusercontent.com/{user}/{repo}/HEAD/{versionPath}`
@@ -52,12 +51,15 @@ declare class GitHubProvider extends BaseProvider {
52
51
  * @param options provider options
53
52
  */
54
53
  constructor(options: GitHubProviderOptions);
54
+ get urlHandler(): URLHandler | undefined;
55
+ set urlHandler(handler: URLHandler);
55
56
  private parseURL;
56
57
  downloadJSON(versionPath: string): Promise<UpdateJSON>;
57
- downloadAsar(name: string, { version, size }: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
58
+ downloadAsar(name: string, info: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
58
59
  }
59
60
 
61
+ declare function getHeader(response: Record<string, Arrayable<string>>, headerKey: any): any;
60
62
  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>;
63
+ declare function defaultDownloadAsar(url: string, headers: Record<string, any>, onDownloading?: OnDownloading): Promise<Buffer>;
62
64
 
63
- export { BaseProvider, DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, URLHandler, defaultDownloadAsar, defaultDownloadUpdateJSON };
65
+ export { BaseProvider, DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, URLHandler, defaultDownloadAsar, defaultDownloadUpdateJSON, getHeader };
@@ -1,7 +1,6 @@
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
- import '@subframe7536/type-utils';
1
+ import { I as IProvider, d as defaultIsLowerVersion, a as UpdateJSON, U as UpdateInfo, D as DownloadingInfo, c as URLHandler, O as OnDownloading } from './types-C5P0h_bB.js';
2
+ import { f as defaultVerify, a as defaultUnzipFile } from './zip-WRrEMkgp.js';
3
+ import { Arrayable } from '@subframe7536/type-utils';
5
4
 
6
5
  declare abstract class BaseProvider implements IProvider {
7
6
  name: string;
@@ -14,10 +13,13 @@ declare abstract class BaseProvider implements IProvider {
14
13
 
15
14
  interface GitHubProviderOptions {
16
15
  /**
17
- * github repo root url
18
- * @example 'https://github.com/electron/electron/'
16
+ * github user name
19
17
  */
20
- url: string;
18
+ username: string;
19
+ /**
20
+ * github repo name
21
+ */
22
+ repo: string;
21
23
  /**
22
24
  * extra headers
23
25
  */
@@ -38,11 +40,8 @@ interface GitHubProviderOptions {
38
40
  urlHandler?: URLHandler;
39
41
  }
40
42
  declare class GitHubProvider extends BaseProvider {
41
- private ua;
42
43
  name: string;
43
- urlHandler?: URLHandler;
44
- private url;
45
- private extraHeaders?;
44
+ private options;
46
45
  /**
47
46
  * Update Provider for Github repo
48
47
  * - download update json from `https://raw.githubusercontent.com/{user}/{repo}/HEAD/{versionPath}`
@@ -52,12 +51,15 @@ declare class GitHubProvider extends BaseProvider {
52
51
  * @param options provider options
53
52
  */
54
53
  constructor(options: GitHubProviderOptions);
54
+ get urlHandler(): URLHandler | undefined;
55
+ set urlHandler(handler: URLHandler);
55
56
  private parseURL;
56
57
  downloadJSON(versionPath: string): Promise<UpdateJSON>;
57
- downloadAsar(name: string, { version, size }: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
58
+ downloadAsar(name: string, info: UpdateInfo, onDownloading?: (info: DownloadingInfo) => void): Promise<Buffer>;
58
59
  }
59
60
 
61
+ declare function getHeader(response: Record<string, Arrayable<string>>, headerKey: any): any;
60
62
  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>;
63
+ declare function defaultDownloadAsar(url: string, headers: Record<string, any>, onDownloading?: OnDownloading): Promise<Buffer>;
62
64
 
63
- export { BaseProvider, DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, URLHandler, defaultDownloadAsar, defaultDownloadUpdateJSON };
65
+ export { BaseProvider, DownloadingInfo, GitHubProvider, type GitHubProviderOptions, IProvider, OnDownloading, URLHandler, defaultDownloadAsar, defaultDownloadUpdateJSON, getHeader };
package/dist/provider.js CHANGED
@@ -1,7 +1,16 @@
1
- import { defaultVerify, defaultUnzipFile } from './chunk-5WFXC5GU.js';
2
- import { defaultIsLowerVersion, isUpdateJSON } from './chunk-BVFQWBLK.js';
1
+ import { defaultVerify, defaultUnzipFile } from './chunk-JSYIRKTR.js';
2
+ import { defaultIsLowerVersion, isUpdateJSON } from './chunk-72ZAJ7AF.js';
3
+ import { URL } from 'node:url';
3
4
  import { app, net } from 'electron';
4
5
 
6
+ function getHeader(response, headerKey) {
7
+ const value = response.headers[headerKey];
8
+ if (Array.isArray(value)) {
9
+ return value.length === 0 ? null : value[value.length - 1];
10
+ } else {
11
+ return value;
12
+ }
13
+ }
5
14
  async function downloadFn(url, headers, onResponse) {
6
15
  await app.whenReady();
7
16
  return new Promise((resolve, reject) => {
@@ -34,10 +43,11 @@ async function defaultDownloadUpdateJSON(url, headers) {
34
43
  });
35
44
  });
36
45
  }
37
- async function defaultDownloadAsar(url, headers, total, onDownloading) {
46
+ async function defaultDownloadAsar(url, headers, onDownloading) {
38
47
  let transferred = 0;
39
48
  let time = Date.now();
40
49
  return await downloadFn(url, headers, (resp, resolve) => {
50
+ const total = getHeader(resp.headers, "content-length") || -1;
41
51
  let data = [];
42
52
  resp.on("data", (chunk) => {
43
53
  transferred += chunk.length;
@@ -66,11 +76,8 @@ var BaseProvider = class {
66
76
 
67
77
  // src/provider/github.ts
68
78
  var GitHubProvider = class extends BaseProvider {
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";
70
79
  name = "GithubProvider";
71
- urlHandler;
72
- url;
73
- extraHeaders;
80
+ options;
74
81
  /**
75
82
  * Update Provider for Github repo
76
83
  * - download update json from `https://raw.githubusercontent.com/{user}/{repo}/HEAD/{versionPath}`
@@ -81,36 +88,34 @@ var GitHubProvider = class extends BaseProvider {
81
88
  */
82
89
  constructor(options) {
83
90
  super();
84
- this.extraHeaders = options.extraHeaders;
85
- this.urlHandler = options.urlHandler;
86
- if (!options.url.startsWith("https://github.com")) {
87
- throw new Error(`${this.name}: invalid github url: ${options.url}`);
88
- }
89
- this.url = options.url;
90
- if (!this.url.endsWith("/")) {
91
- this.url += "/";
92
- }
91
+ this.options = options;
92
+ }
93
+ get urlHandler() {
94
+ return this.options.urlHandler;
95
+ }
96
+ set urlHandler(handler) {
97
+ this.options.urlHandler = handler;
93
98
  }
94
99
  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();
100
+ const url = new URL(
101
+ `/${this.options.username}/${this.options.repo}/${extraPath}`,
102
+ "https://" + (isDownloadAsar ? "github.com" : "raw.githubusercontent.com")
103
+ );
104
+ return (await this.urlHandler?.(url, isDownloadAsar) || url).toString();
99
105
  }
100
106
  async downloadJSON(versionPath) {
101
107
  return await defaultDownloadUpdateJSON(
102
108
  await this.parseURL(false, `HEAD/${versionPath}`),
103
- { userAgent: this.ua, accept: "application/json", ...this.extraHeaders }
109
+ { accept: "application/json", ...this.options.extraHeaders }
104
110
  );
105
111
  }
106
- async downloadAsar(name, { version, size }, onDownloading) {
112
+ async downloadAsar(name, info, onDownloading) {
107
113
  return await defaultDownloadAsar(
108
- await this.parseURL(true, `releases/download/v${version}/${name}-${version}.asar.gz`),
109
- { userAgent: this.ua, accept: "application/octet-stream", ...this.extraHeaders },
110
- size,
114
+ await this.parseURL(true, `releases/download/v${info.version}/${name}-${info.version}.asar.gz`),
115
+ { accept: "application/octet-stream", ...this.options.extraHeaders },
111
116
  onDownloading
112
117
  );
113
118
  }
114
119
  };
115
120
 
116
- export { BaseProvider, GitHubProvider, defaultDownloadAsar, defaultDownloadUpdateJSON };
121
+ export { BaseProvider, GitHubProvider, defaultDownloadAsar, defaultDownloadUpdateJSON, getHeader };
@@ -1,5 +1,30 @@
1
1
  import { Promisable } from '@subframe7536/type-utils';
2
- import { U as UpdateJSON, a as UpdateInfo } from './version-CemSHimT.cjs';
2
+
3
+ interface Version {
4
+ major: number;
5
+ minor: number;
6
+ patch: number;
7
+ stage: string;
8
+ stageVersion: number;
9
+ }
10
+ declare function parseVersion(version: string): Version;
11
+ declare function defaultIsLowerVersion(oldVer: string, newVer: string): boolean;
12
+ /**
13
+ * update info json
14
+ */
15
+ type UpdateInfo = {
16
+ signature: string;
17
+ minimumVersion: string;
18
+ version: string;
19
+ };
20
+ /**
21
+ * {@link UpdateInfo} with beta
22
+ */
23
+ type UpdateJSON = UpdateInfo & {
24
+ beta: UpdateInfo;
25
+ };
26
+ declare function isUpdateJSON(json: any): json is UpdateJSON;
27
+ declare function defaultVersionJsonGenerator(existingJson: UpdateJSON, signature: string, version: string, minimumVersion: string): UpdateJSON;
3
28
 
4
29
  type URLHandler = (url: URL, isDownloadAsar: boolean) => Promisable<URL | string | undefined | null>;
5
30
  type OnDownloading = (progress: DownloadingInfo) => void;
@@ -10,10 +35,14 @@ interface DownloadingInfo {
10
35
  delta: number;
11
36
  /**
12
37
  * downloaded percent, 0 ~ 100
38
+ *
39
+ * If not `Content-Length` header, will be nagative
13
40
  */
14
41
  percent: number;
15
42
  /**
16
43
  * total size
44
+ *
45
+ * If not `Content-Length` header, will be -1
17
46
  */
18
47
  total: number;
19
48
  /**
@@ -57,11 +86,10 @@ interface IProvider {
57
86
  */
58
87
  isLowerVersion: (oldVer: string, newVer: string) => boolean;
59
88
  /**
60
- * unzip file
89
+ * unzip file buffer
61
90
  * @param buffer source buffer
62
- * @param targetFilePath target file path
63
91
  */
64
- unzipFile: (buffer: Buffer, targetFilePath: string) => Promise<void>;
92
+ unzipFile: (buffer: Buffer) => Promise<Buffer>;
65
93
  /**
66
94
  * verify asar signature
67
95
  * @param buffer file buffer
@@ -72,4 +100,4 @@ interface IProvider {
72
100
  verifySignaure: (buffer: Buffer, signature: string, cert: string) => Promisable<string | undefined>;
73
101
  }
74
102
 
75
- export type { DownloadingInfo as D, IProvider as I, OnDownloading as O, URLHandler as U };
103
+ export { type DownloadingInfo as D, type IProvider as I, type OnDownloading as O, type UpdateInfo as U, type Version as V, type UpdateJSON as a, defaultVersionJsonGenerator as b, type URLHandler as c, defaultIsLowerVersion as d, isUpdateJSON as i, parseVersion as p };