From a75ef0877f8490e710b783e77288d813544ddecf Mon Sep 17 00:00:00 2001 From: Dylan Conway Date: Fri, 3 Oct 2025 17:20:46 -0700 Subject: [PATCH] use WTF::URL --- src/bun.js/api/bun/socket/SocketAddress.zig | 2 +- src/bun.js/bindings/BunString.cpp | 6 +++++ src/bun.js/bindings/URL.zig | 12 +++++++--- src/cli/publish_command.zig | 26 +++++++++++++++++---- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/bun.js/api/bun/socket/SocketAddress.zig b/src/bun.js/api/bun/socket/SocketAddress.zig index 1e0e8fe634..ebf4bc2345 100644 --- a/src/bun.js/api/bun/socket/SocketAddress.zig +++ b/src/bun.js/api/bun/socket/SocketAddress.zig @@ -116,7 +116,7 @@ pub fn parse(global: *jsc.JSGlobalObject, callframe: *jsc.CallFrame) bun.JSError }; defer url_str.deref(); - const url = jsc.URL.fromString(url_str) orelse return .js_undefined; + const url = jsc.URL.fromString(url_str) catch return .js_undefined; defer url.deinit(); const host = url.host(); const port_: u16 = blk: { diff --git a/src/bun.js/bindings/BunString.cpp b/src/bun.js/bindings/BunString.cpp index 3fd23c573b..58888529a1 100644 --- a/src/bun.js/bindings/BunString.cpp +++ b/src/bun.js/bindings/BunString.cpp @@ -639,6 +639,12 @@ extern "C" BunString URL__protocol(WTF::URL* url) return Bun::toStringRef(url->protocol().toStringWithoutCopying()); } +extern "C" void URL__setProtocol(WTF::URL* url, BunString newProtocol) +{ + String newProtocolStr = newProtocol.toWTFString(BunString::ZeroCopy); + url->setProtocol(newProtocolStr); +} + extern "C" void URL__deinit(WTF::URL* url) { delete url; diff --git a/src/bun.js/bindings/URL.zig b/src/bun.js/bindings/URL.zig index 8bd1c7b96a..28afff8843 100644 --- a/src/bun.js/bindings/URL.zig +++ b/src/bun.js/bindings/URL.zig @@ -2,6 +2,7 @@ pub const URL = opaque { extern fn URL__fromJS(JSValue, *jsc.JSGlobalObject) ?*URL; extern fn URL__fromString(*bun.String) ?*URL; extern fn URL__protocol(*URL) String; + extern fn URL__setProtocol(*URL, new_protocol: String) void; extern fn URL__href(*URL) String; extern fn URL__username(*URL) String; extern fn URL__password(*URL) String; @@ -58,18 +59,23 @@ pub const URL = opaque { return result; } - pub fn fromUTF8(input: []const u8) ?*URL { + pub fn fromUTF8(input: []const u8) error{InvalidUrl}!*URL { return fromString(String.borrowUTF8(input)); } - pub fn fromString(str: bun.String) ?*URL { + pub fn fromString(str: bun.String) error{InvalidUrl}!*URL { jsc.markBinding(@src()); var input = str; - return URL__fromString(&input); + return URL__fromString(&input) orelse { + return error.InvalidUrl; + }; } pub fn protocol(url: *URL) String { jsc.markBinding(@src()); return URL__protocol(url); } + pub fn setProtocol(url: *URL, new_protocol: String) void { + URL__setProtocol(url, new_protocol); + } pub fn href(url: *URL) String { jsc.markBinding(@src()); return URL__href(url); diff --git a/src/cli/publish_command.zig b/src/cli/publish_command.zig index 3dfd6e33d4..02dca5cfd8 100644 --- a/src/cli/publish_command.zig +++ b/src/cli/publish_command.zig @@ -970,6 +970,25 @@ pub const PublishCommand = struct { logger.Loc.Empty, ), }; + + var registry_url: *jsc.URL = jsc.URL.fromUTF8(registry.url.href) catch |err| { + Output.err(err, "failed to parse registry url: {s}", .{registry.url.href}); + Global.exit(1); + }; + defer registry_url.deinit(); + + // always replace https with http + // https://github.com/npm/cli/blob/9281ebf8e428d40450ad75ba61bc6f040b3bf896/workspaces/libnpmpublish/lib/publish.js#L120 + if (registry_url.protocol().eqlUTF8("https")) { + registry_url.setProtocol(bun.String.static("http")); + } + + const registry_url_str = registry_url.href(); + defer registry_url_str.deref(); + + const registry_url_str_slice = registry_url_str.toSlice(bun.default_allocator); + defer registry_url_str_slice.deinit(); + dist_props[2] = .{ .key = Expr.init( E.String, @@ -979,10 +998,8 @@ pub const PublishCommand = struct { .value = Expr.init( E.String, .{ - .data = try std.fmt.allocPrint(allocator, "http{s}/{s}/-/{}", .{ - // always replace https with http - // https://github.com/npm/cli/blob/9281ebf8e428d40450ad75ba61bc6f040b3bf896/workspaces/libnpmpublish/lib/publish.js#L120 - strings.withoutTrailingSlash(strings.withoutPrefix(registry.url.href, registry.url.protocol)), + .data = try std.fmt.allocPrint(allocator, "{s}/{s}/-/{}", .{ + strings.withoutTrailingSlash(registry_url_str_slice.slice()), package_name, Pack.fmtTarballFilename(package_name, package_version, .raw), }), @@ -1480,3 +1497,4 @@ const Dependency = install.Dependency; const Lockfile = install.Lockfile; const Npm = install.Npm; const PackageManager = install.PackageManager; +const jsc = bun.jsc;