-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
start-wpt-server.js
96 lines (86 loc) · 2.51 KB
/
start-wpt-server.js
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
"use strict";
/* eslint-disable no-console, global-require */
const path = require("path");
const dns = require("dns");
const childProcess = require("child_process");
const q = require("q");
const { inBrowserContext } = require("./util");
const requestHead = require("request-promise-native").head;
const dnsLookup = q.denodeify(dns.lookup);
const wptDir = path.resolve(__dirname, "../wpt");
const configPaths = {
default: path.resolve(__dirname, "wpt-config.json"),
};
const configs = {
default: require(configPaths.default),
};
module.exports = () => {
if (inBrowserContext()) {
return Promise.resolve();
}
const configType = "default";
const configPath = configPaths[configType];
const config = configs[configType];
return dnsLookup("web-platform.test")
.then(
() => {
const configArg = path.relative(path.resolve(wptDir), configPath);
const args = ["./wpt.py", "serve", "--config", configArg];
const python = childProcess.spawn("python", args, {
cwd: wptDir,
stdio: "inherit",
});
return new Promise((resolve, reject) => {
python.on("error", (e) => {
reject(
new Error("Error starting python server process:", e.message),
);
});
resolve(
Promise.all([
Promise.resolve(python),
pollForServer(
`http://${config.browser_host}:${config.ports.http[0]}/`,
),
pollForServer(
`https://${config.browser_host}:${config.ports.https[0]}/`,
),
pollForServer(
`http://${config.browser_host}:${config.ports.ws[0]}/`,
),
pollForServer(
`https://${config.browser_host}:${config.ports.wss[0]}/`,
),
]),
);
process.on("exit", () => {
// Python doesn't register a default handler for SIGTERM and it doesn't run __exit__() methods of context
// managers when it gets that signal. Using SIGINT avoids this problem.
python.kill("SIGINT");
});
});
},
() => {
throw new Error(
"Host entries not present for web platform tests. See " +
"https://github.com/w3c/web-platform-tests#running-the-tests",
);
},
)
.then(([server, firstURL]) => {
return { server, url: firstURL };
});
};
function pollForServer(url) {
return requestHead(url, { strictSSL: false })
.then(() => {
console.log(`WPT server at ${url} is up!`);
return url;
})
.catch((err) => {
console.log(
`WPT server at ${url} is not up yet (${err.message}); trying again`,
);
return q.delay(500).then(() => pollForServer(url));
});
}