-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(rosenet-node): extract RoseNet direct protocol handler
- Loading branch information
1 parent
8883026
commit 7addf22
Showing
3 changed files
with
60 additions
and
46 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
51 changes: 51 additions & 0 deletions
51
packages/rosenet-node/lib/rosenet-direct/handleIncomingMessage.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,51 @@ | ||
import { pipe } from 'it-pipe'; | ||
|
||
import { Libp2p } from '@libp2p/interface'; | ||
|
||
import RoseNetNodeContext from '../context/RoseNetNodeContext'; | ||
|
||
import { decode } from '../utils/codec'; | ||
|
||
import { ACK_BYTE, ROSENET_DIRECT_PROTOCOL_V1 } from '../constants'; | ||
|
||
/** | ||
* protocol handler for RoseNet direct | ||
*/ | ||
const handleIncomingMessageFactory = | ||
(node: Libp2p) => (handler: (from: string, message?: string) => void) => { | ||
node.handle( | ||
ROSENET_DIRECT_PROTOCOL_V1, | ||
async ({ connection, stream }) => { | ||
RoseNetNodeContext.logger.debug( | ||
`incoming connection stream with protocol ${ROSENET_DIRECT_PROTOCOL_V1}`, | ||
{ | ||
remoteAddress: connection.remoteAddr.toString(), | ||
transient: connection.transient, | ||
}, | ||
); | ||
pipe( | ||
stream, | ||
decode, | ||
async function* (source) { | ||
for await (const message of source) { | ||
RoseNetNodeContext.logger.debug( | ||
'message received, calling handler and sending ack', | ||
{ | ||
message, | ||
}, | ||
); | ||
handler(connection.remotePeer.toString(), message); | ||
yield Uint8Array.of(ACK_BYTE); | ||
} | ||
}, | ||
stream, | ||
); | ||
}, | ||
{ runOnTransientConnection: true }, | ||
); | ||
RoseNetNodeContext.logger.debug( | ||
`handler for ${ROSENET_DIRECT_PROTOCOL_V1} protocol set`, | ||
); | ||
}; | ||
|
||
export default handleIncomingMessageFactory; |
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,2 @@ | ||
export { default as handleIncomingMessageFactory } from './handleIncomingMessage'; | ||
export { default as sendMessageFactory } from './sendMessage'; |