electron-incremental-update 0.7.6 → 0.7.8

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.
@@ -31,7 +31,14 @@ var verify = (buffer, signature2, cert) => {
31
31
  }
32
32
  };
33
33
 
34
+ // src/updateJson.ts
35
+ function isUpdateJSON(json) {
36
+ const is = (j) => "signature" in j && "version" in j && "size" in j && "minimumVersion" in j;
37
+ return is(json) && "beta" in json && is(json.beta);
38
+ }
39
+
34
40
  export {
35
41
  signature,
36
- verify
42
+ verify,
43
+ isUpdateJSON
37
44
  };
@@ -20,9 +20,20 @@ function getEntryVersion() {
20
20
  function getProductVersion(name) {
21
21
  return app.isPackaged ? readFileSync(join(getProductAsarPath(name), "version"), "utf-8") : getEntryVersion();
22
22
  }
23
+ var NoSuchNativeModuleError = class extends Error {
24
+ moduleName;
25
+ constructor(moduleName) {
26
+ super(`no such native module: ${moduleName}`);
27
+ this.moduleName = moduleName;
28
+ }
29
+ };
23
30
  function requireNative(packageName) {
24
31
  const path = app.isPackaged ? join(app.getAppPath(), "node_modules", packageName) : packageName;
25
- return __require(path);
32
+ try {
33
+ return __require(path);
34
+ } catch (error) {
35
+ throw new NoSuchNativeModuleError(packageName);
36
+ }
26
37
  }
27
38
  function parseGithubCdnURL(repository, cdnPrefix, relativeFilePath) {
28
39
  if (!repository.startsWith("https://github.com/")) {
@@ -114,12 +125,25 @@ function handleUnexpectedErrors(callback) {
114
125
  process.on("uncaughtException", listener);
115
126
  process.on("unhandledRejection", listener);
116
127
  }
128
+ function parseVersion(version) {
129
+ const semver = /^(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z0-9\.-]+))?/i;
130
+ const match = semver.exec(version);
131
+ if (!match) {
132
+ throw new TypeError(`invalid version: ${version}`);
133
+ }
134
+ const [major, minor, patch] = match.slice(1, 4).map(Number);
135
+ if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
136
+ throw new TypeError(`invalid version: ${version}`);
137
+ }
138
+ return { major, minor, patch, stage: match[4] };
139
+ }
117
140
 
118
141
  export {
119
142
  __require,
120
143
  getProductAsarPath,
121
144
  getEntryVersion,
122
145
  getProductVersion,
146
+ NoSuchNativeModuleError,
123
147
  requireNative,
124
148
  parseGithubCdnURL,
125
149
  getGithubFileCdnGroup,
@@ -128,5 +152,6 @@ export {
128
152
  waitAppReady,
129
153
  unzipFile,
130
154
  zipFile,
131
- handleUnexpectedErrors
155
+ handleUnexpectedErrors,
156
+ parseVersion
132
157
  };
package/dist/index.d.mts CHANGED
@@ -1,32 +1,47 @@
1
1
  import { Buffer } from 'node:buffer';
2
+ import { U as UpdateJSON } from './updateJson-7e45d9e1.js';
2
3
 
3
- type CheckResultType = Omit<UpdateJSON, 'signature'> | undefined | Error;
4
- type InstallResult = true | Error;
4
+ declare class MinimumVersionError extends Error {
5
+ currentVersion: string;
6
+ minVersion: string;
7
+ constructor(version: string, minimumVersion: string);
8
+ }
9
+ declare class VerifyFailedError extends Error {
10
+ signature: string;
11
+ cert: string;
12
+ constructor(signature: string, cert: string);
13
+ }
14
+ /**
15
+ * Creates an updater based on the provided options
16
+ */
17
+ declare function createUpdater(updaterOptions: UpdaterOption): Updater;
18
+
19
+ type CheckResultType = {
20
+ size: number;
21
+ version: string;
22
+ } | undefined | Error | MinimumVersionError | TypeError;
23
+ type DownloadResult = true | Error | VerifyFailedError | TypeError;
5
24
  type UpdateEvents = {
6
25
  downloading: [progress: number];
26
+ downloadBuffer: [buffer: Buffer];
7
27
  debug: [msg: string | Error];
8
28
  };
9
- type UpdateJSON = {
10
- signature: string;
11
- version: string;
12
- size: number;
13
- };
14
- type MaybeArray<T> = T extends undefined | null | never ? [] : T extends any[] ? T['length'] extends 1 ? [data: T[0]] : T : [data: T];
15
- interface TypedUpdater<T extends Record<string | symbol, MaybeArray<any>>, Event extends Exclude<keyof T, number> = Exclude<keyof T, number>> {
16
- removeAllListeners<E extends Event>(event?: E): this;
17
- listeners<E extends Event>(eventName: E): Function[];
18
- eventNames(): (Event)[];
19
- on<E extends Event>(eventName: E, listener: (...data: MaybeArray<T[E]>) => void): this;
20
- once<E extends Event>(eventName: E, listener: (...data: MaybeArray<T[E]>) => void): this;
21
- emit<E extends Event>(eventName: E, ...args: MaybeArray<T[E]>): boolean;
22
- off<E extends Event>(eventName: E, listener: (...args: MaybeArray<T[E]>) => void): this;
29
+ type Evt = Exclude<keyof UpdateEvents, number>;
30
+ interface Updater {
31
+ removeAllListeners<E extends Evt>(event?: E): this;
32
+ listeners<E extends Evt>(eventName: E): Function[];
33
+ eventNames(): Evt[];
34
+ on<E extends Evt>(eventName: E, listener: (...data: UpdateEvents[E]) => void): this;
35
+ once<E extends Evt>(eventName: E, listener: (...data: UpdateEvents[E]) => void): this;
36
+ emit<E extends Evt>(eventName: E, ...args: UpdateEvents[E]): boolean;
37
+ off<E extends Evt>(eventName: E, listener: (...args: UpdateEvents[E]) => void): this;
23
38
  /**
24
39
  * check update info
25
40
  * @param data update json url
26
41
  * @returns
27
42
  * - `{size: number, version: string}`: available
28
43
  * - `false`: unavailable
29
- * - `Error`: fail
44
+ * - `Error`: fail ({@link MinimumVersionError} or other)
30
45
  */
31
46
  checkUpdate(data?: string | UpdateJSON): Promise<CheckResultType>;
32
47
  /**
@@ -37,15 +52,60 @@ interface TypedUpdater<T extends Record<string | symbol, MaybeArray<any>>, Event
37
52
  * @param sig signature
38
53
  * @returns
39
54
  * - `true`: success
40
- * - `Error`: fail
55
+ * - `Error`: fail ({@link VerifyFailedError} or other)
41
56
  */
42
- download(data?: string | Buffer, sig?: string): Promise<InstallResult>;
43
- setDebugMode(debug: boolean): void;
57
+ download(data?: string | Buffer, sig?: string): Promise<DownloadResult>;
58
+ debug: boolean;
44
59
  productName: string;
60
+ receiveBeta: boolean;
45
61
  }
46
- type FunctionVerifySignature = (buffer: Buffer, signature: string, cert: string) => string | false;
47
- type FunctionCompareVersion = (oldVersion: string, newVersion: string) => boolean;
48
- type Updater = TypedUpdater<UpdateEvents>;
62
+ type FunctionVerifySignature = (buffer: Buffer, signature: string, cert: string) => string | false | Promise<string | false>;
63
+ type FunctionCompareVersion = (oldVersion: string, newVersion: string) => boolean | Promise<boolean>;
64
+ type FunctionDownloadBuffer = (url: string, updater: Updater, headers: Record<string, any>) => Promise<Buffer>;
65
+ type FunctionDownloadJSON = (url: string, updater: Updater, headers: Record<string, any>) => Promise<UpdateJSON>;
66
+ type UpdaterOverrideFunctions = {
67
+ /**
68
+ * custom version compare function {@link FunctionCompareVersion}
69
+ * @param oldVersion old version string
70
+ * @param newVersion new version string
71
+ * @returns whether oldVersion < newVersion
72
+ */
73
+ compareVersion?: FunctionCompareVersion;
74
+ /**
75
+ * custom verify signature function {@link FunctionVerifySignature}
76
+ * @param buffer file buffer
77
+ * @param signature signature
78
+ * @param cert certificate
79
+ */
80
+ verifySignaure?: FunctionVerifySignature;
81
+ /**
82
+ * custom download JSON function
83
+ * @param url download url
84
+ * @param updater updater, to trigger events
85
+ * @param header download header
86
+ * @returns `UpdateJSON`
87
+ */
88
+ downloadJSON?: FunctionDownloadJSON;
89
+ /**
90
+ * custom download buffer function
91
+ * @param url download url
92
+ * @param updater updater, to trigger events
93
+ * @param header download header
94
+ * @returns `Buffer`
95
+ */
96
+ downloadBuffer?: FunctionDownloadBuffer;
97
+ };
98
+ type UpdaterDownloadConfig = {
99
+ /**
100
+ * download user agent
101
+ * @default 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
102
+ */
103
+ userAgent?: string;
104
+ /**
105
+ * extra download header, `accept` and `user-agent` is set by default
106
+ */
107
+ extraHeader?: Record<string, string>;
108
+ };
49
109
  interface UpdaterOption {
50
110
  /**
51
111
  * public key of signature, which will be auto generated by plugin
@@ -92,56 +152,14 @@ interface UpdaterOption {
92
152
  * whether to enable debug listener
93
153
  */
94
154
  debug?: boolean;
95
- overrideFunctions?: {
96
- /**
97
- * custom version compare function {@link FunctionCompareVersion}
98
- * @param oldVersion old version string
99
- * @param newVersion new version string
100
- * @returns whether oldVersion < newVersion
101
- */
102
- compareVersion?: FunctionCompareVersion;
103
- /**
104
- * custom verify signature function {@link FunctionVerifySignature}
105
- * @param buffer file buffer
106
- * @param signature signature
107
- * @param cert certificate
108
- */
109
- verifySignaure?: FunctionVerifySignature;
110
- /**
111
- * custom download JSON function
112
- * @param url download url
113
- * @param updater updater, to trigger events
114
- * @param header download header
115
- * @returns `UpdateJSON`
116
- */
117
- downloadJSON?: (url: string, updater: Updater, headers: Record<string, any>) => Promise<UpdateJSON>;
118
- /**
119
- * custom download buffer function
120
- * @param url download url
121
- * @param updater updater, to trigger events
122
- * @param header download header
123
- * @returns `Buffer`
124
- */
125
- downloadBuffer?: (url: string, updater: Updater, headers: Record<string, any>) => Promise<Buffer>;
126
- };
127
- downloadConfig?: {
128
- /**
129
- * download user agent
130
- * @default 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
131
- */
132
- userAgent?: string;
133
- /**
134
- * extra download header, `accept` and `user-agent` is set by default
135
- */
136
- extraHeader?: Record<string, string>;
137
- };
155
+ /**
156
+ * whether to receive beta update
157
+ */
158
+ receiveBeta?: boolean;
159
+ overrideFunctions?: UpdaterOverrideFunctions;
160
+ downloadConfig?: UpdaterDownloadConfig;
138
161
  }
139
162
 
140
- /**
141
- * Creates an updater based on the provided options
142
- */
143
- declare function createUpdater(updaterOptions: UpdaterOption): Updater;
144
-
145
163
  type AppOption = {
146
164
  /**
147
165
  * path of electron output dist when in development
@@ -193,4 +211,4 @@ type SetUpdater = {
193
211
  */
194
212
  declare function initApp(appOptions?: AppOption): SetUpdater;
195
213
 
196
- export { AppOption, FunctionCompareVersion, FunctionVerifySignature, StartupWithUpdater, UpdateJSON, Updater, UpdaterOption, createUpdater, initApp };
214
+ export { AppOption, FunctionCompareVersion, FunctionVerifySignature, MinimumVersionError, StartupWithUpdater, Updater, UpdaterOption, VerifyFailedError, createUpdater, initApp };
package/dist/index.d.ts CHANGED
@@ -1,32 +1,47 @@
1
1
  import { Buffer } from 'node:buffer';
2
+ import { U as UpdateJSON } from './updateJson-7e45d9e1.js';
2
3
 
3
- type CheckResultType = Omit<UpdateJSON, 'signature'> | undefined | Error;
4
- type InstallResult = true | Error;
4
+ declare class MinimumVersionError extends Error {
5
+ currentVersion: string;
6
+ minVersion: string;
7
+ constructor(version: string, minimumVersion: string);
8
+ }
9
+ declare class VerifyFailedError extends Error {
10
+ signature: string;
11
+ cert: string;
12
+ constructor(signature: string, cert: string);
13
+ }
14
+ /**
15
+ * Creates an updater based on the provided options
16
+ */
17
+ declare function createUpdater(updaterOptions: UpdaterOption): Updater;
18
+
19
+ type CheckResultType = {
20
+ size: number;
21
+ version: string;
22
+ } | undefined | Error | MinimumVersionError | TypeError;
23
+ type DownloadResult = true | Error | VerifyFailedError | TypeError;
5
24
  type UpdateEvents = {
6
25
  downloading: [progress: number];
26
+ downloadBuffer: [buffer: Buffer];
7
27
  debug: [msg: string | Error];
8
28
  };
9
- type UpdateJSON = {
10
- signature: string;
11
- version: string;
12
- size: number;
13
- };
14
- type MaybeArray<T> = T extends undefined | null | never ? [] : T extends any[] ? T['length'] extends 1 ? [data: T[0]] : T : [data: T];
15
- interface TypedUpdater<T extends Record<string | symbol, MaybeArray<any>>, Event extends Exclude<keyof T, number> = Exclude<keyof T, number>> {
16
- removeAllListeners<E extends Event>(event?: E): this;
17
- listeners<E extends Event>(eventName: E): Function[];
18
- eventNames(): (Event)[];
19
- on<E extends Event>(eventName: E, listener: (...data: MaybeArray<T[E]>) => void): this;
20
- once<E extends Event>(eventName: E, listener: (...data: MaybeArray<T[E]>) => void): this;
21
- emit<E extends Event>(eventName: E, ...args: MaybeArray<T[E]>): boolean;
22
- off<E extends Event>(eventName: E, listener: (...args: MaybeArray<T[E]>) => void): this;
29
+ type Evt = Exclude<keyof UpdateEvents, number>;
30
+ interface Updater {
31
+ removeAllListeners<E extends Evt>(event?: E): this;
32
+ listeners<E extends Evt>(eventName: E): Function[];
33
+ eventNames(): Evt[];
34
+ on<E extends Evt>(eventName: E, listener: (...data: UpdateEvents[E]) => void): this;
35
+ once<E extends Evt>(eventName: E, listener: (...data: UpdateEvents[E]) => void): this;
36
+ emit<E extends Evt>(eventName: E, ...args: UpdateEvents[E]): boolean;
37
+ off<E extends Evt>(eventName: E, listener: (...args: UpdateEvents[E]) => void): this;
23
38
  /**
24
39
  * check update info
25
40
  * @param data update json url
26
41
  * @returns
27
42
  * - `{size: number, version: string}`: available
28
43
  * - `false`: unavailable
29
- * - `Error`: fail
44
+ * - `Error`: fail ({@link MinimumVersionError} or other)
30
45
  */
31
46
  checkUpdate(data?: string | UpdateJSON): Promise<CheckResultType>;
32
47
  /**
@@ -37,15 +52,60 @@ interface TypedUpdater<T extends Record<string | symbol, MaybeArray<any>>, Event
37
52
  * @param sig signature
38
53
  * @returns
39
54
  * - `true`: success
40
- * - `Error`: fail
55
+ * - `Error`: fail ({@link VerifyFailedError} or other)
41
56
  */
42
- download(data?: string | Buffer, sig?: string): Promise<InstallResult>;
43
- setDebugMode(debug: boolean): void;
57
+ download(data?: string | Buffer, sig?: string): Promise<DownloadResult>;
58
+ debug: boolean;
44
59
  productName: string;
60
+ receiveBeta: boolean;
45
61
  }
46
- type FunctionVerifySignature = (buffer: Buffer, signature: string, cert: string) => string | false;
47
- type FunctionCompareVersion = (oldVersion: string, newVersion: string) => boolean;
48
- type Updater = TypedUpdater<UpdateEvents>;
62
+ type FunctionVerifySignature = (buffer: Buffer, signature: string, cert: string) => string | false | Promise<string | false>;
63
+ type FunctionCompareVersion = (oldVersion: string, newVersion: string) => boolean | Promise<boolean>;
64
+ type FunctionDownloadBuffer = (url: string, updater: Updater, headers: Record<string, any>) => Promise<Buffer>;
65
+ type FunctionDownloadJSON = (url: string, updater: Updater, headers: Record<string, any>) => Promise<UpdateJSON>;
66
+ type UpdaterOverrideFunctions = {
67
+ /**
68
+ * custom version compare function {@link FunctionCompareVersion}
69
+ * @param oldVersion old version string
70
+ * @param newVersion new version string
71
+ * @returns whether oldVersion < newVersion
72
+ */
73
+ compareVersion?: FunctionCompareVersion;
74
+ /**
75
+ * custom verify signature function {@link FunctionVerifySignature}
76
+ * @param buffer file buffer
77
+ * @param signature signature
78
+ * @param cert certificate
79
+ */
80
+ verifySignaure?: FunctionVerifySignature;
81
+ /**
82
+ * custom download JSON function
83
+ * @param url download url
84
+ * @param updater updater, to trigger events
85
+ * @param header download header
86
+ * @returns `UpdateJSON`
87
+ */
88
+ downloadJSON?: FunctionDownloadJSON;
89
+ /**
90
+ * custom download buffer function
91
+ * @param url download url
92
+ * @param updater updater, to trigger events
93
+ * @param header download header
94
+ * @returns `Buffer`
95
+ */
96
+ downloadBuffer?: FunctionDownloadBuffer;
97
+ };
98
+ type UpdaterDownloadConfig = {
99
+ /**
100
+ * download user agent
101
+ * @default 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
102
+ */
103
+ userAgent?: string;
104
+ /**
105
+ * extra download header, `accept` and `user-agent` is set by default
106
+ */
107
+ extraHeader?: Record<string, string>;
108
+ };
49
109
  interface UpdaterOption {
50
110
  /**
51
111
  * public key of signature, which will be auto generated by plugin
@@ -92,56 +152,14 @@ interface UpdaterOption {
92
152
  * whether to enable debug listener
93
153
  */
94
154
  debug?: boolean;
95
- overrideFunctions?: {
96
- /**
97
- * custom version compare function {@link FunctionCompareVersion}
98
- * @param oldVersion old version string
99
- * @param newVersion new version string
100
- * @returns whether oldVersion < newVersion
101
- */
102
- compareVersion?: FunctionCompareVersion;
103
- /**
104
- * custom verify signature function {@link FunctionVerifySignature}
105
- * @param buffer file buffer
106
- * @param signature signature
107
- * @param cert certificate
108
- */
109
- verifySignaure?: FunctionVerifySignature;
110
- /**
111
- * custom download JSON function
112
- * @param url download url
113
- * @param updater updater, to trigger events
114
- * @param header download header
115
- * @returns `UpdateJSON`
116
- */
117
- downloadJSON?: (url: string, updater: Updater, headers: Record<string, any>) => Promise<UpdateJSON>;
118
- /**
119
- * custom download buffer function
120
- * @param url download url
121
- * @param updater updater, to trigger events
122
- * @param header download header
123
- * @returns `Buffer`
124
- */
125
- downloadBuffer?: (url: string, updater: Updater, headers: Record<string, any>) => Promise<Buffer>;
126
- };
127
- downloadConfig?: {
128
- /**
129
- * download user agent
130
- * @default 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
131
- */
132
- userAgent?: string;
133
- /**
134
- * extra download header, `accept` and `user-agent` is set by default
135
- */
136
- extraHeader?: Record<string, string>;
137
- };
155
+ /**
156
+ * whether to receive beta update
157
+ */
158
+ receiveBeta?: boolean;
159
+ overrideFunctions?: UpdaterOverrideFunctions;
160
+ downloadConfig?: UpdaterDownloadConfig;
138
161
  }
139
162
 
140
- /**
141
- * Creates an updater based on the provided options
142
- */
143
- declare function createUpdater(updaterOptions: UpdaterOption): Updater;
144
-
145
163
  type AppOption = {
146
164
  /**
147
165
  * path of electron output dist when in development
@@ -193,4 +211,4 @@ type SetUpdater = {
193
211
  */
194
212
  declare function initApp(appOptions?: AppOption): SetUpdater;
195
213
 
196
- export { AppOption, FunctionCompareVersion, FunctionVerifySignature, StartupWithUpdater, UpdateJSON, Updater, UpdaterOption, createUpdater, initApp };
214
+ export { AppOption, FunctionCompareVersion, FunctionVerifySignature, MinimumVersionError, StartupWithUpdater, Updater, UpdaterOption, VerifyFailedError, createUpdater, initApp };