Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: set proper content-type header to livereload script #64

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 18 additions & 40 deletions livereload_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,27 @@ function canCloseSocket(socket: WebSocket): boolean {
return socket.readyState === socket.OPEN;
}

async function serve(
httpConn: Deno.HttpConn,
function handleHttp(
request: Request,
eventtarget: EventTarget,
port: number,
): Promise<void> {
const requestEvent = await httpConn.nextRequest();
if (requestEvent == null) {
return;
}

const { request, respondWith } = requestEvent;
): Response {
const url = new URL(request.url);
switch (url.pathname) {
case "/livereload.js":
try {
await respondWith(
new Response(livereloadScript(port), {
headers: { connection: "close" },
}),
);
const nr = await httpConn.nextRequest();
if (nr !== null) {
logger.error("Error: something's wrong with livereload server.");
}
httpConn.close();
} catch (e) {
httpConn.close();
logger.error(e);
}
break;
return new Response(livereloadScript(port), {
headers: { "content-type": "text/javascript" },
});
case "/livereload": {
if (request.headers.get("upgrade") !== "websocket") {
return new Response(null, { status: 404 });
}
const { response, socket } = Deno.upgradeWebSocket(request);
handleWs(socket, eventtarget);
respondWith(response);
break;
return response;
}
default:
httpConn.close();
break;
return new Response(null, { status: 404 });
}
}

Expand All @@ -87,20 +69,16 @@ export function livereloadServer(
eventtarget: EventTarget,
): LivereloadServer {
logger.debug(`livereload websocket server is running on port=${port}`);
const listener = Deno.listen({ port });
const done = (async () => {
for await (const conn of listener) {
// TODO: migrate to Deno.serve
// deno-lint-ignore no-deprecated-deno-api
const httpConn = Deno.serveHttp(conn);
serve(httpConn, eventtarget, port).catch(logger.error);
}
})();

const server = Deno.serve({
port,
// suppress unnecessary log
onListen: () => {},
}, (request) => handleHttp(request, eventtarget, port));

return {
async close() {
listener.close();
await done;
await server.shutdown();
},
};
}
Loading