From 89d2b68945ecf7e329982cfa5fb3e5ada6dca63d Mon Sep 17 00:00:00 2001 From: Andrei Mihu Date: Thu, 18 Aug 2022 13:38:56 +0100 Subject: [PATCH] More consistent signature and handling between JavaScript runtime Base64 encode functions. --- CHANGELOG.md | 3 ++- server/runtime_javascript_nakama.go | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7377628c68..a15e96fccc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/server/runtime_javascript_nakama.go b/server/runtime_javascript_nakama.go index d2372bb8f8..79259ec78a 100644 --- a/server/runtime_javascript_nakama.go +++ b/server/runtime_javascript_nakama.go @@ -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) @@ -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)) @@ -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) } }