-
Notifications
You must be signed in to change notification settings - Fork 1
feat: setup project to use hocuspocus v3 #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: rollback-updates
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { SQLite } from "@hocuspocus/extension-sqlite"; | ||
import { Document, Server } from "@hocuspocus/server"; | ||
import { type Document, Hocuspocus } from "@hocuspocus/server"; | ||
|
||
import { serve } from "@hono/node-server"; | ||
import { createNodeWebSocket } from "@hono/node-ws"; | ||
|
@@ -10,7 +10,8 @@ import { FAKE_authInfoFromToken } from "./auth.js"; | |
import { threadsRouter } from "./threads.js"; | ||
import { RejectUnauthorized } from "./rejectUnauthorized.js"; | ||
// Setup Hocuspocus server | ||
const hocuspocusServer = Server.configure({ | ||
|
||
const hocuspocus = new Hocuspocus({ | ||
async onAuthenticate(data) { | ||
const { token } = data; | ||
|
||
|
@@ -19,15 +20,19 @@ const hocuspocusServer = Server.configure({ | |
if (authInfo === "unauthorized") { | ||
throw new Error("Not authorized!"); | ||
} | ||
|
||
data.connection.readOnly = authInfo.role === "COMMENT-ONLY"; | ||
data.connectionConfig.readOnly = authInfo.role === "COMMENT-ONLY"; | ||
}, | ||
|
||
extensions: [ | ||
new SQLite({ | ||
database: "db.sqlite", | ||
database: ":memory:", | ||
}), | ||
// TODO we can actually just do the auth check in here, and not need the server to inject the mark or anything | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might actually really simplify things, if we choose to do the authorization at the hocuspocus layer instead, it would allow clients to safely make modifications to the threads on their side. To make it safe though, we need to inspect what exactly was updated (i.e. they can only modify their own comments). |
||
new RejectUnauthorized("threads", (payload) => { | ||
// eslint-disable-next-line no-console | ||
console.warn("rejecting update to document", payload.documentName); | ||
}), | ||
new RejectUnauthorized("threads"), | ||
], | ||
|
||
// TODO: for good security, you'd want to make sure that either: | ||
|
@@ -46,7 +51,7 @@ app.get( | |
"/hocuspocus", | ||
upgradeWebSocket((c) => ({ | ||
onOpen(_evt, ws) { | ||
hocuspocusServer.handleConnection(ws.raw, c.req.raw); | ||
hocuspocus.handleConnection(ws.raw, c.req.raw as any); | ||
}, | ||
})) | ||
); | ||
|
@@ -61,7 +66,7 @@ const documentMiddleware = createMiddleware<{ | |
}; | ||
}>(async (c, next) => { | ||
const documentId = c.req.param("documentId"); | ||
const document = hocuspocusServer.documents.get(documentId!); | ||
const document = hocuspocus.documents.get(documentId!); | ||
|
||
if (!document) { | ||
return c.json({ error: "Document not found" }, 404); | ||
|
@@ -85,6 +90,12 @@ app.route( | |
const server = serve({ | ||
fetch: app.fetch, | ||
port: 8787, | ||
}, (info) => { | ||
hocuspocus.hooks('onListen', { | ||
instance: hocuspocus, | ||
configuration: hocuspocus.configuration, | ||
port: info.port | ||
}) | ||
}); | ||
|
||
// Setup WebSocket support (needed for HocusPocus) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to double check whether this does what I think it does....