-
Notifications
You must be signed in to change notification settings - Fork 159
feat: wasm build #3024
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
Merged
Merged
feat: wasm build #3024
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
7890fde
feat: wasm build
Codelax 98cfff9
add wasm main draft
Codelax fd9f81c
build target for wasm main
Codelax c3d12ff
scw-wasm: add callback when functions are loaded
Codelax d00fc76
add wasm package
Codelax 6d44ba7
scw-wasm: get global export object and callback from arguments
Codelax b770fe4
change to typescript
Codelax 69ddce5
add build and test to CI
Codelax 13a628f
add pnpm install to test script
Codelax 9e19a78
add build directive to cmd/scw-wasm/args.go
Codelax 8f71063
fix pnpm include and rename github workflow
Codelax 7b44e08
fix linter
Codelax c3a916f
add js go dependencies in wasm package
Codelax 064344a
rename github workflow
Codelax 6253586
remove pnpm include
Codelax 4e83274
add missing newline
Codelax d1d24dc
remove debug prints
Codelax ed00f84
add index and error test
Codelax 27d6b25
move go cjs files and export them
Codelax e74f29c
fix import in wasm tests
Codelax 4588b47
export type definitions
Codelax cd73180
Merge branch 'master' into feat/wasm-build
remyleone 710cf97
Merge branch 'master' into feat/wasm-build
Codelax d55f76c
disable container deploy helpers
Codelax 5255acd
add newlines
Codelax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| name: WebAssembly | ||
|
|
||
| on: | ||
| pull_request: | ||
| merge_group: | ||
|
|
||
| jobs: | ||
| build-and-test: | ||
| strategy: | ||
| matrix: | ||
| go-version: [1.19.x] | ||
| platform: [ubuntu-latest] | ||
| runs-on: ${{ matrix.platform }} | ||
| steps: | ||
| - name: Install Go | ||
| uses: actions/setup-go@v3 | ||
| with: | ||
| go-version: ${{ matrix.go-version }} | ||
| - name: Install pnpm | ||
| uses: pnpm/action-setup@v2 | ||
| with: | ||
| version: 6.0.2 | ||
| - name: Checkout | ||
| uses: actions/checkout@v3 | ||
| with: | ||
| fetch-depth: 1 | ||
| - name: Build | ||
| run: ./scripts/build-wasm.sh | ||
| - name: Run npm package tests | ||
| run: ./scripts/run-tests-wasm.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //go:build wasm && js | ||
|
|
||
| package main | ||
|
|
||
| import "os" | ||
|
|
||
| type Args struct { | ||
| callback string | ||
| targetObject string | ||
| } | ||
|
|
||
| func getArgs() Args { | ||
| args := Args{} | ||
| if len(os.Args) > 0 { | ||
| args.callback = os.Args[0] | ||
| } | ||
| if len(os.Args) > 1 { | ||
| args.targetObject = os.Args[1] | ||
| } | ||
|
|
||
| return args | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| //go:build wasm && js | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "syscall/js" | ||
| ) | ||
|
|
||
| type fn func(this js.Value, args []js.Value) (any, error) | ||
|
|
||
| var ( | ||
| jsErr js.Value = js.Global().Get("Error") | ||
| jsObject js.Value = js.Global().Get("Object") | ||
| jsPromise js.Value = js.Global().Get("Promise") | ||
| ) | ||
|
|
||
| func asyncFunc(innerFunc fn) js.Func { | ||
| return js.FuncOf(func(this js.Value, args []js.Value) any { | ||
| handler := js.FuncOf(func(_ js.Value, promFn []js.Value) any { | ||
| resolve, reject := promFn[0], promFn[1] | ||
|
|
||
| go func() { | ||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| reject.Invoke(jsErr.New(fmt.Sprint("panic:", r))) | ||
| } | ||
| }() | ||
|
|
||
| res, err := innerFunc(this, args) | ||
| if err != nil { | ||
| errContent := jsObject.New() | ||
| errContent.Set("cause", err.Error()) | ||
| reject.Invoke(jsErr.New(res, errContent)) | ||
| } else { | ||
| resolve.Invoke(res) | ||
| } | ||
| }() | ||
|
|
||
| return nil | ||
| }) | ||
|
|
||
| return jsPromise.New(handler) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| //go:build wasm && js | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "io" | ||
| "syscall/js" | ||
|
|
||
| "github.com/scaleway/scaleway-cli/v2/internal/core" | ||
| "github.com/scaleway/scaleway-cli/v2/internal/namespaces" | ||
| ) | ||
|
|
||
| var commands *core.Commands | ||
|
|
||
| func getCommands() *core.Commands { | ||
| if commands == nil { | ||
| commands = namespaces.GetCommands() | ||
| } | ||
| return commands | ||
| } | ||
|
|
||
| func runCommand(args []string, stdout io.Writer, stderr io.Writer) chan int { | ||
| ret := make(chan int, 1) | ||
| go func() { | ||
| exitCode, _, _ := core.Bootstrap(&core.BootstrapConfig{ | ||
| Args: args, | ||
| Commands: getCommands(), | ||
| BuildInfo: &core.BuildInfo{}, | ||
| Stdout: stdout, | ||
| Stderr: stderr, | ||
| Stdin: nil, | ||
| }) | ||
| ret <- exitCode | ||
| }() | ||
|
|
||
| return ret | ||
| } | ||
|
|
||
| func wasmRun(this js.Value, args []js.Value) (any, error) { | ||
| cliArgs := []string{"scw"} | ||
| stdout := bytes.NewBuffer(nil) | ||
| stderr := bytes.NewBuffer(nil) | ||
|
|
||
| for argIndex, arg := range args { | ||
| if arg.Type() != js.TypeString { | ||
| return nil, fmt.Errorf("invalid argument at index %d", argIndex) | ||
| } | ||
| cliArgs = append(cliArgs, arg.String()) | ||
| } | ||
|
|
||
| exitCodeChan := runCommand(cliArgs, stdout, stderr) | ||
| exitCode := <-exitCodeChan | ||
| if exitCode != 0 { | ||
| errBody := stderr.String() | ||
| return js.ValueOf(errBody), fmt.Errorf("exit code: %d", exitCode) | ||
| } | ||
|
|
||
| outBody := stdout.String() | ||
|
|
||
| return js.ValueOf(outBody), nil | ||
| } | ||
|
|
||
| func main() { | ||
| args := getArgs() | ||
|
|
||
| if args.targetObject != "" { | ||
| cliPackage := js.ValueOf(map[string]any{}) | ||
| cliPackage.Set("run", asyncFunc(wasmRun)) | ||
| js.Global().Set(args.targetObject, cliPackage) | ||
| } | ||
|
|
||
| if args.callback != "" { | ||
| givenCallback := js.Global().Get(args.callback) | ||
| if !givenCallback.IsUndefined() { | ||
| givenCallback.Invoke() | ||
| } | ||
| } | ||
| <-make(chan struct{}, 0) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| //go:build freebsd | ||
| // +build freebsd | ||
| //go:build freebsd || wasm | ||
|
|
||
| package core | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| //go:build !wasm | ||
|
|
||
| package gotty | ||
|
|
||
| import ( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| //nolint | ||
| // +build !windows | ||
| //go:build !windows && !wasm | ||
|
|
||
| package gotty | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| //go:build !wasm | ||
|
|
||
| package interactive | ||
|
|
||
| import ( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| //go:build wasm | ||
|
|
||
| package interactive | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| ) | ||
|
|
||
| type PromptPasswordConfig struct { | ||
| Ctx context.Context | ||
| Prompt string | ||
| } | ||
|
|
||
| func PromptPasswordWithConfig(config *PromptPasswordConfig) (string, error) { | ||
| return "", fmt.Errorf("prompt is disabled for this build") | ||
| } | ||
|
|
||
| type PromptBoolConfig struct { | ||
| Ctx context.Context | ||
| Prompt string | ||
| DefaultValue bool | ||
| } | ||
|
|
||
| func PromptBoolWithConfig(config *PromptBoolConfig) (bool, error) { | ||
| return config.DefaultValue, nil | ||
| } | ||
|
|
||
| type PromptStringConfig struct { | ||
| Ctx context.Context | ||
| Prompt string | ||
| DefaultValue string | ||
| DefaultValueDoc string | ||
| ValidateFunc ValidateFunc | ||
| } | ||
|
|
||
| func PromptStringWithConfig(config *PromptStringConfig) (string, error) { | ||
| return config.DefaultValue, nil | ||
| } | ||
|
|
||
| type ReadlineConfig struct { | ||
| Ctx context.Context | ||
| Prompt string | ||
| PromptFunc func(string) string | ||
| Password bool | ||
| ValidateFunc ValidateFunc | ||
| DefaultValue string | ||
| } | ||
|
|
||
| func Readline(config *ReadlineConfig) (string, error) { | ||
| return "", fmt.Errorf("prompt is disabled for this build") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| //go:build !wasm | ||
|
|
||
| package interactive | ||
|
|
||
| import ( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| //go:build wasm | ||
|
|
||
| package interactive | ||
|
|
||
| import ( | ||
| "io" | ||
| ) | ||
|
|
||
| type ValidateFunc func(string) error | ||
|
|
||
| var ( | ||
| // IsInteractive must be set to print anything with Printer functions (Print, Printf,...). | ||
| IsInteractive = false | ||
|
|
||
| // outputWriter is the writer used by Printer functions (Print, Printf,...). | ||
| outputWriter io.Writer | ||
| ) | ||
|
|
||
| // SetOutputWriter set the output writer that will be used by both Printer functions (Print, Printf,...) and | ||
| // readline prompter. This should be called once from the bootstrap function. | ||
| func SetOutputWriter(w io.Writer) { | ||
| outputWriter = w | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| //go:build !wasm | ||
|
|
||
| package container | ||
|
|
||
| import ( | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.