Skip to content

Commit

Permalink
Use gunzip-maybe instead of reimplementing (#2971)
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel authored and arcanis committed Apr 18, 2017
1 parent 4e5b5f0 commit 0897457
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 58 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 @@ -13,6 +12,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 setupMirrorFromCache(): Promise<?string> {
Expand Down Expand Up @@ -70,10 +71,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
38 changes: 36 additions & 2 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.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 @@ -2390,6 +2401,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 @@ -2440,6 +2455,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:
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 @@ -3598,6 +3617,13 @@ pbkdf2@^3.0.3:
dependencies:
create-hmac "^1.1.2"

peek-stream@^1.1.0:
version "1.1.2"
resolved "https://registry.yarnpkg.com/peek-stream/-/peek-stream-1.1.2.tgz#97eb76365bcfd8c89e287f55c8b69d4c3e9bcc52"
dependencies:
duplexify "^3.5.0"
through2 "^2.0.3"

performance-now@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-0.2.0.tgz#33ef30c5c77d4ea21c5a53869d91b56d8f2555e5"
Expand Down Expand Up @@ -3688,6 +3714,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 @@ -4360,7 +4394,7 @@ 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:
Expand Down

0 comments on commit 0897457

Please sign in to comment.