forked from vitest-dev/vitest
/
pool.ts
155 lines (136 loc) · 4.14 KB
/
pool.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
import { MessageChannel } from 'worker_threads'
import { pathToFileURL } from 'url'
import { resolve } from 'pathe'
import type { Options as TinypoolOptions } from 'tinypool'
import { Tinypool } from 'tinypool'
import type { RawSourceMap } from 'source-map-js'
import { createBirpc } from 'birpc'
import { distDir } from '../constants'
import type {ResolvedConfig, WorkerContext, WorkerRPC} from '../types'
import { transformRequest } from './transform'
import type { Vitest } from './index'
export type RunWithFiles = (files: string[], invalidates?: string[]) => Promise<void>
export interface WorkerPool {
runTests: RunWithFiles
collectTests: RunWithFiles
close: () => Promise<void>
}
export function createPool(ctx: Vitest): WorkerPool {
if (ctx.config.threads)
return createWorkerPool(ctx)
else
return createFakePool(ctx)
}
const workerPath = pathToFileURL(resolve(distDir, './worker.js')).href
export function createFakePool(ctx: Vitest): WorkerPool {
const runWithFiles = (name: 'run' | 'collect'): RunWithFiles => {
return async(files, invalidates) => {
const worker = await import(workerPath)
const { workerPort, port } = createChannel(ctx)
const data: WorkerContext = {
port: workerPort,
config: ctx.config,
files,
invalidates,
}
await worker[name](data, { transferList: [workerPort] })
port.close()
workerPort.close()
}
}
return {
runTests: runWithFiles('run'),
collectTests: runWithFiles('collect'),
close: async() => {},
}
}
export function createWorkerPool(ctx: Vitest): WorkerPool {
const options: TinypoolOptions = {
filename: workerPath,
// Disable this for now, for WebContainer capability
// https://github.com/vitest-dev/vitest/issues/93
// In future we could conditionally enable it based on the env
useAtomics: false,
}
if (ctx.config.maxThreads != null)
options.maxThreads = ctx.config.maxThreads
if (ctx.config.minThreads != null)
options.minThreads = ctx.config.minThreads
if (ctx.config.isolate) {
options.isolateWorkers = true
options.concurrentTasksPerWorker = 1
}
const pool = new Tinypool(options)
const config: ResolvedConfig = {
...ctx.config,
// @ts-expect-error remove function to prevent DataCloneError
globalSetup: undefined,
}
const runWithFiles = (name: string): RunWithFiles => {
return async(files, invalidates) => {
await Promise.all(files.map(async(file) => {
const { workerPort, port } = createChannel(ctx)
const data: WorkerContext = {
port: workerPort,
config,
files: [file],
invalidates,
}
await pool.run(data, { transferList: [workerPort], name })
port.close()
workerPort.close()
}))
}
}
return {
runTests: runWithFiles('run'),
collectTests: runWithFiles('collect'),
close: async() => {}, // TODO: not sure why this will cause Node crash: pool.destroy(),
}
}
function createChannel(ctx: Vitest) {
const channel = new MessageChannel()
const port = channel.port2
const workerPort = channel.port1
createBirpc<WorkerRPC>({
functions: {
onWorkerExit(code) {
process.exit(code || 1)
},
snapshotSaved(snapshot) {
ctx.snapshot.add(snapshot)
},
async getSourceMap(id, force) {
if (force) {
const mod = ctx.server.moduleGraph.getModuleById(id)
if (mod)
ctx.server.moduleGraph.invalidateModule(mod)
}
const r = await transformRequest(ctx, id)
return r?.map as RawSourceMap | undefined
},
async fetch(id) {
const r = await transformRequest(ctx, id)
return r?.code
},
onCollected(files) {
ctx.state.collectFiles(files)
ctx.report('onCollected', files)
},
onTaskUpdate(packs) {
ctx.state.updateTasks(packs)
ctx.report('onTaskUpdate', packs)
},
onUserLog(msg) {
ctx.report('onUserConsoleLog', msg)
},
},
post(v) {
port.postMessage(v)
},
on(fn) {
port.on('message', fn)
},
})
return { workerPort, port }
}