Skip to content

Commit

Permalink
add 'unauthenticated' event, fix 'unsubscribed' event capabilities, u…
Browse files Browse the repository at this point in the history
…pdate missing typedefs (#147)

* add missing 'addClean' reporter type definition

* add BaseServer 'unauthenticated' event on par with 'authenticated' to allow advanced logging catch ServerClient (unavailable via reporter interface)

* add 'unsubscribed' event clientNodeId last arg (that can be either serverNodeId (server-initiated -ch) or clientNodeId (client-initiated -ch)

* ClientServerdestroy: allow 'unsubscribed' event handler to retrieve ServerClient by evicting nodeIds / clientIds later, after unsubscribe

* fix indent
  • Loading branch information
erictheswift authored Aug 15, 2023
1 parent ebc9815 commit b605bde
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
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)
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)
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

0 comments on commit b605bde

Please sign in to comment.