diff options
author | 2022-09-16 16:45:26 -0700 | |
---|---|---|
committer | 2022-09-16 16:45:26 -0700 | |
commit | f55b9a853064f276c918fe3f91df55c48f901f86 (patch) | |
tree | 116eb307fa1d6c055cc613e1700c7f3228a7159b | |
parent | da6f954e0dd851af4ad206de74fbcd500d3712e6 (diff) | |
download | bun-f55b9a853064f276c918fe3f91df55c48f901f86.tar.gz bun-f55b9a853064f276c918fe3f91df55c48f901f86.tar.zst bun-f55b9a853064f276c918fe3f91df55c48f901f86.zip |
Fix `origin` missing `protocol` in `URL`
Fixes https://github.com/oven-sh/bun/issues/1244
-rw-r--r-- | src/bun.js/bindings/URLDecomposition.cpp | 16 | ||||
-rw-r--r-- | test/bun.js/web-globals.test.js | 9 |
2 files changed, 24 insertions, 1 deletions
diff --git a/src/bun.js/bindings/URLDecomposition.cpp b/src/bun.js/bindings/URLDecomposition.cpp index 84e734528..f13d6b093 100644 --- a/src/bun.js/bindings/URLDecomposition.cpp +++ b/src/bun.js/bindings/URLDecomposition.cpp @@ -31,7 +31,21 @@ namespace WebCore { String URLDecomposition::origin() const { - return fullURL().hostAndPort(); + auto fullURL = this->fullURL(); + auto protocol = fullURL.protocol(); + auto host = fullURL.host(); + auto port = fullURL.port(); + + if (protocol == "file"_s) + return "file://"_s; + + if (protocol.isEmpty() && host.isEmpty()) + return {}; + + if (!port) + return makeString(protocol, "://", host); + + return makeString(protocol, "://", host, ':', static_cast<uint32_t>(*port)); } String URLDecomposition::protocol() const diff --git a/test/bun.js/web-globals.test.js b/test/bun.js/web-globals.test.js index c5740b0fc..70c2f01e5 100644 --- a/test/bun.js/web-globals.test.js +++ b/test/bun.js/web-globals.test.js @@ -96,3 +96,12 @@ it("crypto.randomUUID", () => { expect(uuid2[23]).toBe("-"); } }); + +it("URL.prototype.origin", () => { + const url = new URL("https://html.spec.whatwg.org/"); + const { origin, host, hostname } = url; + + expect(hostname).toBe("html.spec.whatwg.org"); + expect(host).toBe("html.spec.whatwg.org"); + expect(origin).toBe("https://html.spec.whatwg.org"); +}); |