Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 1 addition & 34 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -510,29 +510,6 @@ inline bool ShouldBeTreatedAsRelativeOrAbsolutePath(
return false;
}

std::string ReadFile(uv_file file) {
std::string contents;
uv_fs_t req;
char buffer_memory[4096];
uv_buf_t buf = uv_buf_init(buffer_memory, sizeof(buffer_memory));

do {
const int r = uv_fs_read(nullptr,
&req,
file,
&buf,
1,
contents.length(), // offset
nullptr);
uv_fs_req_cleanup(&req);

if (r <= 0)
break;
contents.append(buf.base, r);
} while (true);
return contents;
}

enum DescriptorType {
FILE,
DIRECTORY,
Expand Down Expand Up @@ -583,16 +560,6 @@ DescriptorType CheckDescriptorAtPath(const std::string& path) {
return type;
}

Maybe<std::string> ReadIfFile(const std::string& path) {
Maybe<uv_file> fd = OpenDescriptor(path);
if (fd.IsNothing()) return Nothing<std::string>();
DescriptorType type = CheckDescriptorAtFile(fd.FromJust());
if (type != FILE) return Nothing<std::string>();
std::string source = ReadFile(fd.FromJust());
CloseDescriptor(fd.FromJust());
return Just(source);
}

using Exists = PackageConfig::Exists;
using IsValid = PackageConfig::IsValid;
using HasMain = PackageConfig::HasMain;
Expand All @@ -614,7 +581,7 @@ Maybe<const PackageConfig*> GetPackageConfig(Environment* env,
return Just(pcfg);
}

Maybe<std::string> source = ReadIfFile(path);
Maybe<std::string> source = ReadFileSync(path);

if (source.IsNothing()) {
auto entry = env->package_json_cache.emplace(path,
Expand Down
44 changes: 5 additions & 39 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -812,56 +812,22 @@ void Close(const FunctionCallbackInfo<Value>& args) {
static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
uv_loop_t* loop = env->event_loop();

CHECK(args[0]->IsString());
node::Utf8Value path(isolate, args[0]);

if (strlen(*path) != path.length())
return; // Contains a nul byte.

uv_fs_t open_req;
const int fd = uv_fs_open(loop, &open_req, *path, O_RDONLY, 0, nullptr);
uv_fs_req_cleanup(&open_req);

if (fd < 0) {
std::string chars;
const ssize_t nread = ReadFileSyncRaw(*path, path.length(), &chars);
if (nread < 0) {
return;
}

std::shared_ptr<void> defer_close(nullptr, [fd, loop] (...) {
uv_fs_t close_req;
CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr));
uv_fs_req_cleanup(&close_req);
});

const size_t kBlockSize = 32 << 10;
std::vector<char> chars;
int64_t offset = 0;
ssize_t numchars;
do {
const size_t start = chars.size();
chars.resize(start + kBlockSize);

uv_buf_t buf;
buf.base = &chars[start];
buf.len = kBlockSize;

uv_fs_t read_req;
numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr);
uv_fs_req_cleanup(&read_req);

if (numchars < 0)
return;

offset += numchars;
} while (static_cast<size_t>(numchars) == kBlockSize);

size_t start = 0;
if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
if (nread >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
start = 3; // Skip UTF-8 BOM.
}

const size_t size = offset - start;
const size_t size = nread - start;
char* p = &chars[start];
char* pe = &chars[size];
char* pos[2];
Expand Down
29 changes: 2 additions & 27 deletions src/node_native_module.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "node_native_module.h"
#include "util-inl.h"
#include "node_internals.h"

namespace node {
namespace native_module {
Expand Down Expand Up @@ -195,33 +196,7 @@ MaybeLocal<String> NativeModuleLoader::LoadBuiltinModuleSource(Isolate* isolate,
const char* id) {
#ifdef NODE_BUILTIN_MODULES_PATH
std::string filename = OnDiskFileName(id);

uv_fs_t req;
uv_file file =
uv_fs_open(nullptr, &req, filename.c_str(), O_RDONLY, 0, nullptr);
CHECK_GE(req.result, 0);
uv_fs_req_cleanup(&req);

std::shared_ptr<void> defer_close(nullptr, [file](...) {
uv_fs_t close_req;
CHECK_EQ(0, uv_fs_close(nullptr, &close_req, file, nullptr));
uv_fs_req_cleanup(&close_req);
});

std::string contents;
char buffer[4096];
uv_buf_t buf = uv_buf_init(buffer, sizeof(buffer));

while (true) {
const int r =
uv_fs_read(nullptr, &req, file, &buf, 1, contents.length(), nullptr);
CHECK_GE(req.result, 0);
uv_fs_req_cleanup(&req);
if (r <= 0) {
break;
}
contents.append(buf.base, r);
}
std::string contents = ReadFileSync(filename).FromJust();

return String::NewFromUtf8(
isolate, contents.c_str(), v8::NewStringType::kNormal, contents.length());
Expand Down
59 changes: 59 additions & 0 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ namespace node {

using v8::ArrayBufferView;
using v8::Isolate;
using v8::Just;
using v8::Local;
using v8::Maybe;
using v8::Nothing;
using v8::String;
using v8::Value;

Expand Down Expand Up @@ -218,6 +221,62 @@ int WriteFileSync(v8::Isolate* isolate,
return WriteFileSync(path, buf);
}

ssize_t ReadFileSyncRaw(
const char* path, size_t path_len, std::string* contents) {
if (strlen(path) != path_len) {
return UV_EINVAL; // Contains a nul byte.
}

uv_fs_t req;

const int fd = uv_fs_open(nullptr, &req, path, O_RDONLY, 0, nullptr);
uv_fs_req_cleanup(&req);
if (fd < 0) {
return static_cast<ssize_t>(fd);
}

std::shared_ptr<void> defer_close(nullptr, [fd] (...) {
uv_fs_t close_req;
CHECK_EQ(0, uv_fs_close(nullptr, &close_req, fd, nullptr));
uv_fs_req_cleanup(&close_req);
});

const size_t kBlockSize = 32 << 10;
size_t offset = 0;
while (true) {
contents->resize(offset + kBlockSize);

uv_buf_t buf;
buf.base = &(*contents)[offset];
buf.len = kBlockSize;

const ssize_t r = uv_fs_read(nullptr, &req, fd, &buf, 1, offset, nullptr);
uv_fs_req_cleanup(&req);

if (r < 0) {
return r;
}

offset += r;

if (static_cast<size_t>(r) != kBlockSize) {
break;
}
}

return static_cast<ssize_t>(offset);
}

Maybe<std::string> ReadFileSync(const std::string& path) {
std::string contents;
const ssize_t nread = ReadFileSyncRaw(path.c_str(), path.length(), &contents);
if (nread < 0) {
return Nothing<std::string>();
}
contents.resize(static_cast<size_t>(nread));
return Just(contents);
}

void DiagnosticFilename::LocalTime(TIME_TYPE* tm_struct) {
#ifdef _WIN32
GetLocalTime(tm_struct);
Expand Down
4 changes: 4 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,10 @@ class PersistentToLocal {
}
};

ssize_t ReadFileSyncRaw(
const char* path, size_t path_len, std::string* contents);
v8::Maybe<std::string> ReadFileSync(const std::string& path);

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down