@vltpkg/tar 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/unpack.d.ts +5 -0
- package/dist/unpack.js +139 -22
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/unpack.d.ts
CHANGED
package/dist/unpack.js
CHANGED
|
@@ -22,27 +22,115 @@ const exists = async (path) => {
|
|
|
22
22
|
let id = 1;
|
|
23
23
|
const tmp = randomBytes(6).toString('hex') + '.';
|
|
24
24
|
const tmpSuffix = () => tmp + String(id++);
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
/* c8 ignore start - case-folding is platform-specific */
|
|
26
|
+
const foldKeys = process.platform === 'darwin' || process.platform === 'win32';
|
|
27
|
+
const entryKey = (p) => (foldKeys ? p.toLowerCase() : p);
|
|
28
|
+
/* c8 ignore stop */
|
|
29
|
+
// Shared across concurrent unpacks. Reify runs many extractions at
|
|
30
|
+
// once; a per-tarball pool would multiply in-flight writeFile fds.
|
|
31
|
+
const parseWriteLanes = (raw) => {
|
|
32
|
+
const n = Number(raw);
|
|
33
|
+
return Number.isFinite(n) && n >= 1 ? Math.floor(n) : 64;
|
|
34
|
+
};
|
|
35
|
+
const writeLaneLimit = parseWriteLanes(process.env.VLT_TAR_WRITE_LANES);
|
|
36
|
+
let writeLanesUsed = 0;
|
|
37
|
+
const writeLaneWaiters = [];
|
|
38
|
+
let writeLaneHead = 0;
|
|
39
|
+
const acquireWriteLane = async () => {
|
|
40
|
+
if (writeLanesUsed < writeLaneLimit) {
|
|
41
|
+
writeLanesUsed++;
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
await new Promise(res => writeLaneWaiters.push(res));
|
|
45
|
+
};
|
|
46
|
+
const releaseWriteLane = () => {
|
|
47
|
+
// consume from a head index instead of shift(): shift is O(n) per
|
|
48
|
+
// release, quadratic when many writes queue behind the lane limit.
|
|
49
|
+
const next = writeLaneWaiters[writeLaneHead];
|
|
50
|
+
if (next) {
|
|
51
|
+
writeLaneWaiters[writeLaneHead] = undefined;
|
|
52
|
+
writeLaneHead++;
|
|
53
|
+
if (writeLaneHead === writeLaneWaiters.length) {
|
|
54
|
+
writeLaneWaiters.length = 0;
|
|
55
|
+
writeLaneHead = 0;
|
|
56
|
+
}
|
|
57
|
+
next();
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
writeLanesUsed--;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
const withWriteLane = async (fn) => {
|
|
64
|
+
await acquireWriteLane();
|
|
65
|
+
try {
|
|
66
|
+
return await fn();
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
releaseWriteLane();
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const rethrowFirst = (results) => {
|
|
73
|
+
for (const result of results) {
|
|
74
|
+
if (result.status === 'rejected')
|
|
75
|
+
throw result.reason;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
// Fast path: accept only paths that cannot escape. Anything else
|
|
79
|
+
// falls through to relative()/resolve() so drive-relative and
|
|
80
|
+
// `./`-segment cases keep today's exact (platform/cwd-dependent)
|
|
81
|
+
// behavior.
|
|
82
|
+
const isClearlySafeRelPath = (sub) => {
|
|
83
|
+
const len = sub.length;
|
|
84
|
+
if (len === 0)
|
|
85
|
+
return true;
|
|
86
|
+
// leading /
|
|
87
|
+
if (sub.charCodeAt(0) === 47)
|
|
88
|
+
return false;
|
|
89
|
+
// <letter>: prefix (drive-relative)
|
|
90
|
+
if (len >= 2 && sub.charCodeAt(1) === 58) {
|
|
91
|
+
const c = sub.charCodeAt(0);
|
|
92
|
+
if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
let i = 0;
|
|
97
|
+
while (i < len) {
|
|
98
|
+
let j = i;
|
|
99
|
+
while (j < len && sub.charCodeAt(j) !== 47)
|
|
100
|
+
j++;
|
|
101
|
+
const slen = j - i;
|
|
102
|
+
if (slen === 1 && sub.charCodeAt(i) === 46)
|
|
103
|
+
return false;
|
|
104
|
+
if (slen === 2 &&
|
|
105
|
+
sub.charCodeAt(i) === 46 &&
|
|
106
|
+
sub.charCodeAt(i + 1) === 46) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
i = j + 1;
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
};
|
|
113
|
+
export const checkFs = (h, tarDir, target) => {
|
|
27
114
|
if (!h.path)
|
|
28
115
|
return false;
|
|
29
116
|
if (!tarDir)
|
|
30
117
|
return false;
|
|
31
|
-
/* c8 ignore stop */
|
|
32
118
|
h.path = h.path.replace(/[\\/]+/g, '/');
|
|
33
119
|
// packages should always be in a 'package' tarDir in the archive
|
|
34
120
|
if (!h.path.startsWith(tarDir))
|
|
35
121
|
return false;
|
|
122
|
+
const sub = h.path.slice(tarDir.length);
|
|
123
|
+
if (isClearlySafeRelPath(sub))
|
|
124
|
+
return true;
|
|
36
125
|
// entries must stay within the package root. separator-aware, so that
|
|
37
126
|
// a sibling dir whose name extends the target's is not a prefix match.
|
|
38
|
-
const rel = relative(target, resolve(target,
|
|
127
|
+
const rel = relative(target, resolve(target, sub));
|
|
39
128
|
if (rel === '..' || rel.startsWith(`..${sep}`) || isAbsolute(rel)) {
|
|
40
129
|
return false;
|
|
41
130
|
}
|
|
42
131
|
return true;
|
|
43
132
|
};
|
|
44
133
|
const write = async (path, body, executable = false) => {
|
|
45
|
-
await mkdirp(dirname(path));
|
|
46
134
|
// if the mode is world-executable, then make it executable
|
|
47
135
|
// this is needed for some packages that have a file that is
|
|
48
136
|
// not a declared bin, but still used as a cli executable.
|
|
@@ -50,17 +138,6 @@ const write = async (path, body, executable = false) => {
|
|
|
50
138
|
mode: executable ? 0o777 : 0o666,
|
|
51
139
|
});
|
|
52
140
|
};
|
|
53
|
-
const made = new Set();
|
|
54
|
-
const making = new Map();
|
|
55
|
-
const mkdirp = async (d) => {
|
|
56
|
-
if (!made.has(d)) {
|
|
57
|
-
const m = making.get(d) ??
|
|
58
|
-
mkdir(d, { recursive: true, mode: 0o777 }).then(() => making.delete(d));
|
|
59
|
-
making.set(d, m);
|
|
60
|
-
await m;
|
|
61
|
-
made.add(d);
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
141
|
export const unpack = async (tarData, target) => {
|
|
65
142
|
const isGzip = tarData[0] === 0x1f && tarData[1] === 0x8b;
|
|
66
143
|
await unpackUnzipped(isGzip ? await unzip(tarData) : tarData, target);
|
|
@@ -92,9 +169,9 @@ const unpackUnzipped = async (buffer, target) => {
|
|
|
92
169
|
}
|
|
93
170
|
const tmp = dirname(target) + sep + '.' + basename(target) + '.' + tmpSuffix();
|
|
94
171
|
const og = tmp + '.ORIGINAL';
|
|
95
|
-
await Promise.all([rimraf(tmp), rimraf(og)]);
|
|
96
172
|
let succeeded = false;
|
|
97
173
|
try {
|
|
174
|
+
const entries = new Map();
|
|
98
175
|
let tarDir = undefined;
|
|
99
176
|
let offset = 0;
|
|
100
177
|
let h;
|
|
@@ -122,10 +199,23 @@ const unpackUnzipped = async (buffer, target) => {
|
|
|
122
199
|
continue;
|
|
123
200
|
if (!checkFs(h, tarDir, tmp))
|
|
124
201
|
continue;
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
202
|
+
{
|
|
203
|
+
const dest = resolve(tmp, h.path.substring(tarDir.length));
|
|
204
|
+
const key = entryKey(dest);
|
|
205
|
+
// a repeated path is fine (last wins), but flipping between
|
|
206
|
+
// file and directory would silently discard data.
|
|
207
|
+
if (entries.get(key)?.dir === true) {
|
|
208
|
+
throw error('file/directory collision in tarball', {
|
|
209
|
+
path: dest,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
entries.set(key, {
|
|
213
|
+
path: dest,
|
|
214
|
+
body,
|
|
215
|
+
executable: 1 === ((h.mode ?? 0x666) & 1),
|
|
216
|
+
dir: false,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
129
219
|
break;
|
|
130
220
|
case 'Directory':
|
|
131
221
|
/* c8 ignore next 2 */
|
|
@@ -135,7 +225,19 @@ const unpackUnzipped = async (buffer, target) => {
|
|
|
135
225
|
continue;
|
|
136
226
|
if (!checkFs(h, tarDir, tmp))
|
|
137
227
|
continue;
|
|
138
|
-
|
|
228
|
+
{
|
|
229
|
+
const dest = resolve(tmp, h.path.substring(tarDir.length));
|
|
230
|
+
const key = entryKey(dest);
|
|
231
|
+
if (entries.get(key)?.dir === false) {
|
|
232
|
+
throw error('file/directory collision in tarball', {
|
|
233
|
+
path: dest,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
entries.set(key, {
|
|
237
|
+
path: dest,
|
|
238
|
+
dir: true,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
139
241
|
break;
|
|
140
242
|
case 'GlobalExtendedHeader':
|
|
141
243
|
gex = Pax.parse(body.toString(), gex, true);
|
|
@@ -151,6 +253,21 @@ const unpackUnzipped = async (buffer, target) => {
|
|
|
151
253
|
break;
|
|
152
254
|
}
|
|
153
255
|
}
|
|
256
|
+
// Per-unpack memo: paths are tmp-scoped and never reused across
|
|
257
|
+
// unpacks. The unique dir set is the memo; making/made globals
|
|
258
|
+
// previously leaked ~18k strings per install.
|
|
259
|
+
const dirs = new Set();
|
|
260
|
+
const files = [];
|
|
261
|
+
for (const e of entries.values()) {
|
|
262
|
+
if (e.dir)
|
|
263
|
+
dirs.add(e.path);
|
|
264
|
+
else {
|
|
265
|
+
dirs.add(dirname(e.path));
|
|
266
|
+
files.push(e);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
rethrowFirst(await Promise.allSettled([...dirs].map(d => mkdir(d, { recursive: true, mode: 0o777 }))));
|
|
270
|
+
rethrowFirst(await Promise.allSettled(files.map(f => withWriteLane(() => write(f.path, f.body, f.executable)))));
|
|
154
271
|
const targetExists = await exists(target);
|
|
155
272
|
if (targetExists)
|
|
156
273
|
await rename(target, og);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vltpkg/tar",
|
|
3
3
|
"description": "An extremely limited and very fast tar extractor",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.3",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/vltpkg/vltpkg.git",
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"url": "http://vlt.sh"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@vltpkg/error-cause": "1.0.
|
|
17
|
-
"@vltpkg/types": "1.0.
|
|
16
|
+
"@vltpkg/error-cause": "1.0.3",
|
|
17
|
+
"@vltpkg/types": "1.0.3",
|
|
18
18
|
"rimraf": "^6.1.2",
|
|
19
19
|
"tar": "^7.5.2"
|
|
20
20
|
},
|