aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Alex Lam S.L <alexlamsl@gmail.com> 2022-12-27 07:15:58 +0800
committerGravatar GitHub <noreply@github.com> 2022-12-26 15:15:58 -0800
commitc4ca4c70d194f63cc6598c8452656d97c3f7a5e6 (patch)
tree8b6aa6700d0e222050253129a022ec1c9770de39
parentd94b96d9f440d3aeef4402aec9f3788bca2972d0 (diff)
downloadbun-c4ca4c70d194f63cc6598c8452656d97c3f7a5e6.tar.gz
bun-c4ca4c70d194f63cc6598c8452656d97c3f7a5e6.tar.zst
bun-c4ca4c70d194f63cc6598c8452656d97c3f7a5e6.zip
[install] specify `auth-type` (#1667)
-rw-r--r--src/install/install.zig52
-rw-r--r--test/bun.js/install/bun-install.test.ts4
2 files changed, 32 insertions, 24 deletions
diff --git a/src/install/install.zig b/src/install/install.zig
index 5dfddf78a..2748a0af0 100644
--- a/src/install/install.zig
+++ b/src/install/install.zig
@@ -197,6 +197,30 @@ const NetworkTask = struct {
const default_headers_buf: string = "Accept" ++ accept_header_value;
+ fn appendAuth(header_builder: *HeaderBuilder, scope: *const Npm.Registry.Scope) void {
+ if (scope.token.len > 0) {
+ header_builder.appendFmt("Authorization", "Bearer {s}", .{scope.token});
+ } else if (scope.auth.len > 0) {
+ header_builder.appendFmt("Authorization", "Basic {s}", .{scope.auth});
+ } else {
+ return;
+ }
+ header_builder.append("npm-auth-type", "legacy");
+ }
+
+ fn countAuth(header_builder: *HeaderBuilder, scope: *const Npm.Registry.Scope) void {
+ if (scope.token.len > 0) {
+ header_builder.count("Authorization", "");
+ header_builder.content.cap += "Bearer ".len + scope.token.len;
+ } else if (scope.auth.len > 0) {
+ header_builder.count("Authorization", "");
+ header_builder.content.cap += "Basic ".len + scope.auth.len;
+ } else {
+ return;
+ }
+ header_builder.count("npm-auth-type", "legacy");
+ }
+
pub fn forManifest(
this: *NetworkTask,
name: string,
@@ -268,13 +292,7 @@ const NetworkTask = struct {
var header_builder = HeaderBuilder{};
- if (scope.token.len > 0) {
- header_builder.count("Authorization", "");
- header_builder.content.cap += "Bearer ".len + scope.token.len;
- } else if (scope.auth.len > 0) {
- header_builder.count("Authorization", "");
- header_builder.content.cap += "Basic ".len + scope.auth.len;
- }
+ countAuth(&header_builder, scope);
if (etag.len != 0) {
header_builder.count("If-None-Match", etag);
@@ -289,11 +307,7 @@ const NetworkTask = struct {
}
try header_builder.allocate(allocator);
- if (scope.token.len > 0) {
- header_builder.appendFmt("Authorization", "Bearer {s}", .{scope.token});
- } else if (scope.auth.len > 0) {
- header_builder.appendFmt("Authorization", "Basic {s}", .{scope.auth});
- }
+ appendAuth(&header_builder, scope);
if (etag.len != 0) {
header_builder.append("If-None-Match", etag);
@@ -381,23 +395,13 @@ const NetworkTask = struct {
var header_builder = HeaderBuilder{};
- if (scope.token.len > 0) {
- header_builder.count("Authorization", "");
- header_builder.content.cap += "Bearer ".len + scope.token.len;
- } else if (scope.auth.len > 0) {
- header_builder.count("Authorization", "");
- header_builder.content.cap += "Basic ".len + scope.auth.len;
- }
+ countAuth(&header_builder, scope);
var header_buf: string = "";
if (header_builder.header_count > 0) {
try header_builder.allocate(allocator);
- if (scope.token.len > 0) {
- header_builder.appendFmt("Authorization", "Bearer {s}", .{scope.token});
- } else if (scope.auth.len > 0) {
- header_builder.appendFmt("Authorization", "Basic {s}", .{scope.auth});
- }
+ appendAuth(&header_builder, scope);
header_buf = header_builder.content.ptr.?[0..header_builder.content.len];
}
diff --git a/test/bun.js/install/bun-install.test.ts b/test/bun.js/install/bun-install.test.ts
index 8a05ef802..58bd4918e 100644
--- a/test/bun.js/install/bun-install.test.ts
+++ b/test/bun.js/install/bun-install.test.ts
@@ -9,6 +9,7 @@ test("bun install", async () => {
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(request.headers.get("npm-auth-type")).toBe(null);
expect(await request.text()).toBe("");
urls.push(request.url);
return new Response("bar", { status: 404 });
@@ -48,7 +49,10 @@ test("bun install @scoped", async () => {
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");
+ expect(request.headers.get("npm-auth-type")).toBe("legacy");
seen_token = true;
+ } else {
+ expect(request.headers.get("npm-auth-type")).toBe(null);
}
expect(await request.text()).toBe("");
urls.push(request.url);