From 2f5d5e147fdf6953688ca5fcb01bc1df56ebaa93 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 29 Aug 2024 14:40:58 +0000 Subject: [PATCH 1/2] Bump github.com/nats-io/nats.go from 1.36.0 to 1.37.0 Bumps [github.com/nats-io/nats.go](https://github.com/nats-io/nats.go) from 1.36.0 to 1.37.0. - [Release notes](https://github.com/nats-io/nats.go/releases) - [Commits](https://github.com/nats-io/nats.go/compare/v1.36.0...v1.37.0) --- updated-dependencies: - dependency-name: github.com/nats-io/nats.go dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 619d9c2e..91202352 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/mailru/easyjson v0.7.7 github.com/marcw/cachecontrol v0.0.0-20140722115028-30341fe9a7d5 github.com/nats-io/nats-server/v2 v2.10.19 - github.com/nats-io/nats.go v1.36.0 + github.com/nats-io/nats.go v1.37.0 github.com/notedit/janus-go v0.0.0-20200517101215-10eb8b95d1a0 github.com/oschwald/maxminddb-golang v1.13.1 github.com/pion/sdp/v3 v3.0.9 diff --git a/go.sum b/go.sum index ffcb5669..9408f139 100644 --- a/go.sum +++ b/go.sum @@ -123,8 +123,8 @@ github.com/nats-io/jwt/v2 v2.5.8 h1:uvdSzwWiEGWGXf+0Q+70qv6AQdvcvxrv9hPM0RiPamE= github.com/nats-io/jwt/v2 v2.5.8/go.mod h1:ZdWS1nZa6WMZfFwwgpEaqBV8EPGVgOTDHN/wTbz0Y5A= github.com/nats-io/nats-server/v2 v2.10.19 h1:zf+B16ZbzzGGogvDwCOjSCR5776kix/JgF84TiWNSQk= github.com/nats-io/nats-server/v2 v2.10.19/go.mod h1:hgcPnoUtMfxz1qVOvLZGurVypQ+Cg6GXVXjG53iHk+M= -github.com/nats-io/nats.go v1.36.0 h1:suEUPuWzTSse/XhESwqLxXGuj8vGRuPRoG7MoRN/qyU= -github.com/nats-io/nats.go v1.36.0/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8= +github.com/nats-io/nats.go v1.37.0 h1:07rauXbVnnJvv1gfIyghFEo6lUcYRY0WXc3x7x0vUxE= +github.com/nats-io/nats.go v1.37.0/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8= github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI= github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc= github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= From b06f5a84dfe2b7af5e0a24679a4fb75f02240e8e Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Thu, 29 Aug 2024 16:51:34 +0200 Subject: [PATCH 2/2] Stop using deprecated "nats.EncodedConn". --- natsclient.go | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/natsclient.go b/natsclient.go index 6a9feb7e..3c2f6d15 100644 --- a/natsclient.go +++ b/natsclient.go @@ -24,10 +24,12 @@ package signaling import ( "context" "encoding/base64" + "encoding/json" "fmt" "log" "os" "os/signal" + "strings" "time" "github.com/nats-io/nats.go" @@ -61,8 +63,7 @@ func GetEncodedSubject(prefix string, suffix string) string { } type natsClient struct { - nc *nats.Conn - conn *nats.EncodedConn + conn *nats.Conn } func NewNatsClient(url string) (NatsClient, error) { @@ -82,7 +83,7 @@ func NewNatsClient(url string) (NatsClient, error) { client := &natsClient{} - client.nc, err = nats.Connect(url, + client.conn, err = nats.Connect(url, nats.ClosedHandler(client.onClosed), nats.DisconnectHandler(client.onDisconnected), nats.ReconnectHandler(client.onReconnected)) @@ -98,12 +99,9 @@ func NewNatsClient(url string) (NatsClient, error) { return nil, fmt.Errorf("interrupted") } - client.nc, err = nats.Connect(url) + client.conn, err = nats.Connect(url) } - log.Printf("Connection established to %s (%s)", client.nc.ConnectedUrl(), client.nc.ConnectedServerId()) - - // All communication will be JSON based. - client.conn, _ = nats.NewEncodedConn(client.nc, nats.JSON_ENCODER) + log.Printf("Connection established to %s (%s)", client.conn.ConnectedUrl(), client.conn.ConnectedServerId()) return client, nil } @@ -124,13 +122,34 @@ func (c *natsClient) onReconnected(conn *nats.Conn) { } func (c *natsClient) Subscribe(subject string, ch chan *nats.Msg) (NatsSubscription, error) { - return c.nc.ChanSubscribe(subject, ch) + return c.conn.ChanSubscribe(subject, ch) } func (c *natsClient) Publish(subject string, message interface{}) error { - return c.conn.Publish(subject, message) + data, err := json.Marshal(message) + if err != nil { + return err + } + + return c.conn.Publish(subject, data) } -func (c *natsClient) Decode(msg *nats.Msg, v interface{}) error { - return c.conn.Enc.Decode(msg.Subject, msg.Data, v) +func (c *natsClient) Decode(msg *nats.Msg, vPtr interface{}) (err error) { + switch arg := vPtr.(type) { + case *string: + // If they want a string and it is a JSON string, strip quotes + // This allows someone to send a struct but receive as a plain string + // This cast should be efficient for Go 1.3 and beyond. + str := string(msg.Data) + if strings.HasPrefix(str, `"`) && strings.HasSuffix(str, `"`) { + *arg = str[1 : len(str)-1] + } else { + *arg = str + } + case *[]byte: + *arg = msg.Data + default: + err = json.Unmarshal(msg.Data, arg) + } + return }