dgram: support Uint8Array input to send() by addaleax · Pull Request #11985 · nodejs/node · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
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
13 changes: 10 additions & 3 deletions test/parallel/test-dgram-send-default-host.js