Skip to content

Commit f151bde

Browse files
SirR4Tcodebytere
authored andcommitted
dgram: allow typed arrays in .send()
PR-URL: #22413 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]>
1 parent 162092e commit f151bde

5 files changed

+115
-35
lines changed

doc/api/dgram.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ socket is not connected.
384384
<!-- YAML
385385
added: v0.1.99
386386
changes:
387+
- version: REPLACEME
388+
pr-url: https://github.com/nodejs/node/pull/22413
389+
description: The `msg` parameter can now be any `TypedArray` or `DataView`.
387390
- version: v8.0.0
388391
pr-url: https://github.com/nodejs/node/pull/11985
389392
description: The `msg` parameter can be an `Uint8Array` now.
@@ -403,7 +406,7 @@ changes:
403406
description: Added support for sending data on connected sockets.
404407
-->
405408

406-
* `msg` {Buffer|Uint8Array|string|Array} Message to be sent.
409+
* `msg` {Buffer|TypedArray|DataView|string|Array} Message to be sent.
407410
* `offset` {integer} Offset in the buffer where the message starts.
408411
* `length` {integer} Number of bytes in the message.
409412
* `port` {integer} Destination port.
@@ -416,8 +419,8 @@ specified. Connected sockets, on the other hand, will use their associated
416419
remote endpoint, so the `port` and `address` arguments must not be set.
417420

418421
The `msg` argument contains the message to be sent.
419-
Depending on its type, different behavior can apply. If `msg` is a `Buffer`
420-
or `Uint8Array`,
422+
Depending on its type, different behavior can apply. If `msg` is a `Buffer`,
423+
any `TypedArray` or a `DataView`,
421424
the `offset` and `length` specify the offset within the `Buffer` where the
422425
message begins and the number of bytes in the message, respectively.
423426
If `msg` is a `String`, then it is automatically converted to a `Buffer`
@@ -446,7 +449,8 @@ passed as the first argument to the `callback`. If a `callback` is not given,
446449
the error is emitted as an `'error'` event on the `socket` object.
447450

448451
Offset and length are optional but both *must* be set if either are used.
449-
They are supported only when the first argument is a `Buffer` or `Uint8Array`.
452+
They are supported only when the first argument is a `Buffer`, a `TypedArray`,
453+
or a `DataView`.
450454

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

lib/dgram.js

+20-9
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const {
5454
} = require('internal/validators');
5555
const { Buffer } = require('buffer');
5656
const { deprecate } = require('internal/util');
57-
const { isUint8Array } = require('internal/util/types');
57+
const { isArrayBufferView } = require('internal/util/types');
5858
const EventEmitter = require('events');
5959
const {
6060
defaultTriggerAsyncIdScope,
@@ -455,15 +455,19 @@ Socket.prototype.sendto = function(buffer,
455455
function sliceBuffer(buffer, offset, length) {
456456
if (typeof buffer === 'string') {
457457
buffer = Buffer.from(buffer);
458-
} else if (!isUint8Array(buffer)) {
458+
} else if (!isArrayBufferView(buffer)) {
459459
throw new ERR_INVALID_ARG_TYPE('buffer',
460-
['Buffer', 'Uint8Array', 'string'], buffer);
460+
['Buffer',
461+
'TypedArray',
462+
'DataView',
463+
'string'],
464+
buffer);
461465
}
462466

463467
offset = offset >>> 0;
464468
length = length >>> 0;
465469

466-
return buffer.slice(offset, offset + length);
470+
return Buffer.from(buffer.buffer, buffer.byteOffset + offset, length);
467471
}
468472

469473

@@ -474,10 +478,10 @@ function fixBufferList(list) {
474478
const buf = list[i];
475479
if (typeof buf === 'string')
476480
newlist[i] = Buffer.from(buf);
477-
else if (!isUint8Array(buf))
481+
else if (!isArrayBufferView(buf))
478482
return null;
479483
else
480-
newlist[i] = buf;
484+
newlist[i] = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
481485
}
482486

483487
return newlist;
@@ -581,16 +585,23 @@ Socket.prototype.send = function(buffer,
581585
if (!ArrayIsArray(buffer)) {
582586
if (typeof buffer === 'string') {
583587
list = [ Buffer.from(buffer) ];
584-
} else if (!isUint8Array(buffer)) {
588+
} else if (!isArrayBufferView(buffer)) {
585589
throw new ERR_INVALID_ARG_TYPE('buffer',
586-
['Buffer', 'Uint8Array', 'string'],
590+
['Buffer',
591+
'TypedArray',
592+
'DataView',
593+
'string'],
587594
buffer);
588595
} else {
589596
list = [ buffer ];
590597
}
591598
} else if (!(list = fixBufferList(buffer))) {
592599
throw new ERR_INVALID_ARG_TYPE('buffer list arguments',
593-
['Buffer', 'string'], buffer);
600+
['Buffer',
601+
'TypedArray',
602+
'DataView',
603+
'string'],
604+
buffer);
594605
}
595606

596607
if (!connected)

test/parallel/test-dgram-send-bad-arguments.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function checkArgs(connected) {
3636
code: 'ERR_INVALID_ARG_TYPE',
3737
name: 'TypeError',
3838
message: 'The "buffer" argument must be of type string or an instance ' +
39-
'of Buffer or Uint8Array. Received undefined'
39+
'of Buffer, TypedArray, or DataView. Received undefined'
4040
}
4141
);
4242

@@ -90,7 +90,7 @@ function checkArgs(connected) {
9090
code: 'ERR_INVALID_ARG_TYPE',
9191
name: 'TypeError',
9292
message: 'The "buffer" argument must be of type string or an instance ' +
93-
'of Buffer or Uint8Array. Received type number (23)'
93+
'of Buffer, TypedArray, or DataView. Received type number (23)'
9494
}
9595
);
9696

@@ -101,7 +101,8 @@ function checkArgs(connected) {
101101
code: 'ERR_INVALID_ARG_TYPE',
102102
name: 'TypeError',
103103
message: 'The "buffer list arguments" argument must be of type string ' +
104-
'or an instance of Buffer. Received an instance of Array'
104+
'or an instance of Buffer, TypedArray, or DataView. ' +
105+
'Received an instance of Array'
105106
}
106107
);
107108
}

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

+40-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ const toSend = [Buffer.alloc(256, 'x'),
1212
'hello'];
1313

1414
const received = [];
15+
let totalBytesSent = 0;
16+
let totalBytesReceived = 0;
17+
const arrayBufferViewsCount = common.getArrayBufferViews(
18+
Buffer.from('')
19+
).length;
1520

1621
client.on('listening', common.mustCall(() => {
1722
const port = client.address().port;
@@ -21,24 +26,47 @@ client.on('listening', common.mustCall(() => {
2126
client.send([toSend[2]], port);
2227
client.send(toSend[3], 0, toSend[3].length, port);
2328

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);
29+
totalBytesSent += toSend.map((buf) => buf.length)
30+
.reduce((a, b) => a + b, 0);
31+
32+
for (const msgBuf of common.getArrayBufferViews(toSend[0])) {
33+
client.send(msgBuf, 0, msgBuf.byteLength, port);
34+
totalBytesSent += msgBuf.byteLength;
35+
}
36+
for (const msgBuf of common.getArrayBufferViews(toSend[1])) {
37+
client.send(msgBuf, port);
38+
totalBytesSent += msgBuf.byteLength;
39+
}
40+
for (const msgBuf of common.getArrayBufferViews(toSend[2])) {
41+
client.send([msgBuf], port);
42+
totalBytesSent += msgBuf.byteLength;
43+
}
2944
}));
3045

3146
client.on('message', common.mustCall((buf, info) => {
3247
received.push(buf.toString());
48+
totalBytesReceived += info.size;
3349

34-
if (received.length === toSend.length * 2) {
35-
// The replies may arrive out of order -> sort them before checking.
36-
received.sort();
37-
38-
const expected = toSend.concat(toSend).map(String).sort();
39-
assert.deepStrictEqual(received, expected);
50+
if (totalBytesReceived === totalBytesSent) {
4051
client.close();
4152
}
42-
}, toSend.length * 2));
53+
// For every buffer in `toSend`, we send the raw Buffer,
54+
// as well as every TypedArray in getArrayBufferViews()
55+
}, toSend.length + (toSend.length - 1) * arrayBufferViewsCount));
56+
57+
client.on('close', common.mustCall((buf, info) => {
58+
// The replies may arrive out of order -> sort them before checking.
59+
received.sort();
60+
61+
const repeated = [...toSend];
62+
for (let i = 0; i < arrayBufferViewsCount; i++) {
63+
repeated.push(...toSend.slice(0, 3));
64+
}
65+
66+
assert.strictEqual(totalBytesSent, totalBytesReceived);
67+
68+
const expected = repeated.map(String).sort();
69+
assert.deepStrictEqual(received, expected);
70+
}));
4371

4472
client.bind(0);

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

+43-7
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,62 @@ const toSend = [Buffer.alloc(256, 'x'),
1515
'hello'];
1616

1717
const received = [];
18+
let totalBytesSent = 0;
19+
let totalBytesReceived = 0;
20+
const arrayBufferViewLength = common.getArrayBufferViews(
21+
Buffer.from('')
22+
).length;
1823

1924
client.on('listening', common.mustCall(() => {
2025
const port = client.address().port;
26+
2127
client.send(toSend[0], 0, toSend[0].length, port);
2228
client.send(toSend[1], port);
2329
client.send([toSend[2]], port);
2430
client.send(toSend[3], 0, toSend[3].length, port);
31+
32+
totalBytesSent += toSend.map((buf) => buf.length)
33+
.reduce((a, b) => a + b, 0);
34+
35+
for (const msgBuf of common.getArrayBufferViews(toSend[0])) {
36+
client.send(msgBuf, 0, msgBuf.byteLength, port);
37+
totalBytesSent += msgBuf.byteLength;
38+
}
39+
for (const msgBuf of common.getArrayBufferViews(toSend[1])) {
40+
client.send(msgBuf, port);
41+
totalBytesSent += msgBuf.byteLength;
42+
}
43+
for (const msgBuf of common.getArrayBufferViews(toSend[2])) {
44+
client.send([msgBuf], port);
45+
totalBytesSent += msgBuf.byteLength;
46+
}
2547
}));
2648

2749
client.on('message', common.mustCall((buf, info) => {
2850
received.push(buf.toString());
51+
totalBytesReceived += info.size;
2952

30-
if (received.length === toSend.length) {
31-
// The replies may arrive out of order -> sort them before checking.
32-
received.sort();
33-
34-
const expected = toSend.map(String).sort();
35-
assert.deepStrictEqual(received, expected);
53+
if (totalBytesReceived === totalBytesSent) {
3654
client.close();
3755
}
38-
}, toSend.length));
56+
// For every buffer in `toSend`, we send the raw Buffer,
57+
// as well as every TypedArray in getArrayBufferViews()
58+
}, toSend.length + (toSend.length - 1) * arrayBufferViewLength));
59+
60+
client.on('close', common.mustCall((buf, info) => {
61+
// The replies may arrive out of order -> sort them before checking.
62+
received.sort();
63+
64+
const repeated = [...toSend];
65+
for (let i = 0; i < arrayBufferViewLength; i++) {
66+
// We get arrayBufferViews only for toSend[0..2].
67+
repeated.push(...toSend.slice(0, 3));
68+
}
69+
70+
assert.strictEqual(totalBytesSent, totalBytesReceived);
71+
72+
const expected = repeated.map(String).sort();
73+
assert.deepStrictEqual(received, expected);
74+
}));
3975

4076
client.bind(0);

0 commit comments

Comments
 (0)