Skip to content

Commit d4565da

Browse files
committed
fixed bug where server did not upgrade websocket request
1 parent 9057571 commit d4565da

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

bin/hyper-server

-8 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hyper-hyper/hyper-server",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"description": "Simple web server to serve static content built powered by Bun.",
55
"author": "Mikita Stankiewicz <[email protected]>",
66
"private": "true",

src/server.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,35 @@ export const watcher = opts.hot && watch(
4747
export const server = Bun.serve({
4848
fetch: async (request, server) => {
4949
const requestPath = new URL(request.url).pathname;
50-
console.log(`${request.method} ${requestPath}`);
50+
console.log(`\n${request.method} ${requestPath}`);
5151
if(opts.hot && request.url.endsWith(HOT_CMD)) {
52-
console.log(`Server: Got '${HOT_CMD}' request, trying to upgrade...`)
52+
console.log(`Server: Got '${HOT_CMD}' request, trying to upgrade...`);
53+
return server.upgrade(request);
5354
}
54-
else {
55-
let filePath = join(ROOT, requestPath);
56-
if(request.url.endsWith("/")) {
57-
filePath = `${filePath}index.html`;
58-
}
59-
const file = Bun.file(filePath);
60-
if(opts.hot && file.type.startsWith("text/html")) {
61-
return new Response(
62-
new HTMLRewriter().on("head", {
63-
element(head) {
64-
head.append(
65-
`<script>
66-
const socket = new WebSocket("ws://${HOSTNAME}:${PORT}${HOT_CMD}");
67-
socket.addEventListener("message", event => window.location.reload());
68-
</script>`,
69-
{ html: true }
70-
);
71-
}
72-
}).transform(await file.text()),
73-
{
74-
headers: { "Content-Type": file.type }
55+
let filePath = join(ROOT, requestPath);
56+
if(request.url.endsWith("/")) {
57+
filePath = `${filePath}index.html`;
58+
}
59+
const file = Bun.file(filePath);
60+
if(opts.hot && file.type.startsWith("text/html")) {
61+
return new Response(
62+
new HTMLRewriter().on("head", {
63+
element(head) {
64+
head.append(
65+
`<script>
66+
const socket = new WebSocket("ws://${HOSTNAME}:${PORT}${HOT_CMD}");
67+
socket.addEventListener("message", event => window.location.reload());
68+
</script>`,
69+
{ html: true }
70+
);
7571
}
76-
);
77-
}
78-
return new Response(file, {headers:{"Content-Type": file.type}});
72+
}).transform(await file.text()),
73+
{
74+
headers: { "Content-Type": file.type }
75+
}
76+
);
7977
}
78+
return new Response(file, {headers:{"Content-Type": file.type}});
8079
},
8180
error: (error) => {
8281
if("ENOENT" === error.code) {

0 commit comments

Comments
 (0)