Skip to content

Commit

Permalink
esm: internalModuleReadJSON returns an array
Browse files Browse the repository at this point in the history
Refs: #30674
  • Loading branch information
shackijj committed May 20, 2020
1 parent e5f3182 commit 0d947ec
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
3 changes: 2 additions & 1 deletion lib/internal/modules/package_json_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function read(path) {
return cache.get(path);
}

const result = internalModuleReadJSON(path);
const [string, containsKeys] = internalModuleReadJSON(path);
const result = { string, containsKeys };
cache.set(path, result);
return result;
}
Expand Down
32 changes: 14 additions & 18 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -832,27 +832,25 @@ void Close(const FunctionCallbackInfo<Value>& args) {
}


// Used to speed up module loading. Returns object
// {string: string | undefined, containsKeys: undefined | boolean}
// Used to speed up module loading. Returns an array [string, boolean]
static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
uv_loop_t* loop = env->event_loop();
Local<Object> return_value = Object::New(isolate);

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

if (strlen(*path) != path.length()) {
args.GetReturnValue().Set(return_value);
args.GetReturnValue().Set(Array::New(isolate));
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) {
args.GetReturnValue().Set(return_value);
args.GetReturnValue().Set(Array::New(isolate));
return;
}

Expand All @@ -879,7 +877,7 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
uv_fs_req_cleanup(&read_req);

if (numchars < 0) {
args.GetReturnValue().Set(return_value);
args.GetReturnValue().Set(Array::New(isolate));
return;
}
offset += numchars;
Expand Down Expand Up @@ -916,19 +914,17 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
if (0 == memcmp(s, "exports", 7)) break;
}
}
return_value->Set(
isolate->GetCurrentContext(),
FIXED_ONE_BYTE_STRING(isolate, "string"),


Local<Value> return_value[] = {
String::NewFromUtf8(isolate,
&chars[start],
v8::NewStringType::kNormal,
size).ToLocalChecked()).Check();

return_value->Set(
isolate->GetCurrentContext(),
FIXED_ONE_BYTE_STRING(isolate, "containsKeys"),
Boolean::New(isolate, p < pe ? true : false)).Check();
args.GetReturnValue().Set(return_value);
&chars[start],
v8::NewStringType::kNormal,
size).ToLocalChecked(),
Boolean::New(isolate, p < pe ? true : false)
};
args.GetReturnValue().Set(
Array::New(isolate, return_value, arraysize(return_value)));
}

// Used to speed up module loading. Returns 0 if the path refers to
Expand Down
33 changes: 18 additions & 15 deletions test/parallel/test-module-binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,31 @@ const { internalModuleReadJSON } = internalBinding('fs');
const { readFileSync } = require('fs');
const { strictEqual } = require('assert');
{
const result = internalModuleReadJSON('nosuchfile');
strictEqual(result.string, undefined);
strictEqual(result.containsKeys, undefined);
const [string, containsKeys] = internalModuleReadJSON('nosuchfile');
strictEqual(string, undefined);
strictEqual(containsKeys, undefined);
}
{
const result = internalModuleReadJSON(fixtures.path('empty.txt'));
strictEqual(result.string, '');
strictEqual(result.containsKeys, false);
const [string, containsKeys] =
internalModuleReadJSON(fixtures.path('empty.txt'));
strictEqual(string, '');
strictEqual(containsKeys, false);
}
{
const result = internalModuleReadJSON(fixtures.path('empty.txt'));
strictEqual(result.string, '');
strictEqual(result.containsKeys, false);
const [string, containsKeys] =
internalModuleReadJSON(fixtures.path('empty.txt'));
strictEqual(string, '');
strictEqual(containsKeys, false);
}
{
const result = internalModuleReadJSON(fixtures.path('empty-with-bom.txt'));
strictEqual(result.string, '');
strictEqual(result.containsKeys, false);
const [string, containsKeys] =
internalModuleReadJSON(fixtures.path('empty-with-bom.txt'));
strictEqual(string, '');
strictEqual(containsKeys, false);
}
{
const filename = fixtures.path('require-bin/package.json');
const result = internalModuleReadJSON(filename);
strictEqual(result.string, readFileSync(filename, 'utf8'));
strictEqual(result.containsKeys, true);
const [string, containsKeys] = internalModuleReadJSON(filename);
strictEqual(string, readFileSync(filename, 'utf8'));
strictEqual(containsKeys, true);
}

0 comments on commit 0d947ec

Please sign in to comment.