Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 8 additions & 17 deletions server/hocuspocus/bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions server/hocuspocus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
"@hocuspocus/extension-redis": "^4.0.0",
"@hocuspocus/server": "^4.0.0",
"aws-jwt-verify": "^5.1.1",
"ioredis": "^5.9.3",
"pg": "^8.19.0",
"ws": "^8.19.0",
"yjs": "^13.6.29"
},
"devDependencies": {
"@types/node": "^25.3.2",
"@types/pg": "^8.16.0",
"@types/ws": "^8.18.1",
"tsx": "^4.19.0",
"typescript": "^6.0.2",
"vitest": "^4.0.16"
Expand Down
42 changes: 17 additions & 25 deletions server/hocuspocus/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Hocuspocus } from "@hocuspocus/server";
import { createServer, IncomingMessage, ServerResponse } from "http";
import { WebSocketServer } from "ws";
import { Server as HocuspocusServer } from "@hocuspocus/server";
import { IncomingMessage, ServerResponse } from "http";
import { Redis } from "@hocuspocus/extension-redis";
import { Pool, PoolClient } from "pg";
import * as Y from "yjs";
Expand Down Expand Up @@ -285,7 +284,9 @@ if (REDIS_URL) {
}
}

const hocuspocus = new Hocuspocus({
const hocuspocusServer = new HocuspocusServer({
port: PORT,
stopOnSignals: false,
name: "zedi-hocuspocus",
extensions,

Expand Down Expand Up @@ -384,6 +385,7 @@ const hocuspocus = new Hocuspocus({
// ドキュメント変更時(デバウンス前)
},
});
const hocuspocus = hocuspocusServer.hocuspocus;

async function invalidateLiveDocument(documentName: string): Promise<boolean> {
if (!hocuspocus.documents.has(documentName)) {
Expand Down Expand Up @@ -422,8 +424,10 @@ async function handleHttpRequest(req: IncomingMessage, res: ServerResponse): Pro
await handleHttpRequestFallback(requestUrl, res);
}

// カスタムHTTPサーバー(ヘルスチェック用)
const httpServer = createServer((req: IncomingMessage, res: ServerResponse) => {
// Hocuspocus v4 owns WebSocket upgrades; keep our health/internal HTTP routes.
// WebSocket upgrade は Hocuspocus v4 に任せ、health/internal の HTTP ルートだけ差し替える。
hocuspocusServer.httpServer.removeAllListeners("request");
hocuspocusServer.httpServer.on("request", (req: IncomingMessage, res: ServerResponse) => {
void handleHttpRequest(req, res).catch((error) => {
console.error("[HTTP] Request handling failed:", error);
if (!res.headersSent) {
Expand Down Expand Up @@ -454,14 +458,6 @@ async function handleHttpRequestFallback(requestUrl: URL, res: ServerResponse):
res.end(JSON.stringify({ error: "Not Found" }));
}

// WebSocketサーバーをHTTPサーバーにアタッチ
const wss = new WebSocketServer({ server: httpServer });

// WebSocket接続をHocuspocusに渡す
wss.on("connection", (socket, request) => {
hocuspocus.handleConnection(socket, request);
});

if (NODE_ENV === "production" && !API_INTERNAL_URL) {
console.error(
"[Auth] CRITICAL: API_INTERNAL_URL is unset in production. Refusing to start. / " +
Expand All @@ -471,7 +467,7 @@ if (NODE_ENV === "production" && !API_INTERNAL_URL) {
}

// サーバー起動
httpServer.listen(PORT, () => {
void hocuspocusServer.listen(PORT, () => {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
console.log("========================================");
console.log(" Zedi Hocuspocus Server Started");
console.log("========================================");
Expand Down Expand Up @@ -499,24 +495,20 @@ httpServer.listen(PORT, () => {
// Graceful shutdown
process.on("SIGTERM", async () => {
console.log("[Shutdown] SIGTERM received, closing server...");
hocuspocus.closeConnections();
await hocuspocusServer.destroy();
if (pgPool) {
await pgPool.end();
}
httpServer.close(() => {
console.log("[Shutdown] Server closed");
process.exit(0);
});
console.log("[Shutdown] Server closed");
process.exit(0);
});

process.on("SIGINT", async () => {
console.log("[Shutdown] SIGINT received, closing server...");
hocuspocus.closeConnections();
await hocuspocusServer.destroy();
if (pgPool) {
await pgPool.end();
}
httpServer.close(() => {
console.log("[Shutdown] Server closed");
process.exit(0);
});
console.log("[Shutdown] Server closed");
process.exit(0);
});
Loading