-
Notifications
You must be signed in to change notification settings - Fork 241
feat: add import support to wasm playground editor #16440
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
Changes from all commits
dfdbb50
8ea5de7
c8c7958
d4ab500
e56e3e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ package main | |
| import ( | ||
| "syscall/js" | ||
|
|
||
| "github.com/github/gh-aw/pkg/parser" | ||
| "github.com/github/gh-aw/pkg/workflow" | ||
| ) | ||
|
|
||
|
|
@@ -14,21 +15,25 @@ func main() { | |
| } | ||
|
|
||
| // compileWorkflow is the JS-callable function. | ||
| // Usage: compileWorkflow(markdownString) → Promise<{yaml, warnings, error}> | ||
| // Usage: compileWorkflow(markdownString, filesObject?) → Promise<{yaml, warnings, error}> | ||
| // | ||
| // Only a single argument (the markdown string) is accepted. | ||
| // Import resolution is not currently supported in the Wasm build. | ||
| // Arguments: | ||
| // - markdownString: the main workflow markdown content | ||
| // - filesObject (optional): a JS object mapping file paths to content strings, | ||
| // used for import resolution (e.g. {"shared/tools.md": "---\ntools:..."}) | ||
| func compileWorkflow(this js.Value, args []js.Value) any { | ||
| if len(args) < 1 { | ||
| return newRejectedPromise("compileWorkflow requires exactly 1 argument: markdown string") | ||
| } | ||
|
|
||
| if len(args) > 1 { | ||
| return newRejectedPromise("compileWorkflow accepts only 1 argument; importResolver is not supported in the Wasm build") | ||
| return newRejectedPromise("compileWorkflow requires at least 1 argument: markdown string") | ||
| } | ||
|
|
||
| markdown := args[0].String() | ||
|
|
||
| // Extract virtual files from optional second argument | ||
| var files map[string][]byte | ||
| if len(args) >= 2 && !args[1].IsNull() && !args[1].IsUndefined() { | ||
| files = jsObjectToFileMap(args[1]) | ||
| } | ||
|
|
||
| var handler js.Func | ||
| handler = js.FuncOf(func(this js.Value, promiseArgs []js.Value) any { | ||
| resolve := promiseArgs[0] | ||
|
|
@@ -37,7 +42,7 @@ func compileWorkflow(this js.Value, args []js.Value) any { | |
| go func() { | ||
| defer handler.Release() | ||
|
|
||
| result, err := doCompile(markdown) | ||
| result, err := doCompile(markdown, files) | ||
| if err != nil { | ||
| reject.Invoke(js.Global().Get("Error").New(err.Error())) | ||
| return | ||
|
|
@@ -51,8 +56,30 @@ func compileWorkflow(this js.Value, args []js.Value) any { | |
| return js.Global().Get("Promise").New(handler) | ||
| } | ||
|
|
||
| // doCompile performs the actual compilation entirely in memory — no filesystem access. | ||
| func doCompile(markdown string) (js.Value, error) { | ||
| // jsObjectToFileMap converts a JS object {path: content, ...} to map[string][]byte. | ||
| func jsObjectToFileMap(obj js.Value) map[string][]byte { | ||
| files := make(map[string][]byte) | ||
|
|
||
| // Use Object.keys() to iterate over the JS object | ||
| keys := js.Global().Get("Object").Call("keys", obj) | ||
| length := keys.Length() | ||
| for i := 0; i < length; i++ { | ||
| key := keys.Index(i).String() | ||
| value := obj.Get(key).String() | ||
| files[key] = []byte(value) | ||
| } | ||
|
|
||
| return files | ||
| } | ||
|
|
||
| // doCompile performs the actual compilation entirely in memory. | ||
| func doCompile(markdown string, files map[string][]byte) (js.Value, error) { | ||
| // Set up virtual filesystem for import resolution | ||
| if files != nil { | ||
| parser.SetVirtualFiles(files) | ||
| defer parser.ClearVirtualFiles() | ||
| } | ||
|
Comment on lines
+77
to
+81
|
||
|
|
||
| compiler := workflow.NewCompiler( | ||
| workflow.WithNoEmit(true), | ||
| workflow.WithSkipValidation(true), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package parser | ||
|
|
||
| import "os" | ||
|
|
||
| // readFileFunc is the function used to read file contents throughout the parser. | ||
| // In wasm builds, this is overridden to read from a virtual filesystem | ||
| // populated by the browser via SetVirtualFiles. | ||
| var readFileFunc = os.ReadFile |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jsObjectToFileMapassumes the optional second argument is a plain object whose values are strings. If a caller passes a non-object (e.g. a string/array) or non-string values,.String()will coerce silently (e.g. "[object Object]") and lead to confusing compile errors. Consider validatingargs[1]is an object and that each value is a string (or can be converted to one) and rejecting the Promise with a clear message when the shape is invalid.