snapshot of https://github.com/ueberdosis/hocuspocus.git @ cf3cfa355b4d3bb61c6b6c80071f71783e32470e (2026-09-15)
Some checks failed
build / lint (22) (push) Failing after 17s
build / test (22) (push) Failing after 8s
build / lint (23) (push) Failing after 2s
build / build (22) (push) Has been skipped
build / build (23) (push) Has been skipped
build / test (23) (push) Failing after 4s
docsearch / docsearch (push) Failing after 1s
Some checks failed
build / lint (22) (push) Failing after 17s
build / test (22) (push) Failing after 8s
build / lint (23) (push) Failing after 2s
build / build (22) (push) Has been skipped
build / build (23) (push) Has been skipped
build / test (23) (push) Failing after 4s
docsearch / docsearch (push) Failing after 1s
This commit is contained in:
59
playground/backend/src/bun.ts
Normal file
59
playground/backend/src/bun.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { Logger } from "@hocuspocus/extension-logger";
|
||||
import { Hocuspocus } from "@hocuspocus/server";
|
||||
import crossws from "crossws/adapters/bun";
|
||||
|
||||
const hocuspocus = new Hocuspocus({
|
||||
extensions: [new Logger()],
|
||||
});
|
||||
|
||||
const ws = crossws({
|
||||
hooks: {
|
||||
open(peer) {
|
||||
// Use peer methods instead of peer.websocket to avoid
|
||||
// Bun's Proxy `this` binding issue with ServerWebSocket
|
||||
const wsLike = {
|
||||
get readyState() {
|
||||
return peer.websocket.readyState ?? 3; // 3 = CLOSED
|
||||
},
|
||||
send(data: any) {
|
||||
peer.send(data);
|
||||
},
|
||||
close(code?: number, reason?: string) {
|
||||
peer.close(code, reason);
|
||||
},
|
||||
};
|
||||
const clientConnection = hocuspocus.handleConnection(
|
||||
wsLike,
|
||||
peer.request as Request,
|
||||
);
|
||||
(peer as any)._hocuspocus = clientConnection;
|
||||
},
|
||||
message(peer, message) {
|
||||
(peer as any)._hocuspocus?.handleMessage(message.uint8Array());
|
||||
},
|
||||
close(peer, event) {
|
||||
(peer as any)._hocuspocus?.handleClose({
|
||||
code: event.code,
|
||||
reason: event.reason,
|
||||
});
|
||||
},
|
||||
error(peer, error) {
|
||||
console.error("WebSocket error for peer:", peer.id);
|
||||
console.error(error);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Bun.serve({
|
||||
port: 8000,
|
||||
websocket: ws.websocket,
|
||||
fetch(request, server) {
|
||||
if (request.headers.get("upgrade") === "websocket") {
|
||||
return ws.handleUpgrade(request, server);
|
||||
}
|
||||
|
||||
return new Response("Welcome to Hocuspocus!");
|
||||
},
|
||||
});
|
||||
|
||||
console.log("Listening on ws://127.0.0.1:8000");
|
||||
Reference in New Issue
Block a user