diff --git a/src/node_file.cc b/src/node_file.cc index 4b11a69f4d..21297acbcf 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -777,43 +777,6 @@ void AfterScanDir(uv_fs_t* req) { FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); - if (!after.Proceed()) { - return; - } - Environment* env = req_wrap->env(); - Local error; - int r; - std::vector> name_v; - - for (;;) { - uv_dirent_t ent; - - r = uv_fs_scandir_next(req, &ent); - if (r == UV_EOF) - break; - if (r != 0) { - return req_wrap->Reject(UVException( - env->isolate(), r, nullptr, req_wrap->syscall(), req->path)); - } - - MaybeLocal filename = - StringBytes::Encode(env->isolate(), - ent.name, - req_wrap->encoding(), - &error); - if (filename.IsEmpty()) - return req_wrap->Reject(error); - - name_v.push_back(filename.ToLocalChecked()); - } - - req_wrap->Resolve(Array::New(env->isolate(), name_v.data(), name_v.size())); -} - -void AfterScanDirWithTypes(uv_fs_t* req) { - FSReqBase* req_wrap = FSReqBase::from_req(req); - FSReqAfterScope after(req_wrap, req); - if (!after.Proceed()) { return; } @@ -826,6 +789,8 @@ void AfterScanDirWithTypes(uv_fs_t* req) { std::vector> name_v; std::vector> type_v; + const bool with_file_types = req_wrap->with_file_types(); + for (;;) { uv_dirent_t ent; @@ -837,23 +802,23 @@ void AfterScanDirWithTypes(uv_fs_t* req) { UVException(isolate, r, nullptr, req_wrap->syscall(), req->path)); } - MaybeLocal filename = - StringBytes::Encode(isolate, - ent.name, - req_wrap->encoding(), - &error); - if (filename.IsEmpty()) + Local filename; + if (!StringBytes::Encode(isolate, ent.name, req_wrap->encoding(), &error) + .ToLocal(&filename)) { return req_wrap->Reject(error); + } + name_v.push_back(filename); - name_v.push_back(filename.ToLocalChecked()); - type_v.emplace_back(Integer::New(isolate, ent.type)); + if (with_file_types) type_v.emplace_back(Integer::New(isolate, ent.type)); } - Local result[] = { - Array::New(isolate, name_v.data(), name_v.size()), - Array::New(isolate, type_v.data(), type_v.size()) - }; - req_wrap->Resolve(Array::New(isolate, result, arraysize(result))); + if (with_file_types) { + Local result[] = {Array::New(isolate, name_v.data(), name_v.size()), + Array::New(isolate, type_v.data(), type_v.size())}; + req_wrap->Resolve(Array::New(isolate, result, arraysize(result))); + } else { + req_wrap->Resolve(Array::New(isolate, name_v.data(), name_v.size())); + } } void Access(const FunctionCallbackInfo& args) { @@ -1650,13 +1615,16 @@ static void ReadDir(const FunctionCallbackInfo& args) { FSReqBase* req_wrap_async = GetReqWrap(args, 3); if (req_wrap_async != nullptr) { // readdir(path, encoding, withTypes, req) - if (with_types) { - AsyncCall(env, req_wrap_async, args, "scandir", encoding, - AfterScanDirWithTypes, uv_fs_scandir, *path, 0 /*flags*/); - } else { - AsyncCall(env, req_wrap_async, args, "scandir", encoding, - AfterScanDir, uv_fs_scandir, *path, 0 /*flags*/); - } + req_wrap_async->set_with_file_types(with_types); + AsyncCall(env, + req_wrap_async, + args, + "scandir", + encoding, + AfterScanDir, + uv_fs_scandir, + *path, + 0 /*flags*/); } else { // readdir(path, encoding, withTypes, undefined, ctx) CHECK_EQ(argc, 5); FSReqWrapSync req_wrap_sync; diff --git a/src/node_file.h b/src/node_file.h index cf98c5c933..482446eb62 100644 --- a/src/node_file.h +++ b/src/node_file.h @@ -89,8 +89,10 @@ class FSReqBase : public ReqWrap { enum encoding encoding() const { return encoding_; } bool use_bigint() const { return use_bigint_; } bool is_plain_open() const { return is_plain_open_; } + bool with_file_types() const { return with_file_types_; } void set_is_plain_open(bool value) { is_plain_open_ = value; } + void set_with_file_types(bool value) { with_file_types_ = value; } FSContinuationData* continuation_data() const { return continuation_data_.get(); @@ -116,6 +118,7 @@ class FSReqBase : public ReqWrap { bool has_data_ = false; bool use_bigint_ = false; bool is_plain_open_ = false; + bool with_file_types_ = false; const char* syscall_ = nullptr; BaseObjectPtr binding_data_;