all files / modules/middleware/ mapper.js

93.62% Statements 44/47
88.46% Branches 23/26
80% Functions 4/5
93.62% Lines 44/47
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153     28×                                                                                                   14× 14×   14× 14× 24×     24×       16× 16×                                   22×     22× 22× 14× 14×       22×       22×   22×   22×             22×                         21× 21×              
const d = require("describe-property");
const escapeRegExp = require("../utils/escapeRegExp");
 
const {is} = require("ramda");
 
function byMostSpecific(a, b) {
    return b.path.length - a.path.length ||
           (b.host || "").length - (a.host || "").length;
}
 
/**
 * A middleware that provides host and/or location-based routing. Modifies
 * the `basename` connection variable for all downstream apps such that only
 * the portion relevant for dispatch remains in `pathname`.
 *
 *   app.use(mach.mapper, {
 *
 *     'http://example.com/images': function (conn) {
 *       // The hostname used in the request was example.com, and
 *       // the URL path started with "/images". If the request was
 *       // GET /images/avatar.jpg, then conn.pathname is /avatar.jpg
 *     },
 *
 *     '/images': function (conn) {
 *       // The URL path started with "/images"
 *     }
 *
 *   });
 *
 * This function may also be used outside of the context of a middleware
 * stack to create a standalone app. You can either provide mappings one
 * at a time:
 *
 *   let app = mach.mapper();
 *
 *   app.map('/images', function (conn) {
 *     // ...
 *   });
 *
 * Or all at once:
 *
 *   let app = mach.mapper({
 *
 *     '/images': function (conn) {
 *       // ...
 *     }
 *
 *   });
 *
 * Note: Dispatch is done in such a way that the longest paths are tried first
 * since they are the most specific.
 */
function createMapper(app, map) {
  // Allow mach.mapper(map)
    if (typeof app === "object") {
        map = app;
        app = null;
    }
 
    const mappings = [];
 
    function mapper(conn) {
        const hostname = conn.hostname;
        const pathname = conn.pathname;
 
        let mapping, match, remainingPath;
        for (let i = 0, len = mappings.length; i < len; ++i) {
            mapping = mappings[i];
 
      // Try to match the hostname.
            if (mapping.hostname && mapping.hostname !== hostname) {
                continue;
            }
 
      // Try to match the path.
            match = pathname.match(mapping.pattern);
            if (!match) {
                continue;
            }
 
      // Skip if the remaining path doesn't start with a "/".
            remainingPath = match[1];
            Iif (remainingPath.length > 0 && remainingPath[0] !== "/") {
                continue;
            }
 
            conn.basename += mapping.path;
 
            return conn.call(mapping.app);
        }
 
        return conn.call(app);
    }
 
    Object.defineProperties(mapper, {
 
    /**
     * Adds a new mapping that runs the given app when the location used in the
     * request matches the given location.
     */
        map: d(function (location, app) {
            let hostname, path;
 
      // If the location is a fully qualified URL use the host as well.
            const match = location.match(/^https?:\/\/(.*?)(\/.*)/);
            if (match) {
                hostname = match[1].replace(/:\d+$/, ""); // Strip the port.
                path = match[2];
            } else {
                path = location;
            }
 
            Iif (path.charAt(0) !== "/") {
                throw new Error(`Mapping path must start with "/", was "${path}"`);
            }
 
            path = path.replace(/\/$/, "");
 
            const pattern = new RegExp(`^${escapeRegExp(path).replace(/\/+/g, "/+")}(.*)`);
 
            mappings.push({
                hostname,
                path,
                pattern,
                app
            });
 
            mappings.sort(byMostSpecific);
        }),
 
    /**
     * Sets the given app as the default for this mapper.
     */
        run: d(function (downstreamApp) {
            app = downstreamApp;
        })
 
    });
 
  // Allow app.use(mach.mapper, map)
    if (is(Object, map)) {
        for (const location in map) {
            Eif (map.hasOwnProperty(location)) {
                mapper.map(location, map[location]);
            }
        }
    }
 
    return mapper;
}
 
module.exports = createMapper;