-
Notifications
You must be signed in to change notification settings - Fork 52
/
index.js
141 lines (120 loc) · 3.7 KB
/
index.js
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
'use strict';
const path = require('path');
const fs = require('graceful-fs');
const decompressTar = require('decompress-tar');
const decompressTarbz2 = require('decompress-tarbz2');
const decompressTargz = require('decompress-targz');
const decompressUnzip = require('decompress-unzip');
const makeDir = require('make-dir');
const pify = require('pify');
const stripDirs = require('strip-dirs');
const fsP = pify(fs);
const runPlugins = (input, opts) => {
if (opts.plugins.length === 0) {
return Promise.resolve([]);
}
return Promise.all(opts.plugins.map(x => x(input, opts))).then(files => files.reduce((a, b) => a.concat(b)));
};
const safeMakeDir = (dir, realOutputPath) => {
return fsP.realpath(dir)
.catch(_ => {
const parent = path.dirname(dir);
return safeMakeDir(parent, realOutputPath);
})
.then(realParentPath => {
if (realParentPath.indexOf(realOutputPath) !== 0) {
throw (new Error('Refusing to create a directory outside the output path.'));
}
return makeDir(dir);
});
};
const extractFile = (input, output, opts) => runPlugins(input, opts).then(files => {
if (opts.strip > 0) {
files = files
.map(x => {
x.path = stripDirs(x.path, opts.strip);
return x;
})
.filter(x => x.path !== '.');
}
if (typeof opts.filter === 'function') {
files = files.filter(opts.filter);
}
if (typeof opts.map === 'function') {
files = files.map(opts.map);
}
if (!output) {
return files;
}
return Promise.all(files.map(x => {
const dest = path.join(output, x.path);
const mode = x.mode & ~process.umask();
const now = new Date();
if (x.type === 'directory') {
return makeDir(output)
.then(outputPath => fsP.realpath(outputPath))
.then(realOutputPath => safeMakeDir(dest, realOutputPath))
.then(() => fsP.utimes(dest, now, x.mtime))
.then(() => x);
}
return makeDir(output)
.then(outputPath => fsP.realpath(outputPath))
.then(realOutputPath => {
return safeMakeDir(path.dirname(dest), realOutputPath)
.then(() => realOutputPath);
})
.then(realOutputPath => {
// Check for symlinks pointing outside output directory
return fsP.readlink(dest)
.catch(_ => {
// File doesn't exist yet
return null;
})
.then(symlinkPointsTo => {
if (symlinkPointsTo && symlinkPointsTo.indexOf(realOutputPath) !== 0) {
throw (new Error('Refusing to write into a file outside output directory, through a symlink: ' + symlinkPointsTo));
}
return realOutputPath;
});
})
.then(realOutputPath => {
return fsP.realpath(path.dirname(dest))
.then(realDestinationDir => {
if (realDestinationDir.indexOf(realOutputPath) !== 0) {
throw (new Error('Refusing to write outside output directory: ' + realDestinationDir));
}
});
})
.then(() => {
if (x.type === 'link') {
return fsP.link(x.linkname, dest);
}
if (x.type === 'symlink' && process.platform === 'win32') {
return fsP.link(x.linkname, dest);
}
if (x.type === 'symlink') {
return fsP.symlink(x.linkname, dest);
}
return fsP.writeFile(dest, x.data, {mode});
})
.then(() => x.type === 'file' && fsP.utimes(dest, now, x.mtime))
.then(() => x);
}));
});
module.exports = (input, output, opts) => {
if (typeof input !== 'string' && !Buffer.isBuffer(input)) {
return Promise.reject(new TypeError('Input file required'));
}
if (typeof output === 'object') {
opts = output;
output = null;
}
opts = Object.assign({plugins: [
decompressTar(),
decompressTarbz2(),
decompressTargz(),
decompressUnzip()
]}, opts);
const read = typeof input === 'string' ? fsP.readFile(input) : Promise.resolve(input);
return read.then(buf => extractFile(buf, output, opts));
};