@hocuspocus/server
Advanced tools
Comparing version 1.0.0-alpha.106 to 1.0.0-alpha.107
@@ -30,2 +30,6 @@ /// <reference types="node" /> | ||
/** | ||
* Set a callback that will be triggered before an message is handled | ||
*/ | ||
beforeHandleMessage(callback: (payload: Document, update: Uint8Array) => Promise<any>): Connection; | ||
/** | ||
* Send the given message | ||
@@ -32,0 +36,0 @@ */ |
@@ -39,2 +39,3 @@ /// <reference types="node" /> | ||
afterLoadDocument?(data: onLoadDocumentPayload): Promise<any>; | ||
beforeHandleMessage?(data: beforeHandleMessagePayload): Promise<any>; | ||
onChange?(data: onChangePayload): Promise<any>; | ||
@@ -52,3 +53,3 @@ onStoreDocument?(data: onStoreDocumentPayload): Promise<any>; | ||
*/ | ||
'onCreateDocument' | 'onLoadDocument' | 'afterLoadDocument' | 'onChange' | 'onStoreDocument' | 'afterStoreDocument' | 'onAwarenessUpdate' | 'onRequest' | 'onDisconnect' | 'onDestroy'; | ||
'onCreateDocument' | 'onLoadDocument' | 'afterLoadDocument' | 'beforeHandleMessage' | 'onChange' | 'onStoreDocument' | 'afterStoreDocument' | 'onAwarenessUpdate' | 'onRequest' | 'onDisconnect' | 'onDestroy'; | ||
export declare type HookPayload = onConfigurePayload | onListenPayload | onUpgradePayload | onConnectPayload | connectedPayload | onAuthenticatePayload | onLoadDocumentPayload | onChangePayload | onStoreDocumentPayload | afterStoreDocumentPayload | onAwarenessUpdatePayload | onRequestPayload | onDisconnectPayload | onDestroyPayload; | ||
@@ -160,2 +161,13 @@ export interface Configuration extends Extension { | ||
} | ||
export interface beforeHandleMessagePayload { | ||
clientsCount: number; | ||
context: any; | ||
document: Document; | ||
documentName: string; | ||
instance: Hocuspocus; | ||
requestHeaders: IncomingHttpHeaders; | ||
requestParameters: URLSearchParams; | ||
update: Uint8Array; | ||
socketId: string; | ||
} | ||
export interface onStoreDocumentPayload { | ||
@@ -162,0 +174,0 @@ clientsCount: number; |
{ | ||
"name": "@hocuspocus/server", | ||
"description": "plug & play collaboration backend", | ||
"version": "1.0.0-alpha.106", | ||
"version": "1.0.0-alpha.107", | ||
"homepage": "https://hocuspocus.dev", | ||
@@ -43,3 +43,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "6074a6cb832b40bc579f28db2823ba5723887312" | ||
"gitHead": "3dc62aedd8d19d4a9c346188885657ea814271c0" | ||
} |
import AsyncLock from 'async-lock' | ||
import WebSocket from 'ws' | ||
import { IncomingMessage as HTTPIncomingMessage } from 'http' | ||
import { CloseEvent, ConnectionTimeout, WsReadyStates } from '@hocuspocus/common' | ||
import { | ||
CloseEvent, ConnectionTimeout, Forbidden, WsReadyStates, | ||
} from '@hocuspocus/common' | ||
import Document from './Document' | ||
@@ -10,2 +12,3 @@ import { IncomingMessage } from './IncomingMessage' | ||
import { Debugger } from './Debugger' | ||
import { beforeHandleMessagePayload } from './types' | ||
@@ -30,2 +33,3 @@ export class Connection { | ||
onClose: (document: Document) => null, | ||
beforeHandleMessage: (document: Document, update: Uint8Array) => Promise, | ||
} | ||
@@ -88,2 +92,11 @@ | ||
/** | ||
* Set a callback that will be triggered before an message is handled | ||
*/ | ||
beforeHandleMessage(callback: (payload: Document, update: Uint8Array) => Promise<any>): Connection { | ||
this.callbacks.beforeHandleMessage = callback | ||
return this | ||
} | ||
/** | ||
* Send the given message | ||
@@ -113,3 +126,2 @@ */ | ||
this.lock.acquire('close', (done: Function) => { | ||
if (this.pingInterval) { | ||
@@ -177,6 +189,15 @@ clearInterval(this.pingInterval) | ||
private handleMessage(data: Iterable<number>): void { | ||
new MessageReceiver( | ||
new IncomingMessage(data), | ||
this.logger, | ||
).apply(this.document, this) | ||
this.callbacks.beforeHandleMessage(this.document, data) | ||
.then(() => { | ||
new MessageReceiver( | ||
new IncomingMessage(data), | ||
this.logger, | ||
).apply(this.document, this) | ||
}) | ||
.catch((e: any) => { | ||
this.close({ | ||
code: 'code' in e ? e.code : Forbidden.code, | ||
reason: 'reason' in e ? e.reason : Forbidden.reason, | ||
}) | ||
}) | ||
} | ||
@@ -183,0 +204,0 @@ |
@@ -22,3 +22,3 @@ import * as decoding from 'lib0/decoding' | ||
AwarenessUpdate, | ||
HookPayload, | ||
HookPayload, beforeHandleMessagePayload, | ||
} from './types' | ||
@@ -28,3 +28,3 @@ import Document from './Document' | ||
import { OutgoingMessage } from './OutgoingMessage' | ||
import meta from '../package.json' | ||
import meta from '../package.json' assert { type: 'json' } | ||
import { Debugger } from './Debugger' | ||
@@ -60,2 +60,3 @@ import { onListenPayload } from '.' | ||
connected: () => new Promise(r => r(null)), | ||
beforeHandleMessage: () => new Promise(r => r(null)), | ||
onChange: () => new Promise(r => r(null)), | ||
@@ -130,2 +131,3 @@ onCreateDocument: defaultOnCreateDocument, | ||
onLoadDocument, | ||
beforeHandleMessage: this.configuration.beforeHandleMessage, | ||
onChange: this.configuration.onChange, | ||
@@ -351,2 +353,3 @@ onStoreDocument: this.configuration.onStoreDocument, | ||
} catch (error) { | ||
console.error(error) | ||
// | ||
@@ -707,3 +710,18 @@ } | ||
}) | ||
instance.beforeHandleMessage((document, update) => { | ||
const hookPayload: beforeHandleMessagePayload = { | ||
instance: this, | ||
clientsCount: document.getConnectionsCount(), | ||
context, | ||
document, | ||
socketId, | ||
documentName: document.name, | ||
requestHeaders: request.headers, | ||
requestParameters: Hocuspocus.getParameters(request), | ||
update, | ||
} | ||
return this.hooks('beforeHandleMessage', hookPayload) | ||
}) | ||
// If the WebSocket has already disconnected (wow, that was fast) – then | ||
@@ -710,0 +728,0 @@ // immediately call close to cleanup the connection and document in memory. |
@@ -45,2 +45,3 @@ import { | ||
afterLoadDocument?(data: onLoadDocumentPayload): Promise<any>, | ||
beforeHandleMessage?(data: beforeHandleMessagePayload): Promise<any>, | ||
onChange?(data: onChangePayload): Promise<any>, | ||
@@ -68,2 +69,3 @@ onStoreDocument?(data: onStoreDocumentPayload): Promise<any>, | ||
'afterLoadDocument' | | ||
'beforeHandleMessage' | | ||
'onChange' | | ||
@@ -207,2 +209,14 @@ 'onStoreDocument' | | ||
export interface beforeHandleMessagePayload { | ||
clientsCount: number, | ||
context: any, | ||
document: Document, | ||
documentName: string, | ||
instance: Hocuspocus, | ||
requestHeaders: IncomingHttpHeaders, | ||
requestParameters: URLSearchParams, | ||
update: Uint8Array, | ||
socketId: string, | ||
} | ||
export interface onStoreDocumentPayload { | ||
@@ -209,0 +223,0 @@ clientsCount: number, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
640456
127
7786