Skip to content

Commit 35d459f

Browse files
committed
code cleaner
1 parent 3ef8498 commit 35d459f

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

lib/fs.js

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,47 +1630,46 @@ function lstat(path, options = { bigint: false }, callback) {
16301630
function stat(path, options, callback) {
16311631
if (typeof options === 'function') {
16321632
callback = options;
1633-
options = kEmptyObject;
1634-
} else if (options === null || typeof options !== 'object') {
1635-
options = kEmptyObject;
1636-
} else {
1637-
options = getOptions(options, { __proto__: null, bigint: false, signal: undefined });
1633+
options = {};
16381634
}
16391635

1636+
if (options === null || typeof options !== 'object') {
1637+
options = {};
1638+
}
1639+
1640+
options = {
1641+
bigint: false,
1642+
signal: undefined,
1643+
...options
1644+
};
1645+
16401646
if (typeof callback !== 'function') {
16411647
throw new ERR_INVALID_ARG_TYPE('callback', 'Function', callback);
16421648
}
16431649

1644-
const req = new FSReqCallback(options.bigint);
1645-
16461650
if (options.signal?.aborted) {
1647-
const abortErr = new AbortError('The operation was aborted', { __proto__: null, cause: options.signal.reason });
1648-
return process.nextTick(() => callback(abortErr));
1651+
process.nextTick(() => {
1652+
callback(new AbortError(undefined, { cause: options.signal.reason }));
1653+
});
1654+
return;
16491655
}
16501656

1651-
let aborted = false;
1652-
const onAbort = () => {
1653-
aborted = true;
1654-
callback(new AbortError(undefined, { __proto__: null, cause: options.signal.reason }));
1655-
};
1657+
const req = new FSReqCallback(options.bigint, callback);
16561658

16571659
if (options.signal) {
1658-
options.signal.addEventListener('abort', onAbort, { __proto__: null, once: true });
1659-
}
1660-
1661-
// TODO(mertcanaltin): Refactor callback handling to avoid double-wrapping and redundant validations.
1662-
// The current implementation validates and wraps the callback twice in the happy path; optimize when possible.
1663-
req.oncomplete = (err, stats) => {
1664-
if (options.signal) {
1665-
options.signal.removeEventListener('abort', onAbort);
1666-
}
1667-
1668-
if (aborted) return;
1660+
const onAbort = () => {
1661+
req.close();
1662+
callback(new AbortError(undefined, { cause: options.signal.reason }));
1663+
};
16691664

1670-
const wrappedCallback = makeStatsCallback(callback);
1665+
options.signal.addEventListener('abort', onAbort, { once: true });
16711666

1672-
wrappedCallback(err, stats);
1673-
};
1667+
const originalCallback = req.callback;
1668+
req.callback = (err, stats) => {
1669+
options.signal.removeEventListener('abort', onAbort);
1670+
originalCallback(err, stats);
1671+
};
1672+
}
16741673

16751674
binding.stat(getValidatedPath(path), options.bigint, req);
16761675
}

0 commit comments

Comments
 (0)