From 26c34ce2d35a72696a323a1c312c428dcd576553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Fri, 17 Feb 2017 23:07:09 -0800 Subject: [PATCH] feat(prefetch): added tarball prefetch support --- index.js | 3 ++- prefetch.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 prefetch.js diff --git a/index.js b/index.js index c2efd7f..d4c1626 100644 --- a/index.js +++ b/index.js @@ -2,5 +2,6 @@ module.exports = { extract: require('./extract'), - manifest: require('./manifest') + manifest: require('./manifest'), + prefetch: require('./prefetch') } diff --git a/prefetch.js b/prefetch.js new file mode 100644 index 0000000..ebb586c --- /dev/null +++ b/prefetch.js @@ -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 () {}) + }) +}