Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
fix(api): use npa@5 for spec parsing (#78)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: spec objects can no longer be realize-package-specifier objects. Pass a string or generate npa@>=5 spec objects to pass in.
  • Loading branch information
zkat authored Apr 15, 2017
1 parent 017d989 commit 3f56298
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 141 deletions.
22 changes: 10 additions & 12 deletions extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ const BB = require('bluebird')

const cache = require('./lib/cache')
const extractStream = require('./lib/extract-stream')
const npa = require('npm-package-arg')
const pipe = BB.promisify(require('mississippi').pipe)
const optCheck = require('./lib/util/opt-check')
const retry = require('promise-retry')
const rimraf = BB.promisify(require('rimraf'))
const rps = BB.promisify(require('realize-package-specifier'))

module.exports = extract
function extract (spec, dest, opts) {
opts = optCheck(opts)
spec = typeof spec === 'string' ? npa(spec, opts.where) : spec
const startTime = Date.now()
if (opts.integrity && opts.cache && !opts.preferOnline) {
opts.log.silly('pacote', 'trying', spec, 'by hash:', opts.integrity.toString())
opts.log.silly('pacote', 'trying', spec.name, 'by hash:', opts.integrity.toString())
return extractByDigest(
startTime, spec, dest, opts
).catch(err => {
Expand All @@ -34,15 +35,15 @@ function extract (spec, dest, opts) {
}
})
} else {
opts.log.silly('pacote', 'no tarball hash provided for', spec, '- extracting by manifest')
opts.log.silly('pacote', 'no tarball hash provided for', spec.name, '- extracting by manifest')
return retry((tryAgain, attemptNum) => {
return extractByManifest(
startTime, spec, dest, opts
).catch(err => {
// We're only going to retry at this level if the local cache might
// have gotten corrupted.
if (err.code === 'EINTEGRITY' && opts.cache) {
opts.log.warn('pacote', `tarball integrity check for ${spec} failed. Clearing cache entry. ${err.message}`)
opts.log.warn('pacote', `tarball integrity check for ${spec.name}@${spec.saveSpec || spec.fetchSpec} failed. Clearing cache entry. ${err.message}`)
return cleanUpCached(
dest, opts.cache, err.sri, opts
).then(() => tryAgain(err))
Expand All @@ -58,20 +59,17 @@ function extractByDigest (start, spec, dest, opts) {
const xtractor = extractStream(dest, opts)
const cached = cache.get.stream.byDigest(opts.cache, opts.integrity, opts)
return pipe(cached, xtractor).then(() => {
opts.log.verbose('pacote', `${spec} extracted to ${dest} by content address ${Date.now() - start}ms`)
opts.log.verbose('pacote', `${spec.name}@${spec.saveSpec || spec.fetchSpec} extracted to ${dest} by content address ${Date.now() - start}ms`)
})
}

function extractByManifest (start, spec, dest, opts) {
const res = typeof spec === 'string'
? rps(spec, opts.where)
: BB.resolve(spec)
const xtractor = extractStream(dest, opts)
return res.then(res => {
const tarball = require('./lib/handlers/' + res.type + '/tarball')
return pipe(tarball(res, opts), xtractor)
return BB.resolve(() => {
const tarball = require('./lib/handlers/' + spec.type + '/tarball')
return pipe(tarball(spec, opts), xtractor)
}).then(() => {
opts.log.verbose('pacote', `${res.name}@${res.spec} extracted in ${Date.now() - start}ms`)
opts.log.verbose('pacote', `${spec.name}@${spec.saveSpec || spec.fetchSpec} extracted in ${Date.now() - start}ms`)
})
}

Expand Down
2 changes: 1 addition & 1 deletion lib/finalize-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function tarballedProps (pkg, spec, opts) {
_shrinkwrap: sr,
_resolved: (mani && mani._resolved) ||
(pkg && pkg._resolved) ||
spec.spec,
spec.fetchSpec,
_integrity: hash && hash.toString()
})
}
Expand Down
10 changes: 5 additions & 5 deletions lib/handlers/directory/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const readFileAsync = BB.promisify(require('fs').readFile)

module.exports = manifest
function manifest (spec, opts) {
const pkgPath = path.join(spec.spec, 'package.json')
const srPath = path.join(spec.spec, 'npm-shrinkwrap.json')
const pkgPath = path.join(spec.fetchSpec, 'package.json')
const srPath = path.join(spec.fetchSpec, 'npm-shrinkwrap.json')
return BB.join(
readFileAsync(pkgPath).then(JSON.parse).catch({code: 'ENOENT'}, err => {
err.code = 'ENOPACKAGEJSON'
Expand All @@ -20,16 +20,16 @@ function manifest (spec, opts) {
(pkg, sr) => {
pkg._shrinkwrap = sr
pkg._hasShrinkwrap = !!sr
pkg._resolved = spec.spec
pkg._resolved = spec.fetchSpec
pkg._integrity = false // Don't auto-calculate integrity
return pkg
}
).then(pkg => {
if (!pkg.bin && pkg.directories && pkg.directories.bin) {
const dirBin = pkg.directories.bin
return glob(path.join(spec.spec, dirBin, '/**'), {nodir: true}).then(matches => {
return glob(path.join(spec.fetchSpec, dirBin, '/**'), {nodir: true}).then(matches => {
matches.forEach(filePath => {
const relative = path.relative(spec.spec, filePath)
const relative = path.relative(spec.fetchSpec, filePath)
if (relative && relative[0] !== '.') {
if (!pkg.bin) { pkg.bin = {} }
pkg.bin[path.basename(relative)] = relative
Expand Down
14 changes: 7 additions & 7 deletions lib/handlers/git/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ const semver = require('semver')
module.exports = manifest
function manifest (spec, opts) {
opts = optCheck(opts)
if (spec.hosted && spec.spec === spec.hosted.shortcut) {
if (spec.hosted && spec.hosted.getDefaultRepresentation() === 'shortcut') {
return hostedManifest(spec, opts)
} else {
// If it's not a shortcut, don't do fallbacks.
return plainManifest(spec.spec, spec, opts)
return plainManifest(spec.fetchSpec, spec, opts)
}
}

function hostedManifest (spec, opts) {
return BB.resolve(null).then(() => {
return plainManifest(spec.hosted.httpsUrl, spec, opts)
return plainManifest(spec.hosted.https(), spec, opts)
}).catch(err => {
if (!spec.hosted.ssh) {
if (!spec.hosted.ssh()) {
throw err
}
return plainManifest(spec.hosted.ssh, spec, opts)
return plainManifest(spec.hosted.ssh(), spec, opts)
}).catch(err => {
if (!spec.hosted.gitUrl) {
if (!spec.hosted.git()) {
throw err
}
return plainManifest(spec.hosted.gitUrl, spec, opts)
return plainManifest(spec.hosted.git(), spec, opts)
})
}

Expand Down
4 changes: 3 additions & 1 deletion lib/handlers/local/manifest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict'

const BB = require('bluebird')

module.exports = manifest
function manifest () {
// The tarball handler will take care of it!
return Promise.resolve(null)
return BB.resolve(null)
}
2 changes: 1 addition & 1 deletion lib/handlers/local/tarball.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const MAX_BULK_SIZE = 2 * 1024 * 1024 // 2MB

module.exports = tarball
function tarball (spec, opts) {
const src = spec._resolved || spec.spec
const src = spec._resolved || spec.fetchSpec
const stream = through()
statAsync(src).then(stat => {
if (stat.size <= MAX_BULK_SIZE) {
Expand Down
2 changes: 1 addition & 1 deletion lib/handlers/remote/tarball.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const registryTarball = require('../../registry/tarball')

module.exports = tarball
function tarball (spec, opts) {
const uri = spec._resolved || spec.spec
const uri = spec._resolved || spec.fetchSpec
return registryTarball.fromManifest({
_resolved: uri,
_integrity: opts.integrity
Expand Down
2 changes: 1 addition & 1 deletion lib/registry/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function manifest (spec, opts) {
) {
opts.log.silly(
'registry:manifest',
`no matching version for ${spec.name}@${spec.spec} in the cache. Forcing revalidation`
`no matching version for ${spec.name}@${spec.fetchSpec} in the cache. Forcing revalidation`
)
opts.preferOnline = true
return manifest(spec, opts)
Expand Down
12 changes: 6 additions & 6 deletions lib/registry/pick-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ function pickManifest (metadata, spec, opts) {
var target

if (spec.type === 'tag') {
target = distTags[spec.spec]
target = distTags[spec.fetchSpec]
} else if (spec.type === 'version') {
target = spec.spec
target = spec.fetchSpec
} else if (spec.type !== 'range') {
return cb(new Error('Only tag, version, and range are supported'))
}
Expand All @@ -35,16 +35,16 @@ function pickManifest (metadata, spec, opts) {
!target &&
tagVersion &&
metadata.versions[tagVersion] &&
semver.satisfies(tagVersion, spec.spec, true)
semver.satisfies(tagVersion, spec.fetchSpec, true)
) {
target = tagVersion
}

if (!target) {
target = semver.maxSatisfying(versions, spec.spec, true)
target = semver.maxSatisfying(versions, spec.fetchSpec, true)
}

if (!target && spec.spec === '*') {
if (!target && spec.fetchSpec === '*') {
// npm hard-codes `latest` here, but it's likely intended
// to be `defaultTag`.
//
Expand All @@ -56,7 +56,7 @@ function pickManifest (metadata, spec, opts) {

var manifest = target && metadata.versions[target]
if (!manifest) {
err = new Error(`No matching version found for ${spec.name}@${spec.spec}`)
err = new Error(`No matching version found for ${spec.name}@${spec.fetchSpec}`)
err.code = 'ETARGET'
err.name = metadata.name
err.spec = spec
Expand Down
67 changes: 30 additions & 37 deletions manifest.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,47 @@
'use strict'

const BB = require('bluebird')

const finalizeManifest = require('./lib/finalize-manifest')
const optCheck = require('./lib/util/opt-check')
const pinflight = require('promise-inflight')
const rps = BB.promisify(require('realize-package-specifier'))
const npa = require('npm-package-arg')

let handlers = {}

module.exports = manifest
function manifest (spec, opts) {
opts = optCheck(opts)
spec = typeof spec === 'string' ? npa(spec, opts.where) : spec

const res = typeof spec === 'string'
? rps(spec, opts.where)
: BB.resolve(spec)

return res.then(res => {
const label = [
res.raw,
res.spec,
res.type,
opts.cache,
opts.registry,
opts.scope
].join(':')
return pinflight(label, () => {
const startTime = Date.now()
const fetcher = (
handlers[res.type] ||
(
handlers[res.type] =
require('./lib/handlers/' + res.type + '/manifest')
)
const label = [
spec.name,
spec.saveSpec || spec.fetchSpec,
spec.type,
opts.cache,
opts.registry,
opts.scope
].join(':')
return pinflight(label, () => {
const startTime = Date.now()
const fetcher = (
handlers[spec.type] ||
(
handlers[spec.type] =
require('./lib/handlers/' + spec.type + '/manifest')
)
return fetcher(res, opts).then(manifest => {
return finalizeManifest(manifest, res, opts)
}).then(manifest => {
// Metadata about the way this manifest was requested
if (opts.annotate) {
manifest._requested = res
manifest._spec = spec
manifest._where = opts.where
}
)
return fetcher(spec, opts).then(manifest => {
return finalizeManifest(manifest, spec, opts)
}).then(manifest => {
// Metadata about the way this manifest was requested
if (opts.annotate) {
manifest._requested = spec
manifest._spec = spec.raw
manifest._where = opts.where
}

const elapsedTime = Date.now() - startTime
opts.log.verbose('pacote', `${res.type} manifest for ${res.name}@${res.spec} fetched in ${elapsedTime}ms`)
return manifest
})
const elapsedTime = Date.now() - startTime
opts.log.verbose('pacote', `${spec.type} manifest for ${spec.name}@${spec.saveSpec || spec.fetchSpec} fetched in ${elapsedTime}ms`)
return manifest
})
})
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
"mississippi": "^1.2.0",
"normalize-git-url": "^3.0.2",
"normalize-package-data": "^2.3.6",
"npm-package-arg": "^5.0.0",
"osenv": "^0.1.4",
"promise-inflight": "^1.0.1",
"promise-retry": "^1.1.1",
"realize-package-specifier": "^3.0.3",
"semver": "^5.3.0",
"ssri": "^4.1.1",
"tar-fs": "^1.15.1",
Expand Down
12 changes: 5 additions & 7 deletions prefetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const BB = require('bluebird')
const cache = require('./lib/cache')
const finished = BB.promisify(require('mississippi').finished)
const optCheck = require('./lib/util/opt-check')
const rps = BB.promisify(require('realize-package-specifier'))
const npa = require('npm-package-arg')

module.exports = prefetch
function prefetch (spec, opts) {
opts = optCheck(opts)
spec = typeof spec === 'string' ? npa(spec, opts.where) : spec
const startTime = Date.now()
if (!opts.cache) {
opts.log.info('prefetch', 'skipping prefetch: no cache provided')
Expand All @@ -36,20 +37,17 @@ function prefetch (spec, opts) {
}

function prefetchByManifest (start, spec, opts) {
const res = typeof spec === 'string'
? rps(spec, opts.where)
: BB.resolve(spec)
let manifest
let integrity
return res.then(res => {
const stream = require('./lib/handlers/' + res.type + '/tarball')(res, opts)
return BB.resolve().then(() => {
const stream = require('./lib/handlers/' + spec.type + '/tarball')(spec, opts)
if (!stream) { return }
stream.on('data', function () {})
stream.on('manifest', m => { manifest = m })
stream.on('integrity', i => { integrity = i })
return finished(stream)
}).then(() => {
opts.log.verbose('prefetch', `${spec} done in ${Date.now() - start}ms`)
opts.log.verbose('prefetch', `${spec.name}@${spec.saveSpec || spec.fetchSpec} done in ${Date.now() - start}ms`)
return {
manifest,
spec,
Expand Down
2 changes: 1 addition & 1 deletion test/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test('supports directory deps', t => {
bundleDependencies: false,
peerDependencies: {},
deprecated: false,
_resolved: path.resolve(PKG),
_resolved: path.resolve(PKG).replace(/\\/g, '/'),
_integrity: null,
_shrinkwrap: sr,
bin: { mybin: path.join('x', 'mybin') },
Expand Down
4 changes: 2 additions & 2 deletions test/finalize-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ test('uses package.json as base if passed null', t => {
}).then(tarData => {
tnock(t, OPTS.registry).get('/' + tarballPath).reply(200, tarData)
return finalizeManifest(null, {
spec: OPTS.registry + tarballPath,
fetchSpec: OPTS.registry + tarballPath,
type: 'remote'
}, OPTS).then(manifest => {
t.deepEqual(manifest, {
Expand Down Expand Up @@ -279,7 +279,7 @@ test('caches finalized manifests', t => {
t.ok(true, 'manifest entry exists in cache: ' + k)
}
})
return Promise.all(promises)
return BB.all(promises)
}).then(() => {
return finalizeManifest(base, {
name: base.name,
Expand Down
Loading

0 comments on commit 3f56298

Please sign in to comment.