Skip to content

Commit 157a34a

Browse files
authored
switched to tar-fs from node-tar to unpack tars (#2826)
* Set default network concurrency to 8 and switched to tar-fs instead of node-tar * migrated pack test * another file migrated to tar-fs * changed concurrency in another PR * wip migrating git to use tar-fs * removed tar dependency * bump tar-stream * reverted changes to url in hasArchiveCapability * made changes according to feedback * 0o444 * added flow suppress
1 parent ddff4c5 commit 157a34a

File tree

7 files changed

+77
-25
lines changed

7 files changed

+77
-25
lines changed

__tests__/commands/pack.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const os = require('os');
1414
const stream = require('stream');
1515

1616
const zlib = require('zlib');
17-
const tar = require('tar');
17+
const tarFs = require('tar-fs');
1818
const fs2 = require('fs');
1919

2020
const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'pack');
@@ -83,8 +83,12 @@ export async function getFilesFromArchive(source, destination): Promise<Array<st
8383
const unzip = new Promise((resolve, reject) => {
8484
fs2.createReadStream(source)
8585
.pipe(new zlib.Gunzip())
86-
.pipe(tar.Extract({path: destination, strip: 1}))
87-
.on('end', resolve)
86+
.pipe(tarFs.extract(destination, {
87+
strip: 1,
88+
dmode: 0o555, // all dirs should be readable
89+
fmode: 0o444, // all files should be readable
90+
}))
91+
.on('finish', resolve)
8892
.on('error', reject);
8993
});
9094
await unzip;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"roadrunner": "^1.1.0",
3636
"semver": "^5.1.0",
3737
"strip-bom": "^3.0.0",
38-
"tar": "^2.2.1",
38+
"tar-fs": "^1.15.1",
3939
"tar-stream": "^1.5.2",
4040
"v8-compile-cache": "^1.0.0",
4141
"validate-npm-package-license": "^3.0.1"

src/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const CACHE_VERSION = 1;
3131
export const LOCKFILE_VERSION = 1;
3232

3333
// max amount of network requests to perform concurrently
34-
export const NETWORK_CONCURRENCY = 8;
34+
export const NETWORK_CONCURRENCY = 16;
3535

3636
// max amount of child processes to execute concurrently
3737
export const CHILD_CONCURRENCY = 5;

src/fetchers/git-fetcher.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Git from '../util/git.js';
77
import * as fsUtil from '../util/fs.js';
88
import * as crypto from '../util/crypto.js';
99

10-
const tar = require('tar');
10+
const tarFs = require('tar-fs');
1111
const url = require('url');
1212
const path = require('path');
1313
const fs = require('fs');
@@ -34,15 +34,18 @@ export default class GitFetcher extends BaseFetcher {
3434
}
3535

3636
return new Promise((resolve, reject) => {
37-
const untarStream = tar.Extract({path: this.dest});
37+
const untarStream = tarFs.extract(this.dest, {
38+
dmode: 0o555, // all dirs should be readable
39+
fmode: 0o444, // all files should be readable
40+
});
3841

3942
const hashStream = new crypto.HashStream();
4043

4144
const cachedStream = fs.createReadStream(localTarball);
4245
cachedStream
4346
.pipe(hashStream)
4447
.pipe(untarStream)
45-
.on('end', () => {
48+
.on('finish', () => {
4649
const expectHash = this.hash;
4750
const actualHash = hashStream.getHash();
4851
if (!expectHash || expectHash === actualHash) {

src/fetchers/tarball-fetcher.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import ROOT_USER from '../util/root-user.js';
1212

1313
const invariant = require('invariant');
1414
const path = require('path');
15-
const tar = require('tar');
15+
const tarFs = require('tar-fs');
1616
const url = require('url');
1717
const fs = require('fs');
1818

@@ -78,7 +78,11 @@ export default class TarballFetcher extends BaseFetcher {
7878
} {
7979
const validateStream = new crypto.HashStream();
8080
const extractorStream = new UnpackStream();
81-
const untarStream = tar.Extract({path: this.dest, strip: 1});
81+
const untarStream = tarFs.extract(this.dest, {
82+
strip: 1,
83+
dmode: 0o555, // all dirs should be readable
84+
fmode: 0o444, // all files should be readable
85+
});
8286

8387
extractorStream
8488
.pipe(untarStream)
@@ -89,7 +93,7 @@ export default class TarballFetcher extends BaseFetcher {
8993
entry.props.gid = entry.gid = 0;
9094
}
9195
})
92-
.on('end', () => {
96+
.on('finish', () => {
9397
const expectHash = this.hash;
9498
const actualHash = validateStream.getHash();
9599
if (!expectHash || expectHash === actualHash) {

src/util/git.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import map from './map.js';
1111

1212
const invariant = require('invariant');
1313
const semver = require('semver');
14+
const StringDecoder = require('string_decoder').StringDecoder;
15+
const tarFs = require('tar-fs');
16+
const tarStream = require('tar-stream');
1417
const url = require('url');
15-
const tar = require('tar');
1618
import {createWriteStream} from 'fs';
1719

1820
type GitRefs = {
@@ -205,9 +207,12 @@ export default class Git {
205207
async _cloneViaRemoteArchive(dest: string): Promise<void> {
206208
await child.spawn('git', ['archive', `--remote=${this.url}`, this.ref], {
207209
process(proc, update, reject, done) {
208-
const extractor = tar.Extract({path: dest});
210+
const extractor = tarFs.extract(dest, {
211+
dmode: 0o555, // all dirs should be readable
212+
fmode: 0o444, // all files should be readable
213+
});
209214
extractor.on('error', reject);
210-
extractor.on('end', done);
215+
extractor.on('finish', done);
211216

212217
proc.stdout.pipe(extractor);
213218
proc.on('error', reject);
@@ -219,9 +224,13 @@ export default class Git {
219224
await child.spawn('git', ['archive', this.hash], {
220225
cwd: this.cwd,
221226
process(proc, resolve, reject, done) {
222-
const extractor = tar.Extract({path: dest});
227+
const extractor = tarFs.extract(dest, {
228+
dmode: 0o555, // all dirs should be readable
229+
fmode: 0o444, // all files should be readable
230+
});
231+
223232
extractor.on('error', reject);
224-
extractor.on('end', done);
233+
extractor.on('finish', done);
225234

226235
proc.stdout.pipe(extractor);
227236
},
@@ -279,13 +288,25 @@ export default class Git {
279288
try {
280289
return await child.spawn('git', ['archive', `--remote=${this.url}`, this.ref, filename], {
281290
process(proc, update, reject, done) {
282-
const parser = tar.Parse();
291+
const parser = tarStream.extract();
283292

284293
parser.on('error', reject);
285-
parser.on('end', done);
286-
287-
parser.on('data', (entry: Buffer) => {
288-
update(entry.toString());
294+
parser.on('finish', done);
295+
296+
parser.on('entry', (header, stream, next) => {
297+
const decoder = new StringDecoder('utf8');
298+
let fileContent = '';
299+
300+
stream.on('data', (buffer) => {
301+
fileContent += decoder.write(buffer);
302+
});
303+
stream.on('end', () => {
304+
// $FlowFixMe: suppressing this error due to bug https://github.com/facebook/flow/pull/3483
305+
const remaining: string = decoder.end();
306+
update(fileContent + remaining);
307+
next();
308+
});
309+
stream.resume();
289310
});
290311

291312
proc.stdout.pipe(parser);

yarn.lock

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,10 @@ chokidar@^1.4.3, chokidar@^1.6.1:
985985
optionalDependencies:
986986
fsevents "^1.0.0"
987987

988+
chownr@^1.0.1:
989+
version "1.0.1"
990+
resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.0.1.tgz#e2a75042a9551908bebd25b8523d5f9769d79181"
991+
988992
ci-info@^1.0.0:
989993
version "1.0.0"
990994
resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-1.0.0.tgz#dc5285f2b4e251821683681c381c3388f46ec534"
@@ -1380,7 +1384,7 @@ end-of-stream@1.0.0:
13801384
dependencies:
13811385
once "~1.3.0"
13821386

1383-
end-of-stream@^1.0.0:
1387+
end-of-stream@^1.0.0, end-of-stream@^1.1.0:
13841388
version "1.1.0"
13851389
resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.1.0.tgz#e9353258baa9108965efc41cb0ef8ade2f3cfb07"
13861390
dependencies:
@@ -3406,7 +3410,7 @@ object.omit@^2.0.0:
34063410
for-own "^0.1.4"
34073411
is-extendable "^0.1.1"
34083412

3409-
once@^1.3.0, once@^1.4.0:
3413+
once@^1.3.0, once@^1.3.1, once@^1.4.0:
34103414
version "1.4.0"
34113415
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
34123416
dependencies:
@@ -3671,6 +3675,13 @@ public-encrypt@^4.0.0:
36713675
parse-asn1 "^5.0.0"
36723676
randombytes "^2.0.1"
36733677

3678+
pump@^1.0.0:
3679+
version "1.0.2"
3680+
resolved "https://registry.yarnpkg.com/pump/-/pump-1.0.2.tgz#3b3ee6512f94f0e575538c17995f9f16990a5d51"
3681+
dependencies:
3682+
end-of-stream "^1.1.0"
3683+
once "^1.3.1"
3684+
36743685
punycode@1.3.2:
36753686
version "1.3.2"
36763687
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d"
@@ -4268,6 +4279,15 @@ tapable@^0.2.5, tapable@~0.2.5:
42684279
version "0.2.6"
42694280
resolved "https://registry.yarnpkg.com/tapable/-/tapable-0.2.6.tgz#206be8e188860b514425375e6f1ae89bfb01fd8d"
42704281

4282+
tar-fs@^1.15.1:
4283+
version "1.15.1"
4284+
resolved "https://registry.yarnpkg.com/tar-fs/-/tar-fs-1.15.1.tgz#f4622f5d5e250742b3679a9a8463acfc12cdefd1"
4285+
dependencies:
4286+
chownr "^1.0.1"
4287+
mkdirp "^0.5.0"
4288+
pump "^1.0.0"
4289+
tar-stream "^1.1.2"
4290+
42714291
tar-pack@~3.3.0:
42724292
version "3.3.0"
42734293
resolved "https://registry.yarnpkg.com/tar-pack/-/tar-pack-3.3.0.tgz#30931816418f55afc4d21775afdd6720cee45dae"
@@ -4281,7 +4301,7 @@ tar-pack@~3.3.0:
42814301
tar "~2.2.1"
42824302
uid-number "~0.0.6"
42834303

4284-
tar-stream@^1.5.2:
4304+
tar-stream@^1.1.2, tar-stream@^1.5.2:
42854305
version "1.5.2"
42864306
resolved "https://registry.yarnpkg.com/tar-stream/-/tar-stream-1.5.2.tgz#fbc6c6e83c1a19d4cb48c7d96171fc248effc7bf"
42874307
dependencies:
@@ -4290,7 +4310,7 @@ tar-stream@^1.5.2:
42904310
readable-stream "^2.0.0"
42914311
xtend "^4.0.0"
42924312

4293-
tar@^2.0.0, tar@^2.2.1, tar@~2.2.1:
4313+
tar@^2.0.0, tar@~2.2.1:
42944314
version "2.2.1"
42954315
resolved "https://registry.yarnpkg.com/tar/-/tar-2.2.1.tgz#8e4d2a256c0e2185c6b18ad694aec968b83cb1d1"
42964316
dependencies:

0 commit comments

Comments
 (0)