/
cdp-browser.ts
74 lines (57 loc) · 1.84 KB
/
cdp-browser.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
/// <reference lib="dom" />
import Emitter from 'component-emitter'
import { v4 as uuidv4 } from 'uuid'
import { decode, encode } from './utils'
import type { SocketShape } from './types'
type CDPSocketNamespaceKey = `cypressSocket-${string}`
type CDPSendToServerNamespaceKey = `cypressSendToServer-${string}`
declare global {
interface Window {
[key: CDPSocketNamespaceKey]: { send?: (payload: string) => void }
[key: CDPSendToServerNamespaceKey]: (payload: string) => void
}
}
export class CDPBrowserSocket extends Emitter implements SocketShape {
private _namespace: string
constructor (namespace: string) {
super()
this._namespace = namespace
const send = (payload: string) => {
const parsed = JSON.parse(payload)
decode(parsed).then((decoded: any) => {
const [event, callbackEvent, args] = decoded
super.emit(event, ...args)
this.emit(callbackEvent)
})
}
let cypressSocket = window[`cypressSocket-${this._namespace}`]
if (!cypressSocket) {
cypressSocket = {}
window[`cypressSocket-${this._namespace}`] = cypressSocket
}
if (!cypressSocket.send) {
cypressSocket.send = send
}
}
connect () {
// Set timeout so that the connect event is emitted after the constructor returns and the user has a chance to attach a listener
setTimeout(() => {
super.emit('connect')
}, 0)
}
emit = (event: string, ...args: any[]) => {
// Generate a unique key for this event
const uuid = uuidv4()
let callback
if (typeof args[args.length - 1] === 'function') {
callback = args.pop()
}
if (callback) {
this.once(uuid, callback)
}
encode([event, uuid, args], this._namespace).then((encoded: any) => {
window[`cypressSendToServer-${this._namespace}`](JSON.stringify(encoded))
})
return this
}
}