Skip to content

Commit 65313cb

Browse files
authored
new: watm-go quick start (#5)
Signed-off-by: Gaukas Wang <[email protected]>
1 parent 1c052b8 commit 65313cb

File tree

4 files changed

+132
-10
lines changed

4 files changed

+132
-10
lines changed

transport-module/go.md

-9
This file was deleted.

transport-module/go/go.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
layout: default
3+
title: WATM in Go
4+
parent: WebAssembly Transport Module
5+
nav_order: 1
6+
has_children: true
7+
permalink: /transport-module/go.html
8+
---
9+
10+
# WebAssembly Transport Module (WATM) in Go
11+
12+
Go is one of the most popular programming languages for building network applications, and it is possible to build (most of) Go applications into WebAssembly.
13+
14+
Although the official Go compiler does not fully support WebAssembly System Interface (WASI) Preview 1's export feature, TinyGo, a Go compiler designed to build Go applications in a minimal environment, does provide great support for WASI.

transport-module/go/quick-start.md

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
layout: default
3+
title: Quick Start with WATM in Go
4+
grand_parent: WebAssembly Transport Module
5+
parent: WATM in Go
6+
nav_order: 1
7+
---
8+
9+
# Quick Start
10+
11+
This guide walks you through the process of building a simple WebAssembly transport module in Go with TinyGo, with the helper libraries provided by Project WATER.
12+
13+
## Prerequisites
14+
15+
- [TinyGo](https://tinygo.org/getting-started/) v0.31.0 or later
16+
- TinyGo v0.31.0 is used in this guide
17+
- [Go](https://go.dev/) compatible with your TinyGo version
18+
- Go 1.22 is used in this guide
19+
- Internet connection for Go to download the required packages
20+
21+
## Build a String Reversing WATM
22+
23+
First, create a new directory for your WATM project and navigate to it. Initialize your Go module with `go mod init` and required parameters.
24+
25+
```go
26+
package main
27+
28+
import (
29+
"io"
30+
31+
v0 "github.com/refraction-networking/watm/tinygo/v0"
32+
v0net "github.com/refraction-networking/watm/tinygo/v0/net"
33+
)
34+
35+
var dupBuf []byte = make([]byte, 4096) // 4KB buffer for reversing
36+
37+
// type guard: ReverseWrappingTransport must implement [v0.WrappingTransport].
38+
var _ v0.WrappingTransport = (*ReverseWrappingTransport)(nil)
39+
40+
// Inside the init function, we register the ReverseWrappingTransport with
41+
// the helper libraries to enable all three types of transport modules:
42+
// - Dialer
43+
// - Listener
44+
// - Relay
45+
func init() {
46+
v0.BuildDialerWithWrappingTransport(&ReverseWrappingTransport{})
47+
v0.BuildListenerWithWrappingTransport(&ReverseWrappingTransport{})
48+
v0.BuildRelayWithWrappingTransport(&ReverseWrappingTransport{}, v0.RelayWrapRemote)
49+
}
50+
51+
// main function is required for TinyGo to build the WATM.
52+
// It can be empty or contain any code you want to execute
53+
// when the WATM is loaded -- yes, this function will be
54+
// executed right after the WATM is instantiated.
55+
func main() {}
56+
57+
type ReverseWrappingTransport struct {
58+
}
59+
60+
func (rwt *ReverseWrappingTransport) Wrap(conn v0net.Conn) (v0net.Conn, error) {
61+
return &ReverseConn{conn}, conn.SetNonBlock(true) // must set non-block, otherwise will block on read and lose fairness
62+
}
63+
64+
type ReverseConn struct {
65+
v0net.Conn // embedded Conn
66+
}
67+
68+
func (rc *ReverseConn) Read(b []byte) (n int, err error) {
69+
n, err = rc.Conn.Read(dupBuf)
70+
if err != nil {
71+
return 0, err
72+
}
73+
74+
if n > len(b) {
75+
err = io.ErrShortBuffer
76+
n = len(b)
77+
}
78+
79+
// reverse all bytes read successfully so far
80+
for i := 0; i < n; i++ {
81+
b[i] = dupBuf[n-i-1]
82+
}
83+
84+
return n, err
85+
}
86+
87+
func (rc *ReverseConn) Write(b []byte) (n int, err error) {
88+
// reverse the bytes to be written
89+
for i := 0; i < len(b); i++ {
90+
dupBuf[i] = b[len(b)-i-1]
91+
}
92+
93+
return rc.Conn.Write(dupBuf)
94+
}
95+
```
96+
97+
Save as `main.go` in your project directory.
98+
99+
## Build
100+
101+
```bash
102+
go mod tidy # to download the required packages
103+
tinygo build -o reverse.wasm -target=wasi main.go
104+
```
105+
106+
### Release Build with Optimization
107+
108+
With more precise control over the build process,
109+
you can use the following command to fine-tune
110+
the behavior in a release-level standard.
111+
112+
```bash
113+
tinygo build -o reverse.wasm -target=wasi -no-debug -scheduler=none -gc=conservative main.go
114+
```
115+
116+
Now you should end up with a `reverse.wasm` file in your project directory. It can be load into
117+
a [WATER runtime](/runtime.html) and used as a transport module.

transport-module/rust.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: default
3-
title: WebAssembly Transport Module in Rust
3+
title: WATM in Rust
44
parent: WebAssembly Transport Module
55
nav_order: 2
66
---

0 commit comments

Comments
 (0)