Skip to content
This repository has been archived by the owner on Nov 25, 2017. It is now read-only.

Commit

Permalink
fix: account for rare situation where npme is not returning 404 for n…
Browse files Browse the repository at this point in the history
…ot found packages

Closes #1
  • Loading branch information
boennemann committed Aug 22, 2015
1 parent ad24990 commit 400b6bf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
13 changes: 11 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ const RegClient = require('npm-registry-client')

module.exports = function (pluginConfig, {pkg, npm, plugins}, cb) {
npmlog.level = npm.loglevel || 'warn'
const client = new RegClient({log: npmlog})
let clientConfig = {log: npmlog}
// disable retries for tests
if (pluginConfig.retry) clientConfig.retry = pluginConfig.retry
const client = new RegClient(clientConfig)

client.get(`${npm.registry}${pkg.name.replace('/', '%2F')}`, {
auth: npm.auth
}, (err, data) => {
if (err && err.statusCode === 404) return cb(null, {})
if (err && (
err.statusCode === 404 ||
/not found/i.test(err.message)
)) {
return cb(null, {})
}

if (err) return cb(err)

const version = data['dist-tags'][npm.tag]
Expand Down
3 changes: 3 additions & 0 deletions test/mocks/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ module.exports = nock('http://registry.npmjs.org')
.reply(404, {})
.get('/unavailable-no-body')
.reply(404)
.get('/unavailable-no-404')
.times(2)
.replyWithError({message: 'not found', statusCode: 500, code: 'E500'})
43 changes: 30 additions & 13 deletions test/specs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,39 @@ test('last release from registry', (t) => {
})

t.test('get nothing from not yet published package name', (tt) => {
tt.plan(4)
tt.plan(3)

lastRelease({}, {
pkg: {name: 'unavailable'},
npm
}, (err, release) => {
tt.error(err)
tt.is(release.version, undefined, 'no version')
tt.test('unavailable', (ttt) => {
lastRelease({}, {
pkg: {name: 'unavailable'},
npm
}, (err, release) => {
ttt.error(err)
ttt.is(release.version, undefined, 'no version')
ttt.end()
})
})

lastRelease({}, {
pkg: {name: 'unavailable-no-body'},
npm
}, (err, release) => {
tt.error(err)
tt.is(release.version, undefined, 'no version')
tt.test('unavailable w/o response body', (ttt) => {
lastRelease({}, {
pkg: {name: 'unavailable-no-body'},
npm
}, (err, release) => {
ttt.error(err)
ttt.is(release.version, undefined, 'no version')
ttt.end()
})
})

tt.test('unavailable w/o status code', (ttt) => {
lastRelease({retry: {count: 1, factor: 1, minTimeout: 1, maxTimeout: 2}}, {
pkg: {name: 'unavailable-no-404'},
npm
}, (err, release) => {
ttt.error(err)
ttt.is(release.version, undefined, 'no version')
ttt.end()
})
})
})
})

0 comments on commit 400b6bf

Please sign in to comment.