Node.js: socket.js improvements.

- Fixed handling of the "options" parameter in Socket() constructor;
- Now the connect() method returns "this";
- Deduplicated the address() method;
- Added missing "callback" argument to the end() method;
- Now the destroy() method returns "this";
- Added "timeout" argument type check in the setTimeout() method.
This commit is contained in:
Andrey Zelenkov
2018-11-06 19:04:01 +03:00
parent b7442743cf
commit 4a77c447ba

View File

@@ -12,15 +12,16 @@ const unit_lib = require('unit-http/build/Release/unit-http.node');
function Socket(options) { function Socket(options) {
EventEmitter.call(this); EventEmitter.call(this);
if (typeof options === 'number') { options = options || {};
options = { fd: options };
} else if (options === undefined) { if (typeof options !== 'object') {
options = {}; throw new TypeError('Options must be object');
} }
this.readable = options.readable !== false; this.readable = (typeof options.readable === 'boolean' ? options.readable
this.writable = options.writable !== false; : false);
this.writable = (typeof options.writable === 'boolean' ? options.writable
: false);
} }
util.inherits(Socket, EventEmitter); util.inherits(Socket, EventEmitter);
@@ -38,25 +39,24 @@ Socket.prototype.remotePort = 0;
Socket.prototype.address = function address() { Socket.prototype.address = function address() {
}; };
Socket.prototype.connect = function connect(options, callback) { Socket.prototype.connect = function connect(options, connectListener) {
if (callback !== null) { this.once('connect', connectListener);
this.once('connect', cb);
}
this.connecting = true; this.connecting = true;
this.writable = true; this.writable = true;
};
Socket.prototype.address = function address() { return this;
}; };
Socket.prototype.destroy = function destroy(exception) { Socket.prototype.destroy = function destroy(exception) {
this.connecting = false; this.connecting = false;
this.readable = false; this.readable = false;
this.writable = false; this.writable = false;
return this;
}; };
Socket.prototype.end = function end(data, encoding) { Socket.prototype.end = function end(data, encoding, callback) {
}; };
Socket.prototype.pause = function pause() { Socket.prototype.pause = function pause() {
@@ -77,13 +77,15 @@ Socket.prototype.setKeepAlive = function setKeepAlive(enable, initialDelay) {
Socket.prototype.setNoDelay = function setNoDelay(noDelay) { Socket.prototype.setNoDelay = function setNoDelay(noDelay) {
}; };
Socket.prototype.setTimeout = function setTimeout(msecs, callback) { Socket.prototype.setTimeout = function setTimeout(timeout, callback) {
this.timeout = msecs; if (typeof timeout !== 'number') {
throw new TypeError('Timeout must be number');
if (callback) {
this.on('timeout', callback);
} }
this.timeout = timeout;
this.on('timeout', callback);
return this; return this;
}; };