Skip to content

Commit

Permalink
Replace Uint8Array with ArrayBuffer where applicable in the JS runtim…
Browse files Browse the repository at this point in the history
  • Loading branch information
formatCvt authored May 23, 2022
1 parent 239ac88 commit 7dbdb14
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion server/runtime_javascript_match_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ func (rm *RuntimeJavaScriptMatchCore) validateBroadcast(r *goja.Runtime, f goja.
case goja.ArrayBuffer:
dataBytes = dataExport.(goja.ArrayBuffer).Bytes()
default:
panic(r.NewTypeError("expects data to be an Uint8Array, a string or nil"))
panic(r.NewTypeError("expects data to be an ArrayBuffer, a string or nil"))
}
}

Expand Down
10 changes: 5 additions & 5 deletions server/runtime_javascript_nakama.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,18 @@ func (n *runtimeJavascriptNakamaModule) mappings(r *goja.Runtime) map[string]fun

// @group utils
// @summary Convert binary data to string.
// @param data(type=Uint8Array) The binary data to be converted.
// @param data(type=ArrayBuffer) The binary data to be converted.
// @return result(type=string) The resulting string.
// @return error(error) An optional error value if an error occurred.
func (n *runtimeJavascriptNakamaModule) binaryToString(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
return func(f goja.FunctionCall) goja.Value {
if goja.IsUndefined(f.Argument(0)) || goja.IsNull(f.Argument(0)) {
panic(r.NewTypeError("expects a Uint8Array object"))
panic(r.NewTypeError("expects a ArrayBuffer object"))
}

data, ok := f.Argument(0).Export().(goja.ArrayBuffer)
if !ok {
panic(r.NewTypeError("expects a Uint8Array object"))
panic(r.NewTypeError("expects a ArrayBuffer object"))
}

if !utf8.Valid(data.Bytes()) {
Expand All @@ -297,7 +297,7 @@ func (n *runtimeJavascriptNakamaModule) binaryToString(r *goja.Runtime) func(goj
// @group utils
// @summary Convert string data to binary.
// @param str(type=string) The string to be converted.
// @return result(type=Uint8Array) The resulting binary data.
// @return result(type=ArrayBuffer) The resulting binary data.
// @return error(error) An optional error value if an error occurred.
func (n *runtimeJavascriptNakamaModule) stringToBinary(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
return func(f goja.FunctionCall) goja.Value {
Expand All @@ -310,7 +310,7 @@ func (n *runtimeJavascriptNakamaModule) stringToBinary(r *goja.Runtime) func(goj
panic(r.NewTypeError("expects a string"))
}

return r.ToValue([]byte(str))
return r.ToValue(r.NewArrayBuffer([]byte(str)))
}
}

Expand Down

0 comments on commit 7dbdb14

Please sign in to comment.