-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions_session.go
More file actions
90 lines (77 loc) · 2.34 KB
/
Copy pathactions_session.go
File metadata and controls
90 lines (77 loc) · 2.34 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
82
83
84
85
86
87
88
89
90
package browser
// actions_session.go — executors for set_cookies, get_cookies, handle_dialog,
// destroy_session, get_logs, warmup actions.
const defaultWarmupMs = 3000
func init() {
registerAction("set_cookies", execSetCookies)
registerAction("get_cookies", execGetCookies)
registerAction("handle_dialog", execHandleDialog)
registerAction("destroy_session", execDestroySession)
registerAction("get_logs", execGetLogs)
registerAction("warmup", execWarmup)
}
func execSetCookies(dc dispatchContext, a Action) (any, error) {
return nil, doSetCookies(dc.page, a.Cookies)
}
func execGetCookies(dc dispatchContext, a Action) (any, error) {
cookies, err := doGetCookies(dc.page)
if err != nil {
return nil, err
}
if a.Limit > 0 && a.Limit < len(cookies) {
cookies = cookies[len(cookies)-a.Limit:]
}
return cookies, nil
}
func execHandleDialog(dc dispatchContext, a Action) (any, error) {
accept := true
if a.Accept != nil {
accept = *a.Accept
}
return doHandleDialog(dc.page, accept, a.Text)
}
// execDestroySession is a no-op: session lifecycle is managed by the HTTP handler.
func execDestroySession(_ dispatchContext, _ Action) (any, error) {
return nil, nil
}
func execGetLogs(dc dispatchContext, a Action) (any, error) {
netLimit := defaultNetworkLimit
conLimit := defaultConsoleLimit
if a.Limit > 0 {
netLimit = a.Limit
conLimit = a.Limit
}
if dc.logs == nil {
return map[string]any{"network": []NetworkEntry{}, "console": []ConsoleEntry{}}, nil
}
net, con := dc.logs.Collect()
net = lastN(net, netLimit)
con = lastN(con, conLimit)
// Return compact network entries: drop body_size, truncate URL.
type compactNetwork struct {
Method string `json:"method"`
URL string `json:"url"`
Status int `json:"status,omitempty"`
MimeType string `json:"mime_type,omitempty"`
Error string `json:"error,omitempty"`
}
compact := make([]compactNetwork, len(net))
for i, e := range net {
compact[i] = compactNetwork{
Method: e.Method,
URL: truncateURL(e.URL),
Status: e.Status,
MimeType: e.MimeType,
Error: e.Error,
}
}
return map[string]any{"network": compact, "console": con}, nil
}
func execWarmup(dc dispatchContext, a Action) (any, error) {
waitMs := a.WaitMs
if waitMs <= 0 {
waitMs = defaultWarmupMs
}
count, err := doWarmup(dc.ctx, dc.page, waitMs, dc.cursor)
return count, err
}