@@ -1894,6 +1894,64 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
18941894 }
18951895}
18961896
1897+ static void ReadFileSync (const FunctionCallbackInfo<Value>& args) {
1898+ Environment* env = Environment::GetCurrent (args);
1899+
1900+ CHECK_GE (args.Length (), 2 );
1901+
1902+ BufferValue path (env->isolate (), args[0 ]);
1903+ CHECK_NOT_NULL (*path);
1904+
1905+ CHECK (args[1 ]->IsInt32 ());
1906+ const int flags = args[1 ].As <Int32>()->Value ();
1907+
1908+ uv_fs_t req;
1909+ auto defer_req_cleanup = OnScopeLeave ([&req]() { uv_fs_req_cleanup (&req); });
1910+
1911+ FS_SYNC_TRACE_BEGIN (open);
1912+ uv_file file = uv_fs_open (nullptr , &req, *path, flags, 438 , nullptr );
1913+ FS_SYNC_TRACE_END (open);
1914+ if (req.result < 0 ) {
1915+ // req will be cleaned up by scope leave.
1916+ return args.GetReturnValue ().Set (
1917+ v8::Integer::New (env->isolate (), req.result ));
1918+ }
1919+ uv_fs_req_cleanup (&req);
1920+
1921+ auto defer_close = OnScopeLeave ([file]() {
1922+ uv_fs_t close_req;
1923+ CHECK_EQ (0 , uv_fs_close (nullptr , &close_req, file, nullptr ));
1924+ uv_fs_req_cleanup (&close_req);
1925+ });
1926+
1927+ std::string result{};
1928+ char buffer[8192 ];
1929+ uv_buf_t buf = uv_buf_init (buffer, sizeof (buffer));
1930+
1931+ FS_SYNC_TRACE_BEGIN (read);
1932+ while (true ) {
1933+ auto r = uv_fs_read (nullptr , &req, file, &buf, 1 , result.length (), nullptr );
1934+ if (req.result < 0 ) {
1935+ FS_SYNC_TRACE_END (read);
1936+ // req will be cleaned up by scope leave.
1937+ return args.GetReturnValue ().Set (
1938+ v8::Integer::New (env->isolate (), req.result ));
1939+ }
1940+ uv_fs_req_cleanup (&req);
1941+ if (r <= 0 ) {
1942+ break ;
1943+ }
1944+ result.append (buf.base , r);
1945+ }
1946+ FS_SYNC_TRACE_END (read);
1947+
1948+ args.GetReturnValue ().Set (String::NewFromUtf8 (env->isolate (),
1949+ result.data (),
1950+ v8::NewStringType::kNormal ,
1951+ result.size ())
1952+ .ToLocalChecked ());
1953+ }
1954+
18971955static void Open (const FunctionCallbackInfo<Value>& args) {
18981956 Environment* env = Environment::GetCurrent (args);
18991957
@@ -2720,6 +2778,8 @@ void Initialize(Local<Object> target,
27202778 SetMethod (context, target, " realpath" , RealPath);
27212779 SetMethod (context, target, " copyFile" , CopyFile);
27222780
2781+ SetMethodNoSideEffect (context, target, " readFileSync" , ReadFileSync);
2782+
27232783 SetMethod (context, target, " chmod" , Chmod);
27242784 SetMethod (context, target, " fchmod" , FChmod);
27252785
@@ -2730,7 +2790,6 @@ void Initialize(Local<Object> target,
27302790 SetMethod (context, target, " utimes" , UTimes);
27312791 SetMethod (context, target, " futimes" , FUTimes);
27322792 SetMethod (context, target, " lutimes" , LUTimes);
2733-
27342793 SetMethod (context, target, " mkdtemp" , Mkdtemp);
27352794
27362795 target
@@ -2826,6 +2885,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
28262885 registry->Register (Stat);
28272886 registry->Register (LStat);
28282887 registry->Register (FStat);
2888+ registry->Register (ReadFileSync);
28292889 registry->Register (StatFs);
28302890 registry->Register (Link);
28312891 registry->Register (Symlink);
0 commit comments