Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,12 @@ async function post ({ archivePath, target, packmgrPath, checkIfUp }) {
const result = { target }
try {
const urlObj = new URL(target + packmgrPath)
const url = urlObj.origin + urlObj.pathname
const fetchArgs = { method: 'POST', body: form }
if (urlObj.password) {
const credentials = Buffer.from(`${urlObj.username}:${urlObj.password}`).toString('base64')
fetchArgs.headers = { Authorization: `Basic ${credentials}` }
fetchArgs.headers = { Authorization: extractBasicAuth(urlObj) }
}

const res = await fetch(url, fetchArgs)
const res = await fetch(urlObj, fetchArgs)

if (res.ok) {
const text = await res.text()
Expand Down Expand Up @@ -128,15 +126,20 @@ async function post ({ archivePath, target, packmgrPath, checkIfUp }) {
return result
}

function extractBasicAuth(urlObj) {
const credentials = `${decodeURIComponent(urlObj.username)}:${decodeURIComponent(urlObj.password)}`
urlObj.username = ''
urlObj.password = ''
return `Basic ${Buffer.from(credentials).toString('base64')}`
}


async function check (target) {
try {
// Convert embedded credentials to basic auth.
const url = new URL(target)
const auth = `${url.username}:${url.password}`
url.username = ''
url.password = ''
const res = await fetch(target, {
headers: { Authorization: 'Basic ' + Buffer.from(auth).toString('base64') }
const res = await fetch(url, {
headers: { Authorization: extractBasicAuth(url) }
})

return res.status === 200
Expand Down