Skip to content

Commit

Permalink
Fix findFiles function (close #638)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed May 20, 2023
1 parent 63fd272 commit 84b42fc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
25 changes: 13 additions & 12 deletions server/server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,9 @@ func esmHandler() rex.Handle {
))
}

// redirect to real wasm file: `/v100/PKG/es2022/foo.wasm` -> `/PKG/foo.wasm`
// fix url related `import.meta.url`
if hasBuildVerPrefix && endsWith(reqPkg.Subpath, ".wasm", ".json") {
extname := path.Ext(reqPkg.Subpath)
dir := path.Join(cfg.WorkDir, "npm", reqPkg.Name+"@"+reqPkg.Version)
if !dirExists(dir) {
err := installPackage(dir, reqPkg)
Expand All @@ -517,28 +518,28 @@ func esmHandler() rex.Handle {
}
}
pkgRoot := path.Join(dir, "node_modules", reqPkg.Name)
wasmFiles, err := findFiles(pkgRoot, func(fp string) bool {
return endsWith(fp, ".wasm", ".json")
files, err := findFiles(pkgRoot, "", func(fp string) bool {
return strings.HasSuffix(fp, extname)
})
if err != nil {
return rex.Status(500, err.Error())
}
var wasmFile string
if l := len(wasmFiles); l == 1 {
wasmFile = wasmFiles[0]
var file string
if l := len(files); l == 1 {
file = files[0]
} else if l > 1 {
sort.Sort(sort.Reverse(PathSlice(wasmFiles)))
for _, f := range wasmFiles {
sort.Sort(sort.Reverse(PathSlice(files)))
for _, f := range files {
if strings.Contains(reqPkg.Subpath, f) {
wasmFile = f
file = f
break
}
}
}
if wasmFile == "" {
return rex.Status(404, "Wasm File not found")
if file == "" {
return rex.Status(404, "File not found")
}
url := fmt.Sprintf("%s%s/%s@%s/%s", cdnOrigin, cfg.BasePath, reqPkg.Name, reqPkg.Version, wasmFile)
url := fmt.Sprintf("%s%s/%s@%s/%s", cdnOrigin, cfg.BasePath, reqPkg.Name, reqPkg.Version, file)
return rex.Redirect(url, http.StatusMovedPermanently)
}

Expand Down
14 changes: 9 additions & 5 deletions server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func ensureDir(dir string) (err error) {
return
}

func findFiles(root string, fn func(p string) bool) ([]string, error) {
func findFiles(root string, dir string, fn func(p string) bool) ([]string, error) {
rootDir, err := filepath.Abs(root)
if err != nil {
return nil, err
Expand All @@ -129,23 +129,27 @@ func findFiles(root string, fn func(p string) bool) ([]string, error) {
var files []string
for _, entry := range entries {
name := entry.Name()
path := name
if dir != "" {
path = dir + "/" + name
}
if entry.IsDir() {
if name == "node_modules" {
continue
}
subFiles, err := findFiles(filepath.Join(rootDir, name), fn)
subFiles, err := findFiles(filepath.Join(rootDir, name), path, fn)
if err != nil {
return nil, err
}
n := len(files)
files = make([]string, n+len(subFiles))
for i, f := range subFiles {
files[i+n] = filepath.Join(name, f)
files[i+n] = f
}
copy(files, subFiles)
} else {
if fn(name) {
files = append(files, name)
if fn(path) {
files = append(files, path)
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/issue-638/issue-638.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

const { version } = await fetch("http://localhost:8080/status.json").then(
(r) => r.json(),
);

Deno.test("issue #638", async () => {
const res = await fetch(
`http://localhost:8080/v${version}/@sqlite.org/[email protected]/es2022/sqlite3.wasm`,
{ redirect: "manual" },
);
assertEquals(res.status, 301);
assertEquals(
res.headers.get("location")!,
"http://localhost:8080/@sqlite.org/[email protected]/sqlite-wasm/jswasm/sqlite3.wasm",
);
res.body?.cancel();
});

0 comments on commit 84b42fc

Please sign in to comment.