Skip to content

Commit f6f2a7c

Browse files
committed
fix: override service path in context (closes #12)
1 parent 9faf8a6 commit f6f2a7c

3 files changed

Lines changed: 37 additions & 26 deletions

File tree

packages/feathers-distributed/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ __
88

99
Detailed documentation is available at the following [link](https://kalisio.github.io/feathers-ekosystem/pacakges/feathers-distributed).
1010

11+
In the tests four apps are actually created, each app has a distribution key corresponding to its index:
12+
- a gateway (index 0) with a local `users` service then a dynamically added local `custom` service with an additional `custom` event (`created`/`custom` events distributed)
13+
- a first app (index 1) without any local service but with the distributed gateway services
14+
- a second app (index 2) without any local service but with the distributed gateway services
15+
- a third app (index 3) with a local `no-events` service with an additional `custom` event (all events distributed) and with the distributed gateway services
16+
The `custom` service is exposed remotely on a new `custom-name` name.
17+
1118
## License
1219

1320
Licensed under the [MIT license](LICENSE).

packages/feathers-distributed/src/register.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ export async function registerApplication (app, applicationDescriptor) {
6363
debug(`Dispatching ${event} remote service event on path ${object.path} in local app with uuid ${app.shortUuid} and key ${app.distributionKey}`, object, context)
6464
const servicePath = getServicePath(app, object)
6565
const service = getService(app, servicePath)
66-
if (context) Object.assign(context, { service, app })
66+
if (context) {
67+
// We override the context path as the remote service might be registered on a different one.
68+
// Otherwise the socket client will not receive the event as listening to the 'remote-path created' event and not the 'local-path created' event.
69+
Object.assign(context, { path: servicePath, service, app })
70+
}
6771
// Ensure we don't have any local service with the same name to avoid infinite looping
6872
if (service?.remote) service.emit(event, object, context)
6973
})

packages/feathers-distributed/test/index.test.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { vi, beforeAll, afterAll, describe, it, expect } from 'vitest'
22
import { setTimeout as sleep } from 'node:timers/promises'
3-
// FIXME
4-
// import util from 'node:util'
53
import { authenticate } from '@feathersjs/authentication'
64
import auth from '@feathersjs/authentication-client'
75
import restClient from '@feathersjs/rest-client'
@@ -446,8 +444,6 @@ describe('feathers-distributed:main', () => {
446444
expect(name).toBe('Donald Doe')
447445
}, 5000)
448446

449-
/*
450-
FIXME
451447
it('dispatch custom events and ignore the ones not configured for distribution', async () => {
452448
let createdCount = 0
453449
const updatedCount = 0
@@ -460,52 +456,56 @@ describe('feathers-distributed:main', () => {
460456
socketClientCustomServices[service2].removeAllListeners('updated')
461457
socketClientCustomServices[service1].removeAllListeners('custom')
462458
}
463-
await new Promise((resolve, reject) => {
464-
const checkIsDone = () => {
459+
const testFunction = async (resolve, reject) => {
460+
const checkIsDone = (data, context) => {
465461
if ((createdCount === 2) && (updatedCount === 0) && (customCount === 2)) {
466462
removeListeners()
467463
resolve()
468464
}
469465
}
470-
customServices[service1].once('created', user => {
466+
customServices[service1].once('created', (user, context) => {
471467
expect(user.id === 0).toBe(true)
472468
createdCount++
473-
checkIsDone()
469+
checkIsDone(user, context)
474470
})
475-
customServices[service2].once('updated', () => {
471+
customServices[service2].once('updated', (data, context) => {
476472
removeListeners()
477473
reject(new Error('updated should not be called'))
478474
})
479475
customServices[service1].once('custom', (data, context) => {
480476
expect(data.payload === 'Donald Doe').toBe(true)
481-
expect(context.app === apps[service1]).toBe(true)
482-
expect(context.service === customServices[service1]).toBe(true)
477+
if (context) { // When not emitted from hook context might be missing
478+
expect(context.app === apps[service1]).toBe(true)
479+
expect(context.service === customServices[service1]).toBe(true)
480+
}
483481
customCount++
484-
checkIsDone()
482+
checkIsDone(data, context)
485483
})
486-
// FIXME: not called whatever the reason
487-
socketClientCustomServices[service1].once('created', user => {
484+
socketClientCustomServices[service1].once('created', (user, context) => {
488485
expect(user.id === 0).toBe(true)
489486
createdCount++
490-
checkIsDone()
487+
checkIsDone(user, context)
491488
})
492489
socketClientCustomServices[service2].once('updated', () => {
493490
removeListeners()
494491
reject(new Error('updated should not be called'))
495492
})
496-
socketClientCustomServices[service1].once('custom', data => {
493+
socketClientCustomServices[service1].once('custom', (data, context) => {
497494
expect(data.payload === 'Donald Doe').toBe(true)
498495
customCount++
499-
checkIsDone()
496+
checkIsDone(data, context)
500497
})
501-
util.promisify(setTimeout)(5000)
502-
.then(() => customServices[gateway].create({ name: 'Donald Doe' }))
503-
.then(() => customServices[gateway].update(0, { name: 'Donald Dover' }))
504-
.then(() => customServices[gateway].emit('custom', { payload: 'Donald Doe' }))
505-
.catch(reject)
506-
})
507-
}, 40000)
508-
*/
498+
try {
499+
await sleep(5000)
500+
await customServices[gateway].create({ name: 'Donald Doe' })
501+
await customServices[gateway].update(0, { name: 'Donald Dover' })
502+
await customServices[gateway].emit('custom', { payload: 'Donald Doe' })
503+
} catch (error) {
504+
reject(error)
505+
}
506+
}
507+
await new Promise(testFunction)
508+
}, 20000)
509509

510510
it('not found request should return 404 on local service', async () => {
511511
const url = 'http://localhost:' + (baseListenPort + gateway) + '/xxx'

0 commit comments

Comments
 (0)