react-native-update-cli 2.29.1 → 2.29.2

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.
Files changed (80) hide show
  1. package/lib/account.js +2 -4
  2. package/lib/api.d.ts +1 -1
  3. package/lib/api.js +26 -44
  4. package/lib/app.js +13 -27
  5. package/lib/auto-update.d.ts +1 -1
  6. package/lib/auto-update.js +6 -16
  7. package/lib/bin.js +4 -6
  8. package/lib/bundle-pack.js +25 -27
  9. package/lib/bundle-runner.js +55 -54
  10. package/lib/bundle.js +18 -26
  11. package/lib/diff.js +32 -91
  12. package/lib/locales/en.d.ts +0 -10
  13. package/lib/locales/en.js +0 -10
  14. package/lib/locales/zh.d.ts +0 -10
  15. package/lib/locales/zh.js +0 -10
  16. package/lib/native-package.js +20 -60
  17. package/lib/package.d.ts +6 -1
  18. package/lib/package.js +50 -70
  19. package/lib/provider.js +3 -5
  20. package/lib/symbolicate.js +1 -16
  21. package/lib/utils/add-gitignore.js +0 -1
  22. package/lib/utils/app-info-parser/aab.d.ts +5 -22
  23. package/lib/utils/app-info-parser/aab.js +87 -192
  24. package/lib/utils/app-info-parser/apk.d.ts +12 -13
  25. package/lib/utils/app-info-parser/apk.js +28 -78
  26. package/lib/utils/app-info-parser/ipa.d.ts +2 -14
  27. package/lib/utils/app-info-parser/ipa.js +9 -70
  28. package/lib/utils/app-info-parser/resource-finder.d.ts +1 -26
  29. package/lib/utils/app-info-parser/resource-finder.js +41 -183
  30. package/lib/utils/app-info-parser/xml-parser/binary.d.ts +6 -55
  31. package/lib/utils/app-info-parser/xml-parser/binary.js +152 -556
  32. package/lib/utils/app-info-parser/zip.d.ts +6 -24
  33. package/lib/utils/app-info-parser/zip.js +19 -86
  34. package/lib/utils/check-plugin.d.ts +7 -5
  35. package/lib/utils/check-plugin.js +15 -25
  36. package/lib/utils/dep-versions.d.ts +0 -6
  37. package/lib/utils/dep-versions.js +2 -27
  38. package/lib/utils/failure-fingerprint.js +3 -4
  39. package/lib/utils/fs-helpers.d.ts +5 -0
  40. package/lib/utils/fs-helpers.js +19 -0
  41. package/lib/utils/git.d.ts +0 -1
  42. package/lib/utils/git.js +0 -8
  43. package/lib/utils/hash.d.ts +3 -0
  44. package/lib/utils/hash.js +19 -0
  45. package/lib/utils/hbcTransform.d.ts +9 -2
  46. package/lib/utils/hbcTransform.js +41 -70
  47. package/lib/utils/hermes-base.d.ts +1 -5
  48. package/lib/utils/hermes-base.js +93 -156
  49. package/lib/utils/hermes-literals.d.ts +5 -3
  50. package/lib/utils/hermes-literals.js +30 -53
  51. package/lib/utils/hermes-raw.d.ts +7 -0
  52. package/lib/utils/hermes-raw.js +26 -9
  53. package/lib/utils/http-helper.d.ts +0 -1
  54. package/lib/utils/http-helper.js +0 -20
  55. package/lib/utils/i18n.js +0 -2
  56. package/lib/utils/index.d.ts +5 -19
  57. package/lib/utils/index.js +95 -189
  58. package/lib/utils/latest-version.d.ts +19 -0
  59. package/lib/utils/latest-version.js +137 -0
  60. package/lib/utils/options.d.ts +2 -1
  61. package/lib/utils/options.js +5 -7
  62. package/lib/utils/zip-entries.d.ts +23 -0
  63. package/lib/utils/zip-entries.js +125 -102
  64. package/lib/utils/zip-options.js +29 -56
  65. package/lib/utils/zip-range.js +25 -65
  66. package/lib/versions.d.ts +5 -0
  67. package/lib/versions.js +86 -153
  68. package/package.json +1 -3
  69. package/lib/utils/app-info-parser/app.d.ts +0 -4
  70. package/lib/utils/app-info-parser/app.js +0 -10
  71. package/lib/utils/app-info-parser/index.d.ts +0 -16
  72. package/lib/utils/app-info-parser/index.js +0 -46
  73. package/lib/utils/app-info-parser/utils.d.ts +0 -31
  74. package/lib/utils/app-info-parser/utils.js +0 -135
  75. package/lib/utils/app-info-parser/xml-parser/manifest.d.ts +0 -10
  76. package/lib/utils/app-info-parser/xml-parser/manifest.js +0 -202
  77. package/lib/utils/latest-version/index.d.ts +0 -149
  78. package/lib/utils/latest-version/index.js +0 -326
  79. package/lib/utils/plugin-config.d.ts +0 -9
  80. package/lib/utils/plugin-config.js +0 -31
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getLatestVersions = getLatestVersions;
7
+ // Latest dist-tag lookup on the npm registry, with a per-package metadata
8
+ // cache. Trimmed from @badisi/latest-version to what the version check needs;
9
+ // the cache location and file format are unchanged.
10
+ const fs_1 = require("fs");
11
+ const path_1 = require("path");
12
+ const registry_auth_token_1 = __importDefault(require("registry-auth-token"));
13
+ const registry_url_1 = __importDefault(require("registry-auth-token/registry-url"));
14
+ const fs_helpers_1 = require("./fs-helpers");
15
+ const runtime_1 = require("./runtime");
16
+ const ONE_DAY = 1000 * 60 * 60 * 24;
17
+ const cacheFile = (pkgName) => (0, path_1.join)((0, fs_helpers_1.userCacheDir)('@badisi/latest-version'), `${pkgName}.json`);
18
+ const resolveRegistryUrl = (pkgName) => {
19
+ const i = pkgName.indexOf('/');
20
+ return (0, registry_url_1.default)(i !== -1 ? pkgName.slice(0, i) : '');
21
+ };
22
+ function downloadMetadata(pkgName, options) {
23
+ return new Promise((resolve, reject) => {
24
+ const registryUrl = resolveRegistryUrl(pkgName);
25
+ const pkgUrl = new URL(encodeURIComponent(pkgName).replace(/^%40/, '@'), registryUrl);
26
+ const headers = {
27
+ accept: 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*',
28
+ };
29
+ const authInfo = (0, registry_auth_token_1.default)(pkgUrl.toString(), { recursive: true });
30
+ if (authInfo) {
31
+ headers.authorization = `${authInfo.type} ${authInfo.token}`;
32
+ }
33
+ const requestOptions = {
34
+ headers,
35
+ host: pkgUrl.hostname,
36
+ path: pkgUrl.pathname,
37
+ port: pkgUrl.port,
38
+ timeout: options.timeout,
39
+ // HTTP(S)_PROXY / NO_PROXY, like every other request of the CLI
40
+ agent: (0, runtime_1.proxyAgentFor)(pkgUrl.toString()),
41
+ };
42
+ const { get } = require(pkgUrl.protocol === 'https:' ? 'https' : 'http');
43
+ const request = get(requestOptions, (res) => {
44
+ if (res.statusCode !== 200) {
45
+ res.resume(); // consume response data to free up memory
46
+ reject(new Error(`Request error (${res.statusCode}): ${pkgUrl}`));
47
+ return;
48
+ }
49
+ let rawData = '';
50
+ res.setEncoding('utf8');
51
+ res.on('data', (chunk) => {
52
+ rawData += chunk;
53
+ });
54
+ res.once('error', reject);
55
+ res.once('end', () => {
56
+ try {
57
+ resolve({
58
+ name: pkgName,
59
+ lastUpdateDate: Date.now(),
60
+ distTags: JSON.parse(rawData)['dist-tags'],
61
+ registryUrl,
62
+ });
63
+ }
64
+ catch (err) {
65
+ reject(err);
66
+ }
67
+ });
68
+ });
69
+ request.once('timeout', () => {
70
+ request.destroy(new Error(`Request timed out: ${pkgUrl}`));
71
+ });
72
+ request.once('error', reject);
73
+ if (options.unref) {
74
+ // A background check must never delay the process exit: drop the
75
+ // socket's ref (also for a pooled keep-alive socket, which the agent
76
+ // ref's again right before handing it out).
77
+ request.once('socket', (socket) => {
78
+ socket.unref?.();
79
+ });
80
+ }
81
+ });
82
+ }
83
+ function saveMetadataToCache(pkg) {
84
+ try {
85
+ const filePath = cacheFile(pkg.name);
86
+ (0, fs_1.mkdirSync)((0, path_1.dirname)(filePath), { recursive: true });
87
+ (0, fs_1.writeFileSync)(filePath, JSON.stringify(pkg));
88
+ }
89
+ catch {
90
+ // a read-only or missing cache dir must not turn into an error
91
+ }
92
+ }
93
+ /**
94
+ * The cached metadata of a package (from the same registry), and whether it is
95
+ * still younger than `cacheMaxAge`. A stale entry is returned as well so it can
96
+ * serve as a fallback when the registry is unreachable.
97
+ */
98
+ function getMetadataFromCache(pkgName, maxAge) {
99
+ try {
100
+ const metadata = JSON.parse((0, fs_1.readFileSync)(cacheFile(pkgName)).toString());
101
+ if (!metadata ||
102
+ typeof metadata.lastUpdateDate !== 'number' ||
103
+ !metadata.distTags ||
104
+ metadata.registryUrl !== resolveRegistryUrl(pkgName)) {
105
+ return undefined;
106
+ }
107
+ const age = Date.now() - metadata.lastUpdateDate;
108
+ return { metadata, fresh: maxAge !== 0 && age >= 0 && age < maxAge };
109
+ }
110
+ catch {
111
+ return undefined; // missing / unreadable / corrupt cache: no cache
112
+ }
113
+ }
114
+ async function getLatestVersion(pkgName, options) {
115
+ if (!options.useCache) {
116
+ return (await downloadMetadata(pkgName, options)).distTags.latest;
117
+ }
118
+ const cached = getMetadataFromCache(pkgName, options.cacheMaxAge ?? ONE_DAY);
119
+ if (cached?.fresh) {
120
+ return cached.metadata.distTags.latest;
121
+ }
122
+ try {
123
+ const metadata = await downloadMetadata(pkgName, options);
124
+ saveMetadataToCache(metadata);
125
+ return metadata.distTags.latest;
126
+ }
127
+ catch (err) {
128
+ if (!cached) {
129
+ throw err;
130
+ }
131
+ return cached.metadata.distTags.latest; // stale beats nothing
132
+ }
133
+ }
134
+ /** The `latest` dist-tag of each package, `undefined` where unknown. */
135
+ function getLatestVersions(pkgNames, options = {}) {
136
+ return Promise.all(pkgNames.map((name) => getLatestVersion(name, options).catch(() => undefined)));
137
+ }
@@ -1,5 +1,6 @@
1
+ /** `1` or `true` (any case), the usual spelling of an enabled env flag */
2
+ export declare function isTruthyEnv(value: string | undefined): boolean;
1
3
  export declare function getBooleanOption(options: Record<string, unknown>, key: string, fallback?: boolean): boolean;
2
4
  export declare function getStringOption(options: Record<string, unknown>, key: string, fallback?: string): string;
3
5
  export declare function getOptionalStringOption(options: Record<string, unknown>, key: string): string | undefined;
4
6
  export declare function getStringListOption(options: Record<string, unknown>, key: string): string[] | undefined;
5
- export declare function toObjectState<T extends Record<string, unknown>>(value: unknown, fallback: T): T;
@@ -1,10 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isTruthyEnv = isTruthyEnv;
3
4
  exports.getBooleanOption = getBooleanOption;
4
5
  exports.getStringOption = getStringOption;
5
6
  exports.getOptionalStringOption = getOptionalStringOption;
6
7
  exports.getStringListOption = getStringListOption;
7
- exports.toObjectState = toObjectState;
8
+ /** `1` or `true` (any case), the usual spelling of an enabled env flag */
9
+ function isTruthyEnv(value) {
10
+ return value === '1' || value?.toLowerCase() === 'true';
11
+ }
8
12
  function getBooleanOption(options, key, fallback = false) {
9
13
  const value = options[key];
10
14
  if (typeof value === 'boolean') {
@@ -55,9 +59,3 @@ function getStringListOption(options, key) {
55
59
  });
56
60
  return result.length > 0 ? result : undefined;
57
61
  }
58
- function toObjectState(value, fallback) {
59
- if (value && typeof value === 'object') {
60
- return value;
61
- }
62
- return fallback;
63
- }
@@ -1,5 +1,28 @@
1
+ import type { Readable } from 'stream';
1
2
  import { type Entry, type ZipFile as YauzlZipFile } from 'yauzl';
3
+ import { ZipFile as YazlZipFile } from 'yazl';
4
+ export declare function openEntryStream(entry: Entry, zipFile: YauzlZipFile, options?: {
5
+ decodeFileData?: boolean;
6
+ }): Promise<Readable>;
2
7
  export declare function readEntry(entry: Entry, zipFile: YauzlZipFile): Promise<Buffer>;
3
8
  export declare function writeEntry(entry: Entry, zipFile: YauzlZipFile, outputPath: string): Promise<void>;
4
9
  export declare function readEntryPrefix(entry: Entry, zipFile: YauzlZipFile, maxBytes: number): Promise<Buffer>;
10
+ /** Open a local zip and stop at the first matching entry (caller closes). */
11
+ export declare function openFirstEntry(zipPath: string, matches: (name: string) => boolean): Promise<{
12
+ zipFile: YauzlZipFile;
13
+ entry: Entry;
14
+ } | null>;
15
+ /** Content of the first matching entry of a local zip, or null. */
16
+ export declare function readFirstEntry(zipPath: string, matches: (name: string) => boolean): Promise<Buffer | null>;
17
+ /** Extract the first matching entry of a local zip to `outputPath`. */
18
+ export declare function extractFirstEntry(zipPath: string, matches: (name: string) => boolean, outputPath: string): Promise<boolean>;
19
+ /**
20
+ * A yazl archive piped into `output`. `writePromise` settles once the file is
21
+ * closed; `abort` releases the fd and deletes the partial file.
22
+ */
23
+ export declare function createOutputZip(output: string): {
24
+ zipfile: YazlZipFile;
25
+ writePromise: Promise<void>;
26
+ abort: () => Promise<void>;
27
+ };
5
28
  export declare function enumZipEntries(zipFn: string, callback: (entry: Entry, zipFile: YauzlZipFile, nestedPath?: string) => Promise<any> | undefined, nestedPath?: string): Promise<unknown>;
@@ -3,127 +3,163 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.openEntryStream = openEntryStream;
6
7
  exports.readEntry = readEntry;
7
8
  exports.writeEntry = writeEntry;
8
9
  exports.readEntryPrefix = readEntryPrefix;
10
+ exports.openFirstEntry = openFirstEntry;
11
+ exports.readFirstEntry = readFirstEntry;
12
+ exports.extractFirstEntry = extractFirstEntry;
13
+ exports.createOutputZip = createOutputZip;
9
14
  exports.enumZipEntries = enumZipEntries;
10
15
  const fs_1 = __importDefault(require("fs"));
11
16
  const os_1 = __importDefault(require("os"));
12
17
  const path_1 = __importDefault(require("path"));
13
18
  const promises_1 = require("stream/promises");
14
19
  const yauzl_1 = require("yauzl");
20
+ const yazl_1 = require("yazl");
15
21
  const fs_helpers_1 = require("./fs-helpers");
16
- function readEntry(entry, zipFile) {
17
- const buffers = [];
22
+ function openEntryStream(entry, zipFile, options = {}) {
18
23
  return new Promise((resolve, reject) => {
19
- zipFile.openReadStream(entry, (err, stream) => {
20
- if (err) {
21
- return reject(err);
22
- }
23
- if (!stream) {
24
- return reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
24
+ zipFile.openReadStream(entry, options, (err, stream) => {
25
+ if (err || !stream) {
26
+ reject(err ?? new Error(`Unable to read zip entry: ${entry.fileName}`));
27
+ return;
25
28
  }
26
- stream.on('data', (chunk) => {
27
- buffers.push(chunk);
28
- });
29
- stream.on('end', () => {
30
- // a single chunk (a cached Range read) is handed back as-is, not copied
31
- resolve(buffers.length === 1 ? buffers[0] : Buffer.concat(buffers));
32
- });
33
- stream.on('error', (err) => {
34
- reject(err);
35
- });
29
+ resolve(stream);
36
30
  });
37
31
  });
38
32
  }
39
- function writeEntry(entry, zipFile, outputPath) {
33
+ async function readEntry(entry, zipFile) {
34
+ const buffers = [];
35
+ for await (const chunk of await openEntryStream(entry, zipFile)) {
36
+ buffers.push(chunk);
37
+ }
38
+ // a single chunk (a cached Range read) is handed back as-is, not copied
39
+ return buffers.length === 1 ? buffers[0] : Buffer.concat(buffers);
40
+ }
41
+ async function writeEntry(entry, zipFile, outputPath) {
40
42
  (0, fs_helpers_1.ensureDirSync)(path_1.default.dirname(outputPath));
41
- return new Promise((resolve, reject) => {
42
- zipFile.openReadStream(entry, (err, stream) => {
43
- if (err) {
44
- reject(err);
45
- return;
46
- }
47
- if (!stream) {
48
- reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
49
- return;
50
- }
51
- (0, promises_1.pipeline)(stream, fs_1.default.createWriteStream(outputPath)).then(resolve, reject);
52
- });
53
- });
43
+ await (0, promises_1.pipeline)(await openEntryStream(entry, zipFile), fs_1.default.createWriteStream(outputPath));
54
44
  }
55
- function readEntryPrefix(entry, zipFile, maxBytes) {
45
+ async function readEntryPrefix(entry, zipFile, maxBytes) {
56
46
  if (maxBytes <= 0) {
57
- return Promise.resolve(Buffer.alloc(0));
47
+ return Buffer.alloc(0);
58
48
  }
59
49
  const buffers = [];
60
50
  let length = 0;
51
+ // leaving the loop early destroys the stream without an error
52
+ for await (const chunk of await openEntryStream(entry, zipFile)) {
53
+ const slice = chunk.subarray(0, maxBytes - length);
54
+ buffers.push(slice);
55
+ length += slice.length;
56
+ if (length >= maxBytes) {
57
+ break;
58
+ }
59
+ }
60
+ return Buffer.concat(buffers, length);
61
+ }
62
+ /** Open a local zip and stop at the first matching entry (caller closes). */
63
+ function openFirstEntry(zipPath, matches) {
61
64
  return new Promise((resolve, reject) => {
62
- zipFile.openReadStream(entry, (err, stream) => {
63
- if (err) {
65
+ (0, yauzl_1.open)(zipPath, { lazyEntries: true, autoClose: false }, (err, zipFile) => {
66
+ if (err)
64
67
  return reject(err);
65
- }
66
- if (!stream) {
67
- return reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
68
- }
69
68
  let settled = false;
70
- let expectedDestroyError;
71
- const cleanup = () => {
72
- stream.off('data', onData);
73
- stream.off('end', onEnd);
74
- stream.off('error', onError);
75
- };
76
- const finish = (destroyStream = false) => {
77
- if (settled) {
78
- return;
79
- }
80
- settled = true;
81
- stream.off('data', onData);
82
- stream.off('end', onEnd);
83
- if (!destroyStream) {
84
- stream.off('error', onError);
85
- }
86
- resolve(Buffer.concat(buffers, length));
87
- if (destroyStream) {
88
- expectedDestroyError = new Error('zip entry prefix read complete');
89
- stream.destroy(expectedDestroyError);
90
- }
91
- };
92
- const onData = (chunk) => {
93
- const remaining = maxBytes - length;
94
- if (remaining <= 0) {
95
- finish(true);
96
- return;
69
+ zipFile.on('error', (error) => {
70
+ if (!settled) {
71
+ settled = true;
72
+ reject(error);
97
73
  }
98
- const slice = chunk.length > remaining ? chunk.subarray(0, remaining) : chunk;
99
- buffers.push(slice);
100
- length += slice.length;
101
- if (length >= maxBytes) {
102
- finish(true);
74
+ });
75
+ zipFile.on('end', () => {
76
+ if (!settled) {
77
+ settled = true;
78
+ zipFile.close();
79
+ resolve(null);
103
80
  }
104
- };
105
- const onEnd = () => finish();
106
- const onError = (error) => {
107
- if (settled && error === expectedDestroyError) {
108
- cleanup();
81
+ });
82
+ zipFile.on('entry', (entry) => {
83
+ if (settled)
109
84
  return;
110
- }
111
- if (settled) {
85
+ if (matches(entry.fileName)) {
86
+ settled = true;
87
+ resolve({ zipFile, entry });
112
88
  return;
113
89
  }
114
- settled = true;
115
- cleanup();
116
- reject(error);
117
- };
118
- stream.on('data', onData);
119
- stream.once('end', onEnd);
120
- stream.once('error', onError);
90
+ zipFile.readEntry();
91
+ });
92
+ zipFile.readEntry();
121
93
  });
122
94
  });
123
95
  }
96
+ /** Content of the first matching entry of a local zip, or null. */
97
+ async function readFirstEntry(zipPath, matches) {
98
+ const found = await openFirstEntry(zipPath, matches);
99
+ if (!found)
100
+ return null;
101
+ try {
102
+ return await readEntry(found.entry, found.zipFile);
103
+ }
104
+ finally {
105
+ found.zipFile.close();
106
+ }
107
+ }
108
+ /** Extract the first matching entry of a local zip to `outputPath`. */
109
+ async function extractFirstEntry(zipPath, matches, outputPath) {
110
+ const found = await openFirstEntry(zipPath, matches);
111
+ if (!found)
112
+ return false;
113
+ try {
114
+ await writeEntry(found.entry, found.zipFile, outputPath);
115
+ }
116
+ finally {
117
+ found.zipFile.close();
118
+ }
119
+ return true;
120
+ }
121
+ /**
122
+ * A yazl archive piped into `output`. `writePromise` settles once the file is
123
+ * closed; `abort` releases the fd and deletes the partial file.
124
+ */
125
+ function createOutputZip(output) {
126
+ (0, fs_helpers_1.ensureDirSync)(path_1.default.dirname(output));
127
+ const zipfile = new yazl_1.ZipFile();
128
+ const writeStream = fs_1.default.createWriteStream(output);
129
+ const writePromise = new Promise((resolve, reject) => {
130
+ zipfile.outputStream.on('error', reject);
131
+ // without this, a full disk / read-only target left the promise pending
132
+ writeStream.on('error', reject);
133
+ zipfile.outputStream.pipe(writeStream).on('close', () => {
134
+ resolve(void 0);
135
+ });
136
+ });
137
+ // a write error can come while the caller is still building entries, long
138
+ // before it awaits writePromise: mark it handled now (awaiting the original
139
+ // promise still rejects) so it never surfaces as an unhandled rejection
140
+ writePromise.catch(() => { });
141
+ // 失败时释放输出 fd 并删掉半成品,避免残留一个截断的包
142
+ const abort = async () => {
143
+ zipfile.outputStream.unpipe(writeStream);
144
+ // yazl 的 outputStream 类型声明缺 destroy,运行时是 PassThrough
145
+ zipfile.outputStream.destroy();
146
+ if (!writeStream.closed) {
147
+ // 等 fd 真正关闭再删文件,避免 Windows 上删除被占用文件失败
148
+ await new Promise((resolve) => {
149
+ writeStream.once('close', () => resolve());
150
+ if (!writeStream.destroyed) {
151
+ writeStream.destroy();
152
+ }
153
+ });
154
+ }
155
+ // best effort: a failed cleanup must not mask the original error
156
+ await fs_1.default.promises.rm(output, { force: true }).catch(() => { });
157
+ };
158
+ return { zipfile, writePromise, abort };
159
+ }
124
160
  async function enumZipEntries(zipFn, callback, nestedPath = '') {
125
161
  return new Promise((resolve, reject) => {
126
- (0, yauzl_1.open)(zipFn, { lazyEntries: true }, async (err, zipfile) => {
162
+ (0, yauzl_1.open)(zipFn, { lazyEntries: true }, (err, zipfile) => {
127
163
  if (err) {
128
164
  return reject(err);
129
165
  }
@@ -137,27 +173,14 @@ async function enumZipEntries(zipFn, callback, nestedPath = '') {
137
173
  const tempDir = await fs_1.default.promises.mkdtemp(path_1.default.join(os_1.default.tmpdir(), 'nested_zip_'));
138
174
  const tempZipPath = path_1.default.join(tempDir, 'temp.zip');
139
175
  try {
140
- await new Promise((res, rej) => {
141
- zipfile.openReadStream(entry, async (err, readStream) => {
142
- if (err)
143
- return rej(err);
144
- const writeStream = fs_1.default.createWriteStream(tempZipPath);
145
- readStream.on('error', rej);
146
- readStream.pipe(writeStream);
147
- writeStream.on('finish', () => res(void 0));
148
- writeStream.on('error', rej);
149
- });
150
- });
176
+ await writeEntry(entry, zipfile, tempZipPath);
151
177
  await enumZipEntries(tempZipPath, callback, `${fullPath}/`);
152
178
  }
153
179
  finally {
154
180
  await (0, fs_helpers_1.remove)(tempDir);
155
181
  }
156
182
  }
157
- const result = callback(entry, zipfile, fullPath);
158
- if (result && typeof result.then === 'function') {
159
- await result;
160
- }
183
+ await callback(entry, zipfile, fullPath);
161
184
  }
162
185
  catch (error) {
163
186
  // the rejection carries the error; logging it here too would make
@@ -43,6 +43,7 @@ exports.zipOptionsForPayloadEntry = zipOptionsForPayloadEntry;
43
43
  exports.zipOptionsForPayloadFile = zipOptionsForPayloadFile;
44
44
  const fs = __importStar(require("fs"));
45
45
  const path_1 = __importDefault(require("path"));
46
+ const hbcTransform_1 = require("./hbcTransform");
46
47
  exports.ZIP_ENTRY_SNIFF_BYTES = 64;
47
48
  exports.MANIFEST_COMPRESSION_THRESHOLD_BYTES = 256;
48
49
  // Deflate level for the bundle/asset payload. Level 9 measured ~4x the CPU
@@ -74,9 +75,6 @@ const alreadyCompressedExtensions = new Set([
74
75
  '.zip',
75
76
  '.zst',
76
77
  ]);
77
- const HERMES_MAGIC = Buffer.from([
78
- 0xc6, 0x1f, 0xbc, 0x03, 0xc1, 0x03, 0x19, 0x1f,
79
- ]);
80
78
  const HERMES_DELTA_MAGIC = Buffer.from([
81
79
  0x39, 0xe0, 0x43, 0xfc, 0x3e, 0xfc, 0xe6, 0xe0,
82
80
  ]);
@@ -85,64 +83,39 @@ function startsWith(bytes, signature) {
85
83
  bytes.subarray(0, signature.length).equals(signature));
86
84
  }
87
85
  function hasHermesBytecodeMagic(bytes) {
88
- return (startsWith(bytes, HERMES_MAGIC) || startsWith(bytes, HERMES_DELTA_MAGIC));
86
+ return startsWith(bytes, hbcTransform_1.HBC_MAGIC) || startsWith(bytes, HERMES_DELTA_MAGIC);
89
87
  }
88
+ /** leading bytes of formats that are already compressed */
89
+ const COMPRESSED_MAGIC = [
90
+ [0x89, 0x50, 0x4e, 0x47], // PNG
91
+ [0xff, 0xd8, 0xff], // JPEG
92
+ 'GIF87a',
93
+ 'GIF89a',
94
+ [0x50, 0x4b, 0x03, 0x04], // zip
95
+ [0x50, 0x4b, 0x05, 0x06], // empty zip
96
+ [0x50, 0x4b, 0x07, 0x08], // spanned zip
97
+ [0x1f, 0x8b], // gzip
98
+ 'BZh',
99
+ [0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00], // xz
100
+ [0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c], // 7z
101
+ [0x28, 0xb5, 0x2f, 0xfd], // zstd
102
+ 'OggS',
103
+ 'ID3', // mp3 with tags
104
+ 'wOFF',
105
+ 'wOF2',
106
+ ].map((magic) => Buffer.from(magic));
90
107
  function hasAlreadyCompressedMagic(bytes) {
91
108
  if (bytes.length < 2) {
92
109
  return false;
93
110
  }
94
- if (startsWith(bytes, Buffer.from([0x89, 0x50, 0x4e, 0x47]))) {
95
- return true;
96
- }
97
- if (startsWith(bytes, Buffer.from([0xff, 0xd8, 0xff]))) {
98
- return true;
99
- }
100
- if (startsWith(bytes, Buffer.from('GIF87a', 'ascii')) ||
101
- startsWith(bytes, Buffer.from('GIF89a', 'ascii'))) {
102
- return true;
103
- }
104
- if (bytes.length >= 12 &&
105
- bytes.subarray(0, 4).equals(Buffer.from('RIFF', 'ascii')) &&
106
- bytes.subarray(8, 12).equals(Buffer.from('WEBP', 'ascii'))) {
107
- return true;
108
- }
109
- if (startsWith(bytes, Buffer.from([0x50, 0x4b, 0x03, 0x04])) ||
110
- startsWith(bytes, Buffer.from([0x50, 0x4b, 0x05, 0x06])) ||
111
- startsWith(bytes, Buffer.from([0x50, 0x4b, 0x07, 0x08]))) {
112
- return true;
113
- }
114
- if (startsWith(bytes, Buffer.from([0x1f, 0x8b]))) {
115
- return true;
116
- }
117
- if (startsWith(bytes, Buffer.from('BZh', 'ascii'))) {
118
- return true;
119
- }
120
- if (startsWith(bytes, Buffer.from([0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00]))) {
121
- return true;
122
- }
123
- if (startsWith(bytes, Buffer.from([0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c]))) {
124
- return true;
125
- }
126
- if (startsWith(bytes, Buffer.from([0x28, 0xb5, 0x2f, 0xfd]))) {
127
- return true;
128
- }
129
- if (bytes.length >= 12 && bytes.subarray(4, 8).equals(Buffer.from('ftyp'))) {
130
- return true;
131
- }
132
- if (startsWith(bytes, Buffer.from('OggS', 'ascii'))) {
133
- return true;
134
- }
135
- if (startsWith(bytes, Buffer.from('ID3', 'ascii'))) {
136
- return true;
137
- }
138
- if (bytes[0] === 0xff && bytes.length >= 2 && (bytes[1] & 0xe0) === 0xe0) {
139
- return true;
140
- }
141
- if (startsWith(bytes, Buffer.from('wOFF', 'ascii')) ||
142
- startsWith(bytes, Buffer.from('wOF2', 'ascii'))) {
143
- return true;
144
- }
145
- return false;
111
+ const at = (offset, ascii) => bytes.subarray(offset, offset + ascii.length).equals(Buffer.from(ascii));
112
+ return (COMPRESSED_MAGIC.some((magic) => startsWith(bytes, magic)) ||
113
+ // WebP: RIFF container with a WEBP form type
114
+ (bytes.length >= 12 && at(0, 'RIFF') && at(8, 'WEBP')) ||
115
+ // ISO base media (mp4, m4a, heic, avif...)
116
+ (bytes.length >= 12 && at(4, 'ftyp')) ||
117
+ // MPEG audio frame sync
118
+ (bytes[0] === 0xff && (bytes[1] & 0xe0) === 0xe0));
146
119
  }
147
120
  function readFilePrefix(filePath) {
148
121
  const buffer = Buffer.alloc(exports.ZIP_ENTRY_SNIFF_BYTES);