# All files / src utils.ts

33.33% Statements 10/30
100% Branches 0/0
0% Functions 0/2
33.33% Lines 10/30

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
    311x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

     // @ts-nocheck
    const HEADERS = ['range', 'if-range'];

    /**
     * Create a function that add X-Forwarded Headers to superagent requests
     * @function addHeadersFactory
     * @param {Object} req Original request object
     * @return {function} Superagent request function
     */
    export const addHeadersFactory = (orig) => {
      return (request) => {
        const x_forwarded_host = orig.headers['x-forwarded-host'] || orig.hostname;
        const x_forwarded_for = orig.headers['x-forwarded-for'];
        const remote_host = orig.connection.remoteAddress;
        if (x_forwarded_for && remote_host) {
          request.set('x-forwarded-for', x_forwarded_for + ', ' + remote_host);
        } else if (remote_host) {
          request.set('x-forwarded-for', remote_host);
        } else if (x_forwarded_for) {
          request.set('x-forwarded-for', x_forwarded_for);
        }
        x_forwarded_host && request.set('x-forwarded-host', x_forwarded_host);
        // forward additional headers
        HEADERS.forEach((header) => {
          if (orig.headers[header]) {
            request.set(header, orig.headers[header]);
          }
        });
      };
    };