module: improve error message for invalid data URL · nodejs/node@3ab9619
@@ -6,12 +6,14 @@ require('internal/modules/cjs/loader');
66const {
77 FunctionPrototypeBind,
88 ObjectSetPrototypeOf,
9+ RegExpPrototypeExec,
910 SafeWeakMap,
1011 StringPrototypeStartsWith,
1112} = primordials;
12131314const {
1415ERR_INVALID_ARG_VALUE,
16+ERR_INVALID_MODULE_SPECIFIER,
1517ERR_INVALID_RETURN_PROPERTY,
1618ERR_INVALID_RETURN_PROPERTY_VALUE,
1719ERR_INVALID_RETURN_VALUE,
@@ -107,6 +109,15 @@ class Loader {
107109}
108110109111const { format } = getFormatResponse;
112+if (format === null) {
113+const dataUrl = RegExpPrototypeExec(
114+/^data:([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/,
115+url,
116+);
117+throw new ERR_INVALID_MODULE_SPECIFIER(
118+url,
119+dataUrl ? `has an unsupported MIME type "${dataUrl[1]}"` : '');
120+}
110121if (typeof format !== 'string') {
111122throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
112123'string', 'loader getFormat', 'format', format);