Skip to content

Commit

Permalink
Encode slashes in package names in the registry manifest request
Browse files Browse the repository at this point in the history
Co-Authored-By: Max Brosnahan <1177034+gingermusketeer@users.noreply.github.com>
  • Loading branch information
Jarred-Sumner and gingermusketeer committed Sep 19, 2023
1 parent 9d3f60d commit 88cbf23
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,22 @@ const NetworkTask = struct {
warn_on_error: bool,
) !void {
this.url_buf = blk: {

// Not all registries support scoped package names when fetching the manifest.
// registry.npmjs.org supports both "@storybook%2Faddons" and "@storybook/addons"
// Other registries like AWS codeartifact only support the former.
// "npm" CLI requests the the manifest with the encoded name.
var arena = std.heap.ArenaAllocator.init(bun.default_allocator);
defer arena.deinit();
var stack_fallback_allocator = std.heap.stackFallback(512, arena.allocator());
var encoded_name = name;
if (strings.containsChar(name, '/')) {
encoded_name = try std.mem.replaceOwned(u8, stack_fallback_allocator.get(), name, "/", "%2f");
}

const tmp = bun.JSC.URL.join(
bun.String.fromUTF8(scope.url.href),
bun.String.fromUTF8(name),
bun.String.fromUTF8(encoded_name),
);
defer tmp.deref();

Expand Down
4 changes: 2 additions & 2 deletions test/cli/install/bun-add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ it("should handle @scoped names", async () => {
});
expect(stderr).toBeDefined();
const err = await new Response(stderr).text();
expect(err.split(/\r?\n/)).toContain('error: package "@bar/baz" not found localhost/@bar/baz 404');
expect(err.split(/\r?\n/)).toContain('error: package "@bar/baz" not found localhost/@bar%2fbaz 404');
expect(stdout).toBeDefined();
expect(await new Response(stdout).text()).toBe("");
expect(await exited).toBe(1);
expect(urls.sort()).toEqual([`${root_url}/@bar/baz`]);
expect(urls.sort()).toEqual([`${root_url}/@bar%2fbaz`]);
expect(requested).toBe(1);
try {
await access(join(package_dir, "bun.lockb"));
Expand Down

0 comments on commit 88cbf23

Please sign in to comment.