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/vite.js CHANGED
@@ -5,6 +5,7 @@ import ElectronSimple from 'vite-plugin-electron/simple';
5
5
  import { startup } from 'vite-plugin-electron';
6
6
  import { notBundle } from 'vite-plugin-electron/plugin';
7
7
  import { getPackageInfoSync, loadPackageJSON } from 'local-pkg';
8
+ import { isCI } from 'ci-info';
8
9
  import Asar from '@electron/asar';
9
10
  import { build } from 'esbuild';
10
11
  import { spawn } from 'node:child_process';
@@ -41,21 +42,19 @@ function parseVersion(version) {
41
42
  return ret;
42
43
  }
43
44
  function isUpdateJSON(json) {
44
- const is = (j) => !!(j && j.minimumVersion && j.signature && j.size && j.version);
45
+ const is = (j) => !!(j && j.minimumVersion && j.signature && j.version);
45
46
  return is(json) && is(json?.beta);
46
47
  }
47
- function defaultVersionJsonGenerator(existingJson, buffer, signature, version, minimumVersion) {
48
+ function defaultVersionJsonGenerator(existingJson, signature, version, minimumVersion) {
48
49
  existingJson.beta = {
49
50
  version,
50
51
  minimumVersion,
51
- signature,
52
- size: buffer.length
52
+ signature
53
53
  };
54
54
  if (!parseVersion(version).stage) {
55
55
  existingJson.version = version;
56
56
  existingJson.minimumVersion = minimumVersion;
57
57
  existingJson.signature = signature;
58
- existingJson.size = buffer.length;
59
58
  }
60
59
  return existingJson;
61
60
  }
@@ -159,6 +158,17 @@ function obfuscateString(input) {
159
158
  return `(${decodeFn})([${hexArray.join(",")}],${offset})`;
160
159
  }
161
160
 
161
+ // src/build-plugins/utils.ts
162
+ function readableSize(size) {
163
+ const units = ["B", "KB", "MB", "GB"];
164
+ let i = 0;
165
+ while (size >= 1024 && i < units.length - 1) {
166
+ size /= 1024;
167
+ i++;
168
+ }
169
+ return `${size.toFixed(2)} ${units[i]}`;
170
+ }
171
+
162
172
  // src/build-plugins/build.ts
163
173
  async function buildAsar({
164
174
  version,
@@ -171,10 +181,12 @@ async function buildAsar({
171
181
  renameSync(rendererDistPath, join(electronDistPath, "renderer"));
172
182
  writeFileSync(join(electronDistPath, "version"), version);
173
183
  await Asar.createPackage(electronDistPath, asarOutputPath);
174
- await generateGzipFile(readFileSync(asarOutputPath), gzipPath);
184
+ const buf = await generateGzipFile(readFileSync(asarOutputPath));
185
+ writeFileSync(gzipPath, buf);
186
+ log.info(`build update asar to '${gzipPath}' [${readableSize(buf.length)}]`, { timestamp: true });
187
+ return buf;
175
188
  }
176
189
  async function buildVersion({
177
- gzipPath,
178
190
  versionPath,
179
191
  privateKey,
180
192
  cert,
@@ -182,17 +194,15 @@ async function buildVersion({
182
194
  minimumVersion,
183
195
  generateSignature,
184
196
  generateVersionJson
185
- }) {
197
+ }, asarBuffer) {
186
198
  let _json = {
187
199
  beta: {
188
200
  minimumVersion: version,
189
201
  signature: "",
190
- size: 0,
191
202
  version
192
203
  },
193
204
  minimumVersion: version,
194
205
  signature: "",
195
- size: 0,
196
206
  version
197
207
  };
198
208
  if (existsSync(versionPath)) {
@@ -201,18 +211,18 @@ async function buildVersion({
201
211
  if (isUpdateJSON(oldVersionJson)) {
202
212
  _json = oldVersionJson;
203
213
  } else {
204
- log.warn("old version json is invalid, ignore it");
214
+ log.warn("old version json is invalid, ignore it", { timestamp: true });
205
215
  }
206
216
  } catch {
207
217
  }
208
218
  }
209
- const buffer = readFileSync(gzipPath);
210
- const sig = await generateSignature(buffer, privateKey, cert, version);
211
- _json = await generateVersionJson(_json, buffer, sig, version, minimumVersion);
219
+ const sig = await generateSignature(asarBuffer, privateKey, cert, version);
220
+ _json = await generateVersionJson(_json, sig, version, minimumVersion);
212
221
  if (!isUpdateJSON(_json)) {
213
222
  throw new Error("invalid version info");
214
223
  }
215
224
  writeFileSync(versionPath, JSON.stringify(_json, null, 2));
225
+ log.info(`build version info to '${versionPath}'`, { timestamp: true });
216
226
  }
217
227
  async function buildEntry({
218
228
  sourcemap,
@@ -274,20 +284,20 @@ async function buildEntry({
274
284
  { timestamp: true }
275
285
  );
276
286
  }
277
- bytecodeLog.info(`${filePaths.length} bundles compiled into bytecode`, { timestamp: true });
287
+ bytecodeLog.info(`${filePaths.length} file${filePaths.length > 1 ? "s" : ""} compiled into bytecode`, { timestamp: true });
278
288
  }
279
289
  function getCert(code) {
280
290
  const cert = code.match(/-----BEGIN CERTIFICATE-----[\s\S]*-----END CERTIFICATE-----\\n/)?.[0];
281
291
  return cert ? [cert] : [];
282
292
  }
283
- async function defaultZipFile(buffer, targetFilePath) {
293
+ async function defaultZipFile(buffer) {
284
294
  return new Promise((resolve2, reject) => {
285
295
  brotliCompress(buffer, (err, buffer2) => {
286
296
  if (err) {
287
297
  reject(err);
298
+ } else {
299
+ resolve2(buffer2);
288
300
  }
289
- writeFileSync(targetFilePath, buffer2);
290
- resolve2();
291
301
  });
292
302
  });
293
303
  }
@@ -328,15 +338,21 @@ function parseKeys({
328
338
  days
329
339
  }) {
330
340
  const keysDir = dirname(privateKeyPath);
341
+ let privateKey = process.env.UPDATER_PK;
342
+ let cert = process.env.UPDATER_CERT;
343
+ if (privateKey && cert) {
344
+ log.info("use UPDATER_PK and UPDATER_CERT from environment variables", { timestamp: true });
345
+ return { privateKey, cert };
346
+ }
331
347
  if (!existsSync(keysDir)) {
332
348
  mkdirSync(keysDir);
333
349
  }
334
350
  if (!existsSync(privateKeyPath) || !existsSync(certPath)) {
335
- log.warn("no key pair found, generate new key pair");
351
+ log.info("no key pair found, generate new key pair", { timestamp: true });
336
352
  generateKeyPair(keyLength, parseSubjects(subject), days, privateKeyPath, certPath);
337
353
  }
338
- const privateKey = process.env.UPDATER_PK || readFileSync(privateKeyPath, "utf-8");
339
- const cert = process.env.UPDATER_CERT || readFileSync(certPath, "utf-8");
354
+ privateKey = readFileSync(privateKeyPath, "utf-8");
355
+ cert = readFileSync(certPath, "utf-8");
340
356
  return { privateKey, cert };
341
357
  }
342
358
  function parseSubjects(subject) {
@@ -367,7 +383,13 @@ function parseOptions(pkg, sourcemap = false, minify = false, options = {}) {
367
383
  privateKeyPath = "keys/private.pem",
368
384
  certPath = "keys/cert.pem",
369
385
  keyLength = 2048,
370
- certInfo = {}
386
+ certInfo: {
387
+ subject = {
388
+ commonName: pkg.name,
389
+ organizationName: `org.${pkg.name}`
390
+ },
391
+ days = 3650
392
+ } = {}
371
393
  } = {},
372
394
  overrideGenerator: {
373
395
  generateGzipFile = defaultZipFile,
@@ -375,13 +397,6 @@ function parseOptions(pkg, sourcemap = false, minify = false, options = {}) {
375
397
  generateVersionJson = defaultVersionJsonGenerator
376
398
  } = {}
377
399
  } = options;
378
- let {
379
- subject = {
380
- commonName: pkg.name,
381
- organizationName: `org.${pkg.name}`
382
- },
383
- days = 3650
384
- } = certInfo;
385
400
  const buildAsarOption = {
386
401
  version: pkg.version,
387
402
  asarOutputPath,
@@ -408,7 +423,6 @@ function parseOptions(pkg, sourcemap = false, minify = false, options = {}) {
408
423
  const buildVersionOption = {
409
424
  version: pkg.version,
410
425
  minimumVersion,
411
- gzipPath,
412
426
  privateKey,
413
427
  cert,
414
428
  versionPath,
@@ -553,9 +567,8 @@ ${bytecodeLoaderBlock}`) : _code;
553
567
  closeBundle() {
554
568
  const outDir = `${normalizePath(path2.relative(config.root, path2.resolve(config.root, config.build.outDir)))}/`;
555
569
  bytecodeFiles.forEach((file) => {
556
- const kbs = file.size / 1e3;
557
570
  bytecodeLog.info(
558
- `${outDir}${file.name} => ${kbs.toFixed(2)} kB`,
571
+ `${outDir}${file.name} [${readableSize(file.size)}]`,
559
572
  { timestamp: true }
560
573
  );
561
574
  });
@@ -603,6 +616,7 @@ async function electronWithUpdater(options) {
603
616
  preload: _preload,
604
617
  sourcemap = !isBuild,
605
618
  minify = isBuild,
619
+ buildVersionJson = isCI,
606
620
  updater,
607
621
  bytecode,
608
622
  useNotBundle = true,
@@ -641,7 +655,7 @@ async function electronWithUpdater(options) {
641
655
  __EIU_MAIN_DEV_DIR__: JSON.stringify(buildAsarOption.electronDistPath),
642
656
  __EIU_MAIN_FILE__: JSON.stringify(getMainFilePath(_main.files)),
643
657
  __EIU_SIGNATURE_CERT__: JSON.stringify(cert),
644
- __EUI_VERSION_PATH__: JSON.stringify(parseVersionPath(buildVersionOption.versionPath))
658
+ __EIU_VERSION_PATH__: JSON.stringify(parseVersionPath(buildVersionOption.versionPath))
645
659
  };
646
660
  const _buildEntry = async () => {
647
661
  await buildEntry(
@@ -721,10 +735,10 @@ async function electronWithUpdater(options) {
721
735
  async closeBundle() {
722
736
  await _buildEntry();
723
737
  await _postBuild();
724
- await buildAsar(buildAsarOption);
725
- log.info(`build asar to '${buildAsarOption.asarOutputPath}'`, { timestamp: true });
726
- await buildVersion(buildVersionOption);
727
- log.info(`build version info to '${buildVersionOption.versionPath}'`, { timestamp: true });
738
+ const buffer = await buildAsar(buildAsarOption);
739
+ if (buildVersionJson) {
740
+ await buildVersion(buildVersionOption, buffer);
741
+ }
728
742
  }
729
743
  }
730
744
  ],
@@ -4,6 +4,7 @@ declare function defaultSignature(buffer: Buffer, privateKey: string, cert: stri
4
4
  declare function aesDecrypt(encryptedText: string, key: Buffer, iv: Buffer): string;
5
5
  declare function defaultVerify(buffer: Buffer, signature: string, cert: string): string | undefined;
6
6
 
7
- declare function defaultUnzipFile(buffer: Buffer, targetFilePath: string): Promise<void>;
7
+ declare function defaultZipFile(buffer: Buffer): Promise<Buffer>;
8
+ declare function defaultUnzipFile(buffer: Buffer): Promise<Buffer>;
8
9
 
9
- export { aesEncrypt as a, defaultSignature as b, aesDecrypt as c, defaultUnzipFile as d, defaultVerify as e, hashBuffer as h };
10
+ export { defaultUnzipFile as a, aesEncrypt as b, defaultSignature as c, defaultZipFile as d, aesDecrypt as e, defaultVerify as f, hashBuffer as h };
@@ -4,6 +4,7 @@ declare function defaultSignature(buffer: Buffer, privateKey: string, cert: stri
4
4
  declare function aesDecrypt(encryptedText: string, key: Buffer, iv: Buffer): string;
5
5
  declare function defaultVerify(buffer: Buffer, signature: string, cert: string): string | undefined;
6
6
 
7
- declare function defaultUnzipFile(buffer: Buffer, targetFilePath: string): Promise<void>;
7
+ declare function defaultZipFile(buffer: Buffer): Promise<Buffer>;
8
+ declare function defaultUnzipFile(buffer: Buffer): Promise<Buffer>;
8
9
 
9
- export { aesEncrypt as a, defaultSignature as b, aesDecrypt as c, defaultUnzipFile as d, defaultVerify as e, hashBuffer as h };
10
+ export { defaultUnzipFile as a, aesEncrypt as b, defaultSignature as c, defaultZipFile as d, aesDecrypt as e, defaultVerify as f, hashBuffer as h };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "electron-incremental-update",
3
3
  "type": "module",
4
- "version": "2.0.0-beta.3",
4
+ "version": "2.0.0-beta.5",
5
5
  "description": "electron incremental update tools, powered by vite",
6
6
  "author": "subframe7536",
7
7
  "license": "MIT",
@@ -60,6 +60,7 @@
60
60
  "@babel/core": "^7.24.7",
61
61
  "@babel/plugin-transform-arrow-functions": "^7.24.7",
62
62
  "@subframe7536/type-utils": "^0.1.6",
63
+ "ci-info": "^4.0.0",
63
64
  "local-pkg": "^0.5.0",
64
65
  "selfsigned": "^2.4.1",
65
66
  "vite-plugin-electron": "^0.28.7"
@@ -1,33 +0,0 @@
1
- /**
2
- * handle all unhandled error
3
- * @param callback callback function
4
- */
5
- declare function handleUnexpectedErrors(callback: (err: unknown) => void): void;
6
- interface Version {
7
- major: number;
8
- minor: number;
9
- patch: number;
10
- stage: string;
11
- stageVersion: number;
12
- }
13
- declare function parseVersion(version: string): Version;
14
- declare function defaultIsLowerVersion(oldVer: string, newVer: string): boolean;
15
- /**
16
- * update info json
17
- */
18
- type UpdateInfo = {
19
- signature: string;
20
- minimumVersion: string;
21
- version: string;
22
- size: number;
23
- };
24
- /**
25
- * {@link UpdateInfo} with beta
26
- */
27
- type UpdateJSON = UpdateInfo & {
28
- beta: UpdateInfo;
29
- };
30
- declare function isUpdateJSON(json: any): json is UpdateJSON;
31
- declare function defaultVersionJsonGenerator(existingJson: UpdateJSON, buffer: Buffer, signature: string, version: string, minimumVersion: string): UpdateJSON;
32
-
33
- export { type UpdateJSON as U, type Version as V, type UpdateInfo as a, defaultVersionJsonGenerator as b, defaultIsLowerVersion as d, handleUnexpectedErrors as h, isUpdateJSON as i, parseVersion as p };
@@ -1,33 +0,0 @@
1
- /**
2
- * handle all unhandled error
3
- * @param callback callback function
4
- */
5
- declare function handleUnexpectedErrors(callback: (err: unknown) => void): void;
6
- interface Version {
7
- major: number;
8
- minor: number;
9
- patch: number;
10
- stage: string;
11
- stageVersion: number;
12
- }
13
- declare function parseVersion(version: string): Version;
14
- declare function defaultIsLowerVersion(oldVer: string, newVer: string): boolean;
15
- /**
16
- * update info json
17
- */
18
- type UpdateInfo = {
19
- signature: string;
20
- minimumVersion: string;
21
- version: string;
22
- size: number;
23
- };
24
- /**
25
- * {@link UpdateInfo} with beta
26
- */
27
- type UpdateJSON = UpdateInfo & {
28
- beta: UpdateInfo;
29
- };
30
- declare function isUpdateJSON(json: any): json is UpdateJSON;
31
- declare function defaultVersionJsonGenerator(existingJson: UpdateJSON, buffer: Buffer, signature: string, version: string, minimumVersion: string): UpdateJSON;
32
-
33
- export { type UpdateJSON as U, type Version as V, type UpdateInfo as a, defaultVersionJsonGenerator as b, defaultIsLowerVersion as d, handleUnexpectedErrors as h, isUpdateJSON as i, parseVersion as p };