Skip to content

Commit 8b0cd0d

Browse files
authored
Fix legacy dts hostname (#1129)
1 parent 3696823 commit 8b0cd0d

4 files changed

Lines changed: 25 additions & 10 deletions

File tree

server/legacy_router.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,20 @@ func legacyESM(ctx *rex.Context, buildStorage storage.Storage, buildVersionPrefi
190190
ctx.SetHeader("Content-Type", ctJavaScript)
191191
case ".ts", ".mts":
192192
ctx.SetHeader("Content-Type", ctTypeScript)
193+
// resolve hostname in typescript definition files if the origin is not "https://esm.sh"
194+
if endsWith(pathname, ".d.ts", ".d.mts") {
195+
origin := getOrigin(ctx)
196+
if origin != "https://esm.sh" {
197+
defer f.Close()
198+
data, err := io.ReadAll(f)
199+
if err != nil {
200+
return rex.Status(500, "Failed to read data from storage")
201+
}
202+
data = bytes.ReplaceAll(data, []byte("https://esm.sh/v"), []byte(origin+"/v"))
203+
data = bytes.ReplaceAll(data, []byte(config.LegacyServer+"/v"), []byte(origin+"/v"))
204+
return data
205+
}
206+
}
193207
case ".map":
194208
ctx.SetHeader("Content-Type", ctJSON)
195209
case ".css":
@@ -273,19 +287,20 @@ func legacyESM(ctx *rex.Context, buildStorage storage.Storage, buildVersionPrefi
273287
if err != nil {
274288
return rex.Status(500, "Failed to fetch data from the legacy esm.sh server")
275289
}
276-
if endsWith(pathname, ".d.ts", ".d.mts") {
277-
origin := getOrigin(ctx)
278-
if origin != "https://esm.sh" {
279-
data = bytes.ReplaceAll(data, []byte("https://esm.sh"), []byte(origin))
280-
}
281-
data = bytes.ReplaceAll(data, []byte(config.LegacyServer), []byte(origin))
282-
}
283290
err = buildStorage.Put(savePath, bytes.NewReader(data))
284291
if err != nil {
285292
return rex.Status(500, "Storage error: "+err.Error())
286293
}
287294
ctx.SetHeader("Content-Type", res.Header.Get("Content-Type"))
288295
ctx.SetHeader("Control-Cache", ccImmutable)
296+
// resolve hostname in typescript definition files if the origin is not "https://esm.sh"
297+
if endsWith(pathname, ".d.ts", ".d.mts") {
298+
origin := getOrigin(ctx)
299+
if origin != "https://esm.sh" {
300+
data = bytes.ReplaceAll(data, []byte("https://esm.sh/v"), []byte(origin+"/v"))
301+
data = bytes.ReplaceAll(data, []byte(config.LegacyServer+"/v"), []byte(origin+"/v"))
302+
}
303+
}
289304
return data
290305
} else {
291306
code, err := io.ReadAll(res.Body)

test/gh/fluentui-emoji.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assertEquals, assertStringIncludes } from "jsr:@std/assert";
22

33
Deno.test("github assets", async () => {
44
const res = await fetch(
5-
"http://localhost:8080/gh/microsoft/fluentui-emoji/assets/Alien/Flat/alien_flat.svg",
5+
"http://localhost:8080/gh/microsoft/fluentui-emoji@62ecdc0/assets/Alien/Flat/alien_flat.svg",
66
);
77
assertEquals(res.status, 200);
88
assertEquals(res.headers.get("content-type"), "image/svg+xml; charset=utf-8");

test/gh/jsr.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assertEquals } from "jsr:@std/assert";
22

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

55
Deno.test("jsr package from github", async () => {
66
assertEquals(typeof $, "function");

test/gh/tslib.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assertExists } from "jsr:@std/assert";
22

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

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

0 commit comments

Comments
 (0)