61 lines
1.6 KiB
JavaScript
61 lines
1.6 KiB
JavaScript
/**
|
|
* Minimal static file server for e2e tests.
|
|
* Serves the test page and ribbit dist files.
|
|
*/
|
|
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const MIME = {
|
|
'.html': 'text/html',
|
|
'.js': 'application/javascript',
|
|
'.css': 'text/css',
|
|
'.map': 'application/json',
|
|
};
|
|
|
|
function createServer(port = 9999) {
|
|
const distDir = path.join(__dirname, '..', '..', 'dist', 'ribbit');
|
|
const testDir = __dirname;
|
|
|
|
const server = http.createServer((req, res) => {
|
|
let filePath;
|
|
if (req.url === '/' || req.url === '/index.html') {
|
|
filePath = path.join(testDir, 'index.html');
|
|
} else if (req.url.startsWith('/ribbit/')) {
|
|
filePath = path.join(distDir, req.url.replace('/ribbit/', ''));
|
|
} else {
|
|
res.writeHead(404);
|
|
res.end('Not found');
|
|
return;
|
|
}
|
|
|
|
const ext = path.extname(filePath);
|
|
const mime = MIME[ext] || 'application/octet-stream';
|
|
|
|
try {
|
|
const content = fs.readFileSync(filePath);
|
|
res.writeHead(200, { 'Content-Type': mime });
|
|
res.end(content);
|
|
} catch {
|
|
res.writeHead(404);
|
|
res.end('Not found');
|
|
}
|
|
});
|
|
|
|
return {
|
|
start() {
|
|
return new Promise((resolve) => {
|
|
server.listen(port, () => resolve());
|
|
});
|
|
},
|
|
stop() {
|
|
return new Promise((resolve) => {
|
|
server.close(() => resolve());
|
|
});
|
|
},
|
|
url: `http://localhost:${port}`,
|
|
};
|
|
}
|
|
|
|
module.exports = { createServer };
|