Skip to content

Commit 50789a5

Browse files
deokjinkimdanielleadams
authored andcommitted
lib: refactor to use validateBuffer
Use validateBuffer to remove duplicate implementation. PR-URL: #46489 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Ricky Zhou <0x19951125@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 2edd0fd commit 50789a5

3 files changed

Lines changed: 11 additions & 24 deletions

File tree

‎lib/internal/http2/core.js‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,14 @@ const {
121121
}=require('internal/errors');
122122
const{
123123
isUint32,
124+
validateAbortSignal,
125+
validateBuffer,
124126
validateFunction,
125127
validateInt32,
126128
validateInteger,
127129
validateNumber,
128130
validateString,
129131
validateUint32,
130-
validateAbortSignal,
131132
}=require('internal/validators');
132133
constfsPromisesInternal=require('internal/fs/promises');
133134
const{ utcDate }=require('internal/http');
@@ -1377,10 +1378,8 @@ class Http2Session extends EventEmitter {
13771378
callback=payload;
13781379
payload=undefined;
13791380
}
1380-
if(payload&&!isArrayBufferView(payload)){
1381-
thrownewERR_INVALID_ARG_TYPE('payload',
1382-
['Buffer','TypedArray','DataView'],
1383-
payload);
1381+
if(payload){
1382+
validateBuffer(payload,'payload');
13841383
}
13851384
if(payload&&payload.length!==8){
13861385
thrownewERR_HTTP2_PING_LENGTH();
@@ -1506,10 +1505,8 @@ class Http2Session extends EventEmitter {
15061505
if(this.destroyed)
15071506
thrownewERR_HTTP2_INVALID_SESSION();
15081507

1509-
if(opaqueData!==undefined&&!isArrayBufferView(opaqueData)){
1510-
thrownewERR_INVALID_ARG_TYPE('opaqueData',
1511-
['Buffer','TypedArray','DataView'],
1512-
opaqueData);
1508+
if(opaqueData!==undefined){
1509+
validateBuffer(opaqueData,'opaqueData');
15131510
}
15141511
validateNumber(code,'code');
15151512
validateNumber(lastStreamID,'lastStreamID');

‎lib/internal/tls/secure-context.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {
2626
}=require('internal/util/types');
2727

2828
const{
29+
validateBuffer,
2930
validateInt32,
3031
validateObject,
3132
validateString,
@@ -292,12 +293,7 @@ function configSecureContext(context, options = kEmptyObject, name = 'options')
292293
}
293294

294295
if(ticketKeys!==undefined&&ticketKeys!==null){
295-
if(!isArrayBufferView(ticketKeys)){
296-
thrownewERR_INVALID_ARG_TYPE(
297-
`${name}.ticketKeys`,
298-
['Buffer','TypedArray','DataView'],
299-
ticketKeys);
300-
}
296+
validateBuffer(ticketKeys,`${name}.ticketKeys`);
301297
if(ticketKeys.byteLength!==48){
302298
thrownewERR_INVALID_ARG_VALUE(
303299
`${name}.ticketKeys`,

‎lib/vm.js‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ const {
3939
ERR_CONTEXT_NOT_INITIALIZED,
4040
ERR_INVALID_ARG_TYPE,
4141
}=require('internal/errors').codes;
42-
const{
43-
isArrayBufferView,
44-
}=require('internal/util/types');
4542
const{
4643
validateBoolean,
44+
validateBuffer,
4745
validateFunction,
4846
validateInt32,
4947
validateObject,
@@ -84,12 +82,8 @@ class Script extends ContextifyScript {
8482
validateString(filename,'options.filename');
8583
validateInt32(lineOffset,'options.lineOffset');
8684
validateInt32(columnOffset,'options.columnOffset');
87-
if(cachedData!==undefined&&!isArrayBufferView(cachedData)){
88-
thrownewERR_INVALID_ARG_TYPE(
89-
'options.cachedData',
90-
['Buffer','TypedArray','DataView'],
91-
cachedData,
92-
);
85+
if(cachedData!==undefined){
86+
validateBuffer(cachedData,'options.cachedData');
9387
}
9488
validateBoolean(produceCachedData,'options.produceCachedData');
9589

0 commit comments

Comments
 (0)