-
Notifications
You must be signed in to change notification settings - Fork 473
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
feat: allow custom registrar #3040
Open
dozyio
wants to merge
1
commit into
libp2p:main
Choose a base branch
from
dozyio:feat-custom-registrar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* eslint-env mocha */ | ||
|
||
import { expect } from 'aegir/chai' | ||
import sinon from 'sinon' | ||
import { createLibp2p } from '../../src/index.js' | ||
import type { Libp2p } from '@libp2p/interface' | ||
import type { Registrar } from '@libp2p/interface-internal' | ||
|
||
describe('custom registrar functionality', () => { | ||
let libp2p: Libp2p | ||
let customRegistrar: { | ||
handle: sinon.SinonStub<any, Promise<void>> | ||
unhandle: sinon.SinonStub<any, Promise<void>> | ||
getProtocols: sinon.SinonStub<any, string[]> | ||
register: sinon.SinonStub<any, Promise<string>> | ||
unregister: sinon.SinonStub<any, void> | ||
getHandler: sinon.SinonStub<any, { handler(): void, options: Record<string, unknown> }> | ||
getTopologies: sinon.SinonStub<any, any[]> | ||
} | ||
|
||
beforeEach(async (): Promise<void> => { | ||
customRegistrar = { | ||
handle: sinon.stub().resolves(), | ||
unhandle: sinon.stub().resolves(), | ||
getProtocols: sinon.stub().returns(['/custom/1.0.0']), | ||
register: sinon.stub().resolves('custom-topology-id'), | ||
unregister: sinon.stub(), | ||
getHandler: sinon.stub().returns({ handler: (): void => {}, options: {} }), | ||
getTopologies: sinon.stub().returns([]) | ||
} | ||
|
||
libp2p = await createLibp2p({ | ||
registrar: (_components) => customRegistrar as unknown as Registrar | ||
}) | ||
}) | ||
|
||
afterEach(async (): Promise<void> => { | ||
await libp2p?.stop() | ||
sinon.restore() | ||
}) | ||
|
||
it('should reflect custom getProtocols implementation', (): void => { | ||
const protocols: string[] = libp2p.getProtocols() | ||
expect(protocols).to.deep.equal(['/custom/1.0.0']) | ||
expect(customRegistrar.getProtocols.callCount).to.equal(1) | ||
}) | ||
|
||
it('should call custom registrar handle method when registering a protocol handler', async (): Promise<void> => { | ||
const testHandler = (): void => {} | ||
await libp2p.handle('/custom/1.0.0', testHandler) | ||
expect(customRegistrar.handle.callCount).to.equal(1) | ||
expect(customRegistrar.handle.firstCall.args).to.deep.equal(['/custom/1.0.0', testHandler, undefined]) | ||
}) | ||
|
||
it('should call custom registrar unhandle method when unregistering a protocol handler', async (): Promise<void> => { | ||
await libp2p.unhandle('/custom/1.0.0') | ||
expect(customRegistrar.unhandle.callCount).to.equal(1) | ||
expect(customRegistrar.unhandle.firstCall.args).to.deep.equal(['/custom/1.0.0']) | ||
}) | ||
|
||
it('should use custom registrar register method for topologies', async (): Promise<void> => { | ||
const topology = { | ||
onConnect: (): void => {}, | ||
onDisconnect: (): void => {} | ||
} | ||
const topologyId: string = await libp2p.register('/custom/1.0.0', topology) | ||
expect(topologyId).to.equal('custom-topology-id') | ||
expect(customRegistrar.register.callCount).to.equal(1) | ||
expect(customRegistrar.register.firstCall.args).to.deep.equal(['/custom/1.0.0', topology]) | ||
}) | ||
|
||
it('should call custom registrar unregister method for topologies', (): void => { | ||
libp2p.unregister('custom-topology-id') | ||
expect(customRegistrar.unregister.callCount).to.equal(1) | ||
expect(customRegistrar.unregister.firstCall.args).to.deep.equal(['custom-topology-id']) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose this should be in an else block on L143?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially it was an if/else but found it useful to have the default implementation initialised so that it can be passed to a custom registrar. Maybe the initialisation of the default could be handled in the custom one instead?
https://github.com/dozyio/js-libp2p-middleware-registrar/blob/092687470ccd768da81d9d2044ce8ae37ce5715b/src/middleware-registry.ts#L101