Skip to content

Commit d3771c4

Browse files
anonrigpull[bot]
authored andcommitted
fs: improve error performance of realpathSync
PR-URL: #49962 Refs: nodejs/performance#106 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
1 parent b9e2c35 commit d3771c4

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

lib/fs.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2752,10 +2752,10 @@ function realpathSync(p, options) {
27522752
realpathSync.native = (path, options) => {
27532753
options = getOptions(options);
27542754
path = getValidatedPath(path);
2755-
const ctx = { path };
2756-
const result = binding.realpath(pathModule.toNamespacedPath(path), options.encoding, undefined, ctx);
2757-
handleErrorFromBinding(ctx);
2758-
return result;
2755+
return binding.realpath(
2756+
pathModule.toNamespacedPath(path),
2757+
options.encoding,
2758+
);
27592759
};
27602760

27612761
/**

src/node_file.cc

+8-10
Original file line numberDiff line numberDiff line change
@@ -1828,28 +1828,27 @@ static void RealPath(const FunctionCallbackInfo<Value>& args) {
18281828
Isolate* isolate = env->isolate();
18291829

18301830
const int argc = args.Length();
1831-
CHECK_GE(argc, 3);
1831+
CHECK_GE(argc, 2);
18321832

18331833
BufferValue path(isolate, args[0]);
18341834
CHECK_NOT_NULL(*path);
18351835

18361836
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
18371837

1838-
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
1839-
if (req_wrap_async != nullptr) { // realpath(path, encoding, req)
1838+
if (argc > 2) { // realpath(path, encoding, req)
1839+
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
18401840
FS_ASYNC_TRACE_BEGIN1(
18411841
UV_FS_REALPATH, req_wrap_async, "path", TRACE_STR_COPY(*path))
18421842
AsyncCall(env, req_wrap_async, args, "realpath", encoding, AfterStringPtr,
18431843
uv_fs_realpath, *path);
18441844
} else { // realpath(path, encoding, undefined, ctx)
1845-
CHECK_EQ(argc, 4);
1846-
FSReqWrapSync req_wrap_sync;
1845+
FSReqWrapSync req_wrap_sync("realpath", *path);
18471846
FS_SYNC_TRACE_BEGIN(realpath);
1848-
int err = SyncCall(env, args[3], &req_wrap_sync, "realpath",
1849-
uv_fs_realpath, *path);
1847+
int err =
1848+
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_realpath, *path);
18501849
FS_SYNC_TRACE_END(realpath);
18511850
if (err < 0) {
1852-
return; // syscall failed, no need to continue, error info is in ctx
1851+
return;
18531852
}
18541853

18551854
const char* link_path = static_cast<const char*>(req_wrap_sync.req.ptr);
@@ -1860,8 +1859,7 @@ static void RealPath(const FunctionCallbackInfo<Value>& args) {
18601859
encoding,
18611860
&error);
18621861
if (rc.IsEmpty()) {
1863-
Local<Object> ctx = args[3].As<Object>();
1864-
ctx->Set(env->context(), env->error_string(), error).Check();
1862+
env->isolate()->ThrowException(error);
18651863
return;
18661864
}
18671865

typings/internalBinding/fs.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ declare namespace InternalFSBinding {
184184
function realpath(path: StringOrBuffer, encoding: unknown, req: FSReqCallback<string | Buffer>): void;
185185
function realpath(path: StringOrBuffer, encoding: unknown, req: undefined, ctx: FSSyncContext): string | Buffer;
186186
function realpath(path: StringOrBuffer, encoding: unknown, usePromises: typeof kUsePromises): Promise<string | Buffer>;
187+
function realpath(path: StringOrBuffer, encoding: unknown): StringOrBuffer;
187188

188189
function rename(oldPath: string, newPath: string, req: FSReqCallback): void;
189190
function rename(oldPath: string, newPath: string, req: undefined, ctx: FSSyncContext): void;

0 commit comments

Comments
 (0)