-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: check service dependencies on startup
Allows services to optionally define the capabilities they provide to the rest of libp2p and also the capabilities they require from other services. This allows, for example, the `WebRTC` transport to require the `CircuitRelay` transport to be present, or `KAD-DHT` (or anything that uses a topology) to require the identify protocol. Fixes #2263 Refs #2135
- Loading branch information
1 parent
863b3de
commit db7d5e5
Showing
4 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { serviceCapabilities, serviceDependencies, stop } from '@libp2p/interface' | ||
import { expect } from 'aegir/chai' | ||
import { createLibp2p } from '../../src/index.js' | ||
import type { Libp2p } from '@libp2p/interface' | ||
|
||
/** | ||
* A service with no dependencies | ||
*/ | ||
function serviceA () { | ||
return () => { | ||
return { | ||
[serviceCapabilities]: [ | ||
'@libp2p/service-a' | ||
] | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* A service with a dependency on service A | ||
*/ | ||
function serviceB () { | ||
return () => { | ||
return { | ||
[Symbol.toStringTag]: 'service-b', | ||
[serviceDependencies]: [ | ||
'@libp2p/service-a' | ||
] | ||
} | ||
} | ||
} | ||
|
||
describe('service dependencies', () => { | ||
let node: Libp2p | ||
|
||
afterEach(async () => { | ||
await stop(node) | ||
}) | ||
|
||
it('should start when services have no dependencies', async () => { | ||
node = await createLibp2p({ | ||
services: { | ||
a: serviceA() | ||
} | ||
}) | ||
|
||
expect(node).to.be.ok() | ||
}) | ||
|
||
it('should error when service dependencies are unmet', async () => { | ||
await expect(createLibp2p({ | ||
services: { | ||
b: serviceB() | ||
} | ||
})).to.eventually.be.rejected | ||
.with.property('code', 'ERR_UNMET_SERVICE_DEPENDENCIES') | ||
}) | ||
|
||
it('should not error when service dependencies are met', async () => { | ||
node = await createLibp2p({ | ||
services: { | ||
a: serviceA(), | ||
b: serviceB() | ||
} | ||
}) | ||
|
||
expect(node).to.be.ok() | ||
}) | ||
}) |