Skip to content

Commit

Permalink
nits / check both hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
bfarias-godaddy committed Mar 12, 2020
1 parent e5f480c commit 44fcb28
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
37 changes: 27 additions & 10 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const {
StringPrototypeReplace,
} = primordials;

const {
isArrayBufferView,
isAnyArrayBuffer
} = require('internal/util/types');

const {
stripBOM,
loadNativeModule
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 44fcb28

Please sign in to comment.