Skip to content

Commit b547e0d

Browse files
authored
fix: use @npmcli/package-json (#356)
1 parent 066ead2 commit b547e0d

File tree

9 files changed

+45
-45
lines changed

9 files changed

+45
-45
lines changed

lib/dir.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class DirFetcher extends Fetcher {
8787
return Promise.resolve(this.package)
8888
}
8989

90-
return this[_readPackageJson](this.resolved + '/package.json')
90+
return this[_readPackageJson](this.resolved)
9191
.then(mani => this.package = {
9292
...mani,
9393
_integrity: this.integrity && String(this.integrity),

lib/fetcher.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
const npa = require('npm-package-arg')
77
const ssri = require('ssri')
8-
const { promisify } = require('util')
98
const { basename, dirname } = require('path')
109
const tar = require('tar')
1110
const { log } = require('proc-log')
@@ -16,12 +15,14 @@ const cacache = require('cacache')
1615
const isPackageBin = require('./util/is-package-bin.js')
1716
const removeTrailingSlashes = require('./util/trailing-slashes.js')
1817
const getContents = require('@npmcli/installed-package-contents')
19-
const readPackageJsonFast = require('read-package-json-fast')
20-
const readPackageJson = promisify(require('read-package-json'))
18+
const PackageJson = require('@npmcli/package-json')
2119
const { Minipass } = require('minipass')
22-
2320
const cacheDir = require('./util/cache-dir.js')
2421

22+
// Pacote is only concerned with the package.json contents
23+
const packageJsonPrepare = (p) => PackageJson.prepare(p).then(pkg => pkg.content)
24+
const packageJsonNormalize = (p) => PackageJson.normalize(p).then(pkg => pkg.content)
25+
2526
// Private methods.
2627
// Child classes should not have to override these.
2728
// Users should never call them.
@@ -93,9 +94,9 @@ class FetcherBase {
9394
this.fullMetadata = this.before ? true : !!opts.fullMetadata
9495
this.fullReadJson = !!opts.fullReadJson
9596
if (this.fullReadJson) {
96-
this[_readPackageJson] = readPackageJson
97+
this[_readPackageJson] = packageJsonPrepare
9798
} else {
98-
this[_readPackageJson] = readPackageJsonFast
99+
this[_readPackageJson] = packageJsonNormalize
99100
}
100101

101102
// rrh is a registry hostname or 'never' or 'always'

lib/file.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const Fetcher = require('./fetcher.js')
21
const fsm = require('fs-minipass')
32
const cacache = require('cacache')
4-
const _tarballFromResolved = Symbol.for('pacote.Fetcher._tarballFromResolved')
5-
const _exeBins = Symbol('_exeBins')
63
const { resolve } = require('path')
7-
const fs = require('fs')
4+
const { stat, chmod } = require('fs/promises')
5+
const Fetcher = require('./fetcher.js')
6+
7+
const _exeBins = Symbol('_exeBins')
8+
const _tarballFromResolved = Symbol.for('pacote.Fetcher._tarballFromResolved')
89
const _readPackageJson = Symbol.for('package.Fetcher._readPackageJson')
910

1011
class FileFetcher extends Fetcher {
@@ -26,7 +27,7 @@ class FileFetcher extends Fetcher {
2627
// have to unpack the tarball for this.
2728
return cacache.tmp.withTmp(this.cache, this.opts, dir =>
2829
this.extract(dir)
29-
.then(() => this[_readPackageJson](dir + '/package.json'))
30+
.then(() => this[_readPackageJson](dir))
3031
.then(mani => this.package = {
3132
...mani,
3233
_integrity: this.integrity && String(this.integrity),
@@ -40,31 +41,31 @@ class FileFetcher extends Fetcher {
4041
return Promise.resolve()
4142
}
4243

43-
return Promise.all(Object.keys(pkg.bin).map(k => new Promise(res => {
44+
return Promise.all(Object.keys(pkg.bin).map(async k => {
4445
const script = resolve(dest, pkg.bin[k])
4546
// Best effort. Ignore errors here, the only result is that
4647
// a bin script is not executable. But if it's missing or
4748
// something, we just leave it for a later stage to trip over
4849
// when we can provide a more useful contextual error.
49-
fs.stat(script, (er, st) => {
50-
if (er) {
51-
return res()
52-
}
50+
try {
51+
const st = await stat(script)
5352
const mode = st.mode | 0o111
5453
if (mode === st.mode) {
55-
return res()
54+
return
5655
}
57-
fs.chmod(script, mode, res)
58-
})
59-
})))
56+
await chmod(script, mode)
57+
} catch {
58+
// Ignore errors here
59+
}
60+
}))
6061
}
6162

6263
extract (dest) {
6364
// if we've already loaded the manifest, then the super got it.
6465
// but if not, read the unpacked manifest and chmod properly.
6566
return super.extract(dest)
6667
.then(result => this.package ? result
67-
: this[_readPackageJson](dest + '/package.json').then(pkg =>
68+
: this[_readPackageJson](dest).then(pkg =>
6869
this[_exeBins](pkg, dest)).then(() => result))
6970
}
7071

lib/git.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ class GitFetcher extends Fetcher {
156156
[_resolvedFromClone] () {
157157
// do a full or shallow clone, then look at the HEAD
158158
// kind of wasteful, but no other option, really
159-
return this[_clone](dir => this.resolved)
159+
return this[_clone](() => this.resolved)
160160
}
161161

162162
[_prepareDir] (dir) {
163-
return this[_readPackageJson](dir + '/package.json').then(mani => {
163+
return this[_readPackageJson](dir).then(mani => {
164164
// no need if we aren't going to do any preparation.
165165
const scripts = mani.scripts
166166
if (!mani.workspaces && (!scripts || !(
@@ -312,7 +312,7 @@ class GitFetcher extends Fetcher {
312312
return this.spec.hosted && this.resolved
313313
? FileFetcher.prototype.manifest.apply(this)
314314
: this[_clone](dir =>
315-
this[_readPackageJson](dir + '/package.json')
315+
this[_readPackageJson](dir)
316316
.then(mani => this.package = {
317317
...mani,
318318
_resolved: this.resolved,

lib/registry.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const RemoteFetcher = require('./remote.js')
33
const _tarballFromResolved = Symbol.for('pacote.Fetcher._tarballFromResolved')
44
const pacoteVersion = require('../package.json').version
55
const removeTrailingSlashes = require('./util/trailing-slashes.js')
6-
const rpj = require('read-package-json-fast')
6+
const PackageJson = require('@npmcli/package-json')
77
const pickManifest = require('npm-pick-manifest')
88
const ssri = require('ssri')
99
const crypto = require('crypto')
@@ -127,12 +127,12 @@ class RegistryFetcher extends Fetcher {
127127
}
128128

129129
const packument = await this.packument()
130-
let mani = await pickManifest(packument, this.spec.fetchSpec, {
130+
const mani = await new PackageJson().fromContent(pickManifest(packument, this.spec.fetchSpec, {
131131
...this.opts,
132132
defaultTag: this.defaultTag,
133133
before: this.before,
134-
})
135-
mani = rpj.normalize(mani)
134+
})).normalize().then(p => p.content)
135+
136136
/* XXX add ETARGET and E403 revalidation of cached packuments here */
137137

138138
// add _time from packument if fetched with fullMetadata

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"dependencies": {
4747
"@npmcli/git": "^5.0.0",
4848
"@npmcli/installed-package-contents": "^2.0.1",
49+
"@npmcli/package-json": "^5.1.0",
4950
"@npmcli/promise-spawn": "^7.0.0",
5051
"@npmcli/run-script": "^8.0.0",
5152
"cacache": "^18.0.0",
@@ -57,8 +58,6 @@
5758
"npm-registry-fetch": "^16.0.0",
5859
"proc-log": "^4.0.0",
5960
"promise-retry": "^2.0.1",
60-
"read-package-json": "^7.0.0",
61-
"read-package-json-fast": "^3.0.0",
6261
"sigstore": "^2.2.0",
6362
"ssri": "^10.0.0",
6463
"tar": "^6.1.11"

test/bin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pacote.manifest = (spec, conf) => Promise.resolve({
2323
pacote.packument = (spec, conf) => Promise.resolve({ method: 'packument', spec, conf })
2424
pacote.tarball.file = (spec, file, conf) => Promise.resolve({ method: 'tarball', spec, file, conf })
2525
const { Minipass } = require('minipass')
26-
pacote.tarball.stream = (spec, handler, conf) => handler(new Minipass().end('tarball data'))
26+
pacote.tarball.stream = (spec, handler) => handler(new Minipass().end('tarball data'))
2727
pacote.extract = (spec, dest, conf) => Promise.resolve({ method: 'extract', spec, dest, conf })
2828

2929
t.test('running bin runs main file', t => {

test/fetcher.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,14 @@ t.test('tarball data', async t => {
9494

9595
t.test('tarballFile', t => {
9696
const target = resolve(me, 'tarball-file')
97-
t.test('basic copy', t =>
98-
new FileFetcher(abbrevspec, { cache })
99-
.tarballFile(target + '/basic/1.tgz'))
10097

101-
t.test('again, folder already created', t =>
102-
new FileFetcher(abbrevspec, { cache })
103-
.tarballFile(target + '/basic/2.tgz'))
98+
t.test('basic', async t => {
99+
await new FileFetcher(abbrevspec, { cache })
100+
.tarballFile(target + '/basic/1.tgz')
101+
102+
await new FileFetcher(abbrevspec, { cache })
103+
.tarballFile(target + '/basic/2.tgz')
104104

105-
t.test('check it', t => {
106105
const one = fs.readFileSync(target + '/basic/1.tgz')
107106
const two = fs.readFileSync(target + '/basic/2.tgz')
108107
const expect = fs.readFileSync(abbrev)

test/git.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const tar = require('tar')
8787

8888
let REPO_HEAD = ''
8989
t.test('setup', { bail: true }, t => {
90-
t.test('create repo', t => {
90+
t.test('create repo', () => {
9191
const git = (...cmd) => spawnGit(cmd, { cwd: repo })
9292
const write = (f, c) => fs.writeFileSync(`${repo}/${f}`, c)
9393
const npm = (...cmd) => spawnNpm('npm', [
@@ -162,7 +162,7 @@ t.test('setup', { bail: true }, t => {
162162
.then(({ stdout }) => REPO_HEAD = stdout.trim())
163163
})
164164

165-
t.test('create cycle of git prepared deps', async t => {
165+
t.test('create cycle of git prepared deps', async () => {
166166
for (const repoDir of [cycleA, cycleB]) {
167167
const git = (...cmd) => spawnGit(cmd, { cwd: repoDir })
168168
const write = (f, c) => fs.writeFileSync(`${repoDir}/${f}`, c)
@@ -227,7 +227,7 @@ t.test('setup', { bail: true }, t => {
227227
daemon.on('close', () => rmSync(me, { recursive: true, force: true }))
228228
})
229229

230-
t.test('create a repo with a submodule', t => {
230+
t.test('create a repo with a submodule', () => {
231231
const submoduleRepo = resolve(me, 'submodule-repo')
232232
const git = (...cmd) => spawnGit(cmd, { cwd: submoduleRepo })
233233
const write = (f, c) => fs.writeFileSync(`${submoduleRepo}/${f}`, c)
@@ -285,7 +285,7 @@ t.test('setup', { bail: true }, t => {
285285
.then(() => git('commit', '-m', 'package'))
286286
})
287287

288-
t.test('create a repo with workspaces', t => {
288+
t.test('create a repo with workspaces', () => {
289289
const workspacesRepo = resolve(me, 'workspaces-repo')
290290
const wsfolder = resolve(me, 'workspaces-repo/a')
291291
const git = (...cmd) => spawnGit(cmd, { cwd: workspacesRepo })
@@ -315,7 +315,7 @@ t.test('setup', { bail: true }, t => {
315315
.then(() => git('commit', '-m', 'a/package.json'))
316316
})
317317

318-
t.test('create a repo with only a prepack script', t => {
318+
t.test('create a repo with only a prepack script', () => {
319319
const prepackRepo = resolve(me, 'prepack-repo')
320320
const git = (...cmd) => spawnGit(cmd, { cwd: prepackRepo })
321321
const write = (f, c) => fs.writeFileSync(`${prepackRepo}/${f}`, c)
@@ -608,7 +608,7 @@ t.test('fetch a weird ref', t => {
608608
t.end()
609609
})
610610

611-
t.test('fetch a private repo where the tgz is a 404', t => {
611+
t.test('fetch a private repo where the tgz is a 404', () => {
612612
const gf = new GitFetcher(`localhost:repo/x#${REPO_HEAD}`, opts)
613613
gf.spec.hosted.tarball = () => `${hostedUrl}/not-found.tgz`
614614
// should fetch it by falling back to ssh when it gets an http error

0 commit comments

Comments
 (0)