Skip to content

Commit

Permalink
Use denonext for Deno >= 1.33.2 (close #646)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Jun 6, 2023
1 parent 58553ca commit add0938
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 31 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ app.get("/", (req, res) => {
app.listen(3000);
```
For users using deno `< 1.31`, esm.sh uses
[deno.land/[email protected]/node](https://deno.land/[email protected].0/node) as node
For users using deno `< 1.33.2`, esm.sh uses
[deno.land/[email protected]/node](https://deno.land/[email protected].1/node) as node
compatibility layer. You can specify a different version by adding the
`?deno-std=$VER` query:
Expand Down
34 changes: 20 additions & 14 deletions packages/esm-worker/src/compat.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { compare } from "compare-versions";
import uaParser from "ua-parser-js";

export const targets = new Set([
"es2015",
"es2016",
"es2017",
"es2018",
"es2019",
"es2020",
"es2021",
"es2022",
"esnext",
"deno",
"denonext",
"node",
])
"es2015",
"es2016",
"es2017",
"es2018",
"es2019",
"es2020",
"es2021",
"es2022",
"esnext",
"deno",
"denonext",
"node",
]);

/** the js table transpiled from https://github.com/evanw/esbuild/blob/main/internal/compat/js_table.go */
const jsTable: Record<string, Record<string, [number, number, number]>> = {
Expand Down Expand Up @@ -610,13 +611,18 @@ const esmaUnsupportedFeatures: [string, number][] = [
getUnsupportedFeatures(esma.slice(0, 2).toUpperCase(), esma.slice(2)).length,
]);

const deno1_33_2 = "1.33.2";

/** get esma version from the `User-Agent` header by checking the `jsTable` object. */
export const getEsmaVersionFromUA = (userAgent: string | null) => {
if (!userAgent || userAgent.startsWith("curl/")) {
return "esnext";
}
if (userAgent.startsWith("Deno/")) {
return "deno";
if (compare(userAgent.slice(5), deno1_33_2, "<")) {
return "deno";
}
return "denonext";
}
if (userAgent.startsWith("Node/") || userAgent.startsWith("Bun/")) {
return "node";
Expand Down
2 changes: 1 addition & 1 deletion server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ rebuild:
return
}

if task.Bundle && task.Target != "deno" && task.Target != "denonext" {
if task.Bundle && !task.isDenoTarget() {
options.Plugins = []api.Plugin{{
Name: "esm",
Setup: func(build api.PluginBuild) {
Expand Down
14 changes: 7 additions & 7 deletions server/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"strings"

"github.com/Masterminds/semver/v3"
"github.com/evanw/esbuild/pkg/api"
"github.com/ije/esbuild-internal/compat"
"github.com/mssola/useragent"
Expand Down Expand Up @@ -179,19 +180,18 @@ func getEngineInfo(ua string) (name string, version string) {
return useragent.New(ua).Browser()
}

// var deno131 = semver.MustParse("1.31.0")
var deno1_33_2 = semver.MustParse("1.33.2")

func getTargetByUA(ua string) string {
if ua == "" || strings.HasPrefix(ua, "curl/") {
return "esnext"
}
if strings.HasPrefix(ua, "Deno/") {
// uaVersion, err := semver.NewVersion(strings.TrimPrefix(ua, "Deno/"))
// if err == nil && uaVersion.LessThan(deno131) {
// return "deno"
// }
// return "denonext"
return "deno"
uaVersion, err := semver.NewVersion(strings.TrimPrefix(ua, "Deno/"))
if err == nil && uaVersion.LessThan(deno1_33_2) {
return "deno"
}
return "denonext"
}
if strings.HasPrefix(ua, "Node/") || strings.HasPrefix(ua, "Bun/") {
return "node"
Expand Down
4 changes: 2 additions & 2 deletions server/server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func esmHandler() rex.Handle {
"uptime": time.Since(startTime).String(),
}

case "/build-target":
case "/esma-target":
return getTargetByUA(ctx.R.UserAgent())

case "/error.js":
Expand Down Expand Up @@ -409,7 +409,7 @@ func esmHandler() rex.Handle {
if targetFromUA {
target = getTargetByUA(ctx.R.UserAgent())
}
if target == "deno" {
if target == "deno" || target == "denonext" {
ctx.SetHeader("Content-Type", "application/typescript; charset=utf-8")
} else {
ret := api.Transform(string(data), api.TransformOptions{
Expand Down
12 changes: 12 additions & 0 deletions test/deno-target/deno-target.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

Deno.test("deno target", async () => {
const getTarget = async (ua: string) => {
const rest = await fetch(`http://localhost:8080/esma-target`, {
headers: { "User-Agent": ua },
});
return await rest.text();
};
assertEquals(await getTarget("Deno/1.33.1"), "deno");
assertEquals(await getTarget("Deno/1.33.2"), "denonext");
});
21 changes: 16 additions & 5 deletions test/esm-worker/esm-worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Deno.test("esm-worker", {

const modUrl = code.match(/from "(.+)"/)?.[1]!;
assert(modUrl.startsWith(workerOrigin));
assert(modUrl.endsWith("/deno/react-dom.mjs"));
assert(modUrl.endsWith("/denonext/react-dom.mjs"));
const res3 = await fetch(modUrl);
assertEquals(res3.status, 200);
assertEquals(
Expand Down Expand Up @@ -176,7 +176,7 @@ Deno.test("esm-worker", {
assertEquals(res2.headers.get("Cache-Control"), "public, max-age=86400");
assert(/v\d+\/.+\.d\.ts$/.test(res2.headers.get("X-Typescript-Types")!));
const modUrl = code.match(/from "(.+)"/)?.[1]!;
assert(modUrl.endsWith("/deno/server.js"));
assert(modUrl.endsWith("/denonext/server.js"));
const res3 = await fetch(modUrl);
assertEquals(res3.status, 200);
assertEquals(
Expand Down Expand Up @@ -210,7 +210,7 @@ Deno.test("esm-worker", {
const modUrl = new URL(code.match(/from "(.+)"/)?.[1]!);
assertEquals(modUrl.origin, workerOrigin);
assert(modUrl.pathname.startsWith("/stable/react@"));
assert(modUrl.pathname.endsWith("/deno/react.mjs"));
assert(modUrl.pathname.endsWith("/denonext/react.mjs"));
const res3 = await fetch(modUrl);
assertEquals(res3.status, 200);
assertEquals(
Expand Down Expand Up @@ -308,7 +308,7 @@ Deno.test("esm-worker", {
assert(
/v\d+\/gh\/.+\.d\.ts$/.test(res2.headers.get("X-Typescript-Types")!),
);
assert(modUrl.pathname.endsWith("/deno/tslib.mjs"));
assert(modUrl.pathname.endsWith("/denonext/tslib.mjs"));
const res3 = await fetch(modUrl);
assertEquals(res3.status, 200);
assertEquals(
Expand Down Expand Up @@ -372,14 +372,25 @@ Deno.test("esm-worker", {
}
const { default: render } = await import(
new URL(
`/v${VERSION}${new URL(ret.url).pathname}/deno/mod.mjs`,
`/v${VERSION}${new URL(ret.url).pathname}/denonext/mod.mjs`,
workerOrigin,
)
.href
);
assertEquals(render(), "<h1>Hello world!</h1>");
});

await t.step("/esma-target", async () => {
const getTarget = async (ua: string) => {
const rest = await fetch(`${workerOrigin}/esma-target`, {
headers: { "User-Agent": ua },
});
return await rest.text();
};
assertEquals(await getTarget("Deno/1.33.1"), "deno");
assertEquals(await getTarget("Deno/1.33.2"), "denonext");
});

ac.abort();
await new Promise((resolve) => setTimeout(resolve, 500));
});

0 comments on commit add0938

Please sign in to comment.