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

Commit

Permalink
feat(local): local tarball support
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Mar 11, 2017
1 parent 3b99adc commit e2b6a83
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/handlers/local/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = manifest
function manifest () {
// The tarball handler will take care of it!
return null
}
35 changes: 35 additions & 0 deletions lib/handlers/local/tarball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'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)
}
43 changes: 43 additions & 0 deletions test/local.tarball.js
Original file line number Diff line number Diff line change
@@ -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')
})
})
})
})

0 comments on commit e2b6a83

Please sign in to comment.