diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index ac554f961c73ba..55c6f731587351 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -78,6 +78,7 @@ const { promisify } = require('internal/util'); const { EventEmitterMixin } = require('internal/event_target'); const { watch } = require('internal/fs/watchers'); const { isIterable } = require('internal/streams/utils'); +const assert = require('internal/assert'); const kHandle = Symbol('kHandle'); const kFd = Symbol('kFd'); @@ -251,9 +252,8 @@ class FileHandle extends EventEmitterMixin(JSTransferable) { } async function fsCall(fn, handle, ...args) { - if (handle[kRefs] === undefined) { - throw new ERR_INVALID_ARG_TYPE('filehandle', 'FileHandle', handle); - } + assert(handle[kRefs] !== undefined, + 'handle must be an instance of FileHandle'); if (handle.fd === -1) { // eslint-disable-next-line no-restricted-syntax diff --git a/test/parallel/test-fs-promises.js b/test/parallel/test-fs-promises.js index df7fa3e4733cba..f0140084732056 100644 --- a/test/parallel/test-fs-promises.js +++ b/test/parallel/test-fs-promises.js @@ -452,6 +452,16 @@ async function getHandle(dest) { assert.strictEqual(ret.bytesWritten, 2); await handle.close(); } + + // Test prototype methods calling with contexts other than FileHandle + { + const handle = await getHandle(dest); + assert.rejects(() => handle.stat.call({}), { + code: 'ERR_INTERNAL_ASSERTION', + message: /handle must be an instance of FileHandle/ + }); + await handle.close(); + } } doTest().then(common.mustCall());