Commit a7e1f8e8eaa for nodejs
commit a7e1f8e8eaa5a5f3b008f27e8d6697edb2473d93
Author: Livia Medeiros <livia@cirno.name>
Date: Mon Nov 30 07:36:45 2026 +0800
fs: add static `Stats.isStats()` method
Signed-off-by: LiviaMedeiros <livia@cirno.name>
PR-URL: https://github.com/nodejs/node/pull/67716
Reviewed-By: James M Snell <jasnell@gmail.com>
diff --git a/doc/api/fs.md b/doc/api/fs.md
index 186ab76c098..266242fcdc4 100644
--- a/doc/api/fs.md
+++ b/doc/api/fs.md
@@ -7994,6 +7994,9 @@ i.e. before the `'ready'` event is emitted.
<!-- YAML
added: v0.1.21
changes:
+ - version: REPLACEME
+ pr-url: https://github.com/nodejs/node/pull/67716
+ description: Added `Stats.isStats` static method.
- version: v26.2.0
pr-url: https://github.com/nodejs/node/pull/60789
description: Added `Temporal.Instant` support.
@@ -8084,6 +8087,44 @@ BigIntStats {
}
```
+### Static method: `Stats.isStats(value)`
+
+<!-- YAML
+added: REPLACEME
+-->
+
+* `value` {any}
+* Returns: {boolean}
+
+Returns `true` if `value` is a `fs.Stats` object, including objects returned
+when `bigint` option was set to `true`. Returns `false` otherwise.
+
+Prefer this method over `value instanceof fs.Stats`.
+
+```mjs
+import { statSync, Stats } from 'node:fs';
+
+const stats = statSync('/etc/hosts');
+const statsBigInt = statSync('/etc/hosts', { bigint: true });
+
+Stats.isStats(stats); // true
+Stats.isStats(statsBigInt); // true
+stats instanceof Stats; // true
+statsBigInt instanceof Stats; // false
+```
+
+```cjs
+const { statSync, Stats } = require('node:fs');
+
+const stats = statSync('/etc/hosts');
+const statsBigInt = statSync('/etc/hosts', { bigint: true });
+
+Stats.isStats(stats); // true
+Stats.isStats(statsBigInt); // true
+stats instanceof Stats; // true
+statsBigInt instanceof Stats; // false
+```
+
#### `stats.isBlockDevice()`
<!-- YAML
diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js
index 2d7c9139970..33b8ac86fdc 100644
--- a/lib/internal/fs/utils.js
+++ b/lib/internal/fs/utils.js
@@ -9,6 +9,7 @@ const {
DatePrototypeGetTime,
ErrorCaptureStackTrace,
FunctionPrototypeCall,
+ FunctionPrototypeSymbolHasInstance,
MathFloor,
MathMin,
MathRound,
@@ -765,6 +766,14 @@ Stats.prototype._checkModeProperty = function(property) {
return (this.mode & S_IFMT) === property;
};
+/**
+ * @param {unknown} value
+ * @returns {boolean}
+ */
+Stats.isStats = function(value) {
+ return FunctionPrototypeSymbolHasInstance(StatsBase, value);
+};
+
/**
* @param {Float64Array | BigInt64Array} stats
* @param {number} offset
diff --git a/test/parallel/test-fs-stats-isstats.mjs b/test/parallel/test-fs-stats-isstats.mjs
new file mode 100644
index 00000000000..ccf9479757c
--- /dev/null
+++ b/test/parallel/test-fs-stats-isstats.mjs
@@ -0,0 +1,61 @@
+import '../common/index.mjs';
+import assert from 'node:assert/strict';
+import { Stats, statSync } from 'node:fs';
+
+const testFile = new URL(import.meta.url);
+const stats = statSync(testFile);
+const statsBigInt = statSync(testFile, { bigint: true });
+
+// `isStats` shouldn't depend on receiver
+const { isStats } = Stats;
+
+assert.strictEqual(Stats.isStats(stats), true);
+assert.strictEqual(Stats.isStats(statsBigInt), true);
+assert.strictEqual(isStats(stats), true);
+assert.strictEqual(isStats(statsBigInt), true);
+
+for (const value of [
+ undefined,
+ null,
+ false,
+ 0,
+ 0n,
+ '',
+ Symbol(),
+ {},
+ { __proto__: null },
+ [],
+ Promise.resolve(),
+ class Stats {},
+ {
+ dev: stats.dev,
+ mode: stats.mode,
+ nlink: stats.nlink,
+ uid: stats.uid,
+ gid: stats.gid,
+ rdev: stats.rdev,
+ blksize: stats.blksize,
+ ino: stats.ino,
+ size: stats.size,
+ blocks: stats.blocks,
+ atimeMs: stats.atimeMs,
+ mtimeMs: stats.mtimeMs,
+ ctimeMs: stats.ctimeMs,
+ birthtimeMs: stats.birthtimeMs,
+ atime: stats.atime,
+ mtime: stats.mtime,
+ ctime: stats.ctime,
+ birthtime: stats.birthtime,
+ _checkModeProperty: stats._checkModeProperty,
+ isDirectory: stats.isDirectory,
+ isFile: stats.isFile,
+ isBlockDevice: stats.isBlockDevice,
+ isCharacterDevice: stats.isCharacterDevice,
+ isSymbolicLink: stats.isSymbolicLink,
+ isFIFO: stats.isFIFO,
+ isSocket: stats.isSocket,
+ },
+]) {
+ assert.strictEqual(Stats.isStats(value), false);
+ assert.strictEqual(isStats(value), false);
+}