Skip to content
Browse files

Added setEncoding() support to zlib streams

  • Loading branch information...
1 parent de89bbb commit a4ae4030a41e172732b4305743a8022272f55b43 @mhart committed Jun 22, 2012
Showing with 67 additions and 1 deletion.
  1. +19 −1 lib/zlib.js
  2. +48 −0 test/simple/test-zlib-encoding.js
View
20 lib/zlib.js
@@ -269,6 +269,10 @@ function Zlib(opts, mode) {
}
}
+ if (opts.encoding) {
+ this.setEncoding(opts.encoding);
+ }
+
this._binding = new binding.Zlib(mode);
var self = this;
@@ -300,6 +304,11 @@ function Zlib(opts, mode) {
util.inherits(Zlib, Stream);
+Zlib.prototype.setEncoding = function(encoding) {
+ var StringDecoder = require('string_decoder').StringDecoder; // lazy load
+ this._decoder = new StringDecoder(encoding);
+};
+
Zlib.prototype.write = function write(chunk, cb) {
if (this._hadError) return true;
@@ -402,7 +411,7 @@ Zlib.prototype._process = function() {
if (have > 0) {
var out = self._buffer.slice(self._offset, self._offset + have);
self._offset += have;
- self.emit('data', out);
+ self._emitData(out);
}
// XXX Maybe have a 'min buffer' size so we don't dip into the
@@ -441,6 +450,15 @@ Zlib.prototype._process = function() {
}
};
+Zlib.prototype._emitData = function(d) {
+ if (this._decoder) {
+ var string = this._decoder.write(d);
+ if (string.length) this.emit('data', string);
+ } else {
+ this.emit('data', d);
+ }
+};
+
Zlib.prototype.pause = function() {
this._paused = true;
this.emit('pause');
View
48 test/simple/test-zlib-encoding.js
@@ -0,0 +1,48 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// 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.
+
+// test reading from a zlib stream using string encoding.
+
+var common = require('../common.js');
+var assert = require('assert');
+var zlib = require('zlib');
+var path = require('path');
+
+var gunzip = zlib.createGunzip({encoding:'hex'});
+
+var fs = require('fs');
+
+var fixture = path.resolve(common.fixturesDir, 'person.jpg.gz');
+var unzippedFixture = path.resolve(common.fixturesDir, 'person.jpg');
+var expect = fs.readFileSync(unzippedFixture, 'hex');
+var actual = '';
+
+var inp = fs.createReadStream(fixture).pipe(gunzip);
+
+inp.on('data', function(data) {
+ assert.equal(typeof(data), 'string', 'data should be a string');
+ actual += data;
+});
+
+process.on('exit', function() {
+ assert(actual.length > 0, 'data should have been read');
+ assert.equal(actual, expect, 'encoded file strings should match');
+});

0 comments on commit a4ae403

Please sign in to comment.
Something went wrong with that request. Please try again.