Skip to content

Commit

Permalink
net: type check createServer options object
Browse files Browse the repository at this point in the history
net.createServer('aPipe') and net.createServer(8080) are mistakes,
and now throw a TypeError instead of silently being treated as an
object.

PR-URL: #2904
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
sam-github authored and benjamingr committed Mar 15, 2016
1 parent b801e39 commit a78b334
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1090,12 +1090,14 @@ function Server(options, connectionListener) {
connectionListener = options;
options = {};
self.on('connection', connectionListener);
} else {
} else if (options == null || typeof options === 'object') {
options = options || {};

if (typeof connectionListener === 'function') {
self.on('connection', connectionListener);
}
} else {
throw new TypeError('options must be an object');
}

this._connections = 0;
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-net-server-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');

assert.throws(function() { net.createServer('path'); }, TypeError);
assert.throws(function() { net.createServer(common.PORT); }, TypeError);

0 comments on commit a78b334

Please sign in to comment.