From 6aee088eb9e26a6d9554fded84a8f7b23a89a568 Mon Sep 17 00:00:00 2001 From: Julian Londono Date: Tue, 30 Jun 2020 01:10:00 +0000 Subject: [PATCH 1/8] Add initial formatting change --- lib/internal/modules/esm/loader.js | 10 ++++++++-- lib/internal/modules/esm/module_job.js | 5 ++--- lib/internal/modules/esm/translators.js | 12 +++--------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index de08845a820d77..3b36a8079c8dc9 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -224,7 +224,13 @@ class Loader { async getModuleJob(specifier, parentURL) { const url = await this.resolve(specifier, parentURL); - const format = await this.getFormat(url); + let format = await this.getFormat(url); + let source; + if(format !== "builtin" && format !== "commonjs"){ + const sourceObj = await this._getSource(url, { format: format }, defaultGetSource); + ({ source } = sourceObj); + format = sourceObj.format? sourceObj.format : format; + } let job = this.moduleMap.get(url); // CommonJS will set functions for lazy job evaluation. if (typeof job === 'function') @@ -239,7 +245,7 @@ class Loader { const inspectBrk = parentURL === undefined && format === 'module' && getOptionValue('--inspect-brk'); - job = new ModuleJob(this, url, loaderInstance, parentURL === undefined, + job = new ModuleJob(this, url, loaderInstance, source, parentURL === undefined, inspectBrk); this.moduleMap.set(url, job); return job; diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index 4ffa8db9dab903..131b06d092f2e4 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -27,7 +27,7 @@ let hasPausedEntry = false; class ModuleJob { // `loader` is the Loader instance used for loading dependencies. // `moduleProvider` is a function - constructor(loader, url, moduleProvider, isMain, inspectBrk) { + constructor(loader, url, moduleProvider, source, isMain, inspectBrk) { this.loader = loader; this.isMain = isMain; this.inspectBrk = inspectBrk; @@ -35,8 +35,7 @@ class ModuleJob { this.module = undefined; // Expose the promise to the ModuleWrap directly for linking below. // `this.module` is also filled in below. - this.modulePromise = moduleProvider.call(loader, url, isMain); - + this.modulePromise = moduleProvider.call(loader, url, source, isMain); // Wait for the ModuleWrap instance being linked with all dependencies. const link = async () => { this.module = await this.modulePromise; diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index bb3528eddde964..dadcfe68ceeaaa 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -110,9 +110,7 @@ function initializeImportMeta(meta, { url }) { } // Strategy for loading a standard JavaScript module -translators.set('module', async function moduleStrategy(url) { - let { source } = await this._getSource( - url, { format: 'module' }, defaultGetSource); +translators.set('module', async function moduleStrategy(url, source) { assertBufferSource(source, true, 'getSource'); ({ source } = await this._transformSource( source, { url, format: 'module' }, defaultTransformSource)); @@ -171,7 +169,7 @@ translators.set('builtin', async function builtinStrategy(url) { }); // Strategy for loading a JSON file -translators.set('json', async function jsonStrategy(url) { +translators.set('json', async function jsonStrategy(url, source) { emitExperimentalWarning('Importing JSON modules'); debug(`Translating JSONModule ${url}`); debug(`Loading JSONModule ${url}`); @@ -189,8 +187,6 @@ translators.set('json', async function jsonStrategy(url) { }); } } - let { source } = await this._getSource( - url, { format: 'json' }, defaultGetSource); assertBufferSource(source, true, 'getSource'); ({ source } = await this._transformSource( source, { url, format: 'json' }, defaultTransformSource)); @@ -231,10 +227,8 @@ translators.set('json', async function jsonStrategy(url) { }); // Strategy for loading a wasm module -translators.set('wasm', async function(url) { +translators.set('wasm', async function(url, source) { emitExperimentalWarning('Importing Web Assembly modules'); - let { source } = await this._getSource( - url, { format: 'wasm' }, defaultGetSource); assertBufferSource(source, false, 'getSource'); ({ source } = await this._transformSource( source, { url, format: 'wasm' }, defaultTransformSource)); From fb154be634a8fac1c06045ed35a0ebe544ed42fa Mon Sep 17 00:00:00 2001 From: Julian Londono Date: Tue, 30 Jun 2020 22:29:08 +0000 Subject: [PATCH 2/8] Add error-handling wrapper for getSource --- lib/internal/modules/esm/loader.js | 29 ++++++++++++++++++++++++- lib/internal/modules/esm/translators.js | 4 +--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 3b36a8079c8dc9..aef9ea28c38eb2 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -134,6 +134,33 @@ class Loader { return format; } + async getSource(url, format) { + const getSourceResponse = await this._getSource( + url, { format: format }, defaultGetSource); + if (typeof getSourceResponse !== 'object') { + throw new ERR_INVALID_RETURN_VALUE( + 'object', 'loader getSource', getSourceResponse); + } + + // Validate optional format that getSource can return + ({ format } = getSourceResponse); + if (typeof format !== 'string' && typeof format !== 'undefined') { + throw new ERR_INVALID_RETURN_PROPERTY_VALUE( + 'string', 'loader getSource', 'format', format); + } + + const { source } = getSourceResponse; + if (typeof source !== 'string' && !Buffer.isBuffer(source)) { + throw new ERR_INVALID_RETURN_PROPERTY_VALUE( + 'string|buffer', 'loader getSource', 'source', source); + } + + return { + source: source, + format: format + } + } + async eval( source, url = pathToFileURL(`${process.cwd()}/[eval${++this.evalIndex}]`).href @@ -227,7 +254,7 @@ class Loader { let format = await this.getFormat(url); let source; if(format !== "builtin" && format !== "commonjs"){ - const sourceObj = await this._getSource(url, { format: format }, defaultGetSource); + const sourceObj = await this.getSource(url, format); ({ source } = sourceObj); format = sourceObj.format? sourceObj.format : format; } diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index dadcfe68ceeaaa..2298e56be8a5c4 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -23,8 +23,6 @@ const { } = require('internal/modules/cjs/helpers'); const CJSModule = require('internal/modules/cjs/loader').Module; const internalURLModule = require('internal/url'); -const { defaultGetSource } = require( - 'internal/modules/esm/get_source'); const { defaultTransformSource } = require( 'internal/modules/esm/transform_source'); const createDynamicModule = require( @@ -128,7 +126,7 @@ translators.set('module', async function moduleStrategy(url, source) { // Strategy for loading a node-style CommonJS module const isWindows = process.platform === 'win32'; const winSepRegEx = /\//g; -translators.set('commonjs', function commonjsStrategy(url, isMain) { +translators.set('commonjs', function commonjsStrategy(url, source, isMain) { debug(`Translating CJSModule ${url}`); const pathname = internalURLModule.fileURLToPath(new URL(url)); const cached = this.cjsCache.get(url); From 0fc1e6b7a3a6f89f59c45b5caf9c22ed3cd546c8 Mon Sep 17 00:00:00 2001 From: Julian Londono Date: Fri, 17 Jul 2020 22:25:54 +0000 Subject: [PATCH 3/8] Fix test-esm-loader-invalid-format --- lib/internal/modules/esm/loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index aef9ea28c38eb2..3d272b1ca726cb 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -253,7 +253,7 @@ class Loader { const url = await this.resolve(specifier, parentURL); let format = await this.getFormat(url); let source; - if(format !== "builtin" && format !== "commonjs"){ + if(format !== "builtin" && format !== "commonjs" && translators.get(format)){ const sourceObj = await this.getSource(url, format); ({ source } = sourceObj); format = sourceObj.format? sourceObj.format : format; From 9478e979c107efa8f0f166f6137ba95b98328c02 Mon Sep 17 00:00:00 2001 From: Julian Londono Date: Fri, 17 Jul 2020 22:26:15 +0000 Subject: [PATCH 4/8] esm_display_syntax_error_module --- test/message/esm_display_syntax_error_module.out | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/message/esm_display_syntax_error_module.out b/test/message/esm_display_syntax_error_module.out index 708257fcaf5792..6da4c379ccfa15 100644 --- a/test/message/esm_display_syntax_error_module.out +++ b/test/message/esm_display_syntax_error_module.out @@ -3,4 +3,5 @@ await async () => 0; ^^^^^ SyntaxError: Unexpected reserved word - at Loader.moduleStrategy (internal/modules/esm/translators.js:*:*) \ No newline at end of file + at Loader.moduleStrategy (internal/modules/esm/translators.js:*:*) + at async link (internal/modules/esm/module_job.js:*:*) \ No newline at end of file From 2440daadd97163deabeac4c646095f9b66c37962 Mon Sep 17 00:00:00 2001 From: Julian Londono Date: Sat, 18 Jul 2020 00:37:41 +0000 Subject: [PATCH 5/8] Fix getSource validation --- lib/internal/modules/esm/loader.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 3d272b1ca726cb..6d2b552d93b33e 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -149,12 +149,6 @@ class Loader { 'string', 'loader getSource', 'format', format); } - const { source } = getSourceResponse; - if (typeof source !== 'string' && !Buffer.isBuffer(source)) { - throw new ERR_INVALID_RETURN_PROPERTY_VALUE( - 'string|buffer', 'loader getSource', 'source', source); - } - return { source: source, format: format From fa3a7116d0a83e59a6c3b73018f827d409a2c03f Mon Sep 17 00:00:00 2001 From: Julian Londono Date: Mon, 20 Jul 2020 18:18:19 +0000 Subject: [PATCH 6/8] Fix getSource return object --- lib/internal/modules/esm/loader.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 6d2b552d93b33e..4eaa540be23f66 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -149,6 +149,8 @@ class Loader { 'string', 'loader getSource', 'format', format); } + const { source } = getSourceResponse; + return { source: source, format: format From 8e90771343eb29c53da469ceb991368af3002f3b Mon Sep 17 00:00:00 2001 From: Julian Londono Date: Mon, 20 Jul 2020 19:48:41 +0000 Subject: [PATCH 7/8] Change syntax --- lib/internal/modules/esm/loader.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 4eaa540be23f66..8716eb1a777bb4 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -142,6 +142,8 @@ class Loader { 'object', 'loader getSource', getSourceResponse); } + const { source } = getSourceResponse; + // Validate optional format that getSource can return ({ format } = getSourceResponse); if (typeof format !== 'string' && typeof format !== 'undefined') { @@ -149,8 +151,6 @@ class Loader { 'string', 'loader getSource', 'format', format); } - const { source } = getSourceResponse; - return { source: source, format: format From 1b2330a6db94a1da6014cd175a97059bbf3f6b9a Mon Sep 17 00:00:00 2001 From: Julian Londono Date: Tue, 21 Jul 2020 23:55:49 +0000 Subject: [PATCH 8/8] Fix lint-js errors --- lib/internal/modules/esm/loader.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 8716eb1a777bb4..f9e8a99689cea1 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -154,7 +154,7 @@ class Loader { return { source: source, format: format - } + }; } async eval( @@ -249,10 +249,12 @@ class Loader { const url = await this.resolve(specifier, parentURL); let format = await this.getFormat(url); let source; - if(format !== "builtin" && format !== "commonjs" && translators.get(format)){ + if (format !== 'builtin' && + format !== 'commonjs' && + translators.get(format)) { const sourceObj = await this.getSource(url, format); ({ source } = sourceObj); - format = sourceObj.format? sourceObj.format : format; + format = sourceObj.format ? sourceObj.format : format; } let job = this.moduleMap.get(url); // CommonJS will set functions for lazy job evaluation. @@ -268,8 +270,13 @@ class Loader { const inspectBrk = parentURL === undefined && format === 'module' && getOptionValue('--inspect-brk'); - job = new ModuleJob(this, url, loaderInstance, source, parentURL === undefined, - inspectBrk); + job = new ModuleJob( + this, + url, + loaderInstance, + source, + parentURL === undefined, + inspectBrk); this.moduleMap.set(url, job); return job; }