diff --git a/lib/extract-stream.js b/lib/extract-stream.js index f6f68bc..ba85d69 100644 --- a/lib/extract-stream.js +++ b/lib/extract-stream.js @@ -5,16 +5,21 @@ const path = require('path') const pipeline = require('mississippi').pipeline const tar = require('tar-fs') +let uid +let gid +if (process.platform !== 'win32') { + uid = process.getuid() + gid = process.getgid() +} + module.exports = extractStream function extractStream (dest, opts) { opts = opts || {} const sawIgnores = {} return pipeline(gunzip(), tar.extract(dest, { map: (header) => { - if (process.platform !== 'win32') { - header.uid = opts.uid == null ? header.uid : opts.uid - header.gid = opts.gid == null ? header.gid : opts.gid - } + if (uid != null) { header.uid = uid } + if (gid != null) { header.gid = gid } // Note: This mirrors logic in the fs read operations that are // employed during tarball creation, in the fstream-npm module. // It is duplicated here to handle tarballs that are created diff --git a/test/extract-stream.chown.js b/test/extract-stream.chown.js deleted file mode 100644 index 4d7f355..0000000 --- a/test/extract-stream.chown.js +++ /dev/null @@ -1,65 +0,0 @@ -'use strict' - -const BB = require('bluebird') - -const fs = require('fs') -const mockTar = require('./util/mock-tarball') -const npmlog = require('npmlog') -const path = require('path') -const pipe = BB.promisify(require('mississippi').pipe) -const requireInject = require('require-inject') -const test = require('tap').test - -require('./util/test-dir')(__filename) - -npmlog.level = process.env.LOGLEVEL || 'silent' - -test('accepts gid and uid opts', { - skip: !process.getuid -}, function (t) { - const pkg = { - 'package.json': { - data: JSON.stringify({ - name: 'foo', - version: '1.0.0' - }) - }, - 'foo/index.js': 'console.log("hello world!")' - } - const NEWUID = process.getuid() + 1 - const NEWGID = process.getgid() + 1 - // All of this only happens on uid === 0 - process.getuid = () => 0 - const updatedPaths = [] - const fsClone = Object.create(fs) - fsClone.chown = (p, uid, gid, cb) => { - process.nextTick(() => { - t.deepEqual({ - uid: uid, - gid: gid - }, { - uid: NEWUID, - gid: NEWGID - }, 'correct owner set on ' + p) - updatedPaths.push(path.relative('.', p)) - cb(null) - }) - } - const extractStream = requireInject('../lib/extract-stream', { - fs: fsClone - }) - return mockTar(pkg, {stream: true}).then(tarStream => { - return pipe(tarStream, extractStream('./target', { - uid: NEWUID, - gid: NEWGID, - log: npmlog - })) - }).then(() => { - t.deepEqual(updatedPaths, [ - 'target', - 'target/package.json', - 'target/foo', - 'target/foo/index.js' - ], 'extracted files had correct uid/gid set') - }) -})