Skip to content

Commit

Permalink
do NOT put h3 and vite in the same room
Browse files Browse the repository at this point in the history
aaaaaughhhhh whyy is this happening to meeeee
i just want vite middleware to play nice with h3 but its just so weird ughhh
  • Loading branch information
cysabi committed May 4, 2024
1 parent 282c8b8 commit 4e0e51f
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 99 deletions.
6 changes: 1 addition & 5 deletions consume/bento.box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ export default bento.box<State>({
},
updateScore(set, payload) {
set((state) => {
const w = state.scoreboard.shift();
console.log(state.scoreboard);
if (w) {
state.scoreboard.push(w);
}
state.scoreboard[0].score -= 1;
});
},
});
Binary file modified consume/bun.lockb
Binary file not shown.
6 changes: 4 additions & 2 deletions consume/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"type": "module",
"dependencies": { "bento": "../lib/" },
"dependencies": {
"bento": "file:../lib/"
},
"devDependencies": {
"@preact/preset-vite": "^2.8.2",
"@tailwindcss/vite": "^4.0.0-alpha.14",
"tailwindcss": "^4.0.0-alpha.14",
"vite": "^5.2.10"
"vite": "^5.2.11"
}
}
Binary file modified lib/bun.lockb
Binary file not shown.
11 changes: 4 additions & 7 deletions lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@
"./server": "./src/server/index.ts",
"./client": "./src/client/index.ts"
},
"bin": "./src/bin.ts",
"dependencies": {
"@types/bun": "latest",
"@types/icepick": "^2.3.3",
"citty": "^0.1.6",
"defu": "^6.1.4",
"h3": "^1.11.1",
"icepick": "^2.4.0",
"lmdb": "^3.0.8",
"msgpackr": "^1.10.1",
"vite": "^5.2.11"
},
"devDependencies": {
"@types/bun": "latest",
"@types/icepick": "^2.3.3"
"msgpackr": "^1.10.1"
},
"peerDependencies": {
"vite": "^5.2.11",
"typescript": "^5.0.0"
}
}
68 changes: 0 additions & 68 deletions lib/src/bin.ts

This file was deleted.

40 changes: 37 additions & 3 deletions lib/src/box.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import type { Actions, ServerConfig } from "./types";
import {
createApp,
defineWebSocketHandler,
fromNodeMiddleware,
toWebHandler,
} from "h3";
import wsAdapter from "crossws/adapters/bun";
import { createServer as createViteServer } from "vite";
import { Server as BentoServer } from "./server";

export type BentoBoxModel<S> = S | Actions<S>;

export const box = <S extends Record<string, unknown>>(
export const box = async <S extends Record<string, unknown>>(
model: BentoBoxModel<S>
): ServerConfig<S> => {
) => {
const config: ServerConfig<S> = {
state: {} as S,
actions: {},
Expand All @@ -19,5 +28,30 @@ export const box = <S extends Record<string, unknown>>(
}
});

return config;
const app = createApp();

// vite dev server
const vite = await createViteServer({
server: { middlewareMode: true },
build: { target: "chrome95" },
});
app.use(fromNodeMiddleware(vite.middlewares));

// websocket server
const server = new BentoServer(config);
app.use("/_ws", defineWebSocketHandler(server.wss));

// serve
const { handleUpgrade, websocket } = wsAdapter(app.websocket);
const handleHttp = toWebHandler(app);
return Bun.serve({
port: 4400,
websocket,
async fetch(req, server) {
if (await handleUpgrade(req, server)) {
return;
}
return handleHttp(req);
},
});
};
8 changes: 2 additions & 6 deletions lib/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ export class Client<S> {
this.#send({ type: "action", action, payload });
}

constructor({
scopes,
initial,
port = 4400,
}: { scopes?: string[][]; initial?: S; port?: number } = {}) {
constructor({ scopes, initial }: { scopes?: string[][]; initial?: S } = {}) {
this.#state = icepick.freeze(initial || ({} as S));

this.#ws = new WebSocket(`ws://localhost:${port}/_ws`);
this.#ws = new WebSocket(`ws://${window.location.hostname}:4400/_ws`);
this.#ws.binaryType = "arraybuffer";
this.#send({ type: "init", scopes });
this.#ws.addEventListener("message", (event) => {
Expand Down
8 changes: 0 additions & 8 deletions lib/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type {
Actions,
ServerConfig,
} from "../types";
import defu from "defu";
import { pack, unpack } from "msgpackr";
import { State } from "./state";
import { Persist } from "./persist";
Expand All @@ -34,13 +33,6 @@ export class Server<S extends Record<string, unknown>> {
this.#persist.clear();
this.#persist.init(this.#state.snap());

// apply defaults
this.#handleActionStream((draft: Record<string, unknown>) => {
Object.entries(defu(draft, config.state)).forEach(([key, value]) => {
draft[key] = value;
});
});

this.wss = {
message: (ws, msg) => {
const data: Message = unpack(msg.rawData);
Expand Down
1 change: 1 addition & 0 deletions lib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"allowJs": true,
"noEmit": true,

// Best practices
Expand Down

0 comments on commit 4e0e51f

Please sign in to comment.