Skip to content

Commit b702da4

Browse files
committed
fs: use assert in fsCall argument checking
In the user perspective, it's not an arguemnt type error. Replace it with an `assert` expression.
1 parent aed17e9 commit b702da4

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
@@ -80,6 +80,7 @@ const { promisify } = require('internal/util');
8080
const { EventEmitterMixin } = require('internal/event_target');
8181
const { watch } = require('internal/fs/watchers');
8282
const { isIterable } = require('internal/streams/utils');
83+
const assert = require('internal/assert');
8384

8485
const kHandle = Symbol('kHandle');
8586
const kFd = Symbol('kFd');
@@ -266,9 +267,8 @@ async function handleFdClose(fileOpPromise, closeFunc) {
266267
}
267268

268269
async function fsCall(fn, handle, ...args) {
269-
if (handle[kRefs] === undefined) {
270-
throw new ERR_INVALID_ARG_TYPE('filehandle', 'FileHandle', handle);
271-
}
270+
assert(handle instanceof FileHandle,
271+
'handle must be an instance of FileHandle');
272272

273273
if (handle.fd === -1) {
274274
// 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)