@@ -228,18 +228,19 @@ Decompress a raw Buffer with Unzip.
Each class takes an options object. All options are optional. (The
convenience methods use the default settings for all options.)

Note that some options are only
relevant when compressing, and are ignored by the decompression classes.
Note that some options are only relevant when compressing, and are
ignored by the decompression classes.

* flush (default: `zlib.Z_NO_FLUSH`)
* chunkSize (default: 16*1024)
* windowBits
* level (compression only)
* memLevel (compression only)
* strategy (compression only)
* dictionary (deflate/inflate only, empty dictionary by default)

See the description of `deflateInit2` and `inflateInit2`
at <http://zlib.net/manual.html#Advanced> for more information on these.
See the description of `deflateInit2` and `inflateInit2` at
<http://zlib.net/manual.html#Advanced> for more information on these.

## Memory Usage Tuning

@@ -226,8 +226,17 @@ function Zlib(opts, mode) {

Transform.call(this, opts);

// means a different thing there.
this._readableState.chunkSize = null;
if (opts.flush) {
if (opts.flush !== binding.Z_NO_FLUSH &&
opts.flush !== binding.Z_PARTIAL_FLUSH &&
opts.flush !== binding.Z_SYNC_FLUSH &&
opts.flush !== binding.Z_FULL_FLUSH &&
opts.flush !== binding.Z_FINISH &&
opts.flush !== binding.Z_BLOCK) {
throw new Error('Invalid flush flag: ' + opts.flush);
}
}
this._flushFlag = opts.flush || binding.Z_NO_FLUSH;

if (opts.chunkSize) {
if (opts.chunkSize < exports.Z_MIN_CHUNK ||
@@ -308,22 +317,30 @@ Zlib.prototype.reset = function reset() {
return this._binding.reset();
};

// This is the _flush function called by the transform class,
// internally, when the last chunk has been written.
Zlib.prototype._flush = function(callback) {
this._transform(null, '', callback);
this._transform(new Buffer(0), '', callback);
};

Zlib.prototype.flush = function(callback) {
var ws = this._writableState;
var ts = this._transformState;

if (ws.writing) {
ws.needDrain = true;
if (ws.ended) {
if (callback)
process.nextTick(callback);
} else if (ws.ending) {
if (callback)
this.once('end', callback);
} else if (ws.needDrain) {
var self = this;
this.once('drain', function() {
self._flush(callback);
self.flush(callback);
});
} else
this._flush(callback || function() {});
} else {
this._flushFlag = binding.Z_FULL_FLUSH;
this.write(new Buffer(0), '', callback);
}
};

Zlib.prototype.close = function(callback) {
@@ -358,10 +375,14 @@ Zlib.prototype._transform = function(chunk, encoding, cb) {
// goodness.
if (last)
flushFlag = binding.Z_FINISH;
else if (chunk === null)
flushFlag = binding.Z_FULL_FLUSH;
else
flushFlag = binding.Z_NO_FLUSH;
else {
flushFlag = this._flushFlag;
// once we've flushed the last of the queue, stop flushing and
// go back to the normal behavior.
if (chunk.length >= ws.length) {
this._flushFlag = this._opts.flush || binding.Z_NO_FLUSH;
}
}

var availInBefore = chunk && chunk.length;
var availOutBefore = this._chunkSize - this._offset;
@@ -1893,7 +1893,7 @@ Handle<Value> DLOpen(const v8::Arguments& args) {
Local<String> errmsg = String::New(uv_dlerror(&lib));
#ifdef _WIN32
// Windows needs to add the filename into the error message
errmsg = String::Concat(errmsg, args[0]->ToString());
errmsg = String::Concat(errmsg, args[1]->ToString());
#endif
return ThrowException(Exception::Error(errmsg));
}
@@ -24,6 +24,11 @@ var fork = require('child_process').fork;
var assert = require('assert');
var common = require('../common');

if (process.platform === 'win32') {
console.error('Sending dgram sockets to child processes not supported');
process.exit(0);
}

if (process.argv[2] === 'child') {
var childCollected = 0;
var server;
@@ -26,32 +26,33 @@ var path = require('path');
var common = require('../common');
var msg = {test: 'this'};
var nodePath = process.execPath;
var symlinkPath = path.join(common.tmpDir, 'node-symlink');
var copyPath = path.join(common.tmpDir, 'node-copy.exe');

if (process.env.FORK) {
assert(process.send);
assert.equal(process.argv[0], symlinkPath);
assert.equal(process.argv[0], copyPath);
process.send(msg);
process.exit();
}
else {
try {
fs.unlinkSync(symlinkPath);
fs.unlinkSync(copyPath);
}
catch (e) {
if (e.code !== 'ENOENT') throw e;
}
fs.symlinkSync(nodePath, symlinkPath);
fs.writeFileSync(copyPath, fs.readFileSync(nodePath));
fs.chmodSync(copyPath, '0755');

var child = require('child_process').fork(__filename, {
execPath: symlinkPath,
execPath: copyPath,
env: { FORK: 'true' }
});
child.on('message', common.mustCall(function(recv) {
assert.deepEqual(msg, recv);
}));
child.on('exit', common.mustCall(function(code) {
fs.unlinkSync(symlinkPath);
fs.unlinkSync(copyPath);
assert.equal(code, 0);
}));
}
@@ -44,7 +44,8 @@ function grandparent() {
child.on('close', function(code, signal) {
assert.equal(code, 0);
assert.equal(signal, null);
assert.equal(output, input);
// cat on windows adds a \r\n at the end.
assert.equal(output.trim(), input.trim());
});
}

@@ -50,6 +50,17 @@ if (!id) {
var a = fork(__filename, ['one']);
var b = fork(__filename, ['two']);

a.on('exit', function(c) {
if (c)
throw new Error('A exited with ' + c);
});

b.on('exit', function(c) {
if (c)
throw new Error('B exited with ' + c);
});


a.on('message', function(m) {
if (typeof m === 'object') return;
assert.equal(m, 'READY');
@@ -162,7 +162,15 @@ function doTest(cb, done) {

nodeProcess.stdout.once('data', function(c) {
console.log('>>> new node process: %d', nodeProcess.pid);
process._debugProcess(nodeProcess.pid);
var failed = true;
try {
process._debugProcess(nodeProcess.pid);
failed = false;
} finally {
// At least TRY not to leave zombie procs if this fails.
if (failed)
nodeProcess.kill('SIGTERM');
}
console.log('>>> starting debugger session');
});

@@ -31,14 +31,7 @@ var http = require('http');
var cp = require('child_process');
var fs = require('fs');

var filename = require('path').join(common.tmpDir || '/tmp', 'big');

// Clean up after ourselves. Leaving files around
// in the tmp/ directory may break tests that depend
// on a certain number of files being there.
process.on('exit', function() {
fs.unlink(filename);
});
var filename = require('path').join(common.tmpDir, 'big');

var count = 0;
function maybeMakeRequest() {
@@ -50,6 +43,7 @@ function maybeMakeRequest() {
var hex = stdout.match(/([A-Fa-f0-9]{40})/)[0];
assert.equal('8c206a1a87599f532ce68675536f0b1546900d7a', hex);
console.log('got the correct response');
fs.unlink(filename);
server.close();
});
}
@@ -141,13 +141,13 @@ test('async passthrough', function(t) {
pt.write(new Buffer('kuel'));
pt.end();

setTimeout(function() {
pt.on('finish', function() {
t.equal(pt.read(5).toString(), 'foogb');
t.equal(pt.read(5).toString(), 'arkba');
t.equal(pt.read(5).toString(), 'zykue');
t.equal(pt.read(5).toString(), 'l');
t.end();
}, 100);
});
});

test('assymetric transform (expand)', function(t) {
@@ -170,7 +170,7 @@ test('assymetric transform (expand)', function(t) {
pt.write(new Buffer('kuel'));
pt.end();

setTimeout(function() {
pt.on('finish', function() {
t.equal(pt.read(5).toString(), 'foogf');
t.equal(pt.read(5).toString(), 'oogba');
t.equal(pt.read(5).toString(), 'rkbar');
@@ -179,7 +179,7 @@ test('assymetric transform (expand)', function(t) {
t.equal(pt.read(5).toString(), 'uelku');
t.equal(pt.read(5).toString(), 'el');
t.end();
}, 200);
});
});

test('assymetric transform (compress)', function(t) {
@@ -205,11 +205,9 @@ test('assymetric transform (compress)', function(t) {

pt._flush = function(cb) {
// just output whatever we have.
setTimeout(function() {
pt.push(new Buffer(this.state));
this.state = '';
cb();
}.bind(this), 10);
pt.push(new Buffer(this.state));
this.state = '';
cb();
};

pt.write(new Buffer('aaaa'));
@@ -229,12 +227,12 @@ test('assymetric transform (compress)', function(t) {
pt.end();

// 'abcdeabcdeabcd'
setTimeout(function() {
pt.on('finish', function() {
t.equal(pt.read(5).toString(), 'abcde');
t.equal(pt.read(5).toString(), 'abcde');
t.equal(pt.read(5).toString(), 'abcd');
t.end();
}, 200);
});
});


@@ -51,6 +51,7 @@ function doTest() {
};
var requestCount = 0;
var session;
var badOpenSSL = false;

var server = tls.createServer(options, function(cleartext) {
cleartext.on('error', function(er) {
@@ -88,18 +89,30 @@ function doTest() {
'-reconnect',
'-no_ticket'
], {
customFds: [0, 1, 2]
stdio: [ 0, 1, 'pipe' ]
});
var err = '';
client.stderr.setEncoding('utf8');
client.stderr.on('data', function(chunk) {
err += chunk;
});
client.on('exit', function(code) {
assert.equal(code, 0);
if (/^unknown option/.test(err)) {
// using an incompatible version of openssl
assert(code);
badOpenSSL = true;
} else
assert.equal(code, 0);
server.close();
});
});

process.on('exit', function() {
assert.ok(session);
if (!badOpenSSL) {
assert.ok(session);

// initial request + reconnect requests (5 times)
assert.equal(requestCount, 6);
// initial request + reconnect requests (5 times)
assert.equal(requestCount, 6);
}
});
}
@@ -0,0 +1,54 @@
// 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.

var common = require('../common');
var assert = require('assert');
var zlib = require('zlib');
var fs = require('fs');

var gzip = zlib.createGzip();
var gunz = zlib.createUnzip();

gzip.pipe(gunz);

var output = '';
var input = 'A line of data\n';
gunz.setEncoding('utf8');
gunz.on('data', function(c) {
output += c;
});

process.on('exit', function() {
assert.equal(output, input);

// Make sure that the flush flag was set back to normal
assert.equal(gzip._flushFlag, zlib.Z_NO_FLUSH);

console.log('ok');
});

// make sure that flush/write doesn't trigger an assert failure
gzip.flush(); write();
function write() {
gzip.write(input);
gzip.end();
gunz.read(0);
}