2022-10-12 21:25:51 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import http from 'http';
|
|
|
|
import https from 'https';
|
|
|
|
import send from 'send';
|
2023-02-03 00:10:16 +00:00
|
|
|
import enableDestroy from 'server-destroy';
|
2023-02-03 00:12:22 +00:00
|
|
|
import { fileURLToPath } from 'url';
|
2022-10-12 21:25:51 +00:00
|
|
|
|
|
|
|
interface CreateServerOptions {
|
|
|
|
client: URL;
|
|
|
|
port: number;
|
|
|
|
host: string | undefined;
|
2022-11-07 15:05:12 +00:00
|
|
|
removeBase: (pathname: string) => string;
|
2022-10-12 21:25:51 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 21:27:56 +00:00
|
|
|
export function createServer(
|
2022-11-07 15:05:12 +00:00
|
|
|
{ client, port, host, removeBase }: CreateServerOptions,
|
2022-10-12 21:27:56 +00:00
|
|
|
handler: http.RequestListener
|
|
|
|
) {
|
2022-10-12 21:25:51 +00:00
|
|
|
const listener: http.RequestListener = (req, res) => {
|
2022-10-12 21:27:56 +00:00
|
|
|
if (req.url) {
|
2023-01-11 09:52:51 +00:00
|
|
|
let pathname = removeBase(req.url);
|
|
|
|
pathname = pathname[0] === '/' ? pathname : '/' + pathname;
|
2023-02-03 00:10:16 +00:00
|
|
|
pathname = new URL(pathname, `http://${host}:${port}`).pathname;
|
2023-02-01 18:53:09 +00:00
|
|
|
const stream = send(req, encodeURI(decodeURI(pathname)), {
|
2022-10-27 09:17:38 +00:00
|
|
|
root: fileURLToPath(client),
|
2023-01-11 09:52:51 +00:00
|
|
|
dotfiles: pathname.startsWith('/.well-known/') ? 'allow' : 'deny',
|
2022-10-12 21:25:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let forwardError = false;
|
|
|
|
|
2022-10-12 21:27:56 +00:00
|
|
|
stream.on('error', (err) => {
|
|
|
|
if (forwardError) {
|
2022-10-12 21:25:51 +00:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(err.toString());
|
|
|
|
res.writeHead(500);
|
|
|
|
res.end('Internal server error');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// File not found, forward to the SSR handler
|
|
|
|
handler(req, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on('file', () => {
|
|
|
|
forwardError = true;
|
|
|
|
});
|
|
|
|
stream.pipe(res);
|
|
|
|
} else {
|
|
|
|
handler(req, res);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-12 21:27:56 +00:00
|
|
|
let httpServer:
|
|
|
|
| http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>
|
|
|
|
| https.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
|
|
|
|
|
|
if (process.env.SERVER_CERT_PATH && process.env.SERVER_KEY_PATH) {
|
|
|
|
httpServer = https.createServer(
|
|
|
|
{
|
|
|
|
key: fs.readFileSync(process.env.SERVER_KEY_PATH),
|
|
|
|
cert: fs.readFileSync(process.env.SERVER_CERT_PATH),
|
|
|
|
},
|
|
|
|
listener
|
|
|
|
);
|
2022-10-12 21:25:51 +00:00
|
|
|
} else {
|
|
|
|
httpServer = http.createServer(listener);
|
|
|
|
}
|
|
|
|
httpServer.listen(port, host);
|
2023-02-03 00:10:16 +00:00
|
|
|
enableDestroy(httpServer);
|
2022-10-12 21:25:51 +00:00
|
|
|
|
|
|
|
// Resolves once the server is closed
|
|
|
|
const closed = new Promise<void>((resolve, reject) => {
|
|
|
|
httpServer.addListener('close', resolve);
|
|
|
|
httpServer.addListener('error', reject);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
closed() {
|
|
|
|
return closed;
|
|
|
|
},
|
|
|
|
server: httpServer,
|
|
|
|
stop: async () => {
|
|
|
|
await new Promise((resolve, reject) => {
|
2023-02-03 00:10:16 +00:00
|
|
|
httpServer.destroy((err) => (err ? reject(err) : resolve(undefined)));
|
2022-10-12 21:25:51 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|