react-native-update-cli 2.22.7 → 2.22.9
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 +11 -4
- package/lib/bundle-runner.js +176 -90
- 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/native-package.d.ts +5 -1
- package/lib/native-package.js +145 -84
- package/lib/package.d.ts +7 -3
- package/lib/package.js +22 -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 +21 -4
- package/lib/utils/hermes-base.js +146 -37
- 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 +91 -25
- 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 +47 -15
- package/lib/utils/zip-range.js +192 -83
- package/lib/utils/zip-raw-writer.d.ts +47 -0
- package/lib/utils/zip-raw-writer.js +204 -0
- package/lib/versions.js +19 -9
- package/package.json +10 -12
- package/src/api.ts +126 -46
- package/src/app.ts +3 -2
- package/src/bin.ts +17 -2
- package/src/bundle-runner.ts +225 -113
- 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/native-package.ts +186 -96
- package/src/package.ts +26 -13
- 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 +156 -45
- package/src/utils/http-helper.ts +24 -13
- package/src/utils/index.ts +136 -26
- package/src/utils/latest-version/index.ts +109 -33
- package/src/utils/zip-entries.ts +18 -16
- package/src/utils/zip-options.ts +8 -3
- package/src/utils/zip-range.ts +244 -88
- package/src/utils/zip-raw-writer.ts +247 -0
- package/src/versions.ts +17 -7
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"
|
|
@@ -4,14 +4,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ONE_DAY = void 0;
|
|
7
|
+
const compare_versions_1 = require("compare-versions");
|
|
7
8
|
const fs_1 = require("fs");
|
|
8
9
|
const global_dirs_1 = require("global-dirs");
|
|
9
10
|
const os_1 = require("os");
|
|
10
11
|
const path_1 = require("path");
|
|
11
12
|
const registry_auth_token_1 = __importDefault(require("registry-auth-token"));
|
|
12
13
|
const registry_url_1 = __importDefault(require("registry-auth-token/registry-url"));
|
|
13
|
-
const gt_1 = __importDefault(require("semver/functions/gt"));
|
|
14
|
-
const max_satisfying_1 = __importDefault(require("semver/ranges/max-satisfying"));
|
|
15
14
|
const url_1 = require("url");
|
|
16
15
|
exports.ONE_DAY = 1000 * 60 * 60 * 24; // eslint-disable-line @typescript-eslint/naming-convention
|
|
17
16
|
const isPackageJson = (obj) => {
|
|
@@ -19,11 +18,14 @@ const isPackageJson = (obj) => {
|
|
|
19
18
|
obj.devDependencies !== undefined ||
|
|
20
19
|
obj.peerDependencies !== undefined);
|
|
21
20
|
};
|
|
21
|
+
const resolveRegistryUrl = (pkgName, options) => {
|
|
22
|
+
const i = pkgName.indexOf('/');
|
|
23
|
+
const pkgScope = i !== -1 ? pkgName.slice(0, i) : '';
|
|
24
|
+
return options?.registryUrl ?? (0, registry_url_1.default)(pkgScope);
|
|
25
|
+
};
|
|
22
26
|
const downloadMetadata = (pkgName, options) => {
|
|
23
27
|
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);
|
|
28
|
+
const registryUrl = resolveRegistryUrl(pkgName, options);
|
|
27
29
|
const pkgUrl = new url_1.URL(encodeURIComponent(pkgName).replace(/^%40/, '@'), registryUrl);
|
|
28
30
|
let requestOptions = {
|
|
29
31
|
headers: {
|
|
@@ -60,6 +62,7 @@ const downloadMetadata = (pkgName, options) => {
|
|
|
60
62
|
lastUpdateDate: Date.now(),
|
|
61
63
|
versions: Object.keys(pkgMetadata.versions),
|
|
62
64
|
distTags: pkgMetadata['dist-tags'],
|
|
65
|
+
registryUrl,
|
|
63
66
|
});
|
|
64
67
|
return;
|
|
65
68
|
}
|
|
@@ -89,10 +92,22 @@ const downloadMetadata = (pkgName, options) => {
|
|
|
89
92
|
request.on('close', () => {
|
|
90
93
|
request.removeAllListeners();
|
|
91
94
|
});
|
|
95
|
+
if (options?.unref) {
|
|
96
|
+
// A background check must never delay the process exit: drop the
|
|
97
|
+
// socket's ref (also for a pooled keep-alive socket, which the agent
|
|
98
|
+
// ref's again right before handing it out).
|
|
99
|
+
request.once('socket', (socket) => {
|
|
100
|
+
socket.unref?.();
|
|
101
|
+
});
|
|
102
|
+
}
|
|
92
103
|
});
|
|
93
104
|
};
|
|
94
105
|
const getCacheDir = (name = '@badisi/latest-version') => {
|
|
95
106
|
const homeDir = (0, os_1.homedir)();
|
|
107
|
+
if (process.env.XDG_CACHE_HOME) {
|
|
108
|
+
// an explicit override wins on every platform (also lets tests isolate)
|
|
109
|
+
return (0, path_1.join)(process.env.XDG_CACHE_HOME, name);
|
|
110
|
+
}
|
|
96
111
|
switch (process.platform) {
|
|
97
112
|
case 'darwin':
|
|
98
113
|
return (0, path_1.join)(homeDir, 'Library', 'Caches', name);
|
|
@@ -103,32 +118,61 @@ const getCacheDir = (name = '@badisi/latest-version') => {
|
|
|
103
118
|
}
|
|
104
119
|
};
|
|
105
120
|
const saveMetadataToCache = (pkg) => {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
(0, fs_1.
|
|
121
|
+
try {
|
|
122
|
+
const filePath = (0, path_1.join)(getCacheDir(), `${pkg.name}.json`);
|
|
123
|
+
if (!(0, fs_1.existsSync)((0, path_1.dirname)(filePath))) {
|
|
124
|
+
(0, fs_1.mkdirSync)((0, path_1.dirname)(filePath), { recursive: true });
|
|
125
|
+
}
|
|
126
|
+
(0, fs_1.writeFileSync)(filePath, JSON.stringify(pkg));
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
// a read-only or missing cache dir must not turn into an error
|
|
109
130
|
}
|
|
110
|
-
(0, fs_1.writeFileSync)(filePath, JSON.stringify(pkg));
|
|
111
131
|
};
|
|
132
|
+
/**
|
|
133
|
+
* The cached metadata of a package (from the same registry), and whether it is
|
|
134
|
+
* still younger than `cacheMaxAge`. A stale entry is returned as well so it can
|
|
135
|
+
* serve as a fallback when the registry is unreachable.
|
|
136
|
+
*/
|
|
112
137
|
const getMetadataFromCache = (pkgName, options) => {
|
|
113
138
|
const maxAge = options?.cacheMaxAge ?? exports.ONE_DAY;
|
|
114
|
-
|
|
139
|
+
try {
|
|
115
140
|
const pkgCacheFilePath = (0, path_1.join)(getCacheDir(), `${pkgName}.json`);
|
|
116
|
-
if ((0, fs_1.existsSync)(pkgCacheFilePath)) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
141
|
+
if (!(0, fs_1.existsSync)(pkgCacheFilePath)) {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
const metadata = JSON.parse((0, fs_1.readFileSync)(pkgCacheFilePath).toString());
|
|
145
|
+
if (!metadata ||
|
|
146
|
+
typeof metadata.lastUpdateDate !== 'number' ||
|
|
147
|
+
!metadata.distTags ||
|
|
148
|
+
metadata.registryUrl !== resolveRegistryUrl(pkgName, options)) {
|
|
149
|
+
return undefined;
|
|
121
150
|
}
|
|
151
|
+
const age = Date.now() - metadata.lastUpdateDate;
|
|
152
|
+
return { metadata, fresh: maxAge !== 0 && age >= 0 && age < maxAge };
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return undefined; // unreadable / corrupt cache is the same as no cache
|
|
122
156
|
}
|
|
123
|
-
return undefined; // invalidates cache
|
|
124
157
|
};
|
|
125
158
|
const getRegistryVersions = async (pkgName, tagOrRange, options) => {
|
|
126
159
|
let pkgMetadata;
|
|
127
160
|
if (pkgName.length && options?.useCache) {
|
|
128
|
-
|
|
129
|
-
if (
|
|
130
|
-
pkgMetadata =
|
|
131
|
-
|
|
161
|
+
const cached = getMetadataFromCache(pkgName, options);
|
|
162
|
+
if (cached?.fresh) {
|
|
163
|
+
pkgMetadata = cached.metadata;
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
try {
|
|
167
|
+
pkgMetadata = await downloadMetadata(pkgName, options);
|
|
168
|
+
saveMetadataToCache(pkgMetadata);
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
if (!cached) {
|
|
172
|
+
throw err;
|
|
173
|
+
}
|
|
174
|
+
pkgMetadata = cached.metadata; // stale beats nothing
|
|
175
|
+
}
|
|
132
176
|
}
|
|
133
177
|
}
|
|
134
178
|
else if (pkgName.length) {
|
|
@@ -142,11 +186,33 @@ const getRegistryVersions = async (pkgName, tagOrRange, options) => {
|
|
|
142
186
|
versions.wanted = pkgMetadata.distTags[tagOrRange];
|
|
143
187
|
}
|
|
144
188
|
else if (tagOrRange && pkgMetadata?.versions.length) {
|
|
145
|
-
versions.wanted =
|
|
146
|
-
(0, max_satisfying_1.default)(pkgMetadata.versions, tagOrRange) ?? undefined;
|
|
189
|
+
versions.wanted = maxSatisfying(pkgMetadata.versions, tagOrRange);
|
|
147
190
|
}
|
|
148
191
|
return versions;
|
|
149
192
|
};
|
|
193
|
+
/** the highest of `versions` within `range` (npm range syntax), if any */
|
|
194
|
+
const maxSatisfying = (versions, range) => {
|
|
195
|
+
const matching = versions.filter((version) => {
|
|
196
|
+
try {
|
|
197
|
+
return (0, compare_versions_1.satisfies)(version, range);
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
if (!matching.length)
|
|
204
|
+
return undefined;
|
|
205
|
+
return matching.sort(compare_versions_1.compareVersions)[matching.length - 1];
|
|
206
|
+
};
|
|
207
|
+
/** `a > b` for two versions; invalid input counts as "not newer" */
|
|
208
|
+
const gt = (a, b) => {
|
|
209
|
+
try {
|
|
210
|
+
return (0, compare_versions_1.compare)(a, b, '>');
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
150
216
|
const getInstalledVersion = (pkgName, location = 'local') => {
|
|
151
217
|
try {
|
|
152
218
|
if (location === 'globalNpm') {
|
|
@@ -203,17 +269,17 @@ const getInfo = async (pkg, options) => {
|
|
|
203
269
|
...(await getRegistryVersions(pkgInfo.name, pkgInfo.wantedTagOrRange, options)),
|
|
204
270
|
};
|
|
205
271
|
const local = pkgInfo.local && pkgInfo.wanted
|
|
206
|
-
? (
|
|
272
|
+
? gt(pkgInfo.wanted, pkgInfo.local)
|
|
207
273
|
? pkgInfo.wanted
|
|
208
274
|
: false
|
|
209
275
|
: false;
|
|
210
276
|
const globalNpm = pkgInfo.globalNpm && pkgInfo.wanted
|
|
211
|
-
? (
|
|
277
|
+
? gt(pkgInfo.wanted, pkgInfo.globalNpm)
|
|
212
278
|
? pkgInfo.wanted
|
|
213
279
|
: false
|
|
214
280
|
: false;
|
|
215
281
|
const globalYarn = pkgInfo.globalYarn && pkgInfo.wanted
|
|
216
|
-
? (
|
|
282
|
+
? gt(pkgInfo.wanted, pkgInfo.globalYarn)
|
|
217
283
|
? pkgInfo.wanted
|
|
218
284
|
: false
|
|
219
285
|
: false;
|
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
|
@@ -28,6 +28,15 @@ export declare function readZipEntryWithLocation(zipPath: string, matches: (name
|
|
|
28
28
|
* fetched by a single Range request (unknown, empty or exotic compression).
|
|
29
29
|
*/
|
|
30
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
|
+
}
|
|
31
40
|
/** The server answered a Range request with the whole body (HTTP 200). */
|
|
32
41
|
export declare class RangeUnsupportedError extends Error {
|
|
33
42
|
readonly response: Response;
|
|
@@ -39,34 +48,49 @@ interface ContentRange {
|
|
|
39
48
|
total: number;
|
|
40
49
|
}
|
|
41
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;
|
|
42
57
|
/**
|
|
43
58
|
* Fetch one entry whose compressed bytes are already located (the ppk's own
|
|
44
59
|
* publish-time report) and decompress it. Returns the raw entry content.
|
|
45
60
|
*/
|
|
46
|
-
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<{
|
|
47
62
|
data: Buffer;
|
|
48
63
|
fetchedBytes: number;
|
|
49
64
|
totalBytes: number;
|
|
50
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
|
+
}
|
|
51
74
|
/**
|
|
52
75
|
* yauzl reader over HTTP Range. Header reads (EOCD, central directory, local
|
|
53
|
-
* file headers) are widened to CHUNK_BYTES and cached
|
|
54
|
-
* overlapping
|
|
55
|
-
*
|
|
56
|
-
* bytes following the local file header) before
|
|
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.
|
|
57
82
|
*/
|
|
58
83
|
export declare class HttpRangeReader extends RandomAccessReader {
|
|
59
84
|
private readonly url;
|
|
60
85
|
readonly totalSize: number;
|
|
86
|
+
private readonly options;
|
|
87
|
+
/** sorted by `start`, never overlapping */
|
|
61
88
|
private chunks;
|
|
62
89
|
/** span a header read inside it should widen to (see `hintEntry`) */
|
|
63
90
|
private hint;
|
|
64
91
|
requests: number;
|
|
65
92
|
fetchedBytes: number;
|
|
66
|
-
constructor(url: string, totalSize: number, tail?:
|
|
67
|
-
start: number;
|
|
68
|
-
data: Buffer;
|
|
69
|
-
});
|
|
93
|
+
constructor(url: string, totalSize: number, tail?: Chunk, options?: HttpRangeReaderOptions);
|
|
70
94
|
/**
|
|
71
95
|
* The next entry to be read: yauzl first reads its local file header, and
|
|
72
96
|
* widening that read to the entry's data makes the data stream a cache hit
|
|
@@ -74,13 +98,21 @@ export declare class HttpRangeReader extends RandomAccessReader {
|
|
|
74
98
|
* may understate the local header's extra field, hence the slack.
|
|
75
99
|
*/
|
|
76
100
|
hintEntry(entry: Entry): void;
|
|
77
|
-
/** Cache `data` at `start`, merging with any chunk it
|
|
101
|
+
/** Cache `data` at `start`, merging with any chunk it overlaps. */
|
|
78
102
|
addChunk(start: number, data: Buffer): void;
|
|
79
|
-
/**
|
|
80
|
-
|
|
81
|
-
private
|
|
82
|
-
|
|
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` */
|
|
83
113
|
private cachedSuffixStart;
|
|
114
|
+
/** start of the first cached chunk at or after `position` */
|
|
115
|
+
private nextCachedStart;
|
|
84
116
|
_readStreamForRange(start: number, end: number): Readable;
|
|
85
117
|
read(buffer: Buffer, offset: number, length: number, position: number, callback: (err: Error | null, bytesRead?: number) => void): void;
|
|
86
118
|
}
|
|
@@ -102,7 +134,7 @@ export type RemoteZip = {
|
|
|
102
134
|
* Open a remote zip by fetching its tail (EOCD + central directory for
|
|
103
135
|
* typical archives) and letting yauzl read the rest on demand.
|
|
104
136
|
*/
|
|
105
|
-
export declare function openRemoteZip(url: string): Promise<RemoteZip>;
|
|
137
|
+
export declare function openRemoteZip(url: string, options?: RangeOptions): Promise<RemoteZip>;
|
|
106
138
|
/**
|
|
107
139
|
* Read the first matching entry of an opened remote zip, then close it. With
|
|
108
140
|
* `reader` given, the entry's header and data are fetched in one request.
|