Skip to content

Commit

Permalink
More consistent signature and handling between JavaScript runtime Bas…
Browse files Browse the repository at this point in the history
…e64 encode functions.
  • Loading branch information
zyro committed Aug 18, 2022
1 parent 0a833b6 commit 89d2b68
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ All notable changes to this project are documented below.
The format is based on [keep a changelog](http://keepachangelog.com) and this project uses [semantic versioning](http://semver.org).

## [Unreleased]

### Changed
- More consistent signature and handling between JavaScript runtime Base64 encode functions.

## [3.13.1] - 2022-08-18
### Fixed
Expand Down
21 changes: 17 additions & 4 deletions server/runtime_javascript_nakama.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,9 @@ func (n *runtimeJavascriptNakamaModule) base64Encode(r *goja.Runtime) func(goja.
padding = getJsBool(r, f.Argument(1))
}

e := base64.URLEncoding
e := base64.StdEncoding
if !padding {
e = base64.RawURLEncoding
e = base64.RawStdEncoding
}

out := e.EncodeToString(in)
Expand Down Expand Up @@ -699,7 +699,20 @@ func (n *runtimeJavascriptNakamaModule) base64Decode(r *goja.Runtime) func(goja.
// @return error(error) An optional error value if an error occurred.
func (n *runtimeJavascriptNakamaModule) base64UrlEncode(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
return func(f goja.FunctionCall) goja.Value {
in := getJsString(r, f.Argument(0))
if goja.IsUndefined(f.Argument(0)) || goja.IsNull(f.Argument(0)) {
panic(r.NewTypeError("expects a string or ArrayBuffer object"))
}

var in []byte
switch v := f.Argument(0).Export(); v.(type) {
case string:
in = []byte(v.(string))
case goja.ArrayBuffer:
in = v.(goja.ArrayBuffer).Bytes()
default:
panic(r.NewTypeError("expects a string or ArrayBuffer object"))
}

padding := true
if f.Argument(1) != goja.Undefined() {
padding = getJsBool(r, f.Argument(1))
Expand All @@ -710,7 +723,7 @@ func (n *runtimeJavascriptNakamaModule) base64UrlEncode(r *goja.Runtime) func(go
e = base64.RawURLEncoding
}

out := e.EncodeToString([]byte(in))
out := e.EncodeToString(in)
return r.ToValue(out)
}
}
Expand Down

0 comments on commit 89d2b68

Please sign in to comment.