Skip to content

Fix legacy dts hostname #1129

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

Merged
merged 2 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
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
29 changes: 22 additions & 7 deletions server/legacy_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,20 @@ func legacyESM(ctx *rex.Context, buildStorage storage.Storage, buildVersionPrefi
ctx.SetHeader("Content-Type", ctJavaScript)
case ".ts", ".mts":
ctx.SetHeader("Content-Type", ctTypeScript)
// resolve hostname in typescript definition files if the origin is not "https://esm.sh"
if endsWith(pathname, ".d.ts", ".d.mts") {
origin := getOrigin(ctx)
if origin != "https://esm.sh" {
defer f.Close()
data, err := io.ReadAll(f)
if err != nil {
return rex.Status(500, "Failed to read data from storage")
}
data = bytes.ReplaceAll(data, []byte("https://esm.sh/v"), []byte(origin+"/v"))
data = bytes.ReplaceAll(data, []byte(config.LegacyServer+"/v"), []byte(origin+"/v"))
return data
}
}
case ".map":
ctx.SetHeader("Content-Type", ctJSON)
case ".css":
Expand Down Expand Up @@ -273,19 +287,20 @@ func legacyESM(ctx *rex.Context, buildStorage storage.Storage, buildVersionPrefi
if err != nil {
return rex.Status(500, "Failed to fetch data from the legacy esm.sh server")
}
if endsWith(pathname, ".d.ts", ".d.mts") {
origin := getOrigin(ctx)
if origin != "https://esm.sh" {
data = bytes.ReplaceAll(data, []byte("https://esm.sh"), []byte(origin))
}
data = bytes.ReplaceAll(data, []byte(config.LegacyServer), []byte(origin))
}
err = buildStorage.Put(savePath, bytes.NewReader(data))
if err != nil {
return rex.Status(500, "Storage error: "+err.Error())
}
ctx.SetHeader("Content-Type", res.Header.Get("Content-Type"))
ctx.SetHeader("Control-Cache", ccImmutable)
// resolve hostname in typescript definition files if the origin is not "https://esm.sh"
if endsWith(pathname, ".d.ts", ".d.mts") {
origin := getOrigin(ctx)
if origin != "https://esm.sh" {
data = bytes.ReplaceAll(data, []byte("https://esm.sh/v"), []byte(origin+"/v"))
data = bytes.ReplaceAll(data, []byte(config.LegacyServer+"/v"), []byte(origin+"/v"))
}
}
return data
} else {
code, err := io.ReadAll(res.Body)
Expand Down
2 changes: 1 addition & 1 deletion test/gh/fluentui-emoji.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assertEquals, assertStringIncludes } from "jsr:@std/assert";

Deno.test("github assets", async () => {
const res = await fetch(
"http://localhost:8080/gh/microsoft/fluentui-emoji/assets/Alien/Flat/alien_flat.svg",
"http://localhost:8080/gh/microsoft/fluentui-emoji@62ecdc0/assets/Alien/Flat/alien_flat.svg",
);
assertEquals(res.status, 200);
assertEquals(res.headers.get("content-type"), "image/svg+xml; charset=utf-8");
Expand Down
2 changes: 1 addition & 1 deletion test/gh/jsr.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals } from "jsr:@std/assert";

import $ from "http://localhost:8080/gh/dsherret/dax";
import $ from "http://localhost:8080/gh/dsherret/dax@6aed9b0";

Deno.test("jsr package from github", async () => {
assertEquals(typeof $, "function");
Expand Down
2 changes: 1 addition & 1 deletion test/gh/tslib.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertExists } from "jsr:@std/assert";

import * as tslib from "http://localhost:8080/gh/microsoft/tslib";
import * as tslib from "http://localhost:8080/gh/microsoft/tslib@v2.8.1";

Deno.test("tslib from github", async () => {
assertExists(tslib.__await);
Expand Down