lib: consolidate arrayBufferView validation · nodejs/node@7bddfcc (original) (raw)

5 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -69,7 +69,6 @@ const {
69 69 stringToFlags,
70 70 stringToSymlinkType,
71 71 toUnixTimestamp,
72 - validateBuffer,
73 72 validateOffsetLengthRead,
74 73 validateOffsetLengthWrite,
75 74 validatePath
@@ -81,6 +80,7 @@ const {
81 80 const {
82 81 isUint32,
83 82 parseMode,
83 + validateBuffer,
84 84 validateInteger,
85 85 validateInt32,
86 86 validateUint32
Original file line number Diff line number Diff line change
@@ -5,6 +5,9 @@ const {
5 5 certExportPublicKey,
6 6 certVerifySpkac
7 7 } = internalBinding('crypto');
8 +const {
9 + validateBuffer
10 +} = require('internal/validators');
8 11
9 12 const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
10 13 const { isArrayBufferView } = require('internal/util/types');
@@ -14,13 +17,7 @@ const {
14 17 } = require('internal/crypto/util');
15 18
16 19 function verifySpkac(spkac) {
17 -if (!isArrayBufferView(spkac)) {
18 -throw new ERR_INVALID_ARG_TYPE(
19 -'spkac',
20 -['Buffer', 'TypedArray', 'DataView'],
21 -spkac
22 -);
23 -}
20 +validateBuffer(spkac, 'spkac');
24 21 return certVerifySpkac(spkac);
25 22 }
26 23
Original file line number Diff line number Diff line change
@@ -27,13 +27,13 @@ const {
27 27 stringToFlags,
28 28 stringToSymlinkType,
29 29 toUnixTimestamp,
30 - validateBuffer,
31 30 validateOffsetLengthRead,
32 31 validateOffsetLengthWrite,
33 32 validatePath
34 33 } = require('internal/fs/utils');
35 34 const {
36 35 parseMode,
36 + validateBuffer,
37 37 validateInteger,
38 38 validateUint32
39 39 } = require('internal/validators');
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@ const {
14 14 } = require('internal/errors');
15 15 const {
16 16 isUint8Array,
17 - isArrayBufferView,
18 17 isDate
19 18 } = require('internal/util/types');
20 19 const { once } = require('internal/util');
@@ -393,14 +392,6 @@ function toUnixTimestamp(time, name = 'time') {
393 392 throw new ERR_INVALID_ARG_TYPE(name, ['Date', 'Time in seconds'], time);
394 393 }
395 394
396 -const validateBuffer = hideStackFrames((buffer) => {
397 -if (!isArrayBufferView(buffer)) {
398 -throw new ERR_INVALID_ARG_TYPE('buffer',
399 -['Buffer', 'TypedArray', 'DataView'],
400 -buffer);
401 -}
402 -});
403 -
404 395 const validateOffsetLengthRead = hideStackFrames(
405 396 (offset, length, bufferLength) => {
406 397 if (offset < 0 |
@@ -453,7 +444,6 @@ module.exports = {
453 444 stringToSymlinkType,
454 445 Stats,
455 446 toUnixTimestamp,
456 - validateBuffer,
457 447 validateOffsetLengthRead,
458 448 validateOffsetLengthWrite,
459 449 validatePath
Original file line number Diff line number Diff line change
@@ -8,6 +8,9 @@ const {
8 8 ERR_OUT_OF_RANGE
9 9 }
10 10 } = require('internal/errors');
11 +const {
12 + isArrayBufferView
13 +} = require('internal/util/types');
11 14
12 15 function isInt32(value) {
13 16 return value === (value | 0);
@@ -107,10 +110,22 @@ function validateNumber(value, name) {
107 110 throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
108 111 }
109 112
113 +// TODO(BridgeAR): We have multiple validation functions that call
114 +// `require('internal/utils').toBuf()` before validating for array buffer views.
115 +// Those should likely also be consolidated in here.
116 +const validateBuffer = hideStackFrames((buffer, name = 'buffer') => {
117 +if (!isArrayBufferView(buffer)) {
118 +throw new ERR_INVALID_ARG_TYPE(name,
119 +['Buffer', 'TypedArray', 'DataView'],
120 +buffer);
121 +}
122 +});
123 +
110 124 module.exports = {
111 125 isInt32,
112 126 isUint32,
113 127 parseMode,
128 + validateBuffer,
114 129 validateInteger,
115 130 validateInt32,
116 131 validateUint32,