Skip to content

Commit 114e4c8

Browse files
committed
module: improve error message for invalid data URL
Fixes: #37647
1 parent 117e293 commit 114e4c8

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/internal/modules/esm/loader.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require('internal/modules/cjs/loader');
66
const {
77
FunctionPrototypeBind,
88
ObjectSetPrototypeOf,
9+
RegExpPrototypeExec,
910
SafeWeakMap,
1011
StringPrototypeStartsWith,
1112
} = primordials;
@@ -107,6 +108,13 @@ class Loader {
107108
}
108109

109110
const { format } = getFormatResponse;
111+
if(format === null) {
112+
const dataUrl = RegExpPrototypeExec(
113+
/^data:([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/,
114+
url,
115+
);
116+
throw new ERR_UNKNOWN_MODULE_FORMAT(dataUrl ? dataUrl[1] : 'null');
117+
}
110118
if (typeof format !== 'string') {
111119
throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
112120
'string', 'loader getFormat', 'format', format);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
(async () => {
6+
await assert.rejects(import(`data:text/plain,export default0`), {
7+
code: 'ERR_UNKNOWN_MODULE_FORMAT',
8+
message: 'Unknown module format: text/plain',
9+
});
10+
await assert.rejects(import(`data:text/plain;base64,`), {
11+
code: 'ERR_UNKNOWN_MODULE_FORMAT',
12+
message: 'Unknown module format: text/plain',
13+
});
14+
await assert.rejects(import('data:application/json,[]'), {
15+
code: 'ERR_UNKNOWN_MODULE_FORMAT',
16+
message: 'Unknown module format: application/json',
17+
});
18+
})().then(common.mustCall());

0 commit comments

Comments
 (0)