Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: use undefined if no main #18593

Closed
wants to merge 1 commit into from
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
7 changes: 3 additions & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,8 @@ void Close(const FunctionCallbackInfo<Value>& args) {


// Used to speed up module loading. Returns the contents of the file as
// a string or undefined when the file cannot be opened. Returns an empty
// string when the file does not contain the substring '"main"' because that
// is the property we care about.
// a string or undefined when the file cannot be opened or "main" is not found
// in the file.
static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
uv_loop_t* loop = env->event_loop();
Expand Down Expand Up @@ -704,7 +703,7 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {

const size_t size = offset - start;
if (size == 0 || size == SearchString(&chars[start], size, "\"main\"")) {
args.GetReturnValue().SetEmptyString();
return;
} else {
Local<String> chars_string =
String::NewFromUtf8(env->isolate(),
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-module-binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const { readFileSync } = require('fs');
const { strictEqual } = require('assert');

strictEqual(internalModuleReadJSON('nosuchfile'), undefined);
strictEqual(internalModuleReadJSON(fixtures.path('empty.txt')), '');
strictEqual(internalModuleReadJSON(fixtures.path('empty-with-bom.txt')), '');
strictEqual(internalModuleReadJSON(fixtures.path('empty.txt')), undefined);
strictEqual(internalModuleReadJSON(fixtures.path('empty-with-bom.txt')),
undefined);
{
const filename = fixtures.path('require-bin/package.json');
strictEqual(internalModuleReadJSON(filename), readFileSync(filename, 'utf8'));
Expand Down