-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
227 lines (201 loc) · 6.65 KB
/
index.ts
File metadata and controls
227 lines (201 loc) · 6.65 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
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
import fs from 'node:fs'
import url from 'node:url'
import Fastify, { type FastifyInstance, type FastifyRequest } from 'fastify'
import staticServer from '@fastify/static'
import websocket from '@fastify/websocket'
import getPort from 'get-port'
import logger from '@wdio/logger'
import { WebSocket } from 'ws'
import { getDevtoolsApp } from './utils.js'
import { DEFAULT_PORT } from './constants.js'
import { testRunner } from './runner.js'
import type { RunnerRequestBody } from './types.js'
let server: FastifyInstance | undefined
interface DevtoolsBackendOptions {
port?: number
hostname?: string
}
const log = logger('@wdio/devtools-backend')
const clients = new Set<WebSocket>()
/**
* Registry mapping sessionId → absolute path of the encoded .webm file.
* Populated when the service sends { scope: 'screencast', data: { sessionId, videoPath } }.
* Queried by GET /api/video/:sessionId.
*/
const videoRegistry = new Map<string, string>()
export function broadcastToClients(message: string) {
clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(message)
}
})
}
export async function start(
opts: DevtoolsBackendOptions = {}
): Promise<{ server: FastifyInstance; port: number }> {
const host = opts.hostname || 'localhost'
// Use getPort to find an available port, starting with the preferred port
const preferredPort = opts.port || DEFAULT_PORT
const port = await getPort({ port: preferredPort })
// Log if we had to use a different port
if (opts.port && port !== opts.port) {
log.warn(`Port ${opts.port} is already in use, using port ${port} instead`)
}
const appPath = await getDevtoolsApp()
server = Fastify({ logger: true })
await server.register(websocket)
await server.register(staticServer, {
root: appPath
})
server.post(
'/api/tests/run',
async (request: FastifyRequest<{ Body: RunnerRequestBody }>, reply) => {
const body = request.body
if (!body?.uid || !body.entryType) {
return reply.code(400).send({ error: 'Invalid run payload' })
}
try {
await testRunner.run({
...body,
devtoolsHost: host,
devtoolsPort: port
})
return reply.send({ ok: true })
} catch (error) {
log.error(`Failed to start test run: ${(error as Error).message}`)
return reply.code(500).send({ error: (error as Error).message })
}
}
)
server.post('/api/tests/stop', async (_request, reply) => {
testRunner.stop()
broadcastToClients(
JSON.stringify({
scope: 'testStopped',
data: { stopped: true, timestamp: Date.now() }
})
)
reply.send({ ok: true })
})
server.get(
'/client',
{ websocket: true },
(socket: WebSocket, _req: FastifyRequest) => {
log.info('client connected')
clients.add(socket)
socket.on('close', () => clients.delete(socket))
}
)
server.get(
'/worker',
{ websocket: true },
(socket: WebSocket, _req: FastifyRequest) => {
socket.on('message', (message: Buffer) => {
log.info(
`received ${message.length} byte message from worker to ${clients.size} client${clients.size > 1 ? 's' : ''}`
)
// Parse message to check if it needs special handling
try {
const parsed = JSON.parse(message.toString())
// Transform clearCommands → clearExecutionData for the UI
if (parsed.scope === 'clearCommands') {
const testUid = parsed.data?.testUid
log.info(`Clearing commands for test: ${testUid || 'all'}`)
const clearMessage = JSON.stringify({
scope: 'clearExecutionData',
data: { uid: testUid }
})
clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(clearMessage)
}
})
return
}
// Intercept screencast messages: store the absolute videoPath in the
// registry (backend-only), then forward only the sessionId to the UI
// so the UI can request the video via GET /api/video/:sessionId.
if (parsed.scope === 'screencast' && parsed.data?.sessionId) {
const { sessionId, videoPath } = parsed.data
if (videoPath) {
videoRegistry.set(sessionId, videoPath)
log.info(
`Screencast registered for session ${sessionId}: ${videoPath}`
)
}
// Forward trimmed message (no videoPath) to UI clients
const uiMessage = JSON.stringify({
scope: 'screencast',
data: { sessionId }
})
clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(uiMessage)
}
})
return
}
} catch {
// Not JSON or parsing failed, forward as-is
}
// Forward all other messages as-is
clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(message.toString())
}
})
})
}
)
// Serve recorded screencast videos. The service sends an absolute videoPath
// which is stored in videoRegistry; the UI only knows the sessionId and
// requests the file through this endpoint.
server.get(
'/api/video/:sessionId',
async (
request: FastifyRequest<{ Params: { sessionId: string } }>,
reply
) => {
const { sessionId } = request.params
const videoPath = videoRegistry.get(sessionId)
if (!videoPath) {
return reply.code(404).send({ error: 'Video not found' })
}
if (!fs.existsSync(videoPath)) {
return reply.code(404).send({ error: 'Video file missing from disk' })
}
return reply
.header('Content-Type', 'video/webm')
.send(fs.createReadStream(videoPath))
}
)
log.info(`Starting WebdriverIO Devtools application on port ${port}`)
await server.listen({ port, host })
return { server, port }
}
export async function stop() {
if (!server) {
return
}
log.info('Shutting down WebdriverIO Devtools application')
// Close all WebSocket connections first
clients.forEach((client) => {
if (
client.readyState === WebSocket.OPEN ||
client.readyState === WebSocket.CONNECTING
) {
client.terminate()
}
})
clients.clear()
await server.close()
}
/**
* start server if this file is called directly
*/
if (import.meta.url.startsWith('file:')) {
const modulePath = url.fileURLToPath(import.meta.url)
if (process.argv[1] === modulePath) {
start()
}
}