• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3// XXX: This shares a lot in common with extract.js
4// maybe some DRY opportunity here?
5
6// tar -t
7const hlo = require('./high-level-opt.js')
8const Parser = require('./parse.js')
9const fs = require('fs')
10const fsm = require('fs-minipass')
11const path = require('path')
12const stripSlash = require('./strip-trailing-slashes.js')
13
14module.exports = (opt_, files, cb) => {
15  if (typeof opt_ === 'function') {
16    cb = opt_, files = null, opt_ = {}
17  } else if (Array.isArray(opt_)) {
18    files = opt_, opt_ = {}
19  }
20
21  if (typeof files === 'function') {
22    cb = files, files = null
23  }
24
25  if (!files) {
26    files = []
27  } else {
28    files = Array.from(files)
29  }
30
31  const opt = hlo(opt_)
32
33  if (opt.sync && typeof cb === 'function') {
34    throw new TypeError('callback not supported for sync tar functions')
35  }
36
37  if (!opt.file && typeof cb === 'function') {
38    throw new TypeError('callback only supported with file option')
39  }
40
41  if (files.length) {
42    filesFilter(opt, files)
43  }
44
45  if (!opt.noResume) {
46    onentryFunction(opt)
47  }
48
49  return opt.file && opt.sync ? listFileSync(opt)
50    : opt.file ? listFile(opt, cb)
51    : list(opt)
52}
53
54const onentryFunction = opt => {
55  const onentry = opt.onentry
56  opt.onentry = onentry ? e => {
57    onentry(e)
58    e.resume()
59  } : e => e.resume()
60}
61
62// construct a filter that limits the file entries listed
63// include child entries if a dir is included
64const filesFilter = (opt, files) => {
65  const map = new Map(files.map(f => [stripSlash(f), true]))
66  const filter = opt.filter
67
68  const mapHas = (file, r) => {
69    const root = r || path.parse(file).root || '.'
70    const ret = file === root ? false
71      : map.has(file) ? map.get(file)
72      : mapHas(path.dirname(file), root)
73
74    map.set(file, ret)
75    return ret
76  }
77
78  opt.filter = filter
79    ? (file, entry) => filter(file, entry) && mapHas(stripSlash(file))
80    : file => mapHas(stripSlash(file))
81}
82
83const listFileSync = opt => {
84  const p = list(opt)
85  const file = opt.file
86  let threw = true
87  let fd
88  try {
89    const stat = fs.statSync(file)
90    const readSize = opt.maxReadSize || 16 * 1024 * 1024
91    if (stat.size < readSize) {
92      p.end(fs.readFileSync(file))
93    } else {
94      let pos = 0
95      const buf = Buffer.allocUnsafe(readSize)
96      fd = fs.openSync(file, 'r')
97      while (pos < stat.size) {
98        const bytesRead = fs.readSync(fd, buf, 0, readSize, pos)
99        pos += bytesRead
100        p.write(buf.slice(0, bytesRead))
101      }
102      p.end()
103    }
104    threw = false
105  } finally {
106    if (threw && fd) {
107      try {
108        fs.closeSync(fd)
109      } catch (er) {}
110    }
111  }
112}
113
114const listFile = (opt, cb) => {
115  const parse = new Parser(opt)
116  const readSize = opt.maxReadSize || 16 * 1024 * 1024
117
118  const file = opt.file
119  const p = new Promise((resolve, reject) => {
120    parse.on('error', reject)
121    parse.on('end', resolve)
122
123    fs.stat(file, (er, stat) => {
124      if (er) {
125        reject(er)
126      } else {
127        const stream = new fsm.ReadStream(file, {
128          readSize: readSize,
129          size: stat.size,
130        })
131        stream.on('error', reject)
132        stream.pipe(parse)
133      }
134    })
135  })
136  return cb ? p.then(cb, cb) : p
137}
138
139const list = opt => new Parser(opt)
140