Skip to content

Commit c970968

Browse files
committed
better option parsing for socket.write()
1 parent 1dad95a commit c970968

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

doc/api/net.markdown

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,22 @@ context of the defined or default list of trusted CA certificates.
196196
Returns a JSON structure detailing the peer's certificate, containing a dictionary
197197
with keys for the certificate `'subject'`, `'issuer'`, `'valid_from'` and `'valid_to'`.
198198

199-
#### stream.write(data, encoding='ascii')
199+
#### stream.write(data, [encoding])
200200

201-
Sends data on the stream. The second parameter specifies the encoding in
202-
the case of a string--it defaults to ASCII because encoding to UTF8 is rather
203-
slow.
201+
Sends data on the stream. The second parameter specifies the encoding in the
202+
case of a string--it defaults to UTF8 encoding.
204203

205204
Returns `true` if the entire data was flushed successfully to the kernel
206205
buffer. Returns `false` if all or part of the data was queued in user memory.
207206
`'drain'` will be emitted when the buffer is again free.
208207

208+
#### stream.write(data, [encoding], [fileDescriptor])
209+
210+
For UNIX sockets, it is possible to send a file descriptor through the
211+
stream. Simply add the `fileDescriptor` argument and listen for the `'fd'`
212+
event on the other end.
213+
214+
209215
#### stream.end([data], [encoding])
210216

211217
Half-closes the stream. I.E., it sends a FIN packet. It is possible the

lib/net.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,33 @@ Object.defineProperty(Stream.prototype, 'readyState', {
264264
// Returns true if all the data was flushed to socket. Returns false if
265265
// something was queued. If data was queued, then the 'drain' event will
266266
// signal when it has been finally flushed to socket.
267-
Stream.prototype.write = function(data, encoding, fd) {
267+
Stream.prototype.write = function(data /* [encoding], [fd], [cb] */) {
268+
var encoding, fd, cb;
269+
270+
// parse arguments
271+
if (typeof arguments[1] == 'string') {
272+
encoding = arguments[1];
273+
if (typeof arguments[2] == 'number') {
274+
fd = arguments[2];
275+
cb = arguments[3];
276+
} else {
277+
cb = arguments[2];
278+
}
279+
} else if (typeof arguments[1] == 'number') {
280+
fd = arguments[1];
281+
cb = arguments[2];
282+
} else if (typeof arguments[2] == 'number') {
283+
// This case is to support old calls when the encoding argument
284+
// was not optional: s.write(buf, undefined, pipeFDs[1])
285+
encoding = arguments[1];
286+
fd = arguments[2];
287+
cb = arguments[3];
288+
} else {
289+
cb = arguments[1];
290+
}
291+
292+
// TODO - actually use cb
293+
268294
if (this._connecting || (this._writeQueue && this._writeQueue.length)) {
269295
if (!this._writeQueue) {
270296
this._writeQueue = [];

test/simple/test-sendfd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ var srv = net.createServer(function(s) {
9494
buf.write(JSON.stringify(DATA) + '\n', 'utf8');
9595

9696
s.write(str, 'utf8', pipeFDs[1]);
97-
if (s.write(buf, undefined, pipeFDs[1])) {
97+
if (s.write(buf, pipeFDs[1])) {
9898
netBinding.close(pipeFDs[1]);
9999
} else {
100100
s.addListener('drain', function() {

0 commit comments

Comments
 (0)