Skip to content

Commit a0876a7

Browse files
committed
fix(mock-udp): New Node.js API
1 parent 6bd56fb commit a0876a7

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

packages/mock-udp/src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const originalSocketSend = Socket.prototype.send;
1111
class Scope {
1212
public _done: boolean;
1313
public address?: string;
14-
public buffer?: string | readonly any[] | ArrayBufferLike;
14+
public buffer?: ArrayBuffer | SharedArrayBuffer | NodeJS.TypedArray | string | readonly any[];
1515
public length?: number;
1616
public offset?: number;
1717
public port?: number;
@@ -74,17 +74,18 @@ function overriddenSocketSend(
7474
const length = hasLengthAndOffset ? (lengthOrAddressOrCallback as number) || 0 : 0;
7575
const offset = hasLengthAndOffset ? (offsetOrPortOrCallback as number) : 0;
7676
const port = hasLengthAndOffset ? (portOrCallback as number) : (offsetOrPortOrCallback as number);
77-
const message = ArrayBuffer.isView(msg) ? msg.buffer : msg;
77+
const msgLength = msg instanceof DataView ? msg.byteLength : msg.length;
7878

79-
if (offset >= message.slice.length) {
79+
if (offset >= msgLength) {
8080
throw new Error('Offset into buffer too large.');
8181
}
8282

83-
if (offset + length > message.slice.length) {
83+
if (offset + length > msgLength) {
8484
throw new Error('Offset + length beyond buffer length.');
8585
}
8686

87-
const newBuffer = message.slice(offset, offset + length);
87+
const newBuffer =
88+
msg instanceof DataView ? msg.buffer.slice(offset, offset + length) : msg.slice(offset, offset + length);
8889
const host = `${address}:${port}`;
8990

9091
if (intercepts.hasOwnProperty(host)) {

0 commit comments

Comments
 (0)