Skip to content

Commit 59ee0a2

Browse files
committed
Reduce cognitive complexity
1 parent 935632b commit 59ee0a2

File tree

1 file changed

+59
-21
lines changed

1 file changed

+59
-21
lines changed

src/rendezvous/MSC3906Rendezvous.ts

+59-21
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ export class MSC3906Rendezvous {
134134
this._code = JSON.stringify(raw);
135135
}
136136

137+
/**
138+
*
139+
* @returns the checksum of the secure channel if the rendezvous set up was successful, otherwise undefined
140+
*/
137141
public async startAfterShowingCode(): Promise<string | undefined> {
138142
const checksum = await this.channel.connect();
139143

@@ -158,17 +162,43 @@ export class MSC3906Rendezvous {
158162
});
159163

160164
logger.info("Waiting for other device to chose protocol");
161-
const { type, protocol, outcome, reason, intent } = await this.receive();
165+
const nextPayload = await this.receive();
166+
167+
this.checkForV1Fallback(nextPayload);
162168

169+
const protocol = this.v1FallbackEnabled
170+
? await this.handleV1ProtocolPayload(nextPayload)
171+
: await this.handleV2ProtocolPayload(nextPayload);
172+
173+
// invalid protocol
174+
if (!protocol || !LOGIN_TOKEN_PROTOCOL.matches(protocol)) {
175+
await this.cancel(RendezvousFailureReason.UnsupportedAlgorithm);
176+
return undefined;
177+
}
178+
179+
return checksum;
180+
}
181+
182+
private checkForV1Fallback({ type }: MSC3906RendezvousPayload): void {
163183
// even if we didn't start in v1 fallback we might detect that the other device is v1
164184
if (type === PayloadType.Finish || type === PayloadType.Progress) {
165185
// this is a PDU from a v1 flow so use fallback mode
166186
this.v1FallbackEnabled = true;
167187
}
188+
}
168189

169-
// fallback for v1 flow
190+
/**
191+
*
192+
* @returns true if the protocol was received successfully, false otherwise
193+
*/
194+
private async handleV1ProtocolPayload({
195+
type,
196+
protocol,
197+
outcome,
198+
reason,
199+
intent,
200+
}: MSC3906RendezvousPayload): Promise<string | void> {
170201
if (type === PayloadType.Finish) {
171-
this.v1FallbackEnabled = true;
172202
// new device decided not to complete
173203
let reason: RendezvousFailureReason;
174204
if (intent) {
@@ -182,9 +212,29 @@ export class MSC3906Rendezvous {
182212
reason = RendezvousFailureReason.Unknown;
183213
}
184214
await this.cancel(reason);
185-
return undefined;
215+
return;
216+
}
217+
218+
// unexpected payload
219+
if (type !== PayloadType.Progress) {
220+
await this.cancel(RendezvousFailureReason.Unknown);
221+
return;
186222
}
187223

224+
return protocol;
225+
}
226+
227+
/**
228+
*
229+
* @returns true if the protocol was received successfully, false otherwise
230+
*/
231+
private async handleV2ProtocolPayload({
232+
type,
233+
protocol,
234+
outcome,
235+
reason,
236+
intent,
237+
}: MSC3906RendezvousPayload): Promise<string | void> {
188238
// v2 flow
189239
if (type === PayloadType.Failure) {
190240
// new device decided not to complete
@@ -206,28 +256,16 @@ export class MSC3906Rendezvous {
206256
failureReason = RendezvousFailureReason.Unknown;
207257
}
208258
await this.cancel(failureReason);
209-
return undefined;
210-
}
211-
212-
// v1 unexpected payload
213-
if (this.v1FallbackEnabled && type !== PayloadType.Progress) {
214-
await this.cancel(RendezvousFailureReason.Unknown);
215-
return undefined;
259+
return;
216260
}
217261

218-
// v2 unexpected payload
219-
if (!this.v1FallbackEnabled && type !== PayloadType.Protocol) {
262+
// unexpected payload
263+
if (type !== PayloadType.Protocol) {
220264
await this.cancel(RendezvousFailureReason.Unknown);
221-
return undefined;
222-
}
223-
224-
// invalid protocol
225-
if (!protocol || !LOGIN_TOKEN_PROTOCOL.matches(protocol)) {
226-
await this.cancel(RendezvousFailureReason.UnsupportedAlgorithm);
227-
return undefined;
265+
return;
228266
}
229267

230-
return checksum;
268+
return protocol;
231269
}
232270

233271
private async receive(): Promise<MSC3906RendezvousPayload> {

0 commit comments

Comments
 (0)