GeezerVison is showing github.com — fs: improve error performance of `realpathSync` · nodejs/node@36df275
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2750,10 +2750,10 @@ function realpathSync(p, options) { | 2750 | 2750 | realpathSync.native = (path, options) => { | 2751 | 2751 | options = getOptions(options); | 2752 | 2752 | path = getValidatedPath(path); | 2753 | | - const ctx = { path }; | 2754 | | - const result = binding.realpath(pathModule.toNamespacedPath(path), options.encoding, undefined, ctx); | 2755 | | - handleErrorFromBinding(ctx); | 2756 | | - return result; | | 2753 | + return binding.realpath( | | 2754 | + pathModule.toNamespacedPath(path), | | 2755 | + options.encoding, | | 2756 | + ); | 2757 | 2757 | }; | 2758 | 2758 |
| 2759 | 2759 | /** | |
Collapse file Copy file name to clipboardExpand all lines: src/node_file.cc +8-10Lines changed: 8 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1863,28 +1863,27 @@ static void RealPath(const FunctionCallbackInfo<Value>& args) { | 1863 | 1863 | Isolate* isolate = env->isolate(); | 1864 | 1864 |
| 1865 | 1865 | const int argc = args.Length(); | 1866 | | - CHECK_GE(argc, 3); | | 1866 | + CHECK_GE(argc, 2); | 1867 | 1867 |
| 1868 | 1868 | BufferValue path(isolate, args[0]); | 1869 | 1869 | CHECK_NOT_NULL(*path); | 1870 | 1870 |
| 1871 | 1871 | const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); | 1872 | 1872 |
| 1873 | | - FSReqBase* req_wrap_async = GetReqWrap(args, 2); | 1874 | | - if (req_wrap_async != nullptr) { // realpath(path, encoding, req) | | 1873 | + if (argc > 2) { // realpath(path, encoding, req) | | 1874 | + FSReqBase* req_wrap_async = GetReqWrap(args, 2); | 1875 | 1875 | FS_ASYNC_TRACE_BEGIN1( | 1876 | 1876 | UV_FS_REALPATH, req_wrap_async, "path", TRACE_STR_COPY(*path)) | 1877 | 1877 | AsyncCall(env, req_wrap_async, args, "realpath", encoding, AfterStringPtr, | 1878 | 1878 | uv_fs_realpath, *path); | 1879 | 1879 | } else { // realpath(path, encoding, undefined, ctx) | 1880 | | - CHECK_EQ(argc, 4); | 1881 | | - FSReqWrapSync req_wrap_sync; | | 1880 | + FSReqWrapSync req_wrap_sync("realpath", *path); | 1882 | 1881 | FS_SYNC_TRACE_BEGIN(realpath); | 1883 | | - int err = SyncCall(env, args[3], &req_wrap_sync, "realpath", | 1884 | | - uv_fs_realpath, *path); | | 1882 | + int err = | | 1883 | + SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_realpath, *path); | 1885 | 1884 | FS_SYNC_TRACE_END(realpath); | 1886 | 1885 | if (err < 0) { | 1887 | | - return; // syscall failed, no need to continue, error info is in ctx | | 1886 | + return; | 1888 | 1887 | } | 1889 | 1888 |
| 1890 | 1889 | const char* link_path = static_cast<const char*>(req_wrap_sync.req.ptr); | @@ -1895,8 +1894,7 @@ static void RealPath(const FunctionCallbackInfo<Value>& args) { | 1895 | 1894 | encoding, | 1896 | 1895 | &error); | 1897 | 1896 | if (rc.IsEmpty()) { | 1898 | | - Local<Object> ctx = args[3].As<Object>(); | 1899 | | - ctx->Set(env->context(), env->error_string(), error).Check(); | | 1897 | + env->isolate()->ThrowException(error); | 1900 | 1898 | return; | 1901 | 1899 | } | 1902 | 1900 |
| |
Collapse file Copy file name to clipboardExpand all lines: typings/internalBinding/fs.d.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -184,6 +184,7 @@ declare namespace InternalFSBinding { | 184 | 184 | function realpath(path: StringOrBuffer, encoding: unknown, req: FSReqCallback<string | Buffer>): void; | 185 | 185 | function realpath(path: StringOrBuffer, encoding: unknown, req: undefined, ctx: FSSyncContext): string | Buffer; | 186 | 186 | function realpath(path: StringOrBuffer, encoding: unknown, usePromises: typeof kUsePromises): Promise<string | Buffer>; | | 187 | + function realpath(path: StringOrBuffer, encoding: unknown): StringOrBuffer; | 187 | 188 |
| 188 | 189 | function rename(oldPath: string, newPath: string, req: FSReqCallback): void; | 189 | 190 | function rename(oldPath: string, newPath: string, req: undefined, ctx: FSSyncContext): void; | |
|