-
Notifications
You must be signed in to change notification settings - Fork 42
/
unpack.ts
43 lines (35 loc) · 960 Bytes
/
unpack.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
import tar from 'tar-fs';
import zlib from 'zlib';
import fs from 'fs';
import path from 'path';
// see https://github.com/alixaxel/chrome-aws-lambda
export function unpack({
inputPath,
outputBaseDir = `/tmp`,
outputPath = `/tmp/instdir`
}): Promise<string> {
return new Promise((resolve, reject) => {
const input = path.resolve(inputPath);
const output = outputPath;
if (fs.existsSync(output) === true) {
return resolve(output);
}
const source = fs.createReadStream(input);
const target = tar.extract(outputBaseDir);
source.on('error', error => {
return reject(error);
});
target.on('error', error => {
return reject(error);
});
target.on('finish', () => {
fs.chmod(output, '0755', error => {
if (error) {
return reject(error);
}
return resolve(output);
});
});
source.pipe(zlib.createBrotliDecompress()).pipe(target);
});
}