From fec4a5d9d09111400180bf6fee3b7897cbf089b5 Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Fri, 19 Jan 2018 15:05:24 -0600 Subject: [PATCH 1/4] module: ignore package.json/ directories Fixes: https://github.com/nodejs/node/issues/8307 --- lib/module.js | 4 ++++ .../package.json/index.js | 0 .../parallel/test-require-package-json-directory.js | 13 +++++++++++++ 3 files changed, 17 insertions(+) create mode 100644 test/fixtures/module-package-json-directory/package.json/index.js create mode 100644 test/parallel/test-require-package-json-directory.js diff --git a/lib/module.js b/lib/module.js index 0ee109bc325d60..a68f3350f37641 100644 --- a/lib/module.js +++ b/lib/module.js @@ -123,6 +123,10 @@ function readPackage(requestPath) { return entry; const jsonPath = path.resolve(requestPath, 'package.json'); + const rc = stat(jsonPath); + if (rc !== 0) { + return false; + } const json = internalModuleReadJSON(path.toNamespacedPath(jsonPath)); if (json === undefined) { diff --git a/test/fixtures/module-package-json-directory/package.json/index.js b/test/fixtures/module-package-json-directory/package.json/index.js new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/test/parallel/test-require-package-json-directory.js b/test/parallel/test-require-package-json-directory.js new file mode 100644 index 00000000000000..b7e2e30737d0a4 --- /dev/null +++ b/test/parallel/test-require-package-json-directory.js @@ -0,0 +1,13 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const fixtures = require('../common/fixtures'); + +try { + require(fixtures.path('module-package-json-directory')); + process.exit(1); +} catch (err) { + assert.ok( + /Cannot find module/.test(err.message), + `require() should ignore package.json/ directories: ${err.message}`); +} From 8879cc2f04cfd96f7193f6fc843772e846377870 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 20 Jan 2018 08:50:30 -0600 Subject: [PATCH 2/4] bnoordhuis nits Author: Ben Noordhuis --- lib/module.js | 4 ---- src/node_file.cc | 13 +++++++++++-- .../test-require-package-json-directory.js | 16 +++++++--------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/module.js b/lib/module.js index a68f3350f37641..0ee109bc325d60 100644 --- a/lib/module.js +++ b/lib/module.js @@ -123,10 +123,6 @@ function readPackage(requestPath) { return entry; const jsonPath = path.resolve(requestPath, 'package.json'); - const rc = stat(jsonPath); - if (rc !== 0) { - return false; - } const json = internalModuleReadJSON(path.toNamespacedPath(jsonPath)); if (json === undefined) { diff --git a/src/node_file.cc b/src/node_file.cc index c9bbea1ee5f621..5348e5637084a7 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -39,6 +39,7 @@ # include #endif +#include #include namespace node { @@ -475,6 +476,12 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo& args) { return; } + std::shared_ptr 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 chars; int64_t offset = 0; @@ -490,8 +497,10 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo& args) { uv_fs_t read_req; numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr); uv_fs_req_cleanup(&read_req); - - CHECK_GE(numchars, 0); + if (numchars < 0) { + args.GetReturnValue().SetEmptyString(); + return; + } offset += numchars; } while (static_cast(numchars) == kBlockSize); diff --git a/test/parallel/test-require-package-json-directory.js b/test/parallel/test-require-package-json-directory.js index b7e2e30737d0a4..737f58ae5aa9f7 100644 --- a/test/parallel/test-require-package-json-directory.js +++ b/test/parallel/test-require-package-json-directory.js @@ -1,13 +1,11 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const fixtures = require('../common/fixtures'); -try { - require(fixtures.path('module-package-json-directory')); - process.exit(1); -} catch (err) { - assert.ok( - /Cannot find module/.test(err.message), - `require() should ignore package.json/ directories: ${err.message}`); -} +common.expectsError( + () => { require(fixtures.path('module-package-json-directory')); }, + { + code: 'MODULE_NOT_FOUND', + message: /Cannot find module/ + }); \ No newline at end of file From 4d668cbd3efb49786a196e8f1b329ae2484f35e7 Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Sat, 20 Jan 2018 09:16:30 -0600 Subject: [PATCH 3/4] fix bug on empty main --- lib/module.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/module.js b/lib/module.js index 0ee109bc325d60..902d1a74ada741 100644 --- a/lib/module.js +++ b/lib/module.js @@ -118,9 +118,8 @@ Module._debug = util.deprecate(debug, 'Module._debug is deprecated.', const packageMainCache = Object.create(null); function readPackage(requestPath) { - const entry = packageMainCache[requestPath]; - if (entry) - return entry; + if (requestPath in packageMainCache) + return packageMainCache[requestPath]; const jsonPath = path.resolve(requestPath, 'package.json'); const json = internalModuleReadJSON(path.toNamespacedPath(jsonPath)); From bc7bdec04641ef72d6cf18e0e3970f58989c8c08 Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Sat, 20 Jan 2018 09:25:48 -0600 Subject: [PATCH 4/4] fixup extra close_req --- src/node_file.cc | 4 ---- test/parallel/test-require-package-json-directory.js | 3 +-- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index 5348e5637084a7..73b73f7f09e3cc 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -504,10 +504,6 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo& args) { offset += numchars; } while (static_cast(numchars) == kBlockSize); - uv_fs_t close_req; - CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr)); - uv_fs_req_cleanup(&close_req); - size_t start = 0; if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) { start = 3; // Skip UTF-8 BOM. diff --git a/test/parallel/test-require-package-json-directory.js b/test/parallel/test-require-package-json-directory.js index 737f58ae5aa9f7..515f3350114e10 100644 --- a/test/parallel/test-require-package-json-directory.js +++ b/test/parallel/test-require-package-json-directory.js @@ -1,6 +1,5 @@ 'use strict'; const common = require('../common'); -const assert = require('assert'); const fixtures = require('../common/fixtures'); common.expectsError( @@ -8,4 +7,4 @@ common.expectsError( { code: 'MODULE_NOT_FOUND', message: /Cannot find module/ - }); \ No newline at end of file + });