Skip to content

Commit

Permalink
Fix content compression (close #460)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Dec 9, 2022
1 parent 0d19ce6 commit 254319f
Showing 1 changed file with 38 additions and 48 deletions.
86 changes: 38 additions & 48 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,64 +375,54 @@ func esmHandler(options esmHandlerOptions) rex.Handle {

// serve raw dist files like CSS that is fetching from unpkg.com
if storageType == "raw" {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !regFullVersionPath.MatchString(pathname) {
url := fmt.Sprintf("%s/%s", origin, reqPkg.String())
http.Redirect(w, r, url, http.StatusFound)
return
}
savePath := path.Join("raw", reqPkg.String())
exists, size, modtime, err := fs.Exists(savePath)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
if !regFullVersionPath.MatchString(pathname) {
url := fmt.Sprintf("%s/%s", origin, reqPkg.String())
return rex.Redirect(url, http.StatusFound)
}

// fetch the non-existent file from unpkg.com and save to fs
if !exists {
resp, err := httpClient.Get(fmt.Sprintf("%s/%s", strings.TrimSuffix(options.unpkgOrigin, "/"), reqPkg.String()))
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
defer resp.Body.Close()
savePath := path.Join("raw", reqPkg.String())
exists, size, modtime, err := fs.Exists(savePath)
if err != nil {
return rex.Status(500, err.Error())
}

if resp.StatusCode >= 500 {
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte("Bad Gateway"))
return
}
// fetch the non-existent file from unpkg.com and save to fs
if !exists {
resp, err := httpClient.Get(fmt.Sprintf("%s/%s", strings.TrimSuffix(options.unpkgOrigin, "/"), reqPkg.String()))
if err != nil {
return rex.Status(http.StatusBadGateway, "Bad Gateway")
}
defer resp.Body.Close()

if resp.StatusCode >= 400 {
w.WriteHeader(http.StatusBadGateway)
io.Copy(w, resp.Body)
return
}
if resp.StatusCode >= 500 {
return rex.Status(http.StatusBadGateway, "Bad Gateway")
}

size, err = fs.WriteFile(savePath, resp.Body)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
if resp.StatusCode >= 400 {
if resp.StatusCode == 404 {
return rex.Status(404, "Not Found")
}
return rex.Status(http.StatusBadGateway, "Bad Gateway")
}

f, err := fs.ReadFile(savePath, size)
size, err = fs.WriteFile(savePath, resp.Body)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
return rex.Status(500, err.Error())
}
defer f.Close()
}

if strings.HasSuffix(pathname, ".ts") {
w.Header().Set("Content-Type", "application/typescript")
}
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
http.ServeContent(w, r, savePath, modtime, f)
})
f, err := fs.ReadFile(savePath, size)
if err != nil {
return rex.Status(500, err.Error())
}
// rex 1.8.1 has bug that will close the file repeatedly
// defer f.Close()

if strings.HasSuffix(pathname, ".ts") {
ctx.SetHeader("Content-Type", "application/typescript")
}
ctx.SetHeader("Cache-Control", "public, max-age=31536000, immutable")
return rex.Content(savePath, modtime, f)
}

// serve build files
Expand Down

0 comments on commit 254319f

Please sign in to comment.