Skip to content

Commit

Permalink
v0.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltwaterC committed Aug 4, 2011
1 parent 2ffa186 commit caf99dd
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,6 @@
## v0.3.2
* This release includes a patched version of the gzbz2/gunzipstream module in order to workaround the gz error -5. The rest of the gzip dependency is still taken from the upstream module.

## v0.3.1
* The library calls the error code if the server prematurely closes the connection. In the future, if the accept-ranges header is defined into the response headers, the library should try to send subsequent requests in order to get the whole response.

Expand Down
112 changes: 112 additions & 0 deletions lib/gunzipstream.js
@@ -0,0 +1,112 @@
// Copyright 2009, Acknack Ltd. All rights reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

var fs = require('fs'),
sys = require('sys'),
gzbz2 = require('gzbz2'),
stream = require('stream');

/**
* wrap an readable stream (for binary data) with gunzip
*/
var GunzipStream = function(readStream, enc) {
stream.Stream.call(this);
var self = this;

self.stream = readStream;
self.gz = new gzbz2.Gunzip();
self.gz.init({encoding: enc});

self.ondata = function(data) {
try {
var inflated = self.gz.inflate(data);
self.emit('data', inflated);
} catch (err) {
self.gz.end();
self.emit('error', err);
self._cleanup();
}
};
self.onclose = function() {
self.gz.end();
self.emit('close');
self._cleanup();
};
self.onend = function() {
self.gz.end();
self.emit('end');
self._cleanup();
};
self.onerror = function(err) {
self.gz.end();
self.emit('error', err);
self._cleanup();
};
self.pause = function() {
self.stream.pause();
};
self.resume = function() {
self.stream.resume();
};
self._cleanup = function() {
self.stream.removeListener('data', self.ondata);
self.stream.removeListener('close', self.onclose);
self.stream.removeListener('end', self.onend);
self.stream.removeListener('error', self.onerror);
};

self.stream.addListener('data', self.ondata);
self.stream.addListener('end', self.onend);
self.stream.addListener('close', self.onclose);
self.stream.addListener('error', self.onerror);
};
sys.inherits(GunzipStream, stream.Stream);
exports.GunzipStream = GunzipStream;

/**
* this method accepts 4 main flavors:
*
* none gunzip from stdin
* ---------------------------------------------------------------
* @param stream ReadStream, if null/undefined, use stdin
* ---------------------------------------------------------------
* @param path string pathname, if null/undefined: use stdin
* ---------------------------------------------------------------
* @param path string pathname, if null/undefined && options.fd == null/undefined, use stdin
* @param options as would be given to fs.createReadStream()
*
* @return a GunzipStream object, the underlying ReadStream is available as attribute named 'stream'
*/
exports.wrap = function() {
// [stream] | [path, [options,]]
var stream = arguments[0], options = arguments[1];
var enc = options.encoding;
if( stream == null ) {
if( options.fd == null ) {
stream = process.openStdin();
}
} else if( typeof stream == 'string' ) {
if( options ) {
// we want to use buffers
options.encoding = null;
}
stream = fs.createReadStream(stream, options);
} // else stream is all set, options (if provided) are ignored
return new GunzipStream(stream, enc);
};
4 changes: 2 additions & 2 deletions lib/http-get.js
Expand Up @@ -4,8 +4,8 @@ var u = require('url');
var p = require('path');
var http = require('http');
var https = require('https');
/* 3rd party modules */
var gunzipstream = require('gzbz2/gunzipstream');
/* 3rd party patched module */
var gunzipstream = require('./gunzipstream.js');
/* reads the package.json information */
var pack = JSON.parse(fs.readFileSync(p.resolve(__dirname + '/../package.json')).toString('utf8'));

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "http-get",
"version": "0.3.1",
"version": "0.3.2",
"main": "./lib/http-get.js",
"description": "Simple to use node.js HTTP / HTTPS client for downloading remote files. Supports transparent gzip decoding.",
"dependencies": {
Expand Down

0 comments on commit caf99dd

Please sign in to comment.