Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -4051,6 +4051,9 @@ Please use the [`crypto.createHash()`][] method to create Hash instances.

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/67491
description: End-of-Life.
- version: v22.0.0
pr-url: https://github.com/nodejs/node/pull/52067
description: Runtime deprecation.
Expand All @@ -4059,10 +4062,12 @@ changes:
description: Documentation-only deprecation.
-->

Type: Runtime
Type: End-of-Life

Calling `fs.Stats` class directly with `Stats()` or `new Stats()` is
deprecated due to being internals, not intended for public use.
Calling `fs.Stats` class directly with `Stats()` or `new Stats()` throws an
`ERR_ILLEGAL_CONSTRUCTOR` error. The constructor is internal and not intended
for public use. The class remains exported, so `instanceof fs.Stats` checks
continue to work.
Comment on lines +4069 to +4070

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This recommendation doesn't look correct, because

fs.statSync('/etc/hosts') instanceof fs.Stats === true
// however:
fs.statSync('/etc/hosts', { bigint: true }) instanceof fs.Stats === false
// and:
fs.BigIntStats === undefined


### DEP0181: `Hmac` constructor

Expand Down
4 changes: 4 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7989,6 +7989,10 @@ i.e. before the `'ready'` event is emitted.
<!-- YAML
added: v0.1.21
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/67491
description: Calling the constructor directly now throws an
`ERR_ILLEGAL_CONSTRUCTOR` error.
- version: v26.2.0
pr-url: https://github.com/nodejs/node/pull/60789
description: Added `Temporal.Instant` support.
Expand Down
10 changes: 7 additions & 3 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const {
UVException,
codes: {
ERR_FS_EISDIR,
ERR_ILLEGAL_CONSTRUCTOR,
ERR_INCOMPATIBLE_OPTION_PAIR,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
Expand All @@ -48,7 +49,6 @@ const {
isUint8Array,
} = require('internal/util/types');
const {
deprecate,
isWindows,
kEmptyObject,
once,
Expand Down Expand Up @@ -77,6 +77,7 @@ const kPartialAtimeNs = Symbol('partialAtimeNs');
const kPartialMtimeNs = Symbol('partialMtimeNs');
const kPartialCtimeNs = Symbol('partialCtimeNs');
const kPartialBirthtimeNs = Symbol('kPartialBirthtimeNs');
const kPrivateConstructor = Symbol('kPrivateConstructor');
const assert = require('internal/assert');

const {
Expand Down Expand Up @@ -737,9 +738,11 @@ BigIntStats.prototype._checkModeProperty = function(property) {
return (this.mode & BigInt(S_IFMT)) === BigInt(property);
};

function Stats(dev, mode, nlink, uid, gid, rdev, blksize,
function Stats(privateSymbol, dev, mode, nlink, uid, gid, rdev, blksize,
ino, size, blocks,
atimeS, atimeNs, mtimeS, mtimeNs, ctimeS, ctimeNs, birthtimeS, birthtimeNs) {
if (privateSymbol !== kPrivateConstructor)
throw new ERR_ILLEGAL_CONSTRUCTOR();
FunctionPrototypeCall(StatsBase, this, dev, mode, nlink, uid, gid, rdev,
blksize, ino, size, blocks);
this.atimeMs = msFromTimeSpec(atimeS, atimeNs);
Expand Down Expand Up @@ -784,6 +787,7 @@ function getStatsFromBinding(stats, offset = 0) {
);
}
return new Stats(
kPrivateConstructor,
stats[0 + offset], stats[1 + offset], stats[2 + offset],
stats[3 + offset], stats[4 + offset], stats[5 + offset],
stats[6 + offset], stats[7 + offset], stats[8 + offset],
Expand Down Expand Up @@ -1233,7 +1237,7 @@ module.exports = {
getStatsFromBinding,
stringToFlags,
stringToSymlinkType,
Stats: deprecate(Stats, 'fs.Stats constructor is deprecated.', 'DEP0180'),
Stats,
toUnixTimestamp,
validateBufferArray,
validateCpOptions,
Expand Down
34 changes: 12 additions & 22 deletions test/parallel/test-fs-stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,18 @@ fs.open(__filename, 'r', undefined, common.mustCall((err, fd) => {
fs.lstat(__filename, undefined, common.mustCall());

{
fs.Stats(
0, // dev
0, // mode
0, // nlink
0, // uid
0, // gid
0, // rdev
0, // blksize
0, // ino
0, // size
0, // blocks
Date.UTC(1970, 0, 1, 0, 0, 0), // atime
Date.UTC(1970, 0, 1, 0, 0, 0), // mtime
Date.UTC(1970, 0, 1, 0, 0, 0), // ctime
Date.UTC(1970, 0, 1, 0, 0, 0) // birthtime
);
common.expectWarning({
DeprecationWarning: [
['fs.Stats constructor is deprecated.',
'DEP0180'],
]
});
// The fs.Stats constructor is not intended for public use.
const illegalConstructor = {
code: 'ERR_ILLEGAL_CONSTRUCTOR',
name: 'TypeError',
};
assert.throws(() => fs.Stats(), illegalConstructor);
assert.throws(() => new fs.Stats(), illegalConstructor);
assert.throws(() => new fs.Stats(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0), illegalConstructor);

// The class remains exported so that instanceof checks keep working.
assert(fs.statSync(__filename) instanceof fs.Stats);
}

{
Expand Down
24 changes: 4 additions & 20 deletions test/parallel/test-fs-watchfile.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';
// Flags: --expose-internals
const common = require('../common');

const assert = require('assert');
const fs = require('fs');
const path = require('path');

const tmpdir = require('../common/tmpdir');
const { getStatsFromBinding } = require('internal/fs/utils');

// Basic usage tests.
assert.throws(
Expand All @@ -31,26 +33,8 @@ assert.throws(() => {
}, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' });

const enoentFile = tmpdir.resolve('non-existent-file');
const expectedStatObject = new fs.Stats(
0, // dev
0, // mode
0, // nlink
0, // uid
0, // gid
0, // rdev
0, // blksize
0, // ino
0, // size
0, // blocks
0, // atimeS
0, // atimeNs
0, // mtimeS
0, // mtimeNs
0, // ctime
0, // ctimeNs
0, // birthtime
0, // birthtimeNs
);
// All fields are zero and the date fields are the UNIX epoch.
const expectedStatObject = getStatsFromBinding(new Float64Array(18));

tmpdir.refresh();

Expand Down
Loading