-
Notifications
You must be signed in to change notification settings - Fork 502
/
worker.mjs
114 lines (102 loc) · 2.35 KB
/
worker.mjs
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
import { join } from 'node:path'
import { runInThisContext } from 'node:vm'
import { parentPort, workerData } from 'node:worker_threads'
import { readFileSync } from 'node:fs'
import {
setGlobalOrigin,
Response,
Request,
fetch,
FormData,
File,
Headers
} from '../../../../index.js'
const { initScripts, meta, test, url, path } = workerData
const basePath = join(process.cwd(), 'test/wpt/tests')
const urlPath = path.slice(basePath.length)
const globalPropertyDescriptors = {
writable: true,
enumerable: false,
configurable: true
}
Object.defineProperties(globalThis, {
fetch: {
...globalPropertyDescriptors,
enumerable: true,
value: fetch
},
File: {
...globalPropertyDescriptors,
value: File
},
FormData: {
...globalPropertyDescriptors,
value: FormData
},
Headers: {
...globalPropertyDescriptors,
value: Headers
},
Request: {
...globalPropertyDescriptors,
value: Request
},
Response: {
...globalPropertyDescriptors,
value: Response
}
})
// self is required by testharness
// GLOBAL is required by self
runInThisContext(`
globalThis.self = globalThis
globalThis.GLOBAL = {
isWorker () {
return false
},
isShadowRealm () {
return false
},
isWindow () {
return false
}
}
globalThis.window = globalThis
globalThis.location = new URL('${url}')
globalThis.Window = Object.getPrototypeOf(globalThis).constructor
`)
const harness = readFileSync(join(basePath, '../runner/resources/testharness.cjs'), 'utf-8')
runInThisContext(harness)
// add_*_callback comes from testharness
// stolen from node's wpt test runner
// eslint-disable-next-line no-undef
add_result_callback((result) => {
parentPort.postMessage({
type: 'result',
result: {
status: result.status,
name: result.name,
message: result.message,
stack: result.stack
}
})
})
// eslint-disable-next-line no-undef
add_completion_callback((_, status) => {
parentPort.postMessage({
type: 'completion',
status
})
})
setGlobalOrigin(new URL(urlPath, url))
// Inject any script the user provided before
// running the tests.
for (const initScript of initScripts) {
runInThisContext(initScript)
}
// Inject any files from the META tags
for (const script of meta.scripts) {
runInThisContext(script)
}
// Finally, run the test.
runInThisContext(test)