-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathruntime.go
255 lines (228 loc) · 6.85 KB
/
runtime.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package control
import (
"encoding/json"
"errors"
"fmt"
"github.com/ecwid/control/protocol/dom"
"github.com/ecwid/control/protocol/runtime"
)
var ErrExecutionContextDestroyed = errors.New("execution context destroyed")
type DOMException struct {
ExceptionDetails *runtime.ExceptionDetails
}
func (e DOMException) Error() string {
if e.ExceptionDetails.Exception.Description != "" {
return e.ExceptionDetails.Exception.Description
}
b, _ := json.Marshal(e.ExceptionDetails)
return string(b)
}
type nodeType float64
const (
nodeTypeElement nodeType = 1 // An Element node like <p> or <div>
nodeTypeAttribute nodeType = 2 // An Attribute of an Element
nodeTypeText nodeType = 3 // The actual Text inside an Element or Attr
nodeTypeCDataSection nodeType = 4 // A CDATASection
nodeTypeProcessingInstruction nodeType = 7 // A ProcessingInstruction of an XML document
nodeTypeComment nodeType = 8 // A Comment node
nodeTypeDocument nodeType = 9 // A Document node
nodeTypeDocumentType nodeType = 10 // A DocumentType node
nodeTypeFragment nodeType = 11 // A DocumentFragment node
)
type RemoteObject interface {
GetRemoteObjectID() runtime.RemoteObjectId
}
type remoteObjectValue runtime.RemoteObjectId
func (r remoteObjectValue) GetRemoteObjectID() runtime.RemoteObjectId {
return runtime.RemoteObjectId(r)
}
func getNodeType(deepSerializedValue any) nodeType {
return nodeType(deepSerializedValue.(map[string]any)["nodeType"].(float64))
}
func deepUnserialize(self string, value any) any {
switch self {
case "boolean", "string", "number":
return value
case "undefined", "null":
return nil
case "array":
if value == nil {
return value
}
arr := []any{}
for _, e := range value.([]any) {
pair := e.(map[string]any)
arr = append(arr, deepUnserialize(pair["type"].(string), pair["value"]))
}
return arr
case "object":
if value == nil {
return value
}
obj := map[string]any{}
for _, e := range value.([]any) {
var (
val = e.([]any)
pair = val[1].(map[string]any)
)
obj[val[0].(string)] = deepUnserialize(pair["type"].(string), pair["value"])
}
return obj
default:
return value
}
}
// implemented
// + undefined, null, string, number, boolean, promise, node, array, object, bigint, function, window
// unimplemented
// - regexp, date, symbol, map, set, weakmap, weakset, error, proxy, typedarray, arraybuffer
func (f *Frame) unserialize(value *runtime.RemoteObject) (any, error) {
if value == nil {
return nil, errors.New("can't unserialize nil RemoteObject")
}
if value.DeepSerializedValue == nil {
return value.Value, nil
}
switch value.DeepSerializedValue.Type {
case "promise", "function", "weakmap":
return remoteObjectValue(value.ObjectId), nil
case "node":
switch getNodeType(value.DeepSerializedValue.Value) {
case nodeTypeElement, nodeTypeDocument:
return &Node{
object: remoteObjectValue(value.ObjectId),
frame: f,
}, nil
default:
return nil, errors.New("unsupported type of node")
}
case "nodelist":
if value.Description == "NodeList(0)" {
return nil, nil
}
return f.requestNodeList(value.ObjectId)
default:
return deepUnserialize(value.DeepSerializedValue.Type, value.DeepSerializedValue.Value), nil
}
}
func (f *Frame) requestNodeList(objectId runtime.RemoteObjectId) (NodeList, error) {
descriptor, err := f.getProperties(remoteObjectValue(objectId), true, false, false, false)
if err != nil {
return nil, err
}
var i = 0
var nodeList = make(NodeList, 0)
for _, d := range descriptor.Result {
if d.Enumerable {
i++
n := &Node{
object: remoteObjectValue(d.Value.ObjectId),
requestedSelector: d.Value.Description + fmt.Sprintf("(%d)", i),
frame: f,
}
nodeList = append(nodeList, n)
}
}
return nodeList, nil
}
func (f Frame) toCallArgument(args ...any) (arguments []*runtime.CallArgument) {
for _, arg := range args {
callArg := runtime.CallArgument{}
switch a := arg.(type) {
case RemoteObject:
callArg.ObjectId = a.GetRemoteObjectID()
default:
callArg.Value = a
}
arguments = append(arguments, &callArg)
}
return
}
func (f Frame) evaluate(expression string, awaitPromise bool) (any, error) {
var uid = f.executionContextID()
if uid == "" {
return nil, ErrExecutionContextDestroyed
}
value, err := runtime.Evaluate(f, runtime.EvaluateArgs{
Expression: expression,
IncludeCommandLineAPI: true,
UniqueContextId: uid,
AwaitPromise: awaitPromise,
Timeout: runtime.TimeDelta(f.session.timeout.Milliseconds()),
SerializationOptions: &runtime.SerializationOptions{
Serialization: "deep",
},
})
if err != nil {
return nil, err
}
if err = toDOMException(value.ExceptionDetails); err != nil {
return nil, err
}
return f.unserialize(value.Result)
}
func (f Frame) AwaitPromise(promise RemoteObject) (any, error) {
value, err := runtime.AwaitPromise(f, runtime.AwaitPromiseArgs{
PromiseObjectId: promise.GetRemoteObjectID(),
ReturnByValue: true,
GeneratePreview: false,
})
if err != nil {
return nil, err
}
if err = toDOMException(value.ExceptionDetails); err != nil {
return nil, err
}
return f.unserialize(value.Result)
}
func (f Frame) CallFunctionOn(self RemoteObject, function string, awaitPromise bool, args ...any) (any, error) {
value, err := runtime.CallFunctionOn(f, runtime.CallFunctionOnArgs{
FunctionDeclaration: function,
ObjectId: self.GetRemoteObjectID(),
AwaitPromise: awaitPromise,
Arguments: f.toCallArgument(args...),
SerializationOptions: &runtime.SerializationOptions{
Serialization: "deep",
},
})
if err != nil {
return nil, err
}
if err = toDOMException(value.ExceptionDetails); err != nil {
return nil, err
}
return f.unserialize(value.Result)
}
func (f Frame) getProperties(self RemoteObject, ownProperties, accessorPropertiesOnly, generatePreview, nonIndexedPropertiesOnly bool) (*runtime.GetPropertiesVal, error) {
value, err := runtime.GetProperties(f, runtime.GetPropertiesArgs{
ObjectId: self.GetRemoteObjectID(),
OwnProperties: ownProperties,
AccessorPropertiesOnly: accessorPropertiesOnly,
GeneratePreview: generatePreview,
NonIndexedPropertiesOnly: nonIndexedPropertiesOnly,
})
if err != nil {
return nil, err
}
if err = toDOMException(value.ExceptionDetails); err != nil {
return nil, err
}
return value, nil
}
func (f Frame) describeNode(self RemoteObject) (*dom.Node, error) {
value, err := dom.DescribeNode(f, dom.DescribeNodeArgs{
ObjectId: self.GetRemoteObjectID(),
})
if err != nil {
return nil, err
}
return value.Node, nil
}
func toDOMException(value *runtime.ExceptionDetails) error {
if value != nil && value.Exception != nil {
return DOMException{
ExceptionDetails: value,
}
}
return nil
}