From 7f2b068583d50554bf1e68d38263cd22f362e18d Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Tue, 12 Sep 2017 08:50:48 -0500 Subject: [PATCH 1/4] module: deprecate loading files with unknown or missing extensions This prevents treating files as CJS when they are not specified as .js explicitly. Package managers create links without the extension already so the use of "bin" still works. Also, node continues to do path searching and can find a .js file with the same location via command line. --- doc/api/deprecations.md | 7 +++++++ lib/module.js | 9 +++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index e5cec17d03163e..0b1d3ef5d5209d 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -684,6 +684,13 @@ difference is that `querystring.parse()` does url encoding: { '%E5%A5%BD': '1' } ``` + +### DEP0077: Loading files without a known file extension + +Type: Runtime + +Files without file extensions, or unknown file extensions will not be loaded. + [`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size [`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array [`Buffer.from(buffer)`]: buffer.html#buffer_class_method_buffer_from_buffer diff --git a/lib/module.js b/lib/module.js index 0b87cf7480e4d9..86fa4015df72db 100644 --- a/lib/module.js +++ b/lib/module.js @@ -24,6 +24,7 @@ const NativeModule = require('native_module'); const util = require('util'); const internalModule = require('internal/module'); +const internalUtil = require('internal/util'); const { getURLFromFilePath } = require('internal/url'); const vm = require('vm'); const assert = require('assert').ok; @@ -516,8 +517,12 @@ Module.prototype.load = function(filename) { this.filename = filename; this.paths = Module._nodeModulePaths(path.dirname(filename)); - var extension = path.extname(filename) || '.js'; - if (!Module._extensions[extension]) extension = '.js'; + var extension = path.extname(filename); + if (!extension || !Module._extensions[extension]) { + internalUtil.deprecate(() => { + extension = '.js'; + }, 'Loading files without an extension or unknown extension is deprecated', 'DEP0077')(); + }; Module._extensions[extension](this, filename); this.loaded = true; From 54f31125076bdb64853a145836539ac0e2d8341c Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Tue, 12 Sep 2017 09:36:14 -0500 Subject: [PATCH 2/4] should only create 1 deprecate function --- lib/module.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/module.js b/lib/module.js index 86fa4015df72db..84aa0ebf003fd8 100644 --- a/lib/module.js +++ b/lib/module.js @@ -508,7 +508,9 @@ Module._resolveFilename = function(request, parent, isMain) { return filename; }; - +const DEPRECATED_EXTENSION_FILLING = internalUtil.deprecate(() => { + return '.js'; +}, 'Loading files without an extension or unknown extension is deprecated', 'DEP0077'); // Given a file name, pass it to the proper extension handler. Module.prototype.load = function(filename) { debug('load %j for module %j', filename, this.id); @@ -519,9 +521,7 @@ Module.prototype.load = function(filename) { var extension = path.extname(filename); if (!extension || !Module._extensions[extension]) { - internalUtil.deprecate(() => { - extension = '.js'; - }, 'Loading files without an extension or unknown extension is deprecated', 'DEP0077')(); + extension = DEPRECATED_EXTENSION_FILLING(); }; Module._extensions[extension](this, filename); this.loaded = true; From ed60ce45d7ddae43894c809ef230e693fcea20a7 Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Tue, 12 Sep 2017 11:58:30 -0500 Subject: [PATCH 3/4] fix long line --- lib/module.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/module.js b/lib/module.js index 84aa0ebf003fd8..0c084831fd3c07 100644 --- a/lib/module.js +++ b/lib/module.js @@ -509,8 +509,10 @@ Module._resolveFilename = function(request, parent, isMain) { }; const DEPRECATED_EXTENSION_FILLING = internalUtil.deprecate(() => { - return '.js'; -}, 'Loading files without an extension or unknown extension is deprecated', 'DEP0077'); + return '.js'; + }, + 'Loading files without an extension or unknown extension is deprecated', + 'DEP0077'); // Given a file name, pass it to the proper extension handler. Module.prototype.load = function(filename) { debug('load %j for module %j', filename, this.id); From 2f35f90622fbf7c1348aaa44d30a761ee62ee8fd Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Tue, 12 Sep 2017 11:59:15 -0500 Subject: [PATCH 4/4] doc touchup --- doc/api/deprecations.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 0b1d3ef5d5209d..19cb2165b78db7 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -684,12 +684,12 @@ difference is that `querystring.parse()` does url encoding: { '%E5%A5%BD': '1' } ``` - -### DEP0077: Loading files without a known file extension + +### DEP00XX: Loading files without a known file extension Type: Runtime -Files without file extensions, or unknown file extensions will not be loaded. +Files without file extensions or unknown file extensions will not be loaded. [`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size [`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array