Skip to content
Merged
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
59 changes: 25 additions & 34 deletions src/tools/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"

"log"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -80,44 +80,35 @@ func ProxyResponseHandler() func(*http.Response) error {
}
}

// Do some quick fixes to the HTTP response for NPM install requests
// Also support for pnpm and bun
if strings.HasPrefix(r.Request.UserAgent(), "npm") ||
strings.HasPrefix(r.Request.UserAgent(), "pnpm") ||
strings.HasPrefix(r.Request.UserAgent(), "yarn") ||
strings.HasPrefix(r.Request.UserAgent(), "Bun") {

// Respond to only requests that respond with JSON
// There might eventually be additional headers i don't know about?
if !strings.Contains(contentType, "application/json") && !strings.Contains(contentType, "application/vnd.npm.install-v1+json") {
return nil
}

var body io.ReadCloser
// Respond to only requests that respond with JSON
// There might eventually be additional headers i don't know about?
if !strings.Contains(contentType, "application/json") && !strings.Contains(contentType, "application/vnd.npm.install-v1+json") {
return nil
}

if r.Header.Get("Content-Encoding") == "gzip" {
body, _ = gzip.NewReader(r.Body)
r.Header.Del("Content-Encoding")
} else {
body = r.Body
}
var body io.ReadCloser
if r.Header.Get("Content-Encoding") == "gzip" {
body, _ = gzip.NewReader(r.Body)
r.Header.Del("Content-Encoding")
} else {
body = r.Body
}

// replace any instances of the CodeArtifact URL with the local URL
oldContentResponse, _ := ioutil.ReadAll(body)
oldContentResponseStr := string(oldContentResponse)
// replace any instances of the CodeArtifact URL with the local URL
oldContentResponse, _ := io.ReadAll(body)
oldContentResponseStr := string(oldContentResponse)

mutex.Lock()
resolvedHostname := strings.Replace(CodeArtifactAuthInfo.Url, u.Host, hostname, -1)
newUrl := fmt.Sprintf("%s://%s/", originalUrl.Scheme, originalUrl.Host)
mutex.Lock()
resolvedHostname := strings.ReplaceAll(CodeArtifactAuthInfo.Url, u.Host, hostname)
newUrl := fmt.Sprintf("%s://%s/", originalUrl.Scheme, originalUrl.Host)

newResponseContent := strings.Replace(oldContentResponseStr, resolvedHostname, newUrl, -1)
newResponseContent = strings.Replace(newResponseContent, CodeArtifactAuthInfo.Url, newUrl, -1)
mutex.Unlock()
newResponseContent := strings.ReplaceAll(oldContentResponseStr, resolvedHostname, newUrl)
newResponseContent = strings.ReplaceAll(newResponseContent, CodeArtifactAuthInfo.Url, newUrl)
mutex.Unlock()

r.Body = ioutil.NopCloser(strings.NewReader(newResponseContent))
r.ContentLength = int64(len(newResponseContent))
r.Header.Set("Content-Length", strconv.Itoa(len(newResponseContent)))
}
r.Body = io.NopCloser(strings.NewReader(newResponseContent))
r.ContentLength = int64(len(newResponseContent))
r.Header.Set("Content-Length", strconv.Itoa(len(newResponseContent)))

return nil
}
Expand Down