Skip to content

Commit

Permalink
Express is working in deno
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Mar 25, 2023
1 parent 8f261b7 commit bdaecf3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ import { NinetyRing, NinetyRingWithBg } from "https://esm.sh/react-svg-spinners@
esm.sh is a **Deno-friendly** CDN that resolves Node's built-in modules (such as **fs**, **os**, etc.), making it compatible with Deno.

```javascript
import postcss from "https://esm.sh/postcss"
import autoprefixer from "https://esm.sh/autoprefixer"
import express from "https://esm.sh/express";

const { css } = await postcss([ autoprefixer ]).process(`
backdrop-filter: blur(5px);
user-select: none;
.async()
const app = express();
app.get("/", (req, res) => {
res.send("Hello World");
});
app.listen(3000)
```

For users using deno `< 1.31`, esm.sh uses [deno.land/[email protected]/node](https://deno.land/[email protected]/node) as node compatibility layer. You can specify a different version by adding the `?deno-std=$VER` query:
Expand Down
14 changes: 3 additions & 11 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,25 +885,17 @@ esbuild:
}
}

// most of npm packages check for window object to detect browser environment, but Deno also has the window object
// so we need to replace the check with document object
if task.Target == "deno" || task.Target == "denonext" {
// inject xhr polyfill for axios
switch task.Pkg.Name {
case "axios", "cross-fetch", "whatwg-fetch":
outputContent = bytes.Join([][]byte{
[]byte(`import "https://deno.land/x/[email protected]/mod.ts";`),
outputContent,
}, []byte{})
}
// most of npm packages check for window object to detect browser environment, but Deno also has the window object
// so we need to replace the check with document object
if task.DevMode {
outputContent = bytes.Replace(outputContent, []byte("typeof window !== \"undefined\""), []byte("typeof document !== \"undefined\""), -1)
} else {
outputContent = bytes.Replace(outputContent, []byte("typeof window<\"u\""), []byte("typeof document<\"u\""), -1)
}
}

_, err = buf.Write(outputContent)
_, err = buf.Write(rewriteJS(task, outputContent))
if err != nil {
return
}
Expand Down
26 changes: 26 additions & 0 deletions server/rewrite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package server

import (
"bytes"

"github.com/Masterminds/semver/v3"
)

func rewriteJS(task *BuildTask, js []byte) []byte {
denoTarget := task.Target == "deno" || task.Target == "denonext"
switch task.Pkg.Name {
case "axios", "cross-fetch", "whatwg-fetch":
if denoTarget {
xhr := []byte(`import "https://deno.land/x/[email protected]/mod.ts";`)
buf := make([]byte, len(js)+len(xhr))
copy(buf, xhr)
copy(buf[len(xhr):], js)
js = buf
}
case "iconv-lite":
if denoTarget && semver.MustParse(task.Pkg.Version).LessThan(semver.MustParse("0.5.0")) {
js = bytes.Replace(js, []byte("__Process$.versions.node"), []byte("void 0"), 1)
}
}
return js
}
33 changes: 33 additions & 0 deletions test/express/express.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

import express, {
type Request,
type Response,
} from "http://localhost:8080/express@4";

Deno.test(
"express",
{ sanitizeOps: false, sanitizeResources: false },
async () => {
const app = express();
app.get("/", (_req: Request, res: Response) => {
// @ts-ignore
res.send("Hello World");
});

const ac = new AbortController();
await new Promise<void>((resolve, reject) => {
const server = app.listen({ port: 3333 }, (err?: Error) => {
if (err) reject(err);
resolve();
});
ac.signal.onabort = () => {
server.close();
};
});

const res = await fetch("http://localhost:3333");
assertEquals(await res.text(), "Hello World");
ac.abort();
},
);

0 comments on commit bdaecf3

Please sign in to comment.