This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A topology is a topology, let the registrar worry if it's handling multiple protocols. Also, make the registrar do the work of calling topology methods instead of each topology maintaining its own set of event listeners.
- Loading branch information
1 parent
237efe5
commit 45fcaa1
Showing
38 changed files
with
325 additions
and
527 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
File renamed without changes.
File renamed without changes.
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
9 changes: 9 additions & 0 deletions
9
packages/libp2p-interface-compliance-tests/src/mocks/index.ts
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,9 @@ | ||
|
||
export { mockConnectionGater } from './connection-gater.js' | ||
export { mockConnectionManager } from './connection-manager.js' | ||
export { mockConnection, mockStream, connectionPair } from './connection.js' | ||
export { mockMultiaddrConnection } from './multiaddr-connection.js' | ||
export { mockMuxer } from './muxer.js' | ||
export { mockRegistrar } from './registrar.js' | ||
export { mockUpgrader } from './upgrader.js' | ||
export type { MockUpgraderOptions } from './upgrader.js' |
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
File renamed without changes.
71 changes: 71 additions & 0 deletions
71
packages/libp2p-interface-compliance-tests/src/mocks/registrar.ts
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,71 @@ | ||
import type { Registrar, StreamHandler } from '@libp2p/interfaces/registrar' | ||
import type { Topology } from '@libp2p/interfaces/topology' | ||
|
||
export class MockRegistrar implements Registrar { | ||
private readonly topologies: Map<string, { topology: Topology, protocols: string[] }> = new Map() | ||
private readonly handlers: Map<string, { handler: StreamHandler, protocols: string[] }> = new Map() | ||
|
||
async handle (protocols: string | string[], handler: StreamHandler) { | ||
if (!Array.isArray(protocols)) { | ||
protocols = [protocols] | ||
} | ||
|
||
const id = `handler-id-${Math.random()}` | ||
|
||
this.handlers.set(id, { | ||
handler, | ||
protocols | ||
}) | ||
|
||
return id | ||
} | ||
|
||
async unhandle (id: string) { | ||
this.handlers.delete(id) | ||
} | ||
|
||
getHandlers (protocol: string) { | ||
const output: StreamHandler[] = [] | ||
|
||
for (const { handler, protocols } of this.handlers.values()) { | ||
if (protocols.includes(protocol)) { | ||
output.push(handler) | ||
} | ||
} | ||
|
||
return output | ||
} | ||
|
||
register (protocols: string | string[], topology: Topology) { | ||
if (!Array.isArray(protocols)) { | ||
protocols = [protocols] | ||
} | ||
|
||
const id = `topology-id-${Math.random()}` | ||
|
||
this.topologies.set(id, { | ||
topology, | ||
protocols | ||
}) | ||
|
||
return id | ||
} | ||
|
||
unregister (id: string | string[]) { | ||
if (!Array.isArray(id)) { | ||
id = [id] | ||
} | ||
|
||
id.forEach(id => this.topologies.delete(id)) | ||
} | ||
|
||
getTopologies (protocol: string) { | ||
const output: Topology[] = [] | ||
|
||
return output | ||
} | ||
} | ||
|
||
export function mockRegistrar () { | ||
return new MockRegistrar() | ||
} |
4 changes: 2 additions & 2 deletions
4
...mpliance-tests/src/utils/mock-upgrader.ts → ...ce-compliance-tests/src/mocks/upgrader.ts
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
136 changes: 0 additions & 136 deletions
136
packages/libp2p-interface-compliance-tests/src/topology/multicodec-topology.ts
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.