-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
start-wpt-server.js
85 lines (73 loc) · 2.83 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
"use strict";
/* eslint-disable no-console, global-require */
const dns = require("dns");
const path = require("path");
const util = require("util");
const childProcess = require("child_process");
const requestHead = require("request-promise-native").head;
const { inBrowserContext } = require("../util.js");
const dnsLookup = util.promisify(dns.lookup);
const wptDir = path.resolve(__dirname, "tests");
const configPaths = {
default: path.resolve(__dirname, "wpt-config.json"),
toUpstream: path.resolve(__dirname, "tuwpt-config.json")
};
const configs = {
default: require(configPaths.default),
toUpstream: require(configPaths.toUpstream)
};
module.exports = ({ toUpstream = false } = {}) => {
if (inBrowserContext()) {
return Promise.resolve();
}
const configType = toUpstream ? "toUpstream" : "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 subprocess = childProcess.spawn("python", args, {
cwd: wptDir,
stdio: "inherit"
});
return new Promise((resolve, reject) => {
subprocess.on("error", e => {
reject(new Error("Error starting python server process:", e.message));
});
resolve(Promise.all([
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]}/`)
]).then(urls => ({ urls, subprocess })));
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.
subprocess.kill("SIGINT");
});
});
},
() => {
throw new Error("Host entries not present for web platform tests. See " +
"https://github.com/web-platform-tests/wpt#running-the-tests");
}
);
};
function pollForServer(url, lastLogTime = Date.now()) {
return requestHead(url, { strictSSL: false })
.then(() => {
console.log(`WPT server at ${url} is up!`);
return url;
})
.catch(err => {
// Only log every 5 seconds to be less spammy.
if (Date.now() - lastLogTime >= 5000) {
console.log(`WPT server at ${url} is not up yet (${err.message}); trying again`);
lastLogTime = Date.now();
}
return new Promise(resolve => {
setTimeout(() => resolve(pollForServer(url, lastLogTime)), 500);
});
});
}