source: trip-planner-front/node_modules/tar/lib/extract.js@ e29cc2e

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

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1'use strict'
2
3// tar -x
4const hlo = require('./high-level-opt.js')
5const Unpack = require('./unpack.js')
6const fs = require('fs')
7const fsm = require('fs-minipass')
8const path = require('path')
9const stripSlash = require('./strip-trailing-slashes.js')
10
11module.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
44const 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
63const 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
78const 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
105const extractSync = opt => new Unpack.Sync(opt)
106
107const extract = opt => new Unpack(opt)
Note: See TracBrowser for help on using the repository browser.