electron-incremental-update 1.3.0 → 2.0.0-beta.2

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,135 +1,82 @@
1
- import { U as UpdateInfo, a as UpdateJSON } from './pure-GoN_3MEj.js';
1
+ import { EventEmitter } from 'node:stream';
2
+ import { U as UpdateJSON, a as UpdateInfo } from './version-CffZWDhZ.js';
3
+ import { D as DownloadingInfo, U as URLHandler, I as IProvider } from './types-CPq1MrYZ.js';
4
+ import '@subframe7536/type-utils';
2
5
 
3
6
  declare const ErrorInfo: {
4
- readonly downlaod: "Download failed";
7
+ readonly download: "Download failed";
5
8
  readonly validate: "Validate failed";
6
9
  readonly param: "Missing params";
7
- readonly version: "Unsatisfied version";
8
10
  };
9
11
  declare class UpdaterError extends Error {
10
- constructor(msg: typeof ErrorInfo[keyof typeof ErrorInfo], info: string);
12
+ code: keyof typeof ErrorInfo;
13
+ constructor(msg: keyof typeof ErrorInfo, info: string);
11
14
  }
12
- type CheckResult = UpdateInfo | undefined | UpdaterError;
13
- type DownloadResult = true | UpdaterError;
14
- type DownloadingInfo = {
15
- /**
16
- * downloaded percent, 0% - 100%
17
- */
18
- percent: `${number}%`;
19
- /**
20
- * total size
21
- */
22
- total: number;
23
- /**
24
- * downloaded size
25
- */
26
- current: number;
15
+ type CheckResult<T extends UpdateJSON> = {
16
+ success: true;
17
+ data: Omit<T, 'beta'>;
18
+ } | {
19
+ success: false;
20
+ /**
21
+ * minimal version that can update
22
+ */
23
+ data: string;
24
+ } | {
25
+ success: false;
26
+ data: UpdaterError;
27
27
  };
28
- type Logger = {
28
+ type DownloadResult = {
29
+ success: true;
30
+ } | {
31
+ success: false;
32
+ data: UpdaterError;
33
+ };
34
+ interface Logger {
29
35
  info: (msg: string) => void;
30
36
  debug: (msg: string) => void;
31
37
  warn: (msg: string) => void;
32
- error: (msg: string, e?: Error) => void;
33
- };
34
- type UpdaterOverrideFunctions = {
35
- /**
36
- * custom version compare function
37
- * @param version1 old version string
38
- * @param version2 new version string
39
- * @returns if version1 < version2
40
- */
41
- isLowerVersion?: (version1: string, version2: string) => boolean | Promise<boolean>;
42
- /**
43
- * custom verify signature function
44
- * @param buffer file buffer
45
- * @param signature signature
46
- * @param cert certificate
47
- * @returns if signature is valid, returns the version or `true` , otherwise returns `false`
48
- */
49
- verifySignaure?: (buffer: Buffer, signature: string, cert: string) => string | false | Promise<string | false>;
50
- /**
51
- * custom download JSON function
52
- * @param url download url
53
- * @param header download header
54
- * @returns `UpdateJSON`
55
- */
56
- downloadJSON?: (url: string, headers: Record<string, any>) => Promise<UpdateJSON>;
57
- /**
58
- * custom download buffer function
59
- * @param url download url
60
- * @param headers download header
61
- * @param total precaculated file total size
62
- * @param onDownloading on downloading callback
63
- * @returns `Buffer`
64
- */
65
- downloadBuffer?: (url: string, headers: Record<string, any>, total: number, onDownloading?: (progress: DownloadingInfo) => void) => Promise<Buffer>;
66
- };
67
- type UpdaterDownloadConfig = {
68
- /**
69
- * download user agent
70
- * @default 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
71
- */
72
- userAgent?: string;
73
- /**
74
- * extra download header, `accept` and `user-agent` is set by default
75
- */
76
- extraHeader?: Record<string, string>;
77
- };
38
+ error: (msg: string, e?: unknown) => void;
39
+ }
78
40
  interface UpdaterOption {
79
41
  /**
80
42
  * public key of signature, which will be auto generated by plugin,
81
43
  * generate by `selfsigned` if not set
82
44
  */
83
45
  SIGNATURE_CERT?: string;
84
- /**
85
- * repository url, e.g. `https://github.com/electron/electron`
86
- *
87
- * you can use the `repository` in `package.json`
88
- *
89
- * if `updateJsonURL` or `releaseAsarURL` are absent,
90
- * `repository` will be used to determine the url
91
- */
92
- repository?: string;
93
- /**
94
- * URL of version info json
95
- * @default `${repository.replace('github.com', 'raw.githubusercontent.com')}/master/version.json`
96
- * @throws if `updateJsonURL` and `repository` are all not set
97
- */
98
- updateJsonURL?: string;
99
- /**
100
- * URL of release asar.gz
101
- * @default `${repository}/releases/download/v${version}/${app.name}-${version}.asar.gz`
102
- * @throws if `releaseAsarURL` and `repository` are all not set
103
- */
104
- releaseAsarURL?: string;
105
46
  /**
106
47
  * whether to receive beta update
107
48
  */
108
49
  receiveBeta?: boolean;
109
- overrideFunctions?: UpdaterOverrideFunctions;
110
- downloadConfig?: UpdaterDownloadConfig;
50
+ logger?: Logger;
111
51
  }
112
52
 
113
- declare class Updater {
53
+ declare class Updater extends EventEmitter<{
54
+ 'checking': any;
55
+ 'update-available': [data: UpdateInfo];
56
+ 'update-unavailable': [reason: string];
57
+ 'error': [error: UpdaterError];
58
+ 'download-progress': [info: DownloadingInfo];
59
+ 'update-downloaded': any;
60
+ }> {
114
61
  private CERT;
115
62
  private info?;
116
- private option;
63
+ private options;
117
64
  private asarPath;
118
65
  private gzipPath;
119
66
  private tmpFilePath;
67
+ private provider;
120
68
  /**
121
69
  * updater logger
122
70
  */
123
71
  logger?: Logger;
124
72
  /**
125
- * downloading progress hook
126
- * @param progress download progress
127
- * @example
128
- * updater.onDownloading = ({ percent, total, current }) => {
129
- * console.log(`download progress: ${percent}, total: ${total}, current: ${current}`)
130
- * }
73
+ * URL handler hook
74
+ *
75
+ * for Github, there are some {@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L34 public CDNs}
76
+ * @param url source url
77
+ * @param isDownloadAsar whether is download asar
131
78
  */
132
- onDownloading?: (progress: DownloadingInfo) => void;
79
+ handleURL?: URLHandler;
133
80
  /**
134
81
  * whether receive beta version
135
82
  */
@@ -137,10 +84,10 @@ declare class Updater {
137
84
  set receiveBeta(receiveBeta: boolean);
138
85
  /**
139
86
  * initialize incremental updater
87
+ * @param provider update provider
140
88
  * @param option UpdaterOption
141
89
  */
142
- constructor(option?: UpdaterOption);
143
- private needUpdate;
90
+ constructor(provider: IProvider, option?: UpdaterOption);
144
91
  /**
145
92
  * this function is used to parse download data.
146
93
  * - if format is `'json'`
@@ -152,120 +99,72 @@ declare class Updater {
152
99
  * @param format 'json' or 'buffer'
153
100
  * @param data download URL or update json or buffer
154
101
  */
155
- private parseData;
102
+ private fetch;
156
103
  /**
157
- * check update info using default options
158
- * @returns
159
- * - Available: `{size: number, version: string}`
160
- * - Unavailable: `undefined`
161
- * - Fail: `UpdaterError`
104
+ * handle error message and emit error event
162
105
  */
163
- checkUpdate(): Promise<CheckResult>;
106
+ private err;
164
107
  /**
165
- * check update info using custom url
166
- * @param url custom download URL of `updatejson`
167
- * @returns
168
- * - Available:`{size: number, version: string}`
169
- * - Unavailable: `undefined`
170
- * - Fail: `UpdaterError`
108
+ * check update info using default options
171
109
  */
172
- checkUpdate(url: string): Promise<CheckResult>;
110
+ checkUpdate(): Promise<boolean>;
173
111
  /**
174
112
  * check update info using existing update json
175
113
  * @param data existing update json
176
- * @returns
177
- * - Available:`{size: number, version: string}`
178
- * - Unavailable: `undefined`
179
- * - Fail: `UpdaterError`
180
114
  */
181
- checkUpdate(data: UpdateJSON): Promise<CheckResult>;
115
+ checkUpdate(data: UpdateJSON): Promise<boolean>;
182
116
  /**
183
117
  * download update using default options
184
- * @returns
185
- * - Success: `true`
186
- * - Fail: `UpdaterError`
187
118
  */
188
- download(): Promise<DownloadResult>;
189
- /**
190
- * download update using custom url
191
- * @param url custom download URL
192
- * @returns
193
- * - Success: `true`
194
- * - Fail: `UpdaterError`
195
- */
196
- download(url: string): Promise<DownloadResult>;
119
+ download(): Promise<boolean>;
197
120
  /**
198
121
  * download update using existing `asar.gz` buffer and signature
199
122
  * @param data existing `asar.gz` buffer
200
123
  * @param sig signature
201
- * @returns
202
- * - Success: `true`
203
- * - Fail: `UpdaterError`
204
124
  */
205
- download(data: Buffer, sig: string): Promise<DownloadResult>;
125
+ download(data: Uint8Array, sig: string): Promise<boolean>;
206
126
  /**
207
127
  * quit App and install
208
128
  */
209
129
  quitAndInstall(): void;
210
130
  }
211
131
 
212
- type Func = Required<UpdaterOverrideFunctions>;
213
- declare const downloadJSONDefault: Func['downloadJSON'];
214
- declare const downloadBufferDefault: Func['downloadBuffer'];
215
-
216
- declare const isLowerVersionDefault: Func['isLowerVersion'];
217
-
132
+ type Promisable<T> = T | Promise<T>;
218
133
  /**
219
- * create updater instance
220
- * @param option updater option
221
- * @returns updater
134
+ * hooks on rename temp asar path to `${app.name}.asar`
135
+ * @param install `() => renameSync(tempAsarPath, appNameAsarPath)`
136
+ * @param tempAsarPath temp(updated) asar path
137
+ * @param appNameAsarPath `${app.name}.asar` path
138
+ * @param logger logger
139
+ * @default install(); logger.info(`update success!`)
222
140
  */
223
- declare function createUpdater(option?: UpdaterOption): Updater;
224
-
225
- type Promisable<T> = T | Promise<T>;
226
- type OnInstallFunction = (install: VoidFunction, tempAsarPath: string, appNameAsarPath: string, logger?: Logger) => Promisable<void>;
227
- type AppOption = {
141
+ type OnInstallFunction = (install: VoidFunction, tempAsarPath: string, appNameAsarPath: string, logger: Logger) => Promisable<void>;
142
+ interface AppOption {
143
+ /**
144
+ * update provider
145
+ */
146
+ provider: IProvider;
228
147
  /**
229
148
  * updater options
230
149
  */
231
150
  updater?: (() => Promisable<Updater>) | UpdaterOption;
232
151
  /**
233
- * path of electron output dist when in development
234
- * @default '../dist-electron'
152
+ * hooks on rename temp asar path to `${app.name}.asar`
235
153
  */
236
- electronDevDistPath?: string;
154
+ onInstall?: OnInstallFunction;
237
155
  /**
238
- * relative path of main entry in electron dist
239
- * @default 'main/index.js'
156
+ * hooks before app start up
157
+ * @param mainFilePath main file path of `${app.name}.asar`
158
+ * @param logger logger
240
159
  */
241
- mainPath?: string;
160
+ beforeStart?: (mainFilePath: string, logger: Logger) => Promisable<void>;
242
161
  /**
243
- * update hooks
162
+ * hooks on app start up error
163
+ * @param err installing or startup error
164
+ * @param logger logger
244
165
  */
245
- hooks?: {
246
- /**
247
- * hooks on rename temp asar path to `${app.name}.asar`
248
- * @param install `() => renameSync(tempAsarPath, appNameAsarPath)`
249
- * @param tempAsarPath temp(updated) asar path
250
- * @param appNameAsarPath `${app.name}.asar` path
251
- * @param logger logger
252
- * @default install(); logger?.info(`update success!`)
253
- */
254
- onInstall?: OnInstallFunction;
255
- /**
256
- * hooks before start
257
- * @param appNameAsarPath path of `${app.name}.asar`
258
- * @param logger logger
259
- */
260
- beforeStart?: (appNameAsarPath: string, logger?: Logger) => Promisable<void>;
261
- /**
262
- * hooks on start up error
263
- * @param err installing or startup error
264
- * @param logger logger
265
- */
266
- onStartError?: (err: unknown, logger?: Logger) => void;
267
- };
268
- };
166
+ onStartError?: (err: unknown, logger: Logger) => void;
167
+ }
269
168
  /**
270
169
  * utils for startuping with updater
271
170
  * @param fn startup function
@@ -299,6 +198,6 @@ declare function startupWithUpdater(fn: (updater: Updater) => Promisable<void>):
299
198
  * })
300
199
  * ```
301
200
  */
302
- declare function initApp(appOptions?: AppOption): Promise<void>;
201
+ declare function initApp(appOptions: AppOption): Promise<void>;
303
202
 
304
- export { type AppOption, type CheckResult, type DownloadResult, type DownloadingInfo, ErrorInfo, type Func, type Logger, Updater, type UpdaterDownloadConfig, UpdaterError, type UpdaterOption, type UpdaterOverrideFunctions, createUpdater, downloadBufferDefault, downloadJSONDefault, initApp, isLowerVersionDefault, startupWithUpdater };
203
+ export { type AppOption, type CheckResult, type DownloadResult, ErrorInfo, type Logger, Updater, UpdaterError, type UpdaterOption, initApp, startupWithUpdater };