Skip to content

Commit 36b5e51

Browse files
committed
wasmserve: bug fix: invalid URL for some Go versions
1 parent 4d12bd5 commit 36b5e51

File tree

6 files changed

+186
-51
lines changed

6 files changed

+186
-51
lines changed

.github/workflows/test.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Test
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
strategy:
8+
matrix:
9+
os: [ubuntu-latest, macos-latest, windows-latest]
10+
go: ['1.16.x', '1.23.x', '1.24.x']
11+
name: Test with Go ${{ matrix.go }} on ${{ matrix.os }}
12+
runs-on: ${{ matrix.os }}
13+
defaults:
14+
run:
15+
shell: bash
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
20+
- name: Setup Go
21+
uses: actions/setup-go@v5
22+
with:
23+
go-version: ${{ matrix.go }}
24+
25+
- name: Test
26+
run: |
27+
go test -v ./...

example/main.go

-36
This file was deleted.

example/main_js.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2018 Hajime Hoshi
3+
4+
package main
5+
6+
import (
7+
"flag"
8+
"fmt"
9+
"os"
10+
"syscall/js"
11+
)
12+
13+
func main() {
14+
flag.Parse()
15+
fmt.Println(flag.Args())
16+
17+
p := js.Global().Get("document").Call("createElement", "p")
18+
p.Set("innerText", "Hello, World!")
19+
js.Global().Get("document").Get("body").Call("appendChild", p)
20+
21+
pre := js.Global().Get("document").Call("createElement", "pre")
22+
pre.Set("innerText", fmt.Sprintf("args: %q\nevn: %q", os.Args, os.Environ()))
23+
pre.Get("style").Set("whiteSpace", "pre-wrap")
24+
js.Global().Get("document").Get("body").Call("appendChild", pre)
25+
}

internal/wasmserveutil/util.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2025 Hajime Hoshi
3+
4+
package wasmserveutil
5+
6+
import (
7+
"fmt"
8+
"regexp"
9+
"strconv"
10+
)
11+
12+
var reGoVersion = regexp.MustCompile(`^go(\d+)\.(\d+)(\.(\d+))?`)
13+
14+
func WasmExecJSURL(goVersion string) (string, error) {
15+
// go.mod might have a version without `.0` like `go1.22`. This version might not work as a part of URL.
16+
m := reGoVersion.FindStringSubmatch(goVersion)
17+
if len(m) == 0 {
18+
return "", fmt.Errorf("wasmserve: invalid Go version: %s", goVersion)
19+
}
20+
minor, _ := strconv.Atoi(m[2])
21+
if minor >= 22 && m[3] == "" {
22+
goVersion += ".0"
23+
}
24+
dir := "lib"
25+
if minor <= 23 {
26+
dir = "misc"
27+
}
28+
return fmt.Sprintf("https://go.googlesource.com/go/+/refs/tags/%s/%s/wasm/wasm_exec.js?format=TEXT", goVersion, dir), nil
29+
}

internal/wasmserveutil/util_test.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2025 Hajime Hoshi
3+
4+
package wasmserveutil_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hajimehoshi/wasmserve/internal/wasmserveutil"
10+
)
11+
12+
func TestWasmExecJSURL(t *testing.T) {
13+
testCases := []struct {
14+
goVersion string
15+
url string
16+
error bool
17+
}{
18+
{
19+
goVersion: "invalid",
20+
url: "",
21+
error: true,
22+
},
23+
{
24+
goVersion: "go1.16",
25+
url: "https://go.googlesource.com/go/+/refs/tags/go1.16/misc/wasm/wasm_exec.js?format=TEXT",
26+
error: false,
27+
},
28+
{
29+
goVersion: "go1.21",
30+
url: "https://go.googlesource.com/go/+/refs/tags/go1.21/misc/wasm/wasm_exec.js?format=TEXT",
31+
error: false,
32+
},
33+
{
34+
goVersion: "go1.22",
35+
url: "https://go.googlesource.com/go/+/refs/tags/go1.22.0/misc/wasm/wasm_exec.js?format=TEXT",
36+
error: false,
37+
},
38+
{
39+
goVersion: "go1.22.0",
40+
url: "https://go.googlesource.com/go/+/refs/tags/go1.22.0/misc/wasm/wasm_exec.js?format=TEXT",
41+
error: false,
42+
},
43+
{
44+
goVersion: "go1.22.1",
45+
url: "https://go.googlesource.com/go/+/refs/tags/go1.22.1/misc/wasm/wasm_exec.js?format=TEXT",
46+
error: false,
47+
},
48+
{
49+
goVersion: "go1.23",
50+
url: "https://go.googlesource.com/go/+/refs/tags/go1.23.0/misc/wasm/wasm_exec.js?format=TEXT",
51+
error: false,
52+
},
53+
{
54+
goVersion: "go1.23.0",
55+
url: "https://go.googlesource.com/go/+/refs/tags/go1.23.0/misc/wasm/wasm_exec.js?format=TEXT",
56+
error: false,
57+
},
58+
{
59+
goVersion: "go1.23.1",
60+
url: "https://go.googlesource.com/go/+/refs/tags/go1.23.1/misc/wasm/wasm_exec.js?format=TEXT",
61+
error: false,
62+
},
63+
{
64+
goVersion: "go1.24",
65+
url: "https://go.googlesource.com/go/+/refs/tags/go1.24.0/lib/wasm/wasm_exec.js?format=TEXT",
66+
error: false,
67+
},
68+
{
69+
goVersion: "go1.24.0",
70+
url: "https://go.googlesource.com/go/+/refs/tags/go1.24.0/lib/wasm/wasm_exec.js?format=TEXT",
71+
error: false,
72+
},
73+
{
74+
goVersion: "go1.24.1",
75+
url: "https://go.googlesource.com/go/+/refs/tags/go1.24.1/lib/wasm/wasm_exec.js?format=TEXT",
76+
error: false,
77+
},
78+
}
79+
80+
for _, tc := range testCases {
81+
t.Run(tc.goVersion, func(t *testing.T) {
82+
url, err := wasmserveutil.WasmExecJSURL(tc.goVersion)
83+
if err != nil && !tc.error {
84+
t.Errorf("unexpected error: %v", err)
85+
return
86+
}
87+
if err == nil && tc.error {
88+
t.Errorf("no error")
89+
return
90+
}
91+
if url != tc.url {
92+
t.Errorf("got: %s, want: %s", url, tc.url)
93+
}
94+
})
95+
}
96+
}

main.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
// Copyright 2018 Hajime Hoshi
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2018 Hajime Hoshi
143

154
package main
165

@@ -33,6 +22,8 @@ import (
3322
"strings"
3423
"text/template"
3524
"time"
25+
26+
"github.com/hajimehoshi/wasmserve/internal/wasmserveutil"
3627
)
3728

3829
const mainWasm = "main.wasm"
@@ -335,9 +326,12 @@ func fetchWasmExecJS() ([]byte, error) {
335326
return nil, err
336327
}
337328

338-
// TODO: Cache the result.
329+
url, err := wasmserveutil.WasmExecJSURL(v)
330+
if err != nil {
331+
return nil, err
332+
}
339333

340-
url := fmt.Sprintf("https://go.googlesource.com/go/+/refs/tags/%s/misc/wasm/wasm_exec.js?format=TEXT", v)
334+
// TODO: Cache the result.
341335
resp, err := http.Get(url)
342336
if err != nil {
343337
return nil, err

0 commit comments

Comments
 (0)