3 const Buffer = require('./buffer.js')
5 // XXX: This shares a lot in common with extract.js
6 // maybe some DRY opportunity here?
9 const hlo = require('./high-level-opt.js')
10 const Parser = require('./parse.js')
11 const fs = require('fs')
12 const fsm = require('fs-minipass')
13 const path = require('path')
15 const t = module.exports = (opt_, files, cb) => {
16 if (typeof opt_ === 'function')
17 cb = opt_, files = null, opt_ = {}
18 else if (Array.isArray(opt_))
19 files = opt_, opt_ = {}
21 if (typeof files === 'function')
22 cb = files, files = null
27 files = Array.from(files)
31 if (opt.sync && typeof cb === 'function')
32 throw new TypeError('callback not supported for sync tar functions')
34 if (!opt.file && typeof cb === 'function')
35 throw new TypeError('callback only supported with file option')
38 filesFilter(opt, files)
43 return opt.file && opt.sync ? listFileSync(opt)
44 : opt.file ? listFile(opt, cb)
48 const onentryFunction = opt => {
49 const onentry = opt.onentry
50 opt.onentry = onentry ? e => {
56 // construct a filter that limits the file entries listed
57 // include child entries if a dir is included
58 const filesFilter = (opt, files) => {
59 const map = new Map(files.map(f => [f.replace(/\/+$/, ''), true]))
60 const filter = opt.filter
62 const mapHas = (file, r) => {
63 const root = r || path.parse(file).root || '.'
64 const ret = file === root ? false
65 : map.has(file) ? map.get(file)
66 : mapHas(path.dirname(file), root)
73 ? (file, entry) => filter(file, entry) && mapHas(file.replace(/\/+$/, ''))
74 : file => mapHas(file.replace(/\/+$/, ''))
77 const listFileSync = opt => {
83 const stat = fs.statSync(file)
84 const readSize = opt.maxReadSize || 16*1024*1024
85 if (stat.size < readSize) {
86 p.end(fs.readFileSync(file))
89 const buf = Buffer.allocUnsafe(readSize)
90 fd = fs.openSync(file, 'r')
91 while (pos < stat.size) {
92 let bytesRead = fs.readSync(fd, buf, 0, readSize, pos)
94 p.write(buf.slice(0, bytesRead))
101 try { fs.closeSync(fd) } catch (er) {}
105 const listFile = (opt, cb) => {
106 const parse = new Parser(opt)
107 const readSize = opt.maxReadSize || 16*1024*1024
109 const file = opt.file
110 const p = new Promise((resolve, reject) => {
111 parse.on('error', reject)
112 parse.on('end', resolve)
114 fs.stat(file, (er, stat) => {
118 const stream = new fsm.ReadStream(file, {
122 stream.on('error', reject)
127 return cb ? p.then(cb, cb) : p
130 const list = opt => new Parser(opt)