/
setup.ts
147 lines (130 loc) · 3.99 KB
/
setup.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
import { promises as fs } from 'node:fs'
import type { BirpcReturn } from 'birpc'
import { createBirpc } from 'birpc'
import { parse, stringify } from 'flatted'
import type { WebSocket } from 'ws'
import { WebSocketServer } from 'ws'
import { API_PATH } from '../constants'
import type { Vitest } from '../node'
import type { File, ModuleGraphData, Reporter, TaskResultPack, UserConsoleLog } from '../types'
import { getModuleGraph } from '../utils'
import { parseStacktrace } from '../utils/source-map'
import type { TransformResultWithSource, WebSocketEvents, WebSocketHandlers } from './types'
export function setup(ctx: Vitest) {
const wss = new WebSocketServer({ noServer: true })
const clients = new Map<WebSocket, BirpcReturn<WebSocketEvents>>()
ctx.server.httpServer?.on('upgrade', (request, socket, head) => {
if (!request.url)
return
const { pathname } = new URL(request.url, 'http://localhost')
if (pathname !== API_PATH)
return
wss.handleUpgrade(request, socket, head, (ws) => {
wss.emit('connection', ws, request)
setupClient(ws)
})
})
function setupClient(ws: WebSocket) {
const rpc = createBirpc<WebSocketEvents, WebSocketHandlers>(
{
async onWatcherStart() {
await ctx.report('onWatcherStart')
},
async onFinished() {
await ctx.report('onFinished')
},
async onCollected(files) {
ctx.state.collectFiles(files)
await ctx.report('onCollected', files)
},
async onTaskUpdate(packs) {
ctx.state.updateTasks(packs)
await ctx.report('onTaskUpdate', packs)
},
getFiles() {
return ctx.state.getFiles()
},
async getPaths() {
return await ctx.state.getPaths()
},
readFile(id) {
return fs.readFile(id, 'utf-8')
},
writeFile(id, content) {
return fs.writeFile(id, content, 'utf-8')
},
async rerun(files) {
await ctx.rerunFiles(files)
},
getConfig() {
return ctx.config
},
async getTransformResult(id) {
const result: TransformResultWithSource | null | undefined = await ctx.vitenode.transformRequest(id)
if (result) {
try {
result.source = result.source || (await fs.readFile(id, 'utf-8'))
}
catch {}
return result
}
},
async getModuleGraph(id: string): Promise<ModuleGraphData> {
return getModuleGraph(ctx, id)
},
updateSnapshot(file?: File) {
if (!file)
return ctx.updateSnapshot()
return ctx.updateSnapshot([file.filepath])
},
},
{
post: msg => ws.send(msg),
on: fn => ws.on('message', fn),
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected'],
serialize: stringify,
deserialize: parse,
},
)
clients.set(ws, rpc)
ws.on('close', () => {
clients.delete(ws)
})
}
ctx.reporters.push(new WebSocketReporter(ctx, wss, clients))
}
class WebSocketReporter implements Reporter {
constructor(
public ctx: Vitest,
public wss: WebSocketServer,
public clients: Map<WebSocket, BirpcReturn<WebSocketEvents>>,
) {}
onCollected(files?: File[]) {
if (this.clients.size === 0)
return
this.clients.forEach((client) => {
client.onCollected?.(files)
})
}
async onTaskUpdate(packs: TaskResultPack[]) {
if (this.clients.size === 0)
return
packs.forEach(([, result]) => {
if (result?.error)
result.error.stacks = parseStacktrace(result.error)
})
this.clients.forEach((client) => {
client.onTaskUpdate?.(packs)
})
}
onFinished(files?: File[] | undefined) {
this.clients.forEach((client) => {
client.onFinished?.(files)
})
}
onUserConsoleLog(log: UserConsoleLog) {
this.clients.forEach((client) => {
client.onUserConsoleLog?.(log)
})
}
}