Skip to content

Commit c1278e5

Browse files
committed
lib,test: minor refactoring
This refactors a couple tests to have upper case first characters in comments and to use `input` instead of `i`. It also adds a few TODOs and rewrites a few lines to use default arguments and to prevent function recreation when unnecessary. PR-URL: #19445 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent c6b6c92 commit c1278e5

16 files changed

+94
-161
lines changed

lib/fs.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,10 @@ fs.ftruncate = function(fd, len = 0, callback) {
751751
len = 0;
752752
}
753753
validateUint32(fd, 'fd');
754+
// TODO(BridgeAR): This does not seem right.
755+
// There does not seem to be any validation before and if there is any, it
756+
// should work similar to validateUint32 or not have a upper cap at all.
757+
// This applies to all usage of `validateLen`.
754758
validateLen(len);
755759
len = Math.max(0, len);
756760
const req = new FSReqWrap();
@@ -1012,7 +1016,7 @@ fs.fchmod = function(fd, mode, callback) {
10121016
mode = modeNum(mode);
10131017
validateUint32(fd, 'fd');
10141018
validateUint32(mode, 'mode');
1015-
// values for mode < 0 are already checked via the validateUint32 function
1019+
// Values for mode < 0 are already checked via the validateUint32 function
10161020
if (mode > 0o777)
10171021
throw new ERR_OUT_OF_RANGE('mode');
10181022

@@ -1025,7 +1029,8 @@ fs.fchmodSync = function(fd, mode) {
10251029
mode = modeNum(mode);
10261030
validateUint32(fd, 'fd');
10271031
validateUint32(mode, 'mode');
1028-
if (mode < 0 || mode > 0o777)
1032+
// Values for mode < 0 are already checked via the validateUint32 function
1033+
if (mode > 0o777)
10291034
throw new ERR_OUT_OF_RANGE('mode');
10301035
const ctx = {};
10311036
binding.fchmod(fd, mode, undefined, ctx);

lib/fs/promises.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ async function fchmod(handle, mode) {
369369
mode = modeNum(mode);
370370
validateFileHandle(handle);
371371
validateUint32(mode, 'mode');
372-
if (mode < 0 || mode > 0o777)
372+
if (mode > 0o777)
373373
throw new ERR_OUT_OF_RANGE('mode');
374374
return binding.fchmod(handle.fd, mode, kUsePromises);
375375
}

lib/internal/crypto/diffiehellman.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
3939
);
4040
}
4141

42-
if (keyEncoding) {
43-
if (typeof keyEncoding !== 'string' ||
44-
(!Buffer.isEncoding(keyEncoding) && keyEncoding !== 'buffer')) {
45-
genEncoding = generator;
46-
generator = keyEncoding;
47-
keyEncoding = false;
48-
}
42+
if (keyEncoding && !Buffer.isEncoding(keyEncoding) &&
43+
keyEncoding !== 'buffer') {
44+
genEncoding = generator;
45+
generator = keyEncoding;
46+
keyEncoding = false;
4947
}
5048

5149
const encoding = getDefaultEncoding();

lib/internal/errors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
752752
inspected = inspected.slice(0, 128) + '...';
753753
}
754754
return `The argument '${name}' ${reason}. Received ${inspected}`;
755-
}, TypeError, RangeError); // Some are currently falsy implemented as "Error"
755+
}, TypeError, RangeError);
756756
E('ERR_INVALID_ARRAY_LENGTH',
757757
(name, len, actual) => {
758758
internalAssert(typeof actual === 'number', 'actual must be a number');
@@ -762,7 +762,7 @@ E('ERR_INVALID_ASYNC_ID', 'Invalid %s value: %s', RangeError);
762762
E('ERR_INVALID_BUFFER_SIZE',
763763
'Buffer size must be a multiple of %s', RangeError);
764764
E('ERR_INVALID_CALLBACK', 'Callback must be a function', TypeError);
765-
E('ERR_INVALID_CHAR', invalidChar, TypeError); //Check falsy "Error" entries.
765+
E('ERR_INVALID_CHAR', invalidChar, TypeError);
766766

767767
// This should probably be a `TypeError`.
768768
E('ERR_INVALID_CURSOR_POS',

lib/internal/fs.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,9 @@ function validateOffsetLengthWrite(offset, length, byteLength) {
372372
}
373373
}
374374

375-
function validatePath(path, propName) {
375+
function validatePath(path, propName = 'path') {
376376
let err;
377377

378-
if (propName === undefined) {
379-
propName = 'path';
380-
}
381-
382378
if (typeof path !== 'string' && !isUint8Array(path)) {
383379
err = new ERR_INVALID_ARG_TYPE(propName, ['string', 'Buffer', 'URL'], path);
384380
} else {

lib/internal/process.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ function setup_cpuUsage() {
6868
user: cpuValues[0],
6969
system: cpuValues[1]
7070
};
71-
72-
// Ensure that a previously passed in value is valid. Currently, the native
73-
// implementation always returns numbers <= Number.MAX_SAFE_INTEGER.
74-
function previousValueIsValid(num) {
75-
return Number.isFinite(num) &&
76-
num <= Number.MAX_SAFE_INTEGER &&
77-
num >= 0;
78-
}
7971
};
72+
73+
// Ensure that a previously passed in value is valid. Currently, the native
74+
// implementation always returns numbers <= Number.MAX_SAFE_INTEGER.
75+
function previousValueIsValid(num) {
76+
return Number.isFinite(num) &&
77+
num <= Number.MAX_SAFE_INTEGER &&
78+
num >= 0;
79+
}
8080
}
8181

8282
// The 3 entries filled in by the original process.hrtime contains

lib/zlib.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,16 @@ function flushCallback(level, strategy, callback) {
169169
// 4. Throws ERR_OUT_OF_RANGE for infinite numbers
170170
function checkFiniteNumber(number, name) {
171171
// Common case
172-
if (number === undefined || Number.isNaN(number)) {
172+
if (number === undefined) {
173173
return false;
174174
}
175175

176176
if (Number.isFinite(number)) {
177-
return true; // is a valid number
177+
return true; // Is a valid number
178+
}
179+
180+
if (Number.isNaN(number)) {
181+
return false;
178182
}
179183

180184
// Other non-numbers

test/parallel/test-crypto-random.js

Lines changed: 26 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -80,43 +80,18 @@ process.setMaxListeners(256);
8080
}
8181

8282
{
83-
const buf = new Uint16Array(10);
84-
const before = Buffer.from(buf.buffer).toString('hex');
85-
crypto.randomFillSync(buf);
86-
const after = Buffer.from(buf.buffer).toString('hex');
87-
assert.notStrictEqual(before, after);
88-
}
89-
90-
{
91-
const buf = new Uint32Array(10);
92-
const before = Buffer.from(buf.buffer).toString('hex');
93-
crypto.randomFillSync(buf);
94-
const after = Buffer.from(buf.buffer).toString('hex');
95-
assert.notStrictEqual(before, after);
96-
}
97-
98-
{
99-
const buf = new Float32Array(10);
100-
const before = Buffer.from(buf.buffer).toString('hex');
101-
crypto.randomFillSync(buf);
102-
const after = Buffer.from(buf.buffer).toString('hex');
103-
assert.notStrictEqual(before, after);
104-
}
105-
106-
{
107-
const buf = new Float64Array(10);
108-
const before = Buffer.from(buf.buffer).toString('hex');
109-
crypto.randomFillSync(buf);
110-
const after = Buffer.from(buf.buffer).toString('hex');
111-
assert.notStrictEqual(before, after);
112-
}
113-
114-
{
115-
const buf = new DataView(new ArrayBuffer(10));
116-
const before = Buffer.from(buf.buffer).toString('hex');
117-
crypto.randomFillSync(buf);
118-
const after = Buffer.from(buf.buffer).toString('hex');
119-
assert.notStrictEqual(before, after);
83+
[
84+
new Uint16Array(10),
85+
new Uint32Array(10),
86+
new Float32Array(10),
87+
new Float64Array(10),
88+
new DataView(new ArrayBuffer(10))
89+
].forEach((buf) => {
90+
const before = Buffer.from(buf.buffer).toString('hex');
91+
crypto.randomFillSync(buf);
92+
const after = Buffer.from(buf.buffer).toString('hex');
93+
assert.notStrictEqual(before, after);
94+
});
12095
}
12196

12297
{
@@ -140,53 +115,20 @@ process.setMaxListeners(256);
140115
}
141116

142117
{
143-
const buf = new Uint16Array(10);
144-
const before = Buffer.from(buf.buffer).toString('hex');
145-
crypto.randomFill(buf, common.mustCall((err, buf) => {
146-
assert.ifError(err);
147-
const after = Buffer.from(buf.buffer).toString('hex');
148-
assert.notStrictEqual(before, after);
149-
}));
150-
}
151-
152-
{
153-
const buf = new Uint32Array(10);
154-
const before = Buffer.from(buf.buffer).toString('hex');
155-
crypto.randomFill(buf, common.mustCall((err, buf) => {
156-
assert.ifError(err);
157-
const after = Buffer.from(buf.buffer).toString('hex');
158-
assert.notStrictEqual(before, after);
159-
}));
160-
}
161-
162-
{
163-
const buf = new Float32Array(10);
164-
const before = Buffer.from(buf.buffer).toString('hex');
165-
crypto.randomFill(buf, common.mustCall((err, buf) => {
166-
assert.ifError(err);
167-
const after = Buffer.from(buf.buffer).toString('hex');
168-
assert.notStrictEqual(before, after);
169-
}));
170-
}
171-
172-
{
173-
const buf = new Float64Array(10);
174-
const before = Buffer.from(buf.buffer).toString('hex');
175-
crypto.randomFill(buf, common.mustCall((err, buf) => {
176-
assert.ifError(err);
177-
const after = Buffer.from(buf.buffer).toString('hex');
178-
assert.notStrictEqual(before, after);
179-
}));
180-
}
181-
182-
{
183-
const buf = new DataView(new ArrayBuffer(10));
184-
const before = Buffer.from(buf.buffer).toString('hex');
185-
crypto.randomFill(buf, common.mustCall((err, buf) => {
186-
assert.ifError(err);
187-
const after = Buffer.from(buf.buffer).toString('hex');
188-
assert.notStrictEqual(before, after);
189-
}));
118+
[
119+
new Uint16Array(10),
120+
new Uint32Array(10),
121+
new Float32Array(10),
122+
new Float64Array(10),
123+
new DataView(new ArrayBuffer(10))
124+
].forEach((buf) => {
125+
const before = Buffer.from(buf.buffer).toString('hex');
126+
crypto.randomFill(buf, common.mustCall((err, buf) => {
127+
assert.ifError(err);
128+
const after = Buffer.from(buf.buffer).toString('hex');
129+
assert.notStrictEqual(before, after);
130+
}));
131+
});
190132
}
191133

192134
{

test/parallel/test-fs-access.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,16 @@ assert.strictEqual(typeof fs.R_OK, 'number');
6262
assert.strictEqual(typeof fs.W_OK, 'number');
6363
assert.strictEqual(typeof fs.X_OK, 'number');
6464

65-
fs.access(__filename, common.mustCall((err) => {
66-
assert.ifError(err);
67-
}));
68-
69-
fs.access(__filename, fs.R_OK, common.mustCall((err) => {
70-
assert.ifError(err);
71-
}));
65+
fs.access(__filename, common.mustCall(assert.ifError));
66+
fs.access(__filename, fs.R_OK, common.mustCall(assert.ifError));
67+
fs.access(readOnlyFile, fs.F_OK | fs.R_OK, common.mustCall(assert.ifError));
7268

7369
fs.access(doesNotExist, common.mustCall((err) => {
7470
assert.notStrictEqual(err, null, 'error should exist');
7571
assert.strictEqual(err.code, 'ENOENT');
7672
assert.strictEqual(err.path, doesNotExist);
7773
}));
7874

79-
fs.access(readOnlyFile, fs.F_OK | fs.R_OK, common.mustCall((err) => {
80-
assert.ifError(err);
81-
}));
82-
8375
fs.access(readOnlyFile, fs.W_OK, common.mustCall(function(err) {
8476
assert.strictEqual(this, undefined);
8577
if (hasWriteAccessForReadonlyFile) {

test/parallel/test-fs-truncate.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ let stat;
3636
const msg = 'Using fs.truncate with a file descriptor is deprecated.' +
3737
' Please use fs.ftruncate with a file descriptor instead.';
3838

39-
// truncateSync
39+
// Check truncateSync
4040
fs.writeFileSync(filename, data);
4141
stat = fs.statSync(filename);
4242
assert.strictEqual(stat.size, 1024 * 16);
@@ -49,7 +49,7 @@ fs.truncateSync(filename);
4949
stat = fs.statSync(filename);
5050
assert.strictEqual(stat.size, 0);
5151

52-
// ftruncateSync
52+
// Check ftruncateSync
5353
fs.writeFileSync(filename, data);
5454
const fd = fs.openSync(filename, 'r+');
5555

@@ -64,18 +64,16 @@ fs.ftruncateSync(fd);
6464
stat = fs.statSync(filename);
6565
assert.strictEqual(stat.size, 0);
6666

67-
// truncateSync
67+
// Check truncateSync
6868
common.expectWarning('DeprecationWarning', msg);
6969
fs.truncateSync(fd);
7070

7171
fs.closeSync(fd);
7272

73-
// async tests
73+
// Async tests
7474
testTruncate(common.mustCall(function(er) {
7575
assert.ifError(er);
76-
testFtruncate(common.mustCall(function(er) {
77-
assert.ifError(er);
78-
}));
76+
testFtruncate(common.mustCall(assert.ifError));
7977
}));
8078

8179
function testTruncate(cb) {
@@ -105,7 +103,6 @@ function testTruncate(cb) {
105103
});
106104
}
107105

108-
109106
function testFtruncate(cb) {
110107
fs.writeFile(filename, data, function(er) {
111108
if (er) return cb(er);
@@ -136,7 +133,6 @@ function testFtruncate(cb) {
136133
});
137134
}
138135

139-
140136
// Make sure if the size of the file is smaller than the length then it is
141137
// filled with zeroes.
142138

0 commit comments

Comments
 (0)