-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
*.wasm |
This file contains 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,5 @@ | ||
build: | ||
GOOS=js GOARCH=wasm go build -o validator.wasm . | ||
|
||
test: | ||
GOOS=js GOARCH=wasm go test -v -exec="`go env GOROOT`/misc/wasm/go_js_wasm_exec" . ../go/... |
This file contains 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,4 @@ | ||
* Build wasm with `make build`. | ||
* Run `./validate.js /path/to/package.zip` (or `node validate.js /path/to/package.zip`). | ||
|
||
Node.js and Go are required. |
This file contains 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,65 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"syscall/js" | ||
|
||
"github.com/elastic/package-spec/code/go/pkg/validator" | ||
) | ||
|
||
const moduleName = "elasticPackageSpec" | ||
|
||
// asyncFunc helps creating functions that return a promise. | ||
// | ||
// Calling async JavaScript APIs causes deadlocks in the JS event loop. Not sure | ||
// how to find if a Go code does it, but for example ValidateFromZip does, so | ||
// we need to run this code in a goroutine and return the result as a promise. | ||
// Related: https://github.com/golang/go/issues/41310 | ||
func asyncFunc(fn func(this js.Value, args []js.Value) interface{}) js.Func { | ||
return js.FuncOf(func(this js.Value, args []js.Value) interface{} { | ||
handler := js.FuncOf(func(_ js.Value, handlerArgs []js.Value) interface{} { | ||
resolve := handlerArgs[0] | ||
reject := handlerArgs[1] | ||
|
||
go func() { | ||
result := fn(this, args) | ||
if err, ok := result.(error); ok { | ||
reject.Invoke(err.Error()) | ||
return | ||
} | ||
resolve.Invoke(result) | ||
}() | ||
|
||
return nil | ||
}) | ||
|
||
return js.Global().Get("Promise").New(handler) | ||
}) | ||
} | ||
|
||
func main() { | ||
// It doesn't seem to be possible yet to export values as part of the compiled instance. | ||
// So we have to expose it by setting a global value. It may worth to explore tynigo for this. | ||
// Related: https://github.com/golang/go/issues/42372 | ||
js.Global().Set(moduleName, make(map[string]interface{})) | ||
module := js.Global().Get(moduleName) | ||
module.Set("validateFromZip", asyncFunc( | ||
func(this js.Value, args []js.Value) interface{} { | ||
if len(args) == 0 || args[0].IsNull() || args[0].IsUndefined() { | ||
return fmt.Errorf("package path expected") | ||
} | ||
|
||
pkgPath := args[0].String() | ||
return validator.ValidateFromZip(pkgPath) | ||
}, | ||
)) | ||
|
||
// Go runtime must be always available at any moment where exported functionality | ||
// can be executed, so keep it running till done. | ||
done := make(chan struct{}) | ||
module.Set("stop", js.FuncOf(func(_ js.Value, _ []js.Value) interface{} { | ||
close(done) | ||
return nil | ||
})) | ||
<-done | ||
} |
This file contains 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,27 @@ | ||
#!/usr/bin/env node | ||
|
||
if (process.argv.length < 3) { | ||
console.error("usage: " + process.argv[1] + " [package path]"); | ||
process.exit(1); | ||
} | ||
|
||
const path = require('path') | ||
const fs = require('fs'); | ||
|
||
require(path.join(process.env.GOROOT, "misc/wasm/wasm_exec.js")); | ||
const go = new Go(); | ||
|
||
const wasmBuffer = fs.readFileSync('validator.wasm'); | ||
WebAssembly.instantiate(wasmBuffer, go.importObject).then((validator) => { | ||
go.run(validator.instance); | ||
|
||
elasticPackageSpec.validateFromZip(process.argv[2]).then(() => | ||
console.log("OK") | ||
).catch((err) => | ||
console.error(err) | ||
).finally(() => | ||
elasticPackageSpec.stop() | ||
) | ||
}).catch((err) => { | ||
console.error(err); | ||
}); |