Ejemplo n.º 1
0
    // PUBLIC METHODS
    // --------------------------------------------------------------------------------------------
    send(noticeOrNotices: Notice | Notice[]) {
        validate(noticeOrNotices, 'Cannot send notices: notices are undefined');
        const notices = Array.isArray(noticeOrNotices) ? noticeOrNotices : [noticeOrNotices];
        if (notices.length === 0) return Promise.resolve();

        const start = process.hrtime();
        this.logger && this.logger.debug(`Sending (${notices.length}) notices...`);
        for (let notice of notices) {
            if (!notice) continue;
            
            const topic = notice.topic ? notice.topic : '/';
            this.logger && this.logger.debug(`Sending ${topic}:${notice.event} notice to (${notice.target}) target`);
            this.server.of(topic).in(notice.target).emit(notice.event, notice.payload);
        }

        // log the notice sent event
        this.logger && this.logger.log(`Notices Sent`, {
            count   : notices.length,
            time    : util.since(start)
        });
        
        // return success
        return Promise.resolve();
    }
Ejemplo n.º 2
0
    return function (request: Request, response: Response, next: (error?: Error) => void) {

        const start = process.hrtime();

        // log the request
        logger && logger.request(request, response);

        // set basic headers
        onHeaders(response, function(this: Response) {
            this.setHeader(headers.SERVER_NAME, name);
            this.setHeader(headers.API_VERSION, version);
            this.setHeader(headers.RESPONSE_TIME, Math.round(since(start)).toString());
        });

        // parse URL
        if (request._parsedUrl) {
            request.path = request._parsedUrl.pathname;
            request.query = qs.parse(request._parsedUrl.query);
        }
        else {
            const parsedUrl = url.parse(request.url, true);
            request.path = parsedUrl.pathname;
            request.query = parsedUrl.query;
        }

        // get IP address of the request
        const ip: string = proxyaddr(request, trustFunction);
        if (typeof ip === 'string') {
            if (ip === '::1') {
                // localhost
                request.ip = '127.0.0.1';
            }
            else {
                const ipV4 = matchIpV4(ip);
                request.ip = ipV4 ? ipV4 : ip;
            }
        }

        // continue to the next handler
        next();
    }