Files
cypress/packages/socket/lib/cdp-browser.ts
Jennifer Shehane 92e428a357 chore: add type linting + compilation checks to packages (#30776)
* chore: add type linting + compilation checks to runner package

* empty commit

* A bunch of tslint fixes

* wow it is building

* Fix issue with CT not mounting correctly with comments within it

* Fix net-stubbing.ct.ts failures

* Fix tslint: disable comment

* move target into compilerOptions

* fix tslint disable comment

* update proxy-logging to undo changes

* standardize the tslint:disable comments

* fix comment

* fix the banner content not displaying and write a test for this situation

* fix ct reference

* put target to es2020

* actually set the property with replaced title

* Update packages/reporter/src/hooks/hook-model.ts

Co-authored-by: Ryan Manuel <ryanm@cypress.io>

* Fix eslint/tslint settings for system-tests with vue 3

* bump CI cache

* update types/react resolution

* add return

* lint fix

* tslint disable for empty blocks

* exclude dist files from ts linting

* update to exclude all dist folder

* exclude dist file

* change to await

* fix line numbers of stack trace with linting updating vue file

---------

Co-authored-by: Ryan Manuel <ryanm@cypress.io>
2025-01-23 14:55:52 -05:00

76 lines
1.9 KiB
TypeScript

/// <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 = async (payload: string) => {
const parsed = JSON.parse(payload)
await decode(parsed).then(async (decoded: any) => {
const [event, callbackEvent, args] = decoded
super.emit(event, ...args)
await 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)
}
// @ts-expect-error TODO: fix emit type
emit = async (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)
}
await encode([event, uuid, args], this._namespace).then((encoded: any) => {
window[`cypressSendToServer-${this._namespace}`](JSON.stringify(encoded))
})
return this
}
}