All files / src PerfectWSSubRoute.ts

94.73% Statements 54/57
88.23% Branches 15/17
100% Functions 7/7
94.73% Lines 54/57

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 72 73  2x   2x 85x 85x 85x 85x 85x 85x 85x 85x   85x 85x 85x   85x 118x 118x   118x 118x 118x 118x   85x 1x 1x     1x   85x 35x 37x 18x 37x 19x 19x 37x 35x   85x 133x 133x         85x 80x   80x 116x 116x   80x   80x 18x 1x 1x   17x 17x 17x 17x 17x   79x 80x 85x
import { WSListenCallback, PerfectWS } from "./PerfectWS.js";
import { PerfectWSError } from "./PerfectWSError.js";
 
export class PerfectWSSubRoute {
    public _listenForRequests = new Map<string, { method: string; callbacks: WSListenCallback[] }>();
    private _protocol: PerfectWS<any> | null = null;
    private _routesToConnect: [string, WSListenCallback[]][] = [];
    private _routersToConnect: PerfectWSSubRoute[] = [];
    private _prefix: string = '';
    private _parentPrefix: string = '';
    private _middleware: WSListenCallback[] = [];
    private _parentConnected = false;
 
    constructor(prefix: string = '') {
        this._prefix = prefix;
    }
 
    on(method: string, ...callbacks: WSListenCallback[]) {
        this._listenForRequests.set(method, { method, callbacks });
        if (this._protocol) {
            this._protocol.on(this._getFullPrefix(method), ...this._middleware.concat(callbacks));
        } else {
            this._routesToConnect.push([method, callbacks]);
        }
    }
 
    off(method: string) {
        this._listenForRequests.delete(method);
        if (this._protocol) {
            this._protocol.off(method);
        }
    }
 
    use(...routers: (PerfectWSSubRoute | WSListenCallback)[]): void {
        for (const router of routers) {
            if (router instanceof PerfectWSSubRoute) {
                this._routersToConnect.push(router);
            } else {
                this._middleware.push(router);
            }
        }
    }
 
    private _getFullPrefix(method: string = '') {
        return this._parentPrefix + this._prefix + method;
    }
 
    /**
     * @internal
     */
    __connect(protocol: PerfectWS<any, any>) {
        this._protocol = protocol;
 
        for (const [method, callbacks] of this._routesToConnect) {
            this._protocol.on(this._getFullPrefix(method), ...this._middleware.concat(callbacks));
        }
 
        this._routesToConnect.length = 0;
 
        for (const router of this._routersToConnect) {
            if(router._parentConnected){
                throw new PerfectWSError('This subroute is already connected to a parent subroute', 'subrouteAlreadyConnected');
            }
 
            router._parentPrefix = this._getFullPrefix();
            router._middleware = this._middleware.concat(router._middleware);
            router._parentConnected = true;
            router.__connect(protocol);
        }
 
        this._routersToConnect.length = 0;
    }
}