All files / skeleton/modules localServer.js

84.38% Statements 54/64
77.27% Branches 17/22
83.33% Functions 10/12
84.38% Lines 54/64
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205                                            44x 44x 44x 44x 44x 44x 44x   44x 44x 44x                     98x 98x 98x                           98x 98x 98x   98x 54x 54x     98x                                         98x             126x 126x     126x           3x   126x     98x     98x     98x   48x         98x       98x   98x   98x   98x                         98x 98x         98x       98x 1x   97x     98x 98x                     98x 98x 98x                 98x 98x 98x 54x   44x     98x 98x               1x  
import http from 'http';
import connect from 'connect';
import serveStatic from 'serve-static';
import modRewrite from 'connect-modrewrite';
import findPort from 'find-port';
import enableDestroy from 'server-destroy';
import url from 'url';
import path from 'path';
import fs from 'fs-plus';
 
/**
 * Simple local HTTP server tailored for meteor app bundle.
 *
 * @param {Object} log - Logger instance
 * @param app
 *
 * @property {Array} errors
 * @constructor
 */
export default class LocalServer {
 
    constructor(log) {
        this.log = log.loggers.get('localServer');
        this.httpServerInstance = null;
        this.server = null;
        this.retries = 0;
        this.maxRetries = 3;
        this.serverPath = '';
        this.parentServerPath = '';
 
        this.errors = [];
        this.errors[0] = 'Could not find free port.';
        this.errors[1] = 'Could not start http server.';
    }
 
    /**
     * Sets refs for the callbacks.
     *
     * @param {function} onStartupFailed
     * @param {function} onServerReady
     * @param {function} onServerRestarted
     */
    setCallbacks(onStartupFailed, onServerReady, onServerRestarted) {
        this.onStartupFailed = onStartupFailed;
        this.onServerReady = onServerReady;
        this.onServerRestarted = onServerRestarted;
    }
 
    /**
     * Initializes the module. Configures `connect` and searches for free port.
     *
     * @param {string} serverPath       - path for the resources to serve
     * @param {string} parentServerPath - path for the parent resources
     * @param {boolean} restart         - are we restarting the server?
     * @param {boolean} randomPort      - whether to choose a random port from those found
     *                                    to be free
     */
    init(serverPath, parentServerPath, restart, randomPort = true) {
        // `connect` will do the job!
        const server = connect();
        this.serverPath = serverPath;
        this.parentServerPath = parentServerPath;
 
        if (restart) {
            Eif (this.httpServerInstance) {
                this.httpServerInstance.destroy();
            }
        }
        this.log.info('will serve from: ', serverPath, parentServerPath);
 
        // Here, instead of reading the manifest and serving assets based on urls defined there,
        // we are making a shortcut implementation which is just doing a simple regex rewrite to
        // the urls.
 
        // TODO: is serving on actual manifest better in any way? or faster?
        // Answer 1: It would be better to have it so we would not have to check for a sourcemap
        // file existence.
        // Answer 2: We can not set a proper Cache header without manifest.
        // Answer 3: We will still serve files that have been deleted in the new version - hard
        // to say if that is a real problem.
 
        /**
         * Everything that is:
         * - not starting with `app` or `packages`
         * - not a merged-stylesheets.css
         * - not with `meteor_[js/css]_resource` in the name
         * - not a cordova.js file
         * should be taken from /app/ path.
         */
        server.use(modRewrite([
            '^/favicon.ico [R=404,L,NS]',
            '^/(?!($|app|packages|merged-stylesheets.css|.*meteor_(?:js|css)_resource|' +
            'cordova.js))(.*) /app/$2'
        ]));
 
        function setSourceMapHeader(req, res, next) {
            const parsedUrl = url.parse(req.url);
            const ext = path.extname(parsedUrl.pathname);
            // Now here it would be very useful to actually read the manifest and server sourcemaps
            // according to it. For now just checking if a sourcemap for a file exits.
            if ((ext === '.js' || ext === '.css') && (
                    fs.existsSync(path.join(serverPath, `${parsedUrl.pathname}.map`)) ||
                    (parentServerPath &&
                    fs.existsSync(path.join(parentServerPath, `${parsedUrl.pathname}.map`)))
                )
            ) {
                res.setHeader('X-SourceMap', `${parsedUrl.pathname}.map?${parsedUrl.query}`);
            }
            next();
        }
 
        server.use(setSourceMapHeader);
 
        // Serve files as static from the main directory.
        server.use(serveStatic(serverPath),
            {});
 
        if (parentServerPath) {
            // Server files from the parent directory as the main bundle has only changed files.
            server.use(serveStatic(parentServerPath),
                {});
        }
 
        // As last resort we will serve index.html.
        server.use(modRewrite([
            '^(.*) /index.html'
        ]));
 
        server.use(serveStatic(serverPath), {});
 
        this.server = server;
 
        this.findPort(randomPort)
            .then(() => {
                this.startHttpServer(restart);
            })
            .catch(() => {
                this.log.error('could not find free port');
                this.onStartupFailed(0);
            });
    }
 
    /**
     * Checks if we have a free port.
     * @returns {Promise}
     */
    findPort(randomPort) {
        return new Promise((resolve, reject) => {
            findPort(
                '127.0.0.1',
                8034,
                8063,
                (ports) => {
                    Iif (ports.length === 0) {
                        reject();
                    }
 
                    if (randomPort) {
                        this.port = ports[Math.floor(Math.random() * (ports.length - 1))];
                    } else {
                        this.port = ports[0];
                    }
 
                    this.log.info(`assigned port ${this.port}`);
                    resolve();
                }
            );
        });
    }
 
    /**
     * Tries to start the http server.
     * @param {bool} restart - is this restart
     */
    startHttpServer(restart) {
        try {
            this.httpServerInstance = http.createServer(this.server);
            this.httpServerInstance.on('error', (e) => {
                this.log.error(e);
                this.retries += 1;
                if (this.retries < this.maxRetries) {
                    this.init(this.serverPath, this.parentServerPath, true);
                } else {
                    this.onStartupFailed(1);
                }
            });
            this.httpServerInstance.on('listening', () => {
                this.retries = 0;
                if (restart) {
                    this.onServerRestarted(this.port);
                } else {
                    this.onServerReady(this.port);
                }
            });
            this.httpServerInstance.listen(this.port, '127.0.0.1');
            enableDestroy(this.httpServerInstance);
        } catch (e) {
            this.log.error(e);
            this.onStartupFailed(1);
        }
    }
}
 
module.exports = LocalServer;