Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GOOS=js GOARCH=wasm a possible build target #573

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The following emojis are used to highlight certain changes:

### Added

- `filewriter` now has a wasm target, which though noop makes wasm a possible build target.
- `blockservice` now has `ContextWithSession` and `EmbedSessionInContext` functions, which allows to embed a session in a context. Future calls to `BlockGetter.GetBlock`, `BlockGetter.GetBlocks` and `NewSession` will use the session in the context.
- `blockservice.NewWritethrough` deprecated function has been removed, instead you can do `blockservice.New(..., ..., WriteThrough())` like previously.
- `gateway`: a new header configuration middleware has been added to replace the existing header configuration, which can be used more generically.
Expand Down
3 changes: 2 additions & 1 deletion files/filewriter_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//go:build darwin || linux || netbsd || openbsd || freebsd || dragonfly || js || wasip1
//go:build darwin || linux || netbsd || openbsd || freebsd || dragonfly || (js && !wasm) || wasip1
// +build darwin linux netbsd openbsd freebsd dragonfly js,!wasm wasip1

package files

Expand Down
4 changes: 3 additions & 1 deletion files/filewriter_unix_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//go:build darwin || linux || netbsd || openbsd || freebsd || dragonfly || js || wasip1
//go:build darwin || linux || netbsd || openbsd || freebsd || dragonfly || (js && !wasm) || wasip1
// +build darwin linux netbsd openbsd freebsd dragonfly js,!wasm wasip1
Copy link
Contributor

@Jorropo Jorropo Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, it's been since 1.18 that +build isn't used anymore.
I think you should upgrade your version of go. 😉 🙂



package files

Expand Down
20 changes: 20 additions & 0 deletions files/filewriter_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build js && wasm
// +build js,wasm

package files

import (
"errors"
"os"
"strings"
)

func createNewFile(path string) (*os.File, error) {
return nil, errors.New("createNewFile is not supported in WebAssembly environments")
}

func isValidFilename(filename string) bool {
// Filename validation might still be relevant in a WASM context.
invalidChars := `/` + "\x00"
return !strings.ContainsAny(filename, invalidChars)
}
30 changes: 30 additions & 0 deletions files/filewriter_wasm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build js && wasm
// +build js,wasm

package files

import (
"testing"
"github.com/stretchr/testify/assert"
)

func TestIsValidFilenameWASM(t *testing.T) {
// Since file creation isn't supported in WASM, this test only focuses on filename validation.

testCases := []struct {
name string
valid bool
}{
{"validfilename", true},
{"/invalid/filename", false},
{"", false},
{".", false},
{"..", false},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.valid, isValidFilename(tc.name))
})
}
}