/
list.ts
185 lines (166 loc) · 4.18 KB
/
list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// tar -t
import * as fsm from '@isaacs/fs-minipass'
import fs from 'node:fs'
import { dirname, parse } from 'path'
import {
dealias,
isFile,
isSyncFile,
TarOptions,
TarOptionsFile,
TarOptionsSyncFile,
TarOptionsWithAliases,
TarOptionsWithAliasesFile,
TarOptionsWithAliasesSync,
TarOptionsWithAliasesSyncFile,
} from './options.js'
import { Parser } from './parse.js'
import { stripTrailingSlashes } from './strip-trailing-slashes.js'
export function list(
opt: TarOptionsWithAliasesSyncFile,
files?: string[],
): void
export function list(
opt: TarOptionsWithAliasesSync,
files?: string[],
): void
export function list(
opt: TarOptionsWithAliasesFile,
files?: string[],
cb?: () => any,
): Promise<void>
export function list(
opt: TarOptionsWithAliasesFile,
cb: () => any,
): Promise<void>
export function list(
opt: TarOptionsWithAliases,
files?: string[],
): Parser
export function list(
opt_: TarOptionsWithAliases,
files?: string[] | (() => any),
cb?: () => any,
): void | Promise<void> | Parser {
if (typeof opt_ === 'function') {
;(cb = opt_), (files = undefined), (opt_ = {})
} else if (Array.isArray(opt_)) {
;(files = opt_), (opt_ = {})
}
if (typeof files === 'function') {
;(cb = files), (files = undefined)
}
if (!files) {
files = []
} else {
files = Array.from(files)
}
const opt = dealias(opt_)
if (opt.sync && typeof cb === 'function') {
throw new TypeError(
'callback not supported for sync tar functions',
)
}
if (!opt.file && typeof cb === 'function') {
throw new TypeError('callback only supported with file option')
}
if (files.length) {
filesFilter(opt, files)
}
if (!opt.noResume) {
onentryFunction(opt)
}
return isSyncFile(opt)
? listFileSync(opt)
: isFile(opt)
? listFile(opt, cb)
: list_(opt)
}
const onentryFunction = (opt: TarOptions) => {
const onentry = opt.onentry
opt.onentry = onentry
? e => {
onentry(e)
e.resume()
}
: e => e.resume()
}
// construct a filter that limits the file entries listed
// include child entries if a dir is included
const filesFilter = (opt: TarOptions, files: string[]) => {
const map = new Map<string, boolean>(
files.map(f => [stripTrailingSlashes(f), true]),
)
const filter = opt.filter
const mapHas = (file: string, r: string = ''): boolean => {
const root = r || parse(file).root || '.'
let ret: boolean
if (file === root) ret = false
else {
const m = map.get(file)
if (m !== undefined) {
ret = m
} else {
ret = mapHas(dirname(file), root)
}
}
map.set(file, ret)
return ret
}
opt.filter = filter
? (file, entry) =>
filter(file, entry) && mapHas(stripTrailingSlashes(file))
: file => mapHas(stripTrailingSlashes(file))
}
const listFileSync = (opt: TarOptionsSyncFile) => {
const p = list_(opt)
const file = opt.file
let fd
try {
const stat = fs.statSync(file)
const readSize = opt.maxReadSize || 16 * 1024 * 1024
if (stat.size < readSize) {
p.end(fs.readFileSync(file))
} else {
let pos = 0
const buf = Buffer.allocUnsafe(readSize)
fd = fs.openSync(file, 'r')
while (pos < stat.size) {
const bytesRead = fs.readSync(fd, buf, 0, readSize, pos)
pos += bytesRead
p.write(buf.subarray(0, bytesRead))
}
p.end()
}
} finally {
if (typeof fd === 'number') {
try {
fs.closeSync(fd)
/* c8 ignore next */
} catch (er) {}
}
}
}
const listFile = (opt: TarOptionsFile, cb?: () => void): Promise<void> => {
const parse = new Parser(opt)
const readSize = opt.maxReadSize || 16 * 1024 * 1024
const file = opt.file
const p = new Promise<void>((resolve, reject) => {
parse.on('error', reject)
parse.on('end', resolve)
fs.stat(file, (er, stat) => {
if (er) {
reject(er)
} else {
const stream = new fsm.ReadStream(file, {
readSize: readSize,
size: stat.size,
})
stream.on('error', reject)
stream.pipe(parse)
}
})
})
return cb ? p.then(cb, cb) : p
}
const list_ = (opt: TarOptions) => new Parser(opt)