[6a3a178] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | // tar -x
|
---|
| 4 | const hlo = require('./high-level-opt.js')
|
---|
| 5 | const Unpack = require('./unpack.js')
|
---|
| 6 | const fs = require('fs')
|
---|
| 7 | const fsm = require('fs-minipass')
|
---|
| 8 | const path = require('path')
|
---|
| 9 | const stripSlash = require('./strip-trailing-slashes.js')
|
---|
| 10 |
|
---|
| 11 | module.exports = (opt_, files, cb) => {
|
---|
| 12 | if (typeof opt_ === 'function')
|
---|
| 13 | cb = opt_, files = null, opt_ = {}
|
---|
| 14 | else if (Array.isArray(opt_))
|
---|
| 15 | files = opt_, opt_ = {}
|
---|
| 16 |
|
---|
| 17 | if (typeof files === 'function')
|
---|
| 18 | cb = files, files = null
|
---|
| 19 |
|
---|
| 20 | if (!files)
|
---|
| 21 | files = []
|
---|
| 22 | else
|
---|
| 23 | files = Array.from(files)
|
---|
| 24 |
|
---|
| 25 | const opt = hlo(opt_)
|
---|
| 26 |
|
---|
| 27 | if (opt.sync && typeof cb === 'function')
|
---|
| 28 | throw new TypeError('callback not supported for sync tar functions')
|
---|
| 29 |
|
---|
| 30 | if (!opt.file && typeof cb === 'function')
|
---|
| 31 | throw new TypeError('callback only supported with file option')
|
---|
| 32 |
|
---|
| 33 | if (files.length)
|
---|
| 34 | filesFilter(opt, files)
|
---|
| 35 |
|
---|
| 36 | return opt.file && opt.sync ? extractFileSync(opt)
|
---|
| 37 | : opt.file ? extractFile(opt, cb)
|
---|
| 38 | : opt.sync ? extractSync(opt)
|
---|
| 39 | : extract(opt)
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | // construct a filter that limits the file entries listed
|
---|
| 43 | // include child entries if a dir is included
|
---|
| 44 | const filesFilter = (opt, files) => {
|
---|
| 45 | const map = new Map(files.map(f => [stripSlash(f), true]))
|
---|
| 46 | const filter = opt.filter
|
---|
| 47 |
|
---|
| 48 | const mapHas = (file, r) => {
|
---|
| 49 | const root = r || path.parse(file).root || '.'
|
---|
| 50 | const ret = file === root ? false
|
---|
| 51 | : map.has(file) ? map.get(file)
|
---|
| 52 | : mapHas(path.dirname(file), root)
|
---|
| 53 |
|
---|
| 54 | map.set(file, ret)
|
---|
| 55 | return ret
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | opt.filter = filter
|
---|
| 59 | ? (file, entry) => filter(file, entry) && mapHas(stripSlash(file))
|
---|
| 60 | : file => mapHas(stripSlash(file))
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | const extractFileSync = opt => {
|
---|
| 64 | const u = new Unpack.Sync(opt)
|
---|
| 65 |
|
---|
| 66 | const file = opt.file
|
---|
| 67 | const stat = fs.statSync(file)
|
---|
| 68 | // This trades a zero-byte read() syscall for a stat
|
---|
| 69 | // However, it will usually result in less memory allocation
|
---|
| 70 | const readSize = opt.maxReadSize || 16 * 1024 * 1024
|
---|
| 71 | const stream = new fsm.ReadStreamSync(file, {
|
---|
| 72 | readSize: readSize,
|
---|
| 73 | size: stat.size,
|
---|
| 74 | })
|
---|
| 75 | stream.pipe(u)
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | const extractFile = (opt, cb) => {
|
---|
| 79 | const u = new Unpack(opt)
|
---|
| 80 | const readSize = opt.maxReadSize || 16 * 1024 * 1024
|
---|
| 81 |
|
---|
| 82 | const file = opt.file
|
---|
| 83 | const p = new Promise((resolve, reject) => {
|
---|
| 84 | u.on('error', reject)
|
---|
| 85 | u.on('close', resolve)
|
---|
| 86 |
|
---|
| 87 | // This trades a zero-byte read() syscall for a stat
|
---|
| 88 | // However, it will usually result in less memory allocation
|
---|
| 89 | fs.stat(file, (er, stat) => {
|
---|
| 90 | if (er)
|
---|
| 91 | reject(er)
|
---|
| 92 | else {
|
---|
| 93 | const stream = new fsm.ReadStream(file, {
|
---|
| 94 | readSize: readSize,
|
---|
| 95 | size: stat.size,
|
---|
| 96 | })
|
---|
| 97 | stream.on('error', reject)
|
---|
| 98 | stream.pipe(u)
|
---|
| 99 | }
|
---|
| 100 | })
|
---|
| 101 | })
|
---|
| 102 | return cb ? p.then(cb, cb) : p
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | const extractSync = opt => new Unpack.Sync(opt)
|
---|
| 106 |
|
---|
| 107 | const extract = opt => new Unpack(opt)
|
---|