Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dgram: support Uint8Array input to send() #11985

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions doc/api/dgram.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,23 @@ chained.
<!-- YAML
added: v0.1.99
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/11985
description: The `msg` parameter can be an Uint8Array now.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/10473
description: The `address` parameter is always optional now.
- version: v6.0.0
pr-url: https://github.com/nodejs/node/pull/5929
description: On success, `callback` will now be called with an `error`
argument of `null` rather than `0`.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/10473
description: The `address` parameter is always optional now.
- version: v5.7.0
pr-url: https://github.com/nodejs/node/pull/4374
description: The `msg` parameter can be an array now. Also, the `offset`
and `length` parameters are optional now.
-->

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

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

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

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

Expand Down
16 changes: 10 additions & 6 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const UV_UDP_REUSEADDR = process.binding('constants').os.UV_UDP_REUSEADDR;

const UDP = process.binding('udp_wrap').UDP;
const SendWrap = process.binding('udp_wrap').SendWrap;
const { isUint8Array } = process.binding('util');

const BIND_STATE_UNBOUND = 0;
const BIND_STATE_BINDING = 1;
Expand Down Expand Up @@ -266,10 +267,12 @@ Socket.prototype.sendto = function(buffer,


function sliceBuffer(buffer, offset, length) {
if (typeof buffer === 'string')
if (typeof buffer === 'string') {
buffer = Buffer.from(buffer);
else if (!(buffer instanceof Buffer))
throw new TypeError('First argument must be a buffer or string');
} else if (!isUint8Array(buffer)) {
throw new TypeError('First argument must be a Buffer, ' +
'Uint8Array or string');
}

offset = offset >>> 0;
length = length >>> 0;
Expand All @@ -285,7 +288,7 @@ function fixBufferList(list) {
var buf = list[i];
if (typeof buf === 'string')
newlist[i] = Buffer.from(buf);
else if (!(buf instanceof Buffer))
else if (!isUint8Array(buf))
return null;
else
newlist[i] = buf;
Expand Down Expand Up @@ -359,8 +362,9 @@ Socket.prototype.send = function(buffer,
if (!Array.isArray(buffer)) {
if (typeof buffer === 'string') {
list = [ Buffer.from(buffer) ];
} else if (!(buffer instanceof Buffer)) {
throw new TypeError('First argument must be a buffer or a string');
} else if (!isUint8Array(buffer)) {
throw new TypeError('First argument must be a Buffer, ' +
'Uint8Array or string');
} else {
list = [ buffer ];
}
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-dgram-send-default-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const received = [];
client.on('listening', common.mustCall(() => {
const port = client.address().port;
client.send(toSend[0], 0, toSend[0].length, port);
client.send(toSend[1], port);
client.send(new Uint8Array([...toSend[1]]), port);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing test cases for socket.send(u8a, offset, length, port) and socket.send(buffer, port).

Also, is new Uint8Array([...toSend[1]]) here the same as new Uint8Array(toSend[1])?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joyeecheung Right, thanks for pointing that out. I’ve updated the file so all the tests are run twice now, once with the original input types and once with Uint8Arrays.

client.send([toSend[2]], port);
client.send(toSend[3], 0, toSend[3].length, port);
}));
Expand Down