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

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,148 +1,75 @@
1
- import { U as UpdateInfo, a as UpdateJSON } from './pure-GoN_3MEj.js';
1
+ import { EventEmitter } from 'node:events';
2
+ import { U as UpdateInfo, a as UpdateJSON } from './version-C4tF_trh.js';
3
+ import { I as IProvider, D as DownloadingInfo } from './types-Bnc4jz6R.js';
4
+ import '@subframe7536/type-utils';
2
5
 
3
6
  declare const ErrorInfo: {
4
- readonly downlaod: "Download failed";
5
- readonly validate: "Validate failed";
6
- readonly param: "Missing params";
7
- readonly version: "Unsatisfied version";
7
+ readonly download: "Download Failed";
8
+ readonly validate: "Validate Failed";
9
+ readonly param: "Missing Params";
10
+ readonly network: "Network Error";
8
11
  };
9
12
  declare class UpdaterError extends Error {
10
- constructor(msg: typeof ErrorInfo[keyof typeof ErrorInfo], info: string);
13
+ code: keyof typeof ErrorInfo;
14
+ constructor(msg: keyof typeof ErrorInfo, info: string);
11
15
  }
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;
27
- };
28
- type Logger = {
16
+ interface Logger {
29
17
  info: (msg: string) => void;
30
18
  debug: (msg: string) => void;
31
19
  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 = {
20
+ error: (msg: string, e?: unknown) => void;
21
+ }
22
+ interface UpdaterOption {
68
23
  /**
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'
24
+ * Update provider, call setup later
71
25
  */
72
- userAgent?: string;
26
+ provider?: IProvider;
73
27
  /**
74
- * extra download header, `accept` and `user-agent` is set by default
75
- */
76
- extraHeader?: Record<string, string>;
77
- };
78
- interface UpdaterOption {
79
- /**
80
- * public key of signature, which will be auto generated by plugin,
28
+ * Certifaction key of signature, which will be auto generated by plugin,
81
29
  * generate by `selfsigned` if not set
82
30
  */
83
31
  SIGNATURE_CERT?: string;
84
32
  /**
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
33
+ * Whether to receive beta update
103
34
  */
104
- releaseAsarURL?: string;
35
+ receiveBeta?: boolean;
105
36
  /**
106
- * whether to receive beta update
37
+ * Updater logger
107
38
  */
108
- receiveBeta?: boolean;
109
- overrideFunctions?: UpdaterOverrideFunctions;
110
- downloadConfig?: UpdaterDownloadConfig;
39
+ logger?: Logger;
111
40
  }
112
41
 
113
- declare class Updater {
42
+ declare class Updater extends EventEmitter<{
43
+ 'checking': any;
44
+ 'update-available': [data: UpdateInfo];
45
+ 'update-unavailable': [reason: string];
46
+ 'error': [error: UpdaterError];
47
+ 'download-progress': [info: DownloadingInfo];
48
+ 'update-downloaded': any;
49
+ }> {
114
50
  private CERT;
115
51
  private info?;
116
- private option;
117
- private asarPath;
118
- private gzipPath;
119
- private tmpFilePath;
52
+ provider?: IProvider;
120
53
  /**
121
- * updater logger
54
+ * Updater logger
122
55
  */
123
56
  logger?: Logger;
124
57
  /**
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
- * }
58
+ * Whether to receive beta update
131
59
  */
132
- onDownloading?: (progress: DownloadingInfo) => void;
60
+ receiveBeta?: boolean;
133
61
  /**
134
- * whether receive beta version
62
+ * Whether force update in DEV
135
63
  */
136
- get receiveBeta(): boolean;
137
- set receiveBeta(receiveBeta: boolean);
64
+ forceUpdate?: boolean;
138
65
  /**
139
- * initialize incremental updater
140
- * @param option UpdaterOption
66
+ * Initialize incremental updater
67
+ * @param options UpdaterOption
141
68
  */
142
- constructor(option?: UpdaterOption);
143
- private needUpdate;
69
+ constructor(options?: UpdaterOption);
70
+ private checkProvider;
144
71
  /**
145
- * this function is used to parse download data.
72
+ * This function is used to parse download data.
146
73
  * - if format is `'json'`
147
74
  * - if data is `UpdateJSON`, return it
148
75
  * - if data is string or absent, download URL data and return it
@@ -152,122 +79,74 @@ declare class Updater {
152
79
  * @param format 'json' or 'buffer'
153
80
  * @param data download URL or update json or buffer
154
81
  */
155
- private parseData;
82
+ private fetch;
156
83
  /**
157
- * check update info using default options
158
- * @returns
159
- * - Available: `{size: number, version: string}`
160
- * - Unavailable: `undefined`
161
- * - Fail: `UpdaterError`
84
+ * Handle error message and emit error event
162
85
  */
163
- checkUpdate(): Promise<CheckResult>;
86
+ private err;
164
87
  /**
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`
88
+ * Check update info using default options
171
89
  */
172
- checkUpdate(url: string): Promise<CheckResult>;
90
+ checkUpdate(): Promise<boolean>;
173
91
  /**
174
- * check update info using existing update json
92
+ * Check update info using existing update json
175
93
  * @param data existing update json
176
- * @returns
177
- * - Available:`{size: number, version: string}`
178
- * - Unavailable: `undefined`
179
- * - Fail: `UpdaterError`
180
94
  */
181
- checkUpdate(data: UpdateJSON): Promise<CheckResult>;
95
+ checkUpdate(data: UpdateJSON): Promise<boolean>;
182
96
  /**
183
- * download update using default options
184
- * @returns
185
- * - Success: `true`
186
- * - Fail: `UpdaterError`
97
+ * Download update using default options
187
98
  */
188
- download(): Promise<DownloadResult>;
99
+ downloadUpdate(): Promise<boolean>;
189
100
  /**
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>;
197
- /**
198
- * download update using existing `asar.gz` buffer and signature
101
+ * Download update using existing `asar.gz` buffer and signature
199
102
  * @param data existing `asar.gz` buffer
200
- * @param sig signature
201
- * @returns
202
- * - Success: `true`
203
- * - Fail: `UpdaterError`
103
+ * @param info update info
204
104
  */
205
- download(data: Buffer, sig: string): Promise<DownloadResult>;
105
+ downloadUpdate(data: Uint8Array, info: Omit<UpdateInfo, 'minimumVersion'>): Promise<boolean>;
206
106
  /**
207
107
  * quit App and install
208
108
  */
209
109
  quitAndInstall(): void;
210
110
  }
211
-
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
-
218
111
  /**
219
- * create updater instance
220
- * @param option updater option
221
- * @returns updater
112
+ * Auto check update, download and install
222
113
  */
223
- declare function createUpdater(option?: UpdaterOption): Updater;
114
+ declare function autoUpdate(updater: Updater): Promise<void>;
224
115
 
225
116
  type Promisable<T> = T | Promise<T>;
117
+ /**
118
+ * Hooks on rename temp asar path to `${app.name}.asar`
119
+ * @param install `() => renameSync(tempAsarPath, appNameAsarPath)`
120
+ * @param tempAsarPath temp(updated) asar path
121
+ * @param appNameAsarPath `${app.name}.asar` path
122
+ * @param logger logger
123
+ * @default install(); logger.info(`update success!`)
124
+ */
226
125
  type OnInstallFunction = (install: VoidFunction, tempAsarPath: string, appNameAsarPath: string, logger?: Logger) => Promisable<void>;
227
- type AppOption = {
126
+ interface AppOption {
228
127
  /**
229
- * updater options
128
+ * Updater options
230
129
  */
231
130
  updater?: (() => Promisable<Updater>) | UpdaterOption;
232
131
  /**
233
- * path of electron output dist when in development
234
- * @default '../dist-electron'
132
+ * Hooks on rename temp asar path to `${app.name}.asar`
235
133
  */
236
- electronDevDistPath?: string;
134
+ onInstall?: OnInstallFunction;
237
135
  /**
238
- * relative path of main entry in electron dist
239
- * @default 'main/index.js'
136
+ * Hooks before app start up
137
+ * @param mainFilePath main file path of `${app.name}.asar`
138
+ * @param logger logger
240
139
  */
241
- mainPath?: string;
140
+ beforeStart?: (mainFilePath: string, logger?: Logger) => Promisable<void>;
242
141
  /**
243
- * update hooks
142
+ * Hooks on app start up error
143
+ * @param err installing or startup error
144
+ * @param logger logger
244
145
  */
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
- };
146
+ onStartError?: (err: unknown, logger?: Logger) => void;
147
+ }
269
148
  /**
270
- * utils for startuping with updater
149
+ * Utils to startup with updater
271
150
  * @param fn startup function
272
151
  * @example
273
152
  * // in electron/main/index.ts
@@ -279,26 +158,18 @@ declare function startupWithUpdater(fn: (updater: Updater) => Promisable<void>):
279
158
  /**
280
159
  * initialize app
281
160
  * @example
282
- * ```ts
283
- * import { getGithubReleaseCdnGroup, initApp, parseGithubCdnURL } from 'electron-incremental-update'
284
- * import { repository } from '../package.json'
285
- *
286
- * const { cdnPrefix: asarPrefix } = getGithubReleaseCdnGroup()[0]
287
- * const { cdnPrefix: jsonPrefix } = getGithubFileCdnGroup()[0]
288
- *
289
161
  * initApp({
290
- * // can be updater option or function that return updater
291
162
  * updater: {
292
- * SIGNATURE_CERT: 'custom certificate',
293
- * repository,
294
- * updateJsonURL: parseGithubCdnURL(repository, jsonPrefix, 'version.json'),
295
- * releaseAsarURL: parseGithubCdnURL(repository, asarPrefix, `download/latest/${app.name}.asar.gz`),
296
- * receiveBeta: true,
163
+ * provider: new GitHubProvider({
164
+ * username: 'jerry7536',
165
+ * repo: 'electron2',
166
+ * }),
167
+ * },
168
+ * beforeStart(mainFilePath, logger) {
169
+ * logger?.debug(mainFilePath)
297
170
  * },
298
- * onStart: console.log
299
171
  * })
300
- * ```
301
172
  */
302
173
  declare function initApp(appOptions?: AppOption): Promise<void>;
303
174
 
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 };
175
+ export { type AppOption, ErrorInfo, type Logger, Updater, UpdaterError, type UpdaterOption, autoUpdate, initApp, startupWithUpdater };