react-native-update-cli 2.24.1 → 2.25.0

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 (95) hide show
  1. package/README.md +11 -4
  2. package/README.zh-CN.md +11 -4
  3. package/cli.json +7 -1
  4. package/lib/api.d.ts +2 -0
  5. package/lib/api.js +63 -18
  6. package/lib/app.js +25 -3
  7. package/lib/bin.js +39 -45
  8. package/lib/bundle-runner.d.ts +27 -0
  9. package/lib/bundle-runner.js +240 -21
  10. package/lib/bundle.d.ts +1 -1
  11. package/lib/bundle.js +19 -6
  12. package/lib/commands.d.ts +6 -0
  13. package/lib/commands.js +65 -0
  14. package/lib/locales/en.d.ts +5 -14
  15. package/lib/locales/en.js +5 -14
  16. package/lib/locales/zh.d.ts +5 -13
  17. package/lib/locales/zh.js +5 -13
  18. package/lib/package.js +4 -0
  19. package/lib/provider.d.ts +1 -1
  20. package/lib/provider.js +3 -1
  21. package/lib/symbolicate.js +11 -3
  22. package/lib/types.d.ts +1 -0
  23. package/lib/user.js +6 -0
  24. package/lib/utils/add-gitignore.js +7 -1
  25. package/lib/utils/dep-versions.d.ts +7 -0
  26. package/lib/utils/dep-versions.js +78 -26
  27. package/lib/utils/git.js +9 -6
  28. package/lib/utils/hermes-base.js +2 -1
  29. package/lib/utils/i18n.d.ts +5 -0
  30. package/lib/utils/i18n.js +14 -1
  31. package/lib/utils/index.d.ts +2 -1
  32. package/lib/utils/index.js +9 -4
  33. package/lib/utils/latest-version/index.js +8 -0
  34. package/lib/utils/runtime.d.ts +21 -0
  35. package/lib/utils/runtime.js +138 -5
  36. package/lib/utils/slim-sourcemap.d.ts +11 -0
  37. package/lib/utils/slim-sourcemap.js +22 -0
  38. package/lib/utils/zip-range.js +3 -2
  39. package/lib/versions.js +16 -15
  40. package/package.json +4 -2
  41. package/lib/index.d.ts +0 -1
  42. package/lib/index.js +0 -17
  43. package/src/api.ts +0 -470
  44. package/src/app.ts +0 -241
  45. package/src/auto-update.ts +0 -647
  46. package/src/bin-cresc.ts +0 -8
  47. package/src/bin.ts +0 -115
  48. package/src/bundle-pack.ts +0 -61
  49. package/src/bundle-runner.ts +0 -1430
  50. package/src/bundle.ts +0 -371
  51. package/src/cache.ts +0 -20
  52. package/src/diff.ts +0 -1010
  53. package/src/exports.ts +0 -17
  54. package/src/index.ts +0 -1
  55. package/src/install.ts +0 -50
  56. package/src/locales/en.ts +0 -257
  57. package/src/locales/zh.ts +0 -234
  58. package/src/native-package.ts +0 -338
  59. package/src/package.ts +0 -482
  60. package/src/provider.ts +0 -294
  61. package/src/symbolicate.ts +0 -159
  62. package/src/types.ts +0 -150
  63. package/src/user.ts +0 -39
  64. package/src/utils/add-gitignore.ts +0 -35
  65. package/src/utils/app-info-parser/aab.ts +0 -241
  66. package/src/utils/app-info-parser/apk.ts +0 -92
  67. package/src/utils/app-info-parser/app.ts +0 -7
  68. package/src/utils/app-info-parser/index.ts +0 -54
  69. package/src/utils/app-info-parser/ipa.ts +0 -109
  70. package/src/utils/app-info-parser/resource-finder.ts +0 -508
  71. package/src/utils/app-info-parser/utils.ts +0 -161
  72. package/src/utils/app-info-parser/xml-parser/binary.ts +0 -693
  73. package/src/utils/app-info-parser/xml-parser/manifest.ts +0 -228
  74. package/src/utils/app-info-parser/zip.ts +0 -108
  75. package/src/utils/check-lockfile.ts +0 -93
  76. package/src/utils/check-plugin.ts +0 -38
  77. package/src/utils/command-result.ts +0 -24
  78. package/src/utils/constants.ts +0 -25
  79. package/src/utils/dep-versions.ts +0 -83
  80. package/src/utils/git.ts +0 -71
  81. package/src/utils/hbcTransform.ts +0 -649
  82. package/src/utils/hermes-base.ts +0 -1137
  83. package/src/utils/http-helper.ts +0 -152
  84. package/src/utils/i18n.ts +0 -39
  85. package/src/utils/index.ts +0 -698
  86. package/src/utils/latest-version/index.ts +0 -573
  87. package/src/utils/options.ts +0 -78
  88. package/src/utils/plugin-config.ts +0 -32
  89. package/src/utils/runtime.ts +0 -187
  90. package/src/utils/slim-sourcemap.ts +0 -83
  91. package/src/utils/zip-entries.ts +0 -209
  92. package/src/utils/zip-options.ts +0 -178
  93. package/src/utils/zip-range.ts +0 -727
  94. package/src/utils/zip-raw-writer.ts +0 -247
  95. package/src/versions.ts +0 -894
@@ -1,187 +0,0 @@
1
- import {
2
- type ChildProcessWithoutNullStreams,
3
- type SpawnOptionsWithoutStdio,
4
- type SpawnSyncOptions,
5
- spawn,
6
- spawnSync,
7
- } from 'child_process';
8
- import fs from 'fs';
9
- import { createConnection } from 'net';
10
- import nodeFetch from 'node-fetch';
11
- import path from 'path';
12
-
13
- export type RuntimeRequestInit = {
14
- method?: string;
15
- headers?: Record<string, string>;
16
- body?: any;
17
- signal?: AbortSignal;
18
- };
19
-
20
- export type RuntimeResponse = {
21
- status: number;
22
- statusText: string;
23
- text: () => Promise<string>;
24
- };
25
-
26
- export type PackageManager = 'bun' | 'npm' | 'pnpm' | 'yarn';
27
- export type JavaScriptRuntime = 'bun' | 'node';
28
-
29
- export const isBunRuntime =
30
- typeof (process.versions as NodeJS.ProcessVersions & { bun?: string }).bun ===
31
- 'string';
32
-
33
- export function runtimeFetch(
34
- url: string,
35
- options?: RuntimeRequestInit,
36
- ): Promise<RuntimeResponse> {
37
- const fetchImpl =
38
- typeof globalThis.fetch === 'function'
39
- ? globalThis.fetch.bind(globalThis)
40
- : nodeFetch;
41
-
42
- return fetchImpl(url, options as any) as Promise<RuntimeResponse>;
43
- }
44
-
45
- function resolveTcpTarget(input: string): { host: string; port: number } {
46
- try {
47
- const parsed = new URL(input);
48
- const port = parsed.port || (parsed.protocol === 'http:' ? '80' : '443');
49
- return {
50
- host: parsed.hostname,
51
- port: Number(port),
52
- };
53
- } catch {
54
- const [host, port] = input.split(':');
55
- return {
56
- host,
57
- port: port ? Number(port) : 443,
58
- };
59
- }
60
- }
61
-
62
- function measureTcpConnectOnce(
63
- host: string,
64
- port: number,
65
- timeout: number,
66
- ): Promise<number> {
67
- return new Promise((resolve) => {
68
- const startedAt = Date.now();
69
- const socket = createConnection({ host, port });
70
-
71
- const finish = (latency: number) => {
72
- socket.removeAllListeners();
73
- socket.destroy();
74
- resolve(latency);
75
- };
76
-
77
- socket.setTimeout(timeout);
78
- socket.once('connect', () => {
79
- finish(Date.now() - startedAt);
80
- });
81
- socket.once('timeout', () => {
82
- finish(Number.POSITIVE_INFINITY);
83
- });
84
- socket.once('error', () => {
85
- finish(Number.POSITIVE_INFINITY);
86
- });
87
- });
88
- }
89
-
90
- export async function measureTcpLatency(
91
- input: string,
92
- {
93
- attempts = 4,
94
- timeout = 1000,
95
- }: {
96
- attempts?: number;
97
- timeout?: number;
98
- } = {},
99
- ): Promise<number> {
100
- const { host, port } = resolveTcpTarget(input);
101
- const promises = Array.from({ length: attempts }, () =>
102
- measureTcpConnectOnce(host, port, timeout),
103
- );
104
- const results = await Promise.all(promises);
105
- const latencies = results.filter(Number.isFinite);
106
-
107
- if (latencies.length === 0) {
108
- return Number.POSITIVE_INFINITY;
109
- }
110
- return (
111
- latencies.reduce((sum, latency) => sum + latency, 0) / latencies.length
112
- );
113
- }
114
-
115
- export function detectPackageManager(
116
- cwd = process.cwd(),
117
- env: NodeJS.ProcessEnv = process.env,
118
- ): PackageManager {
119
- const lockFiles: Array<[string, PackageManager]> = [
120
- ['bun.lock', 'bun'],
121
- ['bun.lockb', 'bun'],
122
- ['pnpm-lock.yaml', 'pnpm'],
123
- ['yarn.lock', 'yarn'],
124
- ['package-lock.json', 'npm'],
125
- ];
126
- for (const [lockFile, manager] of lockFiles) {
127
- if (fs.existsSync(path.join(cwd, lockFile))) {
128
- return manager;
129
- }
130
- }
131
-
132
- const userAgent = env.npm_config_user_agent ?? '';
133
- if (userAgent.startsWith('bun/')) {
134
- return 'bun';
135
- }
136
- if (userAgent.startsWith('pnpm/')) {
137
- return 'pnpm';
138
- }
139
- if (userAgent.startsWith('yarn/')) {
140
- return 'yarn';
141
- }
142
- if (userAgent.startsWith('npm/')) {
143
- return 'npm';
144
- }
145
-
146
- return isBunRuntime ? 'bun' : 'npm';
147
- }
148
-
149
- export function getInstallCommand(
150
- installArgs: string[],
151
- cwd = process.cwd(),
152
- ): { command: string; args: string[] } {
153
- const packageManager = detectPackageManager(cwd);
154
- if (packageManager === 'npm') {
155
- return { command: 'npm', args: ['install', ...installArgs] };
156
- }
157
- return { command: packageManager, args: ['add', ...installArgs] };
158
- }
159
-
160
- export function getJavaScriptRuntime(
161
- env: NodeJS.ProcessEnv = process.env,
162
- ): JavaScriptRuntime {
163
- const configured = env.RNU_JS_RUNTIME?.toLowerCase();
164
- if (configured === 'bun') {
165
- return 'bun';
166
- }
167
- if (configured === 'auto') {
168
- return isBunRuntime ? 'bun' : 'node';
169
- }
170
- return 'node';
171
- }
172
-
173
- export function spawnJavaScript(
174
- args: string[],
175
- options?: SpawnOptionsWithoutStdio,
176
- env: NodeJS.ProcessEnv = process.env,
177
- ): ChildProcessWithoutNullStreams {
178
- return spawn(getJavaScriptRuntime(env), args, options ?? {});
179
- }
180
-
181
- export function spawnJavaScriptSync(
182
- args: string[],
183
- options?: SpawnSyncOptions,
184
- env: NodeJS.ProcessEnv = process.env,
185
- ) {
186
- return spawnSync(getJavaScriptRuntime(env), args, options ?? {});
187
- }
@@ -1,83 +0,0 @@
1
- import path from 'path';
2
-
3
- interface SourceMapV3 {
4
- sources?: unknown;
5
- sourcesContent?: unknown;
6
- sections?: unknown;
7
- [key: string]: unknown;
8
- }
9
-
10
- /**
11
- * Shrink a source map before archiving it with a version, without losing the
12
- * ability to locate any frame:
13
- *
14
- * - absolute source paths under the project root become relative (build
15
- * machine paths do not belong in an uploaded artifact);
16
- * - `sourcesContent` of node_modules sources is dropped. Mappings and the
17
- * source paths stay, so frames inside dependencies still symbolicate to
18
- * `node_modules/...:line:column` — only the inline code snippet for those
19
- * frames is lost, and dependency sources are recoverable from the lockfile
20
- * anyway. On an RN app this is the bulk of the map's bytes.
21
- *
22
- * Returns null when the content is not a usable plain source map (invalid
23
- * JSON, or an indexed map with `sections`); the caller then uploads the
24
- * original file untouched.
25
- */
26
- export function slimSourceMap(
27
- content: string,
28
- projectRoot: string,
29
- ): string | null {
30
- let map: SourceMapV3;
31
- try {
32
- map = JSON.parse(content) as SourceMapV3;
33
- } catch {
34
- return null;
35
- }
36
- if (!map || typeof map !== 'object' || Array.isArray(map)) {
37
- return null;
38
- }
39
- if (map.sections !== undefined) {
40
- return null;
41
- }
42
- if (!Array.isArray(map.sources)) {
43
- return null;
44
- }
45
- const rootPrefix = normalizeSlashes(path.resolve(projectRoot));
46
- const sources = (map.sources as unknown[]).map((source) =>
47
- typeof source === 'string' ? relativizeSource(source, rootPrefix) : source,
48
- );
49
- map.sources = sources;
50
- if (Array.isArray(map.sourcesContent)) {
51
- map.sourcesContent = (map.sourcesContent as unknown[]).map(
52
- (item, index) => {
53
- const source = sources[index];
54
- if (typeof source === 'string' && isDependencySource(source)) {
55
- return null;
56
- }
57
- return item;
58
- },
59
- );
60
- }
61
- return JSON.stringify(map);
62
- }
63
-
64
- function normalizeSlashes(value: string): string {
65
- return value.replace(/\\/g, '/');
66
- }
67
-
68
- function relativizeSource(source: string, rootPrefix: string): string {
69
- const normalized = normalizeSlashes(source);
70
- if (normalized === rootPrefix) {
71
- return '';
72
- }
73
- if (normalized.startsWith(`${rootPrefix}/`)) {
74
- return normalized.slice(rootPrefix.length + 1);
75
- }
76
- return normalized;
77
- }
78
-
79
- function isDependencySource(source: string): boolean {
80
- return (
81
- source.startsWith('node_modules/') || source.includes('/node_modules/')
82
- );
83
- }
@@ -1,209 +0,0 @@
1
- import * as fs from 'fs-extra';
2
- import os from 'os';
3
- import path from 'path';
4
- import { pipeline } from 'stream/promises';
5
- import {
6
- type Entry,
7
- open as openZipFile,
8
- type ZipFile as YauzlZipFile,
9
- } from 'yauzl';
10
-
11
- export function readEntry(
12
- entry: Entry,
13
- zipFile: YauzlZipFile,
14
- ): Promise<Buffer> {
15
- const buffers: Buffer[] = [];
16
- return new Promise((resolve, reject) => {
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
- }
24
- stream.on('data', (chunk: Buffer) => {
25
- buffers.push(chunk);
26
- });
27
- stream.on('end', () => {
28
- // a single chunk (a cached Range read) is handed back as-is, not copied
29
- resolve(buffers.length === 1 ? buffers[0] : Buffer.concat(buffers));
30
- });
31
- stream.on('error', (err) => {
32
- reject(err);
33
- });
34
- });
35
- });
36
- }
37
-
38
- export function writeEntry(
39
- entry: Entry,
40
- zipFile: YauzlZipFile,
41
- outputPath: string,
42
- ): Promise<void> {
43
- fs.ensureDirSync(path.dirname(outputPath));
44
- return new Promise((resolve, reject) => {
45
- zipFile.openReadStream(entry, (err, stream) => {
46
- if (err) {
47
- reject(err);
48
- return;
49
- }
50
- if (!stream) {
51
- reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
52
- return;
53
- }
54
- pipeline(stream, fs.createWriteStream(outputPath)).then(resolve, reject);
55
- });
56
- });
57
- }
58
-
59
- export function readEntryPrefix(
60
- entry: Entry,
61
- zipFile: YauzlZipFile,
62
- maxBytes: number,
63
- ): Promise<Buffer> {
64
- if (maxBytes <= 0) {
65
- return Promise.resolve(Buffer.alloc(0));
66
- }
67
-
68
- const buffers: Buffer[] = [];
69
- let length = 0;
70
-
71
- return new Promise((resolve, reject) => {
72
- zipFile.openReadStream(entry, (err, stream) => {
73
- if (err) {
74
- return reject(err);
75
- }
76
- if (!stream) {
77
- return reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
78
- }
79
-
80
- let settled = false;
81
- let expectedDestroyError: Error | undefined;
82
- const cleanup = () => {
83
- stream.off('data', onData);
84
- stream.off('end', onEnd);
85
- stream.off('error', onError);
86
- };
87
- const finish = (destroyStream = false) => {
88
- if (settled) {
89
- return;
90
- }
91
- settled = true;
92
- stream.off('data', onData);
93
- stream.off('end', onEnd);
94
- if (!destroyStream) {
95
- stream.off('error', onError);
96
- }
97
- resolve(Buffer.concat(buffers, length));
98
-
99
- if (destroyStream) {
100
- expectedDestroyError = new Error('zip entry prefix read complete');
101
- stream.destroy(expectedDestroyError);
102
- }
103
- };
104
- const onData = (chunk: Buffer) => {
105
- const remaining = maxBytes - length;
106
- if (remaining <= 0) {
107
- finish(true);
108
- return;
109
- }
110
-
111
- const slice =
112
- chunk.length > remaining ? chunk.subarray(0, remaining) : chunk;
113
- buffers.push(slice);
114
- length += slice.length;
115
- if (length >= maxBytes) {
116
- finish(true);
117
- }
118
- };
119
- const onEnd = () => finish();
120
- const onError = (error: Error) => {
121
- if (settled && error === expectedDestroyError) {
122
- cleanup();
123
- return;
124
- }
125
- if (settled) {
126
- return;
127
- }
128
- settled = true;
129
- cleanup();
130
- reject(error);
131
- };
132
-
133
- stream.on('data', onData);
134
- stream.once('end', onEnd);
135
- stream.once('error', onError);
136
- });
137
- });
138
- }
139
-
140
- export async function enumZipEntries(
141
- zipFn: string,
142
- callback: (
143
- entry: Entry,
144
- zipFile: YauzlZipFile,
145
- nestedPath?: string,
146
- ) => Promise<any> | undefined,
147
- nestedPath = '',
148
- ) {
149
- return new Promise((resolve, reject) => {
150
- openZipFile(
151
- zipFn,
152
- { lazyEntries: true },
153
- async (err: any, zipfile: YauzlZipFile) => {
154
- if (err) {
155
- return reject(err);
156
- }
157
-
158
- zipfile.on('end', resolve);
159
- zipfile.on('error', reject);
160
- zipfile.on('entry', async (entry) => {
161
- const fullPath = nestedPath + entry.fileName;
162
-
163
- try {
164
- if (
165
- !entry.fileName.endsWith('/') &&
166
- entry.fileName.toLowerCase().endsWith('.hap')
167
- ) {
168
- const tempDir = await fs.mkdtemp(
169
- path.join(os.tmpdir(), 'nested_zip_'),
170
- );
171
- const tempZipPath = path.join(tempDir, 'temp.zip');
172
- try {
173
- await new Promise((res, rej) => {
174
- zipfile.openReadStream(entry, async (err, readStream) => {
175
- if (err) return rej(err);
176
- const writeStream = fs.createWriteStream(tempZipPath);
177
- readStream.on('error', rej);
178
- readStream.pipe(writeStream);
179
- writeStream.on('finish', () => res(void 0));
180
- writeStream.on('error', rej);
181
- });
182
- });
183
-
184
- await enumZipEntries(tempZipPath, callback, `${fullPath}/`);
185
- } finally {
186
- await fs.remove(tempDir);
187
- }
188
- }
189
-
190
- const result = callback(entry, zipfile, fullPath);
191
- if (result && typeof result.then === 'function') {
192
- await result;
193
- }
194
- } catch (error) {
195
- // the rejection carries the error; logging it here too would make
196
- // callers report it twice
197
- zipfile.close();
198
- reject(error);
199
- return;
200
- }
201
-
202
- zipfile.readEntry();
203
- });
204
-
205
- zipfile.readEntry();
206
- },
207
- );
208
- });
209
- }
@@ -1,178 +0,0 @@
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
- export const MANIFEST_COMPRESSION_THRESHOLD_BYTES = 256;
11
- // Deflate level for the bundle/asset payload. Level 9 measured ~4x the CPU
12
- // of level 6 on Hermes bytecode for ~0.6% smaller output, so 6 is the sweet
13
- // spot; the manifest is tiny, so it can afford 9.
14
- export const PAYLOAD_COMPRESSION_LEVEL = 6;
15
- export const MANIFEST_COMPRESSION_LEVEL = 9;
16
-
17
- const alreadyCompressedExtensions = new Set([
18
- '.7z',
19
- '.aab',
20
- '.apk',
21
- '.br',
22
- '.bz2',
23
- '.gif',
24
- '.gz',
25
- '.heic',
26
- '.jpeg',
27
- '.jpg',
28
- '.lzma',
29
- '.mp3',
30
- '.mp4',
31
- '.ogg',
32
- '.png',
33
- '.webm',
34
- '.webp',
35
- '.woff',
36
- '.woff2',
37
- '.xz',
38
- '.zip',
39
- '.zst',
40
- ]);
41
-
42
- const HERMES_MAGIC = Buffer.from([
43
- 0xc6, 0x1f, 0xbc, 0x03, 0xc1, 0x03, 0x19, 0x1f,
44
- ]);
45
- const HERMES_DELTA_MAGIC = Buffer.from([
46
- 0x39, 0xe0, 0x43, 0xfc, 0x3e, 0xfc, 0xe6, 0xe0,
47
- ]);
48
-
49
- function startsWith(bytes: Buffer, signature: Buffer): boolean {
50
- return (
51
- bytes.length >= signature.length &&
52
- bytes.subarray(0, signature.length).equals(signature)
53
- );
54
- }
55
-
56
- function hasHermesBytecodeMagic(bytes: Buffer): boolean {
57
- return (
58
- startsWith(bytes, HERMES_MAGIC) || startsWith(bytes, HERMES_DELTA_MAGIC)
59
- );
60
- }
61
-
62
- function hasAlreadyCompressedMagic(bytes: Buffer): boolean {
63
- if (bytes.length < 2) {
64
- return false;
65
- }
66
-
67
- if (startsWith(bytes, Buffer.from([0x89, 0x50, 0x4e, 0x47]))) {
68
- return true;
69
- }
70
- if (startsWith(bytes, Buffer.from([0xff, 0xd8, 0xff]))) {
71
- return true;
72
- }
73
- if (
74
- startsWith(bytes, Buffer.from('GIF87a', 'ascii')) ||
75
- startsWith(bytes, Buffer.from('GIF89a', 'ascii'))
76
- ) {
77
- return true;
78
- }
79
- if (
80
- bytes.length >= 12 &&
81
- bytes.subarray(0, 4).equals(Buffer.from('RIFF', 'ascii')) &&
82
- bytes.subarray(8, 12).equals(Buffer.from('WEBP', 'ascii'))
83
- ) {
84
- return true;
85
- }
86
- if (
87
- startsWith(bytes, Buffer.from([0x50, 0x4b, 0x03, 0x04])) ||
88
- startsWith(bytes, Buffer.from([0x50, 0x4b, 0x05, 0x06])) ||
89
- startsWith(bytes, Buffer.from([0x50, 0x4b, 0x07, 0x08]))
90
- ) {
91
- return true;
92
- }
93
- if (startsWith(bytes, Buffer.from([0x1f, 0x8b]))) {
94
- return true;
95
- }
96
- if (startsWith(bytes, Buffer.from('BZh', 'ascii'))) {
97
- return true;
98
- }
99
- if (startsWith(bytes, Buffer.from([0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00]))) {
100
- return true;
101
- }
102
- if (startsWith(bytes, Buffer.from([0x37, 0x7a, 0xbc, 0xaf, 0x27, 0x1c]))) {
103
- return true;
104
- }
105
- if (startsWith(bytes, Buffer.from([0x28, 0xb5, 0x2f, 0xfd]))) {
106
- return true;
107
- }
108
- if (bytes.length >= 12 && bytes.subarray(4, 8).equals(Buffer.from('ftyp'))) {
109
- return true;
110
- }
111
- if (startsWith(bytes, Buffer.from('OggS', 'ascii'))) {
112
- return true;
113
- }
114
- if (startsWith(bytes, Buffer.from('ID3', 'ascii'))) {
115
- return true;
116
- }
117
- if (bytes[0] === 0xff && bytes.length >= 2 && (bytes[1] & 0xe0) === 0xe0) {
118
- return true;
119
- }
120
- if (
121
- startsWith(bytes, Buffer.from('wOFF', 'ascii')) ||
122
- startsWith(bytes, Buffer.from('wOF2', 'ascii'))
123
- ) {
124
- return true;
125
- }
126
-
127
- return false;
128
- }
129
-
130
- function readFilePrefix(filePath: string): Buffer {
131
- const buffer = Buffer.alloc(ZIP_ENTRY_SNIFF_BYTES);
132
- const fd = fs.openSync(filePath, 'r');
133
- try {
134
- const bytesRead = fs.readSync(fd, buffer, 0, buffer.length, 0);
135
- return buffer.subarray(0, bytesRead);
136
- } finally {
137
- fs.closeSync(fd);
138
- }
139
- }
140
-
141
- export function zipOptionsForPatchEntry(): ZipEntryOptions {
142
- return { compress: false };
143
- }
144
-
145
- export function zipOptionsForManifestEntry(byteLength = 0): ZipEntryOptions {
146
- if (byteLength >= MANIFEST_COMPRESSION_THRESHOLD_BYTES) {
147
- return { compressionLevel: MANIFEST_COMPRESSION_LEVEL };
148
- }
149
- return { compress: false };
150
- }
151
-
152
- export function zipOptionsForPayloadEntry(
153
- fileName: string,
154
- prefix?: Buffer,
155
- ): ZipEntryOptions {
156
- if (prefix && prefix.length > 0) {
157
- if (hasHermesBytecodeMagic(prefix)) {
158
- // Hermes bytecode is binary, but still benefits significantly from zip deflate.
159
- return { compress: true, compressionLevel: PAYLOAD_COMPRESSION_LEVEL };
160
- }
161
- if (hasAlreadyCompressedMagic(prefix)) {
162
- return { compress: false };
163
- }
164
- }
165
-
166
- const extension = path.extname(fileName).toLowerCase();
167
- if (alreadyCompressedExtensions.has(extension)) {
168
- return { compress: false };
169
- }
170
- return { compress: true, compressionLevel: PAYLOAD_COMPRESSION_LEVEL };
171
- }
172
-
173
- export function zipOptionsForPayloadFile(
174
- filePath: string,
175
- entryName = filePath,
176
- ): ZipEntryOptions {
177
- return zipOptionsForPayloadEntry(entryName, readFilePrefix(filePath));
178
- }