Skip to content

Commit

Permalink
Use gunzip-maybe instead of reimplementing
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Mar 22, 2017
1 parent 6e9a9a6 commit 704adc4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 59 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"death": "^1.0.0",
"debug": "^2.2.0",
"detect-indent": "^5.0.0",
"gunzip-maybe": "^1.4.0",
"ini": "^1.3.4",
"inquirer": "^3.0.1",
"invariant": "^2.2.0",
Expand Down
7 changes: 4 additions & 3 deletions src/fetchers/tarball-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import http from 'http';
import {SecurityError, MessageError} from '../errors.js';
import type {FetchedOverride} from '../types.js';
import {UnpackStream} from '../util/stream.js';
import * as constants from '../constants.js';
import * as crypto from '../util/crypto.js';
import BaseFetcher from './base-fetcher.js';
Expand All @@ -14,6 +13,8 @@ const path = require('path');
const tarFs = require('tar-fs');
const url = require('url');
const fs = require('fs');
const stream = require('stream');
const gunzip = require('gunzip-maybe');

export default class TarballFetcher extends BaseFetcher {
async getResolvedFromCached(hash: string): Promise<?string> {
Expand Down Expand Up @@ -73,10 +74,10 @@ export default class TarballFetcher extends BaseFetcher {
reject: (error: Error) => void,
): {
validateStream: crypto.HashStream,
extractorStream: UnpackStream,
extractorStream: stream.Transform,
} {
const validateStream = new crypto.HashStream();
const extractorStream = new UnpackStream();
const extractorStream = gunzip();
const untarStream = tarFs.extract(this.dest, {
strip: 1,
dmode: 0o555, // all dirs should be readable
Expand Down
53 changes: 0 additions & 53 deletions src/util/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,6 @@

const invariant = require('invariant');
const stream = require('stream');
const zlib = require('zlib');

function hasGzipHeader(chunk: Buffer): boolean {
return chunk[0] === 0x1F && chunk[1] === 0x8B && chunk[2] === 0x08;
}

type UnpackOptions = duplexStreamOptions;

export class UnpackStream extends stream.Transform {
constructor(options?: UnpackOptions) {
super(options);
this._srcStream = null;
this._readHeader = false;
this.once('pipe', (src: stream.Readable) => {
this._srcStream = src;
});
}

_srcStream: ?stream.Readable;
_readHeader: boolean;

_transform(
chunk: Buffer | string,
encoding: string,
callback: (error: ?Error, data?: Buffer | string) => void,
) {
if (!this._readHeader) {
this._readHeader = true;
invariant(chunk instanceof Buffer, 'Chunk must be a buffer');
if (hasGzipHeader(chunk)) {
// Stop receiving data from the src stream, and pipe it instead to zlib,
// then pipe it's output through us.
const unzipStream = zlib.createUnzip();
unzipStream.on('error', (err) => this.emit('error', err));

const srcStream = this._srcStream;
invariant(srcStream, 'How? To get here a stream must have been piped!');
srcStream
.pipe(unzipStream)
.pipe(this);

// Unpipe after another stream has been piped so it's always piping to
// something, thus avoiding pausing it.
srcStream.unpipe(this);
unzipStream.write(chunk);
this._srcStream = null;
callback();
return;
}
}
callback(null, chunk);
}
}

export class ConcatStream extends stream.Transform {
constructor(done: (buf: Buffer) => void) {
Expand Down
51 changes: 48 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ duplexer2@0.0.2:
dependencies:
readable-stream "~1.1.9"

duplexify@^3.5.0:
duplexify@^3.0.1, duplexify@^3.1.2, duplexify@^3.5.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-3.5.0.tgz#1aa773002e1578457e9d9d4a50b0ccaaebcbd604"
dependencies:
Expand Down Expand Up @@ -2161,6 +2161,17 @@ gulplog@^1.0.0:
dependencies:
glogg "^1.0.0"

gunzip-maybe@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/gunzip-maybe/-/gunzip-maybe-1.4.0.tgz#7d8316c8d0571e1d08a5a79e46fff0afe8172b19"
dependencies:
browserify-zlib "^0.1.4"
is-deflate "^1.0.0"
is-gzip "^1.0.0"
peek-stream "^1.1.0"
pumpify "^1.3.3"
through2 "^2.0.3"

handlebars@^4.0.3:
version "4.0.6"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.6.tgz#2ce4484850537f9c97a8026d5399b935c4ed4ed7"
Expand Down Expand Up @@ -2388,6 +2399,10 @@ is-ci@^1.0.10, is-ci@^1.0.9:
dependencies:
ci-info "^1.0.0"

is-deflate@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-deflate/-/is-deflate-1.0.0.tgz#c862901c3c161fb09dac7cdc7e784f80e98f2f14"

is-dotfile@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-dotfile/-/is-dotfile-1.0.2.tgz#2c132383f39199f8edc268ca01b9b007d205cc4d"
Expand Down Expand Up @@ -2438,6 +2453,10 @@ is-glob@^3.1.0:
dependencies:
is-extglob "^2.1.0"

is-gzip@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-gzip/-/is-gzip-1.0.0.tgz#6ca8b07b99c77998025900e555ced8ed80879a83"

is-my-json-valid@^2.10.0, is-my-json-valid@^2.12.4:
version "2.16.0"
resolved "https://registry.yarnpkg.com/is-my-json-valid/-/is-my-json-valid-2.16.0.tgz#f079dd9bfdae65ee2038aae8acbc86ab109e3693"
Expand Down Expand Up @@ -3596,6 +3615,13 @@ pbkdf2@^3.0.3:
dependencies:
create-hmac "^1.1.2"

peek-stream@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/peek-stream/-/peek-stream-1.1.1.tgz#5324259966a9c97ce2f0470f67c23bf248f8e7f0"
dependencies:
duplexify "^3.0.1"
through2 "^0.5.1"

pify@^2.0.0, pify@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c"
Expand Down Expand Up @@ -3682,6 +3708,14 @@ pump@^1.0.0:
end-of-stream "^1.1.0"
once "^1.3.1"

pumpify@^1.3.3:
version "1.3.5"
resolved "https://registry.yarnpkg.com/pumpify/-/pumpify-1.3.5.tgz#1b671c619940abcaeac0ad0e3a3c164be760993b"
dependencies:
duplexify "^3.1.2"
inherits "^2.0.1"
pump "^1.0.0"

punycode@1.3.2:
version "1.3.2"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d"
Expand Down Expand Up @@ -3743,7 +3777,7 @@ read@^1.0.7:
dependencies:
mute-stream "~0.0.4"

"readable-stream@>=1.0.33-1 <1.1.0-0":
"readable-stream@>=1.0.33-1 <1.1.0-0", readable-stream@~1.0.17:
version "1.0.34"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c"
dependencies:
Expand Down Expand Up @@ -4352,13 +4386,20 @@ throat@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/throat/-/throat-3.0.0.tgz#e7c64c867cbb3845f10877642f7b60055b8ec0d6"

through2@2.X, through2@^2, through2@^2.0.0, through2@^2.0.1:
through2@2.X, through2@^2, through2@^2.0.0, through2@^2.0.1, through2@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.3.tgz#0004569b37c7c74ba39c43f3ced78d1ad94140be"
dependencies:
readable-stream "^2.1.5"
xtend "~4.0.1"

through2@^0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/through2/-/through2-0.5.1.tgz#dfdd012eb9c700e2323fd334f38ac622ab372da7"
dependencies:
readable-stream "~1.0.17"
xtend "~3.0.0"

through2@^0.6.1:
version "0.6.5"
resolved "https://registry.yarnpkg.com/through2/-/through2-0.6.5.tgz#41ab9c67b29d57209071410e1d7a7a968cd3ad48"
Expand Down Expand Up @@ -4717,6 +4758,10 @@ xml-name-validator@^2.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"

xtend@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-3.0.0.tgz#5cce7407baf642cba7becda568111c493f59665a"

y18n@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.1.tgz#6d15fba884c08679c0d77e88e7759e811e07fa41"
Expand Down

0 comments on commit 704adc4

Please sign in to comment.