fix(node:module): allow file url strings in createRequire (#7533)

* fix(node:module): allow file url strings in createRequire

* add a non-happy path test :)
This commit is contained in:
dave caruso
2023-12-08 16:32:35 -08:00
committed by GitHub
parent c84be8c48b
commit aaa827f90e
4 changed files with 49 additions and 4 deletions

View File

@@ -180,14 +180,26 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionNodeModuleCreateRequire,
if (callFrame->argumentCount() < 1) {
throwTypeError(globalObject, scope,
"createRequire() requires at least one argument"_s);
RELEASE_AND_RETURN(scope, JSC::JSValue::encode(JSC::jsUndefined()));
RELEASE_AND_RETURN(scope, JSC::JSValue::encode({}));
}
auto val = callFrame->uncheckedArgument(0).toWTFString(globalObject);
if (val.startsWith("file://"_s)) {
WTF::URL url(val);
if (!url.isValid()) {
throwTypeError(globalObject, scope, makeString("createRequire() was given an invalid URL '"_s, url.string(), "'"_s));;
RELEASE_AND_RETURN(scope, JSValue::encode({}));
}
if (!url.protocolIsFile()) {
throwTypeError(globalObject, scope, "createRequire() does not support non-file URLs"_s);
RELEASE_AND_RETURN(scope, JSValue::encode({}));
}
val = url.fileSystemPath();
}
RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(JSC::jsUndefined()));
RELEASE_AND_RETURN(
scope, JSValue::encode(Bun::JSCommonJSModule::createBoundRequireFunction(
vm, globalObject, val)));
RELEASE_AND_RETURN(scope, JSValue::encode(Bun::JSCommonJSModule::createBoundRequireFunction(vm, globalObject, val)));
}
extern "C" JSC::EncodedJSValue Resolver__nodeModulePathsForJS(JSGlobalObject *,
CallFrame *);