Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherrypick commits from joyent/node #834

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/api/assert.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ access it with `require('assert')`.

Throws an exception that displays the values for `actual` and `expected` separated by the provided operator.

## assert(value, message), assert.ok(value[, message])
## assert(value[, message]), assert.ok(value[, message])

Tests if value is truthy, it is equivalent to `assert.equal(true, !!value, message);`

Expand Down
2 changes: 1 addition & 1 deletion doc/api/fs.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Here is the synchronous version:

var fs = require('fs');

fs.unlinkSync('/tmp/hello')
fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');

With the asynchronous methods there is no guaranteed ordering. So the
Expand Down
2 changes: 1 addition & 1 deletion doc/api/smalloc.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ this it is possible to allocate external array data to more than a plain Object.
v8 does not support allocating external array data to an Array, and if passed
will throw.

It's possible is to specify the type of external array data you would like. All
It's possible to specify the type of external array data you would like. All
possible options are listed in `smalloc.Types`. Example usage:

var doubleArr = smalloc.alloc(3, smalloc.Types.Double);
Expand Down
8 changes: 3 additions & 5 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

const NativeModule = require('native_module');
const util = NativeModule.require('util');
const util = require('util');
const runInThisContext = require('vm').runInThisContext;
const runInNewContext = require('vm').runInNewContext;
const assert = require('assert').ok;
const fs = NativeModule.require('fs');
const fs = require('fs');
const path = require('path');


// If obj.hasOwnProperty has been overridden, then calling
Expand Down Expand Up @@ -41,9 +42,6 @@ Module.globalPaths = [];

Module.wrapper = NativeModule.wrapper;
Module.wrap = NativeModule.wrap;

const path = NativeModule.require('path');

Module._debug = util.debuglog('module');


Expand Down
34 changes: 18 additions & 16 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ function isPipeName(s) {
return typeof s === 'string' && toNumber(s) === false;
}

exports.createServer = function() {
return new Server(arguments[0], arguments[1]);
exports.createServer = function(options, connectionListener) {
return new Server(options, connectionListener);
};


Expand Down Expand Up @@ -300,17 +300,17 @@ Socket.prototype.listen = function() {


Socket.prototype.setTimeout = function(msecs, callback) {
if (msecs > 0 && isFinite(msecs)) {
if (msecs === 0) {
timers.unenroll(this);
if (callback) {
this.removeListener('timeout', callback);
}
} else {
timers.enroll(this, msecs);
timers._unrefActive(this);
if (callback) {
this.once('timeout', callback);
}
} else if (msecs === 0) {
timers.unenroll(this);
if (callback) {
this.removeListener('timeout', callback);
}
}
};

Expand Down Expand Up @@ -991,22 +991,24 @@ function afterConnect(status, handle, req, readable, writable) {
}


function Server(/* [ options, ] listener */) {
if (!(this instanceof Server)) return new Server(arguments[0], arguments[1]);
function Server(options, connectionListener) {
if (!(this instanceof Server))
return new Server(options, connectionListener);

events.EventEmitter.call(this);

var self = this;

var options;

if (typeof arguments[0] === 'function') {
if (typeof options === 'function') {
connectionListener = options;
options = {};
self.on('connection', arguments[0]);
self.on('connection', connectionListener);
} else {
options = arguments[0] || {};
options = options || {};

if (typeof arguments[1] === 'function') {
self.on('connection', arguments[1]);
if (typeof connectionListener === 'function') {
self.on('connection', connectionListener);
}
}

Expand Down
18 changes: 12 additions & 6 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
const Timer = process.binding('timer_wrap').Timer;
const L = require('_linklist');
const assert = require('assert').ok;

const util = require('util');
const debug = util.debuglog('timer');
const kOnTimeout = Timer.kOnTimeout | 0;

// Timeout values > TIMEOUT_MAX are set to 1.
const TIMEOUT_MAX = 2147483647; // 2^31-1

const debug = require('util').debuglog('timer');


// IDLE TIMEOUTS
//
// Because often many sockets will have the same idle timeout we will not
Expand Down Expand Up @@ -132,13 +130,21 @@ const unenroll = exports.unenroll = function(item) {

// Does not start the time, just sets up the members needed.
exports.enroll = function(item, msecs) {
if (typeof msecs !== 'number') {
throw new TypeError('msecs must be a number');
}

if (msecs < 0 || !isFinite(msecs)) {
throw new RangeError('msecs must be a non-negative finite number');
}

// if this item was already in a list somewhere
// then we should unenroll it from that
if (item._idleNext) unenroll(item);

// Ensure that msecs fits into signed int32
if (msecs > 0x7fffffff) {
msecs = 0x7fffffff;
if (msecs > TIMEOUT_MAX) {
msecs = TIMEOUT_MAX;
}

item._idleTimeout = msecs;
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-net-settimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var server = net.createServer(function(c) {
});
server.listen(common.PORT);

var killers = [0, Infinity, NaN];
var killers = [0];

var left = killers.length;
killers.forEach(function(killer) {
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-net-socket-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@ var common = require('../common');
var net = require('net');
var assert = require('assert');

// Verify that invalid delays throw
var noop = function() {};
var s = new net.Socket();
var nonNumericDelays = ['100', true, false, undefined, null, '', {}, noop, []];
var badRangeDelays = [-0.001, -1, -Infinity, Infinity, NaN];
var validDelays = [0, 0.001, 1, 1e6];

for (var i = 0; i < nonNumericDelays.length; i++) {
assert.throws(function() {
s.setTimeout(nonNumericDelays[i], noop);
}, TypeError);
}

for (var i = 0; i < badRangeDelays.length; i++) {
assert.throws(function() {
s.setTimeout(badRangeDelays[i], noop);
}, RangeError);
}

for (var i = 0; i < validDelays.length; i++) {
assert.doesNotThrow(function() {
s.setTimeout(validDelays[i], noop);
});
}

var timedout = false;

var server = net.Server();
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-timers-unref.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ setTimeout(function() {
interval = setInterval(function() {
unref_interval = true;
clearInterval(interval);
}, SHORT_TIME).unref();
}, SHORT_TIME);
interval.unref();

setTimeout(function() {
unref_timer = true;
Expand Down