Skip to content

Commit

Permalink
Add connection and context to connected hook payload
Browse files Browse the repository at this point in the history
  • Loading branch information
canadaduane committed May 16, 2023
1 parent 4c8d33b commit bcd0bf9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
18 changes: 9 additions & 9 deletions packages/server/src/Hocuspocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export class Hocuspocus {

// To override settings for specific connections, we’ll
// keep track of a few things in the `ConnectionConfiguration`.
const connection: ConnectionConfiguration = {
const connectionConfig: ConnectionConfiguration = {
readOnly: false,
requiresAuthentication: this.requiresAuthentication,
isAuthenticated: false,
Expand All @@ -396,7 +396,7 @@ export class Hocuspocus {
requestHeaders: request.headers,
requestParameters: Hocuspocus.getParameters(request),
socketId,
connection,
connectionConfig,
}

// this map indicates whether a `Connection` instance has already taken over for incoming message for the key (i.e. documentName)
Expand All @@ -415,8 +415,8 @@ export class Hocuspocus {
clearTimeout(closeIdleConnection)

// If no hook interrupts, create a document and connection
const document = await this.createDocument(documentName, request, socketId, connection, context)
const instance = this.createConnection(incoming, request, document, socketId, connection.readOnly, context)
const document = await this.createDocument(documentName, request, socketId, connectionConfig, context)
const instance = this.createConnection(incoming, request, document, socketId, connectionConfig.readOnly, context)

instance.onClose((document, event) => {
delete documentConnections[documentName]
Expand All @@ -436,7 +436,7 @@ export class Hocuspocus {
incoming.emit('message', input)
})

this.hooks('connected', { ...hookPayload, documentName })
this.hooks('connected', { ...hookPayload, documentName, context, connection: instance })
}

// This listener handles authentication messages and queues everything else.
Expand Down Expand Up @@ -473,7 +473,7 @@ export class Hocuspocus {
})
.then(() => {
// All `onAuthenticate` hooks passed.
connection.isAuthenticated = true
connectionConfig.isAuthenticated = true

// Let the client know that authentication was successful.
const message = new OutgoingMessage(documentName).writeAuthenticated()
Expand Down Expand Up @@ -545,7 +545,7 @@ export class Hocuspocus {
})
.then(() => {
// Authentication is required, we’ll need to wait for the Authentication message.
if (connection.requiresAuthentication || connectionEstablishing[documentName]) {
if (connectionConfig.requiresAuthentication || connectionEstablishing[documentName]) {
return
}
connectionEstablishing[documentName] = true
Expand Down Expand Up @@ -654,7 +654,7 @@ export class Hocuspocus {
/**
* Create a new document by the given request
*/
private async createDocument(documentName: string, request: IncomingMessage, socketId: string, connection: ConnectionConfiguration, context?: any): Promise<Document> {
private async createDocument(documentName: string, request: IncomingMessage, socketId: string, connectionConfig: ConnectionConfiguration, context?: any): Promise<Document> {
if (this.documents.has(documentName)) {
const document = this.documents.get(documentName)

Expand All @@ -669,7 +669,7 @@ export class Hocuspocus {
const hookPayload = {
instance: this,
context,
connection,
connectionConfig,
document,
documentName,
socketId,
Expand Down
14 changes: 8 additions & 6 deletions packages/server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export interface onAuthenticatePayload {
requestParameters: URLSearchParams,
socketId: string,
token: string,
connection: ConnectionConfiguration
connectionConfig: ConnectionConfiguration
}

export interface onConnectPayload {
Expand All @@ -171,17 +171,19 @@ export interface onConnectPayload {
requestHeaders: IncomingHttpHeaders,
requestParameters: URLSearchParams,
socketId: string,
connection: ConnectionConfiguration
connectionConfig: ConnectionConfiguration
}

export interface connectedPayload {
context: any,
documentName: string,
instance: Hocuspocus,
request: IncomingMessage,
requestHeaders: IncomingHttpHeaders,
requestParameters: URLSearchParams,
socketId: string,
connection: ConnectionConfiguration
connectionConfig: ConnectionConfiguration,
connection: Connection
}

export interface onLoadDocumentPayload {
Expand All @@ -192,7 +194,7 @@ export interface onLoadDocumentPayload {
requestHeaders: IncomingHttpHeaders,
requestParameters: URLSearchParams,
socketId: string,
connection: ConnectionConfiguration
connectionConfig: ConnectionConfiguration
}

export interface afterLoadDocumentPayload {
Expand All @@ -203,7 +205,7 @@ export interface afterLoadDocumentPayload {
requestHeaders: IncomingHttpHeaders,
requestParameters: URLSearchParams,
socketId: string,
connection: ConnectionConfiguration
connectionConfig: ConnectionConfiguration
}

export interface onChangePayload {
Expand Down Expand Up @@ -277,7 +279,7 @@ export interface fetchPayload {
requestHeaders: IncomingHttpHeaders,
requestParameters: URLSearchParams,
socketId: string,
connection: ConnectionConfiguration
connectionConfig: ConnectionConfiguration
}

export interface storePayload extends onStoreDocumentPayload {
Expand Down

0 comments on commit bcd0bf9

Please sign in to comment.