Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add access to dynamic namespace #8

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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