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

Commit

Permalink
feat(prefetch): added tarball prefetch support
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Mar 5, 2017
1 parent a559711 commit 26c34ce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

module.exports = {
extract: require('./extract'),
manifest: require('./manifest')
manifest: require('./manifest'),
prefetch: require('./prefetch')
}
46 changes: 46 additions & 0 deletions prefetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict'

var cache = require('./lib/cache')
var finished = require('mississippi').finished
var optCheck = require('./lib/util/opt-check')
var rps = require('realize-package-specifier')

module.exports = prefetch
function prefetch (spec, opts, cb) {
if (!cb) {
cb = opts
opts = null
}
opts = optCheck(opts)
if (!opts.cache) {
opts.log.info('prefetch', 'skipping prefetch: no cache provided')
setImmediate(function () { cb() })
}
if (opts.digest) {
opts.log.silly('prefetch', 'checking if ', spec, ' digest is already cached')
cache.get.hasContent(opts.cache, opts.digest, function (err, exists) {
if (err) { return cb(err) }
if (exists) {
opts.log.silly('prefetch', 'content already exists for', spec)
return cb(null)
} else {
return prefetchByManifest(spec, opts, cb)
}
})
} else {
opts.log.silly('prefetch', 'no digest provided for ', spec, '- fetching by manifest')
prefetchByManifest(spec, opts, cb)
}
}

function prefetchByManifest (spec, opts, cb) {
rps(spec, function (err, res) {
if (err) { return cb(err) }
var stream = require('./lib/handlers/' + res.type + '/tarball')(res, opts)
finished(stream, function (err) {
opts.log.silly('prefetch', 'prefetch finished for', spec)
cb(err)
})
stream.on('data', function () {})
})
}

0 comments on commit 26c34ce

Please sign in to comment.