aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/BunString.cpp
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-07-21 23:27:28 -0700
committerGravatar GitHub <noreply@github.com> 2023-07-21 23:27:28 -0700
commit636cec03e10ab487b1df3057aaab60b4d2b02c99 (patch)
treefbf10b89dcd64270ba7fee1d78efba2b5caf2414 /src/bun.js/bindings/BunString.cpp
parent1ecd9f8a18da1af9f2090791b15a18ff3e68411d (diff)
downloadbun-636cec03e10ab487b1df3057aaab60b4d2b02c99.tar.gz
bun-636cec03e10ab487b1df3057aaab60b4d2b02c99.tar.zst
bun-636cec03e10ab487b1df3057aaab60b4d2b02c99.zip
Use WebKit's URL parser in fetch() and `bun install` (#3730)
* Use WebKit's URL parser in fetch() and `bun install` * Allocate less memory * Fix test --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js/bindings/BunString.cpp')
-rw-r--r--src/bun.js/bindings/BunString.cpp109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/bun.js/bindings/BunString.cpp b/src/bun.js/bindings/BunString.cpp
index 0676dce19..714f10080 100644
--- a/src/bun.js/bindings/BunString.cpp
+++ b/src/bun.js/bindings/BunString.cpp
@@ -297,4 +297,113 @@ extern "C" void BunString__toWTFString(BunString* bunString)
bunString->impl.wtf = Zig::toStringStatic(bunString->impl.zig).impl();
bunString->tag = BunStringTag::WTFStringImpl;
}
+}
+
+extern "C" WTF::URL* URL__fromJS(EncodedJSValue encodedValue, JSC::JSGlobalObject* globalObject)
+{
+ auto throwScope = DECLARE_THROW_SCOPE(globalObject->vm());
+ JSC::JSValue value = JSC::JSValue::decode(encodedValue);
+ auto str = value.toWTFString(globalObject);
+ RETURN_IF_EXCEPTION(throwScope, nullptr);
+ if (str.isEmpty()) {
+ return nullptr;
+ }
+
+ auto url = WTF::URL(str);
+ if (!url.isValid() || url.isNull())
+ return nullptr;
+
+ return new WTF::URL(WTFMove(url));
+}
+
+extern "C" BunString URL__getHrefFromJS(EncodedJSValue encodedValue, JSC::JSGlobalObject* globalObject)
+{
+ auto throwScope = DECLARE_THROW_SCOPE(globalObject->vm());
+ JSC::JSValue value = JSC::JSValue::decode(encodedValue);
+ auto str = value.toWTFString(globalObject);
+ RETURN_IF_EXCEPTION(throwScope, { BunStringTag::Dead });
+ if (str.isEmpty()) {
+ return { BunStringTag::Dead };
+ }
+
+ auto url = WTF::URL(str);
+ if (!url.isValid() || url.isEmpty())
+ return { BunStringTag::Dead };
+
+ return Bun::toStringRef(url.string());
+}
+
+extern "C" BunString URL__getHref(BunString* input)
+{
+ auto str = Bun::toWTFString(*input);
+ auto url = WTF::URL(str);
+ if (!url.isValid() || url.isEmpty())
+ return { BunStringTag::Dead };
+
+ return Bun::toStringRef(url.string());
+}
+
+extern "C" WTF::URL* URL__fromString(BunString* input)
+{
+ auto str = Bun::toWTFString(*input);
+ auto url = WTF::URL(str);
+ if (!url.isValid())
+ return nullptr;
+
+ return new WTF::URL(WTFMove(url));
+}
+
+extern "C" BunString URL__protocol(WTF::URL* url)
+{
+ return Bun::toStringRef(url->protocol().toStringWithoutCopying());
+}
+
+extern "C" void URL__deinit(WTF::URL* url)
+{
+ delete url;
+}
+
+extern "C" BunString URL__href(WTF::URL* url)
+{
+ return Bun::toStringRef(url->string());
+}
+
+extern "C" BunString URL__username(WTF::URL* url)
+{
+ return Bun::toStringRef(url->user());
+}
+
+extern "C" BunString URL__password(WTF::URL* url)
+{
+ return Bun::toStringRef(url->password());
+}
+
+extern "C" BunString URL__search(WTF::URL* url)
+{
+ return Bun::toStringRef(url->query().toStringWithoutCopying());
+}
+
+extern "C" BunString URL__host(WTF::URL* url)
+{
+ return Bun::toStringRef(url->host().toStringWithoutCopying());
+}
+extern "C" BunString URL__hostname(WTF::URL* url)
+{
+ return Bun::toStringRef(url->hostAndPort());
+}
+
+extern "C" uint32_t URL__port(WTF::URL* url)
+{
+ auto port = url->port();
+
+ if (port.has_value()) {
+ return port.value();
+ }
+
+ return std::numeric_limits<uint32_t>::max();
+}
+
+extern "C" BunString URL__pathname(WTF::URL* url)
+{
+ return Bun::toStringRef(url->path().toStringWithoutCopying());
} \ No newline at end of file