Skip to content

Commit

Permalink
[MERGE] Merge pull request #8 from ofir-zeitoun/dev/dynamic0namespace…
Browse files Browse the repository at this point in the history
…-acces into main

Add access to dynamic namespace
  • Loading branch information
ivan-topp authored Sep 20, 2023
2 parents 16a812c + 359fe78 commit 412f8f7
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/server/y-socket-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ export class YSocketIO extends Observable<string> {
* @type {YSocketIOConfiguration}
*/
private readonly configuration?: YSocketIOConfiguration

/**
* @type {Namespace | null}
*/
public nsp: Namespace | null = null
/**
* YSocketIO constructor.
* @constructor
Expand Down Expand Up @@ -90,15 +93,15 @@ export class YSocketIO extends Observable<string> {
* @type {() => void}
*/
public initialize (): void {
const dynamicNamespace = this.io.of(/^\/yjs\|.*$/)
this.nsp = this.io.of(/^\/yjs\|.*$/)

dynamicNamespace.use(async (socket, next) => {
this.nsp.use(async (socket, next) => {
if ((this.configuration?.authenticate) == null) return next()
if (await this.configuration.authenticate(socket.handshake)) return next()
else return next(new Error('Unauthorized'))
})

dynamicNamespace.on('connection', async (socket) => {
this.nsp.on('connection', async (socket) => {
const namespace = socket.nsp.name.replace(/\/yjs\|/, '')

const doc = await this.initDocument(namespace, socket.nsp, this.configuration?.gcEnabled)
Expand Down

0 comments on commit 412f8f7

Please sign in to comment.