From 964cb809b79f6ed15c3a59841ecc7decd6423fc6 Mon Sep 17 00:00:00 2001 From: Evan Hahn Date: Sun, 25 Feb 2024 16:29:58 -0600 Subject: [PATCH] chore: invite API's membership check can be synchronous (#480) This change should have no user impact. This function doesn't do anything async so we can make it synchronous. I think this is a useful change on its own but will make upcoming changes a bit simpler. --- src/invite-api.js | 15 +++++---------- src/mapeo-manager.js | 2 +- tests/invite-api.js | 26 +++++++++++++------------- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/invite-api.js b/src/invite-api.js index f162b6568..c97a5844c 100644 --- a/src/invite-api.js +++ b/src/invite-api.js @@ -49,7 +49,7 @@ export class InviteApi extends TypedEmitter { * @param {Object} options * @param {import('./local-peers.js').LocalPeers} options.rpc * @param {object} options.queries - * @param {(projectId: string) => Promise} options.queries.isMember + * @param {(projectId: string) => boolean} options.queries.isMember * @param {(invite: import('./generated/rpc.js').Invite) => Promise} options.queries.addProject */ constructor({ rpc, queries }) { @@ -58,21 +58,16 @@ export class InviteApi extends TypedEmitter { this.#isMember = queries.isMember this.#addProject = queries.addProject - this.rpc.on('invite', (peerId, invite) => { - this.#handleInvite(peerId, invite).catch(() => { - /* c8 ignore next */ - // TODO: Log errors, but otherwise ignore them, but can't think of a reason there would be an error here - }) - }) + this.rpc.on('invite', this.#handleInvite) } /** * @param {string} peerId * @param {import('./generated/rpc.js').Invite} invite */ - async #handleInvite(peerId, invite) { + #handleInvite = (peerId, invite) => { const projectId = projectKeyToId(invite.projectKey) - const isAlreadyMember = await this.#isMember(projectId) + const isAlreadyMember = this.#isMember(projectId) if (isAlreadyMember) { this.#sendAlreadyResponse({ peerId, projectId }) @@ -107,7 +102,7 @@ export class InviteApi extends TypedEmitter { async accept(projectPublicId) { const projectId = this.#getProjectIdFromPublicId(projectPublicId) - const isAlreadyMember = await this.#isMember(projectId) + const isAlreadyMember = this.#isMember(projectId) const peersToRespondTo = this.#peersToRespondTo.get(projectId) if (isAlreadyMember) { diff --git a/src/mapeo-manager.js b/src/mapeo-manager.js index c1120a8ea..ed0992fc4 100644 --- a/src/mapeo-manager.js +++ b/src/mapeo-manager.js @@ -153,7 +153,7 @@ export class MapeoManager extends TypedEmitter { this.#invite = new InviteApi({ rpc: this.#localPeers, queries: { - isMember: async (projectId) => { + isMember: (projectId) => { const projectExists = this.#db .select() .from(projectKeysTable) diff --git a/tests/invite-api.js b/tests/invite-api.js index 39b229a70..38221029c 100644 --- a/tests/invite-api.js +++ b/tests/invite-api.js @@ -22,7 +22,7 @@ test('invite-received event has expected payload', async (t) => { const inviteApi = new InviteApi({ rpc: r2, queries: { - isMember: async (projectId) => { + isMember: (projectId) => { return projects.has(projectId) }, addProject: async (invite) => { @@ -77,7 +77,7 @@ test('Accept invite', async (t) => { const inviteApi = new InviteApi({ rpc: r2, queries: { - isMember: async (projectId) => { + isMember: (projectId) => { const projectKey = Buffer.from(projectId, 'hex') return projects.has(projectKeyToPublicId(projectKey)) }, @@ -125,7 +125,7 @@ test('Reject invite', async (t) => { const inviteApi = new InviteApi({ rpc: r2, queries: { - isMember: async (projectId) => { + isMember: (projectId) => { const projectKey = Buffer.from(projectId, 'hex') return projects.has(projectKeyToPublicId(projectKey)) }, @@ -172,7 +172,7 @@ test('Receiving invite for project that peer already belongs to', async (t) => { const inviteApi = new InviteApi({ rpc: r2, queries: { - isMember: async () => { + isMember: () => { return true }, addProject: async () => { @@ -220,7 +220,7 @@ test('Receiving invite for project that peer already belongs to', async (t) => { const inviteApi = new InviteApi({ rpc: r2, queries: { - isMember: async () => { + isMember: () => { return isMember }, addProject: async () => { @@ -270,7 +270,7 @@ test('Receiving invite for project that peer already belongs to', async (t) => { const inviteApi = new InviteApi({ rpc: r2, queries: { - isMember: async (projectId) => { + isMember: (projectId) => { return projects.has(projectId) }, addProject: async (invite) => { @@ -314,7 +314,7 @@ test('trying to accept or reject non-existent invite throws', async (t) => { const inviteApi = new InviteApi({ rpc, queries: { - isMember: async () => true, + isMember: () => true, addProject: async () => {}, }, }) @@ -336,7 +336,7 @@ test('invitor disconnecting results in accept throwing', async (t) => { const inviteApi = new InviteApi({ rpc: r2, queries: { - isMember: async () => false, + isMember: () => false, addProject: async () => { t.fail('should not try to add project if could not accept') }, @@ -378,7 +378,7 @@ test('invitor disconnecting results in invite reject response not throwing', asy const inviteApi = new InviteApi({ rpc: r2, queries: { - isMember: async () => false, + isMember: () => false, addProject: async () => {}, }, }) @@ -420,7 +420,7 @@ test('invitor disconnecting results in invite already response not throwing', as const inviteApi = new InviteApi({ rpc: r2, queries: { - isMember: async () => { + isMember: () => { return isMember }, addProject: async () => {}, @@ -463,7 +463,7 @@ test('addProject throwing results in invite accept throwing', async (t) => { const inviteApi = new InviteApi({ rpc: r2, queries: { - isMember: async () => false, + isMember: () => false, addProject: async () => { throw new Error('Failed to add project') }, @@ -503,7 +503,7 @@ test('Invite from multiple peers', async (t) => { const inviteApi = new InviteApi({ rpc: invitee, queries: { - isMember: async (projectId) => { + isMember: (projectId) => { const projectKey = Buffer.from(projectId, 'hex') return projects.has(projectKeyToPublicId(projectKey)) }, @@ -570,7 +570,7 @@ test.skip('Invite from multiple peers, first disconnects before accepted, receiv const inviteApi = new InviteApi({ rpc: invitee, queries: { - isMember: async (projectId) => { + isMember: (projectId) => { return projects.has(projectId) }, addProject: async (invite) => {