From 47080bcfc8795216b01a755289da9c98cdaad383 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Mon, 3 May 2021 17:18:23 +0800 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/38519 Refs: https://coverage.nodejs.org/coverage-68e6673224365120/lib/internal/fs/promises.js.html#L268 Reviewed-By: Antoine du Hamel Reviewed-By: Darshan Sen Reviewed-By: James M Snell --- lib/internal/fs/promises.js | 6 +++--- test/parallel/test-fs-promises.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) 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());