forked from wendigo/chrome-protocol-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
72 lines (52 loc) · 1.2 KB
/
utils.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
package main
import (
"encoding/json"
"fmt"
"strings"
)
const (
ellipsisLength = 80
)
func center(message string, length int) string {
padding := (length - len(message)) / 2
if padding < 0 {
return message
}
return strings.Repeat(" ", padding) + message + strings.Repeat(" ", length-len(message)-padding)
}
func asString(value interface{}) string {
if casted, ok := value.(string); ok {
return casted
}
return fmt.Sprintf("%+v", value)
}
func serialize(value interface{}) string {
buff, err := json.Marshal(value)
if err == nil {
if *flagEllipsis && len(buff) > ellipsisLength {
return string(buff[:ellipsisLength]) + "..."
}
serialized := string(buff)
if serialized == "null" {
return "{}"
}
return serialized
}
return err.Error()
}
func decodeMessage(bytes []byte) (*protocolMessage, error) {
var msg protocolMessage
if err := json.Unmarshal(bytes, &msg); err != nil {
return nil, err
}
return &msg, nil
}
func decodeProtocolMessage(message *protocolMessage) (*protocolMessage, error) {
if message.IsFlatten() {
return message, nil
}
if message.FromTargetDomain() {
return decodeMessage([]byte(asString(message.Params["message"])))
}
return message, nil
}