From 0f99f932387985a7f17ea8a340b5044f0d548d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 21 Aug 2025 12:31:55 +0200 Subject: [PATCH] term: allow compiling for js/wasm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a platform-specific implementation to allow the module to compile for GOOS=js and GOARCH=wasm, without providing terminal functionality. Terminal detection reports false, and unsupported operations return errors instead of silently succeeding. This is particularly important for operations such as disabling terminal echo, where silently ignoring the request could expose sensitive input. Actual WebAssembly terminal support can be considered separately. Signed-off-by: Rémy Léone Signed-off-by: Sebastiaan van Stijn --- term_unix.go | 2 +- term_unsupported.go | 56 ++++++++++++++++++++++++++++++++++++++++++ termios_nonbsd.go | 3 +-- termios_unix.go | 2 +- termios_unsupported.go | 12 +++++++++ 5 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 term_unsupported.go create mode 100644 termios_unsupported.go diff --git a/term_unix.go b/term_unix.go index 251a04f..7ee5e0d 100644 --- a/term_unix.go +++ b/term_unix.go @@ -1,4 +1,4 @@ -//go:build !windows +//go:build !js && !wasip1 && !windows package term diff --git a/term_unsupported.go b/term_unsupported.go new file mode 100644 index 0000000..539064a --- /dev/null +++ b/term_unsupported.go @@ -0,0 +1,56 @@ +//go:build js || wasip1 + +package term + +import ( + "fmt" + "io" + "os" + "runtime" +) + +// terminalState holds the platform-specific state / console mode for the terminal. +type terminalState struct{} + +func stdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) { + return os.Stdin, os.Stdout, os.Stderr +} + +func getFdInfo(in any) (uintptr, bool) { + if file, ok := in.(*os.File); ok { + return file.Fd(), false + } + return 0, false +} + +func getWinsize(fd uintptr) (*Winsize, error) { + return nil, fmt.Errorf("terminal: GetWinsize not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) +} + +func setWinsize(fd uintptr, ws *Winsize) error { + return fmt.Errorf("terminal: SetWinsize not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) +} + +func isTerminal(fd uintptr) bool { + return false +} + +func restoreTerminal(fd uintptr, state *State) error { + return fmt.Errorf("terminal: RestoreTerminal not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) +} + +func saveState(fd uintptr) (*State, error) { + return nil, fmt.Errorf("terminal: SaveState not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) +} + +func disableEcho(fd uintptr, state *State) error { + return fmt.Errorf("terminal: DisableEcho not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) +} + +func setRawTerminal(fd uintptr) (*State, error) { + return fmt.Errorf("terminal: SetRawTerminal not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) +} + +func setRawTerminalOutput(uintptr) (*State, error) { + return fmt.Errorf("terminal: SetRawTerminalOutput not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) +} diff --git a/termios_nonbsd.go b/termios_nonbsd.go index 88b7b21..0a9e922 100644 --- a/termios_nonbsd.go +++ b/termios_nonbsd.go @@ -1,5 +1,4 @@ -//go:build !darwin && !freebsd && !netbsd && !openbsd && !windows -// +build !darwin,!freebsd,!netbsd,!openbsd,!windows +//go:build !darwin && !freebsd && !js && !netbsd && !openbsd && !wasip1 && !windows package term diff --git a/termios_unix.go b/termios_unix.go index a94eebb..c5c7252 100644 --- a/termios_unix.go +++ b/termios_unix.go @@ -1,4 +1,4 @@ -//go:build !windows +//go:build !js && !wasip1 && !windows package term diff --git a/termios_unsupported.go b/termios_unsupported.go new file mode 100644 index 0000000..5712224 --- /dev/null +++ b/termios_unsupported.go @@ -0,0 +1,12 @@ +//go:build js || wasip1 + +package term + +import ( + "fmt" + "runtime" +) + +func makeRaw(fd uintptr) (*State, error) { + return nil, fmt.Errorf("terminal: MakeRaw not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) +}