Commit ad1f9c03 authored by Andrei Mihu's avatar Andrei Mihu
Browse files

Improve signature of JavaScript runtime Base16 encode and decode functions.

parent 6dc1f2fe
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
- Improve handling of database connections going through proxies.
- Improve extraction of purchases and subscriptions from Apple receipts.
- Improve signature of JavaScript runtime Base64 decode functions.
- Improve signature of JavaScript runtime Base16 encode and decode functions.

### Fixed
- Graceful handling of storage list errors in JavaScript runtime.
+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ require (
	github.com/gorilla/mux v1.8.0
	github.com/gorilla/websocket v1.4.2
	github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0
	github.com/heroiclabs/nakama-common v0.0.0-20220914101240-3f8026e3577d
	github.com/heroiclabs/nakama-common v0.0.0-20220923113023-a8923ae42e2e
	github.com/jackc/pgconn v1.10.0
	github.com/jackc/pgerrcode v0.0.0-20201024163028-a0d42d470451
	github.com/jackc/pgtype v1.8.1
+2 −2
Original line number Diff line number Diff line
@@ -291,8 +291,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/heroiclabs/nakama-common v0.0.0-20220914101240-3f8026e3577d h1:mxvlHXFcPe/amuFlz3LveeyCZr4SK7zanAHjUYqwm1U=
github.com/heroiclabs/nakama-common v0.0.0-20220914101240-3f8026e3577d/go.mod h1:WF4YG46afwY3ibzsXnkt3zvhQ3tBY03IYeU7xSLr8HE=
github.com/heroiclabs/nakama-common v0.0.0-20220923113023-a8923ae42e2e h1:5N9WNQ3l6vfBDgiqD7DbIuJU/Cs/xr8CzOhe3tHk+zs=
github.com/heroiclabs/nakama-common v0.0.0-20220923113023-a8923ae42e2e/go.mod h1:WF4YG46afwY3ibzsXnkt3zvhQ3tBY03IYeU7xSLr8HE=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
+19 −7
Original line number Diff line number Diff line
@@ -667,7 +667,7 @@ func (n *runtimeJavascriptNakamaModule) base64Encode(r *goja.Runtime) func(goja.
// @group utils
// @summary Decode a base64 encoded string.
// @param input(type=string) The string which will be base64 decoded.
// @return out(ArrayBuffer) Decoded string.
// @return out(ArrayBuffer) Decoded data.
// @return error(error) An optional error value if an error occurred.
func (n *runtimeJavascriptNakamaModule) base64Decode(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
	return func(f goja.FunctionCall) goja.Value {
@@ -731,7 +731,7 @@ func (n *runtimeJavascriptNakamaModule) base64UrlEncode(r *goja.Runtime) func(go
// @group utils
// @summary Decode a base64 URL encoded string.
// @param input(type=string) The string to be decoded.
// @return out(ArrayBuffer) Decoded string.
// @return out(ArrayBuffer) Decoded data.
// @return error(error) An optional error value if an error occurred.
func (n *runtimeJavascriptNakamaModule) base64UrlDecode(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
	return func(f goja.FunctionCall) goja.Value {
@@ -757,15 +757,27 @@ func (n *runtimeJavascriptNakamaModule) base64UrlDecode(r *goja.Runtime) func(go
}

// @group utils
// @summary base16 encode a string input.
// @summary base16 encode a string or ArrayBuffer input.
// @param input(type=string) The string to be encoded.
// @return out(string) Encoded string.
// @return error(error) An optional error value if an error occurred.
func (n *runtimeJavascriptNakamaModule) base16Encode(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"))
		}

		out := hex.EncodeToString([]byte(in))
		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"))
		}

		out := hex.EncodeToString(in)
		return r.ToValue(out)
	}
}
@@ -773,7 +785,7 @@ func (n *runtimeJavascriptNakamaModule) base16Encode(r *goja.Runtime) func(goja.
// @group utils
// @summary Decode a base16 encoded string.
// @param input(type=string) The string to be decoded.
// @return out(string) Decoded string.
// @return out(ArrayBuffer) Decoded data.
// @return error(error) An optional error value if an error occurred.
func (n *runtimeJavascriptNakamaModule) base16Decode(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
	return func(f goja.FunctionCall) goja.Value {
@@ -783,7 +795,7 @@ func (n *runtimeJavascriptNakamaModule) base16Decode(r *goja.Runtime) func(goja.
		if err != nil {
			panic(r.NewGoError(fmt.Errorf("Failed to decode string: %s", in)))
		}
		return r.ToValue(string(out))
		return r.ToValue(r.NewArrayBuffer(out))
	}
}

+1 −1
Original line number Diff line number Diff line
@@ -142,7 +142,7 @@ github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopena
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options
github.com/grpc-ecosystem/grpc-gateway/v2/runtime
github.com/grpc-ecosystem/grpc-gateway/v2/utilities
# github.com/heroiclabs/nakama-common v0.0.0-20220914101240-3f8026e3577d
# github.com/heroiclabs/nakama-common v0.0.0-20220923113023-a8923ae42e2e
## explicit; go 1.14
github.com/heroiclabs/nakama-common/api
github.com/heroiclabs/nakama-common/rtapi