diff --git a/src/local-peers.js b/src/local-peers.js index 5cfd1edb0..ebd0309c4 100644 --- a/src/local-peers.js +++ b/src/local-peers.js @@ -152,6 +152,15 @@ class Peer { this.#connected.reject(new PeerFailedConnectionError()) this.#log('disconnected') } + /** + * Exposed for tests. + * @param {Buffer} buf + */ + __sendRawInvite(buf) { + this.#assertConnected() + const messageType = MESSAGE_TYPES.Invite + this.#channel.messages[messageType].send(buf) + } /** @param {Invite} invite */ sendInvite(invite) { this.#assertConnected() @@ -213,6 +222,7 @@ class Peer { * @property {(peerId: string, inviteResponse: InviteResponse) => void} invite-response Emitted when an invite response is received * @property {(peerId: string, details: ProjectJoinDetails) => void} got-project-details Emitted when project details are received * @property {(discoveryKey: Buffer, protomux: Protomux) => void} discovery-key Emitted when a new hypercore is replicated (by a peer) to a peer protomux instance (passed as the second parameter) + * @property {(messageType: string, errorMessage?: string) => void} failed-to-handle-message Emitted when we received a message we couldn't handle for some reason. Primarily useful for testing */ /** @extends {TypedEmitter} */ @@ -301,6 +311,17 @@ export class LocalPeers extends TypedEmitter { peer.sendDeviceInfo(deviceInfo) } + /** + * Used for tests. + * @param {string} deviceId + * @param {Buffer} buf + */ + async __sendRawInvite(deviceId, buf) { + await this.#waitForPendingConnections() + const peer = await this.#getPeerByDeviceId(deviceId) + peer.__sendRawInvite(buf) + } + /** * Connect to a peer over an existing NoiseSecretStream * @@ -391,7 +412,9 @@ export class LocalPeers extends TypedEmitter { message ) } catch (err) { - this.#l.log(`Error handling ${type} message: ${String(err)}`) + const errorMessage = String(err) + this.emit('failed-to-handle-message', type, errorMessage) + this.#l.log(`Error handling ${type} message: ${errorMessage}`) } }, } diff --git a/tests/local-peers.js b/tests/local-peers.js index fed5bb1c2..8c9fc59da 100644 --- a/tests/local-peers.js +++ b/tests/local-peers.js @@ -151,6 +151,28 @@ test('messages to unknown peers', async (t) => { ) }) +test('handles invalid invites', async (t) => { + t.plan(1) + + const r1 = new LocalPeers() + const r2 = new LocalPeers() + + r1.once('peers', async ([peer]) => { + await r1.__sendRawInvite(peer.deviceId, Buffer.from([1, 2, 3])) + }) + + r2.on('invite', () => { + t.fail('should not receive invite') + }) + + r2.once('failed-to-handle-message', (messageType) => { + t.is(messageType, 'Invite') + }) + + const destroy = replicate(r1, r2) + t.teardown(destroy) +}) + test('Disconnected peer shows in state', async (t) => { t.plan(6) const r1 = new LocalPeers()