From 089951257531ac44a679354e6ea738c7eee1c846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sat, 11 Mar 2017 00:27:32 -0800 Subject: [PATCH] feat(local): local tarball support Fixes: #7 --- lib/handlers/local/manifest.js | 7 ++++++ lib/handlers/local/tarball.js | 36 ++++++++++++++++++++++++++++ test/local.tarball.js | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 lib/handlers/local/manifest.js create mode 100644 lib/handlers/local/tarball.js create mode 100644 test/local.tarball.js diff --git a/lib/handlers/local/manifest.js b/lib/handlers/local/manifest.js new file mode 100644 index 0000000..f4e6bcd --- /dev/null +++ b/lib/handlers/local/manifest.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = manifest +function manifest () { + // The tarball handler will take care of it! + return null +} diff --git a/lib/handlers/local/tarball.js b/lib/handlers/local/tarball.js new file mode 100644 index 0000000..f22cc1d --- /dev/null +++ b/lib/handlers/local/tarball.js @@ -0,0 +1,36 @@ +'use strict' + +const BB = require('bluebird') + +const fs = require('graceful-fs') +const pipe = require('mississippi').pipe +const through = require('mississippi').through + +const readFileAsync = BB.promisify(fs.readFile) +const statAsync = BB.promisify(fs.stat) + +const MAX_BULK_SIZE = 2 * 1024 * 1024 // 2MB + +module.exports = tarball +function tarball (spec, opts) { + const src = spec._resolved || spec.spec + const stream = through() + statAsync(src).then(stat => { + if (stat.size <= MAX_BULK_SIZE) { + // YAY LET'S DO THING IN BULK + return readFileAsync(src).then(data => { + stream.write(data, () => { + stream.end() + }) + }) + } else { + return pipe(fs.createReadStream(src), stream) + } + }, err => stream.emit('error', err)) + return stream +} + +module.exports.fromManifest = fromManifest +function fromManifest (manifest, spec, opts) { + return tarball(manifest || spec, opts) +} diff --git a/test/local.tarball.js b/test/local.tarball.js new file mode 100644 index 0000000..20eea2a --- /dev/null +++ b/test/local.tarball.js @@ -0,0 +1,43 @@ +'use strict' + +const BB = require('bluebird') + +const finished = BB.promisify(require('mississippi').finished) +const fs = BB.promisifyAll(require('fs')) +const mockTar = require('./util/mock-tarball') +const npmlog = require('npmlog') +const path = require('path') +const test = require('tap').test + +const CACHE = require('./util/test-dir')(__filename) + +const tarball = require('../lib/handlers/local/tarball') + +npmlog.level = process.env.LOGLEVEL || 'silent' +const OPTS = { + log: npmlog +} + +test('basic tarball streaming', function (t) { + const pkg = { + 'package.json': JSON.stringify({ + name: 'foo', + version: '1.2.3' + }), + 'index.js': 'console.log("hello world!")' + } + const tarballPath = path.join(CACHE, 'foo-1.2.3.tgz') + return mockTar(pkg).then(tarData => { + return fs.writeFileAsync(tarballPath, tarData).then(() => { + let data = '' + return finished( + tarball({ + type: 'local', + spec: tarballPath + }, OPTS).on('data', d => { data += d }) + ).then(() => { + t.equal(data, tarData, 'fetched tarball data matches one from local') + }) + }) + }) +})