Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 06defaa

Browse filesBrowse files
mertcanaltinaduh95
authored andcommitted
fs: add signal option to fs.stat()
PR-URL: #57775 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jason Zhang <xzha4350@gmail.com> Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 15530a7 commit 06defaa
Copy full SHA for 06defaa

3 files changed

+49-5Lines changed: 49 additions & 5 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/fs.md‎

Copy file name to clipboardExpand all lines: doc/api/fs.md
+6-4Lines changed: 6 additions & 4 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -778,15 +778,17 @@ Read from a file and write to an array of {ArrayBufferView}s
778778
<!-- YAML
779779
added: v10.0.0
780780
changes:
781+
- version: REPLACEME
782+
pr-url: https://github.com/nodejs/node/pull/57775
783+
description: Now accepts an additional `signal` property to allow aborting the operation.
781784
- version: v10.5.0
782785
pr-url: https://github.com/nodejs/node/pull/20220
783-
description: Accepts an additional `options` object to specify whether
784-
the numeric values returned should be bigint.
786+
description: Accepts an additional `options` object to specify whether the numeric values returned should be bigint.
785787
-->
786788
787789
* `options` {Object}
788-
* `bigint` {boolean} Whether the numeric values in the returned
789-
{fs.Stats} object should be `bigint`. **Default:** `false`.
790+
* `bigint` {boolean} Whether the numeric values in the returned {fs.Stats} object should be `bigint`. **Default:** `false`.
791+
* `signal` {AbortSignal} An AbortSignal to cancel the operation. **Default:** `undefined`.
790792
* Returns: {Promise} Fulfills with an {fs.Stats} for the file.
791793
792794
#### `filehandle.sync()`
Collapse file

‎lib/fs.js‎

Copy file name to clipboardExpand all lines: lib/fs.js
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,7 @@ function lstat(path, options = { bigint: false }, callback) {
16151615
/**
16161616
* Asynchronously gets the stats of a file.
16171617
* @param {string | Buffer | URL} path
1618-
* @param {{ bigint?: boolean; }} [options]
1618+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
16191619
* @param {(
16201620
* err?: Error,
16211621
* stats?: Stats
@@ -1626,8 +1626,16 @@ function stat(path, options = { bigint: false, throwIfNoEntry: true }, callback)
16261626
if (typeof options === 'function') {
16271627
callback = options;
16281628
options = kEmptyObject;
1629+
} else if (options === null || typeof options !== 'object') {
1630+
options = kEmptyObject;
1631+
} else {
1632+
options = getOptions(options, { bigint: false });
16291633
}
1634+
16301635
callback = makeStatsCallback(callback);
1636+
path = getValidatedPath(path);
1637+
1638+
if (checkAborted(options.signal, callback)) return;
16311639

16321640
const req = new FSReqCallback(options.bigint);
16331641
req.oncomplete = callback;
Collapse file
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
require('../common');
4+
const test = require('node:test');
5+
const assert = require('node:assert');
6+
const fs = require('node:fs');
7+
const tmpdir = require('../common/tmpdir');
8+
9+
test('fs.stat should throw AbortError when called with an already aborted AbortSignal', async () => {
10+
// This test verifies that fs.stat immediately throws an AbortError if the provided AbortSignal
11+
// has already been canceled. This approach is used because trying to abort an fs.stat call in flight
12+
// is unreliable given that file system operations tend to complete very quickly on many platforms.
13+
tmpdir.refresh();
14+
15+
const filePath = tmpdir.resolve('temp.txt');
16+
fs.writeFileSync(filePath, 'Test');
17+
18+
// Create an already aborted AbortSignal.
19+
const signal = AbortSignal.abort();
20+
21+
const { promise, resolve, reject } = Promise.withResolvers();
22+
fs.stat(filePath, { signal }, (err, stats) => {
23+
if (err) {
24+
return reject(err);
25+
}
26+
resolve(stats);
27+
});
28+
29+
// Assert that the promise is rejected with an AbortError.
30+
await assert.rejects(promise, { name: 'AbortError' });
31+
32+
fs.unlinkSync(filePath);
33+
tmpdir.refresh();
34+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.