Skip to content

Commit 0ddf224

Browse files
committed
wip
1 parent 8add658 commit 0ddf224

File tree

2 files changed

+20
-55
lines changed

2 files changed

+20
-55
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"http-server": "^14.1.1",
3535
"prettier": "^2.8.3",
3636
"selenium-webdriver": "^4.8.0",
37-
"es-main":"^1.3.0"
37+
"es-main":"^1.3.0",
38+
"local-web-server": "^5.4.0"
3839
}
3940
}

tests/server.mjs

Lines changed: 18 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,29 @@
1-
// Simple server adapted from https://developer.mozilla.org/en-US/docs/Learn/Server-side/Node_server_without_framework:
2-
import * as fs from "fs";
3-
import * as http from "http";
1+
// Simple local server
42
import * as path from "path";
53
import commandLineArgs from "command-line-args";
6-
import esMain from 'es-main';
4+
import esMain from "es-main";
5+
import LocalWebServer from "local-web-server";
76

8-
const MIME_TYPES = {
9-
default: "application/octet-stream",
10-
html: "text/html; charset=UTF-8",
11-
js: "application/javascript; charset=UTF-8",
12-
mjs: "application/javascript; charset=UTF-8",
13-
css: "text/css",
14-
png: "image/png",
15-
jpg: "image/jpg",
16-
gif: "image/gif",
17-
ico: "image/x-icon",
18-
svg: "image/svg+xml",
19-
};
7+
const ROOT_DIR = path.join(process.cwd(), "./");
208

21-
const STATIC_PATH = path.join(process.cwd(), "./");
22-
const toBool = [() => true, () => false];
23-
24-
export default function serve(port) {
9+
export default async function serve(port) {
2510
if (!port)
2611
throw new Error("Port is required");
27-
28-
const prepareFile = async (url) => {
29-
const paths = [STATIC_PATH, url.pathname];
30-
if (url.pathname.endsWith("/"))
31-
paths.push("index.html");
32-
const filePath = path.join(...paths);
33-
const pathTraversal = !filePath.startsWith(STATIC_PATH);
34-
const exists = await fs.promises.access(filePath).then(...toBool);
35-
const found = !pathTraversal && exists;
36-
const streamPath = found ? filePath : `${STATIC_PATH}/index.html`;
37-
const ext = path.extname(streamPath).substring(1).toLowerCase();
38-
const stream = fs.createReadStream(streamPath);
39-
return { found, ext, stream };
12+
const ws = await LocalWebServer.create({
13+
port: port,
14+
directory: ROOT_DIR,
15+
corsOpenerPolicy: "same-origin",
16+
corsEmbedderPolicy: "require-corp",
17+
});
18+
console.log(`Server started on http://localhost:${port}`);
19+
process.on("exit", () => ws.server.close());
20+
return {
21+
close() {
22+
ws.server.close();
23+
}
4024
};
41-
42-
const server = http
43-
.createServer(async (req, res) => {
44-
const url = new URL(`http://localhost${req.url}`);
45-
const file = await prepareFile(url);
46-
const statusCode = file.found ? 200 : 404;
47-
const mimeType = MIME_TYPES[file.ext] || MIME_TYPES.default;
48-
res.writeHead(statusCode, {
49-
"Content-Type": mimeType,
50-
"Cross-Origin-Embedder-Policy": "require-corp",
51-
"Cross-Origin-Opener-Policy": "same-origin",
52-
});
53-
file.stream.pipe(res);
54-
})
55-
.listen(port);
56-
57-
console.log(`Server running at http://127.0.0.1:${port}/`);
58-
return server;
5925
}
6026

61-
6227
function main() {
6328
const optionDefinitions = [
6429
{ name: "port", type: Number, defaultValue: 8010, description: "Set the test-server port, The default value is 8010." },
@@ -67,6 +32,5 @@ function main() {
6732
serve(options.port);
6833
}
6934

70-
if (esMain(import.meta)) {
35+
if (esMain(import.meta))
7136
main();
72-
}

0 commit comments

Comments
 (0)