Migrate to Bun websockets

This commit is contained in:
tedspare
2024-03-19 22:05:08 -04:00
parent 675b527833
commit 18041ae3fb
13 changed files with 106 additions and 1336 deletions
+41
View File
@@ -0,0 +1,41 @@
import { parseArgs } from "util";
const serverUrl = `ws://localhost:1234?new`;
async function main({ url }: { url: string }) {
const socket = new WebSocket(serverUrl);
socket.addEventListener("message", (event) => {
const data = JSON.parse(event.data);
console.log("message:", data);
if (data.method) {
fetch(`${url}${data.path}`, {
method: data.method,
headers: data.headers,
})
.then((res) => res.text())
.then(socket.send);
}
});
socket.addEventListener("open", (event) => {
console.log("socket ready:", !!(event.target as any).readyState);
socket.ping();
});
}
const { values } = parseArgs({
args: Bun.argv,
options: {
port: {
type: "string",
required: true,
short: "p",
},
},
allowPositionals: true,
});
if (!values.port) throw "pass --port 3000";
main({ url: `localhost:${values.port}` });