From 6efa34988229918debe6e881d45ba6715282f283 Mon Sep 17 00:00:00 2001 From: Alex Kessock Date: Wed, 24 Apr 2024 09:13:00 -0600 Subject: [PATCH] fix: COREPACK_NPM_REGISTRY should allow for username/password auth (#466) --- sources/httpUtils.ts | 4 ++-- tests/_registryServer.mjs | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/sources/httpUtils.ts b/sources/httpUtils.ts index 2095a333b..6c2dc9040 100644 --- a/sources/httpUtils.ts +++ b/sources/httpUtils.ts @@ -17,8 +17,8 @@ async function fetch(input: string | URL, init?: RequestInit) { let headers = init?.headers; - const username: string | undefined = input.username ?? process.env.COREPACK_NPM_USERNAME; - const password: string | undefined = input.password ?? process.env.COREPACK_NPM_PASSWORD; + const username: string | undefined = input.username || process.env.COREPACK_NPM_USERNAME; + const password: string | undefined = input.password || process.env.COREPACK_NPM_PASSWORD; if (username || password) { headers = { diff --git a/tests/_registryServer.mjs b/tests/_registryServer.mjs index ff0be263f..d051b0a66 100644 --- a/tests/_registryServer.mjs +++ b/tests/_registryServer.mjs @@ -116,8 +116,10 @@ const server = createServer((req, res) => { const auth = req.headers.authorization; if ( - (auth?.startsWith(`Bearer `) && auth.slice(`Bearer `.length) !== TOKEN_MOCK) || - (auth?.startsWith(`Basic `) && Buffer.from(auth.slice(`Basic `.length), `base64`).toString() !== `user:pass`) + auth == null || + (auth.startsWith(`Bearer `) && auth.slice(`Bearer `.length) !== TOKEN_MOCK) || + (auth.startsWith(`Basic `) && Buffer.from(auth.slice(`Basic `.length), `base64`).toString() !== `user:pass`) || + !/^(Basic|Bearer) /.test(auth) ) { res.writeHead(401).end(`Unauthorized`); return;