-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
tgzUtils.ts
113 lines (85 loc) Β· 3.49 KB
/
tgzUtils.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
import {FakeFS, PortablePath, ZipFS, NodeFS, ppath} from '@yarnpkg/fslib';
import {Parse} from 'tar';
import {tmpNameSync} from 'tmp';
interface MakeArchiveFromDirectoryOptions {
baseFs?: FakeFS<PortablePath>,
prefixPath?: PortablePath | null,
};
export async function makeArchiveFromDirectory(source: PortablePath, {baseFs = new NodeFS(), prefixPath = PortablePath.root}: MakeArchiveFromDirectoryOptions = {}): Promise<ZipFS> {
const zipFs = new ZipFS(NodeFS.toPortablePath(tmpNameSync()), {create: true});
const target = ppath.resolve(PortablePath.root, prefixPath!);
await zipFs.copyPromise(target, source, {baseFs});
return zipFs;
}
interface ExtractBufferOptions {
prefixPath?: PortablePath,
stripComponents?: number,
};
export async function convertToZip(tgz: Buffer, opts: ExtractBufferOptions) {
return await extractArchiveTo(tgz, new ZipFS(NodeFS.toPortablePath(tmpNameSync()), {create: true}), opts);
}
export async function extractArchiveTo<T extends FakeFS<PortablePath>>(tgz: Buffer, targetFs: T, {stripComponents = 0, prefixPath = PortablePath.dot}: ExtractBufferOptions = {}): Promise<T> {
// 1980-01-01, like Fedora
const defaultTime = 315532800;
// @ts-ignore: Typescript doesn't want me to use new
const parser = new Parse();
function ignore(entry: any) {
// Disallow absolute paths; might be malicious (ex: /etc/passwd)
if (entry[0] === `/`)
return true;
const parts = entry.path.split(/\//g);
// We also ignore paths that could lead to escaping outside the archive
if (parts.some((part: string) => part === `..`))
return true;
if (parts.length <= stripComponents)
return true;
return false;
}
parser.on(`entry`, (entry: any) => {
if (ignore(entry)) {
entry.resume();
return;
}
const parts = entry.path.split(/\//g);
const mappedPath = ppath.join(prefixPath, parts.slice(stripComponents).join(`/`));
const chunks: Array<Buffer> = [];
let mode = 0o644;
// If a single executable bit is set, normalize so that all are
if (entry.type === `Directory` || (entry.mode & 0o111) !== 0)
mode |= 0o111;
entry.on(`data`, (chunk: Buffer) => {
chunks.push(chunk);
});
entry.on(`end`, () => {
switch (entry.type) {
case `Directory`: {
targetFs.mkdirpSync(ppath.dirname(mappedPath), {chmod: 0o755, utimes: [defaultTime, defaultTime]});
targetFs.mkdirSync(mappedPath);
targetFs.chmodSync(mappedPath, mode);
targetFs.utimesSync(mappedPath, defaultTime, defaultTime);
} break;
case `OldFile`:
case `File`: {
targetFs.mkdirpSync(ppath.dirname(mappedPath), {chmod: 0o755, utimes: [defaultTime, defaultTime]});
targetFs.writeFileSync(mappedPath, Buffer.concat(chunks));
targetFs.chmodSync(mappedPath, mode);
targetFs.utimesSync(mappedPath, defaultTime, defaultTime);
} break;
case `SymbolicLink`: {
targetFs.mkdirpSync(ppath.dirname(mappedPath), {chmod: 0o755, utimes: [defaultTime, defaultTime]});
targetFs.symlinkSync(entry.linkpath, mappedPath);
targetFs.lutimesSync!(mappedPath, defaultTime, defaultTime);
} break;
}
});
});
return await new Promise<T>((resolve, reject) => {
parser.on(`error`, (error: Error) => {
reject(error);
});
parser.on(`close`, () => {
resolve(targetFs);
});
parser.end(tgz);
});
}