source: trip-planner-front/node_modules/tar/lib/list.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.1 KB
Line 
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 if (typeof files === 'function')
21 cb = files, files = null
22
23 if (!files)
24 files = []
25 else
26 files = Array.from(files)
27
28 const opt = hlo(opt_)
29
30 if (opt.sync && typeof cb === 'function')
31 throw new TypeError('callback not supported for sync tar functions')
32
33 if (!opt.file && typeof cb === 'function')
34 throw new TypeError('callback only supported with file option')
35
36 if (files.length)
37 filesFilter(opt, files)
38
39 if (!opt.noResume)
40 onentryFunction(opt)
41
42 return opt.file && opt.sync ? listFileSync(opt)
43 : opt.file ? listFile(opt, cb)
44 : list(opt)
45}
46
47const onentryFunction = opt => {
48 const onentry = opt.onentry
49 opt.onentry = onentry ? e => {
50 onentry(e)
51 e.resume()
52 } : e => e.resume()
53}
54
55// construct a filter that limits the file entries listed
56// include child entries if a dir is included
57const filesFilter = (opt, files) => {
58 const map = new Map(files.map(f => [stripSlash(f), true]))
59 const filter = opt.filter
60
61 const mapHas = (file, r) => {
62 const root = r || path.parse(file).root || '.'
63 const ret = file === root ? false
64 : map.has(file) ? map.get(file)
65 : mapHas(path.dirname(file), root)
66
67 map.set(file, ret)
68 return ret
69 }
70
71 opt.filter = filter
72 ? (file, entry) => filter(file, entry) && mapHas(stripSlash(file))
73 : file => mapHas(stripSlash(file))
74}
75
76const listFileSync = opt => {
77 const p = list(opt)
78 const file = opt.file
79 let threw = true
80 let fd
81 try {
82 const stat = fs.statSync(file)
83 const readSize = opt.maxReadSize || 16 * 1024 * 1024
84 if (stat.size < readSize)
85 p.end(fs.readFileSync(file))
86 else {
87 let pos = 0
88 const buf = Buffer.allocUnsafe(readSize)
89 fd = fs.openSync(file, 'r')
90 while (pos < stat.size) {
91 const bytesRead = fs.readSync(fd, buf, 0, readSize, pos)
92 pos += bytesRead
93 p.write(buf.slice(0, bytesRead))
94 }
95 p.end()
96 }
97 threw = false
98 } finally {
99 if (threw && fd) {
100 try {
101 fs.closeSync(fd)
102 } catch (er) {}
103 }
104 }
105}
106
107const listFile = (opt, cb) => {
108 const parse = new Parser(opt)
109 const readSize = opt.maxReadSize || 16 * 1024 * 1024
110
111 const file = opt.file
112 const p = new Promise((resolve, reject) => {
113 parse.on('error', reject)
114 parse.on('end', resolve)
115
116 fs.stat(file, (er, stat) => {
117 if (er)
118 reject(er)
119 else {
120 const stream = new fsm.ReadStream(file, {
121 readSize: readSize,
122 size: stat.size,
123 })
124 stream.on('error', reject)
125 stream.pipe(parse)
126 }
127 })
128 })
129 return cb ? p.then(cb, cb) : p
130}
131
132const list = opt => new Parser(opt)
Note: See TracBrowser for help on using the repository browser.