forked from pusher/pusher-http-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
120 lines (112 loc) · 3.11 KB
/
encoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package pusher
import (
"encoding/json"
"errors"
"fmt"
)
// defaultMaxEventPayloadSizeKB indicates the max size allowed for the data content
// (payload) of each event, unless an override is present in the client
const defaultMaxEventPayloadSizeKB = 10
type batchEvent struct {
Channel string `json:"channel"`
Name string `json:"name"`
Data string `json:"data"`
SocketID *string `json:"socket_id,omitempty"`
Info *string `json:"info,omitempty"`
}
type batchPayload struct {
Batch []batchEvent `json:"batch"`
}
func encodeTriggerBody(
channels []string,
event string,
data interface{},
params map[string]string,
encryptionKey []byte,
overrideMaxMessagePayloadKB int,
) ([]byte, error) {
dataBytes, err := encodeEventData(data)
if err != nil {
return nil, err
}
var payloadData string
if isEncryptedChannel(channels[0]) {
payloadData = encrypt(channels[0], dataBytes, encryptionKey)
} else {
payloadData = string(dataBytes)
}
eventExceedsMaximumSize := false
if overrideMaxMessagePayloadKB == 0 {
eventExceedsMaximumSize = len(payloadData) > defaultMaxEventPayloadSizeKB*1024
} else {
eventExceedsMaximumSize = len(payloadData) > overrideMaxMessagePayloadKB*1024
}
if eventExceedsMaximumSize {
return nil, errors.New(fmt.Sprintf("Event payload exceeded maximum size (%d bytes is too much)", len(payloadData)))
}
eventPayload := map[string]interface{}{
"name": event,
"channels": channels,
"data": payloadData,
}
for k, v := range params {
if _, ok := eventPayload[k]; ok {
return nil, errors.New(fmt.Sprintf("Paramater %s specified multiple times", k))
}
eventPayload[k] = v
}
return json.Marshal(eventPayload)
}
func encodeTriggerBatchBody(
batch []Event,
encryptionKey []byte,
overrideMaxMessagePayloadKB int,
) ([]byte, error) {
batchEvents := make([]batchEvent, len(batch))
for idx, e := range batch {
var stringifyedDataBytes string
dataBytes, err := encodeEventData(e.Data)
if err != nil {
return nil, err
}
if isEncryptedChannel(e.Channel) {
stringifyedDataBytes = encrypt(e.Channel, dataBytes, encryptionKey)
} else {
stringifyedDataBytes = string(dataBytes)
}
eventExceedsMaximumSize := false
if overrideMaxMessagePayloadKB == 0 {
eventExceedsMaximumSize = len(stringifyedDataBytes) > defaultMaxEventPayloadSizeKB*1024
} else {
eventExceedsMaximumSize = len(stringifyedDataBytes) > overrideMaxMessagePayloadKB*1024
}
if eventExceedsMaximumSize {
return nil, fmt.Errorf("Data of the event #%d in batch, exceeded maximum size (%d bytes is too much)", idx, len(stringifyedDataBytes))
}
newBatchEvent := batchEvent{
Channel: e.Channel,
Name: e.Name,
Data: stringifyedDataBytes,
SocketID: e.SocketID,
Info: e.Info,
}
batchEvents[idx] = newBatchEvent
}
return json.Marshal(&batchPayload{batchEvents})
}
func encodeEventData(data interface{}) ([]byte, error) {
var dataBytes []byte
var err error
switch d := data.(type) {
case []byte:
dataBytes = d
case string:
dataBytes = []byte(d)
default:
dataBytes, err = json.Marshal(data)
if err != nil {
return nil, err
}
}
return dataBytes, nil
}