Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update references to reqOrigin to use cdnOrigin #529

Merged
merged 1 commit into from
Feb 25, 2023
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
16 changes: 8 additions & 8 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func esmHandler() rex.Handle {

// redirect `/@types/` to `.d.ts` files
if strings.HasPrefix(reqPkg.Name, "@types/") && (reqPkg.Submodule == "" || !strings.HasSuffix(reqPkg.Submodule, ".d.ts")) {
url := fmt.Sprintf("%s%s/v%d%s", reqOrigin, cfg.BasePath, VERSION, pathname)
url := fmt.Sprintf("%s%s/v%d%s", cdnOrigin, cfg.BasePath, VERSION, pathname)
if reqPkg.Submodule == "" {
info, _, err := getPackageInfo("", reqPkg.Name, reqPkg.Version)
if err != nil {
Expand All @@ -271,7 +271,7 @@ func esmHandler() rex.Handle {
// redirect to css for CSS packages
css := cssPackages[reqPkg.Name]
if css != "" && reqPkg.Submodule == "" {
url := fmt.Sprintf("%s%s/%s/%s", reqOrigin, cfg.BasePath, reqPkg.String(), css)
url := fmt.Sprintf("%s%s/%s/%s", cdnOrigin, cfg.BasePath, reqPkg.String(), css)
return rex.Redirect(url, http.StatusFound)
}

Expand Down Expand Up @@ -299,12 +299,12 @@ func esmHandler() rex.Handle {
if ctx.R.URL.RawQuery != "" {
query = "&" + ctx.R.URL.RawQuery
}
return rex.Redirect(fmt.Sprintf("%s%s/%s%s@%s%s%s", reqOrigin, cfg.BasePath, eaSign, reqPkg.Name, reqPkg.Version, query, submodule), http.StatusFound)
return rex.Redirect(fmt.Sprintf("%s%s/%s%s@%s%s%s", cdnOrigin, cfg.BasePath, eaSign, reqPkg.Name, reqPkg.Version, query, submodule), http.StatusFound)
}
if ctx.R.URL.RawQuery != "" {
query = "?" + ctx.R.URL.RawQuery
}
return rex.Redirect(fmt.Sprintf("%s%s/%s%s%s", reqOrigin, cfg.BasePath, eaSign, reqPkg.String(), query), http.StatusFound)
return rex.Redirect(fmt.Sprintf("%s%s/%s%s%s", cdnOrigin, cfg.BasePath, eaSign, reqPkg.String(), query), http.StatusFound)
}

// redirect to the url with full package version
Expand Down Expand Up @@ -333,7 +333,7 @@ func esmHandler() rex.Handle {
if ctx.R.URL.RawQuery != "" {
query = "?" + ctx.R.URL.RawQuery
}
return rex.Redirect(fmt.Sprintf("%s%s%s/%s@%s%s%s", reqOrigin, cfg.BasePath, prefix, reqPkg.Name, reqPkg.Version, subpath, query), http.StatusFound)
return rex.Redirect(fmt.Sprintf("%s%s%s/%s@%s%s%s", cdnOrigin, cfg.BasePath, prefix, reqPkg.Name, reqPkg.Version, subpath, query), http.StatusFound)
}

// support `https://esm.sh/react?dev&target=es2020/jsx-runtime` for jsx transformer
Expand Down Expand Up @@ -384,7 +384,7 @@ func esmHandler() rex.Handle {
// serve raw dist files like CSS that is fetching from npmCDN
if storageType == "raw" {
if !regexpFullVersionPath.MatchString(pathname) {
url := fmt.Sprintf("%s%s/%s", reqOrigin, cfg.BasePath, reqPkg.String())
url := fmt.Sprintf("%s%s/%s", cdnOrigin, cfg.BasePath, reqPkg.String())
return rex.Redirect(url, http.StatusFound)
}

Expand Down Expand Up @@ -760,7 +760,7 @@ func esmHandler() rex.Handle {
if esm.Dts != "" && !noCheck {
value := fmt.Sprintf(
"%s%s/%s",
reqOrigin,
cdnOrigin,
cfg.BasePath,
strings.TrimPrefix(esm.Dts, "/"),
)
Expand All @@ -777,7 +777,7 @@ func esmHandler() rex.Handle {
}

if !regexpFullVersionPath.MatchString(pathname) || !isPined || targetFromUA {
url := fmt.Sprintf("%s%s/%s.css", reqOrigin, cfg.BasePath, strings.TrimSuffix(taskID, ".js"))
url := fmt.Sprintf("%s%s/%s.css", cdnOrigin, cfg.BasePath, strings.TrimSuffix(taskID, ".js"))
return rex.Redirect(url, http.StatusFound)
}

Expand Down