use WTF::URL

This commit is contained in:
Dylan Conway
2025-10-03 17:20:46 -07:00
parent 34a03152d2
commit a75ef0877f
4 changed files with 38 additions and 8 deletions

View File

@@ -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: {

View File

@@ -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;

View File

@@ -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);

View File

@@ -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;