-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathprotocol.go
More file actions
81 lines (67 loc) · 1.62 KB
/
Copy pathprotocol.go
File metadata and controls
81 lines (67 loc) · 1.62 KB
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
package main
import (
"encoding/json"
"fmt"
)
type protocolMessage struct {
/**
The raw message as string.
*/
raw string
ID uint64 `json:"id"`
Result json.RawMessage `json:"result"`
Error struct {
Code int64 `json:"code"`
Message string `json:"message"`
Data string `json:"data"`
} `json:"error"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
SessionId string `json:"sessionId"`
}
func (p *protocolMessage) String() string {
return fmt.Sprintf(
"protocolMessage{id=%d, method=%s, sessionId=%s, result=%s, error=%+v, params=%s}",
p.ID,
p.Method,
p.SessionId,
p.Result,
p.Error,
p.Params,
)
}
func (p *protocolMessage) IsError() bool {
return p.Error.Code != 0
}
func (p *protocolMessage) IsResponse() bool {
return p.ID > 0 && (len(p.Result) > 0 || p.IsError())
}
func (p *protocolMessage) IsRequest() bool {
return p.Method != "" && p.ID > 0
}
func (p *protocolMessage) IsEvent() bool {
return !(p.IsRequest() || p.IsResponse())
}
func (p *protocolMessage) FromTargetDomain() bool {
return p.Method == "Target.sendMessageToTarget" || p.Method == "Target.receivedMessageFromTarget"
}
func (p *protocolMessage) HasSessionId() bool {
return p.FromTargetDomain() || p.IsFlatten()
}
func (p *protocolMessage) IsFlatten() bool {
return p.SessionId != ""
}
func (p *protocolMessage) TargetID() string {
if p.SessionId != "" {
return p.SessionId
}
if p.FromTargetDomain() {
var params struct {
SessionID string `json:"sessionId"`
}
if json.Unmarshal(p.Params, ¶ms) == nil {
return params.SessionID
}
}
return ""
}