Skip to content

Commit

Permalink
fs: use assert in fsCall argument checking
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
pd4d10 authored and targos committed May 17, 2021
1 parent 11dd9a6 commit 47080bc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-fs-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 47080bc

Please sign in to comment.