From a78b3344f8bc7e6ca6afb37326347d68fe21b062 Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Sun, 13 Sep 2015 19:49:35 -0700 Subject: [PATCH] net: type check createServer options object net.createServer('aPipe') and net.createServer(8080) are mistakes, and now throw a TypeError instead of silently being treated as an object. PR-URL: https://github.com/nodejs/node/pull/2904 Reviewed-By: Colin Ihrig Reviewed-By: Evan Lucas Reviewed-By: Benjamin Gruenbaum --- lib/net.js | 4 +++- test/parallel/test-net-server-options.js | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-net-server-options.js diff --git a/lib/net.js b/lib/net.js index 2442bcdee36cd4..79692da3524ca9 100644 --- a/lib/net.js +++ b/lib/net.js @@ -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; diff --git a/test/parallel/test-net-server-options.js b/test/parallel/test-net-server-options.js new file mode 100644 index 00000000000000..1445c3e6b8245f --- /dev/null +++ b/test/parallel/test-net-server-options.js @@ -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);