All files / src/services/handle close-call-handler.ts

100% Statements 19/19
100% Branches 9/9
100% Functions 2/2
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71              15x     15x                                         15x                 17x 17x     4x 4x 4x 1x     1x   3x 1x     1x   2x 2x 1x     1x   1x 1x        
import { CloseCallData } from '../../models/close-call-data';
import { CallRequest } from '../../models/infrasctructure/call-request';
import { Base64 } from '../../utils/base64';
import { Cryptography } from '../../utils/cryptography';
import { Logger } from '../../utils/logger';
import { Utf8 } from '../../utils/utf8';
import { ConnectionService } from '../connection-service';
import { ConnectionState } from '../connection/connection';
import { SessionService } from '../session-service';
import { TimeService } from '../time-service';
import { CallHandler, getCallHandler } from './call-handler';
 
/**
 * Interface for handling 'close' call requests in the WebRTC signaling process.
 * Extends the base CallHandler with connection termination functionality.
 */
export interface CloseCallHandler extends CallHandler<CloseCallData> {}
 
/**
 * Factory function that creates and returns an implementation of the CloseCallHandler interface.
 * Handles incoming requests to terminate WebRTC connections.
 *
 * @param logger - Logger instance for error and debugging information
 * @param timeService - Service for managing time synchronization
 * @param sessionService - Service for accessing session information
 * @param base64 - Utility for Base64 encoding/decoding
 * @param utf8 - Utility for UTF-8 encoding/decoding
 * @param cryptography - Cryptography service for signature verification
 * @param connectionService - Service for managing peer connections
 * @returns An implementation of the CloseCallHandler interface
 */
export function getCloseCallHandler(
    logger: Logger,
    timeService: TimeService,
    sessionService: SessionService,
    base64: Base64,
    utf8: Utf8,
    cryptography: Cryptography,
    connectionService: ConnectionService,
): CloseCallHandler {
    const handler = getCallHandler<CloseCallData>(logger, timeService, sessionService, base64, utf8, cryptography);
    return {
        ...handler,
        handle(request: CallRequest<CloseCallData>): Promise<boolean> {
            const peerPublicKey = request.b.a;
            const connection = connectionService.getConnection(peerPublicKey);
            if (!connection) {
                logger.debug(
                    `[close-call-handler] Incoming call '${request.a}' from ${peerPublicKey} ignored. No one connection is there.`,
                );
                return Promise.resolve(true);
            }
            if (connection.state === ConnectionState.Closed) {
                logger.debug(
                    `[close-call-handler] Incoming call '${request.a}' from ${peerPublicKey} ignored. Connection is not in suitable state (not ${ConnectionState.Closed} expected, ${connection.state} found).`,
                );
                return Promise.resolve(true);
            }
            const connectionOpenedAt = connection.openedAt;
            if (connectionOpenedAt !== undefined && connectionOpenedAt !== null && connectionOpenedAt >= request.b.b) {
                logger.debug(
                    `[close-call-handler] Incoming call '${request.a}' from ${peerPublicKey} ignored. Timestamp is too late.`,
                );
                return Promise.resolve(true);
            }
            connection.close();
            return Promise.resolve(true);
        },
    };
}