Skip to content

Commit

Permalink
simpler and more robust startup
Browse files Browse the repository at this point in the history
  • Loading branch information
camillobruni committed Jan 28, 2025
1 parent 4f874f4 commit d8167c7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
"prettier": "^2.8.3",
"selenium-webdriver": "^4.8.0",
"es-main":"^1.3.0",
"local-web-server": "^5.4.0"
"lws": "^4.2.0",
"lws-cors": "^4.2.1",
"lws-index": "^3.1.1",
"lws-log": "^3.0.0",
"lws-static": "^3.1.1"
}
}
34 changes: 27 additions & 7 deletions tests/server.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Simple local server
// Simple server for local testing.

import * as path from "path";
import commandLineArgs from "command-line-args";
import esMain from "es-main";
import LocalWebServer from "local-web-server";
import LocalWebServer from "lws";
import "lws-cors";
import "lws-index";
import "lws-log";
import "lws-static";

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

Expand All @@ -14,20 +19,35 @@ export default async function serve(port) {
directory: ROOT_DIR,
corsOpenerPolicy: "same-origin",
corsEmbedderPolicy: "require-corp",
logFormat: "dev",
stack: ["lws-log", "lws-cors", "lws-static", "lws-index"],
});
console.log(`Server started on http://localhost:${port}`);
await verifyStartup(ws, port);

process.on("exit", () => ws.server.close());

return {
close() {
ws.server.close();
}
},
};
}

async function verifyStartup(ws, port) {
await new Promise((resolve, reject) => {
ws.server.on("listening", () => {
console.log(`Server started on http://localhost:${port}`);
resolve();
});
ws.server.on("error", (e) => {
console.error("Error while starting the server", e);
reject(e);
});
});
}

function main() {
const optionDefinitions = [
{ name: "port", type: Number, defaultValue: 8010, description: "Set the test-server port, The default value is 8010." },
];
const optionDefinitions = [{ name: "port", type: Number, defaultValue: 8010, description: "Set the test-server port, The default value is 8010." }];
const options = commandLineArgs(optionDefinitions);
serve(options.port);
}
Expand Down

0 comments on commit d8167c7

Please sign in to comment.