Skip to content

Commit

Permalink
Merge pull request #152 from trheyi/main
Browse files Browse the repository at this point in the history
[change] js bridge bytes type and remove wasm
  • Loading branch information
trheyi authored Sep 21, 2023
2 parents c9ed191 + 0e55ece commit 98c7d55
Show file tree
Hide file tree
Showing 30 changed files with 140 additions and 3,621 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pr-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ jobs:
- name: Checkout V8Go
uses: actions/checkout@v3
with:
repository: yaoapp/v8go
ref: aac4923ed74083e58ae7b42b1acb0fa0d11a26cb
repository: rogchap/v8go
# ref: aac4923ed74083e58ae7b42b1acb0fa0d11a26cb
path: v8go

- name: Checkout Demo WMS
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ jobs:
- name: Checkout V8Go
uses: actions/checkout@v3
with:
repository: yaoapp/v8go
ref: aac4923ed74083e58ae7b42b1acb0fa0d11a26cb
repository: rogchap/v8go
# ref: aac4923ed74083e58ae7b42b1acb0fa0d11a26cb
path: v8go

- name: Checkout Demo WMS
Expand Down
36 changes: 24 additions & 12 deletions api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,21 @@ func ProcessGuard(name string) gin.HandlerFunc {
}
}

// get the origin
func getOrigin(c *gin.Context) string {
referer := c.Request.Referer()
origin := c.Request.Header.Get("Origin")
if origin == "" {
origin = referer
}
return origin
}

// IsAllowed check if the referer is in allow list
func IsAllowed(c *gin.Context, allowsMap map[string]bool) bool {
referer := c.Request.Referer()
if referer != "" {
url, err := url.Parse(referer)
origin := getOrigin(c)
if origin != "" {
url, err := url.Parse(origin)
if err != nil {
return true
}
Expand Down Expand Up @@ -140,20 +150,22 @@ func (http HTTP) Route(router gin.IRoutes, path Path, allows ...string) {
}

// Cross domain
http.crossDomain(path.Path, allowsMap, router)
http.setCorsOption(path.Path, allowsMap, router)
handlers = append(handlers, func(c *gin.Context) {
referer := c.Request.Referer()
if referer != "" {
origin := getOrigin(c)
if origin != "" {

if !IsAllowed(c, allowsMap) {
c.AbortWithStatus(403)
c.JSON(403, gin.H{"code": 403, "message": "referer is not allowed. allows: " + strings.Join(allows, ",")})
c.Abort()
return
}

// url parse
url, _ := url.Parse(referer)
referer = fmt.Sprintf("%s://%s", url.Scheme, url.Host)
url, _ := url.Parse(origin)
origin = fmt.Sprintf("%s://%s", url.Scheme, url.Host)
// fmt.Println("referer is:", referer)
c.Writer.Header().Set("Access-Control-Allow-Origin", referer)
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
Expand Down Expand Up @@ -198,8 +210,8 @@ func (http HTTP) guard(handlers *[]gin.HandlerFunc, guard string, defaults strin
}
}

// crossDomain 跨域许可
func (http HTTP) crossDomain(path string, allows map[string]bool, router gin.IRoutes) {
// setCorsOption 跨域许可
func (http HTTP) setCorsOption(path string, allows map[string]bool, router gin.IRoutes) {
if _, has := registeredOptions[fmt.Sprintf("%s.%s", http.Name, path)]; has {
return
}
Expand Down
11 changes: 8 additions & 3 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (process *Process) Run() interface{} {
exception.New("%s", 500, err.Error()).Throw()
return nil
}

return hd(process)
}

Expand Down Expand Up @@ -71,7 +72,11 @@ func RegisterGroup(name string, group map[string]Handler) {
func Alias(name string, alias string) {
name = strings.ToLower(name)
alias = strings.ToLower(alias)
Handlers[alias] = Handlers[name]
if _, has := Handlers[name]; has {
Handlers[alias] = Handlers[name]
return
}
exception.New("Process: %s does not exist", 404, name).Throw()
}

// WithSID set the session id
Expand All @@ -88,10 +93,10 @@ func (process *Process) WithGlobal(global map[string]interface{}) *Process {

// handler get the process handler
func (process *Process) handler() (Handler, error) {
if hander, has := Handlers[process.Handler]; has {
if hander, has := Handlers[process.Handler]; has && hander != nil {
return hander, nil
}
return nil, fmt.Errorf("Exception|404:%s (%s) not found", process.Name, process.Handler)
return nil, fmt.Errorf("Exception|404:%s Handler -> %s not found", process.Name, process.Handler)
}

// make parse the process
Expand Down
4 changes: 2 additions & 2 deletions process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestRun(t *testing.T) {

// models.widget.Notfound
p = New("models.widget.Notfound", "foo", "bar")
assert.PanicsWithValue(t, *exception.New("models.widget.Notfound (models.notfound) not found", 404), func() {
assert.PanicsWithValue(t, *exception.New("models.widget.Notfound Handler -> models.notfound not found", 404), func() {
p.Run()
})
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestExec(t *testing.T) {
p = New("models.widget.Notfound", "foo", "bar")
res, err = p.Exec()
assert.Equal(t, nil, res)
assert.Equal(t, "Exception|404:models.widget.Notfound (models.notfound) not found", err.Error())
assert.Equal(t, "Exception|404:models.widget.Notfound Handler -> models.notfound not found", err.Error())
}

func TestWithSID(t *testing.T) {
Expand Down
130 changes: 87 additions & 43 deletions runtime/v8/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,32 @@ func GoValues(jsValues []*v8go.Value, ctx *v8go.Context) ([]interface{}, error)

// JsValue cast golang value to JavasScript value
//
// * |-------------------------------------------------------
// * | | Golang | JavaScript |
// * |-------------------------------------------------------
// * | ✅ | nil | null |
// * | ✅ | bool | boolean |
// * | ✅ | int | number(int) |
// * | ✅ | uint | number(int) |
// * | ✅ | uint8 | number(int) |
// * | ✅ | uint16 | number(int) |
// * | ✅ | uint32 | number(int) |
// * | ✅ | int8 | number(int) |
// * | ✅ | int16 | number(int) |
// * | ✅ | int32 | number(int) |
// * | ✅ | float32 | number(float) |
// * | ✅ | float64 | number(float) |
// * | ✅ | int64 | number(int) |
// * | ✅ | uint64 | number(int) |
// * | ✅ | *big.Int | bigint |
// * | ✅ | string | string |
// * | ✅ | map[string]interface{} | object |
// * | ✅ | []interface{} | array |
// * | ✅ | []byte | object(Uint8Array) |
// * | ✅ | struct | object |
// * | ✅ | bridge.PromiseT | object(Promise) |
// * | ✅ | bridge.FunctionT | function |
// * |-------------------------------------------------------
// * |-----------------------------------------------------------
// * | | Golang | JavaScript |
// * |-----------------------------------------------------------
// * | ✅ | nil | null |
// * | ✅ | bool | boolean |
// * | ✅ | int | number(int) |
// * | ✅ | uint | number(int) |
// * | ✅ | uint8 | number(int) |
// * | ✅ | uint16 | number(int) |
// * | ✅ | uint32 | number(int) |
// * | ✅ | int8 | number(int) |
// * | ✅ | int16 | number(int) |
// * | ✅ | int32 | number(int) |
// * | ✅ | float32 | number(float) |
// * | ✅ | float64 | number(float) |
// * | ✅ | int64 | number(int) |
// * | ✅ | uint64 | number(int) |
// * | ✅ | *big.Int | bigint |
// * | ✅ | string | string |
// * | ✅ | map[string]interface{} | object |
// * | ✅ | []interface{} | array |
// * | ✅ | []byte | object(Uint8Array) |
// * | ✅ | struct | object |
// * | ✅ | bridge.PromiseT | object(Promise) |
// * | ✅ | bridge.FunctionT | function |
// * |-----------------------------------------------------------
func JsValue(ctx *v8go.Context, value interface{}) (*v8go.Value, error) {

if value == nil {
Expand All @@ -156,9 +156,25 @@ func JsValue(ctx *v8go.Context, value interface{}) (*v8go.Value, error) {

switch v := value.(type) {

case string, int32, uint32, bool, *big.Int, float64, []byte:
case string, int32, uint32, bool, *big.Int, float64:
return v8go.NewValue(ctx.Isolate(), v)

case []byte:
newObj, err := ctx.RunScript(fmt.Sprintf("new Uint8Array(%d)", len(v)), "")
if err != nil {
return nil, err
}

jsObj, err := newObj.AsObject()
if err != nil {
return nil, err
}

for i := 0; i < len(v); i++ {
jsObj.SetIdx(uint32(i), uint32(v[i]))
}
return jsObj.Value, nil

case int64:
return v8go.NewValue(ctx.Isolate(), int32(v))

Expand Down Expand Up @@ -218,21 +234,22 @@ func jsValueParse(ctx *v8go.Context, value interface{}) (*v8go.Value, error) {
// GoValue cast JavasScript value to Golang value
//
// * JavaScript -> Golang
// * |--------------------------------------------------------
// * | | JavaScript | Golang |
// * |--------------------------------------------------------
// * | ✅ | null | nil |
// * | ✅ | undefined | bridge.Undefined |
// * | ✅ | boolean | bool |
// * | ✅ | number(int) | int |
// * | ✅ | number(float) | float64 |
// * | ✅ | bigint | int64 |
// * | ✅ | string | string |
// * | ✅ | object(Int8Array) | []byte |
// * | ✅ | object | map[string]interface{} |
// * | ✅ | array | []interface{} |
// * | ✅ | object(Promise) | bridge.PromiseT |
// * | ✅ | function | bridge.FunctionT |
// * |-----------------------------------------------------------
// * | | JavaScript | Golang |
// * |-----------------------------------------------------------
// * | ✅ | null | nil |
// * | ✅ | undefined | bridge.Undefined |
// * | ✅ | boolean | bool |
// * | ✅ | number(int) | int |
// * | ✅ | number(float) | float64 |
// * | ✅ | bigint | int64 |
// * | ✅ | string | string |
// * | ✅ | object(SharedArrayBuffer) | []byte |
// * | ✅ | object(Uint8Array) | []byte |
// * | ✅ | object | map[string]interface{} |
// * | ✅ | array | []interface{} |
// * | ✅ | object(Promise) | bridge.PromiseT |
// * | ✅ | function | bridge.FunctionT |
// * |-------------------------------------------------------
func GoValue(value *v8go.Value, ctx *v8go.Context) (interface{}, error) {

Expand Down Expand Up @@ -273,8 +290,35 @@ func GoValue(value *v8go.Value, ctx *v8go.Context) (interface{}, error) {
return value.BigInt().Int64(), nil
}

if value.IsSharedArrayBuffer() { // bytes
buf, cleanup, err := value.SharedArrayBufferGetContents()
if err != nil {
return nil, err
}
defer cleanup()
return buf, nil
}

if value.IsUint8Array() { // bytes
return value.Uint8Array(), nil
arr, err := value.AsObject()
if err != nil {
return nil, err
}

length, err := arr.Get("length")
if err != nil {
return nil, err
}

var goValue []byte
for i := uint32(0); i < length.Uint32(); i++ {
v, err := arr.GetIdx(i)
if err != nil {
return nil, err
}
goValue = append(goValue, byte(v.Uint32()))
}
return goValue, nil
}

if value.IsArray() {
Expand Down
12 changes: 12 additions & 0 deletions runtime/v8/bridge/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ func TestReturnUint8Array(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.Len(t, res, 2)
checkReturn(t, []byte{0x1a, 0x2a}, res)
}

func TestReturnSharedArrayBuffer(t *testing.T) {
ctx := prepare(t)
defer close(ctx)
res, err := call(ctx, "ReturnSharedArrayBuffer")
if err != nil {
t.Fatal(err)
}
assert.Len(t, res, 1)
checkReturn(t, []byte{0x2a}, res)
}

Expand Down
6 changes: 3 additions & 3 deletions runtime/v8/bridge/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ func TestValueOfArray(t *testing.T) {
checkValueOf(t, res, "object", value)
}

func TestValueOfUint8Array(t *testing.T) {
func TestValueUint8Array(t *testing.T) {
ctx := prepare(t)
defer close(ctx)

value := []byte{0x2a}
res, err := call(ctx, "ValueOfUint8Array", value)
value := []byte{0x1a, 0x2a}
res, err := call(ctx, "ValueUint8Array", value)
if err != nil {
t.Fatal(err)
}
Expand Down
19 changes: 0 additions & 19 deletions runtime/wasm/benchmark_test.go

This file was deleted.

Loading

0 comments on commit 98c7d55

Please sign in to comment.