@hocuspocus/server
Advanced tools
Comparing version 2.4.0-rc.1 to 2.4.0
@@ -28,3 +28,2 @@ /// <reference types="node" /> | ||
boundClose: (event?: CloseEvent) => void; | ||
boundHandleMessage: (data: Uint8Array) => void; | ||
boundHandlePong: () => void; | ||
@@ -68,6 +67,6 @@ handlePong(): void; | ||
* Handle an incoming message | ||
* @private | ||
* @public | ||
*/ | ||
private handleMessage; | ||
handleMessage(data: Uint8Array): void; | ||
} | ||
export default Connection; |
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import { Server as HTTPServer, IncomingMessage } from 'http'; | ||
import WebSocket, { AddressInfo, WebSocketServer } from 'ws'; | ||
import { IncomingMessage } from 'http'; | ||
import WebSocket, { AddressInfo } from 'ws'; | ||
import { Server as HocuspocusServer } from './Server'; | ||
import { Debugger } from './Debugger.js'; | ||
@@ -28,4 +29,3 @@ import { DirectConnection } from './DirectConnection.js'; | ||
documents: Map<string, Document>; | ||
httpServer?: HTTPServer; | ||
webSocketServer?: WebSocketServer; | ||
server?: HocuspocusServer; | ||
debugger: Debugger; | ||
@@ -70,3 +70,3 @@ constructor(configuration?: Partial<Configuration>); | ||
* | ||
* … and if nothings fails it’ll fully establish the connection and | ||
* … and if nothing fails it’ll fully establish the connection and | ||
* load the Document then. | ||
@@ -73,0 +73,0 @@ */ |
{ | ||
"name": "@hocuspocus/server", | ||
"description": "plug & play collaboration backend", | ||
"version": "2.4.0-rc.1", | ||
"version": "2.4.0", | ||
"homepage": "https://hocuspocus.dev", | ||
@@ -32,3 +32,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@hocuspocus/common": "^2.4.0-rc.1", | ||
"@hocuspocus/common": "^2.4.0", | ||
"async-lock": "^1.3.1", | ||
@@ -35,0 +35,0 @@ "kleur": "^4.1.4", |
@@ -32,3 +32,3 @@ import { IncomingHttpHeaders, IncomingMessage } from 'http' | ||
// this map indicates whether a `Connection` instance has already taken over for incoming message for the key (i.e. documentName) | ||
private readonly documentConnections: Record<string, boolean> = {} | ||
private readonly documentConnections: Record<string, Connection> = {} | ||
@@ -197,3 +197,3 @@ // While the connection will be establishing messages will | ||
this.documentConnections[documentName] = true | ||
this.documentConnections[documentName] = instance | ||
@@ -294,3 +294,7 @@ // There’s no need to queue messages anymore. | ||
if (this.documentConnections[documentName] === true) { | ||
const connection = this.documentConnections[documentName] | ||
if (connection) { | ||
// forward the message to the connection | ||
connection.handleMessage(data) | ||
// we already have a `Connection` set up for this document | ||
@@ -297,0 +301,0 @@ return |
@@ -74,3 +74,2 @@ import { IncomingMessage as HTTPIncomingMessage } from 'http' | ||
this.webSocket.on('close', this.boundClose) | ||
this.webSocket.on('message', this.boundHandleMessage) | ||
this.webSocket.on('pong', this.boundHandlePong) | ||
@@ -83,4 +82,2 @@ | ||
boundHandleMessage = this.handleMessage.bind(this) | ||
boundHandlePong = this.handlePong.bind(this) | ||
@@ -172,3 +169,2 @@ | ||
this.webSocket.removeListener('close', this.boundClose) | ||
this.webSocket.removeListener('message', this.boundHandleMessage) | ||
this.webSocket.removeListener('pong', this.boundHandlePong) | ||
@@ -223,5 +219,5 @@ | ||
* Handle an incoming message | ||
* @private | ||
* @public | ||
*/ | ||
private handleMessage(data: Uint8Array): void { | ||
public handleMessage(data: Uint8Array): void { | ||
const message = new IncomingMessage(data) | ||
@@ -228,0 +224,0 @@ const documentName = message.readVarString() |
@@ -1,2 +0,2 @@ | ||
import { Server as HTTPServer, IncomingMessage, createServer } from 'http' | ||
import { IncomingMessage } from 'http' | ||
import { ListenOptions } from 'net' | ||
@@ -8,5 +8,6 @@ import { | ||
import { v4 as uuid } from 'uuid' | ||
import WebSocket, { AddressInfo, WebSocketServer } from 'ws' | ||
import WebSocket, { AddressInfo } from 'ws' | ||
import { Doc, applyUpdate, encodeStateAsUpdate } from 'yjs' | ||
import meta from '../package.json' assert { type: 'json' } | ||
import { Server as HocuspocusServer } from './Server' | ||
import { ClientConnection } from './ClientConnection.js' | ||
@@ -73,6 +74,4 @@ // TODO: would be nice to only have a dependency on ClientConnection, and not on Connection | ||
httpServer?: HTTPServer | ||
server?: HocuspocusServer | ||
webSocketServer?: WebSocketServer | ||
debugger = new Debugger() | ||
@@ -168,70 +167,6 @@ | ||
const webSocketServer = new WebSocketServer({ noServer: true }) | ||
this.server = new HocuspocusServer(this) | ||
webSocketServer.on('connection', async (incoming: WebSocket, request: IncomingMessage) => { | ||
incoming.on('error', error => { | ||
/** | ||
* Handle a ws instance error, which is required to prevent | ||
* the server from crashing when one happens | ||
* See https://github.com/websockets/ws/issues/1777#issuecomment-660803472 | ||
* @private | ||
*/ | ||
this.debugger.log('Error emitted from webSocket instance:') | ||
this.debugger.log(error) | ||
}) | ||
this.handleConnection(incoming, request) | ||
}) | ||
const server = createServer(async (request, response) => { | ||
try { | ||
await this.hooks('onRequest', { request, response, instance: this }) | ||
// default response if all prior hooks don't interfere | ||
response.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
response.end('OK') | ||
} catch (error) { | ||
// if a hook rejects and the error is empty, do nothing | ||
// this is only meant to prevent later hooks and the | ||
// default handler to do something. if a error is present | ||
// just rethrow it | ||
if (error) { | ||
throw error | ||
} | ||
} | ||
}) | ||
server.on('upgrade', async (request, socket, head) => { | ||
try { | ||
await this.hooks('onUpgrade', { | ||
request, | ||
socket, | ||
head, | ||
instance: this, | ||
}) | ||
// let the default websocket server handle the connection if | ||
// prior hooks don't interfere | ||
webSocketServer.handleUpgrade(request, socket, head, ws => { | ||
webSocketServer.emit('connection', ws, request) | ||
}) | ||
} catch (error) { | ||
// if a hook rejects and the error is empty, do nothing | ||
// this is only meant to prevent later hooks and the | ||
// default handler to do something. if a error is present | ||
// just rethrow it | ||
// TODO: why? | ||
if (error) { | ||
throw error | ||
} | ||
} | ||
}) | ||
this.httpServer = server | ||
this.webSocketServer = webSocketServer | ||
return new Promise((resolve: Function, reject: Function) => { | ||
server.listen({ | ||
this.server?.httpServer.listen({ | ||
port: this.configuration.port, | ||
@@ -261,3 +196,3 @@ host: this.configuration.address, | ||
get address(): AddressInfo { | ||
return (this.httpServer?.address() || { | ||
return (this.server?.httpServer?.address() || { | ||
port: this.configuration.port, | ||
@@ -353,7 +288,7 @@ address: this.configuration.address, | ||
async destroy(): Promise<any> { | ||
this.httpServer?.close() | ||
this.server?.httpServer?.close() | ||
try { | ||
this.webSocketServer?.close() | ||
this.webSocketServer?.clients.forEach(client => { | ||
this.server?.webSocketServer?.close() | ||
this.server?.webSocketServer?.clients.forEach(client => { | ||
client.terminate() | ||
@@ -363,3 +298,2 @@ }) | ||
console.error(error) | ||
// | ||
} | ||
@@ -379,3 +313,3 @@ | ||
* | ||
* … and if nothings fails it’ll fully establish the connection and | ||
* … and if nothing fails it’ll fully establish the connection and | ||
* load the Document then. | ||
@@ -382,0 +316,0 @@ */ |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
813534
145
9610
0
Updated@hocuspocus/common@^2.4.0