Commit 89d2b689 authored by Andrei Mihu's avatar Andrei Mihu
Browse files

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

parent 0a833b6f
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -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
+17 −4
Original line number Diff line number Diff line
@@ -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)
	}
}