add http caller example
parent
f6ac4e734a
commit
ff4ee892fe
36
bridge.go
36
bridge.go
|
@ -8,6 +8,7 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
@ -107,7 +108,9 @@ func (b *Bridge) addValues() {
|
|||
false,
|
||||
&object{
|
||||
props: map[string]interface{}{
|
||||
"Object": propObject("Object", nil),
|
||||
"Object": &object{name: "Object", new: func(args []interface{}) interface{} {
|
||||
return &object{name: "ObjectInner", props: map[string]interface{}{}}
|
||||
}},
|
||||
"Array": propObject("Array", nil),
|
||||
"Int8Array": typedArray,
|
||||
"Int16Array": typedArray,
|
||||
|
@ -137,6 +140,28 @@ func (b *Bridge) addValues() {
|
|||
return rand.Read(arr.data())
|
||||
}),
|
||||
}),
|
||||
"AbortController": &object{name: "AbortController", new: func(args []interface{}) interface{} {
|
||||
return &object{name: "AbortControllerInner", props: map[string]interface{}{
|
||||
"signal": propObject("signal", map[string]interface{}{}),
|
||||
}}
|
||||
}},
|
||||
"Headers": &object{name: "Headers", new: func(args []interface{}) interface{} {
|
||||
headers := http.Header{}
|
||||
obj := &object{name: "HeadersInner", props: map[string]interface{}{
|
||||
"headers": headers,
|
||||
"append": Func(func(args []interface{}) (interface{}, error) {
|
||||
headers.Add(args[0].(string), args[1].(string))
|
||||
return nil, nil
|
||||
}),
|
||||
}}
|
||||
|
||||
return obj
|
||||
}},
|
||||
"fetch": Func(func(args []interface{}) (interface{}, error) {
|
||||
// TODO(ved): implement fetch
|
||||
log.Fatalln(args)
|
||||
return nil, nil
|
||||
}),
|
||||
"fs": propObject("fs", map[string]interface{}{
|
||||
"constants": propObject("constants", map[string]interface{}{
|
||||
"O_WRONLY": syscall.O_WRONLY,
|
||||
|
@ -494,6 +519,15 @@ func Bytes(v interface{}) ([]byte, error) {
|
|||
return arr.data(), nil
|
||||
}
|
||||
|
||||
func String(v interface{}) (string, error) {
|
||||
str, ok := v.(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("got %t instead of string", v)
|
||||
}
|
||||
|
||||
return str, nil
|
||||
}
|
||||
|
||||
func Error(v interface{}) (errVal error, err error) {
|
||||
str, ok := v.(string)
|
||||
if !ok {
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/vedhavyas/go-wasm"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b, err := wasm.BridgeFromFile("test", "./examples/http/main.wasm", nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ctx, canc := context.WithCancel(context.Background())
|
||||
defer canc()
|
||||
init := make(chan error)
|
||||
go b.Run(ctx, init)
|
||||
if err := <-init; err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
res, err := b.CallFunc("call", []interface{}{"https://google.com"})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
str, err := wasm.String(res)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Println("Result:", str)
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
// +build js,wasm
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"syscall/js"
|
||||
)
|
||||
|
||||
func call(this js.Value, args []js.Value) interface{} {
|
||||
res, err := http.Get(args[0].String())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
f := fmt.Sprintln(res.Status, res.StatusCode, res.ContentLength)
|
||||
err = res.Body.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
func main() {
|
||||
js.Global().Set("call", js.FuncOf(call))
|
||||
select {}
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue