diff options
author | npm team <ops+robot@npmjs.com> | 2021-08-19 17:47:33 +0000 |
---|---|---|
committer | Luigi Pinca <luigipinca@gmail.com> | 2021-08-20 14:53:49 +0200 |
commit | 248f4c376444d0202f1d08f039c59485d6aea2e9 (patch) | |
tree | 270bcf6dd5734fa43ea1603bfc4a066b6f8f7926 /deps/npm/lib | |
parent | 279162cf98b22ec5d0afc182ea22c7a782ccd083 (diff) | |
download | node-new-248f4c376444d0202f1d08f039c59485d6aea2e9.tar.gz |
deps: upgrade npm to 7.21.0
PR-URL: https://github.com/nodejs/node/pull/39813
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'deps/npm/lib')
-rw-r--r-- | deps/npm/lib/cache.js | 128 | ||||
-rw-r--r-- | deps/npm/lib/cli.js | 3 | ||||
-rw-r--r-- | deps/npm/lib/set.js | 2 | ||||
-rw-r--r-- | deps/npm/lib/test.js | 10 | ||||
-rw-r--r-- | deps/npm/lib/utils/config/definitions.js | 6 | ||||
-rw-r--r-- | deps/npm/lib/utils/config/describe-all.js | 5 | ||||
-rw-r--r-- | deps/npm/lib/utils/did-you-mean.js | 8 | ||||
-rw-r--r-- | deps/npm/lib/utils/error-message.js | 8 | ||||
-rw-r--r-- | deps/npm/lib/utils/format-bytes.js | 22 | ||||
-rw-r--r-- | deps/npm/lib/utils/tar.js | 10 | ||||
-rw-r--r-- | deps/npm/lib/view.js | 6 |
11 files changed, 157 insertions, 51 deletions
diff --git a/deps/npm/lib/cache.js b/deps/npm/lib/cache.js index 55fb3e8636..aed2cce31e 100644 --- a/deps/npm/lib/cache.js +++ b/deps/npm/lib/cache.js @@ -4,7 +4,59 @@ const log = require('npmlog') const pacote = require('pacote') const path = require('path') const rimraf = promisify(require('rimraf')) +const semver = require('semver') const BaseCommand = require('./base-command.js') +const npa = require('npm-package-arg') +const jsonParse = require('json-parse-even-better-errors') + +const searchCachePackage = async (path, spec, cacheKeys) => { + const parsed = npa(spec) + if (parsed.rawSpec !== '' && parsed.type === 'tag') + throw new Error(`Cannot list cache keys for a tagged package.`) + const searchMFH = new RegExp(`^make-fetch-happen:request-cache:.*(?<!/[@a-zA-Z]+)/${parsed.name}/-/(${parsed.name}[^/]+.tgz)$`) + const searchPack = new RegExp(`^make-fetch-happen:request-cache:.*/${parsed.escapedName}$`) + const results = new Set() + cacheKeys = new Set(cacheKeys) + for (const key of cacheKeys) { + // match on the public key registry url format + if (searchMFH.test(key)) { + // extract the version from the filename + const filename = key.match(searchMFH)[1] + const noExt = filename.slice(0, -4) + const noScope = `${parsed.name.split('/').pop()}-` + const ver = noExt.slice(noScope.length) + if (semver.satisfies(ver, parsed.rawSpec)) + results.add(key) + continue + } + // is this key a packument? + if (!searchPack.test(key)) + continue + + results.add(key) + let packument, details + try { + details = await cacache.get(path, key) + packument = jsonParse(details.data) + } catch (_) { + // if we couldn't parse the packument, abort + continue + } + if (!packument.versions || typeof packument.versions !== 'object') + continue + // assuming this is a packument + for (const ver of Object.keys(packument.versions)) { + if (semver.satisfies(ver, parsed.rawSpec)) { + if (packument.versions[ver].dist + && typeof packument.versions[ver].dist === 'object' + && packument.versions[ver].dist.tarball !== undefined + && cacheKeys.has(`make-fetch-happen:request-cache:${packument.versions[ver].dist.tarball}`)) + results.add(`make-fetch-happen:request-cache:${packument.versions[ver].dist.tarball}`) + } + } + } + return results +} class Cache extends BaseCommand { static get description () { @@ -29,7 +81,8 @@ class Cache extends BaseCommand { 'add <tarball url>', 'add <git url>', 'add <name>@<version>', - 'clean', + 'clean [<key>]', + 'ls [<name>@<version>]', 'verify', ] } @@ -37,13 +90,15 @@ class Cache extends BaseCommand { async completion (opts) { const argv = opts.conf.argv.remain if (argv.length === 2) - return ['add', 'clean', 'verify'] + return ['add', 'clean', 'verify', 'ls', 'delete'] // TODO - eventually... switch (argv[2]) { case 'verify': case 'clean': case 'add': + case 'ls': + case 'delete': return [] } } @@ -61,6 +116,8 @@ class Cache extends BaseCommand { return await this.add(args) case 'verify': case 'check': return await this.verify() + case 'ls': + return await this.ls(args) default: throw Object.assign(new Error(this.usage), { code: 'EUSAGE' }) } @@ -68,27 +125,38 @@ class Cache extends BaseCommand { // npm cache clean [pkg]* async clean (args) { - if (args.length) - throw new Error('npm cache clear does not accept arguments') - const cachePath = path.join(this.npm.cache, '_cacache') - if (!this.npm.config.get('force')) { - throw new Error(`As of npm@5, the npm cache self-heals from corruption issues -by treating integrity mismatches as cache misses. As a result, -data extracted from the cache is guaranteed to be valid. If you -want to make sure everything is consistent, use \`npm cache verify\` -instead. Deleting the cache can only make npm go slower, and is -not likely to correct any problems you may be encountering! - -On the other hand, if you're debugging an issue with the installer, -or race conditions that depend on the timing of writing to an empty -cache, you can use \`npm install --cache /tmp/empty-cache\` to use a -temporary cache instead of nuking the actual one. - -If you're sure you want to delete the entire cache, rerun this command -with --force.`) + if (args.length === 0) { + if (!this.npm.config.get('force')) { + throw new Error(`As of npm@5, the npm cache self-heals from corruption issues + by treating integrity mismatches as cache misses. As a result, + data extracted from the cache is guaranteed to be valid. If you + want to make sure everything is consistent, use \`npm cache verify\` + instead. Deleting the cache can only make npm go slower, and is + not likely to correct any problems you may be encountering! + + On the other hand, if you're debugging an issue with the installer, + or race conditions that depend on the timing of writing to an empty + cache, you can use \`npm install --cache /tmp/empty-cache\` to use a + temporary cache instead of nuking the actual one. + + If you're sure you want to delete the entire cache, rerun this command + with --force.`) + } + return rimraf(cachePath) + } + for (const key of args) { + let entry + try { + entry = await cacache.get(cachePath, key) + } catch (err) { + this.npm.log.warn(`Not Found: ${key}`) + break + } + this.npm.output(`Deleted: ${key}`) + await cacache.rm.entry(cachePath, key) + await cacache.rm.content(cachePath, entry.integrity) } - return rimraf(cachePath) } // npm cache add <tarball-url>... @@ -131,6 +199,24 @@ with --force.`) this.npm.output(`Index entries: ${stats.totalEntries}`) this.npm.output(`Finished in ${stats.runTime.total / 1000}s`) } + + // npm cache ls [--package <spec> ...] + async ls (specs) { + const cachePath = path.join(this.npm.cache, '_cacache') + const cacheKeys = Object.keys(await cacache.ls(cachePath)) + if (specs.length > 0) { + // get results for each package spec specified + const results = new Set() + for (const spec of specs) { + const keySet = await searchCachePackage(cachePath, spec, cacheKeys) + for (const key of keySet) + results.add(key) + } + [...results].sort((a, b) => a.localeCompare(b, 'en')).forEach(key => this.npm.output(key)) + return + } + cacheKeys.sort((a, b) => a.localeCompare(b, 'en')).forEach(key => this.npm.output(key)) + } } module.exports = Cache diff --git a/deps/npm/lib/cli.js b/deps/npm/lib/cli.js index 9544f8451f..e33ac91fa5 100644 --- a/deps/npm/lib/cli.js +++ b/deps/npm/lib/cli.js @@ -27,7 +27,8 @@ module.exports = async (process) => { if (process.argv[1][process.argv[1].length - 1] === 'g') process.argv.splice(1, 1, 'npm', '-g') - log.verbose('cli', process.argv) + const replaceInfo = require('../lib/utils/replace-info.js') + log.verbose('cli', replaceInfo(process.argv)) log.info('using', 'npm@%s', npm.version) log.info('using', 'node@%s', process.version) diff --git a/deps/npm/lib/set.js b/deps/npm/lib/set.js index 74a002cd63..a9f16f3b34 100644 --- a/deps/npm/lib/set.js +++ b/deps/npm/lib/set.js @@ -22,7 +22,7 @@ class Set extends BaseCommand { exec (args, cb) { if (!args.length) - return cb(this.usage) + return cb(this.usageError()) this.npm.commands.config(['set'].concat(args), cb) } } diff --git a/deps/npm/lib/test.js b/deps/npm/lib/test.js index e78fdf0c78..8ab1e85823 100644 --- a/deps/npm/lib/test.js +++ b/deps/npm/lib/test.js @@ -19,15 +19,5 @@ class Test extends LifecycleCmd { 'script-shell', ] } - - exec (args, cb) { - super.exec(args, er => { - if (er && er.code === 'ELIFECYCLE') { - /* eslint-disable standard/no-callback-literal */ - cb('Test failed. See above for more details.') - } else - cb(er) - }) - } } module.exports = Test diff --git a/deps/npm/lib/utils/config/definitions.js b/deps/npm/lib/utils/config/definitions.js index 36b8a84a61..c717816278 100644 --- a/deps/npm/lib/utils/config/definitions.js +++ b/deps/npm/lib/utils/config/definitions.js @@ -160,6 +160,12 @@ define('access', { (and installable) set \`--access=public\`. The only valid values for \`access\` are \`public\` and \`restricted\`. Unscoped packages _always_ have an access level of \`public\`. + + Note: Using the \`--access\` flag on the \`npm publish\` command will only + set the package access level on the initial publish of the package. Any + subsequent \`npm publish\` commands using the \`--access\` flag will not + have an effect to the access level. To make changes to the access level + after the initial publish use \`npm access\`. `, flatten, }) diff --git a/deps/npm/lib/utils/config/describe-all.js b/deps/npm/lib/utils/config/describe-all.js index 5fb785f083..c8a973cc0f 100644 --- a/deps/npm/lib/utils/config/describe-all.js +++ b/deps/npm/lib/utils/config/describe-all.js @@ -11,6 +11,9 @@ const describeAll = () => { } return Object.entries(definitions).sort(sort) .map(([key, def]) => def.describe()) - .join('\n\n') + .join( + '\n\n<!-- automatically generated, do not edit manually -->\n' + + '<!-- see lib/utils/config/definitions.js -->\n\n' + ) } module.exports = describeAll diff --git a/deps/npm/lib/utils/did-you-mean.js b/deps/npm/lib/utils/did-you-mean.js index 98133196e3..0cfdd03525 100644 --- a/deps/npm/lib/utils/did-you-mean.js +++ b/deps/npm/lib/utils/did-you-mean.js @@ -1,10 +1,10 @@ -const leven = require('leven') +const { distance } = require('fastest-levenshtein') const readJson = require('read-package-json-fast') const { cmdList } = require('./cmd-list.js') const didYouMean = async (npm, path, scmd) => { const bestCmd = cmdList - .filter(cmd => leven(scmd, cmd) < scmd.length * 0.4 && scmd !== cmd) + .filter(cmd => distance(scmd, cmd) < scmd.length * 0.4 && scmd !== cmd) .map(str => ` npm ${str} # ${npm.commands[str].description}`) const pkg = await readJson(`${path}/package.json`) @@ -12,13 +12,13 @@ const didYouMean = async (npm, path, scmd) => { // We would already be suggesting this in `npm x` so omit them here const runScripts = ['stop', 'start', 'test', 'restart'] const bestRun = Object.keys(scripts || {}) - .filter(cmd => leven(scmd, cmd) < scmd.length * 0.4 && + .filter(cmd => distance(scmd, cmd) < scmd.length * 0.4 && !runScripts.includes(cmd)) .map(str => ` npm run ${str} # run the "${str}" package script`) const { bin } = pkg const bestBin = Object.keys(bin || {}) - .filter(cmd => leven(scmd, cmd) < scmd.length * 0.4) + .filter(cmd => distance(scmd, cmd) < scmd.length * 0.4) .map(str => ` npm exec ${str} # run the "${str}" command from either this or a remote npm package`) const best = [...bestCmd, ...bestRun, ...bestBin] diff --git a/deps/npm/lib/utils/error-message.js b/deps/npm/lib/utils/error-message.js index 3b590f712e..da97195dd0 100644 --- a/deps/npm/lib/utils/error-message.js +++ b/deps/npm/lib/utils/error-message.js @@ -99,9 +99,9 @@ module.exports = (er, npm) => { case 'EJSONPARSE': // Check whether we ran into a conflict in our own package.json - if (er.file === resolve(npm.prefix, 'package.json')) { + if (er.path === resolve(npm.prefix, 'package.json')) { const { isDiff } = require('parse-conflict-json') - const txt = require('fs').readFileSync(er.file, 'utf8') + const txt = require('fs').readFileSync(er.path, 'utf8') .replace(/\r\n/g, '\n') if (isDiff(txt)) { detail.push([ @@ -109,9 +109,7 @@ module.exports = (er, npm) => { [ 'Merge conflict detected in your package.json.', '', - 'Please resolve the package.json conflict and retry the command:', - '', - `$ ${process.argv.join(' ')}`, + 'Please resolve the package.json conflict and retry.', ].join('\n'), ]) break diff --git a/deps/npm/lib/utils/format-bytes.js b/deps/npm/lib/utils/format-bytes.js new file mode 100644 index 0000000000..87fb561aab --- /dev/null +++ b/deps/npm/lib/utils/format-bytes.js @@ -0,0 +1,22 @@ +// Convert bytes to printable output, for file reporting in tarballs +// Only supports up to GB because that's way larger than anything the registry +// supports anyways. + +const formatBytes = (bytes, space = true) => { + let spacer = '' + if (space) + spacer = ' ' + + if (bytes < 1000) // B + return `${bytes}${spacer}B` + + if (bytes < 1000000) // kB + return `${(bytes / 1000).toFixed(1)}${spacer}kB` + + if (bytes < 1000000000) // MB + return `${(bytes / 1000000).toFixed(1)}${spacer}MB` + + return `${(bytes / 1000000000).toFixed(1)}${spacer}GB` +} + +module.exports = formatBytes diff --git a/deps/npm/lib/utils/tar.js b/deps/npm/lib/utils/tar.js index 9e7c332953..c3071c1bd4 100644 --- a/deps/npm/lib/utils/tar.js +++ b/deps/npm/lib/utils/tar.js @@ -1,7 +1,7 @@ const tar = require('tar') const ssri = require('ssri') const npmlog = require('npmlog') -const byteSize = require('byte-size') +const formatBytes = require('./format-bytes.js') const columnify = require('columnify') const logTar = (tarball, opts = {}) => { @@ -11,9 +11,9 @@ const logTar = (tarball, opts = {}) => { log.notice('=== Tarball Contents ===') if (tarball.files.length) { log.notice('', columnify(tarball.files.map((f) => { - const bytes = byteSize(f.size) + const bytes = formatBytes(f.size, false) return (/^node_modules\//.test(f.path)) ? null - : { path: f.path, size: `${bytes.value}${bytes.unit}` } + : { path: f.path, size: `${bytes}` } }).filter(f => f), { include: ['size', 'path'], showHeaders: false, @@ -28,8 +28,8 @@ const logTar = (tarball, opts = {}) => { { name: 'name:', value: tarball.name }, { name: 'version:', value: tarball.version }, tarball.filename && { name: 'filename:', value: tarball.filename }, - { name: 'package size:', value: byteSize(tarball.size) }, - { name: 'unpacked size:', value: byteSize(tarball.unpackedSize) }, + { name: 'package size:', value: formatBytes(tarball.size) }, + { name: 'unpacked size:', value: formatBytes(tarball.unpackedSize) }, { name: 'shasum:', value: tarball.shasum }, { name: 'integrity:', diff --git a/deps/npm/lib/view.js b/deps/npm/lib/view.js index 47e631f556..f4fc5974ee 100644 --- a/deps/npm/lib/view.js +++ b/deps/npm/lib/view.js @@ -1,6 +1,5 @@ // npm view [pkg [pkg ...]] -const byteSize = require('byte-size') const color = require('ansicolors') const columns = require('cli-columns') const fs = require('fs') @@ -8,6 +7,7 @@ const jsonParse = require('json-parse-even-better-errors') const log = require('npmlog') const npa = require('npm-package-arg') const { resolve } = require('path') +const formatBytes = require('./utils/format-bytes.js') const relativeDate = require('tiny-relative-date') const semver = require('semver') const style = require('ansistyles') @@ -315,7 +315,7 @@ class View extends BaseCommand { tags.push(`${style.bright(color.green(t))}: ${version}`) }) const unpackedSize = manifest.dist.unpackedSize && - byteSize(manifest.dist.unpackedSize) + formatBytes(manifest.dist.unpackedSize, true) const licenseField = manifest.license || 'Proprietary' const info = { name: color.green(manifest.name), @@ -356,7 +356,7 @@ class View extends BaseCommand { manifest.dist.integrity && color.yellow(manifest.dist.integrity), fileCount: manifest.dist.fileCount && color.yellow(manifest.dist.fileCount), - unpackedSize: unpackedSize && color.yellow(unpackedSize.value) + ' ' + unpackedSize.unit, + unpackedSize: unpackedSize && color.yellow(unpackedSize), } if (info.license.toLowerCase().trim() === 'proprietary') info.license = style.bright(color.red(info.license)) |