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 'unauthenticated' event, fix 'unsubscribed' event capabilities, update missing typedefs #147

Merged
merged 5 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 5 additions & 3 deletions base-server/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ interface AuthenticationReporter {

interface ReportersArguments {
add: ActionReporter
addClean: ActionReporter
authenticated: AuthenticationReporter
clean: CleanReporter
clientError: {
Expand Down Expand Up @@ -975,7 +976,7 @@ export class BaseServer<
* @param listener Client listener.
*/
on(
event: 'authenticated',
event: 'authenticated' | 'unauthenticated',
listener: (client: ServerClient, latencyMilliseconds: number) => void
): Unsubscribe

Expand Down Expand Up @@ -1009,8 +1010,9 @@ export class BaseServer<
event: 'unsubscribed',
listener: (
action: LoguxUnsubscribeAction,
meta: Readonly<ServerMeta>
) => void
meta: Readonly<ServerMeta>,
clientNodeId: string,
) => void,
): Unsubscribe

/**
Expand Down
2 changes: 1 addition & 1 deletion base-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ export class BaseServer {
}
}
}
this.emitter.emit('unsubscribed', action, meta)
this.emitter.emit('unsubscribed', action, meta, clientNodeId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need extra parameter? We can use parseId(meta.id).clientId

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in case of server-side channels cleanup (e.g. client is disconnected because of timeout), nodeId is server.nodeId

let actionId = this.app.log.generateId()

this.emitter.emit('report', 'unsubscribed', {
actionId: meta.id,
channel: action.channel
Expand Down
7 changes: 4 additions & 3 deletions server-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class ServerClient {
}

if (nodeId === 'server' || userId === 'server') {
this.app.emitter.emit('unauthenticated', this, 0)
this.app.emitter.emit('report', 'unauthenticated', reportDetails(this))
return false
}
Expand Down Expand Up @@ -133,6 +134,7 @@ export class ServerClient {
this.app.emitter.emit('authenticated', this, Date.now() - start)
this.app.emitter.emit('report', 'authenticated', reportDetails(this))
} else {
this.app.emitter.emit('unauthenticated', this, Date.now() - start)
this.app.emitter.emit('report', 'unauthenticated', reportDetails(this))
this.app.rememberBadAuth(this.remoteAddress)
}
Expand Down Expand Up @@ -162,9 +164,6 @@ export class ServerClient {
}
}
if (this.clientId) {
this.app.clientIds.delete(this.clientId)
this.app.nodeIds.delete(this.nodeId)

for (let channel in this.app.subscribers) {
let subscriber = this.app.subscribers[channel][this.nodeId]
if (subscriber) {
Expand All @@ -174,6 +173,8 @@ export class ServerClient {
this.app.performUnsubscribe(this.nodeId, action, meta)
}
}
this.app.clientIds.delete(this.clientId)
this.app.nodeIds.delete(this.nodeId)
}
if (!this.app.destroying) {
this.app.emitter.emit('disconnected', this)
Expand Down
8 changes: 8 additions & 0 deletions server-client/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,15 @@ it('removes itself on destroy', async () => {
'10:client2': { filters: { '{}': true } }
}
}
let unsubscribedClientNodeIds: string[] = []
test.app.on('unsubscribed', (action, meta, clientNodeId) => {
unsubscribedClientNodeIds.push(clientNodeId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 space indent should be used

expect(test.app.nodeIds.get(clientNodeId)).toBeDefined()
})
client1.destroy()
await delay(1)

expect(unsubscribedClientNodeIds).toEqual(['10:client1'])
expect(Array.from(test.app.userIds.keys())).toEqual(['10'])
expect(test.app.subscribers).toEqual({
'user/10': { '10:client2': { filters: { '{}': true } } }
Expand All @@ -272,6 +278,8 @@ it('removes itself on destroy', async () => {

client2.destroy()
await delay(1)

expect(unsubscribedClientNodeIds).toEqual(['10:client1', '10:client2'])
expect(pullNewReports()).toMatchObject([
['unsubscribed', { channel: 'user/10' }],
['disconnect', { nodeId: '10:client2' }]
Expand Down