electron-incremental-update 2.0.0-beta.1 → 2.0.0-beta.11

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.cts CHANGED
@@ -1,35 +1,18 @@
1
- import { U as UpdateJSON } from './version-CffZWDhZ.cjs';
2
- import { O as OnDownloading, U as URLHandler, I as IProvider } from './types-DxPmQmaq.cjs';
3
- import '@subframe7536/type-utils';
1
+ import { EventEmitter } from 'node:events';
2
+ import { U as UpdateInfo, a as UpdateJSON } from './version-BYVQ367i.cjs';
3
+ import { I as IProvider, D as DownloadingInfo } from './types-CGSkHX4Y.cjs';
4
+ import { Promisable } from '@subframe7536/type-utils';
4
5
 
5
6
  declare const ErrorInfo: {
6
- readonly download: "Download failed";
7
- readonly validate: "Validate failed";
8
- readonly param: "Missing params";
9
- readonly version: "Unsatisfied version";
7
+ readonly download: "Download Failed";
8
+ readonly validate: "Validate Failed";
9
+ readonly param: "Missing Params";
10
+ readonly network: "Network Error";
10
11
  };
11
12
  declare class UpdaterError extends Error {
12
- constructor(msg: typeof ErrorInfo[keyof typeof ErrorInfo], info: string);
13
+ code: keyof typeof ErrorInfo;
14
+ constructor(msg: keyof typeof ErrorInfo, info: string);
13
15
  }
14
- type CheckResult<T extends UpdateJSON> = {
15
- success: true;
16
- data: Omit<T, 'beta'>;
17
- } | {
18
- success: false;
19
- /**
20
- * minimal version that can update
21
- */
22
- data: string;
23
- } | {
24
- success: false;
25
- data: UpdaterError;
26
- };
27
- type DownloadResult = {
28
- success: true;
29
- } | {
30
- success: false;
31
- data: UpdaterError;
32
- };
33
16
  interface Logger {
34
17
  info: (msg: string) => void;
35
18
  debug: (msg: string) => void;
@@ -38,143 +21,136 @@ interface Logger {
38
21
  }
39
22
  interface UpdaterOption {
40
23
  /**
41
- * public key of signature, which will be auto generated by plugin,
24
+ * Update provider
25
+ *
26
+ * If you will not setup `UpdateJSON` or `Buffer` in params when checking update or download, this option is **required**
27
+ */
28
+ provider?: IProvider;
29
+ /**
30
+ * Certifaction key of signature, which will be auto generated by plugin,
42
31
  * generate by `selfsigned` if not set
43
32
  */
44
33
  SIGNATURE_CERT?: string;
45
34
  /**
46
- * whether to receive beta update
35
+ * Whether to receive beta update
47
36
  */
48
37
  receiveBeta?: boolean;
38
+ /**
39
+ * Updater logger
40
+ */
49
41
  logger?: Logger;
50
42
  }
51
43
 
52
- declare class Updater {
44
+ declare class Updater extends EventEmitter<{
45
+ 'checking': any;
46
+ 'update-available': [data: UpdateInfo];
47
+ 'update-not-available': [reason: string];
48
+ 'error': [error: UpdaterError];
49
+ 'download-progress': [info: DownloadingInfo];
50
+ 'update-downloaded': any;
51
+ 'update-cancelled': any;
52
+ }> {
53
53
  private CERT;
54
+ private controller;
54
55
  private info?;
55
- private options;
56
- private asarPath;
57
- private gzipPath;
58
- private tmpFilePath;
59
- private provider;
56
+ provider?: IProvider;
60
57
  /**
61
- * updater logger
58
+ * Updater logger
62
59
  */
63
60
  logger?: Logger;
64
61
  /**
65
- * downloading progress hook
66
- * @param progress download progress
67
- * @example
68
- * updater.onDownloading = ({ percent, total, current }) => {
69
- * console.log(`download progress: ${percent}, total: ${total}, current: ${current}`)
70
- * }
71
- */
72
- onDownloading?: OnDownloading;
73
- /**
74
- * URL handler hook
75
- *
76
- * for Github, there are some {@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L34 public CDN links}
77
- * @param url source url
78
- * @param isDownloadAsar whether is download asar
62
+ * Whether to receive beta update
79
63
  */
80
- handleURL?: URLHandler;
64
+ receiveBeta?: boolean;
81
65
  /**
82
- * whether receive beta version
66
+ * Whether force update in DEV
83
67
  */
84
- get receiveBeta(): boolean;
85
- set receiveBeta(receiveBeta: boolean);
68
+ forceUpdate?: boolean;
86
69
  /**
87
- * initialize incremental updater
88
- * @param provider update provider
89
- * @param option UpdaterOption
70
+ * Initialize incremental updater
71
+ * @param options UpdaterOption
90
72
  */
91
- constructor(provider: IProvider, option?: UpdaterOption);
92
- private needUpdate;
73
+ constructor(options?: UpdaterOption);
93
74
  /**
94
- * this function is used to parse download data.
95
- * - if format is `'json'`
96
- * - if data is `UpdateJSON`, return it
97
- * - if data is string or absent, download URL data and return it
98
- * - if format is `'buffer'`
99
- * - if data is `Buffer`, return it
100
- * - if data is string or absent, download URL data and return it
101
- * @param format 'json' or 'buffer'
102
- * @param data download URL or update json or buffer
75
+ * This function is used to parse download data.
76
+ *
77
+ * if data is absent, download URL from provider and return it,
78
+ * else if data is `UpdateJSON`, return it
103
79
  */
104
- private parseData;
80
+ private fetch;
105
81
  /**
106
- * check update info using default options
82
+ * Handle error message and emit error event
107
83
  */
108
- checkUpdate<T extends UpdateJSON>(): Promise<CheckResult<T>>;
84
+ private err;
109
85
  /**
110
- * check update info using custom url
111
- * @param url custom download URL of `updatejson`
86
+ * Check update info using default options
112
87
  */
113
- checkUpdate<T extends UpdateJSON>(url: string): Promise<CheckResult<T>>;
88
+ checkForUpdates(): Promise<boolean>;
114
89
  /**
115
- * check update info using existing update json
90
+ * Check update info using existing update json
116
91
  * @param data existing update json
117
92
  */
118
- checkUpdate<T extends UpdateJSON>(data: T): Promise<CheckResult<T>>;
93
+ checkForUpdates(data: UpdateJSON): Promise<boolean>;
119
94
  /**
120
- * download update using default options
95
+ * Download update using default options
121
96
  */
122
- download(): Promise<DownloadResult>;
97
+ downloadUpdate(): Promise<boolean>;
123
98
  /**
124
- * download update using custom url
125
- * @param url custom download URL
126
- */
127
- download(url: string): Promise<DownloadResult>;
128
- /**
129
- * download update using existing `asar.gz` buffer and signature
99
+ * Download update using existing `asar.gz` buffer and signature
130
100
  * @param data existing `asar.gz` buffer
131
- * @param sig signature
101
+ * @param info update info
132
102
  */
133
- download(data: Buffer, sig: string): Promise<DownloadResult>;
103
+ downloadUpdate(data: Uint8Array, info: Omit<UpdateInfo, 'minimumVersion'>): Promise<boolean>;
134
104
  /**
135
105
  * quit App and install
136
106
  */
137
107
  quitAndInstall(): void;
108
+ cancel(): void;
138
109
  }
110
+ /**
111
+ * Auto check update, download and install
112
+ */
113
+ declare function autoUpdate(updater: Updater): Promise<void>;
139
114
 
140
- type Promisable<T> = T | Promise<T>;
141
115
  /**
142
- * hooks on rename temp asar path to `${app.name}.asar`
116
+ * Hooks on rename temp asar path to `${app.name}.asar`
143
117
  * @param install `() => renameSync(tempAsarPath, appNameAsarPath)`
144
118
  * @param tempAsarPath temp(updated) asar path
145
119
  * @param appNameAsarPath `${app.name}.asar` path
146
120
  * @param logger logger
147
121
  * @default install(); logger.info(`update success!`)
148
122
  */
149
- type OnInstallFunction = (install: VoidFunction, tempAsarPath: string, appNameAsarPath: string, logger: Logger) => Promisable<void>;
123
+ type OnInstallFunction = (install: VoidFunction, tempAsarPath: string, appNameAsarPath: string, logger?: Logger) => Promisable<void>;
150
124
  interface AppOption {
151
125
  /**
152
- * update provider
126
+ * Path to index file that make {@link startupWithUpdater} as default export
127
+ *
128
+ * Generate from plugin configuration by default
153
129
  */
154
- provider: IProvider;
130
+ mainPath?: string;
155
131
  /**
156
- * updater options
132
+ * Updater options
157
133
  */
158
134
  updater?: (() => Promisable<Updater>) | UpdaterOption;
159
135
  /**
160
- * hooks on rename temp asar path to `${app.name}.asar`
136
+ * Hooks on rename temp asar path to `${app.name}.asar`
161
137
  */
162
138
  onInstall?: OnInstallFunction;
163
139
  /**
164
- * hooks before app start up
140
+ * Hooks before app startup
165
141
  * @param mainFilePath main file path of `${app.name}.asar`
166
142
  * @param logger logger
167
143
  */
168
- beforeStart?: (mainFilePath: string, logger: Logger) => Promisable<void>;
144
+ beforeStart?: (mainFilePath: string, logger?: Logger) => Promisable<void>;
169
145
  /**
170
- * hooks on app start up error
146
+ * Hooks on app startup error
171
147
  * @param err installing or startup error
172
148
  * @param logger logger
173
149
  */
174
- onStartError?: (err: unknown, logger: Logger) => void;
150
+ onStartError?: (err: unknown, logger?: Logger) => void;
175
151
  }
176
152
  /**
177
- * utils for startuping with updater
153
+ * Utils to startup with updater
178
154
  * @param fn startup function
179
155
  * @example
180
156
  * // in electron/main/index.ts
@@ -184,28 +160,24 @@ interface AppOption {
184
160
  */
185
161
  declare function startupWithUpdater(fn: (updater: Updater) => Promisable<void>): (updater: Updater) => Promisable<void>;
186
162
  /**
187
- * initialize app
163
+ * Initialize Electron with updater
188
164
  * @example
189
- * ```ts
190
- * import { getGithubReleaseCdnGroup, initApp, parseGithubCdnURL } from 'electron-incremental-update'
191
- * import { repository } from '../package.json'
192
- *
193
- * const { cdnPrefix: asarPrefix } = getGithubReleaseCdnGroup()[0]
194
- * const { cdnPrefix: jsonPrefix } = getGithubFileCdnGroup()[0]
195
- *
196
- * initApp({
197
- * // can be updater option or function that return updater
165
+ * createElectronApp({
198
166
  * updater: {
199
- * SIGNATURE_CERT: 'custom certificate',
200
- * repository,
201
- * updateJsonURL: parseGithubCdnURL(repository, jsonPrefix, 'version.json'),
202
- * releaseAsarURL: parseGithubCdnURL(repository, asarPrefix, `download/latest/${app.name}.asar.gz`),
203
- * receiveBeta: true,
167
+ * provider: new GitHubProvider({
168
+ * username: 'yourname',
169
+ * repo: 'electron',
170
+ * }),
171
+ * },
172
+ * beforeStart(mainFilePath, logger) {
173
+ * logger?.debug(mainFilePath)
204
174
  * },
205
- * onStart: console.log
206
175
  * })
207
- * ```
208
176
  */
209
- declare function initApp(appOptions: AppOption): Promise<void>;
177
+ declare function createElectronApp(appOptions?: AppOption): Promise<void>;
178
+ /**
179
+ * @alias {@link createElectronApp}
180
+ */
181
+ declare const initApp: typeof createElectronApp;
210
182
 
211
- export { type AppOption, type CheckResult, type DownloadResult, ErrorInfo, type Logger, Updater, UpdaterError, type UpdaterOption, initApp, startupWithUpdater };
183
+ export { type AppOption, ErrorInfo, type Logger, Updater, UpdaterError, type UpdaterOption, autoUpdate, createElectronApp, initApp, startupWithUpdater };
package/dist/index.d.ts CHANGED
@@ -1,35 +1,18 @@
1
- import { U as UpdateJSON } from './version-CffZWDhZ.js';
2
- import { O as OnDownloading, U as URLHandler, I as IProvider } from './types-seJf3Wbc.js';
3
- import '@subframe7536/type-utils';
1
+ import { EventEmitter } from 'node:events';
2
+ import { U as UpdateInfo, a as UpdateJSON } from './version-BYVQ367i.js';
3
+ import { I as IProvider, D as DownloadingInfo } from './types-C8JhnJjU.js';
4
+ import { Promisable } from '@subframe7536/type-utils';
4
5
 
5
6
  declare const ErrorInfo: {
6
- readonly download: "Download failed";
7
- readonly validate: "Validate failed";
8
- readonly param: "Missing params";
9
- readonly version: "Unsatisfied version";
7
+ readonly download: "Download Failed";
8
+ readonly validate: "Validate Failed";
9
+ readonly param: "Missing Params";
10
+ readonly network: "Network Error";
10
11
  };
11
12
  declare class UpdaterError extends Error {
12
- constructor(msg: typeof ErrorInfo[keyof typeof ErrorInfo], info: string);
13
+ code: keyof typeof ErrorInfo;
14
+ constructor(msg: keyof typeof ErrorInfo, info: string);
13
15
  }
14
- type CheckResult<T extends UpdateJSON> = {
15
- success: true;
16
- data: Omit<T, 'beta'>;
17
- } | {
18
- success: false;
19
- /**
20
- * minimal version that can update
21
- */
22
- data: string;
23
- } | {
24
- success: false;
25
- data: UpdaterError;
26
- };
27
- type DownloadResult = {
28
- success: true;
29
- } | {
30
- success: false;
31
- data: UpdaterError;
32
- };
33
16
  interface Logger {
34
17
  info: (msg: string) => void;
35
18
  debug: (msg: string) => void;
@@ -38,143 +21,136 @@ interface Logger {
38
21
  }
39
22
  interface UpdaterOption {
40
23
  /**
41
- * public key of signature, which will be auto generated by plugin,
24
+ * Update provider
25
+ *
26
+ * If you will not setup `UpdateJSON` or `Buffer` in params when checking update or download, this option is **required**
27
+ */
28
+ provider?: IProvider;
29
+ /**
30
+ * Certifaction key of signature, which will be auto generated by plugin,
42
31
  * generate by `selfsigned` if not set
43
32
  */
44
33
  SIGNATURE_CERT?: string;
45
34
  /**
46
- * whether to receive beta update
35
+ * Whether to receive beta update
47
36
  */
48
37
  receiveBeta?: boolean;
38
+ /**
39
+ * Updater logger
40
+ */
49
41
  logger?: Logger;
50
42
  }
51
43
 
52
- declare class Updater {
44
+ declare class Updater extends EventEmitter<{
45
+ 'checking': any;
46
+ 'update-available': [data: UpdateInfo];
47
+ 'update-not-available': [reason: string];
48
+ 'error': [error: UpdaterError];
49
+ 'download-progress': [info: DownloadingInfo];
50
+ 'update-downloaded': any;
51
+ 'update-cancelled': any;
52
+ }> {
53
53
  private CERT;
54
+ private controller;
54
55
  private info?;
55
- private options;
56
- private asarPath;
57
- private gzipPath;
58
- private tmpFilePath;
59
- private provider;
56
+ provider?: IProvider;
60
57
  /**
61
- * updater logger
58
+ * Updater logger
62
59
  */
63
60
  logger?: Logger;
64
61
  /**
65
- * downloading progress hook
66
- * @param progress download progress
67
- * @example
68
- * updater.onDownloading = ({ percent, total, current }) => {
69
- * console.log(`download progress: ${percent}, total: ${total}, current: ${current}`)
70
- * }
71
- */
72
- onDownloading?: OnDownloading;
73
- /**
74
- * URL handler hook
75
- *
76
- * for Github, there are some {@link https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L34 public CDN links}
77
- * @param url source url
78
- * @param isDownloadAsar whether is download asar
62
+ * Whether to receive beta update
79
63
  */
80
- handleURL?: URLHandler;
64
+ receiveBeta?: boolean;
81
65
  /**
82
- * whether receive beta version
66
+ * Whether force update in DEV
83
67
  */
84
- get receiveBeta(): boolean;
85
- set receiveBeta(receiveBeta: boolean);
68
+ forceUpdate?: boolean;
86
69
  /**
87
- * initialize incremental updater
88
- * @param provider update provider
89
- * @param option UpdaterOption
70
+ * Initialize incremental updater
71
+ * @param options UpdaterOption
90
72
  */
91
- constructor(provider: IProvider, option?: UpdaterOption);
92
- private needUpdate;
73
+ constructor(options?: UpdaterOption);
93
74
  /**
94
- * this function is used to parse download data.
95
- * - if format is `'json'`
96
- * - if data is `UpdateJSON`, return it
97
- * - if data is string or absent, download URL data and return it
98
- * - if format is `'buffer'`
99
- * - if data is `Buffer`, return it
100
- * - if data is string or absent, download URL data and return it
101
- * @param format 'json' or 'buffer'
102
- * @param data download URL or update json or buffer
75
+ * This function is used to parse download data.
76
+ *
77
+ * if data is absent, download URL from provider and return it,
78
+ * else if data is `UpdateJSON`, return it
103
79
  */
104
- private parseData;
80
+ private fetch;
105
81
  /**
106
- * check update info using default options
82
+ * Handle error message and emit error event
107
83
  */
108
- checkUpdate<T extends UpdateJSON>(): Promise<CheckResult<T>>;
84
+ private err;
109
85
  /**
110
- * check update info using custom url
111
- * @param url custom download URL of `updatejson`
86
+ * Check update info using default options
112
87
  */
113
- checkUpdate<T extends UpdateJSON>(url: string): Promise<CheckResult<T>>;
88
+ checkForUpdates(): Promise<boolean>;
114
89
  /**
115
- * check update info using existing update json
90
+ * Check update info using existing update json
116
91
  * @param data existing update json
117
92
  */
118
- checkUpdate<T extends UpdateJSON>(data: T): Promise<CheckResult<T>>;
93
+ checkForUpdates(data: UpdateJSON): Promise<boolean>;
119
94
  /**
120
- * download update using default options
95
+ * Download update using default options
121
96
  */
122
- download(): Promise<DownloadResult>;
97
+ downloadUpdate(): Promise<boolean>;
123
98
  /**
124
- * download update using custom url
125
- * @param url custom download URL
126
- */
127
- download(url: string): Promise<DownloadResult>;
128
- /**
129
- * download update using existing `asar.gz` buffer and signature
99
+ * Download update using existing `asar.gz` buffer and signature
130
100
  * @param data existing `asar.gz` buffer
131
- * @param sig signature
101
+ * @param info update info
132
102
  */
133
- download(data: Buffer, sig: string): Promise<DownloadResult>;
103
+ downloadUpdate(data: Uint8Array, info: Omit<UpdateInfo, 'minimumVersion'>): Promise<boolean>;
134
104
  /**
135
105
  * quit App and install
136
106
  */
137
107
  quitAndInstall(): void;
108
+ cancel(): void;
138
109
  }
110
+ /**
111
+ * Auto check update, download and install
112
+ */
113
+ declare function autoUpdate(updater: Updater): Promise<void>;
139
114
 
140
- type Promisable<T> = T | Promise<T>;
141
115
  /**
142
- * hooks on rename temp asar path to `${app.name}.asar`
116
+ * Hooks on rename temp asar path to `${app.name}.asar`
143
117
  * @param install `() => renameSync(tempAsarPath, appNameAsarPath)`
144
118
  * @param tempAsarPath temp(updated) asar path
145
119
  * @param appNameAsarPath `${app.name}.asar` path
146
120
  * @param logger logger
147
121
  * @default install(); logger.info(`update success!`)
148
122
  */
149
- type OnInstallFunction = (install: VoidFunction, tempAsarPath: string, appNameAsarPath: string, logger: Logger) => Promisable<void>;
123
+ type OnInstallFunction = (install: VoidFunction, tempAsarPath: string, appNameAsarPath: string, logger?: Logger) => Promisable<void>;
150
124
  interface AppOption {
151
125
  /**
152
- * update provider
126
+ * Path to index file that make {@link startupWithUpdater} as default export
127
+ *
128
+ * Generate from plugin configuration by default
153
129
  */
154
- provider: IProvider;
130
+ mainPath?: string;
155
131
  /**
156
- * updater options
132
+ * Updater options
157
133
  */
158
134
  updater?: (() => Promisable<Updater>) | UpdaterOption;
159
135
  /**
160
- * hooks on rename temp asar path to `${app.name}.asar`
136
+ * Hooks on rename temp asar path to `${app.name}.asar`
161
137
  */
162
138
  onInstall?: OnInstallFunction;
163
139
  /**
164
- * hooks before app start up
140
+ * Hooks before app startup
165
141
  * @param mainFilePath main file path of `${app.name}.asar`
166
142
  * @param logger logger
167
143
  */
168
- beforeStart?: (mainFilePath: string, logger: Logger) => Promisable<void>;
144
+ beforeStart?: (mainFilePath: string, logger?: Logger) => Promisable<void>;
169
145
  /**
170
- * hooks on app start up error
146
+ * Hooks on app startup error
171
147
  * @param err installing or startup error
172
148
  * @param logger logger
173
149
  */
174
- onStartError?: (err: unknown, logger: Logger) => void;
150
+ onStartError?: (err: unknown, logger?: Logger) => void;
175
151
  }
176
152
  /**
177
- * utils for startuping with updater
153
+ * Utils to startup with updater
178
154
  * @param fn startup function
179
155
  * @example
180
156
  * // in electron/main/index.ts
@@ -184,28 +160,24 @@ interface AppOption {
184
160
  */
185
161
  declare function startupWithUpdater(fn: (updater: Updater) => Promisable<void>): (updater: Updater) => Promisable<void>;
186
162
  /**
187
- * initialize app
163
+ * Initialize Electron with updater
188
164
  * @example
189
- * ```ts
190
- * import { getGithubReleaseCdnGroup, initApp, parseGithubCdnURL } from 'electron-incremental-update'
191
- * import { repository } from '../package.json'
192
- *
193
- * const { cdnPrefix: asarPrefix } = getGithubReleaseCdnGroup()[0]
194
- * const { cdnPrefix: jsonPrefix } = getGithubFileCdnGroup()[0]
195
- *
196
- * initApp({
197
- * // can be updater option or function that return updater
165
+ * createElectronApp({
198
166
  * updater: {
199
- * SIGNATURE_CERT: 'custom certificate',
200
- * repository,
201
- * updateJsonURL: parseGithubCdnURL(repository, jsonPrefix, 'version.json'),
202
- * releaseAsarURL: parseGithubCdnURL(repository, asarPrefix, `download/latest/${app.name}.asar.gz`),
203
- * receiveBeta: true,
167
+ * provider: new GitHubProvider({
168
+ * username: 'yourname',
169
+ * repo: 'electron',
170
+ * }),
171
+ * },
172
+ * beforeStart(mainFilePath, logger) {
173
+ * logger?.debug(mainFilePath)
204
174
  * },
205
- * onStart: console.log
206
175
  * })
207
- * ```
208
176
  */
209
- declare function initApp(appOptions: AppOption): Promise<void>;
177
+ declare function createElectronApp(appOptions?: AppOption): Promise<void>;
178
+ /**
179
+ * @alias {@link createElectronApp}
180
+ */
181
+ declare const initApp: typeof createElectronApp;
210
182
 
211
- export { type AppOption, type CheckResult, type DownloadResult, ErrorInfo, type Logger, Updater, UpdaterError, type UpdaterOption, initApp, startupWithUpdater };
183
+ export { type AppOption, ErrorInfo, type Logger, Updater, UpdaterError, type UpdaterOption, autoUpdate, createElectronApp, initApp, startupWithUpdater };