react-native-update-cli 2.22.6 → 2.22.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.
- package/README.md +1 -1
- package/README.zh-CN.md +1 -1
- package/cli.json +5 -0
- package/lib/api.d.ts +7 -0
- package/lib/api.js +106 -38
- package/lib/app.js +3 -2
- package/lib/bin.js +16 -2
- package/lib/bundle-runner.d.ts +35 -2
- package/lib/bundle-runner.js +313 -128
- package/lib/bundle.d.ts +1 -0
- package/lib/bundle.js +2 -0
- package/lib/diff.d.ts +9 -0
- package/lib/diff.js +162 -120
- package/lib/locales/en.d.ts +1 -0
- package/lib/locales/en.js +1 -0
- package/lib/locales/zh.d.ts +1 -0
- package/lib/locales/zh.js +1 -0
- package/lib/package.d.ts +7 -3
- package/lib/package.js +31 -12
- package/lib/types.d.ts +2 -0
- package/lib/utils/dep-versions.d.ts +7 -0
- package/lib/utils/dep-versions.js +66 -31
- package/lib/utils/hbcTransform.d.ts +21 -0
- package/lib/utils/hbcTransform.js +146 -5
- package/lib/utils/hermes-base.d.ts +31 -7
- package/lib/utils/hermes-base.js +284 -76
- package/lib/utils/http-helper.d.ts +6 -1
- package/lib/utils/http-helper.js +23 -11
- package/lib/utils/index.d.ts +30 -1
- package/lib/utils/index.js +98 -24
- package/lib/utils/latest-version/index.d.ts +11 -8
- package/lib/utils/latest-version/index.js +63 -18
- package/lib/utils/zip-entries.js +20 -15
- package/lib/utils/zip-options.d.ts +2 -0
- package/lib/utils/zip-options.js +9 -4
- package/lib/utils/zip-range.d.ts +84 -13
- package/lib/utils/zip-range.js +363 -63
- package/lib/versions.js +30 -23
- package/package.json +11 -11
- package/src/api.ts +126 -46
- package/src/app.ts +3 -2
- package/src/bin.ts +17 -2
- package/src/bundle-runner.ts +419 -171
- package/src/bundle.ts +3 -0
- package/src/diff.ts +210 -153
- package/src/locales/en.ts +2 -0
- package/src/locales/zh.ts +2 -0
- package/src/package.ts +42 -14
- package/src/types.ts +2 -0
- package/src/utils/dep-versions.ts +76 -33
- package/src/utils/hbcTransform.ts +194 -5
- package/src/utils/hermes-base.ts +311 -81
- package/src/utils/http-helper.ts +24 -13
- package/src/utils/index.ts +136 -26
- package/src/utils/latest-version/index.ts +82 -29
- package/src/utils/zip-entries.ts +18 -16
- package/src/utils/zip-options.ts +8 -3
- package/src/utils/zip-range.ts +436 -67
- package/src/versions.ts +34 -26
package/lib/utils/index.js
CHANGED
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.checkPlugins = exports.IpaBundleFileName = exports.ApkBundleFileName = void 0;
|
|
7
|
+
exports.loadTtyTable = loadTtyTable;
|
|
7
8
|
exports.isNonInteractive = isNonInteractive;
|
|
8
9
|
exports.question = question;
|
|
9
10
|
exports.translateOptions = translateOptions;
|
|
@@ -19,13 +20,25 @@ const path_1 = __importDefault(require("path"));
|
|
|
19
20
|
const read_1 = require("read");
|
|
20
21
|
const yauzl_1 = require("yauzl");
|
|
21
22
|
const package_json_1 = __importDefault(require("../../package.json"));
|
|
22
|
-
const latest_version_1 = __importDefault(require("../utils/latest-version"));
|
|
23
|
-
const app_info_parser_1 = __importDefault(require("./app-info-parser"));
|
|
24
23
|
const check_plugin_1 = require("./check-plugin");
|
|
25
24
|
Object.defineProperty(exports, "checkPlugins", { enumerable: true, get: function () { return check_plugin_1.checkPlugins; } });
|
|
26
25
|
const constants_1 = require("./constants");
|
|
27
26
|
const dep_versions_1 = require("./dep-versions");
|
|
28
27
|
const i18n_1 = require("./i18n");
|
|
28
|
+
// app-info-parser (and its zip/plist/protobuf stack) is only needed by the
|
|
29
|
+
// package commands; load it on first use instead of on every CLI start.
|
|
30
|
+
function createAppInfoParser(fn) {
|
|
31
|
+
const { default: AppInfoParser } = require('./app-info-parser');
|
|
32
|
+
return new AppInfoParser(fn);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* tty-table (~25 ms to load) only when a table is rendered. Bun's require of
|
|
36
|
+
* a CJS module can hand back a namespace object, so unwrap `default`.
|
|
37
|
+
*/
|
|
38
|
+
function loadTtyTable() {
|
|
39
|
+
const mod = require('tty-table');
|
|
40
|
+
return (mod.default ?? mod);
|
|
41
|
+
}
|
|
29
42
|
function isNonInteractive() {
|
|
30
43
|
const envValue = process.env.NO_INTERACTIVE?.toLowerCase();
|
|
31
44
|
return (global.NO_INTERACTIVE === true || envValue === 'true' || envValue === '1');
|
|
@@ -90,7 +103,7 @@ async function sha256(data) {
|
|
|
90
103
|
exports.ApkBundleFileName = /^assets\/index\.android\.bundle$/;
|
|
91
104
|
const ApkUpdateJsonName = /^res\/raw\/update\.json$/;
|
|
92
105
|
async function getApkInfo(fn) {
|
|
93
|
-
const appInfoParser =
|
|
106
|
+
const appInfoParser = createAppInfoParser(fn);
|
|
94
107
|
// read both entries in a single scan over the archive
|
|
95
108
|
const entries = await appInfoParser.parser.getEntries([
|
|
96
109
|
exports.ApkBundleFileName,
|
|
@@ -135,7 +148,7 @@ async function getApkInfo(fn) {
|
|
|
135
148
|
};
|
|
136
149
|
}
|
|
137
150
|
async function getAppInfo(fn) {
|
|
138
|
-
const appInfoParser =
|
|
151
|
+
const appInfoParser = createAppInfoParser(fn);
|
|
139
152
|
// single scan (and single nested .hap extraction) for all three entries
|
|
140
153
|
const [bundleFile, updateJsonFile, metaJsonFile] = await appInfoParser.parser.getEntriesFromHarmonyApp([
|
|
141
154
|
/^resources\/rawfile\/bundle\.harmony\.js$/,
|
|
@@ -180,7 +193,7 @@ const IpaBuildTimeName = /^payload\/[^/]+\.app\/pushy_build_time\.txt$/;
|
|
|
180
193
|
// Not in root bundle when use `use_frameworks`
|
|
181
194
|
const IpaBuildTimeFrameworkName = /^payload\/[^/]+\.app\/frameworks\/react_native_update\.framework\/pushy_build_time\.txt$/;
|
|
182
195
|
async function getIpaInfo(fn) {
|
|
183
|
-
const appInfoParser =
|
|
196
|
+
const appInfoParser = createAppInfoParser(fn);
|
|
184
197
|
// read all four entries in a single scan over the archive
|
|
185
198
|
const entries = await appInfoParser.parser.getEntries([
|
|
186
199
|
exports.IpaBundleFileName,
|
|
@@ -383,33 +396,72 @@ async function resolveResource(fn, resourceId, root) {
|
|
|
383
396
|
}
|
|
384
397
|
return null;
|
|
385
398
|
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
399
|
+
const VERSION_CHECK_PACKAGES = [
|
|
400
|
+
'react-native-update-cli',
|
|
401
|
+
'react-native-update',
|
|
402
|
+
];
|
|
403
|
+
/**
|
|
404
|
+
* Latest registry versions of the CLI and the client library, in that order
|
|
405
|
+
* (`undefined` where unknown). Never rejects.
|
|
406
|
+
*
|
|
407
|
+
* `background: true` answers from a 1-day cache when possible and otherwise
|
|
408
|
+
* refreshes it with an unref'd request, so the process is free to exit before
|
|
409
|
+
* the registry answers; `background: false` always asks the registry and
|
|
410
|
+
* waits (refreshing the cache on the way).
|
|
411
|
+
*/
|
|
412
|
+
async function getLatestVersions(background) {
|
|
413
|
+
// the registry client pulls in global-dirs/registry-auth-token/semver: only
|
|
414
|
+
// pay for them when actually checking
|
|
415
|
+
const { default: latestVersion } = require('./latest-version');
|
|
416
|
+
return latestVersion(VERSION_CHECK_PACKAGES, {
|
|
417
|
+
useCache: true,
|
|
418
|
+
...(background ? { unref: true } : { cacheMaxAge: 0 }),
|
|
389
419
|
requestOptions: {
|
|
390
420
|
timeout: 2000,
|
|
391
421
|
},
|
|
392
422
|
})
|
|
393
423
|
.then((pkgs) => pkgs.map((pkg) => pkg.latest))
|
|
394
|
-
.catch(() =>
|
|
424
|
+
.catch(() => VERSION_CHECK_PACKAGES.map(() => undefined));
|
|
395
425
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
426
|
+
function isNewer(latest, current) {
|
|
427
|
+
if (!latest || !current) {
|
|
428
|
+
return false;
|
|
429
|
+
}
|
|
430
|
+
try {
|
|
431
|
+
return (0, compare_versions_1.compare)(latest, current, '>');
|
|
432
|
+
}
|
|
433
|
+
catch {
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
function latestTag(version) {
|
|
438
|
+
return version
|
|
439
|
+
? ` ${(0, i18n_1.t)('latestVersionTag', { version: chalk_1.default.green(version) })}`
|
|
405
440
|
: '';
|
|
406
|
-
|
|
407
|
-
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Print the installed CLI / react-native-update versions (and refuse or warn
|
|
444
|
+
* about unsupported client versions) immediately, without touching the
|
|
445
|
+
* network.
|
|
446
|
+
*
|
|
447
|
+
* With `wait: true` (the `-v`/`version` command) the registry is queried
|
|
448
|
+
* first and the latest versions are printed inline. Otherwise the registry
|
|
449
|
+
* check runs in the background and the returned `printHints` shows what is
|
|
450
|
+
* newer once the command is done — the command itself is never delayed by it.
|
|
451
|
+
*/
|
|
452
|
+
async function printVersionCommand({ wait = true, } = {}) {
|
|
453
|
+
const rnuVersion = (0, dep_versions_1.getDepVersions)()['react-native-update'];
|
|
454
|
+
let latest;
|
|
455
|
+
const check = getLatestVersions(!wait).then((versions) => {
|
|
456
|
+
latest = versions;
|
|
457
|
+
});
|
|
458
|
+
if (wait) {
|
|
459
|
+
await check;
|
|
460
|
+
}
|
|
461
|
+
const [latestCliVersion, latestRnuVersion] = latest ?? [];
|
|
462
|
+
console.log(`react-native-update-cli: ${package_json_1.default.version}${latestTag(latestCliVersion)}`);
|
|
408
463
|
if (rnuVersion) {
|
|
409
|
-
|
|
410
|
-
? ` ${(0, i18n_1.t)('latestVersionTag', { version: chalk_1.default.green(latestRnuVersion) })}`
|
|
411
|
-
: '';
|
|
412
|
-
console.log(`react-native-update: ${rnuVersion}${latestRnuVersion}`);
|
|
464
|
+
console.log(`react-native-update: ${rnuVersion}${latestTag(latestRnuVersion)}`);
|
|
413
465
|
if (constants_1.IS_CRESC) {
|
|
414
466
|
if ((0, compare_versions_1.satisfies)(rnuVersion, '<10.27.0')) {
|
|
415
467
|
console.error('Unsupported version, please update to the latest version: npm i react-native-update@latest');
|
|
@@ -433,4 +485,26 @@ async function printVersionCommand() {
|
|
|
433
485
|
else {
|
|
434
486
|
console.log((0, i18n_1.t)('rnuVersionNotFound'));
|
|
435
487
|
}
|
|
488
|
+
let hinted = wait; // inline tags already shown
|
|
489
|
+
const printHints = () => {
|
|
490
|
+
if (hinted || !latest) {
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
hinted = true;
|
|
494
|
+
const [cliLatest, rnuLatest] = latest;
|
|
495
|
+
if (isNewer(cliLatest, package_json_1.default.version)) {
|
|
496
|
+
console.log(`react-native-update-cli: ${package_json_1.default.version}${latestTag(cliLatest)}`);
|
|
497
|
+
}
|
|
498
|
+
if (rnuVersion && isNewer(rnuLatest, rnuVersion)) {
|
|
499
|
+
console.log(`react-native-update: ${rnuVersion}${latestTag(rnuLatest)}`);
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
const settle = (graceMs) => {
|
|
503
|
+
let timer;
|
|
504
|
+
const grace = new Promise((resolve) => {
|
|
505
|
+
timer = setTimeout(resolve, graceMs);
|
|
506
|
+
});
|
|
507
|
+
return Promise.race([check, grace]).then(() => clearTimeout(timer));
|
|
508
|
+
};
|
|
509
|
+
return { done: check, settle, printHints };
|
|
436
510
|
}
|
|
@@ -61,23 +61,26 @@ interface LatestVersionOptions {
|
|
|
61
61
|
* Awaiting the api to return might take time, depending on the network, and might impact your package loading performance.
|
|
62
62
|
* You can use the cache mechanism to improve load performance and reduce unnecessary network requests.
|
|
63
63
|
* If `useCache` is not supplied, the api will always check for updates and wait for every requests to return before returning itself.
|
|
64
|
-
* If `useCache` is used
|
|
65
|
-
* 1) a
|
|
66
|
-
* 2)
|
|
67
|
-
*
|
|
64
|
+
* If `useCache` is used (for each provided package):
|
|
65
|
+
* 1) a fresh cache (younger than `cacheMaxAge`, from the same registry) answers without any network request
|
|
66
|
+
* 2) otherwise the registry is queried and the cache refreshed; when that query fails, a stale cache (if any) is used instead.
|
|
67
|
+
* Combine with `unref` so a slow refresh never keeps the process alive.
|
|
68
68
|
* @default false
|
|
69
69
|
*/
|
|
70
70
|
readonly useCache?: boolean;
|
|
71
71
|
/**
|
|
72
72
|
* How long the cache for the provided packages should be used before being refreshed (in milliseconds).
|
|
73
73
|
* If `useCache` is not supplied, this option has no effect.
|
|
74
|
-
* If `0` is used,
|
|
75
|
-
* 1) The api will returned immediately (without any latest nor next version available for the provided packages)
|
|
76
|
-
* 2) New updates will be fetched in the background
|
|
77
|
-
* 3) The cache for each provided packages will be refreshed and made available for the next call to the api
|
|
74
|
+
* If `0` is used, the cache is always refreshed (a stale cache still serves as a fallback when the refresh fails).
|
|
78
75
|
* @default ONE_DAY
|
|
79
76
|
*/
|
|
80
77
|
readonly cacheMaxAge?: number;
|
|
78
|
+
/**
|
|
79
|
+
* Do not let the registry request keep the process alive: its socket is `unref`'d, so the process may exit before the
|
|
80
|
+
* request completes (the returned promise then simply never settles). Use for opportunistic background checks.
|
|
81
|
+
* @default false
|
|
82
|
+
*/
|
|
83
|
+
readonly unref?: boolean;
|
|
81
84
|
/**
|
|
82
85
|
* A JavaScript package registry url that implements the CommonJS Package Registry specification.
|
|
83
86
|
* @default "Looks at any registry urls in the .npmrc file or fallback to the default npm registry instead"
|
|
@@ -19,11 +19,14 @@ const isPackageJson = (obj) => {
|
|
|
19
19
|
obj.devDependencies !== undefined ||
|
|
20
20
|
obj.peerDependencies !== undefined);
|
|
21
21
|
};
|
|
22
|
+
const resolveRegistryUrl = (pkgName, options) => {
|
|
23
|
+
const i = pkgName.indexOf('/');
|
|
24
|
+
const pkgScope = i !== -1 ? pkgName.slice(0, i) : '';
|
|
25
|
+
return options?.registryUrl ?? (0, registry_url_1.default)(pkgScope);
|
|
26
|
+
};
|
|
22
27
|
const downloadMetadata = (pkgName, options) => {
|
|
23
28
|
return new Promise((resolve, reject) => {
|
|
24
|
-
const
|
|
25
|
-
const pkgScope = i !== -1 ? pkgName.slice(0, i) : '';
|
|
26
|
-
const registryUrl = options?.registryUrl ?? (0, registry_url_1.default)(pkgScope);
|
|
29
|
+
const registryUrl = resolveRegistryUrl(pkgName, options);
|
|
27
30
|
const pkgUrl = new url_1.URL(encodeURIComponent(pkgName).replace(/^%40/, '@'), registryUrl);
|
|
28
31
|
let requestOptions = {
|
|
29
32
|
headers: {
|
|
@@ -60,6 +63,7 @@ const downloadMetadata = (pkgName, options) => {
|
|
|
60
63
|
lastUpdateDate: Date.now(),
|
|
61
64
|
versions: Object.keys(pkgMetadata.versions),
|
|
62
65
|
distTags: pkgMetadata['dist-tags'],
|
|
66
|
+
registryUrl,
|
|
63
67
|
});
|
|
64
68
|
return;
|
|
65
69
|
}
|
|
@@ -89,10 +93,22 @@ const downloadMetadata = (pkgName, options) => {
|
|
|
89
93
|
request.on('close', () => {
|
|
90
94
|
request.removeAllListeners();
|
|
91
95
|
});
|
|
96
|
+
if (options?.unref) {
|
|
97
|
+
// A background check must never delay the process exit: drop the
|
|
98
|
+
// socket's ref (also for a pooled keep-alive socket, which the agent
|
|
99
|
+
// ref's again right before handing it out).
|
|
100
|
+
request.once('socket', (socket) => {
|
|
101
|
+
socket.unref?.();
|
|
102
|
+
});
|
|
103
|
+
}
|
|
92
104
|
});
|
|
93
105
|
};
|
|
94
106
|
const getCacheDir = (name = '@badisi/latest-version') => {
|
|
95
107
|
const homeDir = (0, os_1.homedir)();
|
|
108
|
+
if (process.env.XDG_CACHE_HOME) {
|
|
109
|
+
// an explicit override wins on every platform (also lets tests isolate)
|
|
110
|
+
return (0, path_1.join)(process.env.XDG_CACHE_HOME, name);
|
|
111
|
+
}
|
|
96
112
|
switch (process.platform) {
|
|
97
113
|
case 'darwin':
|
|
98
114
|
return (0, path_1.join)(homeDir, 'Library', 'Caches', name);
|
|
@@ -103,32 +119,61 @@ const getCacheDir = (name = '@badisi/latest-version') => {
|
|
|
103
119
|
}
|
|
104
120
|
};
|
|
105
121
|
const saveMetadataToCache = (pkg) => {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
(0, fs_1.
|
|
122
|
+
try {
|
|
123
|
+
const filePath = (0, path_1.join)(getCacheDir(), `${pkg.name}.json`);
|
|
124
|
+
if (!(0, fs_1.existsSync)((0, path_1.dirname)(filePath))) {
|
|
125
|
+
(0, fs_1.mkdirSync)((0, path_1.dirname)(filePath), { recursive: true });
|
|
126
|
+
}
|
|
127
|
+
(0, fs_1.writeFileSync)(filePath, JSON.stringify(pkg));
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
// a read-only or missing cache dir must not turn into an error
|
|
109
131
|
}
|
|
110
|
-
(0, fs_1.writeFileSync)(filePath, JSON.stringify(pkg));
|
|
111
132
|
};
|
|
133
|
+
/**
|
|
134
|
+
* The cached metadata of a package (from the same registry), and whether it is
|
|
135
|
+
* still younger than `cacheMaxAge`. A stale entry is returned as well so it can
|
|
136
|
+
* serve as a fallback when the registry is unreachable.
|
|
137
|
+
*/
|
|
112
138
|
const getMetadataFromCache = (pkgName, options) => {
|
|
113
139
|
const maxAge = options?.cacheMaxAge ?? exports.ONE_DAY;
|
|
114
|
-
|
|
140
|
+
try {
|
|
115
141
|
const pkgCacheFilePath = (0, path_1.join)(getCacheDir(), `${pkgName}.json`);
|
|
116
|
-
if ((0, fs_1.existsSync)(pkgCacheFilePath)) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
142
|
+
if (!(0, fs_1.existsSync)(pkgCacheFilePath)) {
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
const metadata = JSON.parse((0, fs_1.readFileSync)(pkgCacheFilePath).toString());
|
|
146
|
+
if (!metadata ||
|
|
147
|
+
typeof metadata.lastUpdateDate !== 'number' ||
|
|
148
|
+
!metadata.distTags ||
|
|
149
|
+
metadata.registryUrl !== resolveRegistryUrl(pkgName, options)) {
|
|
150
|
+
return undefined;
|
|
121
151
|
}
|
|
152
|
+
const age = Date.now() - metadata.lastUpdateDate;
|
|
153
|
+
return { metadata, fresh: maxAge !== 0 && age >= 0 && age < maxAge };
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
return undefined; // unreadable / corrupt cache is the same as no cache
|
|
122
157
|
}
|
|
123
|
-
return undefined; // invalidates cache
|
|
124
158
|
};
|
|
125
159
|
const getRegistryVersions = async (pkgName, tagOrRange, options) => {
|
|
126
160
|
let pkgMetadata;
|
|
127
161
|
if (pkgName.length && options?.useCache) {
|
|
128
|
-
|
|
129
|
-
if (
|
|
130
|
-
pkgMetadata =
|
|
131
|
-
|
|
162
|
+
const cached = getMetadataFromCache(pkgName, options);
|
|
163
|
+
if (cached?.fresh) {
|
|
164
|
+
pkgMetadata = cached.metadata;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
try {
|
|
168
|
+
pkgMetadata = await downloadMetadata(pkgName, options);
|
|
169
|
+
saveMetadataToCache(pkgMetadata);
|
|
170
|
+
}
|
|
171
|
+
catch (err) {
|
|
172
|
+
if (!cached) {
|
|
173
|
+
throw err;
|
|
174
|
+
}
|
|
175
|
+
pkgMetadata = cached.metadata; // stale beats nothing
|
|
176
|
+
}
|
|
132
177
|
}
|
|
133
178
|
}
|
|
134
179
|
else if (pkgName.length) {
|
package/lib/utils/zip-entries.js
CHANGED
|
@@ -45,7 +45,6 @@ const os_1 = __importDefault(require("os"));
|
|
|
45
45
|
const path_1 = __importDefault(require("path"));
|
|
46
46
|
const promises_1 = require("stream/promises");
|
|
47
47
|
const yauzl_1 = require("yauzl");
|
|
48
|
-
const i18n_1 = require("./i18n");
|
|
49
48
|
function readEntry(entry, zipFile) {
|
|
50
49
|
const buffers = [];
|
|
51
50
|
return new Promise((resolve, reject) => {
|
|
@@ -60,7 +59,8 @@ function readEntry(entry, zipFile) {
|
|
|
60
59
|
buffers.push(chunk);
|
|
61
60
|
});
|
|
62
61
|
stream.on('end', () => {
|
|
63
|
-
|
|
62
|
+
// a single chunk (a cached Range read) is handed back as-is, not copied
|
|
63
|
+
resolve(buffers.length === 1 ? buffers[0] : Buffer.concat(buffers));
|
|
64
64
|
});
|
|
65
65
|
stream.on('error', (err) => {
|
|
66
66
|
reject(err);
|
|
@@ -168,19 +168,23 @@ async function enumZipEntries(zipFn, callback, nestedPath = '') {
|
|
|
168
168
|
entry.fileName.toLowerCase().endsWith('.hap')) {
|
|
169
169
|
const tempDir = await fs.mkdtemp(path_1.default.join(os_1.default.tmpdir(), 'nested_zip_'));
|
|
170
170
|
const tempZipPath = path_1.default.join(tempDir, 'temp.zip');
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
171
|
+
try {
|
|
172
|
+
await new Promise((res, rej) => {
|
|
173
|
+
zipfile.openReadStream(entry, async (err, readStream) => {
|
|
174
|
+
if (err)
|
|
175
|
+
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
|
+
});
|
|
180
182
|
});
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
183
|
+
await enumZipEntries(tempZipPath, callback, `${fullPath}/`);
|
|
184
|
+
}
|
|
185
|
+
finally {
|
|
186
|
+
await fs.remove(tempDir);
|
|
187
|
+
}
|
|
184
188
|
}
|
|
185
189
|
const result = callback(entry, zipfile, fullPath);
|
|
186
190
|
if (result && typeof result.then === 'function') {
|
|
@@ -188,7 +192,8 @@ async function enumZipEntries(zipFn, callback, nestedPath = '') {
|
|
|
188
192
|
}
|
|
189
193
|
}
|
|
190
194
|
catch (error) {
|
|
191
|
-
|
|
195
|
+
// the rejection carries the error; logging it here too would make
|
|
196
|
+
// callers report it twice
|
|
192
197
|
zipfile.close();
|
|
193
198
|
reject(error);
|
|
194
199
|
return;
|
|
@@ -4,6 +4,8 @@ export type ZipEntryOptions = {
|
|
|
4
4
|
};
|
|
5
5
|
export declare const ZIP_ENTRY_SNIFF_BYTES = 64;
|
|
6
6
|
export declare const MANIFEST_COMPRESSION_THRESHOLD_BYTES = 256;
|
|
7
|
+
export declare const PAYLOAD_COMPRESSION_LEVEL = 6;
|
|
8
|
+
export declare const MANIFEST_COMPRESSION_LEVEL = 9;
|
|
7
9
|
export declare function zipOptionsForPatchEntry(): ZipEntryOptions;
|
|
8
10
|
export declare function zipOptionsForManifestEntry(byteLength?: number): ZipEntryOptions;
|
|
9
11
|
export declare function zipOptionsForPayloadEntry(fileName: string, prefix?: Buffer): ZipEntryOptions;
|
package/lib/utils/zip-options.js
CHANGED
|
@@ -36,7 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.MANIFEST_COMPRESSION_THRESHOLD_BYTES = exports.ZIP_ENTRY_SNIFF_BYTES = void 0;
|
|
39
|
+
exports.MANIFEST_COMPRESSION_LEVEL = exports.PAYLOAD_COMPRESSION_LEVEL = exports.MANIFEST_COMPRESSION_THRESHOLD_BYTES = exports.ZIP_ENTRY_SNIFF_BYTES = void 0;
|
|
40
40
|
exports.zipOptionsForPatchEntry = zipOptionsForPatchEntry;
|
|
41
41
|
exports.zipOptionsForManifestEntry = zipOptionsForManifestEntry;
|
|
42
42
|
exports.zipOptionsForPayloadEntry = zipOptionsForPayloadEntry;
|
|
@@ -45,6 +45,11 @@ const fs = __importStar(require("fs"));
|
|
|
45
45
|
const path_1 = __importDefault(require("path"));
|
|
46
46
|
exports.ZIP_ENTRY_SNIFF_BYTES = 64;
|
|
47
47
|
exports.MANIFEST_COMPRESSION_THRESHOLD_BYTES = 256;
|
|
48
|
+
// Deflate level for the bundle/asset payload. Level 9 measured ~4x the CPU
|
|
49
|
+
// of level 6 on Hermes bytecode for ~0.6% smaller output, so 6 is the sweet
|
|
50
|
+
// spot; the manifest is tiny, so it can afford 9.
|
|
51
|
+
exports.PAYLOAD_COMPRESSION_LEVEL = 6;
|
|
52
|
+
exports.MANIFEST_COMPRESSION_LEVEL = 9;
|
|
48
53
|
const alreadyCompressedExtensions = new Set([
|
|
49
54
|
'.7z',
|
|
50
55
|
'.aab',
|
|
@@ -155,7 +160,7 @@ function zipOptionsForPatchEntry() {
|
|
|
155
160
|
}
|
|
156
161
|
function zipOptionsForManifestEntry(byteLength = 0) {
|
|
157
162
|
if (byteLength >= exports.MANIFEST_COMPRESSION_THRESHOLD_BYTES) {
|
|
158
|
-
return { compressionLevel:
|
|
163
|
+
return { compressionLevel: exports.MANIFEST_COMPRESSION_LEVEL };
|
|
159
164
|
}
|
|
160
165
|
return { compress: false };
|
|
161
166
|
}
|
|
@@ -163,7 +168,7 @@ function zipOptionsForPayloadEntry(fileName, prefix) {
|
|
|
163
168
|
if (prefix && prefix.length > 0) {
|
|
164
169
|
if (hasHermesBytecodeMagic(prefix)) {
|
|
165
170
|
// Hermes bytecode is binary, but still benefits significantly from zip deflate.
|
|
166
|
-
return { compress: true, compressionLevel:
|
|
171
|
+
return { compress: true, compressionLevel: exports.PAYLOAD_COMPRESSION_LEVEL };
|
|
167
172
|
}
|
|
168
173
|
if (hasAlreadyCompressedMagic(prefix)) {
|
|
169
174
|
return { compress: false };
|
|
@@ -173,7 +178,7 @@ function zipOptionsForPayloadEntry(fileName, prefix) {
|
|
|
173
178
|
if (alreadyCompressedExtensions.has(extension)) {
|
|
174
179
|
return { compress: false };
|
|
175
180
|
}
|
|
176
|
-
return { compress: true, compressionLevel:
|
|
181
|
+
return { compress: true, compressionLevel: exports.PAYLOAD_COMPRESSION_LEVEL };
|
|
177
182
|
}
|
|
178
183
|
function zipOptionsForPayloadFile(filePath, entryName = filePath) {
|
|
179
184
|
return zipOptionsForPayloadEntry(entryName, readFilePrefix(filePath));
|
package/lib/utils/zip-range.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Readable } from 'stream';
|
|
2
|
-
import { RandomAccessReader, type ZipFile } from 'yauzl';
|
|
2
|
+
import { type Entry, RandomAccessReader, type ZipFile } from 'yauzl';
|
|
3
3
|
export declare const ZIP_STORED = 0;
|
|
4
4
|
export declare const ZIP_DEFLATED = 8;
|
|
5
5
|
export interface ZipEntryLocation {
|
|
@@ -13,6 +13,30 @@ export interface ZipEntryLocation {
|
|
|
13
13
|
}
|
|
14
14
|
/** Data location of the first entry matching `matches` in a local zip. */
|
|
15
15
|
export declare function locateZipEntry(zipPath: string, matches: (name: string) => boolean): Promise<ZipEntryLocation | null>;
|
|
16
|
+
/**
|
|
17
|
+
* Content and data location of the first matching entry in one pass over the
|
|
18
|
+
* archive (publish needs both: the bundle to hash and cache, its location to
|
|
19
|
+
* report to the server).
|
|
20
|
+
*/
|
|
21
|
+
export declare function readZipEntryWithLocation(zipPath: string, matches: (name: string) => boolean): Promise<{
|
|
22
|
+
data: Buffer;
|
|
23
|
+
location: ZipEntryLocation;
|
|
24
|
+
} | null>;
|
|
25
|
+
/**
|
|
26
|
+
* The `bundleOffset` / `bundleCompressedSize` / `bundleCompression` fields for
|
|
27
|
+
* version/create and package/create, or nothing when the entry cannot be
|
|
28
|
+
* fetched by a single Range request (unknown, empty or exotic compression).
|
|
29
|
+
*/
|
|
30
|
+
export declare function bundleLocationFields(location: ZipEntryLocation | null | undefined): Record<string, number>;
|
|
31
|
+
/** Deadlines for the Range requests; every field has a default. */
|
|
32
|
+
export interface RangeOptions {
|
|
33
|
+
/** reads of at most CHUNK_BYTES (tail, headers, directory pages), default 20 s */
|
|
34
|
+
headerTimeoutMs?: number;
|
|
35
|
+
/** base deadline for larger reads (entry data, a prefetched directory), default 30 s */
|
|
36
|
+
dataTimeoutMs?: number;
|
|
37
|
+
/** added to `dataTimeoutMs` per MB requested, default 1 s */
|
|
38
|
+
dataTimeoutPerMbMs?: number;
|
|
39
|
+
}
|
|
16
40
|
/** The server answered a Range request with the whole body (HTTP 200). */
|
|
17
41
|
export declare class RangeUnsupportedError extends Error {
|
|
18
42
|
readonly response: Response;
|
|
@@ -24,34 +48,78 @@ interface ContentRange {
|
|
|
24
48
|
total: number;
|
|
25
49
|
}
|
|
26
50
|
export declare function parseContentRange(header: string | null): ContentRange | null;
|
|
51
|
+
/**
|
|
52
|
+
* The validator to send as `If-Range` on later requests for the same object,
|
|
53
|
+
* so a replaced archive answers 200 (handled like a server without Range)
|
|
54
|
+
* instead of mixing bytes of two versions. Weak ETags are not allowed there.
|
|
55
|
+
*/
|
|
56
|
+
export declare function rangeValidator(headers: Headers): string | undefined;
|
|
27
57
|
/**
|
|
28
58
|
* Fetch one entry whose compressed bytes are already located (the ppk's own
|
|
29
59
|
* publish-time report) and decompress it. Returns the raw entry content.
|
|
30
60
|
*/
|
|
31
|
-
export declare function fetchZipEntryData(url: string, location: Pick<ZipEntryLocation, 'dataOffset' | 'compressedSize' | 'compressionMethod'
|
|
61
|
+
export declare function fetchZipEntryData(url: string, location: Pick<ZipEntryLocation, 'dataOffset' | 'compressedSize' | 'compressionMethod'>, options?: RangeOptions): Promise<{
|
|
32
62
|
data: Buffer;
|
|
33
63
|
fetchedBytes: number;
|
|
34
64
|
totalBytes: number;
|
|
35
65
|
}>;
|
|
66
|
+
export interface HttpRangeReaderOptions extends RangeOptions {
|
|
67
|
+
/** validator of the archive (see `rangeValidator`), sent as If-Range */
|
|
68
|
+
ifRange?: string;
|
|
69
|
+
}
|
|
70
|
+
interface Chunk {
|
|
71
|
+
start: number;
|
|
72
|
+
data: Buffer;
|
|
73
|
+
}
|
|
36
74
|
/**
|
|
37
75
|
* yauzl reader over HTTP Range. Header reads (EOCD, central directory, local
|
|
38
|
-
* file headers) are widened to CHUNK_BYTES and cached
|
|
39
|
-
*
|
|
76
|
+
* file headers) are widened to CHUNK_BYTES and cached. Chunks are kept as
|
|
77
|
+
* fetched — only overlapping ones are merged, touching ones are read across —
|
|
78
|
+
* so an entry's bytes are held once, and a read reuses whatever prefix the
|
|
79
|
+
* cache holds (typically the bytes following the local file header) before
|
|
80
|
+
* requesting the rest. Every request after the first carries If-Range and
|
|
81
|
+
* must report the same archive size.
|
|
40
82
|
*/
|
|
41
83
|
export declare class HttpRangeReader extends RandomAccessReader {
|
|
42
84
|
private readonly url;
|
|
43
85
|
readonly totalSize: number;
|
|
44
|
-
private readonly
|
|
86
|
+
private readonly options;
|
|
87
|
+
/** sorted by `start`, never overlapping */
|
|
88
|
+
private chunks;
|
|
89
|
+
/** span a header read inside it should widen to (see `hintEntry`) */
|
|
90
|
+
private hint;
|
|
45
91
|
requests: number;
|
|
46
92
|
fetchedBytes: number;
|
|
47
|
-
constructor(url: string, totalSize: number, tail?:
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
93
|
+
constructor(url: string, totalSize: number, tail?: Chunk, options?: HttpRangeReaderOptions);
|
|
94
|
+
/**
|
|
95
|
+
* The next entry to be read: yauzl first reads its local file header, and
|
|
96
|
+
* widening that read to the entry's data makes the data stream a cache hit
|
|
97
|
+
* — one request for header + bundle instead of two. The central directory
|
|
98
|
+
* may understate the local header's extra field, hence the slack.
|
|
99
|
+
*/
|
|
100
|
+
hintEntry(entry: Entry): void;
|
|
101
|
+
/** Cache `data` at `start`, merging with any chunk it overlaps. */
|
|
102
|
+
addChunk(start: number, data: Buffer): void;
|
|
103
|
+
/** Fetch `[start, end)` into the cache in one request. */
|
|
104
|
+
prefetch(start: number, end: number): Promise<void>;
|
|
105
|
+
private requestOptions;
|
|
106
|
+
private fetchBuffer;
|
|
107
|
+
/**
|
|
108
|
+
* Contiguous cached pieces starting exactly at `start`, up to `end`: views
|
|
109
|
+
* into one chunk or into a run of touching chunks, possibly ending short.
|
|
110
|
+
*/
|
|
111
|
+
private cachedPieces;
|
|
112
|
+
/** start of the cached run (touching chunks) holding byte `end - 1` */
|
|
113
|
+
private cachedSuffixStart;
|
|
114
|
+
/** start of the first cached chunk at or after `position` */
|
|
115
|
+
private nextCachedStart;
|
|
52
116
|
_readStreamForRange(start: number, end: number): Readable;
|
|
53
117
|
read(buffer: Buffer, offset: number, length: number, position: number, callback: (err: Error | null, bytesRead?: number) => void): void;
|
|
54
118
|
}
|
|
119
|
+
export declare function parseEndOfCentralDirectory(tail: Buffer): {
|
|
120
|
+
cdOffset: number;
|
|
121
|
+
cdSize: number;
|
|
122
|
+
} | null;
|
|
55
123
|
export type RemoteZip = {
|
|
56
124
|
kind: 'zip';
|
|
57
125
|
zipFile: ZipFile;
|
|
@@ -66,7 +134,10 @@ export type RemoteZip = {
|
|
|
66
134
|
* Open a remote zip by fetching its tail (EOCD + central directory for
|
|
67
135
|
* typical archives) and letting yauzl read the rest on demand.
|
|
68
136
|
*/
|
|
69
|
-
export declare function openRemoteZip(url: string): Promise<RemoteZip>;
|
|
70
|
-
/**
|
|
71
|
-
|
|
137
|
+
export declare function openRemoteZip(url: string, options?: RangeOptions): Promise<RemoteZip>;
|
|
138
|
+
/**
|
|
139
|
+
* Read the first matching entry of an opened remote zip, then close it. With
|
|
140
|
+
* `reader` given, the entry's header and data are fetched in one request.
|
|
141
|
+
*/
|
|
142
|
+
export declare function readRemoteZipEntry(zipFile: ZipFile, matches: (name: string) => boolean, reader?: HttpRangeReader): Promise<Buffer | null>;
|
|
72
143
|
export {};
|