diff options
-rw-r--r-- | src/install/install.zig | 111 | ||||
-rw-r--r-- | src/install/npm.zig | 4 | ||||
-rw-r--r-- | test/bun.js/bun-install.test.ts | 38 | ||||
-rw-r--r-- | test/bun.js/install/basic.toml (renamed from test/bun.js/bun-install.toml) | 3 | ||||
-rw-r--r-- | test/bun.js/install/bun-install.test.ts | 80 |
5 files changed, 168 insertions, 68 deletions
diff --git a/src/install/install.zig b/src/install/install.zig index 7fba71b76..5dfddf78a 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -3590,20 +3590,28 @@ pub const PackageManager = struct { ) !void { this.save_lockfile_path = this.lockfile_path; - var registry = if (bun_install_) |bun_install| bun_install.default_registry else null; - this.scope = try Npm.Registry.Scope.fromAPI("", registry orelse Api.NpmRegistry{ + var base = Api.NpmRegistry{ .url = "", .username = "", .password = "", .token = "", - }, allocator, env_loader); + }; + if (bun_install_) |bun_install| { + if (bun_install.default_registry) |registry| { + base = registry; + } + } + if (base.url.len == 0) base.url = Npm.Registry.default_url; + this.scope = try Npm.Registry.Scope.fromAPI("", base, allocator, env_loader); defer { - this.did_override_default_scope = !strings.eqlComptime(this.scope.url.href, Npm.Registry.DefaultURL); + this.did_override_default_scope = !strings.eqlComptime(this.scope.url.href, Npm.Registry.default_url); } if (bun_install_) |bun_install| { if (bun_install.scoped) |scoped| { for (scoped.scopes) |name, i| { - try this.registries.put(allocator, Npm.Registry.Scope.hash(name), try Npm.Registry.Scope.fromAPI(name, scoped.registries[i], allocator, env_loader)); + var registry = scoped.registries[i]; + if (registry.url.len == 0) registry.url = base.url; + try this.registries.put(allocator, Npm.Registry.Scope.hash(name), try Npm.Registry.Scope.fromAPI(name, registry, allocator, env_loader)); } } @@ -6787,10 +6795,10 @@ test "PackageManager.Options - default registry, default values" { try options.load(allocator, &log, &env_loader, null, null); - try std.testing.expectEqualStrings(options.scope.name, ""); - try std.testing.expectEqualStrings(options.scope.auth, ""); - try std.testing.expectEqualStrings(options.scope.url.href, Npm.Registry.DefaultURL); - try std.testing.expectEqualStrings(options.scope.token, ""); + try std.testing.expectEqualStrings("", options.scope.name); + try std.testing.expectEqualStrings("", options.scope.auth); + try std.testing.expectEqualStrings(Npm.Registry.default_url, options.scope.url.href); + try std.testing.expectEqualStrings("", options.scope.token); } test "PackageManager.Options - default registry, custom token" { @@ -6801,9 +6809,9 @@ test "PackageManager.Options - default registry, custom token" { var install = Api.BunInstall{ .default_registry = Api.NpmRegistry{ .url = "", - .username = "", - .password = "", - .token = "foo", + .username = "foo", + .password = "bar", + .token = "baz", }, .native_bin_links = &.{}, }; @@ -6811,10 +6819,10 @@ test "PackageManager.Options - default registry, custom token" { try options.load(allocator, &log, &env_loader, null, &install); - try std.testing.expectEqualStrings(options.scope.name, ""); - try std.testing.expectEqualStrings(options.scope.auth, ""); - try std.testing.expectEqualStrings(options.scope.url.href, Npm.Registry.DefaultURL); - try std.testing.expectEqualStrings(options.scope.token, "foo"); + try std.testing.expectEqualStrings("", options.scope.name); + try std.testing.expectEqualStrings("", options.scope.auth); + try std.testing.expectEqualStrings(Npm.Registry.default_url, options.scope.url.href); + try std.testing.expectEqualStrings("baz", options.scope.token); } test "PackageManager.Options - default registry, custom URL" { @@ -6835,10 +6843,10 @@ test "PackageManager.Options - default registry, custom URL" { try options.load(allocator, &log, &env_loader, null, &install); - try std.testing.expectEqualStrings(options.scope.name, ""); - try std.testing.expectEqualStrings(options.scope.auth, "Zm9vOmJhcg=="); - try std.testing.expectEqualStrings(options.scope.url.href, "https://example.com/"); - try std.testing.expectEqualStrings(options.scope.token, ""); + try std.testing.expectEqualStrings("", options.scope.name); + try std.testing.expectEqualStrings("Zm9vOmJhcg==", options.scope.auth); + try std.testing.expectEqualStrings("https://example.com/", options.scope.url.href); + try std.testing.expectEqualStrings("", options.scope.token); } test "PackageManager.Options - scoped registry" { @@ -6866,18 +6874,65 @@ test "PackageManager.Options - scoped registry" { try options.load(allocator, &log, &env_loader, null, &install); - try std.testing.expectEqualStrings(options.scope.name, ""); - try std.testing.expectEqualStrings(options.scope.auth, ""); - try std.testing.expectEqualStrings(options.scope.url.href, Npm.Registry.DefaultURL); - try std.testing.expectEqualStrings(options.scope.token, ""); + try std.testing.expectEqualStrings("", options.scope.name); + try std.testing.expectEqualStrings("", options.scope.auth); + try std.testing.expectEqualStrings(Npm.Registry.default_url, options.scope.url.href); + try std.testing.expectEqualStrings("", options.scope.token); var scoped = options.registries.getPtr(Npm.Registry.Scope.hash(Npm.Registry.Scope.getName("foo"))); try std.testing.expect(scoped != null); if (scoped) |scope| { - try std.testing.expectEqualStrings(scope.name, "foo"); - try std.testing.expectEqualStrings(scope.auth, ""); - try std.testing.expectEqualStrings(scope.url.href, Npm.Registry.DefaultURL); - try std.testing.expectEqualStrings(scope.token, "bar"); + try std.testing.expectEqualStrings("foo", scope.name); + try std.testing.expectEqualStrings("", scope.auth); + try std.testing.expectEqualStrings(Npm.Registry.default_url, scope.url.href); + try std.testing.expectEqualStrings("bar", scope.token); + } +} + +test "PackageManager.Options - mixed default/scoped registry" { + var allocator = default_allocator; + var log = logger.Log.init(allocator); + defer log.deinit(); + var env_loader = DotEnv.Loader.init(&DotEnv.Map.init(allocator), allocator); + var install = Api.BunInstall{ + .default_registry = Api.NpmRegistry{ + .url = "https://example.com/", + .username = "", + .password = "", + .token = "foo", + }, + .scoped = Api.NpmRegistryMap{ + .scopes = &.{ + "bar", + }, + .registries = &.{ + Api.NpmRegistry{ + .url = "", + .username = "baz", + .password = "moo", + .token = "", + }, + }, + }, + .native_bin_links = &.{}, + }; + var options = PackageManager.Options{}; + + try options.load(allocator, &log, &env_loader, null, &install); + + try std.testing.expectEqualStrings("", options.scope.name); + try std.testing.expectEqualStrings("", options.scope.auth); + try std.testing.expectEqualStrings("https://example.com/", options.scope.url.href); + try std.testing.expectEqualStrings("foo", options.scope.token); + + var scoped = options.registries.getPtr(Npm.Registry.Scope.hash(Npm.Registry.Scope.getName("bar"))); + + try std.testing.expect(scoped != null); + if (scoped) |scope| { + try std.testing.expectEqualStrings("bar", scope.name); + try std.testing.expectEqualStrings("YmF6Om1vbw==", scope.auth); + try std.testing.expectEqualStrings("https://example.com/", scope.url.href); + try std.testing.expectEqualStrings("", scope.token); } } diff --git a/src/install/npm.zig b/src/install/npm.zig index 3874c2c01..60ffefdbb 100644 --- a/src/install/npm.zig +++ b/src/install/npm.zig @@ -35,7 +35,7 @@ const ComptimeStringMap = @import("../comptime_string_map.zig").ComptimeStringMa const Npm = @This(); pub const Registry = struct { - pub const DefaultURL = "https://registry.npmjs.org/"; + pub const default_url = "https://registry.npmjs.org/"; pub const BodyPool = ObjectPool(MutableString, MutableString.init2048, true, 8); pub const Scope = struct { @@ -67,7 +67,7 @@ pub const Registry = struct { pub fn fromAPI(name: string, registry_: Api.NpmRegistry, allocator: std.mem.Allocator, env: *DotEnv.Loader) !Scope { var registry = registry_; - var url = URL.parse(if (registry.url.len == 0) DefaultURL else registry.url); + var url = URL.parse(registry.url); var auth: string = ""; if (registry.token.len == 0) { diff --git a/test/bun.js/bun-install.test.ts b/test/bun.js/bun-install.test.ts deleted file mode 100644 index 1bc4655eb..000000000 --- a/test/bun.js/bun-install.test.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { spawn, spawnSync } from "bun"; -import { describe, expect, it, test } from "bun:test"; -import { bunExe } from "bunExe"; - -test("bun install", async () => { - const urls = []; - const server = Bun.serve({ - async fetch(request) { - try { - expect(request.method).toBe("GET"); - expect(request.headers.get("accept")).toBe("application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*"); - expect(await request.text()).toBe(""); - urls.push(request.url); - return new Response("bar", { status: 404 }); - } finally { - server.stop(); - } - }, - port: 54321, - }); - const { stdout, stderr, exited } = spawn({ - cmd: [bunExe(), "install", "foo", "--config", import.meta.dir + "/bun-install.toml"], - stdout: null, - stdin: "pipe", - stderr: "pipe", - env: { - ...process.env, - BUN_DEBUG_QUIET_LOGS: "1", - }, - }); - expect(stdout).toBeDefined(); - expect(stderr).toBeDefined(); - expect(await new Response(stdout).text()).toBe(""); - var err = await new Response(stderr).text(); - expect(err.split(/\n/)).toContain('error: package "foo" not found localhost/foo 404'); - expect(urls).toContain("http://localhost:54321/foo"); - expect(await exited).toBe(1); -}); diff --git a/test/bun.js/bun-install.toml b/test/bun.js/install/basic.toml index 112c61f86..5007a8efd 100644 --- a/test/bun.js/bun-install.toml +++ b/test/bun.js/install/basic.toml @@ -1,2 +1,5 @@ [install] registry = "http://localhost:54321/" + +[install.scopes] +foo = { token = "bar" }
\ No newline at end of file diff --git a/test/bun.js/install/bun-install.test.ts b/test/bun.js/install/bun-install.test.ts new file mode 100644 index 000000000..8a05ef802 --- /dev/null +++ b/test/bun.js/install/bun-install.test.ts @@ -0,0 +1,80 @@ +import { spawn, spawnSync } from "bun"; +import { describe, expect, it, test } from "bun:test"; +import { bunExe } from "bunExe"; + +test("bun install", async () => { + const urls = []; + const server = Bun.serve({ + async fetch(request) { + try { + expect(request.method).toBe("GET"); + expect(request.headers.get("accept")).toBe("application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*"); + expect(await request.text()).toBe(""); + urls.push(request.url); + return new Response("bar", { status: 404 }); + } finally { + server.stop(); + } + }, + port: 54321, + }); + const { stdout, stderr, exited } = spawn({ + cmd: [bunExe(), "install", "foo", "--config", import.meta.dir + "/basic.toml"], + stdout: null, + stdin: "pipe", + stderr: "pipe", + env: { + ...process.env, + BUN_DEBUG_QUIET_LOGS: "1", + }, + }); + expect(stdout).toBeDefined(); + expect(stderr).toBeDefined(); + expect(await new Response(stdout).text()).toBe(""); + var err = await new Response(stderr).text(); + expect(err.split(/\n/)).toContain('error: package "foo" not found localhost/foo 404'); + expect(urls).toContain("http://localhost:54321/foo"); + expect(await exited).toBe(1); +}); + +test("bun install @scoped", async () => { + let seen_token = false; + const url = "http://localhost:54321/@foo/bar"; + const urls = []; + const server = Bun.serve({ + async fetch(request) { + try { + expect(request.method).toBe("GET"); + expect(request.headers.get("accept")).toBe("application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*"); + if (request.url === url) { + expect(request.headers.get("authorization")).toBe("Bearer bar"); + seen_token = true; + } + expect(await request.text()).toBe(""); + urls.push(request.url); + return new Response("Tea?", { status: 418 }); + } finally { + server.stop(); + } + }, + port: 54321, + }); + const { stdout, stderr, exited } = spawn({ + cmd: [bunExe(), "install", "@foo/bar", "--config", import.meta.dir + "/basic.toml"], + stdout: null, + stdin: "pipe", + stderr: "pipe", + env: { + ...process.env, + BUN_DEBUG_QUIET_LOGS: "1", + }, + }); + expect(stdout).toBeDefined(); + expect(stderr).toBeDefined(); + expect(await new Response(stdout).text()).toBe(""); + var err = await new Response(stderr).text(); + expect(err.split(/\n/)).toContain(`GET ${url} - 418`); + expect(urls).toContain(url); + expect(seen_token).toBe(true); + expect(await exited).toBe(1); +}); |