From bd4773a0431da9c846c3b4a2f6459db1f151c115 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 5 Feb 2018 21:59:38 -0800 Subject: [PATCH] module: use undefined if no main MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the package.json file does not have a "main" entry, return undefined rather than an empty string. This is to make more consistent behavior. For example, when package.json is a directory, "main" is undefined rather than an empty string. PR-URL: https://github.com/nodejs/node/pull/18593 Reviewed-By: Bradley Farias Reviewed-By: Michaƫl Zasso Reviewed-By: Benjamin Gruenbaum Reviewed-By: Vladimir de Turckheim Reviewed-By: Yuta Hiroto Reviewed-By: Benedikt Meurer Reviewed-By: Ali Ijaz Sheikh Reviewed-By: Matteo Collina --- src/node_file.cc | 7 +++---- test/parallel/test-module-binding.js | 5 +++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index 9f9c7044f91167..ccb9c65cfaf63c 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -652,9 +652,8 @@ void Close(const FunctionCallbackInfo& 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& args) { Environment* env = Environment::GetCurrent(args); uv_loop_t* loop = env->event_loop(); @@ -708,7 +707,7 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo& args) { const size_t size = offset - start; if (size == 0 || size == SearchString(&chars[start], size, "\"main\"")) { - args.GetReturnValue().SetEmptyString(); + return; } else { Local chars_string = String::NewFromUtf8(env->isolate(), diff --git a/test/parallel/test-module-binding.js b/test/parallel/test-module-binding.js index bea0c91f0c5bca..a3ebaf9e7266d2 100644 --- a/test/parallel/test-module-binding.js +++ b/test/parallel/test-module-binding.js @@ -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'));