react-native-update-cli 2.9.4 → 2.9.6

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/src/diff.ts CHANGED
@@ -6,9 +6,25 @@ import type { CommandContext } from './types';
6
6
  import { translateOptions } from './utils';
7
7
  import { isPPKBundleFileName, scriptName, tempDir } from './utils/constants';
8
8
  import { t } from './utils/i18n';
9
- import { enumZipEntries, readEntry } from './utils/zip-entries';
9
+ import {
10
+ enumZipEntries,
11
+ readEntry,
12
+ readEntryPrefix,
13
+ } from './utils/zip-entries';
14
+ import {
15
+ ZIP_ENTRY_SNIFF_BYTES,
16
+ zipOptionsForManifestEntry,
17
+ zipOptionsForPatchEntry,
18
+ zipOptionsForPayloadEntry,
19
+ } from './utils/zip-options';
10
20
 
11
21
  type Diff = (oldSource?: Buffer, newSource?: Buffer) => Buffer;
22
+ type HdiffModule = {
23
+ diff?: Diff;
24
+ };
25
+ type BsdiffModule = {
26
+ diff?: Diff;
27
+ };
12
28
  type EntryMap = Record<string, { crc32: number; fileName: string }>;
13
29
  type CrcMap = Record<number, string>;
14
30
  type CopyMap = Record<string, string>;
@@ -28,22 +44,19 @@ type DiffCommandConfig = {
28
44
 
29
45
  export { enumZipEntries, readEntry };
30
46
 
31
- const loadDiffModule = (pkgName: string): Diff | undefined => {
47
+ const loadModule = <T>(pkgName: string): T | undefined => {
32
48
  const resolvePaths = ['.', npm.packages, yarn.packages];
33
49
 
34
50
  try {
35
51
  const resolved = require.resolve(pkgName, { paths: resolvePaths });
36
- const mod = require(resolved);
37
- if (mod?.diff) {
38
- return mod.diff as Diff;
39
- }
52
+ return require(resolved) as T;
40
53
  } catch {}
41
54
 
42
55
  return undefined;
43
56
  };
44
57
 
45
- const hdiff = loadDiffModule('node-hdiffpatch');
46
- const bsdiff = loadDiffModule('node-bsdiff');
58
+ const hdiff = loadModule<HdiffModule>('node-hdiffpatch');
59
+ const bsdiff = loadModule<BsdiffModule>('node-bsdiff');
47
60
 
48
61
  function basename(fn: string): string | undefined {
49
62
  const m = /^(.+\/)[^\/]+\/?$/.exec(fn);
@@ -123,6 +136,7 @@ async function diffFromPPK(
123
136
  zipfile.addBuffer(
124
137
  diffFn(originSource, newSource),
125
138
  `${entry.fileName}.patch`,
139
+ zipOptionsForPatchEntry(),
126
140
  );
127
141
  //console.log('End diff');
128
142
  } else {
@@ -150,6 +164,11 @@ async function diffFromPPK(
150
164
  addEntry(basePath);
151
165
  }
152
166
 
167
+ const entryPrefix = await readEntryPrefix(
168
+ entry,
169
+ nextZipfile,
170
+ ZIP_ENTRY_SNIFF_BYTES,
171
+ );
153
172
  await new Promise<void>((resolve, reject) => {
154
173
  nextZipfile.openReadStream(entry, (err, readStream) => {
155
174
  if (err) {
@@ -160,7 +179,11 @@ async function diffFromPPK(
160
179
  new Error(`Unable to read zip entry: ${entry.fileName}`),
161
180
  );
162
181
  }
163
- zipfile.addReadStream(readStream, entry.fileName);
182
+ zipfile.addReadStream(
183
+ readStream,
184
+ entry.fileName,
185
+ zipOptionsForPayloadEntry(entry.fileName, entryPrefix),
186
+ );
164
187
  readStream.on('end', () => {
165
188
  //console.log('add finished');
166
189
  resolve(void 0);
@@ -183,6 +206,7 @@ async function diffFromPPK(
183
206
  zipfile.addBuffer(
184
207
  Buffer.from(JSON.stringify({ copies, deletes })),
185
208
  '__diff.json',
209
+ zipOptionsForManifestEntry(),
186
210
  );
187
211
  zipfile.end();
188
212
  await writePromise;
@@ -248,6 +272,7 @@ async function diffFromPackage(
248
272
  zipfile.addBuffer(
249
273
  diffFn(originSource, newSource),
250
274
  `${entry.fileName}.patch`,
275
+ zipOptionsForPatchEntry(),
251
276
  );
252
277
  //console.log('End diff');
253
278
  } else {
@@ -263,6 +288,11 @@ async function diffFromPackage(
263
288
  return;
264
289
  }
265
290
 
291
+ const entryPrefix = await readEntryPrefix(
292
+ entry,
293
+ nextZipfile,
294
+ ZIP_ENTRY_SNIFF_BYTES,
295
+ );
266
296
  await new Promise<void>((resolve, reject) => {
267
297
  nextZipfile.openReadStream(entry, (err, readStream) => {
268
298
  if (err) {
@@ -273,7 +303,11 @@ async function diffFromPackage(
273
303
  new Error(`Unable to read zip entry: ${entry.fileName}`),
274
304
  );
275
305
  }
276
- zipfile.addReadStream(readStream, entry.fileName);
306
+ zipfile.addReadStream(
307
+ readStream,
308
+ entry.fileName,
309
+ zipOptionsForPayloadEntry(entry.fileName, entryPrefix),
310
+ );
277
311
  readStream.on('end', () => {
278
312
  //console.log('add finished');
279
313
  resolve(void 0);
@@ -283,13 +317,18 @@ async function diffFromPackage(
283
317
  }
284
318
  });
285
319
 
286
- zipfile.addBuffer(Buffer.from(JSON.stringify({ copies })), '__diff.json');
320
+ zipfile.addBuffer(
321
+ Buffer.from(JSON.stringify({ copies })),
322
+ '__diff.json',
323
+ zipOptionsForManifestEntry(),
324
+ );
287
325
  zipfile.end();
288
326
  await writePromise;
289
327
  }
290
328
 
291
329
  type DiffCommandOptions = {
292
330
  customDiff?: Diff;
331
+ customHdiffModule?: HdiffModule;
293
332
  [key: string]: any;
294
333
  };
295
334
 
@@ -302,16 +341,17 @@ function resolveDiffImplementation(
302
341
  }
303
342
 
304
343
  if (useHdiff) {
305
- if (!hdiff) {
344
+ const hdiffModule = options.customHdiffModule ?? hdiff;
345
+ if (!hdiffModule?.diff) {
306
346
  throw new Error(t('nodeHdiffpatchRequired', { scriptName }));
307
347
  }
308
- return hdiff;
348
+ return hdiffModule.diff;
309
349
  }
310
350
 
311
- if (!bsdiff) {
351
+ if (!bsdiff?.diff) {
312
352
  throw new Error(t('nodeBsdiffRequired', { scriptName }));
313
353
  }
314
- return bsdiff;
354
+ return bsdiff.diff;
315
355
  }
316
356
 
317
357
  function diffArgsCheck(
@@ -40,10 +40,6 @@ export class ModuleManager {
40
40
  if (module.init) {
41
41
  module.init(this.provider);
42
42
  }
43
-
44
- // console.log(
45
- // `Module '${module.name}' (v${module.version}) registered successfully`,
46
- // );
47
43
  }
48
44
 
49
45
  unregisterModule(moduleName: string): void {
package/src/package.ts CHANGED
@@ -99,7 +99,7 @@ async function uploadNativePackage(
99
99
  const { appId: appIdInPkg, appKey: appKeyInPkg } = info;
100
100
  const { appId, appKey } = await getSelectedApp(config.platform);
101
101
 
102
- if (appIdInPkg && appIdInPkg != appId) {
102
+ if (appIdInPkg && String(appIdInPkg) !== appId) {
103
103
  throw new Error(t(config.appIdMismatchKey, { appIdInPkg, appId }));
104
104
  }
105
105
 
@@ -175,10 +175,11 @@ export async function listPackage(appId: string) {
175
175
 
176
176
  export async function choosePackage(appId: string) {
177
177
  const list = await listPackage(appId);
178
+ const packageMap = new Map(list?.map((v) => [v.id.toString(), v]));
178
179
 
179
180
  while (true) {
180
181
  const id = await question(t('enterNativePackageId'));
181
- const app = list?.find((v) => v.id.toString() === id);
182
+ const app = packageMap.get(id);
182
183
  if (app) {
183
184
  return app;
184
185
  }
@@ -329,7 +330,7 @@ export const packageCommands = {
329
330
  packages: async ({ options }: { options: { platform: Platform } }) => {
330
331
  const platform = await getPlatform(options.platform);
331
332
  const { appId } = await getSelectedApp(platform);
332
- await listPackage(appId);
333
+ await listPackage(String(appId));
333
334
  },
334
335
  deletePackage: async ({
335
336
  args,
@@ -347,7 +348,7 @@ export const packageCommands = {
347
348
 
348
349
  if (!appId) {
349
350
  const platform = await getPlatform(options.platform);
350
- appId = (await getSelectedApp(platform)).appId as string;
351
+ appId = (await getSelectedApp(platform)).appId;
351
352
  }
352
353
 
353
354
  // If no packageId provided as argument, let user choose from list
@@ -1,10 +1,10 @@
1
+ import * as fs from 'fs-extra';
1
2
  import os from 'os';
2
3
  import path from 'path';
3
- import * as fs from 'fs-extra';
4
4
  import {
5
5
  type Entry,
6
- type ZipFile as YauzlZipFile,
7
6
  open as openZipFile,
7
+ type ZipFile as YauzlZipFile,
8
8
  } from 'yauzl';
9
9
  import { t } from './i18n';
10
10
 
@@ -15,6 +15,12 @@ export function readEntry(
15
15
  const buffers: Buffer[] = [];
16
16
  return new Promise((resolve, reject) => {
17
17
  zipFile.openReadStream(entry, (err, stream) => {
18
+ if (err) {
19
+ return reject(err);
20
+ }
21
+ if (!stream) {
22
+ return reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
23
+ }
18
24
  stream.on('data', (chunk: Buffer) => {
19
25
  buffers.push(chunk);
20
26
  });
@@ -28,6 +34,87 @@ export function readEntry(
28
34
  });
29
35
  }
30
36
 
37
+ export function readEntryPrefix(
38
+ entry: Entry,
39
+ zipFile: YauzlZipFile,
40
+ maxBytes: number,
41
+ ): Promise<Buffer> {
42
+ if (maxBytes <= 0) {
43
+ return Promise.resolve(Buffer.alloc(0));
44
+ }
45
+
46
+ const buffers: Buffer[] = [];
47
+ let length = 0;
48
+
49
+ return new Promise((resolve, reject) => {
50
+ zipFile.openReadStream(entry, (err, stream) => {
51
+ if (err) {
52
+ return reject(err);
53
+ }
54
+ if (!stream) {
55
+ return reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
56
+ }
57
+
58
+ let settled = false;
59
+ let expectedDestroyError: Error | undefined;
60
+ const cleanup = () => {
61
+ stream.off('data', onData);
62
+ stream.off('end', onEnd);
63
+ stream.off('error', onError);
64
+ };
65
+ const finish = (destroyStream = false) => {
66
+ if (settled) {
67
+ return;
68
+ }
69
+ settled = true;
70
+ stream.off('data', onData);
71
+ stream.off('end', onEnd);
72
+ if (!destroyStream) {
73
+ stream.off('error', onError);
74
+ }
75
+ resolve(Buffer.concat(buffers, length));
76
+
77
+ if (destroyStream) {
78
+ expectedDestroyError = new Error('zip entry prefix read complete');
79
+ stream.destroy(expectedDestroyError);
80
+ }
81
+ };
82
+ const onData = (chunk: Buffer) => {
83
+ const remaining = maxBytes - length;
84
+ if (remaining <= 0) {
85
+ finish(true);
86
+ return;
87
+ }
88
+
89
+ const slice =
90
+ chunk.length > remaining ? chunk.subarray(0, remaining) : chunk;
91
+ buffers.push(slice);
92
+ length += slice.length;
93
+ if (length >= maxBytes) {
94
+ finish(true);
95
+ }
96
+ };
97
+ const onEnd = () => finish();
98
+ const onError = (error: Error) => {
99
+ if (settled && error === expectedDestroyError) {
100
+ cleanup();
101
+ return;
102
+ }
103
+ if (settled) {
104
+ return;
105
+ }
106
+ settled = true;
107
+ cleanup();
108
+ reject(error);
109
+ };
110
+
111
+ stream.on('data', onData);
112
+ stream.once('end', onEnd);
113
+ stream.once('error', onError);
114
+ });
115
+ });
116
+ }
117
+
31
118
  export async function enumZipEntries(
32
119
  zipFn: string,
33
120
  callback: (
@@ -0,0 +1,169 @@
1
+ import * as fs from 'fs';
2
+ import path from 'path';
3
+
4
+ export type ZipEntryOptions = {
5
+ compress?: boolean;
6
+ compressionLevel?: number;
7
+ };
8
+
9
+ export const ZIP_ENTRY_SNIFF_BYTES = 64;
10
+
11
+ const alreadyCompressedExtensions = new Set([
12
+ '.7z',
13
+ '.aab',
14
+ '.apk',
15
+ '.br',
16
+ '.bz2',
17
+ '.gif',
18
+ '.gz',
19
+ '.heic',
20
+ '.jpeg',
21
+ '.jpg',
22
+ '.lzma',
23
+ '.mp3',
24
+ '.mp4',
25
+ '.ogg',
26
+ '.png',
27
+ '.webm',
28
+ '.webp',
29
+ '.woff',
30
+ '.woff2',
31
+ '.xz',
32
+ '.zip',
33
+ '.zst',
34
+ ]);
35
+
36
+ const HERMES_MAGIC = Buffer.from([
37
+ 0xc6, 0x1f, 0xbc, 0x03, 0xc1, 0x03, 0x19, 0x1f,
38
+ ]);
39
+ const HERMES_DELTA_MAGIC = Buffer.from([
40
+ 0x39, 0xe0, 0x43, 0xfc, 0x3e, 0xfc, 0xe6, 0xe0,
41
+ ]);
42
+
43
+ function startsWith(bytes: Buffer, signature: Buffer): boolean {
44
+ return (
45
+ bytes.length >= signature.length &&
46
+ bytes.subarray(0, signature.length).equals(signature)
47
+ );
48
+ }
49
+
50
+ function hasHermesBytecodeMagic(bytes: Buffer): boolean {
51
+ return (
52
+ startsWith(bytes, HERMES_MAGIC) || startsWith(bytes, HERMES_DELTA_MAGIC)
53
+ );
54
+ }
55
+
56
+ function hasAlreadyCompressedMagic(bytes: Buffer): boolean {
57
+ if (bytes.length < 2) {
58
+ return false;
59
+ }
60
+
61
+ if (startsWith(bytes, Buffer.from([0x89, 0x50, 0x4e, 0x47]))) {
62
+ return true;
63
+ }
64
+ if (startsWith(bytes, Buffer.from([0xff, 0xd8, 0xff]))) {
65
+ return true;
66
+ }
67
+ if (
68
+ startsWith(bytes, Buffer.from('GIF87a', 'ascii')) ||
69
+ startsWith(bytes, Buffer.from('GIF89a', 'ascii'))
70
+ ) {
71
+ return true;
72
+ }
73
+ if (
74
+ bytes.length >= 12 &&
75
+ bytes.subarray(0, 4).equals(Buffer.from('RIFF', 'ascii')) &&
76
+ bytes.subarray(8, 12).equals(Buffer.from('WEBP', 'ascii'))
77
+ ) {
78
+ return true;
79
+ }
80
+ if (
81
+ startsWith(bytes, Buffer.from([0x50, 0x4b, 0x03, 0x04])) ||
82
+ startsWith(bytes, Buffer.from([0x50, 0x4b, 0x05, 0x06])) ||
83
+ startsWith(bytes, Buffer.from([0x50, 0x4b, 0x07, 0x08]))
84
+ ) {
85
+ return true;
86
+ }
87
+ if (startsWith(bytes, Buffer.from([0x1f, 0x8b]))) {
88
+ return true;
89
+ }
90
+ if (startsWith(bytes, Buffer.from('BZh', 'ascii'))) {
91
+ return true;
92
+ }
93
+ if (startsWith(bytes, Buffer.from([0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00]))) {
94
+ return true;
95
+ }
96
+ if (startsWith(bytes, Buffer.from([0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c]))) {
97
+ return true;
98
+ }
99
+ if (startsWith(bytes, Buffer.from([0x28, 0xb5, 0x2f, 0xfd]))) {
100
+ return true;
101
+ }
102
+ if (bytes.length >= 12 && bytes.subarray(4, 8).equals(Buffer.from('ftyp'))) {
103
+ return true;
104
+ }
105
+ if (startsWith(bytes, Buffer.from('OggS', 'ascii'))) {
106
+ return true;
107
+ }
108
+ if (startsWith(bytes, Buffer.from('ID3', 'ascii'))) {
109
+ return true;
110
+ }
111
+ if (bytes[0] === 0xff && bytes.length >= 2 && (bytes[1] & 0xe0) === 0xe0) {
112
+ return true;
113
+ }
114
+ if (
115
+ startsWith(bytes, Buffer.from('wOFF', 'ascii')) ||
116
+ startsWith(bytes, Buffer.from('wOF2', 'ascii'))
117
+ ) {
118
+ return true;
119
+ }
120
+
121
+ return false;
122
+ }
123
+
124
+ function readFilePrefix(filePath: string): Buffer {
125
+ const buffer = Buffer.alloc(ZIP_ENTRY_SNIFF_BYTES);
126
+ const fd = fs.openSync(filePath, 'r');
127
+ try {
128
+ const bytesRead = fs.readSync(fd, buffer, 0, buffer.length, 0);
129
+ return buffer.subarray(0, bytesRead);
130
+ } finally {
131
+ fs.closeSync(fd);
132
+ }
133
+ }
134
+
135
+ export function zipOptionsForPatchEntry(): ZipEntryOptions {
136
+ return { compress: false };
137
+ }
138
+
139
+ export function zipOptionsForManifestEntry(): ZipEntryOptions {
140
+ return { compress: false };
141
+ }
142
+
143
+ export function zipOptionsForPayloadEntry(
144
+ fileName: string,
145
+ prefix?: Buffer,
146
+ ): ZipEntryOptions {
147
+ if (prefix && prefix.length > 0) {
148
+ if (hasHermesBytecodeMagic(prefix)) {
149
+ // Hermes bytecode is binary, but still benefits significantly from zip deflate.
150
+ return { compress: true, compressionLevel: 9 };
151
+ }
152
+ if (hasAlreadyCompressedMagic(prefix)) {
153
+ return { compress: false };
154
+ }
155
+ }
156
+
157
+ const extension = path.extname(fileName).toLowerCase();
158
+ if (alreadyCompressedExtensions.has(extension)) {
159
+ return { compress: false };
160
+ }
161
+ return { compress: true, compressionLevel: 9 };
162
+ }
163
+
164
+ export function zipOptionsForPayloadFile(
165
+ filePath: string,
166
+ entryName = filePath,
167
+ ): ZipEntryOptions {
168
+ return zipOptionsForPayloadEntry(entryName, readFilePrefix(filePath));
169
+ }
package/src/versions.ts CHANGED
@@ -480,12 +480,13 @@ export const versionCommands = {
480
480
  versions: async ({ options }: { options: VersionCommandOptions }) => {
481
481
  const platform = await getPlatform(options.platform);
482
482
  const { appId } = await getSelectedApp(platform);
483
- await listVersions(appId);
483
+ await listVersions(String(appId));
484
484
  },
485
485
  update: async ({ options }: { options: VersionCommandOptions }) => {
486
486
  const platform = await getPlatform(options.platform);
487
487
  const appId = options.appId || (await getSelectedApp(platform)).appId;
488
- let versionId = options.versionId || (await chooseVersion(appId)).id;
488
+ let versionId =
489
+ options.versionId || (await chooseVersion(String(appId))).id;
489
490
  if (versionId === 'null') {
490
491
  versionId = undefined;
491
492
  }
@@ -498,17 +499,13 @@ export const versionCommands = {
498
499
  let rollout: number | undefined = undefined;
499
500
 
500
501
  if (options.rollout !== undefined) {
501
- try {
502
- rollout = Number.parseInt(options.rollout);
503
- } catch (e) {
504
- throw new Error(t('rolloutRangeError'));
505
- }
506
- if (rollout < 1 || rollout > 100) {
502
+ rollout = Number.parseInt(options.rollout, 10);
503
+ if (Number.isNaN(rollout) || rollout < 1 || rollout > 100) {
507
504
  throw new Error(t('rolloutRangeError'));
508
505
  }
509
506
  }
510
507
 
511
- const allPkgs = await getAllPackages(appId);
508
+ const allPkgs = await getAllPackages(String(appId));
512
509
 
513
510
  if (!allPkgs) {
514
511
  throw new Error(t('noPackagesFound', { appId }));
@@ -558,7 +555,7 @@ export const versionCommands = {
558
555
  }
559
556
  } else {
560
557
  if (!pkgId) {
561
- pkgId = (await choosePackage(appId)).id;
558
+ pkgId = (await choosePackage(String(appId))).id;
562
559
  }
563
560
 
564
561
  if (!pkgId) {
@@ -575,15 +572,15 @@ export const versionCommands = {
575
572
  }
576
573
 
577
574
  await printDepsChangesForPublish({
578
- appId,
579
- versionId,
575
+ appId: String(appId),
576
+ versionId: String(versionId),
580
577
  pkgs: pkgsToBind,
581
578
  providedVersionDeps: options.versionDeps,
582
579
  });
583
580
 
584
581
  await bindVersionToPackages({
585
- appId,
586
- versionId,
582
+ appId: String(appId),
583
+ versionId: String(versionId),
587
584
  pkgs: pkgsToBind,
588
585
  rollout,
589
586
  dryRun: options.dryRun,
@@ -596,14 +593,15 @@ export const versionCommands = {
596
593
  }) => {
597
594
  const platform = await getPlatform(options.platform);
598
595
  const { appId } = await getSelectedApp(platform);
599
- const versionId = options.versionId || (await chooseVersion(appId)).id;
596
+ const versionId =
597
+ options.versionId || (await chooseVersion(String(appId))).id;
600
598
 
601
599
  const updateParams: Record<string, string> = {};
602
600
  if (options.name) updateParams.name = options.name;
603
601
  if (options.description) updateParams.description = options.description;
604
602
  if (options.metaInfo) updateParams.metaInfo = options.metaInfo;
605
603
 
606
- await put(`/app/${appId}/version/${versionId}`, updateParams);
604
+ await put(`/app/${String(appId)}/version/${versionId}`, updateParams);
607
605
  console.log(t('operationSuccess'));
608
606
  },
609
607
  deleteVersion: async ({
@@ -619,11 +617,11 @@ export const versionCommands = {
619
617
 
620
618
  let versionId = options.versionId;
621
619
  if (!versionId) {
622
- versionId = (await chooseVersion(appId as string)).id;
620
+ versionId = (await chooseVersion(String(appId))).id;
623
621
  }
624
622
 
625
623
  try {
626
- await doDelete(`/app/${appId}/version/${versionId}`);
624
+ await doDelete(`/app/${String(appId)}/version/${versionId}`);
627
625
  console.log(t('deleteVersionSuccess', { versionId }));
628
626
  } catch (error: any) {
629
627
  throw new Error(