Skip to content

Commit 64a87e7

Browse files
authored
Merge pull request #120 from shelltime/claude/issue-117-20251003-0854
feat: remove msgpack support and replace with JSON
2 parents 3b1b2a5 + 9d0bdfe commit 64a87e7

14 files changed

Lines changed: 63 additions & 383 deletions

daemon/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package daemon
22

33
import (
44
"context"
5+
"encoding/json"
56
"net"
67
"os"
78
"time"
89

910
"github.com/malamtime/cli/model"
10-
"github.com/vmihailenco/msgpack/v5"
1111
)
1212

1313
func IsSocketReady(ctx context.Context, socketPath string) bool {
@@ -38,7 +38,7 @@ func SendLocalDataToSocket(
3838
},
3939
}
4040

41-
encoded, err := msgpack.Marshal(data)
41+
encoded, err := json.Marshal(data)
4242
if err != nil {
4343
return err
4444
}

daemon/handlers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package daemon
22

33
import (
44
"context"
5+
"encoding/json"
56
"log/slog"
67

78
"github.com/ThreeDotsLabs/watermill/message"
8-
"github.com/vmihailenco/msgpack/v5"
99
)
1010

1111
func SocketTopicProccessor(messages <-chan *message.Message) {
@@ -14,7 +14,7 @@ func SocketTopicProccessor(messages <-chan *message.Message) {
1414
slog.InfoContext(ctx, "received message: ", slog.String("msg.uuid", msg.UUID))
1515

1616
var socketMsg SocketMessage
17-
if err := msgpack.Unmarshal(msg.Payload, &socketMsg); err != nil {
17+
if err := json.Unmarshal(msg.Payload, &socketMsg); err != nil {
1818
slog.ErrorContext(ctx, "failed to parse socket message", slog.Any("err", err))
1919
msg.Nack()
2020
}

daemon/handlers.sync.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ import (
77
"time"
88

99
"github.com/malamtime/cli/model"
10-
"github.com/vmihailenco/msgpack/v5"
1110
)
1211

1312
func handlePubSubSync(ctx context.Context, socketMsgPayload interface{}) error {
14-
pb, err := msgpack.Marshal(socketMsgPayload)
13+
pb, err := json.Marshal(socketMsgPayload)
1514
if err != nil {
1615
slog.Error("Failed to marshal the sync payload again for unmarshal", slog.Any("payload", socketMsgPayload))
1716
return err
1817
}
1918

2019
var syncMsg model.PostTrackArgs
21-
err = msgpack.Unmarshal(pb, &syncMsg)
20+
err = json.Unmarshal(pb, &syncMsg)
2221
if err != nil {
2322
slog.Error("Failed to parse sync payload", slog.Any("payload", socketMsgPayload))
2423
return err

daemon/handlers_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package daemon
33

44
import (
5+
"encoding/json"
56
"net/http"
67
"net/http/httptest"
78
"testing"
@@ -12,7 +13,6 @@ import (
1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/mock"
1415
"github.com/stretchr/testify/suite"
15-
"github.com/vmihailenco/msgpack/v5"
1616
)
1717

1818
type handlersTestSuite struct {
@@ -49,7 +49,7 @@ func (s *handlersTestSuite) TestSocketTopicProcessorValidSync() {
4949
},
5050
},
5151
}
52-
payload, err := msgpack.Marshal(socketMsg)
52+
payload, err := json.Marshal(socketMsg)
5353
assert.NoError(s.T(), err)
5454

5555
msg := message.NewMessage("test-uuid", payload)
@@ -91,7 +91,7 @@ func (s *handlersTestSuite) TestSocketTopicProcessorNonSync() {
9191
},
9292
},
9393
}
94-
payload, err := msgpack.Marshal(socketMsg)
94+
payload, err := json.Marshal(socketMsg)
9595
assert.NoError(s.T(), err)
9696

9797
msg := message.NewMessage("test-uuid", payload)
@@ -112,7 +112,7 @@ func (s *handlersTestSuite) TestSocketTopicProcessorInvalidPayload() {
112112
Type: "sync",
113113
Payload: []byte(`invalid json`),
114114
}
115-
payload, err := msgpack.Marshal(socketMsg)
115+
payload, err := json.Marshal(socketMsg)
116116
assert.NoError(s.T(), err)
117117

118118
msg := message.NewMessage("test-uuid", payload)
@@ -140,7 +140,7 @@ func (s *handlersTestSuite) TestSocketTopicProcessorMultipleMessages() {
140140
},
141141
},
142142
}
143-
payload1, err := msgpack.Marshal(socketMsg1)
143+
payload1, err := json.Marshal(socketMsg1)
144144
assert.NoError(s.T(), err)
145145

146146
socketMsg2 := SocketMessage{
@@ -154,7 +154,7 @@ func (s *handlersTestSuite) TestSocketTopicProcessorMultipleMessages() {
154154
},
155155
},
156156
}
157-
payload2, err := msgpack.Marshal(socketMsg2)
157+
payload2, err := json.Marshal(socketMsg2)
158158
assert.NoError(s.T(), err)
159159

160160
msg1 := message.NewMessage("test-uuid-1", payload1)

daemon/socket.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package daemon
22

33
import (
4+
"encoding/json"
45
"log/slog"
56
"net"
67
"os"
78

89
"github.com/ThreeDotsLabs/watermill"
910
"github.com/ThreeDotsLabs/watermill/message"
10-
"github.com/vmihailenco/msgpack/v5"
1111
)
1212

1313
type SocketMessageType string
@@ -17,9 +17,9 @@ const (
1717
)
1818

1919
type SocketMessage struct {
20-
Type SocketMessageType `msgpack:"type"`
20+
Type SocketMessageType `json:"type"`
2121
// if parse from buffer, it will be the map[any]any
22-
Payload interface{} `msgpack:"payload"`
22+
Payload interface{} `json:"payload"`
2323
}
2424

2525
type SocketHandler struct {
@@ -89,7 +89,7 @@ func (p *SocketHandler) acceptConnections() {
8989

9090
func (p *SocketHandler) handleConnection(conn net.Conn) {
9191
defer conn.Close()
92-
decoder := msgpack.NewDecoder(conn)
92+
decoder := json.NewDecoder(conn)
9393
var msg SocketMessage
9494
if err := decoder.Decode(&msg); err != nil {
9595
slog.Error("Error decoding message", slog.Any("err", err))
@@ -102,7 +102,7 @@ func (p *SocketHandler) handleConnection(conn net.Conn) {
102102
// case "track":
103103
// p.handleTrack(conn, msg.Payload)
104104
case SocketMessageTypeSync:
105-
buf, err := msgpack.Marshal(msg)
105+
buf, err := json.Marshal(msg)
106106
if err != nil {
107107
slog.Error("Error encoding message", slog.Any("err", err))
108108
}

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ require (
1414
github.com/pkg/errors v0.9.1
1515
github.com/sirupsen/logrus v1.9.3
1616
github.com/stretchr/testify v1.10.0
17-
github.com/ugorji/go/codec v1.3.0
1817
github.com/uptrace/uptrace-go v1.37.0
1918
github.com/urfave/cli/v2 v2.27.7
20-
github.com/vmihailenco/msgpack/v5 v5.4.1
2119
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.62.0
2220
go.opentelemetry.io/otel v1.37.0
2321
go.opentelemetry.io/otel/trace v1.37.0
@@ -66,7 +64,6 @@ require (
6664
github.com/sergi/go-diff v1.4.0 // indirect
6765
github.com/skeema/knownhosts v1.3.1 // indirect
6866
github.com/stretchr/objx v0.5.2 // indirect
69-
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
7067
github.com/xanzy/ssh-agent v0.3.3 // indirect
7168
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
7269
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 // indirect

model/alias.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ type Alias struct {
1818
}
1919

2020
type importShellAliasRequest struct {
21-
Aliases []string `json:"aliases" msgpack:"aliases"`
22-
IsFullRefresh bool `json:"isFullRefresh" msgpack:"isFullRefresh"`
23-
ShellType string `json:"shellType" msgpack:"shellType"`
24-
FileLocation string `json:"fileLocation" msgpack:"fileLocation"`
21+
Aliases []string `json:"aliases"`
22+
IsFullRefresh bool `json:"isFullRefresh"`
23+
ShellType string `json:"shellType"`
24+
FileLocation string `json:"fileLocation"`
2525

26-
Hostname string `json:"hostname" msgpack:"hostname"`
27-
Username string `json:"username" msgpack:"username"`
28-
OS string `json:"os" msgpack:"os"`
29-
OSVersion string `json:"osVersion" msgpack:"osVersion"`
26+
Hostname string `json:"hostname"`
27+
Username string `json:"username"`
28+
OS string `json:"os"`
29+
OSVersion string `json:"osVersion"`
3030
}
3131

3232
type importShellAliasResponse struct {

model/api.base.go

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import (
88
"fmt"
99
"io"
1010
"net/http"
11-
"strings"
1211
"time"
1312

1413
"github.com/sirupsen/logrus"
15-
"github.com/vmihailenco/msgpack/v5"
1614
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1715
)
1816

@@ -24,102 +22,10 @@ type HTTPRequestOptions[T any, R any] struct {
2422
Path string
2523
Payload T
2624
Response *R
27-
ContentType string // Optional, defaults to "application/msgpack"
25+
ContentType string // Optional, defaults to "application/json"
2826
Timeout time.Duration // Optional, defaults to 10 seconds
2927
}
3028

31-
// SendHTTPRequest is a legacy HTTP request function that uses msgpack encoding
32-
// Deprecated: Use SendHTTPRequestJSON for new implementations
33-
func SendHTTPRequest[T any, R any](opts HTTPRequestOptions[T, R]) error {
34-
ctx, span := modelTracer.Start(opts.Context, "http.send")
35-
defer span.End()
36-
37-
jsonData, err := msgpack.Marshal(opts.Payload)
38-
if err != nil {
39-
logrus.Errorln(err)
40-
return err
41-
}
42-
43-
timeout := time.Second * 10
44-
if opts.Timeout > 0 {
45-
timeout = opts.Timeout
46-
}
47-
48-
client := &http.Client{
49-
Timeout: timeout,
50-
Transport: otelhttp.NewTransport(http.DefaultTransport),
51-
}
52-
53-
req, err := http.NewRequestWithContext(ctx, opts.Method, opts.Endpoint.APIEndpoint+opts.Path, bytes.NewBuffer(jsonData))
54-
if err != nil {
55-
logrus.Errorln(err)
56-
return err
57-
}
58-
59-
contentType := "application/msgpack"
60-
if opts.ContentType != "" {
61-
contentType = opts.ContentType
62-
}
63-
64-
req.Header.Set("Content-Type", contentType)
65-
req.Header.Set("User-Agent", fmt.Sprintf("shelltimeCLI@%s", commitID))
66-
req.Header.Set("Authorization", "CLI "+opts.Endpoint.Token)
67-
68-
logrus.Traceln("http: ", req.URL.String())
69-
70-
resp, err := client.Do(req)
71-
if err != nil {
72-
logrus.Errorln(err)
73-
return err
74-
}
75-
defer resp.Body.Close()
76-
77-
logrus.Traceln("http: ", resp.Status)
78-
79-
if resp.StatusCode == http.StatusNoContent {
80-
return nil
81-
}
82-
83-
buf, err := io.ReadAll(resp.Body)
84-
if err != nil {
85-
logrus.Errorln(err)
86-
return err
87-
}
88-
89-
if resp.StatusCode != http.StatusOK {
90-
var msg errorResponse
91-
err = json.Unmarshal(buf, &msg)
92-
if err != nil {
93-
logrus.Errorln("Failed to parse error response:", err)
94-
return fmt.Errorf("HTTP error: %d", resp.StatusCode)
95-
}
96-
logrus.Errorln("Error response:", msg.ErrorMessage)
97-
return errors.New(msg.ErrorMessage)
98-
}
99-
100-
// Only try to unmarshal if we have a response struct
101-
if opts.Response != nil {
102-
contentType := resp.Header.Get("Content-Type")
103-
if strings.Contains(contentType, "json") {
104-
err = json.Unmarshal(buf, opts.Response)
105-
if err != nil {
106-
logrus.Errorln("Failed to unmarshal JSON response:", err)
107-
return err
108-
}
109-
return nil
110-
}
111-
if strings.Contains(contentType, "msgpack") {
112-
err = msgpack.Unmarshal(buf, opts.Response)
113-
if err != nil {
114-
logrus.Errorln("Failed to unmarshal response:", err)
115-
return err
116-
}
117-
}
118-
}
119-
120-
return nil
121-
}
122-
12329
// SendHTTPRequestJSON is a generic HTTP request function that sends JSON data and unmarshals the response
12430
func SendHTTPRequestJSON[T any, R any](opts HTTPRequestOptions[T, R]) error {
12531
ctx, span := modelTracer.Start(opts.Context, "http.send.json")

model/api.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,37 @@ type errorResponse struct {
1414
}
1515

1616
type TrackingData struct {
17-
SessionID int64 `json:"sessionId" msgpack:"sessionId"`
18-
Command string `json:"command" msgpack:"command"`
19-
StartTime int64 `json:"startTime" msgpack:"startTime"`
20-
EndTime int64 `json:"endTime" msgpack:"endTime"`
21-
StartTimeNano int64 `json:"startTimeNano" msgpack:"startTimeNano"`
22-
EndTimeNano int64 `json:"endTimeNano" msgpack:"endTimeNano"`
23-
Result int `json:"result" msgpack:"result"`
17+
SessionID int64 `json:"sessionId"`
18+
Command string `json:"command"`
19+
StartTime int64 `json:"startTime"`
20+
EndTime int64 `json:"endTime"`
21+
StartTimeNano int64 `json:"startTimeNano"`
22+
EndTimeNano int64 `json:"endTimeNano"`
23+
Result int `json:"result"`
2424
}
2525

2626
type TrackingMetaData struct {
27-
Hostname string `json:"hostname" msgpack:"hostname"`
28-
Username string `json:"username" msgpack:"username"`
29-
OS string `json:"os" msgpack:"os"`
30-
OSVersion string `json:"osVersion" msgpack:"osVersion"`
31-
Shell string `json:"shell" msgpack:"shell"`
27+
Hostname string `json:"hostname"`
28+
Username string `json:"username"`
29+
OS string `json:"os"`
30+
OSVersion string `json:"osVersion"`
31+
Shell string `json:"shell"`
3232

3333
// 0: cli, 1: daemon
34-
Source int `json:"source" msgpack:"source"`
34+
Source int `json:"source"`
3535
}
3636

3737
type PostTrackArgs struct {
3838
// nano timestamp
39-
CursorID int64 `json:"cursorId" msgpack:"cursorId"`
40-
Data []TrackingData `json:"data" msgpack:"data"`
41-
Meta TrackingMetaData `json:"meta" msgpack:"meta"`
39+
CursorID int64 `json:"cursorId"`
40+
Data []TrackingData `json:"data"`
41+
Meta TrackingMetaData `json:"meta"`
4242

43-
Encrypted string `json:"encrypted" msgpack:"encrypted"`
43+
Encrypted string `json:"encrypted"`
4444
// a base64 encoded AES-GCM key that encrypted by PublicKey from open token
45-
AesKey string `json:"aesKey" msgpack:"aesKey"`
45+
AesKey string `json:"aesKey"`
4646
// the AES-GCM nonce. not encrypted
47-
Nonce string `json:"nonce" msgpack:"nonce"`
47+
Nonce string `json:"nonce"`
4848
}
4949

5050
func doSendData(ctx context.Context, endpoint Endpoint, data PostTrackArgs) error {

0 commit comments

Comments
 (0)