Skip to content

Commit 47080bc

Browse files
pd4d10targos
authored andcommitted
fs: use assert in fsCall argument checking
In the user perspective, it's not an arguemnt type error. Replace it with an `assert` expression. PR-URL: #38519 Refs: https://coverage.nodejs.org/coverage-68e6673224365120/lib/internal/fs/promises.js.html#L268 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 11dd9a6 commit 47080bc

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/internal/fs/promises.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const { promisify } = require('internal/util');
7878
const { EventEmitterMixin } = require('internal/event_target');
7979
const { watch } = require('internal/fs/watchers');
8080
const { isIterable } = require('internal/streams/utils');
81+
const assert = require('internal/assert');
8182

8283
const kHandle = Symbol('kHandle');
8384
const kFd = Symbol('kFd');
@@ -251,9 +252,8 @@ class FileHandle extends EventEmitterMixin(JSTransferable) {
251252
}
252253

253254
async function fsCall(fn, handle, ...args) {
254-
if (handle[kRefs] === undefined) {
255-
throw new ERR_INVALID_ARG_TYPE('filehandle', 'FileHandle', handle);
256-
}
255+
assert(handle[kRefs] !== undefined,
256+
'handle must be an instance of FileHandle');
257257

258258
if (handle.fd === -1) {
259259
// eslint-disable-next-line no-restricted-syntax

test/parallel/test-fs-promises.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,16 @@ async function getHandle(dest) {
452452
assert.strictEqual(ret.bytesWritten, 2);
453453
await handle.close();
454454
}
455+
456+
// Test prototype methods calling with contexts other than FileHandle
457+
{
458+
const handle = await getHandle(dest);
459+
assert.rejects(() => handle.stat.call({}), {
460+
code: 'ERR_INTERNAL_ASSERTION',
461+
message: /handle must be an instance of FileHandle/
462+
});
463+
await handle.close();
464+
}
455465
}
456466

457467
doTest().then(common.mustCall());

0 commit comments

Comments
 (0)