Skip to content

Commit d8167c7

Browse files
committed
simpler and more robust startup
1 parent 4f874f4 commit d8167c7

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
"prettier": "^2.8.3",
3636
"selenium-webdriver": "^4.8.0",
3737
"es-main":"^1.3.0",
38-
"local-web-server": "^5.4.0"
38+
"lws": "^4.2.0",
39+
"lws-cors": "^4.2.1",
40+
"lws-index": "^3.1.1",
41+
"lws-log": "^3.0.0",
42+
"lws-static": "^3.1.1"
3943
}
4044
}

tests/server.mjs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
// Simple local server
1+
// Simple server for local testing.
2+
23
import * as path from "path";
34
import commandLineArgs from "command-line-args";
45
import esMain from "es-main";
5-
import LocalWebServer from "local-web-server";
6+
import LocalWebServer from "lws";
7+
import "lws-cors";
8+
import "lws-index";
9+
import "lws-log";
10+
import "lws-static";
611

712
const ROOT_DIR = path.join(process.cwd(), "./");
813

@@ -14,20 +19,35 @@ export default async function serve(port) {
1419
directory: ROOT_DIR,
1520
corsOpenerPolicy: "same-origin",
1621
corsEmbedderPolicy: "require-corp",
22+
logFormat: "dev",
23+
stack: ["lws-log", "lws-cors", "lws-static", "lws-index"],
1724
});
18-
console.log(`Server started on http://localhost:${port}`);
25+
await verifyStartup(ws, port);
26+
1927
process.on("exit", () => ws.server.close());
28+
2029
return {
2130
close() {
2231
ws.server.close();
23-
}
32+
},
2433
};
2534
}
2635

36+
async function verifyStartup(ws, port) {
37+
await new Promise((resolve, reject) => {
38+
ws.server.on("listening", () => {
39+
console.log(`Server started on http://localhost:${port}`);
40+
resolve();
41+
});
42+
ws.server.on("error", (e) => {
43+
console.error("Error while starting the server", e);
44+
reject(e);
45+
});
46+
});
47+
}
48+
2749
function main() {
28-
const optionDefinitions = [
29-
{ name: "port", type: Number, defaultValue: 8010, description: "Set the test-server port, The default value is 8010." },
30-
];
50+
const optionDefinitions = [{ name: "port", type: Number, defaultValue: 8010, description: "Set the test-server port, The default value is 8010." }];
3151
const options = commandLineArgs(optionDefinitions);
3252
serve(options.port);
3353
}

0 commit comments

Comments
 (0)