Skip to content

Commit

Permalink
Validation of zip using javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano committed Feb 27, 2022
1 parent 1e13380 commit ef53bb9
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions code/go-wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.wasm
5 changes: 5 additions & 0 deletions code/go-wasm/Makefile
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/...
4 changes: 4 additions & 0 deletions code/go-wasm/README.md
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.
65 changes: 65 additions & 0 deletions code/go-wasm/main.go
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
}
27 changes: 27 additions & 0 deletions code/go-wasm/validate.js
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);
});

0 comments on commit ef53bb9

Please sign in to comment.