Skip to content

Commit 704adc4

Browse files
committed
Use gunzip-maybe instead of reimplementing
1 parent 6e9a9a6 commit 704adc4

File tree

4 files changed

+53
-59
lines changed

4 files changed

+53
-59
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"death": "^1.0.0",
1616
"debug": "^2.2.0",
1717
"detect-indent": "^5.0.0",
18+
"gunzip-maybe": "^1.4.0",
1819
"ini": "^1.3.4",
1920
"inquirer": "^3.0.1",
2021
"invariant": "^2.2.0",

src/fetchers/tarball-fetcher.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import http from 'http';
44
import {SecurityError, MessageError} from '../errors.js';
55
import type {FetchedOverride} from '../types.js';
6-
import {UnpackStream} from '../util/stream.js';
76
import * as constants from '../constants.js';
87
import * as crypto from '../util/crypto.js';
98
import BaseFetcher from './base-fetcher.js';
@@ -14,6 +13,8 @@ const path = require('path');
1413
const tarFs = require('tar-fs');
1514
const url = require('url');
1615
const fs = require('fs');
16+
const stream = require('stream');
17+
const gunzip = require('gunzip-maybe');
1718

1819
export default class TarballFetcher extends BaseFetcher {
1920
async getResolvedFromCached(hash: string): Promise<?string> {
@@ -73,10 +74,10 @@ export default class TarballFetcher extends BaseFetcher {
7374
reject: (error: Error) => void,
7475
): {
7576
validateStream: crypto.HashStream,
76-
extractorStream: UnpackStream,
77+
extractorStream: stream.Transform,
7778
} {
7879
const validateStream = new crypto.HashStream();
79-
const extractorStream = new UnpackStream();
80+
const extractorStream = gunzip();
8081
const untarStream = tarFs.extract(this.dest, {
8182
strip: 1,
8283
dmode: 0o555, // all dirs should be readable

src/util/stream.js

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,6 @@
22

33
const invariant = require('invariant');
44
const stream = require('stream');
5-
const zlib = require('zlib');
6-
7-
function hasGzipHeader(chunk: Buffer): boolean {
8-
return chunk[0] === 0x1F && chunk[1] === 0x8B && chunk[2] === 0x08;
9-
}
10-
11-
type UnpackOptions = duplexStreamOptions;
12-
13-
export class UnpackStream extends stream.Transform {
14-
constructor(options?: UnpackOptions) {
15-
super(options);
16-
this._srcStream = null;
17-
this._readHeader = false;
18-
this.once('pipe', (src: stream.Readable) => {
19-
this._srcStream = src;
20-
});
21-
}
22-
23-
_srcStream: ?stream.Readable;
24-
_readHeader: boolean;
25-
26-
_transform(
27-
chunk: Buffer | string,
28-
encoding: string,
29-
callback: (error: ?Error, data?: Buffer | string) => void,
30-
) {
31-
if (!this._readHeader) {
32-
this._readHeader = true;
33-
invariant(chunk instanceof Buffer, 'Chunk must be a buffer');
34-
if (hasGzipHeader(chunk)) {
35-
// Stop receiving data from the src stream, and pipe it instead to zlib,
36-
// then pipe it's output through us.
37-
const unzipStream = zlib.createUnzip();
38-
unzipStream.on('error', (err) => this.emit('error', err));
39-
40-
const srcStream = this._srcStream;
41-
invariant(srcStream, 'How? To get here a stream must have been piped!');
42-
srcStream
43-
.pipe(unzipStream)
44-
.pipe(this);
45-
46-
// Unpipe after another stream has been piped so it's always piping to
47-
// something, thus avoiding pausing it.
48-
srcStream.unpipe(this);
49-
unzipStream.write(chunk);
50-
this._srcStream = null;
51-
callback();
52-
return;
53-
}
54-
}
55-
callback(null, chunk);
56-
}
57-
}
585

596
export class ConcatStream extends stream.Transform {
607
constructor(done: (buf: Buffer) => void) {

yarn.lock

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ duplexer2@0.0.2:
13471347
dependencies:
13481348
readable-stream "~1.1.9"
13491349

1350-
duplexify@^3.5.0:
1350+
duplexify@^3.0.1, duplexify@^3.1.2, duplexify@^3.5.0:
13511351
version "3.5.0"
13521352
resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-3.5.0.tgz#1aa773002e1578457e9d9d4a50b0ccaaebcbd604"
13531353
dependencies:
@@ -2161,6 +2161,17 @@ gulplog@^1.0.0:
21612161
dependencies:
21622162
glogg "^1.0.0"
21632163

2164+
gunzip-maybe@^1.4.0:
2165+
version "1.4.0"
2166+
resolved "https://registry.yarnpkg.com/gunzip-maybe/-/gunzip-maybe-1.4.0.tgz#7d8316c8d0571e1d08a5a79e46fff0afe8172b19"
2167+
dependencies:
2168+
browserify-zlib "^0.1.4"
2169+
is-deflate "^1.0.0"
2170+
is-gzip "^1.0.0"
2171+
peek-stream "^1.1.0"
2172+
pumpify "^1.3.3"
2173+
through2 "^2.0.3"
2174+
21642175
handlebars@^4.0.3:
21652176
version "4.0.6"
21662177
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.6.tgz#2ce4484850537f9c97a8026d5399b935c4ed4ed7"
@@ -2388,6 +2399,10 @@ is-ci@^1.0.10, is-ci@^1.0.9:
23882399
dependencies:
23892400
ci-info "^1.0.0"
23902401

2402+
is-deflate@^1.0.0:
2403+
version "1.0.0"
2404+
resolved "https://registry.yarnpkg.com/is-deflate/-/is-deflate-1.0.0.tgz#c862901c3c161fb09dac7cdc7e784f80e98f2f14"
2405+
23912406
is-dotfile@^1.0.0:
23922407
version "1.0.2"
23932408
resolved "https://registry.yarnpkg.com/is-dotfile/-/is-dotfile-1.0.2.tgz#2c132383f39199f8edc268ca01b9b007d205cc4d"
@@ -2438,6 +2453,10 @@ is-glob@^3.1.0:
24382453
dependencies:
24392454
is-extglob "^2.1.0"
24402455

2456+
is-gzip@^1.0.0:
2457+
version "1.0.0"
2458+
resolved "https://registry.yarnpkg.com/is-gzip/-/is-gzip-1.0.0.tgz#6ca8b07b99c77998025900e555ced8ed80879a83"
2459+
24412460
is-my-json-valid@^2.10.0, is-my-json-valid@^2.12.4:
24422461
version "2.16.0"
24432462
resolved "https://registry.yarnpkg.com/is-my-json-valid/-/is-my-json-valid-2.16.0.tgz#f079dd9bfdae65ee2038aae8acbc86ab109e3693"
@@ -3596,6 +3615,13 @@ pbkdf2@^3.0.3:
35963615
dependencies:
35973616
create-hmac "^1.1.2"
35983617

3618+
peek-stream@^1.1.0:
3619+
version "1.1.1"
3620+
resolved "https://registry.yarnpkg.com/peek-stream/-/peek-stream-1.1.1.tgz#5324259966a9c97ce2f0470f67c23bf248f8e7f0"
3621+
dependencies:
3622+
duplexify "^3.0.1"
3623+
through2 "^0.5.1"
3624+
35993625
pify@^2.0.0, pify@^2.3.0:
36003626
version "2.3.0"
36013627
resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c"
@@ -3682,6 +3708,14 @@ pump@^1.0.0:
36823708
end-of-stream "^1.1.0"
36833709
once "^1.3.1"
36843710

3711+
pumpify@^1.3.3:
3712+
version "1.3.5"
3713+
resolved "https://registry.yarnpkg.com/pumpify/-/pumpify-1.3.5.tgz#1b671c619940abcaeac0ad0e3a3c164be760993b"
3714+
dependencies:
3715+
duplexify "^3.1.2"
3716+
inherits "^2.0.1"
3717+
pump "^1.0.0"
3718+
36853719
punycode@1.3.2:
36863720
version "1.3.2"
36873721
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d"
@@ -3743,7 +3777,7 @@ read@^1.0.7:
37433777
dependencies:
37443778
mute-stream "~0.0.4"
37453779

3746-
"readable-stream@>=1.0.33-1 <1.1.0-0":
3780+
"readable-stream@>=1.0.33-1 <1.1.0-0", readable-stream@~1.0.17:
37473781
version "1.0.34"
37483782
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c"
37493783
dependencies:
@@ -4352,13 +4386,20 @@ throat@^3.0.0:
43524386
version "3.0.0"
43534387
resolved "https://registry.yarnpkg.com/throat/-/throat-3.0.0.tgz#e7c64c867cbb3845f10877642f7b60055b8ec0d6"
43544388

4355-
through2@2.X, through2@^2, through2@^2.0.0, through2@^2.0.1:
4389+
through2@2.X, through2@^2, through2@^2.0.0, through2@^2.0.1, through2@^2.0.3:
43564390
version "2.0.3"
43574391
resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.3.tgz#0004569b37c7c74ba39c43f3ced78d1ad94140be"
43584392
dependencies:
43594393
readable-stream "^2.1.5"
43604394
xtend "~4.0.1"
43614395

4396+
through2@^0.5.1:
4397+
version "0.5.1"
4398+
resolved "https://registry.yarnpkg.com/through2/-/through2-0.5.1.tgz#dfdd012eb9c700e2323fd334f38ac622ab372da7"
4399+
dependencies:
4400+
readable-stream "~1.0.17"
4401+
xtend "~3.0.0"
4402+
43624403
through2@^0.6.1:
43634404
version "0.6.5"
43644405
resolved "https://registry.yarnpkg.com/through2/-/through2-0.6.5.tgz#41ab9c67b29d57209071410e1d7a7a968cd3ad48"
@@ -4717,6 +4758,10 @@ xml-name-validator@^2.0.1:
47174758
version "4.0.1"
47184759
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"
47194760

4761+
xtend@~3.0.0:
4762+
version "3.0.0"
4763+
resolved "https://registry.yarnpkg.com/xtend/-/xtend-3.0.0.tgz#5cce7407baf642cba7becda568111c493f59665a"
4764+
47204765
y18n@^3.2.1:
47214766
version "3.2.1"
47224767
resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.1.tgz#6d15fba884c08679c0d77e88e7759e811e07fa41"

0 commit comments

Comments
 (0)