From 34f1f0dc75eb2e87112234fa26423c15cb9f4052 Mon Sep 17 00:00:00 2001 From: Lars Bahner Date: Sun, 28 Jan 2024 10:19:51 +0100 Subject: [PATCH 1/2] js can't support filewriter on wasm --- files/filewriter_unix.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/files/filewriter_unix.go b/files/filewriter_unix.go index cd99aeb9a..c808a773b 100644 --- a/files/filewriter_unix.go +++ b/files/filewriter_unix.go @@ -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 From 68fa6cc904ecee95ffd792d9606710d0d2ded39f Mon Sep 17 00:00:00 2001 From: Lars Bahner Date: Sun, 28 Jan 2024 10:32:26 +0100 Subject: [PATCH 2/2] Add wasm filewriter --- files/filewriter_unix_test.go | 4 +++- files/filewriter_wasm.go | 27 +++++++++++++++++++++++++++ files/filewriter_wasm_test.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 files/filewriter_wasm.go create mode 100644 files/filewriter_wasm_test.go diff --git a/files/filewriter_unix_test.go b/files/filewriter_unix_test.go index 9f63fe0fe..23b47f01c 100644 --- a/files/filewriter_unix_test.go +++ b/files/filewriter_unix_test.go @@ -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 + package files diff --git a/files/filewriter_wasm.go b/files/filewriter_wasm.go new file mode 100644 index 000000000..4c49c7c50 --- /dev/null +++ b/files/filewriter_wasm.go @@ -0,0 +1,27 @@ +//go:build js && wasm +// +build js,wasm + +package files + +import ( + "errors" + "os" + "strings" +) + +// In a WebAssembly context, direct file system operations like in Unix are not possible. +// Therefore, these functions could be adapted to work with in-memory storage or +// interfacing with JavaScript for file operations. + +func createNewFile(path string) (*os.File, error) { + // Since direct file creation isn't possible, you might want to simulate it or + // interface with JavaScript. For now, this will return an 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. + // You can keep the logic same or adapt it based on your application's needs. + invalidChars := `/` + "\x00" + return !strings.ContainsAny(filename, invalidChars) +} diff --git a/files/filewriter_wasm_test.go b/files/filewriter_wasm_test.go new file mode 100644 index 000000000..29730bfcb --- /dev/null +++ b/files/filewriter_wasm_test.go @@ -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)) + }) + } +}