Skip to content

Commit 988cc03

Browse files
committed
quic: update js impl to use primordials consistently
1 parent f3327ea commit 988cc03

File tree

3 files changed

+88
-69
lines changed

3 files changed

+88
-69
lines changed

lib/internal/quic/quic.js

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
DataViewPrototypeGetBuffer,
1414
DataViewPrototypeGetByteLength,
1515
DataViewPrototypeGetByteOffset,
16+
FunctionPrototypeBind,
1617
ObjectDefineProperties,
1718
ObjectKeys,
1819
PromiseWithResolvers,
@@ -597,24 +598,35 @@ function validateBody(body) {
597598
// With a SharedArrayBuffer, we always copy. We cannot transfer
598599
// and it's likely unsafe to use the underlying buffer directly.
599600
if (isSharedArrayBuffer(body)) {
600-
return new Uint8Array(body).slice();
601+
return TypedArrayPrototypeSlice(new Uint8Array(body));
601602
}
602603
if (isArrayBufferView(body)) {
603-
const size = body.byteLength;
604-
const offset = body.byteOffset;
604+
let size, offset, buffer;
605+
if (isDataView(body)) {
606+
size = DataViewPrototypeGetByteLength(body);
607+
offset = DataViewPrototypeGetByteOffset(body);
608+
buffer = DataViewPrototypeGetBuffer(body);
609+
} else {
610+
size = TypedArrayPrototypeGetByteLength(body);
611+
offset = TypedArrayPrototypeGetByteOffset(body);
612+
buffer = TypedArrayPrototypeGetBuffer(body);
613+
}
605614
// We have to be careful in this case. If the ArrayBufferView is a
606615
// subset of the underlying ArrayBuffer, transferring the entire
607616
// ArrayBuffer could be incorrect if other views are also using it.
608617
// So if offset > 0 or size != buffer.byteLength, we'll copy the
609618
// subset into a new ArrayBuffer instead of transferring.
610-
if (isSharedArrayBuffer(body.buffer) ||
611-
offset !== 0 || size !== body.buffer.byteLength) {
612-
return new Uint8Array(body, offset, size).slice();
619+
if (isSharedArrayBuffer(buffer) ||
620+
offset !== 0 ||
621+
size !== ArrayBufferPrototypeGetByteLength(buffer)) {
622+
return TypedArrayPrototypeSlice(
623+
new Uint8Array(buffer, offset, size));
613624
}
614625
// It's still possible that the ArrayBuffer is being used elsewhere,
615626
// but we really have no way of knowing. We'll just have to trust
616627
// the caller in this case.
617-
return new Uint8Array(ArrayBufferPrototypeTransfer(body.buffer), offset, size);
628+
return new Uint8Array(
629+
ArrayBufferPrototypeTransfer(buffer), offset, size);
618630
}
619631
if (isBlob(body)) return body[kBlobHandle];
620632

@@ -763,7 +775,7 @@ class QuicStream {
763775
this.#state.wantsBlock = false;
764776
} else {
765777
validateFunction(fn, 'onblocked');
766-
this.#onblocked = fn.bind(this);
778+
this.#onblocked = FunctionPrototypeBind(fn, this);
767779
this.#state.wantsBlock = true;
768780
}
769781
}
@@ -781,7 +793,7 @@ class QuicStream {
781793
this.#state.wantsReset = false;
782794
} else {
783795
validateFunction(fn, 'onreset');
784-
this.#onreset = fn.bind(this);
796+
this.#onreset = FunctionPrototypeBind(fn, this);
785797
this.#state.wantsReset = true;
786798
}
787799
}
@@ -803,7 +815,7 @@ class QuicStream {
803815
'The negotiated QUIC application protocol does not support headers');
804816
}
805817
validateFunction(fn, 'onheaders');
806-
this.#onheaders = fn.bind(this);
818+
this.#onheaders = FunctionPrototypeBind(fn, this);
807819
this.#state[kWantsHeaders] = true;
808820
}
809821
}
@@ -825,7 +837,7 @@ class QuicStream {
825837
'The negotiated QUIC application protocol does not support headers');
826838
}
827839
validateFunction(fn, 'ontrailers');
828-
this.#ontrailers = fn.bind(this);
840+
this.#ontrailers = FunctionPrototypeBind(fn, this);
829841
this.#state[kWantsTrailers] = true;
830842
}
831843
}
@@ -846,7 +858,7 @@ class QuicStream {
846858
'The negotiated QUIC application protocol does not support headers');
847859
}
848860
validateFunction(fn, 'onwanttrailers');
849-
this.#onwanttrailers = fn.bind(this);
861+
this.#onwanttrailers = FunctionPrototypeBind(fn, this);
850862
}
851863
}
852864

@@ -1339,7 +1351,7 @@ class QuicSession {
13391351
this.#onstream = undefined;
13401352
} else {
13411353
validateFunction(fn, 'onstream');
1342-
this.#onstream = fn.bind(this);
1354+
this.#onstream = FunctionPrototypeBind(fn, this);
13431355
}
13441356
}
13451357

@@ -1356,7 +1368,7 @@ class QuicSession {
13561368
this.#state.hasDatagramListener = false;
13571369
} else {
13581370
validateFunction(fn, 'ondatagram');
1359-
this.#ondatagram = fn.bind(this);
1371+
this.#ondatagram = FunctionPrototypeBind(fn, this);
13601372
this.#state.hasDatagramListener = true;
13611373
}
13621374
}
@@ -1378,7 +1390,7 @@ class QuicSession {
13781390
this.#state.hasDatagramStatusListener = false;
13791391
} else {
13801392
validateFunction(fn, 'ondatagramstatus');
1381-
this.#ondatagramstatus = fn.bind(this);
1393+
this.#ondatagramstatus = FunctionPrototypeBind(fn, this);
13821394
this.#state.hasDatagramStatusListener = true;
13831395
}
13841396
}
@@ -1396,7 +1408,7 @@ class QuicSession {
13961408
this.#state.hasPathValidationListener = false;
13971409
} else {
13981410
validateFunction(fn, 'onpathvalidation');
1399-
this.#onpathvalidation = fn.bind(this);
1411+
this.#onpathvalidation = FunctionPrototypeBind(fn, this);
14001412
this.#state.hasPathValidationListener = true;
14011413
}
14021414
}
@@ -1414,7 +1426,7 @@ class QuicSession {
14141426
this.#state.hasSessionTicketListener = false;
14151427
} else {
14161428
validateFunction(fn, 'onsessionticket');
1417-
this.#onsessionticket = fn.bind(this);
1429+
this.#onsessionticket = FunctionPrototypeBind(fn, this);
14181430
this.#state.hasSessionTicketListener = true;
14191431
}
14201432
}
@@ -1431,7 +1443,7 @@ class QuicSession {
14311443
this.#onversionnegotiation = undefined;
14321444
} else {
14331445
validateFunction(fn, 'onversionnegotiation');
1434-
this.#onversionnegotiation = fn.bind(this);
1446+
this.#onversionnegotiation = FunctionPrototypeBind(fn, this);
14351447
}
14361448
}
14371449

@@ -1447,7 +1459,7 @@ class QuicSession {
14471459
this.#onhandshake = undefined;
14481460
} else {
14491461
validateFunction(fn, 'onhandshake');
1450-
this.#onhandshake = fn.bind(this);
1462+
this.#onhandshake = FunctionPrototypeBind(fn, this);
14511463
}
14521464
}
14531465

@@ -1464,7 +1476,7 @@ class QuicSession {
14641476
this.#state.hasNewTokenListener = false;
14651477
} else {
14661478
validateFunction(fn, 'onnewtoken');
1467-
this.#onnewtoken = fn.bind(this);
1479+
this.#onnewtoken = FunctionPrototypeBind(fn, this);
14681480
this.#state.hasNewTokenListener = true;
14691481
}
14701482
}
@@ -1482,7 +1494,7 @@ class QuicSession {
14821494
this.#state.hasOriginListener = false;
14831495
} else {
14841496
validateFunction(fn, 'onorigin');
1485-
this.#onorigin = fn.bind(this);
1497+
this.#onorigin = FunctionPrototypeBind(fn, this);
14861498
this.#state.hasOriginListener = true;
14871499
}
14881500
}
@@ -2450,7 +2462,7 @@ class QuicEndpoint {
24502462
throw new ERR_INVALID_STATE('Endpoint is already listening');
24512463
}
24522464
validateObject(options, 'options');
2453-
this.#onsession = onsession.bind(this);
2465+
this.#onsession = FunctionPrototypeBind(onsession, this);
24542466

24552467
const {
24562468
onstream,

0 commit comments

Comments
 (0)