-
Notifications
You must be signed in to change notification settings - Fork 502
/
server.mjs
148 lines (119 loc) · 4.19 KB
/
server.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
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
import { once } from 'node:events'
import { createServer } from 'node:http'
import { join } from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import { createReadStream } from 'node:fs'
import { setTimeout as sleep } from 'node:timers/promises'
const tests = fileURLToPath(join(import.meta.url, '../../tests'))
// https://web-platform-tests.org/tools/wptserve/docs/stash.html
class Stash extends Map {
take (key) {
if (this.has(key)) {
const value = this.get(key)
this.delete(key)
return value.value
}
}
put (key, value, path) {
this.set(key, { value, path })
}
}
const stash = new Stash()
const server = createServer(async (req, res) => {
const fullUrl = new URL(req.url, `http://localhost:${server.address().port}`)
switch (fullUrl.pathname) {
case '/interfaces/dom.idl':
case '/interfaces/html.idl':
case '/interfaces/fetch.idl':
case '/interfaces/referrer-policy.idl':
case '/xhr/resources/utf16-bom.json':
case '/fetch/data-urls/resources/base64.json':
case '/fetch/data-urls/resources/data-urls.json':
case '/fetch/api/resources/empty.txt':
case '/fetch/api/resources/data.json': {
// https://github.com/web-platform-tests/wpt/blob/6ae3f702a332e8399fab778c831db6b7dca3f1c6/fetch/api/resources/data.json
return createReadStream(join(tests, fullUrl.pathname))
.on('end', () => res.end())
.pipe(res)
}
case '/fetch/api/resources/trickle.py': {
// Note: python's time.sleep(...) takes seconds, while setTimeout
// takes ms.
const delay = parseFloat(fullUrl.searchParams.get('ms') ?? 500)
const count = parseInt(fullUrl.searchParams.get('count') ?? 50)
// eslint-disable-next-line no-unused-vars
for await (const chunk of req); // read request body
await sleep(delay)
if (!fullUrl.searchParams.has('notype')) {
res.setHeader('Content-type', 'text/plain')
}
res.statusCode = 200
await sleep(delay)
for (let i = 0; i < count; i++) {
res.write('TEST_TRICKLE\n')
await sleep(delay)
}
res.end()
break
}
case '/fetch/api/resources/infinite-slow-response.py': {
// https://github.com/web-platform-tests/wpt/blob/master/fetch/api/resources/infinite-slow-response.py
const stateKey = fullUrl.searchParams.get('stateKey') ?? ''
const abortKey = fullUrl.searchParams.get('abortKey') ?? ''
if (stateKey) {
stash.put(stateKey, 'open', fullUrl.pathname)
}
res.setHeader('Content-Type', 'text/plain')
res.statusCode = 200
res.write('.'.repeat(2048))
while (true) {
if (!res.write('.')) {
break
} else if (abortKey && stash.take(abortKey, fullUrl.pathname)) {
break
}
await sleep(10)
}
if (stateKey) {
stash.put(stateKey, 'closed', fullUrl.pathname)
}
return res.end()
}
case '/fetch/api/resources/stash-take.py': {
// https://github.com/web-platform-tests/wpt/blob/6ae3f702a332e8399fab778c831db6b7dca3f1c6/fetch/api/resources/stash-take.py
const key = fullUrl.searchParams.get('key')
res.setHeader('Access-Control-Allow-Origin', '*')
const took = stash.take(key, fullUrl.pathname) ?? null
res.write(JSON.stringify(took))
return res.end()
}
case '/fetch/api/resources/echo-content.py': {
res.setHeader('X-Request-Method', req.method)
res.setHeader('X-Request-Content-Length', req.headers['content-length'] ?? 'NO')
res.setHeader('X-Request-Content-Type', req.headers['content-type'] ?? 'NO')
res.setHeader('Content-Type', 'text/plain')
for await (const chunk of req) {
res.write(chunk)
}
res.end()
break
}
default: {
res.statusCode = 200
res.end('body')
}
}
}).listen(0)
await once(server, 'listening')
const send = (message) => {
if (typeof process.send === 'function') {
process.send(message)
}
}
send({ server: `http://localhost:${server.address().port}` })
process.on('message', (message) => {
if (message === 'shutdown') {
server.close((err) => err ? send(err) : send({ message: 'shutdown' }))
}
})