From c970968ee60555a63cecf2253b25fa0ea161b00f Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 15 Dec 2010 15:15:27 -0800 Subject: [PATCH] better option parsing for socket.write() --- doc/api/net.markdown | 14 ++++++++++---- lib/net.js | 28 +++++++++++++++++++++++++++- test/simple/test-sendfd.js | 2 +- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/doc/api/net.markdown b/doc/api/net.markdown index 30dbd8a87b7..229ecaf38f4 100644 --- a/doc/api/net.markdown +++ b/doc/api/net.markdown @@ -196,16 +196,22 @@ context of the defined or default list of trusted CA certificates. Returns a JSON structure detailing the peer's certificate, containing a dictionary with keys for the certificate `'subject'`, `'issuer'`, `'valid_from'` and `'valid_to'`. -#### stream.write(data, encoding='ascii') +#### stream.write(data, [encoding]) -Sends data on the stream. The second parameter specifies the encoding in -the case of a string--it defaults to ASCII because encoding to UTF8 is rather -slow. +Sends data on the stream. The second parameter specifies the encoding in the +case of a string--it defaults to UTF8 encoding. Returns `true` if the entire data was flushed successfully to the kernel buffer. Returns `false` if all or part of the data was queued in user memory. `'drain'` will be emitted when the buffer is again free. +#### stream.write(data, [encoding], [fileDescriptor]) + +For UNIX sockets, it is possible to send a file descriptor through the +stream. Simply add the `fileDescriptor` argument and listen for the `'fd'` +event on the other end. + + #### stream.end([data], [encoding]) Half-closes the stream. I.E., it sends a FIN packet. It is possible the diff --git a/lib/net.js b/lib/net.js index 1f4910192f6..e6ec7961f0f 100644 --- a/lib/net.js +++ b/lib/net.js @@ -264,7 +264,33 @@ Object.defineProperty(Stream.prototype, 'readyState', { // Returns true if all the data was flushed to socket. Returns false if // something was queued. If data was queued, then the 'drain' event will // signal when it has been finally flushed to socket. -Stream.prototype.write = function(data, encoding, fd) { +Stream.prototype.write = function(data /* [encoding], [fd], [cb] */) { + var encoding, fd, cb; + + // parse arguments + if (typeof arguments[1] == 'string') { + encoding = arguments[1]; + if (typeof arguments[2] == 'number') { + fd = arguments[2]; + cb = arguments[3]; + } else { + cb = arguments[2]; + } + } else if (typeof arguments[1] == 'number') { + fd = arguments[1]; + cb = arguments[2]; + } else if (typeof arguments[2] == 'number') { + // This case is to support old calls when the encoding argument + // was not optional: s.write(buf, undefined, pipeFDs[1]) + encoding = arguments[1]; + fd = arguments[2]; + cb = arguments[3]; + } else { + cb = arguments[1]; + } + + // TODO - actually use cb + if (this._connecting || (this._writeQueue && this._writeQueue.length)) { if (!this._writeQueue) { this._writeQueue = []; diff --git a/test/simple/test-sendfd.js b/test/simple/test-sendfd.js index 6dd8000bf97..28586a606b7 100644 --- a/test/simple/test-sendfd.js +++ b/test/simple/test-sendfd.js @@ -94,7 +94,7 @@ var srv = net.createServer(function(s) { buf.write(JSON.stringify(DATA) + '\n', 'utf8'); s.write(str, 'utf8', pipeFDs[1]); - if (s.write(buf, undefined, pipeFDs[1])) { + if (s.write(buf, pipeFDs[1])) { netBinding.close(pipeFDs[1]); } else { s.addListener('drain', function() {