Commit 6673eba

Browse files
mertcanaltinaduh95
authored andcommitted
fs: abort in-flight stat operations
Signed-off-by: Mert Can Altin <mertgold60@gmail.com> PR-URL: #63143 Refs: #57775 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 74234ee commit 6673eba

4 files changed

Lines changed: 200 additions & 34 deletions

File tree

β€Ždoc/api/fs.mdβ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,10 @@ link(2) documentation for more detail.
15421542
<!-- YAML
15431543
added: v10.0.0
15441544
changes:
1545+
- version: REPLACEME
1546+
pr-url: https://github.com/nodejs/node/pull/63143
1547+
description: Accepts an additional `signal` option to allow aborting the
1548+
operation.
15451549
- version: v10.5.0
15461550
pr-url: https://github.com/nodejs/node/pull/20220
15471551
description: Accepts an additional `options` object to specify whether
@@ -1552,6 +1556,8 @@ changes:
15521556
* `options` {Object}
15531557
* `bigint` {boolean} Whether the numeric values in the returned
15541558
{fs.Stats} object should be `bigint`. **Default:** `false`.
1559+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
1560+
**Default:** `undefined`.
15551561
* Returns: {Promise} Fulfills with the {fs.Stats} object for the given
15561562
symbolic link `path`.
15571563
@@ -2080,6 +2086,10 @@ Removes files and directories (modeled on the standard POSIX `rm` utility).
20802086
<!-- YAML
20812087
added: v10.0.0
20822088
changes:
2089+
- version: REPLACEME
2090+
pr-url: https://github.com/nodejs/node/pull/63143
2091+
description: Accepts an additional `signal` option to allow aborting the
2092+
operation.
20832093
- version: v25.7.0
20842094
pr-url: https://github.com/nodejs/node/pull/61178
20852095
description: Accepts a `throwIfNoEntry` option to specify whether
@@ -2097,6 +2107,8 @@ changes:
20972107
* `throwIfNoEntry` {boolean} Whether an exception will be thrown
20982108
if no file system entry exists, rather than returning `undefined`.
20992109
**Default:** `true`.
2110+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
2111+
**Default:** `undefined`.
21002112
* Returns: {Promise} Fulfills with the {fs.Stats} object for the
21012113
given `path`.
21022114
@@ -3437,6 +3449,10 @@ exception are given to the completion callback.
34373449
<!-- YAML
34383450
added: v0.1.95
34393451
changes:
3452+
- version: REPLACEME
3453+
pr-url: https://github.com/nodejs/node/pull/63143
3454+
description: Accepts an additional `signal` option to allow aborting the
3455+
operation.
34403456
- version: v18.0.0
34413457
pr-url: https://github.com/nodejs/node/pull/41678
34423458
description: Passing an invalid callback to the `callback` argument
@@ -3460,6 +3476,8 @@ changes:
34603476
* `options` {Object}
34613477
* `bigint` {boolean} Whether the numeric values in the returned
34623478
{fs.Stats} object should be `bigint`. **Default:** `false`.
3479+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3480+
**Default:** `undefined`.
34633481
* `callback` {Function}
34643482
* `err` {Error}
34653483
* `stats` {fs.Stats}
@@ -3802,6 +3820,10 @@ exception are given to the completion callback.
38023820
<!-- YAML
38033821
added: v0.1.30
38043822
changes:
3823+
- version: REPLACEME
3824+
pr-url: https://github.com/nodejs/node/pull/63143
3825+
description: Accepts an additional `signal` option to allow aborting the
3826+
operation.
38053827
- version: v18.0.0
38063828
pr-url: https://github.com/nodejs/node/pull/41678
38073829
description: Passing an invalid callback to the `callback` argument
@@ -3829,6 +3851,8 @@ changes:
38293851
* `options` {Object}
38303852
* `bigint` {boolean} Whether the numeric values in the returned
38313853
{fs.Stats} object should be `bigint`. **Default:** `false`.
3854+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3855+
**Default:** `undefined`.
38323856
* `callback` {Function}
38333857
* `err` {Error}
38343858
* `stats` {fs.Stats}

β€Žlib/fs.jsβ€Ž

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const {
139139
const{
140140
isInt32,
141141
parseFileMode,
142+
validateAbortSignal,
142143
validateBoolean,
143144
validateBuffer,
144145
validateEncoding,
@@ -383,6 +384,25 @@ function checkAborted(signal, callback) {
383384
returnfalse;
384385
}
385386

387+
functionbindSignalToReq(req,signal,callback){
388+
if(!signal){
389+
req.oncomplete=callback;
390+
return;
391+
}
392+
letaborted=false;
393+
constonAbort=()=>{
394+
aborted=true;
395+
callback(newAbortError(undefined,{cause: signal.reason}));
396+
};
397+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
398+
signal.addEventListener('abort',onAbort,{__proto__: null,[kResistStopPropagation]: true});
399+
req.oncomplete=function(err,result){
400+
signal.removeEventListener('abort',onAbort);
401+
if(aborted)return;
402+
callback(err,result);
403+
};
404+
}
405+
386406
/**
387407
* Asynchronously reads the entire contents of a file.
388408
* @param {string | Buffer | URL | number} path
@@ -1954,7 +1974,7 @@ function readdirSync(path, options) {
19541974
* Invokes the callback with the `fs.Stats`
19551975
* for the file descriptor.
19561976
* @param {number} fd
1957-
* @param {{ bigint?: boolean; }} [options]
1977+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19581978
* @param {(
19591979
* err?: Error,
19601980
* stats?: Stats
@@ -1965,23 +1985,28 @@ function fstat(fd, options = { __proto__: null, bigint: false }, callback) {
19651985
if(typeofoptions==='function'){
19661986
callback=options;
19671987
options=kEmptyObject;
1988+
}elseif(options===null||typeofoptions!=='object'){
1989+
options=kEmptyObject;
19681990
}
19691991

19701992
consth=vfsState.handlers;
19711993
if(h!==null&&vfsResult(h.fstat(fd,options),callback))return;
19721994

19731995
callback=makeStatsCallback(callback);
19741996

1997+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
1998+
if(checkAborted(options.signal,callback))return;
1999+
19752000
constreq=newFSReqCallback(options.bigint);
1976-
req.oncomplete=callback;
2001+
bindSignalToReq(req,options.signal,callback);
19772002
binding.fstat(fd,options.bigint,req);
19782003
}
19792004

19802005
/**
19812006
* Retrieves the `fs.Stats` for the symbolic link
19822007
* referred to by the `path`.
19832008
* @param {string | Buffer | URL} path
1984-
* @param {{ bigint?: boolean; }} [options]
2009+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19852010
* @param {(
19862011
* err?: Error,
19872012
* stats?: Stats
@@ -1992,6 +2017,10 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
19922017
if(typeofoptions==='function'){
19932018
callback=options;
19942019
options=kEmptyObject;
2020+
}elseif(options===null||typeofoptions!=='object'){
2021+
options=kEmptyObject;
2022+
}else{
2023+
options=getOptions(options,{bigint: false});
19952024
}
19962025

19972026
consth=vfsState.handlers;
@@ -2005,8 +2034,11 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
20052034
return;
20062035
}
20072036

2037+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
2038+
if(checkAborted(options.signal,callback))return;
2039+
20082040
constreq=newFSReqCallback(options.bigint);
2009-
req.oncomplete=callback;
2041+
bindSignalToReq(req,options.signal,callback);
20102042
binding.lstat(path,options.bigint,req);
20112043
}
20122044

@@ -2036,11 +2068,12 @@ function stat(path, options = { __proto__: null, bigint: false, throwIfNoEntry:
20362068
callback=makeStatsCallback(callback);
20372069
path=getValidatedPath(path);
20382070

2071+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
20392072
if(checkAborted(options.signal,callback))return;
20402073

20412074
constreq=newFSReqCallback(options.bigint);
2042-
req.oncomplete=callback;
2043-
binding.stat(getValidatedPath(path),options.bigint,req,options.throwIfNoEntry);
2075+
bindSignalToReq(req,options.signal,callback);
2076+
binding.stat(path,options.bigint,req,options.throwIfNoEntry);
20442077
}
20452078

20462079
functionstatfs(path,options={__proto__: null,bigint: false},callback){

β€Žlib/internal/fs/promises.jsβ€Ž

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ const {
1212
PromisePrototypeThen,
1313
PromiseReject,
1414
PromiseResolve,
15+
PromiseWithResolvers,
1516
SafeArrayIterator,
1617
SafePromisePrototypeFinally,
18+
SafePromiseRace,
1719
Symbol,
1820
SymbolAsyncDispose,
1921
SymbolAsyncIterator,
@@ -1126,6 +1128,21 @@ function checkAborted(signal) {
11261128
thrownewAbortError(undefined,{cause: signal.reason});
11271129
}
11281130

1131+
functionrejectWithReason(signal,reject){
1132+
return()=>{
1133+
reject(newAbortError(undefined,{cause: signal.reason}));
1134+
};
1135+
}
1136+
1137+
asyncfunctionraceWithSignal(opPromise,signal){
1138+
if(!signal)returnopPromise;
1139+
const{promise: abortPromise, reject }=PromiseWithResolvers();
1140+
// eslint-disable-next-line no-unused-vars
1141+
using_=EventEmitter.addAbortListener(signal,
1142+
rejectWithReason(signal,reject));
1143+
returnawaitSafePromiseRace([opPromise,abortPromise]);
1144+
}
1145+
11291146
asyncfunctionwriteFileHandle(filehandle,data,signal,encoding){
11301147
checkAborted(signal);
11311148
if(isCustomIterable(data)){
@@ -1779,15 +1796,26 @@ async function symlink(target, path, type) {
17791796
}
17801797

17811798
asyncfunctionfstat(handle,options={__proto__: null,bigint: false}){
1782-
constresult=awaitPromisePrototypeThen(
1783-
binding.fstat(handle.fd,options.bigint,kUsePromises),
1784-
undefined,
1785-
handleErrorFromBinding,
1799+
validateObject(options,'options');
1800+
const{ signal }=options;
1801+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1802+
checkAborted(signal);
1803+
constresult=awaitraceWithSignal(
1804+
PromisePrototypeThen(
1805+
binding.fstat(handle.fd,options.bigint,kUsePromises),
1806+
undefined,
1807+
handleErrorFromBinding,
1808+
),
1809+
signal,
17861810
);
17871811
returngetStatsFromBinding(result);
17881812
}
17891813

17901814
asyncfunctionlstat(path,options={__proto__: null,bigint: false}){
1815+
validateObject(options,'options');
1816+
const{ signal }=options;
1817+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1818+
checkAborted(signal);
17911819
consth=vfsState.handlers;
17921820
if(h!==null){
17931821
constpromise=h.lstat(path,options);
@@ -1798,24 +1826,34 @@ async function lstat(path, options = { __proto__: null, bigint: false }) {
17981826
constresource=pathModule.toNamespacedPath(BufferIsBuffer(path) ? BufferToString(path) : path);
17991827
thrownewERR_ACCESS_DENIED('Access to this API has been restricted','FileSystemRead',resource);
18001828
}
1801-
constresult=awaitPromisePrototypeThen(
1802-
binding.lstat(path,options.bigint,kUsePromises),
1803-
undefined,
1804-
handleErrorFromBinding,
1829+
constresult=awaitraceWithSignal(
1830+
PromisePrototypeThen(
1831+
binding.lstat(path,options.bigint,kUsePromises),
1832+
undefined,
1833+
handleErrorFromBinding,
1834+
),
1835+
signal,
18051836
);
18061837
returngetStatsFromBinding(result);
18071838
}
18081839

18091840
asyncfunctionstat(path,options={__proto__: null,bigint: false,throwIfNoEntry: true}){
1841+
validateObject(options,'options');
1842+
const{ signal }=options;
1843+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1844+
checkAborted(signal);
18101845
consth=vfsState.handlers;
18111846
if(h!==null){
18121847
constpromise=h.stat(path,options);
18131848
if(promise!==undefined)returnawaitpromise;
18141849
}
1815-
constresult=awaitPromisePrototypeThen(
1816-
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1817-
undefined,
1818-
handleErrorFromBinding,
1850+
constresult=awaitraceWithSignal(
1851+
PromisePrototypeThen(
1852+
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1853+
undefined,
1854+
handleErrorFromBinding,
1855+
),
1856+
signal,
18191857
);
18201858

18211859
// Binding will resolve undefined if UV_ENOENT or UV_ENOTDIR and throwIfNoEntry is false

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit 6673eba

Browse files
mertcanaltinaduh95
authored andcommitted
fs: abort in-flight stat operations
Signed-off-by: Mert Can Altin <mertgold60@gmail.com> PR-URL: #63143 Refs: #57775 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 74234ee commit 6673eba

4 files changed

Lines changed: 200 additions & 34 deletions

File tree

β€Ždoc/api/fs.mdβ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,10 @@ link(2) documentation for more detail.
15421542
<!-- YAML
15431543
added: v10.0.0
15441544
changes:
1545+
- version: REPLACEME
1546+
pr-url: https://github.com/nodejs/node/pull/63143
1547+
description: Accepts an additional `signal` option to allow aborting the
1548+
operation.
15451549
- version: v10.5.0
15461550
pr-url: https://github.com/nodejs/node/pull/20220
15471551
description: Accepts an additional `options` object to specify whether
@@ -1552,6 +1556,8 @@ changes:
15521556
* `options` {Object}
15531557
* `bigint` {boolean} Whether the numeric values in the returned
15541558
{fs.Stats} object should be `bigint`. **Default:** `false`.
1559+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
1560+
**Default:** `undefined`.
15551561
* Returns: {Promise} Fulfills with the {fs.Stats} object for the given
15561562
symbolic link `path`.
15571563
@@ -2080,6 +2086,10 @@ Removes files and directories (modeled on the standard POSIX `rm` utility).
20802086
<!-- YAML
20812087
added: v10.0.0
20822088
changes:
2089+
- version: REPLACEME
2090+
pr-url: https://github.com/nodejs/node/pull/63143
2091+
description: Accepts an additional `signal` option to allow aborting the
2092+
operation.
20832093
- version: v25.7.0
20842094
pr-url: https://github.com/nodejs/node/pull/61178
20852095
description: Accepts a `throwIfNoEntry` option to specify whether
@@ -2097,6 +2107,8 @@ changes:
20972107
* `throwIfNoEntry` {boolean} Whether an exception will be thrown
20982108
if no file system entry exists, rather than returning `undefined`.
20992109
**Default:** `true`.
2110+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
2111+
**Default:** `undefined`.
21002112
* Returns: {Promise} Fulfills with the {fs.Stats} object for the
21012113
given `path`.
21022114
@@ -3437,6 +3449,10 @@ exception are given to the completion callback.
34373449
<!-- YAML
34383450
added: v0.1.95
34393451
changes:
3452+
- version: REPLACEME
3453+
pr-url: https://github.com/nodejs/node/pull/63143
3454+
description: Accepts an additional `signal` option to allow aborting the
3455+
operation.
34403456
- version: v18.0.0
34413457
pr-url: https://github.com/nodejs/node/pull/41678
34423458
description: Passing an invalid callback to the `callback` argument
@@ -3460,6 +3476,8 @@ changes:
34603476
* `options` {Object}
34613477
* `bigint` {boolean} Whether the numeric values in the returned
34623478
{fs.Stats} object should be `bigint`. **Default:** `false`.
3479+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3480+
**Default:** `undefined`.
34633481
* `callback` {Function}
34643482
* `err` {Error}
34653483
* `stats` {fs.Stats}
@@ -3802,6 +3820,10 @@ exception are given to the completion callback.
38023820
<!-- YAML
38033821
added: v0.1.30
38043822
changes:
3823+
- version: REPLACEME
3824+
pr-url: https://github.com/nodejs/node/pull/63143
3825+
description: Accepts an additional `signal` option to allow aborting the
3826+
operation.
38053827
- version: v18.0.0
38063828
pr-url: https://github.com/nodejs/node/pull/41678
38073829
description: Passing an invalid callback to the `callback` argument
@@ -3829,6 +3851,8 @@ changes:
38293851
* `options` {Object}
38303852
* `bigint` {boolean} Whether the numeric values in the returned
38313853
{fs.Stats} object should be `bigint`. **Default:** `false`.
3854+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3855+
**Default:** `undefined`.
38323856
* `callback` {Function}
38333857
* `err` {Error}
38343858
* `stats` {fs.Stats}

β€Žlib/fs.jsβ€Ž

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const {
139139
const{
140140
isInt32,
141141
parseFileMode,
142+
validateAbortSignal,
142143
validateBoolean,
143144
validateBuffer,
144145
validateEncoding,
@@ -383,6 +384,25 @@ function checkAborted(signal, callback) {
383384
returnfalse;
384385
}
385386

387+
functionbindSignalToReq(req,signal,callback){
388+
if(!signal){
389+
req.oncomplete=callback;
390+
return;
391+
}
392+
letaborted=false;
393+
constonAbort=()=>{
394+
aborted=true;
395+
callback(newAbortError(undefined,{cause: signal.reason}));
396+
};
397+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
398+
signal.addEventListener('abort',onAbort,{__proto__: null,[kResistStopPropagation]: true});
399+
req.oncomplete=function(err,result){
400+
signal.removeEventListener('abort',onAbort);
401+
if(aborted)return;
402+
callback(err,result);
403+
};
404+
}
405+
386406
/**
387407
* Asynchronously reads the entire contents of a file.
388408
* @param {string | Buffer | URL | number} path
@@ -1954,7 +1974,7 @@ function readdirSync(path, options) {
19541974
* Invokes the callback with the `fs.Stats`
19551975
* for the file descriptor.
19561976
* @param {number} fd
1957-
* @param {{ bigint?: boolean; }} [options]
1977+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19581978
* @param {(
19591979
* err?: Error,
19601980
* stats?: Stats
@@ -1965,23 +1985,28 @@ function fstat(fd, options = { __proto__: null, bigint: false }, callback) {
19651985
if(typeofoptions==='function'){
19661986
callback=options;
19671987
options=kEmptyObject;
1988+
}elseif(options===null||typeofoptions!=='object'){
1989+
options=kEmptyObject;
19681990
}
19691991

19701992
consth=vfsState.handlers;
19711993
if(h!==null&&vfsResult(h.fstat(fd,options),callback))return;
19721994

19731995
callback=makeStatsCallback(callback);
19741996

1997+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
1998+
if(checkAborted(options.signal,callback))return;
1999+
19752000
constreq=newFSReqCallback(options.bigint);
1976-
req.oncomplete=callback;
2001+
bindSignalToReq(req,options.signal,callback);
19772002
binding.fstat(fd,options.bigint,req);
19782003
}
19792004

19802005
/**
19812006
* Retrieves the `fs.Stats` for the symbolic link
19822007
* referred to by the `path`.
19832008
* @param {string | Buffer | URL} path
1984-
* @param {{ bigint?: boolean; }} [options]
2009+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19852010
* @param {(
19862011
* err?: Error,
19872012
* stats?: Stats
@@ -1992,6 +2017,10 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
19922017
if(typeofoptions==='function'){
19932018
callback=options;
19942019
options=kEmptyObject;
2020+
}elseif(options===null||typeofoptions!=='object'){
2021+
options=kEmptyObject;
2022+
}else{
2023+
options=getOptions(options,{bigint: false});
19952024
}
19962025

19972026
consth=vfsState.handlers;
@@ -2005,8 +2034,11 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
20052034
return;
20062035
}
20072036

2037+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
2038+
if(checkAborted(options.signal,callback))return;
2039+
20082040
constreq=newFSReqCallback(options.bigint);
2009-
req.oncomplete=callback;
2041+
bindSignalToReq(req,options.signal,callback);
20102042
binding.lstat(path,options.bigint,req);
20112043
}
20122044

@@ -2036,11 +2068,12 @@ function stat(path, options = { __proto__: null, bigint: false, throwIfNoEntry:
20362068
callback=makeStatsCallback(callback);
20372069
path=getValidatedPath(path);
20382070

2071+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
20392072
if(checkAborted(options.signal,callback))return;
20402073

20412074
constreq=newFSReqCallback(options.bigint);
2042-
req.oncomplete=callback;
2043-
binding.stat(getValidatedPath(path),options.bigint,req,options.throwIfNoEntry);
2075+
bindSignalToReq(req,options.signal,callback);
2076+
binding.stat(path,options.bigint,req,options.throwIfNoEntry);
20442077
}
20452078

20462079
functionstatfs(path,options={__proto__: null,bigint: false},callback){

β€Žlib/internal/fs/promises.jsβ€Ž

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ const {
1212
PromisePrototypeThen,
1313
PromiseReject,
1414
PromiseResolve,
15+
PromiseWithResolvers,
1516
SafeArrayIterator,
1617
SafePromisePrototypeFinally,
18+
SafePromiseRace,
1719
Symbol,
1820
SymbolAsyncDispose,
1921
SymbolAsyncIterator,
@@ -1126,6 +1128,21 @@ function checkAborted(signal) {
11261128
thrownewAbortError(undefined,{cause: signal.reason});
11271129
}
11281130

1131+
functionrejectWithReason(signal,reject){
1132+
return()=>{
1133+
reject(newAbortError(undefined,{cause: signal.reason}));
1134+
};
1135+
}
1136+
1137+
asyncfunctionraceWithSignal(opPromise,signal){
1138+
if(!signal)returnopPromise;
1139+
const{promise: abortPromise, reject }=PromiseWithResolvers();
1140+
// eslint-disable-next-line no-unused-vars
1141+
using_=EventEmitter.addAbortListener(signal,
1142+
rejectWithReason(signal,reject));
1143+
returnawaitSafePromiseRace([opPromise,abortPromise]);
1144+
}
1145+
11291146
asyncfunctionwriteFileHandle(filehandle,data,signal,encoding){
11301147
checkAborted(signal);
11311148
if(isCustomIterable(data)){
@@ -1779,15 +1796,26 @@ async function symlink(target, path, type) {
17791796
}
17801797

17811798
asyncfunctionfstat(handle,options={__proto__: null,bigint: false}){
1782-
constresult=awaitPromisePrototypeThen(
1783-
binding.fstat(handle.fd,options.bigint,kUsePromises),
1784-
undefined,
1785-
handleErrorFromBinding,
1799+
validateObject(options,'options');
1800+
const{ signal }=options;
1801+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1802+
checkAborted(signal);
1803+
constresult=awaitraceWithSignal(
1804+
PromisePrototypeThen(
1805+
binding.fstat(handle.fd,options.bigint,kUsePromises),
1806+
undefined,
1807+
handleErrorFromBinding,
1808+
),
1809+
signal,
17861810
);
17871811
returngetStatsFromBinding(result);
17881812
}
17891813

17901814
asyncfunctionlstat(path,options={__proto__: null,bigint: false}){
1815+
validateObject(options,'options');
1816+
const{ signal }=options;
1817+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1818+
checkAborted(signal);
17911819
consth=vfsState.handlers;
17921820
if(h!==null){
17931821
constpromise=h.lstat(path,options);
@@ -1798,24 +1826,34 @@ async function lstat(path, options = { __proto__: null, bigint: false }) {
17981826
constresource=pathModule.toNamespacedPath(BufferIsBuffer(path) ? BufferToString(path) : path);
17991827
thrownewERR_ACCESS_DENIED('Access to this API has been restricted','FileSystemRead',resource);
18001828
}
1801-
constresult=awaitPromisePrototypeThen(
1802-
binding.lstat(path,options.bigint,kUsePromises),
1803-
undefined,
1804-
handleErrorFromBinding,
1829+
constresult=awaitraceWithSignal(
1830+
PromisePrototypeThen(
1831+
binding.lstat(path,options.bigint,kUsePromises),
1832+
undefined,
1833+
handleErrorFromBinding,
1834+
),
1835+
signal,
18051836
);
18061837
returngetStatsFromBinding(result);
18071838
}
18081839

18091840
asyncfunctionstat(path,options={__proto__: null,bigint: false,throwIfNoEntry: true}){
1841+
validateObject(options,'options');
1842+
const{ signal }=options;
1843+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1844+
checkAborted(signal);
18101845
consth=vfsState.handlers;
18111846
if(h!==null){
18121847
constpromise=h.stat(path,options);
18131848
if(promise!==undefined)returnawaitpromise;
18141849
}
1815-
constresult=awaitPromisePrototypeThen(
1816-
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1817-
undefined,
1818-
handleErrorFromBinding,
1850+
constresult=awaitraceWithSignal(
1851+
PromisePrototypeThen(
1852+
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1853+
undefined,
1854+
handleErrorFromBinding,
1855+
),
1856+
signal,
18191857
);
18201858

18211859
// Binding will resolve undefined if UV_ENOENT or UV_ENOTDIR and throwIfNoEntry is false

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 6673eba

Browse files
mertcanaltinaduh95
authored andcommitted
fs: abort in-flight stat operations
Signed-off-by: Mert Can Altin <mertgold60@gmail.com> PR-URL: #63143 Refs: #57775 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 74234ee commit 6673eba

4 files changed

Lines changed: 200 additions & 34 deletions

File tree

β€Ždoc/api/fs.mdβ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,10 @@ link(2) documentation for more detail.
15421542
<!-- YAML
15431543
added: v10.0.0
15441544
changes:
1545+
- version: REPLACEME
1546+
pr-url: https://github.com/nodejs/node/pull/63143
1547+
description: Accepts an additional `signal` option to allow aborting the
1548+
operation.
15451549
- version: v10.5.0
15461550
pr-url: https://github.com/nodejs/node/pull/20220
15471551
description: Accepts an additional `options` object to specify whether
@@ -1552,6 +1556,8 @@ changes:
15521556
* `options` {Object}
15531557
* `bigint` {boolean} Whether the numeric values in the returned
15541558
{fs.Stats} object should be `bigint`. **Default:** `false`.
1559+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
1560+
**Default:** `undefined`.
15551561
* Returns: {Promise} Fulfills with the {fs.Stats} object for the given
15561562
symbolic link `path`.
15571563
@@ -2080,6 +2086,10 @@ Removes files and directories (modeled on the standard POSIX `rm` utility).
20802086
<!-- YAML
20812087
added: v10.0.0
20822088
changes:
2089+
- version: REPLACEME
2090+
pr-url: https://github.com/nodejs/node/pull/63143
2091+
description: Accepts an additional `signal` option to allow aborting the
2092+
operation.
20832093
- version: v25.7.0
20842094
pr-url: https://github.com/nodejs/node/pull/61178
20852095
description: Accepts a `throwIfNoEntry` option to specify whether
@@ -2097,6 +2107,8 @@ changes:
20972107
* `throwIfNoEntry` {boolean} Whether an exception will be thrown
20982108
if no file system entry exists, rather than returning `undefined`.
20992109
**Default:** `true`.
2110+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
2111+
**Default:** `undefined`.
21002112
* Returns: {Promise} Fulfills with the {fs.Stats} object for the
21012113
given `path`.
21022114
@@ -3437,6 +3449,10 @@ exception are given to the completion callback.
34373449
<!-- YAML
34383450
added: v0.1.95
34393451
changes:
3452+
- version: REPLACEME
3453+
pr-url: https://github.com/nodejs/node/pull/63143
3454+
description: Accepts an additional `signal` option to allow aborting the
3455+
operation.
34403456
- version: v18.0.0
34413457
pr-url: https://github.com/nodejs/node/pull/41678
34423458
description: Passing an invalid callback to the `callback` argument
@@ -3460,6 +3476,8 @@ changes:
34603476
* `options` {Object}
34613477
* `bigint` {boolean} Whether the numeric values in the returned
34623478
{fs.Stats} object should be `bigint`. **Default:** `false`.
3479+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3480+
**Default:** `undefined`.
34633481
* `callback` {Function}
34643482
* `err` {Error}
34653483
* `stats` {fs.Stats}
@@ -3802,6 +3820,10 @@ exception are given to the completion callback.
38023820
<!-- YAML
38033821
added: v0.1.30
38043822
changes:
3823+
- version: REPLACEME
3824+
pr-url: https://github.com/nodejs/node/pull/63143
3825+
description: Accepts an additional `signal` option to allow aborting the
3826+
operation.
38053827
- version: v18.0.0
38063828
pr-url: https://github.com/nodejs/node/pull/41678
38073829
description: Passing an invalid callback to the `callback` argument
@@ -3829,6 +3851,8 @@ changes:
38293851
* `options` {Object}
38303852
* `bigint` {boolean} Whether the numeric values in the returned
38313853
{fs.Stats} object should be `bigint`. **Default:** `false`.
3854+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3855+
**Default:** `undefined`.
38323856
* `callback` {Function}
38333857
* `err` {Error}
38343858
* `stats` {fs.Stats}

β€Žlib/fs.jsβ€Ž

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const {
139139
const{
140140
isInt32,
141141
parseFileMode,
142+
validateAbortSignal,
142143
validateBoolean,
143144
validateBuffer,
144145
validateEncoding,
@@ -383,6 +384,25 @@ function checkAborted(signal, callback) {
383384
returnfalse;
384385
}
385386

387+
functionbindSignalToReq(req,signal,callback){
388+
if(!signal){
389+
req.oncomplete=callback;
390+
return;
391+
}
392+
letaborted=false;
393+
constonAbort=()=>{
394+
aborted=true;
395+
callback(newAbortError(undefined,{cause: signal.reason}));
396+
};
397+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
398+
signal.addEventListener('abort',onAbort,{__proto__: null,[kResistStopPropagation]: true});
399+
req.oncomplete=function(err,result){
400+
signal.removeEventListener('abort',onAbort);
401+
if(aborted)return;
402+
callback(err,result);
403+
};
404+
}
405+
386406
/**
387407
* Asynchronously reads the entire contents of a file.
388408
* @param {string | Buffer | URL | number} path
@@ -1954,7 +1974,7 @@ function readdirSync(path, options) {
19541974
* Invokes the callback with the `fs.Stats`
19551975
* for the file descriptor.
19561976
* @param {number} fd
1957-
* @param {{ bigint?: boolean; }} [options]
1977+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19581978
* @param {(
19591979
* err?: Error,
19601980
* stats?: Stats
@@ -1965,23 +1985,28 @@ function fstat(fd, options = { __proto__: null, bigint: false }, callback) {
19651985
if(typeofoptions==='function'){
19661986
callback=options;
19671987
options=kEmptyObject;
1988+
}elseif(options===null||typeofoptions!=='object'){
1989+
options=kEmptyObject;
19681990
}
19691991

19701992
consth=vfsState.handlers;
19711993
if(h!==null&&vfsResult(h.fstat(fd,options),callback))return;
19721994

19731995
callback=makeStatsCallback(callback);
19741996

1997+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
1998+
if(checkAborted(options.signal,callback))return;
1999+
19752000
constreq=newFSReqCallback(options.bigint);
1976-
req.oncomplete=callback;
2001+
bindSignalToReq(req,options.signal,callback);
19772002
binding.fstat(fd,options.bigint,req);
19782003
}
19792004

19802005
/**
19812006
* Retrieves the `fs.Stats` for the symbolic link
19822007
* referred to by the `path`.
19832008
* @param {string | Buffer | URL} path
1984-
* @param {{ bigint?: boolean; }} [options]
2009+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19852010
* @param {(
19862011
* err?: Error,
19872012
* stats?: Stats
@@ -1992,6 +2017,10 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
19922017
if(typeofoptions==='function'){
19932018
callback=options;
19942019
options=kEmptyObject;
2020+
}elseif(options===null||typeofoptions!=='object'){
2021+
options=kEmptyObject;
2022+
}else{
2023+
options=getOptions(options,{bigint: false});
19952024
}
19962025

19972026
consth=vfsState.handlers;
@@ -2005,8 +2034,11 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
20052034
return;
20062035
}
20072036

2037+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
2038+
if(checkAborted(options.signal,callback))return;
2039+
20082040
constreq=newFSReqCallback(options.bigint);
2009-
req.oncomplete=callback;
2041+
bindSignalToReq(req,options.signal,callback);
20102042
binding.lstat(path,options.bigint,req);
20112043
}
20122044

@@ -2036,11 +2068,12 @@ function stat(path, options = { __proto__: null, bigint: false, throwIfNoEntry:
20362068
callback=makeStatsCallback(callback);
20372069
path=getValidatedPath(path);
20382070

2071+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
20392072
if(checkAborted(options.signal,callback))return;
20402073

20412074
constreq=newFSReqCallback(options.bigint);
2042-
req.oncomplete=callback;
2043-
binding.stat(getValidatedPath(path),options.bigint,req,options.throwIfNoEntry);
2075+
bindSignalToReq(req,options.signal,callback);
2076+
binding.stat(path,options.bigint,req,options.throwIfNoEntry);
20442077
}
20452078

20462079
functionstatfs(path,options={__proto__: null,bigint: false},callback){

β€Žlib/internal/fs/promises.jsβ€Ž

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ const {
1212
PromisePrototypeThen,
1313
PromiseReject,
1414
PromiseResolve,
15+
PromiseWithResolvers,
1516
SafeArrayIterator,
1617
SafePromisePrototypeFinally,
18+
SafePromiseRace,
1719
Symbol,
1820
SymbolAsyncDispose,
1921
SymbolAsyncIterator,
@@ -1126,6 +1128,21 @@ function checkAborted(signal) {
11261128
thrownewAbortError(undefined,{cause: signal.reason});
11271129
}
11281130

1131+
functionrejectWithReason(signal,reject){
1132+
return()=>{
1133+
reject(newAbortError(undefined,{cause: signal.reason}));
1134+
};
1135+
}
1136+
1137+
asyncfunctionraceWithSignal(opPromise,signal){
1138+
if(!signal)returnopPromise;
1139+
const{promise: abortPromise, reject }=PromiseWithResolvers();
1140+
// eslint-disable-next-line no-unused-vars
1141+
using_=EventEmitter.addAbortListener(signal,
1142+
rejectWithReason(signal,reject));
1143+
returnawaitSafePromiseRace([opPromise,abortPromise]);
1144+
}
1145+
11291146
asyncfunctionwriteFileHandle(filehandle,data,signal,encoding){
11301147
checkAborted(signal);
11311148
if(isCustomIterable(data)){
@@ -1779,15 +1796,26 @@ async function symlink(target, path, type) {
17791796
}
17801797

17811798
asyncfunctionfstat(handle,options={__proto__: null,bigint: false}){
1782-
constresult=awaitPromisePrototypeThen(
1783-
binding.fstat(handle.fd,options.bigint,kUsePromises),
1784-
undefined,
1785-
handleErrorFromBinding,
1799+
validateObject(options,'options');
1800+
const{ signal }=options;
1801+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1802+
checkAborted(signal);
1803+
constresult=awaitraceWithSignal(
1804+
PromisePrototypeThen(
1805+
binding.fstat(handle.fd,options.bigint,kUsePromises),
1806+
undefined,
1807+
handleErrorFromBinding,
1808+
),
1809+
signal,
17861810
);
17871811
returngetStatsFromBinding(result);
17881812
}
17891813

17901814
asyncfunctionlstat(path,options={__proto__: null,bigint: false}){
1815+
validateObject(options,'options');
1816+
const{ signal }=options;
1817+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1818+
checkAborted(signal);
17911819
consth=vfsState.handlers;
17921820
if(h!==null){
17931821
constpromise=h.lstat(path,options);
@@ -1798,24 +1826,34 @@ async function lstat(path, options = { __proto__: null, bigint: false }) {
17981826
constresource=pathModule.toNamespacedPath(BufferIsBuffer(path) ? BufferToString(path) : path);
17991827
thrownewERR_ACCESS_DENIED('Access to this API has been restricted','FileSystemRead',resource);
18001828
}
1801-
constresult=awaitPromisePrototypeThen(
1802-
binding.lstat(path,options.bigint,kUsePromises),
1803-
undefined,
1804-
handleErrorFromBinding,
1829+
constresult=awaitraceWithSignal(
1830+
PromisePrototypeThen(
1831+
binding.lstat(path,options.bigint,kUsePromises),
1832+
undefined,
1833+
handleErrorFromBinding,
1834+
),
1835+
signal,
18051836
);
18061837
returngetStatsFromBinding(result);
18071838
}
18081839

18091840
asyncfunctionstat(path,options={__proto__: null,bigint: false,throwIfNoEntry: true}){
1841+
validateObject(options,'options');
1842+
const{ signal }=options;
1843+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1844+
checkAborted(signal);
18101845
consth=vfsState.handlers;
18111846
if(h!==null){
18121847
constpromise=h.stat(path,options);
18131848
if(promise!==undefined)returnawaitpromise;
18141849
}
1815-
constresult=awaitPromisePrototypeThen(
1816-
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1817-
undefined,
1818-
handleErrorFromBinding,
1850+
constresult=awaitraceWithSignal(
1851+
PromisePrototypeThen(
1852+
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1853+
undefined,
1854+
handleErrorFromBinding,
1855+
),
1856+
signal,
18191857
);
18201858

18211859
// Binding will resolve undefined if UV_ENOENT or UV_ENOTDIR and throwIfNoEntry is false

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 6673eba

Browse files
mertcanaltinaduh95
authored andcommitted
fs: abort in-flight stat operations
Signed-off-by: Mert Can Altin <mertgold60@gmail.com> PR-URL: #63143 Refs: #57775 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 74234ee commit 6673eba

4 files changed

Lines changed: 200 additions & 34 deletions

File tree

β€Ždoc/api/fs.mdβ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,10 @@ link(2) documentation for more detail.
15421542
<!-- YAML
15431543
added: v10.0.0
15441544
changes:
1545+
- version: REPLACEME
1546+
pr-url: https://github.com/nodejs/node/pull/63143
1547+
description: Accepts an additional `signal` option to allow aborting the
1548+
operation.
15451549
- version: v10.5.0
15461550
pr-url: https://github.com/nodejs/node/pull/20220
15471551
description: Accepts an additional `options` object to specify whether
@@ -1552,6 +1556,8 @@ changes:
15521556
* `options` {Object}
15531557
* `bigint` {boolean} Whether the numeric values in the returned
15541558
{fs.Stats} object should be `bigint`. **Default:** `false`.
1559+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
1560+
**Default:** `undefined`.
15551561
* Returns: {Promise} Fulfills with the {fs.Stats} object for the given
15561562
symbolic link `path`.
15571563
@@ -2080,6 +2086,10 @@ Removes files and directories (modeled on the standard POSIX `rm` utility).
20802086
<!-- YAML
20812087
added: v10.0.0
20822088
changes:
2089+
- version: REPLACEME
2090+
pr-url: https://github.com/nodejs/node/pull/63143
2091+
description: Accepts an additional `signal` option to allow aborting the
2092+
operation.
20832093
- version: v25.7.0
20842094
pr-url: https://github.com/nodejs/node/pull/61178
20852095
description: Accepts a `throwIfNoEntry` option to specify whether
@@ -2097,6 +2107,8 @@ changes:
20972107
* `throwIfNoEntry` {boolean} Whether an exception will be thrown
20982108
if no file system entry exists, rather than returning `undefined`.
20992109
**Default:** `true`.
2110+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
2111+
**Default:** `undefined`.
21002112
* Returns: {Promise} Fulfills with the {fs.Stats} object for the
21012113
given `path`.
21022114
@@ -3437,6 +3449,10 @@ exception are given to the completion callback.
34373449
<!-- YAML
34383450
added: v0.1.95
34393451
changes:
3452+
- version: REPLACEME
3453+
pr-url: https://github.com/nodejs/node/pull/63143
3454+
description: Accepts an additional `signal` option to allow aborting the
3455+
operation.
34403456
- version: v18.0.0
34413457
pr-url: https://github.com/nodejs/node/pull/41678
34423458
description: Passing an invalid callback to the `callback` argument
@@ -3460,6 +3476,8 @@ changes:
34603476
* `options` {Object}
34613477
* `bigint` {boolean} Whether the numeric values in the returned
34623478
{fs.Stats} object should be `bigint`. **Default:** `false`.
3479+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3480+
**Default:** `undefined`.
34633481
* `callback` {Function}
34643482
* `err` {Error}
34653483
* `stats` {fs.Stats}
@@ -3802,6 +3820,10 @@ exception are given to the completion callback.
38023820
<!-- YAML
38033821
added: v0.1.30
38043822
changes:
3823+
- version: REPLACEME
3824+
pr-url: https://github.com/nodejs/node/pull/63143
3825+
description: Accepts an additional `signal` option to allow aborting the
3826+
operation.
38053827
- version: v18.0.0
38063828
pr-url: https://github.com/nodejs/node/pull/41678
38073829
description: Passing an invalid callback to the `callback` argument
@@ -3829,6 +3851,8 @@ changes:
38293851
* `options` {Object}
38303852
* `bigint` {boolean} Whether the numeric values in the returned
38313853
{fs.Stats} object should be `bigint`. **Default:** `false`.
3854+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3855+
**Default:** `undefined`.
38323856
* `callback` {Function}
38333857
* `err` {Error}
38343858
* `stats` {fs.Stats}

β€Žlib/fs.jsβ€Ž

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const {
139139
const{
140140
isInt32,
141141
parseFileMode,
142+
validateAbortSignal,
142143
validateBoolean,
143144
validateBuffer,
144145
validateEncoding,
@@ -383,6 +384,25 @@ function checkAborted(signal, callback) {
383384
returnfalse;
384385
}
385386

387+
functionbindSignalToReq(req,signal,callback){
388+
if(!signal){
389+
req.oncomplete=callback;
390+
return;
391+
}
392+
letaborted=false;
393+
constonAbort=()=>{
394+
aborted=true;
395+
callback(newAbortError(undefined,{cause: signal.reason}));
396+
};
397+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
398+
signal.addEventListener('abort',onAbort,{__proto__: null,[kResistStopPropagation]: true});
399+
req.oncomplete=function(err,result){
400+
signal.removeEventListener('abort',onAbort);
401+
if(aborted)return;
402+
callback(err,result);
403+
};
404+
}
405+
386406
/**
387407
* Asynchronously reads the entire contents of a file.
388408
* @param {string | Buffer | URL | number} path
@@ -1954,7 +1974,7 @@ function readdirSync(path, options) {
19541974
* Invokes the callback with the `fs.Stats`
19551975
* for the file descriptor.
19561976
* @param {number} fd
1957-
* @param {{ bigint?: boolean; }} [options]
1977+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19581978
* @param {(
19591979
* err?: Error,
19601980
* stats?: Stats
@@ -1965,23 +1985,28 @@ function fstat(fd, options = { __proto__: null, bigint: false }, callback) {
19651985
if(typeofoptions==='function'){
19661986
callback=options;
19671987
options=kEmptyObject;
1988+
}elseif(options===null||typeofoptions!=='object'){
1989+
options=kEmptyObject;
19681990
}
19691991

19701992
consth=vfsState.handlers;
19711993
if(h!==null&&vfsResult(h.fstat(fd,options),callback))return;
19721994

19731995
callback=makeStatsCallback(callback);
19741996

1997+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
1998+
if(checkAborted(options.signal,callback))return;
1999+
19752000
constreq=newFSReqCallback(options.bigint);
1976-
req.oncomplete=callback;
2001+
bindSignalToReq(req,options.signal,callback);
19772002
binding.fstat(fd,options.bigint,req);
19782003
}
19792004

19802005
/**
19812006
* Retrieves the `fs.Stats` for the symbolic link
19822007
* referred to by the `path`.
19832008
* @param {string | Buffer | URL} path
1984-
* @param {{ bigint?: boolean; }} [options]
2009+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19852010
* @param {(
19862011
* err?: Error,
19872012
* stats?: Stats
@@ -1992,6 +2017,10 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
19922017
if(typeofoptions==='function'){
19932018
callback=options;
19942019
options=kEmptyObject;
2020+
}elseif(options===null||typeofoptions!=='object'){
2021+
options=kEmptyObject;
2022+
}else{
2023+
options=getOptions(options,{bigint: false});
19952024
}
19962025

19972026
consth=vfsState.handlers;
@@ -2005,8 +2034,11 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
20052034
return;
20062035
}
20072036

2037+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
2038+
if(checkAborted(options.signal,callback))return;
2039+
20082040
constreq=newFSReqCallback(options.bigint);
2009-
req.oncomplete=callback;
2041+
bindSignalToReq(req,options.signal,callback);
20102042
binding.lstat(path,options.bigint,req);
20112043
}
20122044

@@ -2036,11 +2068,12 @@ function stat(path, options = { __proto__: null, bigint: false, throwIfNoEntry:
20362068
callback=makeStatsCallback(callback);
20372069
path=getValidatedPath(path);
20382070

2071+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
20392072
if(checkAborted(options.signal,callback))return;
20402073

20412074
constreq=newFSReqCallback(options.bigint);
2042-
req.oncomplete=callback;
2043-
binding.stat(getValidatedPath(path),options.bigint,req,options.throwIfNoEntry);
2075+
bindSignalToReq(req,options.signal,callback);
2076+
binding.stat(path,options.bigint,req,options.throwIfNoEntry);
20442077
}
20452078

20462079
functionstatfs(path,options={__proto__: null,bigint: false},callback){

β€Žlib/internal/fs/promises.jsβ€Ž

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ const {
1212
PromisePrototypeThen,
1313
PromiseReject,
1414
PromiseResolve,
15+
PromiseWithResolvers,
1516
SafeArrayIterator,
1617
SafePromisePrototypeFinally,
18+
SafePromiseRace,
1719
Symbol,
1820
SymbolAsyncDispose,
1921
SymbolAsyncIterator,
@@ -1126,6 +1128,21 @@ function checkAborted(signal) {
11261128
thrownewAbortError(undefined,{cause: signal.reason});
11271129
}
11281130

1131+
functionrejectWithReason(signal,reject){
1132+
return()=>{
1133+
reject(newAbortError(undefined,{cause: signal.reason}));
1134+
};
1135+
}
1136+
1137+
asyncfunctionraceWithSignal(opPromise,signal){
1138+
if(!signal)returnopPromise;
1139+
const{promise: abortPromise, reject }=PromiseWithResolvers();
1140+
// eslint-disable-next-line no-unused-vars
1141+
using_=EventEmitter.addAbortListener(signal,
1142+
rejectWithReason(signal,reject));
1143+
returnawaitSafePromiseRace([opPromise,abortPromise]);
1144+
}
1145+
11291146
asyncfunctionwriteFileHandle(filehandle,data,signal,encoding){
11301147
checkAborted(signal);
11311148
if(isCustomIterable(data)){
@@ -1779,15 +1796,26 @@ async function symlink(target, path, type) {
17791796
}
17801797

17811798
asyncfunctionfstat(handle,options={__proto__: null,bigint: false}){
1782-
constresult=awaitPromisePrototypeThen(
1783-
binding.fstat(handle.fd,options.bigint,kUsePromises),
1784-
undefined,
1785-
handleErrorFromBinding,
1799+
validateObject(options,'options');
1800+
const{ signal }=options;
1801+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1802+
checkAborted(signal);
1803+
constresult=awaitraceWithSignal(
1804+
PromisePrototypeThen(
1805+
binding.fstat(handle.fd,options.bigint,kUsePromises),
1806+
undefined,
1807+
handleErrorFromBinding,
1808+
),
1809+
signal,
17861810
);
17871811
returngetStatsFromBinding(result);
17881812
}
17891813

17901814
asyncfunctionlstat(path,options={__proto__: null,bigint: false}){
1815+
validateObject(options,'options');
1816+
const{ signal }=options;
1817+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1818+
checkAborted(signal);
17911819
consth=vfsState.handlers;
17921820
if(h!==null){
17931821
constpromise=h.lstat(path,options);
@@ -1798,24 +1826,34 @@ async function lstat(path, options = { __proto__: null, bigint: false }) {
17981826
constresource=pathModule.toNamespacedPath(BufferIsBuffer(path) ? BufferToString(path) : path);
17991827
thrownewERR_ACCESS_DENIED('Access to this API has been restricted','FileSystemRead',resource);
18001828
}
1801-
constresult=awaitPromisePrototypeThen(
1802-
binding.lstat(path,options.bigint,kUsePromises),
1803-
undefined,
1804-
handleErrorFromBinding,
1829+
constresult=awaitraceWithSignal(
1830+
PromisePrototypeThen(
1831+
binding.lstat(path,options.bigint,kUsePromises),
1832+
undefined,
1833+
handleErrorFromBinding,
1834+
),
1835+
signal,
18051836
);
18061837
returngetStatsFromBinding(result);
18071838
}
18081839

18091840
asyncfunctionstat(path,options={__proto__: null,bigint: false,throwIfNoEntry: true}){
1841+
validateObject(options,'options');
1842+
const{ signal }=options;
1843+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1844+
checkAborted(signal);
18101845
consth=vfsState.handlers;
18111846
if(h!==null){
18121847
constpromise=h.stat(path,options);
18131848
if(promise!==undefined)returnawaitpromise;
18141849
}
1815-
constresult=awaitPromisePrototypeThen(
1816-
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1817-
undefined,
1818-
handleErrorFromBinding,
1850+
constresult=awaitraceWithSignal(
1851+
PromisePrototypeThen(
1852+
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1853+
undefined,
1854+
handleErrorFromBinding,
1855+
),
1856+
signal,
18191857
);
18201858

18211859
// Binding will resolve undefined if UV_ENOENT or UV_ENOTDIR and throwIfNoEntry is false

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit 6673eba

Browse files
mertcanaltinaduh95
authored andcommitted
fs: abort in-flight stat operations
Signed-off-by: Mert Can Altin <mertgold60@gmail.com> PR-URL: #63143 Refs: #57775 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 74234ee commit 6673eba

4 files changed

Lines changed: 200 additions & 34 deletions

File tree

β€Ždoc/api/fs.mdβ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,10 @@ link(2) documentation for more detail.
15421542
<!-- YAML
15431543
added: v10.0.0
15441544
changes:
1545+
- version: REPLACEME
1546+
pr-url: https://github.com/nodejs/node/pull/63143
1547+
description: Accepts an additional `signal` option to allow aborting the
1548+
operation.
15451549
- version: v10.5.0
15461550
pr-url: https://github.com/nodejs/node/pull/20220
15471551
description: Accepts an additional `options` object to specify whether
@@ -1552,6 +1556,8 @@ changes:
15521556
* `options` {Object}
15531557
* `bigint` {boolean} Whether the numeric values in the returned
15541558
{fs.Stats} object should be `bigint`. **Default:** `false`.
1559+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
1560+
**Default:** `undefined`.
15551561
* Returns: {Promise} Fulfills with the {fs.Stats} object for the given
15561562
symbolic link `path`.
15571563
@@ -2080,6 +2086,10 @@ Removes files and directories (modeled on the standard POSIX `rm` utility).
20802086
<!-- YAML
20812087
added: v10.0.0
20822088
changes:
2089+
- version: REPLACEME
2090+
pr-url: https://github.com/nodejs/node/pull/63143
2091+
description: Accepts an additional `signal` option to allow aborting the
2092+
operation.
20832093
- version: v25.7.0
20842094
pr-url: https://github.com/nodejs/node/pull/61178
20852095
description: Accepts a `throwIfNoEntry` option to specify whether
@@ -2097,6 +2107,8 @@ changes:
20972107
* `throwIfNoEntry` {boolean} Whether an exception will be thrown
20982108
if no file system entry exists, rather than returning `undefined`.
20992109
**Default:** `true`.
2110+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
2111+
**Default:** `undefined`.
21002112
* Returns: {Promise} Fulfills with the {fs.Stats} object for the
21012113
given `path`.
21022114
@@ -3437,6 +3449,10 @@ exception are given to the completion callback.
34373449
<!-- YAML
34383450
added: v0.1.95
34393451
changes:
3452+
- version: REPLACEME
3453+
pr-url: https://github.com/nodejs/node/pull/63143
3454+
description: Accepts an additional `signal` option to allow aborting the
3455+
operation.
34403456
- version: v18.0.0
34413457
pr-url: https://github.com/nodejs/node/pull/41678
34423458
description: Passing an invalid callback to the `callback` argument
@@ -3460,6 +3476,8 @@ changes:
34603476
* `options` {Object}
34613477
* `bigint` {boolean} Whether the numeric values in the returned
34623478
{fs.Stats} object should be `bigint`. **Default:** `false`.
3479+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3480+
**Default:** `undefined`.
34633481
* `callback` {Function}
34643482
* `err` {Error}
34653483
* `stats` {fs.Stats}
@@ -3802,6 +3820,10 @@ exception are given to the completion callback.
38023820
<!-- YAML
38033821
added: v0.1.30
38043822
changes:
3823+
- version: REPLACEME
3824+
pr-url: https://github.com/nodejs/node/pull/63143
3825+
description: Accepts an additional `signal` option to allow aborting the
3826+
operation.
38053827
- version: v18.0.0
38063828
pr-url: https://github.com/nodejs/node/pull/41678
38073829
description: Passing an invalid callback to the `callback` argument
@@ -3829,6 +3851,8 @@ changes:
38293851
* `options` {Object}
38303852
* `bigint` {boolean} Whether the numeric values in the returned
38313853
{fs.Stats} object should be `bigint`. **Default:** `false`.
3854+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3855+
**Default:** `undefined`.
38323856
* `callback` {Function}
38333857
* `err` {Error}
38343858
* `stats` {fs.Stats}

β€Žlib/fs.jsβ€Ž

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const {
139139
const{
140140
isInt32,
141141
parseFileMode,
142+
validateAbortSignal,
142143
validateBoolean,
143144
validateBuffer,
144145
validateEncoding,
@@ -383,6 +384,25 @@ function checkAborted(signal, callback) {
383384
returnfalse;
384385
}
385386

387+
functionbindSignalToReq(req,signal,callback){
388+
if(!signal){
389+
req.oncomplete=callback;
390+
return;
391+
}
392+
letaborted=false;
393+
constonAbort=()=>{
394+
aborted=true;
395+
callback(newAbortError(undefined,{cause: signal.reason}));
396+
};
397+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
398+
signal.addEventListener('abort',onAbort,{__proto__: null,[kResistStopPropagation]: true});
399+
req.oncomplete=function(err,result){
400+
signal.removeEventListener('abort',onAbort);
401+
if(aborted)return;
402+
callback(err,result);
403+
};
404+
}
405+
386406
/**
387407
* Asynchronously reads the entire contents of a file.
388408
* @param {string | Buffer | URL | number} path
@@ -1954,7 +1974,7 @@ function readdirSync(path, options) {
19541974
* Invokes the callback with the `fs.Stats`
19551975
* for the file descriptor.
19561976
* @param {number} fd
1957-
* @param {{ bigint?: boolean; }} [options]
1977+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19581978
* @param {(
19591979
* err?: Error,
19601980
* stats?: Stats
@@ -1965,23 +1985,28 @@ function fstat(fd, options = { __proto__: null, bigint: false }, callback) {
19651985
if(typeofoptions==='function'){
19661986
callback=options;
19671987
options=kEmptyObject;
1988+
}elseif(options===null||typeofoptions!=='object'){
1989+
options=kEmptyObject;
19681990
}
19691991

19701992
consth=vfsState.handlers;
19711993
if(h!==null&&vfsResult(h.fstat(fd,options),callback))return;
19721994

19731995
callback=makeStatsCallback(callback);
19741996

1997+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
1998+
if(checkAborted(options.signal,callback))return;
1999+
19752000
constreq=newFSReqCallback(options.bigint);
1976-
req.oncomplete=callback;
2001+
bindSignalToReq(req,options.signal,callback);
19772002
binding.fstat(fd,options.bigint,req);
19782003
}
19792004

19802005
/**
19812006
* Retrieves the `fs.Stats` for the symbolic link
19822007
* referred to by the `path`.
19832008
* @param {string | Buffer | URL} path
1984-
* @param {{ bigint?: boolean; }} [options]
2009+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19852010
* @param {(
19862011
* err?: Error,
19872012
* stats?: Stats
@@ -1992,6 +2017,10 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
19922017
if(typeofoptions==='function'){
19932018
callback=options;
19942019
options=kEmptyObject;
2020+
}elseif(options===null||typeofoptions!=='object'){
2021+
options=kEmptyObject;
2022+
}else{
2023+
options=getOptions(options,{bigint: false});
19952024
}
19962025

19972026
consth=vfsState.handlers;
@@ -2005,8 +2034,11 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
20052034
return;
20062035
}
20072036

2037+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
2038+
if(checkAborted(options.signal,callback))return;
2039+
20082040
constreq=newFSReqCallback(options.bigint);
2009-
req.oncomplete=callback;
2041+
bindSignalToReq(req,options.signal,callback);
20102042
binding.lstat(path,options.bigint,req);
20112043
}
20122044

@@ -2036,11 +2068,12 @@ function stat(path, options = { __proto__: null, bigint: false, throwIfNoEntry:
20362068
callback=makeStatsCallback(callback);
20372069
path=getValidatedPath(path);
20382070

2071+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
20392072
if(checkAborted(options.signal,callback))return;
20402073

20412074
constreq=newFSReqCallback(options.bigint);
2042-
req.oncomplete=callback;
2043-
binding.stat(getValidatedPath(path),options.bigint,req,options.throwIfNoEntry);
2075+
bindSignalToReq(req,options.signal,callback);
2076+
binding.stat(path,options.bigint,req,options.throwIfNoEntry);
20442077
}
20452078

20462079
functionstatfs(path,options={__proto__: null,bigint: false},callback){

β€Žlib/internal/fs/promises.jsβ€Ž

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ const {
1212
PromisePrototypeThen,
1313
PromiseReject,
1414
PromiseResolve,
15+
PromiseWithResolvers,
1516
SafeArrayIterator,
1617
SafePromisePrototypeFinally,
18+
SafePromiseRace,
1719
Symbol,
1820
SymbolAsyncDispose,
1921
SymbolAsyncIterator,
@@ -1126,6 +1128,21 @@ function checkAborted(signal) {
11261128
thrownewAbortError(undefined,{cause: signal.reason});
11271129
}
11281130

1131+
functionrejectWithReason(signal,reject){
1132+
return()=>{
1133+
reject(newAbortError(undefined,{cause: signal.reason}));
1134+
};
1135+
}
1136+
1137+
asyncfunctionraceWithSignal(opPromise,signal){
1138+
if(!signal)returnopPromise;
1139+
const{promise: abortPromise, reject }=PromiseWithResolvers();
1140+
// eslint-disable-next-line no-unused-vars
1141+
using_=EventEmitter.addAbortListener(signal,
1142+
rejectWithReason(signal,reject));
1143+
returnawaitSafePromiseRace([opPromise,abortPromise]);
1144+
}
1145+
11291146
asyncfunctionwriteFileHandle(filehandle,data,signal,encoding){
11301147
checkAborted(signal);
11311148
if(isCustomIterable(data)){
@@ -1779,15 +1796,26 @@ async function symlink(target, path, type) {
17791796
}
17801797

17811798
asyncfunctionfstat(handle,options={__proto__: null,bigint: false}){
1782-
constresult=awaitPromisePrototypeThen(
1783-
binding.fstat(handle.fd,options.bigint,kUsePromises),
1784-
undefined,
1785-
handleErrorFromBinding,
1799+
validateObject(options,'options');
1800+
const{ signal }=options;
1801+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1802+
checkAborted(signal);
1803+
constresult=awaitraceWithSignal(
1804+
PromisePrototypeThen(
1805+
binding.fstat(handle.fd,options.bigint,kUsePromises),
1806+
undefined,
1807+
handleErrorFromBinding,
1808+
),
1809+
signal,
17861810
);
17871811
returngetStatsFromBinding(result);
17881812
}
17891813

17901814
asyncfunctionlstat(path,options={__proto__: null,bigint: false}){
1815+
validateObject(options,'options');
1816+
const{ signal }=options;
1817+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1818+
checkAborted(signal);
17911819
consth=vfsState.handlers;
17921820
if(h!==null){
17931821
constpromise=h.lstat(path,options);
@@ -1798,24 +1826,34 @@ async function lstat(path, options = { __proto__: null, bigint: false }) {
17981826
constresource=pathModule.toNamespacedPath(BufferIsBuffer(path) ? BufferToString(path) : path);
17991827
thrownewERR_ACCESS_DENIED('Access to this API has been restricted','FileSystemRead',resource);
18001828
}
1801-
constresult=awaitPromisePrototypeThen(
1802-
binding.lstat(path,options.bigint,kUsePromises),
1803-
undefined,
1804-
handleErrorFromBinding,
1829+
constresult=awaitraceWithSignal(
1830+
PromisePrototypeThen(
1831+
binding.lstat(path,options.bigint,kUsePromises),
1832+
undefined,
1833+
handleErrorFromBinding,
1834+
),
1835+
signal,
18051836
);
18061837
returngetStatsFromBinding(result);
18071838
}
18081839

18091840
asyncfunctionstat(path,options={__proto__: null,bigint: false,throwIfNoEntry: true}){
1841+
validateObject(options,'options');
1842+
const{ signal }=options;
1843+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1844+
checkAborted(signal);
18101845
consth=vfsState.handlers;
18111846
if(h!==null){
18121847
constpromise=h.stat(path,options);
18131848
if(promise!==undefined)returnawaitpromise;
18141849
}
1815-
constresult=awaitPromisePrototypeThen(
1816-
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1817-
undefined,
1818-
handleErrorFromBinding,
1850+
constresult=awaitraceWithSignal(
1851+
PromisePrototypeThen(
1852+
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1853+
undefined,
1854+
handleErrorFromBinding,
1855+
),
1856+
signal,
18191857
);
18201858

18211859
// Binding will resolve undefined if UV_ENOENT or UV_ENOTDIR and throwIfNoEntry is false

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 6673eba

Browse files
mertcanaltinaduh95
authored andcommitted
fs: abort in-flight stat operations
Signed-off-by: Mert Can Altin <mertgold60@gmail.com> PR-URL: #63143 Refs: #57775 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 74234ee commit 6673eba

4 files changed

Lines changed: 200 additions & 34 deletions

File tree

β€Ždoc/api/fs.mdβ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,10 @@ link(2) documentation for more detail.
15421542
<!-- YAML
15431543
added: v10.0.0
15441544
changes:
1545+
- version: REPLACEME
1546+
pr-url: https://github.com/nodejs/node/pull/63143
1547+
description: Accepts an additional `signal` option to allow aborting the
1548+
operation.
15451549
- version: v10.5.0
15461550
pr-url: https://github.com/nodejs/node/pull/20220
15471551
description: Accepts an additional `options` object to specify whether
@@ -1552,6 +1556,8 @@ changes:
15521556
* `options` {Object}
15531557
* `bigint` {boolean} Whether the numeric values in the returned
15541558
{fs.Stats} object should be `bigint`. **Default:** `false`.
1559+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
1560+
**Default:** `undefined`.
15551561
* Returns: {Promise} Fulfills with the {fs.Stats} object for the given
15561562
symbolic link `path`.
15571563
@@ -2080,6 +2086,10 @@ Removes files and directories (modeled on the standard POSIX `rm` utility).
20802086
<!-- YAML
20812087
added: v10.0.0
20822088
changes:
2089+
- version: REPLACEME
2090+
pr-url: https://github.com/nodejs/node/pull/63143
2091+
description: Accepts an additional `signal` option to allow aborting the
2092+
operation.
20832093
- version: v25.7.0
20842094
pr-url: https://github.com/nodejs/node/pull/61178
20852095
description: Accepts a `throwIfNoEntry` option to specify whether
@@ -2097,6 +2107,8 @@ changes:
20972107
* `throwIfNoEntry` {boolean} Whether an exception will be thrown
20982108
if no file system entry exists, rather than returning `undefined`.
20992109
**Default:** `true`.
2110+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
2111+
**Default:** `undefined`.
21002112
* Returns: {Promise} Fulfills with the {fs.Stats} object for the
21012113
given `path`.
21022114
@@ -3437,6 +3449,10 @@ exception are given to the completion callback.
34373449
<!-- YAML
34383450
added: v0.1.95
34393451
changes:
3452+
- version: REPLACEME
3453+
pr-url: https://github.com/nodejs/node/pull/63143
3454+
description: Accepts an additional `signal` option to allow aborting the
3455+
operation.
34403456
- version: v18.0.0
34413457
pr-url: https://github.com/nodejs/node/pull/41678
34423458
description: Passing an invalid callback to the `callback` argument
@@ -3460,6 +3476,8 @@ changes:
34603476
* `options` {Object}
34613477
* `bigint` {boolean} Whether the numeric values in the returned
34623478
{fs.Stats} object should be `bigint`. **Default:** `false`.
3479+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3480+
**Default:** `undefined`.
34633481
* `callback` {Function}
34643482
* `err` {Error}
34653483
* `stats` {fs.Stats}
@@ -3802,6 +3820,10 @@ exception are given to the completion callback.
38023820
<!-- YAML
38033821
added: v0.1.30
38043822
changes:
3823+
- version: REPLACEME
3824+
pr-url: https://github.com/nodejs/node/pull/63143
3825+
description: Accepts an additional `signal` option to allow aborting the
3826+
operation.
38053827
- version: v18.0.0
38063828
pr-url: https://github.com/nodejs/node/pull/41678
38073829
description: Passing an invalid callback to the `callback` argument
@@ -3829,6 +3851,8 @@ changes:
38293851
* `options` {Object}
38303852
* `bigint` {boolean} Whether the numeric values in the returned
38313853
{fs.Stats} object should be `bigint`. **Default:** `false`.
3854+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3855+
**Default:** `undefined`.
38323856
* `callback` {Function}
38333857
* `err` {Error}
38343858
* `stats` {fs.Stats}

β€Žlib/fs.jsβ€Ž

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const {
139139
const{
140140
isInt32,
141141
parseFileMode,
142+
validateAbortSignal,
142143
validateBoolean,
143144
validateBuffer,
144145
validateEncoding,
@@ -383,6 +384,25 @@ function checkAborted(signal, callback) {
383384
returnfalse;
384385
}
385386

387+
functionbindSignalToReq(req,signal,callback){
388+
if(!signal){
389+
req.oncomplete=callback;
390+
return;
391+
}
392+
letaborted=false;
393+
constonAbort=()=>{
394+
aborted=true;
395+
callback(newAbortError(undefined,{cause: signal.reason}));
396+
};
397+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
398+
signal.addEventListener('abort',onAbort,{__proto__: null,[kResistStopPropagation]: true});
399+
req.oncomplete=function(err,result){
400+
signal.removeEventListener('abort',onAbort);
401+
if(aborted)return;
402+
callback(err,result);
403+
};
404+
}
405+
386406
/**
387407
* Asynchronously reads the entire contents of a file.
388408
* @param {string | Buffer | URL | number} path
@@ -1954,7 +1974,7 @@ function readdirSync(path, options) {
19541974
* Invokes the callback with the `fs.Stats`
19551975
* for the file descriptor.
19561976
* @param {number} fd
1957-
* @param {{ bigint?: boolean; }} [options]
1977+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19581978
* @param {(
19591979
* err?: Error,
19601980
* stats?: Stats
@@ -1965,23 +1985,28 @@ function fstat(fd, options = { __proto__: null, bigint: false }, callback) {
19651985
if(typeofoptions==='function'){
19661986
callback=options;
19671987
options=kEmptyObject;
1988+
}elseif(options===null||typeofoptions!=='object'){
1989+
options=kEmptyObject;
19681990
}
19691991

19701992
consth=vfsState.handlers;
19711993
if(h!==null&&vfsResult(h.fstat(fd,options),callback))return;
19721994

19731995
callback=makeStatsCallback(callback);
19741996

1997+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
1998+
if(checkAborted(options.signal,callback))return;
1999+
19752000
constreq=newFSReqCallback(options.bigint);
1976-
req.oncomplete=callback;
2001+
bindSignalToReq(req,options.signal,callback);
19772002
binding.fstat(fd,options.bigint,req);
19782003
}
19792004

19802005
/**
19812006
* Retrieves the `fs.Stats` for the symbolic link
19822007
* referred to by the `path`.
19832008
* @param {string | Buffer | URL} path
1984-
* @param {{ bigint?: boolean; }} [options]
2009+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19852010
* @param {(
19862011
* err?: Error,
19872012
* stats?: Stats
@@ -1992,6 +2017,10 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
19922017
if(typeofoptions==='function'){
19932018
callback=options;
19942019
options=kEmptyObject;
2020+
}elseif(options===null||typeofoptions!=='object'){
2021+
options=kEmptyObject;
2022+
}else{
2023+
options=getOptions(options,{bigint: false});
19952024
}
19962025

19972026
consth=vfsState.handlers;
@@ -2005,8 +2034,11 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
20052034
return;
20062035
}
20072036

2037+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
2038+
if(checkAborted(options.signal,callback))return;
2039+
20082040
constreq=newFSReqCallback(options.bigint);
2009-
req.oncomplete=callback;
2041+
bindSignalToReq(req,options.signal,callback);
20102042
binding.lstat(path,options.bigint,req);
20112043
}
20122044

@@ -2036,11 +2068,12 @@ function stat(path, options = { __proto__: null, bigint: false, throwIfNoEntry:
20362068
callback=makeStatsCallback(callback);
20372069
path=getValidatedPath(path);
20382070

2071+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
20392072
if(checkAborted(options.signal,callback))return;
20402073

20412074
constreq=newFSReqCallback(options.bigint);
2042-
req.oncomplete=callback;
2043-
binding.stat(getValidatedPath(path),options.bigint,req,options.throwIfNoEntry);
2075+
bindSignalToReq(req,options.signal,callback);
2076+
binding.stat(path,options.bigint,req,options.throwIfNoEntry);
20442077
}
20452078

20462079
functionstatfs(path,options={__proto__: null,bigint: false},callback){

β€Žlib/internal/fs/promises.jsβ€Ž

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ const {
1212
PromisePrototypeThen,
1313
PromiseReject,
1414
PromiseResolve,
15+
PromiseWithResolvers,
1516
SafeArrayIterator,
1617
SafePromisePrototypeFinally,
18+
SafePromiseRace,
1719
Symbol,
1820
SymbolAsyncDispose,
1921
SymbolAsyncIterator,
@@ -1126,6 +1128,21 @@ function checkAborted(signal) {
11261128
thrownewAbortError(undefined,{cause: signal.reason});
11271129
}
11281130

1131+
functionrejectWithReason(signal,reject){
1132+
return()=>{
1133+
reject(newAbortError(undefined,{cause: signal.reason}));
1134+
};
1135+
}
1136+
1137+
asyncfunctionraceWithSignal(opPromise,signal){
1138+
if(!signal)returnopPromise;
1139+
const{promise: abortPromise, reject }=PromiseWithResolvers();
1140+
// eslint-disable-next-line no-unused-vars
1141+
using_=EventEmitter.addAbortListener(signal,
1142+
rejectWithReason(signal,reject));
1143+
returnawaitSafePromiseRace([opPromise,abortPromise]);
1144+
}
1145+
11291146
asyncfunctionwriteFileHandle(filehandle,data,signal,encoding){
11301147
checkAborted(signal);
11311148
if(isCustomIterable(data)){
@@ -1779,15 +1796,26 @@ async function symlink(target, path, type) {
17791796
}
17801797

17811798
asyncfunctionfstat(handle,options={__proto__: null,bigint: false}){
1782-
constresult=awaitPromisePrototypeThen(
1783-
binding.fstat(handle.fd,options.bigint,kUsePromises),
1784-
undefined,
1785-
handleErrorFromBinding,
1799+
validateObject(options,'options');
1800+
const{ signal }=options;
1801+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1802+
checkAborted(signal);
1803+
constresult=awaitraceWithSignal(
1804+
PromisePrototypeThen(
1805+
binding.fstat(handle.fd,options.bigint,kUsePromises),
1806+
undefined,
1807+
handleErrorFromBinding,
1808+
),
1809+
signal,
17861810
);
17871811
returngetStatsFromBinding(result);
17881812
}
17891813

17901814
asyncfunctionlstat(path,options={__proto__: null,bigint: false}){
1815+
validateObject(options,'options');
1816+
const{ signal }=options;
1817+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1818+
checkAborted(signal);
17911819
consth=vfsState.handlers;
17921820
if(h!==null){
17931821
constpromise=h.lstat(path,options);
@@ -1798,24 +1826,34 @@ async function lstat(path, options = { __proto__: null, bigint: false }) {
17981826
constresource=pathModule.toNamespacedPath(BufferIsBuffer(path) ? BufferToString(path) : path);
17991827
thrownewERR_ACCESS_DENIED('Access to this API has been restricted','FileSystemRead',resource);
18001828
}
1801-
constresult=awaitPromisePrototypeThen(
1802-
binding.lstat(path,options.bigint,kUsePromises),
1803-
undefined,
1804-
handleErrorFromBinding,
1829+
constresult=awaitraceWithSignal(
1830+
PromisePrototypeThen(
1831+
binding.lstat(path,options.bigint,kUsePromises),
1832+
undefined,
1833+
handleErrorFromBinding,
1834+
),
1835+
signal,
18051836
);
18061837
returngetStatsFromBinding(result);
18071838
}
18081839

18091840
asyncfunctionstat(path,options={__proto__: null,bigint: false,throwIfNoEntry: true}){
1841+
validateObject(options,'options');
1842+
const{ signal }=options;
1843+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1844+
checkAborted(signal);
18101845
consth=vfsState.handlers;
18111846
if(h!==null){
18121847
constpromise=h.stat(path,options);
18131848
if(promise!==undefined)returnawaitpromise;
18141849
}
1815-
constresult=awaitPromisePrototypeThen(
1816-
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1817-
undefined,
1818-
handleErrorFromBinding,
1850+
constresult=awaitraceWithSignal(
1851+
PromisePrototypeThen(
1852+
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1853+
undefined,
1854+
handleErrorFromBinding,
1855+
),
1856+
signal,
18191857
);
18201858

18211859
// Binding will resolve undefined if UV_ENOENT or UV_ENOTDIR and throwIfNoEntry is false

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 6673eba

Browse files
mertcanaltinaduh95
authored andcommitted
fs: abort in-flight stat operations
Signed-off-by: Mert Can Altin <mertgold60@gmail.com> PR-URL: #63143 Refs: #57775 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 74234ee commit 6673eba

4 files changed

Lines changed: 200 additions & 34 deletions

File tree

β€Ždoc/api/fs.mdβ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,10 @@ link(2) documentation for more detail.
15421542
<!-- YAML
15431543
added: v10.0.0
15441544
changes:
1545+
- version: REPLACEME
1546+
pr-url: https://github.com/nodejs/node/pull/63143
1547+
description: Accepts an additional `signal` option to allow aborting the
1548+
operation.
15451549
- version: v10.5.0
15461550
pr-url: https://github.com/nodejs/node/pull/20220
15471551
description: Accepts an additional `options` object to specify whether
@@ -1552,6 +1556,8 @@ changes:
15521556
* `options` {Object}
15531557
* `bigint` {boolean} Whether the numeric values in the returned
15541558
{fs.Stats} object should be `bigint`. **Default:** `false`.
1559+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
1560+
**Default:** `undefined`.
15551561
* Returns: {Promise} Fulfills with the {fs.Stats} object for the given
15561562
symbolic link `path`.
15571563
@@ -2080,6 +2086,10 @@ Removes files and directories (modeled on the standard POSIX `rm` utility).
20802086
<!-- YAML
20812087
added: v10.0.0
20822088
changes:
2089+
- version: REPLACEME
2090+
pr-url: https://github.com/nodejs/node/pull/63143
2091+
description: Accepts an additional `signal` option to allow aborting the
2092+
operation.
20832093
- version: v25.7.0
20842094
pr-url: https://github.com/nodejs/node/pull/61178
20852095
description: Accepts a `throwIfNoEntry` option to specify whether
@@ -2097,6 +2107,8 @@ changes:
20972107
* `throwIfNoEntry` {boolean} Whether an exception will be thrown
20982108
if no file system entry exists, rather than returning `undefined`.
20992109
**Default:** `true`.
2110+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
2111+
**Default:** `undefined`.
21002112
* Returns: {Promise} Fulfills with the {fs.Stats} object for the
21012113
given `path`.
21022114
@@ -3437,6 +3449,10 @@ exception are given to the completion callback.
34373449
<!-- YAML
34383450
added: v0.1.95
34393451
changes:
3452+
- version: REPLACEME
3453+
pr-url: https://github.com/nodejs/node/pull/63143
3454+
description: Accepts an additional `signal` option to allow aborting the
3455+
operation.
34403456
- version: v18.0.0
34413457
pr-url: https://github.com/nodejs/node/pull/41678
34423458
description: Passing an invalid callback to the `callback` argument
@@ -3460,6 +3476,8 @@ changes:
34603476
* `options` {Object}
34613477
* `bigint` {boolean} Whether the numeric values in the returned
34623478
{fs.Stats} object should be `bigint`. **Default:** `false`.
3479+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3480+
**Default:** `undefined`.
34633481
* `callback` {Function}
34643482
* `err` {Error}
34653483
* `stats` {fs.Stats}
@@ -3802,6 +3820,10 @@ exception are given to the completion callback.
38023820
<!-- YAML
38033821
added: v0.1.30
38043822
changes:
3823+
- version: REPLACEME
3824+
pr-url: https://github.com/nodejs/node/pull/63143
3825+
description: Accepts an additional `signal` option to allow aborting the
3826+
operation.
38053827
- version: v18.0.0
38063828
pr-url: https://github.com/nodejs/node/pull/41678
38073829
description: Passing an invalid callback to the `callback` argument
@@ -3829,6 +3851,8 @@ changes:
38293851
* `options` {Object}
38303852
* `bigint` {boolean} Whether the numeric values in the returned
38313853
{fs.Stats} object should be `bigint`. **Default:** `false`.
3854+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3855+
**Default:** `undefined`.
38323856
* `callback` {Function}
38333857
* `err` {Error}
38343858
* `stats` {fs.Stats}

β€Žlib/fs.jsβ€Ž

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const {
139139
const{
140140
isInt32,
141141
parseFileMode,
142+
validateAbortSignal,
142143
validateBoolean,
143144
validateBuffer,
144145
validateEncoding,
@@ -383,6 +384,25 @@ function checkAborted(signal, callback) {
383384
returnfalse;
384385
}
385386

387+
functionbindSignalToReq(req,signal,callback){
388+
if(!signal){
389+
req.oncomplete=callback;
390+
return;
391+
}
392+
letaborted=false;
393+
constonAbort=()=>{
394+
aborted=true;
395+
callback(newAbortError(undefined,{cause: signal.reason}));
396+
};
397+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
398+
signal.addEventListener('abort',onAbort,{__proto__: null,[kResistStopPropagation]: true});
399+
req.oncomplete=function(err,result){
400+
signal.removeEventListener('abort',onAbort);
401+
if(aborted)return;
402+
callback(err,result);
403+
};
404+
}
405+
386406
/**
387407
* Asynchronously reads the entire contents of a file.
388408
* @param {string | Buffer | URL | number} path
@@ -1954,7 +1974,7 @@ function readdirSync(path, options) {
19541974
* Invokes the callback with the `fs.Stats`
19551975
* for the file descriptor.
19561976
* @param {number} fd
1957-
* @param {{ bigint?: boolean; }} [options]
1977+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19581978
* @param {(
19591979
* err?: Error,
19601980
* stats?: Stats
@@ -1965,23 +1985,28 @@ function fstat(fd, options = { __proto__: null, bigint: false }, callback) {
19651985
if(typeofoptions==='function'){
19661986
callback=options;
19671987
options=kEmptyObject;
1988+
}elseif(options===null||typeofoptions!=='object'){
1989+
options=kEmptyObject;
19681990
}
19691991

19701992
consth=vfsState.handlers;
19711993
if(h!==null&&vfsResult(h.fstat(fd,options),callback))return;
19721994

19731995
callback=makeStatsCallback(callback);
19741996

1997+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
1998+
if(checkAborted(options.signal,callback))return;
1999+
19752000
constreq=newFSReqCallback(options.bigint);
1976-
req.oncomplete=callback;
2001+
bindSignalToReq(req,options.signal,callback);
19772002
binding.fstat(fd,options.bigint,req);
19782003
}
19792004

19802005
/**
19812006
* Retrieves the `fs.Stats` for the symbolic link
19822007
* referred to by the `path`.
19832008
* @param {string | Buffer | URL} path
1984-
* @param {{ bigint?: boolean; }} [options]
2009+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19852010
* @param {(
19862011
* err?: Error,
19872012
* stats?: Stats
@@ -1992,6 +2017,10 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
19922017
if(typeofoptions==='function'){
19932018
callback=options;
19942019
options=kEmptyObject;
2020+
}elseif(options===null||typeofoptions!=='object'){
2021+
options=kEmptyObject;
2022+
}else{
2023+
options=getOptions(options,{bigint: false});
19952024
}
19962025

19972026
consth=vfsState.handlers;
@@ -2005,8 +2034,11 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
20052034
return;
20062035
}
20072036

2037+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
2038+
if(checkAborted(options.signal,callback))return;
2039+
20082040
constreq=newFSReqCallback(options.bigint);
2009-
req.oncomplete=callback;
2041+
bindSignalToReq(req,options.signal,callback);
20102042
binding.lstat(path,options.bigint,req);
20112043
}
20122044

@@ -2036,11 +2068,12 @@ function stat(path, options = { __proto__: null, bigint: false, throwIfNoEntry:
20362068
callback=makeStatsCallback(callback);
20372069
path=getValidatedPath(path);
20382070

2071+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
20392072
if(checkAborted(options.signal,callback))return;
20402073

20412074
constreq=newFSReqCallback(options.bigint);
2042-
req.oncomplete=callback;
2043-
binding.stat(getValidatedPath(path),options.bigint,req,options.throwIfNoEntry);
2075+
bindSignalToReq(req,options.signal,callback);
2076+
binding.stat(path,options.bigint,req,options.throwIfNoEntry);
20442077
}
20452078

20462079
functionstatfs(path,options={__proto__: null,bigint: false},callback){

β€Žlib/internal/fs/promises.jsβ€Ž

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ const {
1212
PromisePrototypeThen,
1313
PromiseReject,
1414
PromiseResolve,
15+
PromiseWithResolvers,
1516
SafeArrayIterator,
1617
SafePromisePrototypeFinally,
18+
SafePromiseRace,
1719
Symbol,
1820
SymbolAsyncDispose,
1921
SymbolAsyncIterator,
@@ -1126,6 +1128,21 @@ function checkAborted(signal) {
11261128
thrownewAbortError(undefined,{cause: signal.reason});
11271129
}
11281130

1131+
functionrejectWithReason(signal,reject){
1132+
return()=>{
1133+
reject(newAbortError(undefined,{cause: signal.reason}));
1134+
};
1135+
}
1136+
1137+
asyncfunctionraceWithSignal(opPromise,signal){
1138+
if(!signal)returnopPromise;
1139+
const{promise: abortPromise, reject }=PromiseWithResolvers();
1140+
// eslint-disable-next-line no-unused-vars
1141+
using_=EventEmitter.addAbortListener(signal,
1142+
rejectWithReason(signal,reject));
1143+
returnawaitSafePromiseRace([opPromise,abortPromise]);
1144+
}
1145+
11291146
asyncfunctionwriteFileHandle(filehandle,data,signal,encoding){
11301147
checkAborted(signal);
11311148
if(isCustomIterable(data)){
@@ -1779,15 +1796,26 @@ async function symlink(target, path, type) {
17791796
}
17801797

17811798
asyncfunctionfstat(handle,options={__proto__: null,bigint: false}){
1782-
constresult=awaitPromisePrototypeThen(
1783-
binding.fstat(handle.fd,options.bigint,kUsePromises),
1784-
undefined,
1785-
handleErrorFromBinding,
1799+
validateObject(options,'options');
1800+
const{ signal }=options;
1801+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1802+
checkAborted(signal);
1803+
constresult=awaitraceWithSignal(
1804+
PromisePrototypeThen(
1805+
binding.fstat(handle.fd,options.bigint,kUsePromises),
1806+
undefined,
1807+
handleErrorFromBinding,
1808+
),
1809+
signal,
17861810
);
17871811
returngetStatsFromBinding(result);
17881812
}
17891813

17901814
asyncfunctionlstat(path,options={__proto__: null,bigint: false}){
1815+
validateObject(options,'options');
1816+
const{ signal }=options;
1817+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1818+
checkAborted(signal);
17911819
consth=vfsState.handlers;
17921820
if(h!==null){
17931821
constpromise=h.lstat(path,options);
@@ -1798,24 +1826,34 @@ async function lstat(path, options = { __proto__: null, bigint: false }) {
17981826
constresource=pathModule.toNamespacedPath(BufferIsBuffer(path) ? BufferToString(path) : path);
17991827
thrownewERR_ACCESS_DENIED('Access to this API has been restricted','FileSystemRead',resource);
18001828
}
1801-
constresult=awaitPromisePrototypeThen(
1802-
binding.lstat(path,options.bigint,kUsePromises),
1803-
undefined,
1804-
handleErrorFromBinding,
1829+
constresult=awaitraceWithSignal(
1830+
PromisePrototypeThen(
1831+
binding.lstat(path,options.bigint,kUsePromises),
1832+
undefined,
1833+
handleErrorFromBinding,
1834+
),
1835+
signal,
18051836
);
18061837
returngetStatsFromBinding(result);
18071838
}
18081839

18091840
asyncfunctionstat(path,options={__proto__: null,bigint: false,throwIfNoEntry: true}){
1841+
validateObject(options,'options');
1842+
const{ signal }=options;
1843+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1844+
checkAborted(signal);
18101845
consth=vfsState.handlers;
18111846
if(h!==null){
18121847
constpromise=h.stat(path,options);
18131848
if(promise!==undefined)returnawaitpromise;
18141849
}
1815-
constresult=awaitPromisePrototypeThen(
1816-
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1817-
undefined,
1818-
handleErrorFromBinding,
1850+
constresult=awaitraceWithSignal(
1851+
PromisePrototypeThen(
1852+
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1853+
undefined,
1854+
handleErrorFromBinding,
1855+
),
1856+
signal,
18191857
);
18201858

18211859
// Binding will resolve undefined if UV_ENOENT or UV_ENOTDIR and throwIfNoEntry is false

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit 6673eba

Browse files
mertcanaltinaduh95
authored andcommitted
fs: abort in-flight stat operations
Signed-off-by: Mert Can Altin <mertgold60@gmail.com> PR-URL: #63143 Refs: #57775 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 74234ee commit 6673eba

4 files changed

Lines changed: 200 additions & 34 deletions

File tree

β€Ždoc/api/fs.mdβ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,10 @@ link(2) documentation for more detail.
15421542
<!-- YAML
15431543
added: v10.0.0
15441544
changes:
1545+
- version: REPLACEME
1546+
pr-url: https://github.com/nodejs/node/pull/63143
1547+
description: Accepts an additional `signal` option to allow aborting the
1548+
operation.
15451549
- version: v10.5.0
15461550
pr-url: https://github.com/nodejs/node/pull/20220
15471551
description: Accepts an additional `options` object to specify whether
@@ -1552,6 +1556,8 @@ changes:
15521556
* `options` {Object}
15531557
* `bigint` {boolean} Whether the numeric values in the returned
15541558
{fs.Stats} object should be `bigint`. **Default:** `false`.
1559+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
1560+
**Default:** `undefined`.
15551561
* Returns: {Promise} Fulfills with the {fs.Stats} object for the given
15561562
symbolic link `path`.
15571563
@@ -2080,6 +2086,10 @@ Removes files and directories (modeled on the standard POSIX `rm` utility).
20802086
<!-- YAML
20812087
added: v10.0.0
20822088
changes:
2089+
- version: REPLACEME
2090+
pr-url: https://github.com/nodejs/node/pull/63143
2091+
description: Accepts an additional `signal` option to allow aborting the
2092+
operation.
20832093
- version: v25.7.0
20842094
pr-url: https://github.com/nodejs/node/pull/61178
20852095
description: Accepts a `throwIfNoEntry` option to specify whether
@@ -2097,6 +2107,8 @@ changes:
20972107
* `throwIfNoEntry` {boolean} Whether an exception will be thrown
20982108
if no file system entry exists, rather than returning `undefined`.
20992109
**Default:** `true`.
2110+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
2111+
**Default:** `undefined`.
21002112
* Returns: {Promise} Fulfills with the {fs.Stats} object for the
21012113
given `path`.
21022114
@@ -3437,6 +3449,10 @@ exception are given to the completion callback.
34373449
<!-- YAML
34383450
added: v0.1.95
34393451
changes:
3452+
- version: REPLACEME
3453+
pr-url: https://github.com/nodejs/node/pull/63143
3454+
description: Accepts an additional `signal` option to allow aborting the
3455+
operation.
34403456
- version: v18.0.0
34413457
pr-url: https://github.com/nodejs/node/pull/41678
34423458
description: Passing an invalid callback to the `callback` argument
@@ -3460,6 +3476,8 @@ changes:
34603476
* `options` {Object}
34613477
* `bigint` {boolean} Whether the numeric values in the returned
34623478
{fs.Stats} object should be `bigint`. **Default:** `false`.
3479+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3480+
**Default:** `undefined`.
34633481
* `callback` {Function}
34643482
* `err` {Error}
34653483
* `stats` {fs.Stats}
@@ -3802,6 +3820,10 @@ exception are given to the completion callback.
38023820
<!-- YAML
38033821
added: v0.1.30
38043822
changes:
3823+
- version: REPLACEME
3824+
pr-url: https://github.com/nodejs/node/pull/63143
3825+
description: Accepts an additional `signal` option to allow aborting the
3826+
operation.
38053827
- version: v18.0.0
38063828
pr-url: https://github.com/nodejs/node/pull/41678
38073829
description: Passing an invalid callback to the `callback` argument
@@ -3829,6 +3851,8 @@ changes:
38293851
* `options` {Object}
38303852
* `bigint` {boolean} Whether the numeric values in the returned
38313853
{fs.Stats} object should be `bigint`. **Default:** `false`.
3854+
* `signal` {AbortSignal} An AbortSignal to cancel the operation.
3855+
**Default:** `undefined`.
38323856
* `callback` {Function}
38333857
* `err` {Error}
38343858
* `stats` {fs.Stats}

β€Žlib/fs.jsβ€Ž

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const {
139139
const{
140140
isInt32,
141141
parseFileMode,
142+
validateAbortSignal,
142143
validateBoolean,
143144
validateBuffer,
144145
validateEncoding,
@@ -383,6 +384,25 @@ function checkAborted(signal, callback) {
383384
returnfalse;
384385
}
385386

387+
functionbindSignalToReq(req,signal,callback){
388+
if(!signal){
389+
req.oncomplete=callback;
390+
return;
391+
}
392+
letaborted=false;
393+
constonAbort=()=>{
394+
aborted=true;
395+
callback(newAbortError(undefined,{cause: signal.reason}));
396+
};
397+
kResistStopPropagation??=require('internal/event_target').kResistStopPropagation;
398+
signal.addEventListener('abort',onAbort,{__proto__: null,[kResistStopPropagation]: true});
399+
req.oncomplete=function(err,result){
400+
signal.removeEventListener('abort',onAbort);
401+
if(aborted)return;
402+
callback(err,result);
403+
};
404+
}
405+
386406
/**
387407
* Asynchronously reads the entire contents of a file.
388408
* @param {string | Buffer | URL | number} path
@@ -1954,7 +1974,7 @@ function readdirSync(path, options) {
19541974
* Invokes the callback with the `fs.Stats`
19551975
* for the file descriptor.
19561976
* @param {number} fd
1957-
* @param {{ bigint?: boolean; }} [options]
1977+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19581978
* @param {(
19591979
* err?: Error,
19601980
* stats?: Stats
@@ -1965,23 +1985,28 @@ function fstat(fd, options = { __proto__: null, bigint: false }, callback) {
19651985
if(typeofoptions==='function'){
19661986
callback=options;
19671987
options=kEmptyObject;
1988+
}elseif(options===null||typeofoptions!=='object'){
1989+
options=kEmptyObject;
19681990
}
19691991

19701992
consth=vfsState.handlers;
19711993
if(h!==null&&vfsResult(h.fstat(fd,options),callback))return;
19721994

19731995
callback=makeStatsCallback(callback);
19741996

1997+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
1998+
if(checkAborted(options.signal,callback))return;
1999+
19752000
constreq=newFSReqCallback(options.bigint);
1976-
req.oncomplete=callback;
2001+
bindSignalToReq(req,options.signal,callback);
19772002
binding.fstat(fd,options.bigint,req);
19782003
}
19792004

19802005
/**
19812006
* Retrieves the `fs.Stats` for the symbolic link
19822007
* referred to by the `path`.
19832008
* @param {string | Buffer | URL} path
1984-
* @param {{ bigint?: boolean; }} [options]
2009+
* @param {{ bigint?: boolean, signal?: AbortSignal }} [options]
19852010
* @param {(
19862011
* err?: Error,
19872012
* stats?: Stats
@@ -1992,6 +2017,10 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
19922017
if(typeofoptions==='function'){
19932018
callback=options;
19942019
options=kEmptyObject;
2020+
}elseif(options===null||typeofoptions!=='object'){
2021+
options=kEmptyObject;
2022+
}else{
2023+
options=getOptions(options,{bigint: false});
19952024
}
19962025

19972026
consth=vfsState.handlers;
@@ -2005,8 +2034,11 @@ function lstat(path, options = { __proto__: null, bigint: false }, callback) {
20052034
return;
20062035
}
20072036

2037+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
2038+
if(checkAborted(options.signal,callback))return;
2039+
20082040
constreq=newFSReqCallback(options.bigint);
2009-
req.oncomplete=callback;
2041+
bindSignalToReq(req,options.signal,callback);
20102042
binding.lstat(path,options.bigint,req);
20112043
}
20122044

@@ -2036,11 +2068,12 @@ function stat(path, options = { __proto__: null, bigint: false, throwIfNoEntry:
20362068
callback=makeStatsCallback(callback);
20372069
path=getValidatedPath(path);
20382070

2071+
if(options.signal!==undefined)validateAbortSignal(options.signal,'options.signal');
20392072
if(checkAborted(options.signal,callback))return;
20402073

20412074
constreq=newFSReqCallback(options.bigint);
2042-
req.oncomplete=callback;
2043-
binding.stat(getValidatedPath(path),options.bigint,req,options.throwIfNoEntry);
2075+
bindSignalToReq(req,options.signal,callback);
2076+
binding.stat(path,options.bigint,req,options.throwIfNoEntry);
20442077
}
20452078

20462079
functionstatfs(path,options={__proto__: null,bigint: false},callback){

β€Žlib/internal/fs/promises.jsβ€Ž

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ const {
1212
PromisePrototypeThen,
1313
PromiseReject,
1414
PromiseResolve,
15+
PromiseWithResolvers,
1516
SafeArrayIterator,
1617
SafePromisePrototypeFinally,
18+
SafePromiseRace,
1719
Symbol,
1820
SymbolAsyncDispose,
1921
SymbolAsyncIterator,
@@ -1126,6 +1128,21 @@ function checkAborted(signal) {
11261128
thrownewAbortError(undefined,{cause: signal.reason});
11271129
}
11281130

1131+
functionrejectWithReason(signal,reject){
1132+
return()=>{
1133+
reject(newAbortError(undefined,{cause: signal.reason}));
1134+
};
1135+
}
1136+
1137+
asyncfunctionraceWithSignal(opPromise,signal){
1138+
if(!signal)returnopPromise;
1139+
const{promise: abortPromise, reject }=PromiseWithResolvers();
1140+
// eslint-disable-next-line no-unused-vars
1141+
using_=EventEmitter.addAbortListener(signal,
1142+
rejectWithReason(signal,reject));
1143+
returnawaitSafePromiseRace([opPromise,abortPromise]);
1144+
}
1145+
11291146
asyncfunctionwriteFileHandle(filehandle,data,signal,encoding){
11301147
checkAborted(signal);
11311148
if(isCustomIterable(data)){
@@ -1779,15 +1796,26 @@ async function symlink(target, path, type) {
17791796
}
17801797

17811798
asyncfunctionfstat(handle,options={__proto__: null,bigint: false}){
1782-
constresult=awaitPromisePrototypeThen(
1783-
binding.fstat(handle.fd,options.bigint,kUsePromises),
1784-
undefined,
1785-
handleErrorFromBinding,
1799+
validateObject(options,'options');
1800+
const{ signal }=options;
1801+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1802+
checkAborted(signal);
1803+
constresult=awaitraceWithSignal(
1804+
PromisePrototypeThen(
1805+
binding.fstat(handle.fd,options.bigint,kUsePromises),
1806+
undefined,
1807+
handleErrorFromBinding,
1808+
),
1809+
signal,
17861810
);
17871811
returngetStatsFromBinding(result);
17881812
}
17891813

17901814
asyncfunctionlstat(path,options={__proto__: null,bigint: false}){
1815+
validateObject(options,'options');
1816+
const{ signal }=options;
1817+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1818+
checkAborted(signal);
17911819
consth=vfsState.handlers;
17921820
if(h!==null){
17931821
constpromise=h.lstat(path,options);
@@ -1798,24 +1826,34 @@ async function lstat(path, options = { __proto__: null, bigint: false }) {
17981826
constresource=pathModule.toNamespacedPath(BufferIsBuffer(path) ? BufferToString(path) : path);
17991827
thrownewERR_ACCESS_DENIED('Access to this API has been restricted','FileSystemRead',resource);
18001828
}
1801-
constresult=awaitPromisePrototypeThen(
1802-
binding.lstat(path,options.bigint,kUsePromises),
1803-
undefined,
1804-
handleErrorFromBinding,
1829+
constresult=awaitraceWithSignal(
1830+
PromisePrototypeThen(
1831+
binding.lstat(path,options.bigint,kUsePromises),
1832+
undefined,
1833+
handleErrorFromBinding,
1834+
),
1835+
signal,
18051836
);
18061837
returngetStatsFromBinding(result);
18071838
}
18081839

18091840
asyncfunctionstat(path,options={__proto__: null,bigint: false,throwIfNoEntry: true}){
1841+
validateObject(options,'options');
1842+
const{ signal }=options;
1843+
if(signal!==undefined)validateAbortSignal(signal,'options.signal');
1844+
checkAborted(signal);
18101845
consth=vfsState.handlers;
18111846
if(h!==null){
18121847
constpromise=h.stat(path,options);
18131848
if(promise!==undefined)returnawaitpromise;
18141849
}
1815-
constresult=awaitPromisePrototypeThen(
1816-
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1817-
undefined,
1818-
handleErrorFromBinding,
1850+
constresult=awaitraceWithSignal(
1851+
PromisePrototypeThen(
1852+
binding.stat(getValidatedPath(path),options.bigint,kUsePromises,options.throwIfNoEntry),
1853+
undefined,
1854+
handleErrorFromBinding,
1855+
),
1856+
signal,
18191857
);
18201858

18211859
// Binding will resolve undefined if UV_ENOENT or UV_ENOTDIR and throwIfNoEntry is false

0 commit comments

Comments
Β (0)