Skip to content

Commit edbc25a

Browse files
committed
1 parent 8b0e755 commit edbc25a

39 files changed

+4100
-46
lines changed

mock-registry/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"json-stringify-safe": "^5.0.1",
5151
"nock": "^13.3.0",
5252
"npm-package-arg": "^10.1.0",
53-
"pacote": "^15.0.8",
53+
"pacote": "^16.0.0",
5454
"tap": "^16.3.4"
5555
}
5656
}

node_modules/.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
!/@npmcli/installed-package-contents
2626
!/@npmcli/map-workspaces
2727
!/@npmcli/metavuln-calculator
28+
!/@npmcli/metavuln-calculator/node_modules/
29+
/@npmcli/metavuln-calculator/node_modules/*
30+
!/@npmcli/metavuln-calculator/node_modules/npm-registry-fetch
31+
!/@npmcli/metavuln-calculator/node_modules/pacote
2832
!/@npmcli/name-from-folder
2933
!/@npmcli/node-gyp
3034
!/@npmcli/package-json
@@ -209,7 +213,7 @@
209213
!/pacote
210214
!/pacote/node_modules/
211215
/pacote/node_modules/*
212-
!/pacote/node_modules/npm-registry-fetch
216+
!/pacote/node_modules/minipass
213217
!/parse-conflict-json
214218
!/path-is-absolute
215219
!/path-key
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
The ISC License
2+
3+
Copyright (c) Isaac Z. Schlueter, Kat Marchán, npm, Inc., and Contributors
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15+
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#!/usr/bin/env node
2+
3+
const run = conf => {
4+
const pacote = require('../')
5+
switch (conf._[0]) {
6+
case 'resolve':
7+
case 'manifest':
8+
case 'packument':
9+
if (conf._[0] === 'resolve' && conf.long) {
10+
return pacote.manifest(conf._[1], conf).then(mani => ({
11+
resolved: mani._resolved,
12+
integrity: mani._integrity,
13+
from: mani._from,
14+
}))
15+
}
16+
return pacote[conf._[0]](conf._[1], conf)
17+
18+
case 'tarball':
19+
if (!conf._[2] || conf._[2] === '-') {
20+
return pacote.tarball.stream(conf._[1], stream => {
21+
stream.pipe(
22+
conf.testStdout ||
23+
/* istanbul ignore next */
24+
process.stdout
25+
)
26+
// make sure it resolves something falsey
27+
return stream.promise().then(() => {
28+
return false
29+
})
30+
}, conf)
31+
} else {
32+
return pacote.tarball.file(conf._[1], conf._[2], conf)
33+
}
34+
35+
case 'extract':
36+
return pacote.extract(conf._[1], conf._[2], conf)
37+
38+
default: /* istanbul ignore next */ {
39+
throw new Error(`bad command: ${conf._[0]}`)
40+
}
41+
}
42+
}
43+
44+
const version = require('../package.json').version
45+
const usage = () =>
46+
`Pacote - The JavaScript Package Handler, v${version}
47+
48+
Usage:
49+
50+
pacote resolve <spec>
51+
Resolve a specifier and output the fully resolved target
52+
Returns integrity and from if '--long' flag is set.
53+
54+
pacote manifest <spec>
55+
Fetch a manifest and print to stdout
56+
57+
pacote packument <spec>
58+
Fetch a full packument and print to stdout
59+
60+
pacote tarball <spec> [<filename>]
61+
Fetch a package tarball and save to <filename>
62+
If <filename> is missing or '-', the tarball will be streamed to stdout.
63+
64+
pacote extract <spec> <folder>
65+
Extract a package to the destination folder.
66+
67+
Configuration values all match the names of configs passed to npm, or
68+
options passed to Pacote. Additional flags for this executable:
69+
70+
--long Print an object from 'resolve', including integrity and spec.
71+
--json Print result objects as JSON rather than node's default.
72+
(This is the default if stdout is not a TTY.)
73+
--help -h Print this helpful text.
74+
75+
For example '--cache=/path/to/folder' will use that folder as the cache.
76+
`
77+
78+
const shouldJSON = (conf, result) =>
79+
conf.json ||
80+
!process.stdout.isTTY &&
81+
conf.json === undefined &&
82+
result &&
83+
typeof result === 'object'
84+
85+
const pretty = (conf, result) =>
86+
shouldJSON(conf, result) ? JSON.stringify(result, 0, 2) : result
87+
88+
let addedLogListener = false
89+
const main = args => {
90+
const conf = parse(args)
91+
if (conf.help || conf.h) {
92+
return console.log(usage())
93+
}
94+
95+
if (!addedLogListener) {
96+
process.on('log', console.error)
97+
addedLogListener = true
98+
}
99+
100+
try {
101+
return run(conf)
102+
.then(result => result && console.log(pretty(conf, result)))
103+
.catch(er => {
104+
console.error(er)
105+
process.exit(1)
106+
})
107+
} catch (er) {
108+
console.error(er.message)
109+
console.error(usage())
110+
}
111+
}
112+
113+
const parseArg = arg => {
114+
const split = arg.slice(2).split('=')
115+
const k = split.shift()
116+
const v = split.join('=')
117+
const no = /^no-/.test(k) && !v
118+
const key = (no ? k.slice(3) : k)
119+
.replace(/^tag$/, 'defaultTag')
120+
.replace(/-([a-z])/g, (_, c) => c.toUpperCase())
121+
const value = v ? v.replace(/^~/, process.env.HOME) : !no
122+
return { key, value }
123+
}
124+
125+
const parse = args => {
126+
const conf = {
127+
_: [],
128+
cache: process.env.HOME + '/.npm/_cacache',
129+
}
130+
let dashdash = false
131+
args.forEach(arg => {
132+
if (dashdash) {
133+
conf._.push(arg)
134+
} else if (arg === '--') {
135+
dashdash = true
136+
} else if (arg === '-h') {
137+
conf.help = true
138+
} else if (/^--/.test(arg)) {
139+
const { key, value } = parseArg(arg)
140+
conf[key] = value
141+
} else {
142+
conf._.push(arg)
143+
}
144+
})
145+
return conf
146+
}
147+
148+
if (module === require.main) {
149+
main(process.argv.slice(2))
150+
} else {
151+
module.exports = {
152+
main,
153+
run,
154+
usage,
155+
parseArg,
156+
parse,
157+
}
158+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
const Fetcher = require('./fetcher.js')
2+
const FileFetcher = require('./file.js')
3+
const { Minipass } = require('minipass')
4+
const tarCreateOptions = require('./util/tar-create-options.js')
5+
const packlist = require('npm-packlist')
6+
const tar = require('tar')
7+
const _prepareDir = Symbol('_prepareDir')
8+
const { resolve } = require('path')
9+
const _readPackageJson = Symbol.for('package.Fetcher._readPackageJson')
10+
11+
const runScript = require('@npmcli/run-script')
12+
13+
const _tarballFromResolved = Symbol.for('pacote.Fetcher._tarballFromResolved')
14+
class DirFetcher extends Fetcher {
15+
constructor (spec, opts) {
16+
super(spec, opts)
17+
// just the fully resolved filename
18+
this.resolved = this.spec.fetchSpec
19+
20+
this.tree = opts.tree || null
21+
this.Arborist = opts.Arborist || null
22+
}
23+
24+
// exposes tarCreateOptions as public API
25+
static tarCreateOptions (manifest) {
26+
return tarCreateOptions(manifest)
27+
}
28+
29+
get types () {
30+
return ['directory']
31+
}
32+
33+
[_prepareDir] () {
34+
return this.manifest().then(mani => {
35+
if (!mani.scripts || !mani.scripts.prepare) {
36+
return
37+
}
38+
39+
// we *only* run prepare.
40+
// pre/post-pack is run by the npm CLI for publish and pack,
41+
// but this function is *also* run when installing git deps
42+
const stdio = this.opts.foregroundScripts ? 'inherit' : 'pipe'
43+
44+
// hide the banner if silent opt is passed in, or if prepare running
45+
// in the background.
46+
const banner = this.opts.silent ? false : stdio === 'inherit'
47+
48+
return runScript({
49+
pkg: mani,
50+
event: 'prepare',
51+
path: this.resolved,
52+
stdio,
53+
banner,
54+
env: {
55+
npm_package_resolved: this.resolved,
56+
npm_package_integrity: this.integrity,
57+
npm_package_json: resolve(this.resolved, 'package.json'),
58+
},
59+
})
60+
})
61+
}
62+
63+
[_tarballFromResolved] () {
64+
if (!this.tree && !this.Arborist) {
65+
throw new Error('DirFetcher requires either a tree or an Arborist constructor to pack')
66+
}
67+
68+
const stream = new Minipass()
69+
stream.resolved = this.resolved
70+
stream.integrity = this.integrity
71+
72+
const { prefix, workspaces } = this.opts
73+
74+
// run the prepare script, get the list of files, and tar it up
75+
// pipe to the stream, and proxy errors the chain.
76+
this[_prepareDir]()
77+
.then(async () => {
78+
if (!this.tree) {
79+
const arb = new this.Arborist({ path: this.resolved })
80+
this.tree = await arb.loadActual()
81+
}
82+
return packlist(this.tree, { path: this.resolved, prefix, workspaces })
83+
})
84+
.then(files => tar.c(tarCreateOptions(this.package), files)
85+
.on('error', er => stream.emit('error', er)).pipe(stream))
86+
.catch(er => stream.emit('error', er))
87+
return stream
88+
}
89+
90+
manifest () {
91+
if (this.package) {
92+
return Promise.resolve(this.package)
93+
}
94+
95+
return this[_readPackageJson](this.resolved + '/package.json')
96+
.then(mani => this.package = {
97+
...mani,
98+
_integrity: this.integrity && String(this.integrity),
99+
_resolved: this.resolved,
100+
_from: this.from,
101+
})
102+
}
103+
104+
packument () {
105+
return FileFetcher.prototype.packument.apply(this)
106+
}
107+
}
108+
module.exports = DirFetcher

0 commit comments

Comments
 (0)