From 44fcb285fde95e2ce6ee818354762f2ceb463346 Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Wed, 11 Mar 2020 19:39:18 -0500 Subject: [PATCH] nits / check both hooks --- doc/api/esm.md | 2 +- lib/internal/modules/esm/translators.js | 37 ++++++++++++++++++------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index 02d20996a8cdfa..1468062e145dbf 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -1098,7 +1098,7 @@ a URL should be interpreted. The `format` returned also affects what the acceptable forms of source values are for a module when parsing. This can be one of the following: -| `format` | Description | Acceptable Source Values | +| `format` | Description | Acceptable Types For `source` Returned by `getSource` or `transformSource` | | --- | --- | | `'builtin'` | Load a Node.js builtin module | Not applicable | | `'commonjs'` | Load a Node.js CommonJS module | Not applicable | diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 9d0b7edecb0343..3241d6446b1c44 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -9,6 +9,11 @@ const { StringPrototypeReplace, } = primordials; +const { + isArrayBufferView, + isAnyArrayBuffer +} = require('internal/util/types'); + const { stripBOM, loadNativeModule @@ -41,16 +46,24 @@ const translators = new SafeMap(); exports.translators = translators; const DECODER = new TextDecoder(); +function assertBufferSource(body, allowString, hookName) { + if (allowString && typeof body === 'string') { + return; + } + if (isArrayBufferView(body) || isAnyArrayBuffer(body)) { + return; + } + throw new ERR_INVALID_RETURN_PROPERTY_VALUE( + `${allowString ? 'string, ' : ''}array buffer, or typed array`, + hookName, + 'source', + body + ); +} + function stringify(body) { if (typeof body === 'string') return body; - if (typeof body !== 'object' || !body) { - throw new ERR_INVALID_RETURN_PROPERTY_VALUE( - 'string, array buffer, or typed array', - 'transformSource', - 'source', - body - ); - } + assertBufferSource(body, false, 'transformSource'); return DECODER.decode(body); } @@ -90,9 +103,10 @@ function initializeImportMeta(meta, { url }) { translators.set('module', async function moduleStrategy(url) { let { source } = await this._getSource( url, { format: 'module' }, defaultGetSource); - source = stringify(source); + assertBufferSource(source, true, 'getSource'); ({ source } = await this._transformSource( source, { url, format: 'module' }, defaultTransformSource)); + source = stringify(source); maybeCacheSourceMap(url, source); debug(`Translating StandardModule ${url}`); const module = new ModuleWrap(url, undefined, source, 0, 0); @@ -167,9 +181,10 @@ translators.set('json', async function jsonStrategy(url) { } let { source } = await this._getSource( url, { format: 'json' }, defaultGetSource); - source = stringify(source); + assertBufferSource(source, true, 'getSource'); ({ source } = await this._transformSource( source, { url, format: 'json' }, defaultTransformSource)); + source = stringify(source); if (pathname) { // A require call could have been called on the same file during loading and // that resolves synchronously. To make sure we always return the identical @@ -210,8 +225,10 @@ translators.set('wasm', async function(url) { 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)); + assertBufferSource(source, false, 'transformSource'); debug(`Translating WASMModule ${url}`); let compiled; try {