Skip to content

Commit 2dc1053

Browse files
committed
dgram: support Uint8Array input to send()
Fixes: #11954 Refs: #11961 PR-URL: #11985 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent 90403dd commit 2dc1053

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

doc/api/dgram.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -245,20 +245,23 @@ chained.
245245
<!-- YAML
246246
added: v0.1.99
247247
changes:
248+
- version: REPLACEME
249+
pr-url: https://github.com/nodejs/node/pull/11985
250+
description: The `msg` parameter can be an Uint8Array now.
251+
- version: REPLACEME
252+
pr-url: https://github.com/nodejs/node/pull/10473
253+
description: The `address` parameter is always optional now.
248254
- version: v6.0.0
249255
pr-url: https://github.com/nodejs/node/pull/5929
250256
description: On success, `callback` will now be called with an `error`
251257
argument of `null` rather than `0`.
252-
- version: REPLACEME
253-
pr-url: https://github.com/nodejs/node/pull/10473
254-
description: The `address` parameter is always optional now.
255258
- version: v5.7.0
256259
pr-url: https://github.com/nodejs/node/pull/4374
257260
description: The `msg` parameter can be an array now. Also, the `offset`
258261
and `length` parameters are optional now.
259262
-->
260263

261-
* `msg` {Buffer|string|array} Message to be sent
264+
* `msg` {Buffer|Uint8Array|string|array} Message to be sent
262265
* `offset` {number} Integer. Optional. Offset in the buffer where the message starts.
263266
* `length` {number} Integer. Optional. Number of bytes in the message.
264267
* `port` {number} Integer. Destination port.
@@ -269,7 +272,8 @@ Broadcasts a datagram on the socket. The destination `port` and `address` must
269272
be specified.
270273

271274
The `msg` argument contains the message to be sent.
272-
Depending on its type, different behavior can apply. If `msg` is a `Buffer`,
275+
Depending on its type, different behavior can apply. If `msg` is a `Buffer`
276+
or `Uint8Array`,
273277
the `offset` and `length` specify the offset within the `Buffer` where the
274278
message begins and the number of bytes in the message, respectively.
275279
If `msg` is a `String`, then it is automatically converted to a `Buffer`
@@ -299,7 +303,7 @@ the error is emitted as an `'error'` event on the `socket` object.
299303

300304
Offset and length are optional, but if you specify one you would need to
301305
specify the other. Also, they are supported only when the first
302-
argument is a `Buffer`.
306+
argument is a `Buffer` or `Uint8Array`.
303307

304308
Example of sending a UDP packet to a random port on `localhost`;
305309

lib/dgram.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const UV_UDP_REUSEADDR = process.binding('constants').os.UV_UDP_REUSEADDR;
2929

3030
const UDP = process.binding('udp_wrap').UDP;
3131
const SendWrap = process.binding('udp_wrap').SendWrap;
32+
const { isUint8Array } = process.binding('util');
3233

3334
const BIND_STATE_UNBOUND = 0;
3435
const BIND_STATE_BINDING = 1;
@@ -266,10 +267,12 @@ Socket.prototype.sendto = function(buffer,
266267

267268

268269
function sliceBuffer(buffer, offset, length) {
269-
if (typeof buffer === 'string')
270+
if (typeof buffer === 'string') {
270271
buffer = Buffer.from(buffer);
271-
else if (!(buffer instanceof Buffer))
272-
throw new TypeError('First argument must be a buffer or string');
272+
} else if (!isUint8Array(buffer)) {
273+
throw new TypeError('First argument must be a Buffer, ' +
274+
'Uint8Array or string');
275+
}
273276

274277
offset = offset >>> 0;
275278
length = length >>> 0;
@@ -285,7 +288,7 @@ function fixBufferList(list) {
285288
var buf = list[i];
286289
if (typeof buf === 'string')
287290
newlist[i] = Buffer.from(buf);
288-
else if (!(buf instanceof Buffer))
291+
else if (!isUint8Array(buf))
289292
return null;
290293
else
291294
newlist[i] = buf;
@@ -359,8 +362,9 @@ Socket.prototype.send = function(buffer,
359362
if (!Array.isArray(buffer)) {
360363
if (typeof buffer === 'string') {
361364
list = [ Buffer.from(buffer) ];
362-
} else if (!(buffer instanceof Buffer)) {
363-
throw new TypeError('First argument must be a buffer or a string');
365+
} else if (!isUint8Array(buffer)) {
366+
throw new TypeError('First argument must be a Buffer, ' +
367+
'Uint8Array or string');
364368
} else {
365369
list = [ buffer ];
366370
}

test/parallel/test-dgram-send-default-host.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,30 @@ const received = [];
1515

1616
client.on('listening', common.mustCall(() => {
1717
const port = client.address().port;
18+
1819
client.send(toSend[0], 0, toSend[0].length, port);
1920
client.send(toSend[1], port);
2021
client.send([toSend[2]], port);
2122
client.send(toSend[3], 0, toSend[3].length, port);
23+
24+
client.send(new Uint8Array(toSend[0]), 0, toSend[0].length, port);
25+
client.send(new Uint8Array(toSend[1]), port);
26+
client.send([new Uint8Array(toSend[2])], port);
27+
client.send(new Uint8Array(Buffer.from(toSend[3])),
28+
0, toSend[3].length, port);
2229
}));
2330

2431
client.on('message', common.mustCall((buf, info) => {
2532
received.push(buf.toString());
2633

27-
if (received.length === toSend.length) {
34+
if (received.length === toSend.length * 2) {
2835
// The replies may arrive out of order -> sort them before checking.
2936
received.sort();
3037

31-
const expected = toSend.map(String).sort();
38+
const expected = toSend.concat(toSend).map(String).sort();
3239
assert.deepStrictEqual(received, expected);
3340
client.close();
3441
}
35-
}, toSend.length));
42+
}, toSend.length * 2));
3643

3744
client.bind(0);

0 commit comments

Comments
 (0)