/
addFilesFromTarball.ts
54 lines (52 loc) · 1.97 KB
/
addFilesFromTarball.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
import { Duplex, PassThrough } from 'stream'
import { DeferredManifestPromise, FilesIndex, FileWriteResult } from '@pnpm/fetcher-base'
import decompress from 'decompress-maybe'
import tar from 'tar-stream'
import { parseJsonStream } from './parseJson'
export default async function (
addStreamToCafs: (fileStream: PassThrough, mode: number) => Promise<FileWriteResult>,
_ignore: null | ((filename: string) => Boolean),
stream: NodeJS.ReadableStream,
manifest?: DeferredManifestPromise
): Promise<FilesIndex> {
const ignore = _ignore ?? (() => false)
const extract = tar.extract()
const filesIndex = {}
await new Promise<void>((resolve, reject) => {
extract.on('entry', (header, fileStream, next) => {
// There are some edge cases, where the same files are extracted multiple times.
// So there will be an entry for "lib/index.js" and another one for "lib//index.js",
// which are the same file.
// Hence, we are normalizing the file name, replacing // with / and checking for duplicates.
// Example of such package: @pnpm/colorize-semver-diff@1.0.1
const filename = header.name.slice(header.name.indexOf('/') + 1).replace(/\/\//g, '/')
if (header.type !== 'file' || ignore(filename) || filesIndex[filename]) {
fileStream.resume()
next()
return
}
if (filename === 'package.json' && (manifest != null)) {
parseJsonStream(fileStream, manifest)
}
const writeResult = addStreamToCafs(fileStream, header.mode!)
filesIndex[filename] = {
mode: header.mode!,
size: header.size,
writeResult,
}
next()
})
// listener
extract.on('finish', () => resolve())
extract.on('error', reject)
// pipe through extractor
stream
.on('error', reject)
.pipe(decompress() as Duplex)
.on('error', reject).pipe(extract)
})
if (!filesIndex['package.json'] && manifest != null) {
manifest.resolve(undefined)
}
return filesIndex
}