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

Commit

Permalink
fix(manifest): retry registry manifests once on ETARGET (#66)
Browse files Browse the repository at this point in the history
Fixes: #33
  • Loading branch information
zkat committed Mar 11, 2017
1 parent 931a9cb commit 3b99adc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/registry/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var url = require('url')
var request = require('./request')

module.exports = manifest
function manifest (spec, opts, cb) {
function manifest (spec, opts) {
opts = optCheck(opts)
opts.memoize = true

Expand Down Expand Up @@ -40,6 +40,14 @@ function manifest (spec, opts, cb) {
'.tgz'
)
return manifest
}).catch({code: 'ETARGET'}, err => {
if (!opts.cache || opts.maxAge < 0 || opts.offline) {
throw err
} else {
opts.log.silly('registry.manifest', 'version missing from current metadata, forcing network check')
opts.maxAge = -1
return manifest(spec, opts)
}
})
}

Expand Down
2 changes: 1 addition & 1 deletion lib/registry/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function isStale (meta, opts) {
if (!meta ||
!meta.time ||
(meta.cacheControl && meta.cacheControl.toLowerCase() === 'immutable') ||
opts.preferOffline ||
(opts.preferOffline && opts.maxAge > 0) ||
opts.offline) {
opts.log.silly('registry.get', 'skipping staleness check for')
return false
Expand Down
23 changes: 20 additions & 3 deletions test/registry.manifest.cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const OPTS = {
}

test('memoizes identical registry requests', t => {
t.plan(2)
cache.clearMemoized()
const srv = tnock(t, OPTS.registry)

srv.get('/foo').once().reply(200, META)
Expand All @@ -73,7 +73,7 @@ test('memoizes identical registry requests', t => {
})

test('tag requests memoize versions', t => {
t.plan(2)
cache.clearMemoized()
const srv = tnock(t, OPTS.registry)

srv.get('/foo').once().reply(200, META)
Expand All @@ -88,7 +88,7 @@ test('tag requests memoize versions', t => {
})

test('tag requests memoize tags', t => {
t.plan(2)
cache.clearMemoized()
const srv = tnock(t, OPTS.registry)

srv.get('/foo').once().reply(200, META)
Expand All @@ -105,6 +105,7 @@ test('tag requests memoize tags', t => {
test('memoization is scoped to a given cache')

test('inflights concurrent requests', t => {
cache.clearMemoized()
const srv = tnock(t, OPTS.registry)

srv.get('/foo').once().reply(200, META)
Expand All @@ -119,6 +120,7 @@ test('inflights concurrent requests', t => {
})

test('supports fetching from an optional cache', t => {
cache.clearMemoized()
tnock(t, OPTS.registry)
const key = cache.key('registry-request', OPTS.registry + '/foo')
return cache.put(CACHE, key, JSON.stringify(META), OPTS).then(() => {
Expand All @@ -129,6 +131,7 @@ test('supports fetching from an optional cache', t => {
})

test('falls back to registry if cache entry missing', t => {
cache.clearMemoized()
const opts = {
registry: OPTS.registry,
log: OPTS.log,
Expand All @@ -142,6 +145,20 @@ test('falls back to registry if cache entry missing', t => {
})
})

test('tries again if cached data is missing target', t => {
cache.clearMemoized()
const srv = tnock(t, OPTS.registry)
const key = cache.key('registry-request', OPTS.registry + '/foo')
srv.get('/foo').reply(200, META)
return cache.put(CACHE, key, JSON.stringify({
versions: { '1.1.2': BASE }
}), OPTS).then(() => {
return manifest('foo@1.2.3', OPTS).then(pkg => {
t.deepEqual(pkg, PKG)
})
})
})

test('expires stale request data')
test('allows forcing use of cache when data stale')
test('falls back to registry if cache entry is invalid JSON')
Expand Down

0 comments on commit 3b99adc

Please sign in to comment.