11import { Database } from '@hocuspocus/extension-database'
22import { Document } from '@hocuspocus/server'
33import { TiptapTransformer } from '@hocuspocus/transformer'
4- import * as Sentry from '@sentry/node'
54import { generateHTML , generateJSON } from '@tiptap/html'
65import { fromUint8Array , toUint8Array } from 'js-base64'
76import * as Y from 'yjs'
@@ -14,14 +13,14 @@ import { Context } from './types'
1413const extensions = getNoteExtensions ( )
1514
1615export function sendVersionToConnections ( document : Document , version : number ) {
17- document . connections . forEach ( ( connection ) => {
18- const connectionSchemaVersion = connection . connection . context . schemaVersion ?? 0
16+ document . getConnections ( ) . forEach ( ( connection ) => {
17+ const connectionSchemaVersion = ( connection . context as Context | undefined ) ? .schemaVersion ?? 0
1918
2019 // Update connections to readOnly if the schema version is lower than the current version
21- connection . connection . readOnly = connectionSchemaVersion < version
20+ connection . readOnly = connectionSchemaVersion < version
2221
2322 // Send the schema version to the client
24- connection . connection . sendStateless (
23+ connection . sendStateless (
2524 JSON . stringify ( {
2625 type : 'schema' ,
2726 version
@@ -45,20 +44,41 @@ export async function getResource({ token, id, type, organization }: GetResource
4544 }
4645}
4746
47+ function resolveContext ( data : {
48+ context ?: Context
49+ lastContext ?: Context
50+ requestParameters ?: URLSearchParams
51+ } ) : Context | undefined {
52+ const fromPayload = data . lastContext ?? data . context
53+
54+ if ( fromPayload ?. token && fromPayload . organization ) {
55+ return fromPayload
56+ }
57+
58+ const organization = data . requestParameters ?. get ( 'organization' ) ?? fromPayload ?. organization
59+ const type = data . requestParameters ?. get ( 'type' ) ?? fromPayload ?. type ?? null
60+ const token = fromPayload ?. token
61+ const schemaVersion = fromPayload ?. schemaVersion ?? 0
62+
63+ if ( ! token || ! organization ) return fromPayload
64+
65+ return { token, schemaVersion, organization, type }
66+ }
67+
4868export const database = new Database ( {
4969 /**
5070 * Fetch the document state from Campsite, or generate a new document from the existing
5171 * HTML if the document has never been edited before.
5272 */
5373 async fetch ( data ) {
54- const context : Context = data . context
74+ const context = resolveContext ( data )
5575
5676 const id = data . documentName
57- const organization = data . requestParameters . get ( ' organization' )
58- const type = data . requestParameters . get ( ' type' )
77+ const organization = context ?. organization
78+ const type = context ?. type ?? null
5979
6080 try {
61- if ( ! organization ) return new Uint8Array ( )
81+ if ( ! context ?. token || ! organization ) return new Uint8Array ( )
6282
6383 const state = await getResource ( { token : context . token , id, type, organization } )
6484
@@ -79,16 +99,11 @@ export const database = new Database({
7999
80100 return Y . encodeStateAsUpdate ( ydoc )
81101 } catch ( error ) {
82- Sentry . setContext ( 'document' , {
83- id,
84- organization,
85- type
86- } )
87- Sentry . setContext ( 'context' , {
88- schemaVersion : context . schemaVersion ,
89- token : context . token
102+ console . error ( 'database.fetch failed' , {
103+ document : { id, organization, type } ,
104+ schemaVersion : context ?. schemaVersion ,
105+ error
90106 } )
91- Sentry . captureException ( error )
92107 throw error
93108 }
94109 } ,
@@ -97,14 +112,14 @@ export const database = new Database({
97112 * Store the document state in Campsite.
98113 */
99114 async store ( data ) {
100- const context : Context = data . context
115+ const context = resolveContext ( data )
101116
102117 const id = data . documentName
103- const organization = data . requestParameters . get ( ' organization' )
104- const type = data . requestParameters . get ( ' type' )
118+ const organization = context ?. organization
119+ const type = context ?. type ?? null
105120
106121 try {
107- if ( ! organization ) return
122+ if ( ! context ?. token || ! organization ) return
108123
109124 // Generate a state from the Yjs document
110125 const state = Y . encodeStateAsUpdate ( data . document )
@@ -128,16 +143,11 @@ export const database = new Database({
128143 }
129144 )
130145 } catch ( error ) {
131- Sentry . setContext ( 'document' , {
132- id,
133- organization,
134- type
135- } )
136- Sentry . setContext ( 'context' , {
137- schemaVersion : context . schemaVersion ,
138- token : context . token
146+ console . error ( 'database.store failed' , {
147+ document : { id, organization, type } ,
148+ schemaVersion : context ?. schemaVersion ,
149+ error
139150 } )
140- Sentry . captureException ( error )
141151 throw error
142152 }
143153 }
0 commit comments