Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: improve error message for invalid data URL #37701

Merged
merged 1 commit into from Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/internal/modules/esm/loader.js
Expand Up @@ -6,12 +6,14 @@ require('internal/modules/cjs/loader');
const {
FunctionPrototypeBind,
ObjectSetPrototypeOf,
RegExpPrototypeExec,
SafeWeakMap,
StringPrototypeStartsWith,
} = primordials;

const {
ERR_INVALID_ARG_VALUE,
ERR_INVALID_MODULE_SPECIFIER,
ERR_INVALID_RETURN_PROPERTY,
ERR_INVALID_RETURN_PROPERTY_VALUE,
ERR_INVALID_RETURN_VALUE,
Expand Down Expand Up @@ -107,6 +109,15 @@ class Loader {
}

const { format } = getFormatResponse;
if (format === null) {
DerekNonGeneric marked this conversation as resolved.
Show resolved Hide resolved
const dataUrl = RegExpPrototypeExec(
/^data:([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/,
url,
);
throw new ERR_INVALID_MODULE_SPECIFIER(
url,
dataUrl ? `has an unsupported MIME type "${dataUrl[1]}"` : '');
}
if (typeof format !== 'string') {
throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
'string', 'loader getFormat', 'format', format);
Expand Down
2 changes: 1 addition & 1 deletion test/es-module/test-esm-data-urls.js
Expand Up @@ -99,7 +99,7 @@ function createBase64URL(mime, body) {
await import(plainESMURL);
common.mustNotCall()();
} catch (e) {
assert.strictEqual(e.code, 'ERR_INVALID_RETURN_PROPERTY_VALUE');
assert.strictEqual(e.code, 'ERR_INVALID_MODULE_SPECIFIER');
}
}
{
Expand Down
24 changes: 24 additions & 0 deletions test/es-module/test-esm-invalid-data-urls.js
@@ -0,0 +1,24 @@
'use strict';
DerekNonGeneric marked this conversation as resolved.
Show resolved Hide resolved
const common = require('../common');
const assert = require('assert');

(async () => {
await assert.rejects(import('data:text/plain,export default0'), {
code: 'ERR_INVALID_MODULE_SPECIFIER',
message:
'Invalid module "data:text/plain,export default0" has an unsupported ' +
'MIME type "text/plain"',
});
await assert.rejects(import('data:text/plain;base64,'), {
code: 'ERR_INVALID_MODULE_SPECIFIER',
message:
'Invalid module "data:text/plain;base64," has an unsupported ' +
'MIME type "text/plain"',
});
await assert.rejects(import('data:application/json,[]'), {
code: 'ERR_INVALID_MODULE_SPECIFIER',
message:
'Invalid module "data:application/json,[]" has an unsupported ' +
'MIME type "application/json"',
});
})().then(common.mustCall());