Skip to content

Commit 5b2b715

Browse files
committed
fix(site): fix terminal rendering
1 parent b16f550 commit 5b2b715

File tree

14 files changed

+245
-140
lines changed

14 files changed

+245
-140
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ main.wasm
55
test.txt
66
.next/
77
.env
8-
site/public/*.js
9-
site/public/*.wasm
8+
site/wasm/*.js
9+
site/wasm/*.wasm
1010
site/node_modules/
1111
.env.local
12+
13+
.vercel

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@ require (
2323
github.com/mattn/go-isatty v0.0.20
2424
github.com/mitchellh/go-wordwrap v1.0.1
2525
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a
26-
github.com/pion/datachannel v1.5.9
27-
github.com/pion/logging v0.2.2
2826
github.com/pion/stun/v3 v3.0.0
2927
github.com/pion/webrtc/v4 v4.0.1
3028
github.com/prometheus/client_golang v1.20.4
3129
github.com/puzpuzpuz/xsync/v3 v3.4.0
3230
github.com/schollz/progressbar/v3 v3.16.1
3331
github.com/spf13/afero v1.11.0
3432
github.com/valyala/fasthttp v1.56.0
35-
go.uber.org/atomic v1.11.0
3633
go4.org/mem v0.0.0-20220726221520-4f986261bf13
3734
golang.org/x/crypto v0.28.0
3835
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948
@@ -161,9 +158,11 @@ require (
161158
github.com/outcaste-io/ristretto v0.2.3 // indirect
162159
github.com/philhofer/fwd v1.1.2 // indirect
163160
github.com/pierrec/lz4/v4 v4.1.21 // indirect
161+
github.com/pion/datachannel v1.5.9 // indirect
164162
github.com/pion/dtls/v3 v3.0.3 // indirect
165163
github.com/pion/ice/v4 v4.0.2 // indirect
166164
github.com/pion/interceptor v0.1.37 // indirect
165+
github.com/pion/logging v0.2.2 // indirect
167166
github.com/pion/mdns/v2 v2.0.7 // indirect
168167
github.com/pion/randutil v0.1.0 // indirect
169168
github.com/pion/rtcp v1.2.14 // indirect
@@ -218,6 +217,7 @@ require (
218217
go.opentelemetry.io/otel/sdk v1.30.0 // indirect
219218
go.opentelemetry.io/otel/trace v1.32.0 // indirect
220219
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
220+
go.uber.org/atomic v1.11.0 // indirect
221221
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
222222
golang.org/x/mod v0.21.0 // indirect
223223
golang.org/x/oauth2 v0.23.0 // indirect

overlay/receive.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io"
1212
"log/slog"
1313
"net"
14+
"net/http"
1415
"net/netip"
1516
"os"
1617
"sync"
@@ -83,12 +84,46 @@ func (r *Receive) IPs() []netip.Addr {
8384
}
8485
}
8586

86-
var webrtcConfig = webrtc.Configuration{
87-
ICEServers: []webrtc.ICEServer{
88-
{
89-
URLs: []string{"stun:stun.l.google.com:19302"},
87+
func getWebRTCConfig() webrtc.Configuration {
88+
defaultConfig := webrtc.Configuration{
89+
ICEServers: []webrtc.ICEServer{
90+
{
91+
URLs: []string{"stun:stun.l.google.com:19302"},
92+
},
9093
},
91-
},
94+
}
95+
96+
resp, err := http.Get("https://wush.dev/api/iceConfig")
97+
if err != nil {
98+
fmt.Println("failed to get ice config:", err)
99+
return defaultConfig
100+
}
101+
defer resp.Body.Close()
102+
103+
var iceConfig struct {
104+
IceServers []struct {
105+
URLs []string `json:"urls"`
106+
Username string `json:"username"`
107+
Credential string `json:"credential"`
108+
} `json:"iceServers"`
109+
}
110+
111+
if err := json.NewDecoder(resp.Body).Decode(&iceConfig); err != nil {
112+
return defaultConfig
113+
}
114+
115+
config := webrtc.Configuration{
116+
ICEServers: make([]webrtc.ICEServer, len(iceConfig.IceServers)),
117+
}
118+
for i, server := range iceConfig.IceServers {
119+
config.ICEServers[i] = webrtc.ICEServer{
120+
URLs: server.URLs,
121+
Username: server.Username,
122+
Credential: server.Credential,
123+
}
124+
}
125+
126+
return config
92127
}
93128

94129
func (r *Receive) PickDERPHome(ctx context.Context) error {
@@ -424,7 +459,7 @@ func (r *Receive) setupWebrtcConnection(src key.NodePublic, res *overlayMessage,
424459
api := webrtc.NewAPI(webrtc.WithSettingEngine(settingEngine))
425460

426461
// Use the custom API to create the peer connection
427-
peerConnection, err := api.NewPeerConnection(webrtcConfig)
462+
peerConnection, err := api.NewPeerConnection(getWebRTCConfig())
428463
if err != nil {
429464
panic(err)
430465
}

overlay/send.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func (s *Send) handleNextMessage(msg []byte) (resRaw []byte, _ error) {
312312

313313
func (s *Send) setupWebrtcConnection() {
314314
var err error
315-
s.RtcConn, err = webrtc.NewPeerConnection(webrtcConfig)
315+
s.RtcConn, err = webrtc.NewPeerConnection(getWebRTCConfig())
316316
if err != nil {
317317
panic("failed to create webrtc connection: " + err.Error())
318318
}

site/build_wasm.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ set -eux
44

55
cd "$(dirname "$0")"
66

7+
mkdir -p wasm
8+
79
echo "WARNING: make sure you're using 'nix develop' for the correct go version"
810

9-
GOOS=js GOARCH=wasm go build -ldflags="-s -w" -o ./public/main.wasm ../cmd/wasm
10-
# wasm-opt -Oz ./public/main.wasm -o ./public/main.wasm --enable-bulk-memory
11+
GOOS=js GOARCH=wasm go build -ldflags="-s -w" -o ./wasm/main.wasm ../cmd/wasm
12+
wasm-opt -Oz ./wasm/main.wasm -o ./wasm/main.wasm --enable-bulk-memory
1113

12-
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" ./public/wasm_exec.js
14+
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" ./wasm/wasm_exec.js && chmod 644 ./wasm/wasm_exec.js

site/bun.lockb

224 Bytes
Binary file not shown.

site/context/wush.tsx

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import "../public/wasm_exec.js";
1+
import "../wasm/wasm_exec.js";
2+
import wasmModule from "../wasm/main.wasm";
23

34
import type React from "react";
45
import type { ReactNode } from "react";
@@ -89,6 +90,7 @@ export const WasmProvider: React.FC<WasmProviderProps> = ({ children }) => {
8990

9091
const config: WushConfig = {
9192
onNewPeer: (peer: Peer) => {
93+
console.log(JSON.stringify(iceServers));
9294
const newPeerConnection = new RTCPeerConnection({
9395
iceServers,
9496
});
@@ -218,44 +220,48 @@ export const WasmProvider: React.FC<WasmProviderProps> = ({ children }) => {
218220
async function loadWasm() {
219221
const go = new Go();
220222

221-
const module = await WebAssembly.instantiateStreaming(
222-
fetch("/main.wasm"),
223-
go.importObject
224-
);
223+
try {
224+
const response = await fetch(wasmModule as unknown as string);
225+
const buffer = await response.arrayBuffer();
226+
const module = await WebAssembly.instantiate(buffer, go.importObject);
225227

226-
go.run(module.instance).then(() => {
227-
setWasm((prevState) => {
228-
return {
228+
go.run(module.instance).then(() => {
229+
setWasm((prevState) => ({
229230
...prevState,
230231
loading: false,
231232
peers: [],
232233
error: "WASM exited",
233-
};
234+
}));
234235
});
235-
});
236-
237-
newWush(config)
238-
.then((wush) => {
239-
setWasm((prevState) => {
240-
prevState.wush.current = wush;
241236

242-
return {
243-
...prevState,
244-
loading: false,
245-
};
237+
newWush(config)
238+
.then((wush) => {
239+
setWasm((prevState) => {
240+
prevState.wush.current = wush;
241+
return {
242+
...prevState,
243+
loading: false,
244+
};
245+
});
246+
})
247+
.catch((ex) => {
248+
setWasm((prevState) => {
249+
prevState.wush.current = null;
250+
return {
251+
...prevState,
252+
loading: false,
253+
peers: [],
254+
error: `Wush failed to initialize: ${ex}`,
255+
};
256+
});
246257
});
247-
})
248-
.catch((ex) => {
249-
setWasm((prevState) => {
250-
prevState.wush.current = null;
251-
return {
252-
...prevState,
253-
loading: false,
254-
peers: [],
255-
error: `Wush failed to initialize: ${ex}`,
256-
};
257-
});
258-
});
258+
} catch (error) {
259+
setWasm((prevState) => ({
260+
...prevState,
261+
loading: false,
262+
error: `Failed to load WASM: ${error}`,
263+
}));
264+
}
259265
}
260266

261267
loadWasm();

site/next.config.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ const createConfig = async (): Promise<NextConfig> => {
1010
env: {
1111
GITHUB_STARS: githubStars,
1212
},
13+
webpack: (config, { dev }) => {
14+
// Add WASM support
15+
config.experiments = {
16+
...config.experiments,
17+
asyncWebAssembly: true,
18+
};
19+
20+
// Add rule for wasm files with content hashing
21+
config.module.rules.push({
22+
test: /\.wasm$/,
23+
type: "asset/resource",
24+
generator: {
25+
filename: dev
26+
? "static/wasm/[name].wasm"
27+
: "static/wasm/[name].[hash][ext]",
28+
},
29+
});
30+
31+
return config;
32+
},
1333
};
1434
};
1535

site/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"lucide-react": "^0.454.0",
2222
"next": "15.0.2",
2323
"next-themes": "^0.4.3",
24-
"react": "19.0.0-beta-26f2496093-20240514",
25-
"react-dom": "19.0.0-beta-26f2496093-20240514",
24+
"react": "19.0.0-rc.1",
25+
"react-dom": "19.0.0-rc.1",
2626
"sonner": "^1.7.0",
2727
"tailwind-merge": "^2.5.4",
2828
"tailwind-scrollbar": "^3.1.0",
@@ -39,5 +39,8 @@
3939
"postcss": "^8.4.47",
4040
"tailwindcss": "^3.4.14",
4141
"typescript": "^5.0.0"
42+
},
43+
"engines": {
44+
"node": "22.x"
4245
}
4346
}

site/pages/_document.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MyDocument extends Document {
1313
rel="stylesheet"
1414
href="https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap"
1515
/>
16-
<link rel="manifest" href="/app.webmanifest" />
16+
<link rel="manifest" href="/site.webmanifest" />
1717
</Head>
1818
<body className="bg-bg text-fg">
1919
<Main />

0 commit comments

Comments
 (0)