-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathautomation.ts
191 lines (160 loc) · 5.23 KB
/
automation.ts
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
import Bluebird from 'bluebird'
import { v4 as uuidv4 } from 'uuid'
import { Cookies } from './cookies'
import { Screenshot } from './screenshot'
import type { BrowserPreRequest } from '@packages/proxy'
import type { AutomationMiddleware, OnRequestEvent } from '@packages/types'
import { cookieJar } from '../util/cookies'
export type OnBrowserPreRequest = (browserPreRequest: BrowserPreRequest) => void
export class Automation {
private requests: Record<number, (any) => void>
private middleware: AutomationMiddleware
private cookies: Cookies
private screenshot: { capture: (data: any, automate: any) => any }
constructor (cyNamespace?: string, cookieNamespace?: string, screenshotsFolder?: string | false, public onBrowserPreRequest?: OnBrowserPreRequest, public onRequestEvent?: OnRequestEvent, public onRequestServedFromCache?: (requestId: string) => void, public onRequestFailed?: (requestId: string) => void, public onDownloadLinkClicked?: (downloadUrl: string) => void) {
this.requests = {}
// set the middleware
this.middleware = this.initializeMiddleware()
this.cookies = new Cookies(cyNamespace, cookieNamespace)
this.screenshot = Screenshot(screenshotsFolder)
}
initializeMiddleware = (): AutomationMiddleware => {
return {
onPush: this.middleware?.onPush || null,
onBeforeRequest: null,
onRequest: null,
onResponse: null,
onAfterResponse: null,
}
}
reset () {
this.middleware = this.initializeMiddleware()
}
automationValve (message: string, fn: (...args: any) => any) {
return (msg: string, data: any) => {
// enable us to omit message
// argument
if (!data) {
data = msg
msg = message
}
const onReq = this.get('onRequest')
// if we have an onRequest function
// then just invoke that
if (onReq) {
return Bluebird.resolve(onReq(msg, data))
}
// do the default
return Bluebird.resolve(this.requestAutomationResponse(msg, data, fn))
}
}
requestAutomationResponse (message: string, data: any, fn: (...args: any) => any) {
return new Bluebird((resolve, reject) => {
const id = uuidv4()
this.requests[id] = function (obj) {
// normalize the error from automation responses
const e = obj.__error
if (e) {
const err = new Error(e)
err.name = obj.__name
err.stack = obj.__stack
return reject(err)
}
// normalize the response
return resolve(obj.response)
}
// callback onAutomate with the right args
return fn(message, data, id)
})
}
invokeAsync (fn, ...args) {
return Bluebird
.try(() => {
fn = this.get(fn)
if (fn) {
return fn(...args)
}
})
}
normalize (message: string, data: any, automate?) {
return Bluebird.try(() => {
switch (message) {
case 'take:screenshot':
return this.screenshot.capture(data, automate)
case 'get:cookies':
return this.cookies.getCookies(data, automate)
case 'get:cookie':
return this.cookies.getCookie(data, automate)
case 'set:cookie':
return this.cookies.setCookie(data, automate)
case 'set:cookies':
return this.cookies.setCookies(data, automate)
case 'add:cookies':
return this.cookies.addCookies(data, automate)
case 'clear:cookies':
return Bluebird.all([
this.cookies.clearCookies(data, automate),
cookieJar.removeAllCookies(),
])
.spread((automationResult) => automationResult)
case 'clear:cookie':
return Bluebird.all([
this.cookies.clearCookie(data, automate),
cookieJar.removeCookie(data),
])
.spread((automationResult) => automationResult)
case 'change:cookie':
return this.cookies.changeCookie(data)
case 'create:download':
case 'canceled:download':
case 'complete:download':
return data
default:
return automate(data)
}
})
}
getRequests () {
return this.requests
}
getMiddleware () {
return this.middleware
}
use (middlewares: AutomationMiddleware) {
return this.middleware = {
...this.middleware,
...middlewares,
}
}
push (message: string, data: unknown) {
return this.normalize(message, data)
.then((data) => {
if (data) {
this.invokeAsync('onPush', message, data)
}
})
}
request (message, data, fn) {
// curry in the message + callback function
// for obtaining the external automation data
const automate = this.automationValve(message, fn)
// enable us to tap into before making the request
return this.invokeAsync('onBeforeRequest', message, data)
.then(() => {
return this.normalize(message, data, automate)
})
.tap((resp) => {
return this.invokeAsync('onAfterResponse', message, data, resp)
})
}
response = (id, resp) => {
const request = this.requests[id]
if (request) {
delete request[id]
return request(resp)
}
}
get = <K extends keyof AutomationMiddleware>(fn: K): AutomationMiddleware[K] => {
return this.middleware[fn]
}
}