From 81307440440127c7c99183734098f97fac5e214d Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Mon, 28 Oct 2013 14:58:37 -0700 Subject: [PATCH] dgram: send() can accept strings Strings passed to Socket#send() will be passed to Buffer and parsed as UTF8. --- doc/api/dgram.markdown | 7 ++++++- lib/dgram.js | 5 ++++- test/simple/test-dgram-oob-buffer.js | 9 +++++++++ test/simple/test-dgram-regress-4496.js | 4 ++-- test/simple/test-dgram-udp4.js | 5 ++--- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/doc/api/dgram.markdown b/doc/api/dgram.markdown index 922cdfc5460..e4ce0fb9c2c 100644 --- a/doc/api/dgram.markdown +++ b/doc/api/dgram.markdown @@ -74,7 +74,7 @@ Emitted when an error occurs. ### socket.send(buf, offset, length, port, address, [callback]) -* `buf` Buffer object. Message to be sent +* `buf` Buffer object or string. Message to be sent * `offset` Integer. Offset in the buffer where the message starts. * `length` Integer. Number of bytes in the message. * `port` Integer. destination port @@ -93,6 +93,11 @@ If the socket has not been previously bound with a call to `bind`, it's assigned a random port number and bound to the "all interfaces" address (0.0.0.0 for `udp4` sockets, ::0 for `udp6` sockets). +With consideration for multi-byte characters, `offset` and `length` will +be calculated with respect to +[byte length](buffer.html#buffer_class_method_buffer_bytelength_string_encoding) +and not the character position. + Example of sending a UDP packet to a random port on `localhost`; var dgram = require('dgram'); diff --git a/lib/dgram.js b/lib/dgram.js index 8f199825251..f31fd7158cf 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -235,8 +235,11 @@ Socket.prototype.send = function(buffer, callback) { var self = this; + if (util.isString(buffer)) + buffer = new Buffer(buffer); + if (!util.isBuffer(buffer)) - throw new TypeError('First argument must be a buffer object.'); + throw new TypeError('First argument must be a buffer or string.'); offset = offset | 0; if (offset < 0) diff --git a/test/simple/test-dgram-oob-buffer.js b/test/simple/test-dgram-oob-buffer.js index a3967fb614a..b273057da22 100644 --- a/test/simple/test-dgram-oob-buffer.js +++ b/test/simple/test-dgram-oob-buffer.js @@ -48,5 +48,14 @@ assert.throws(function() { assert.throws(function() { socket.send(buf, 4, 4, common.PORT, '127.0.0.1', assert.fail); }); +assert.throws(function() { + socket.send('abc', 4, 1, common.PORT, '127.0.0.1', assert.fail); +}); +assert.throws(function() { + socket.send('abc', 0, 4, common.PORT, '127.0.0.1', assert.fail); +}); +assert.throws(function() { + socket.send('abc', -1, 2, common.PORT, '127.0.0.1', assert.fail); +}); socket.close(); // FIXME should not be necessary diff --git a/test/simple/test-dgram-regress-4496.js b/test/simple/test-dgram-regress-4496.js index 33b12847f43..e45824a107f 100644 --- a/test/simple/test-dgram-regress-4496.js +++ b/test/simple/test-dgram-regress-4496.js @@ -27,6 +27,6 @@ var dgram = require('dgram'); // Should throw but not crash. var socket = dgram.createSocket('udp4'); -assert.throws(function() { socket.send('BAM', 0, 1, 1, 'host') }, TypeError); -assert.throws(function() { socket.sendto('BAM', 0, 1, 1, 'host') }, TypeError); +assert.throws(function() { socket.send(true, 0, 1, 1, 'host') }, TypeError); +assert.throws(function() { socket.sendto(5, 0, 1, 1, 'host') }, TypeError); socket.close(); diff --git a/test/simple/test-dgram-udp4.js b/test/simple/test-dgram-udp4.js index 9b1fdfd42cc..838c48f2bb9 100644 --- a/test/simple/test-dgram-udp4.js +++ b/test/simple/test-dgram-udp4.js @@ -25,11 +25,10 @@ var common = require('../common'); var assert = require('assert'); -var Buffer = require('buffer').Buffer, - fs = require('fs'), +var fs = require('fs'), dgram = require('dgram'), server, client, server_port = 20989, - message_to_send = new Buffer('A message to send'), + message_to_send = 'A message to send', timer; server = dgram.createSocket('udp4');