Skip to content

Commit 0897457

Browse files
victornoelarcanis
authored andcommitted
Use gunzip-maybe instead of reimplementing (#2971)
1 parent 4e5b5f0 commit 0897457

File tree

4 files changed

+41
-58
lines changed

4 files changed

+41
-58
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';
@@ -13,6 +12,8 @@ const path = require('path');
1312
const tarFs = require('tar-fs');
1413
const url = require('url');
1514
const fs = require('fs');
15+
const stream = require('stream');
16+
const gunzip = require('gunzip-maybe');
1617

1718
export default class TarballFetcher extends BaseFetcher {
1819
async setupMirrorFromCache(): Promise<?string> {
@@ -70,10 +71,10 @@ export default class TarballFetcher extends BaseFetcher {
7071
reject: (error: Error) => void,
7172
): {
7273
validateStream: crypto.HashStream,
73-
extractorStream: UnpackStream,
74+
extractorStream: stream.Transform,
7475
} {
7576
const validateStream = new crypto.HashStream();
76-
const extractorStream = new UnpackStream();
77+
const extractorStream = gunzip();
7778
const untarStream = tarFs.extract(this.dest, {
7879
strip: 1,
7980
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: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ [email protected]:
13471347
dependencies:
13481348
readable-stream "~1.1.9"
13491349

1350-
duplexify@^3.5.0:
1350+
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"
@@ -2390,6 +2401,10 @@ is-ci@^1.0.10, is-ci@^1.0.9:
23902401
dependencies:
23912402
ci-info "^1.0.0"
23922403

2404+
is-deflate@^1.0.0:
2405+
version "1.0.0"
2406+
resolved "https://registry.yarnpkg.com/is-deflate/-/is-deflate-1.0.0.tgz#c862901c3c161fb09dac7cdc7e784f80e98f2f14"
2407+
23932408
is-dotfile@^1.0.0:
23942409
version "1.0.2"
23952410
resolved "https://registry.yarnpkg.com/is-dotfile/-/is-dotfile-1.0.2.tgz#2c132383f39199f8edc268ca01b9b007d205cc4d"
@@ -2440,6 +2455,10 @@ is-glob@^3.1.0:
24402455
dependencies:
24412456
is-extglob "^2.1.0"
24422457

2458+
is-gzip@^1.0.0:
2459+
version "1.0.0"
2460+
resolved "https://registry.yarnpkg.com/is-gzip/-/is-gzip-1.0.0.tgz#6ca8b07b99c77998025900e555ced8ed80879a83"
2461+
24432462
is-my-json-valid@^2.10.0:
24442463
version "2.16.0"
24452464
resolved "https://registry.yarnpkg.com/is-my-json-valid/-/is-my-json-valid-2.16.0.tgz#f079dd9bfdae65ee2038aae8acbc86ab109e3693"
@@ -3598,6 +3617,13 @@ pbkdf2@^3.0.3:
35983617
dependencies:
35993618
create-hmac "^1.1.2"
36003619

3620+
peek-stream@^1.1.0:
3621+
version "1.1.2"
3622+
resolved "https://registry.yarnpkg.com/peek-stream/-/peek-stream-1.1.2.tgz#97eb76365bcfd8c89e287f55c8b69d4c3e9bcc52"
3623+
dependencies:
3624+
duplexify "^3.5.0"
3625+
through2 "^2.0.3"
3626+
36013627
performance-now@^0.2.0:
36023628
version "0.2.0"
36033629
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-0.2.0.tgz#33ef30c5c77d4ea21c5a53869d91b56d8f2555e5"
@@ -3688,6 +3714,14 @@ pump@^1.0.0:
36883714
end-of-stream "^1.1.0"
36893715
once "^1.3.1"
36903716

3717+
pumpify@^1.3.3:
3718+
version "1.3.5"
3719+
resolved "https://registry.yarnpkg.com/pumpify/-/pumpify-1.3.5.tgz#1b671c619940abcaeac0ad0e3a3c164be760993b"
3720+
dependencies:
3721+
duplexify "^3.1.2"
3722+
inherits "^2.0.1"
3723+
pump "^1.0.0"
3724+
36913725
36923726
version "1.3.2"
36933727
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d"
@@ -4360,7 +4394,7 @@ throat@^3.0.0:
43604394
version "3.0.0"
43614395
resolved "https://registry.yarnpkg.com/throat/-/throat-3.0.0.tgz#e7c64c867cbb3845f10877642f7b60055b8ec0d6"
43624396

4363-
[email protected], through2@^2, through2@^2.0.0, through2@^2.0.1:
4397+
[email protected], through2@^2, through2@^2.0.0, through2@^2.0.1, through2@^2.0.3:
43644398
version "2.0.3"
43654399
resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.3.tgz#0004569b37c7c74ba39c43f3ced78d1ad94140be"
43664400
dependencies:

0 commit comments

Comments
 (0)