All files utils.js

86.36% Statements 76/88
77.94% Branches 53/68
94.74% Functions 18/19
89.02% Lines 73/82
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 2602x 2x 2x 2x 2x 2x 2x   2x 2x   2x         2x               2x     5x         3x   3x 3x   3x           12x   12x   12x 12x 12x                     10x                                 10x   9x         3x                                         10x   10x 10x     10x 10x 10x 10x 10x 10x   10x   10x                     10x   10x       10x   10x 1x 1x 1x 1x       9x           9x 1x 1x 1x     8x           8x   8x       8x 8x               1x         8x         8x   8x                           3x       2x           9x 2x   7x     9x                                 2x   1x   1x             1x           2x 2x 2x 2x
var path = require("path");
var Provider = require("react-redux").Provider;
var WrapperProvider = require("./wrapper").WrapperProvider;
var renderToString = require("react-dom/server").renderToString;
var React = require("react");
var ReactRouter = require("react-router");
var lib = require("./lib");
 
var match = ReactRouter.match;
var RouterContext = ReactRouter.RouterContext;
 
var roots = [
    '/',
    '/index.html'
];
 
var httpCodes = {
    redirect: 301,
    ok: 200,
    notFound: 404,
    internalServerError: 500,
    notImplemented: 501
};
 
var pollInterval = 200;
 
function isRedirect(res) {
    return res.statusCode === httpCodes.redirect;
}
 
function setErrorStatus(res, e) {
 
    e.code = e.code || httpCodes.internalServerError;
 
    res.statusMessage = e.message || e;
    res.status(e.code);
 
    return e;
 
}
 
function waitForTemplate(options) {
 
    return (new Promise(function(resolve) {
 
        var interval = setInterval(function() {
 
            Eif (options.fs.existsSync(options.templatePath)) {
                clearInterval(interval);
                resolve(options.fs.readFileSync(options.templatePath).toString());
            }
 
        }, pollInterval);
 
    }));
 
}
 
function renderHTML(config, options) {
 
    var parsedTemplate = options.template({
        component: config.component,
        error: config.error,
        html: config.html,
        initialProps: config.initialProps,
        store: config.store,
        renderProps: config.renderProps,
        req: config.req,
        res: config.res,
        template: config.template.replace(
            '<head>', // this should be the first script on a page so that others can pick it up
            '<head>' +
            '<script type="text/javascript">window["' + options.initialStateKey + '"] = ' + JSON.stringify(config.store ? config.store.getState() : undefined) + ';</script>' +
            '<script type="text/javascript">window["' + options.initialPropsKey + '"] = ' + JSON.stringify(config.initialProps) + ';</script>'
        )
    });
 
    if (typeof parsedTemplate !== 'string') throw new Error('Return type of options.template() has to be a string');
 
    return parsedTemplate;
 
}
 
function errorTemplate(config) {
    return (
        "<h1>" + config.error.code + " Server Error</h1>" +
        "<pre>" + (config.error.stack || config.error) + "</pre>"
    );
}
 
function defaultTemplate(config) {
 
    var error = config.error ? ('<div id="server-error">' + errorTemplate(config) + '</div>') : '';
 
    return config.template.replace(
        '<div id="root"></div>',
        error + '<div id="root">' + config.html + '</div>'
    );
 
}
 
function middleware(options, routes, history, template, req, res, next) {
 
    var Cmp, renderProps, store, initialProps; // these vars are passes all the way
 
    return (new Promise(function performRouting(resolve, reject) {
 
        ['createRoutes'].forEach((k) => {
            Iif (!options[k]) throw new Error('Mandatory option not defined: ' + k);
        });
 
        options.initialStateKey = options.initialStateKey || '__INITIAL__STATE__';
        options.initialPropsKey = options.initialPropsKey || '__INITIAL__PROPS__';
        options.createStore = options.createStore || null;
        options.outputPath = options.outputPath || path.join(process.cwd(), 'build');
        options.templatePath = options.templatePath || path.join(options.outputPath, 'index.html');
        options.template = options.template || defaultTemplate;
 
        var location = history.createLocation(req.url);
 
        Iif (
            options.fs.existsSync(path.join(options.outputPath, location.pathname)) &&
            !~roots.indexOf(location.pathname)
        ) {
            if (options.debug) console.log('Static', location.pathname);
            next();
            var e = new Error('Static');
            e.static = true;
            return reject(e);
        }
 
        match({routes: routes, location: location}, function(error, redirectLocation, props) {
 
            renderProps = props;
 
            var e;
 
            Iif (options.debug) console.log('Rendering', location.pathname + location.search);
 
            if (redirectLocation) {
                res.redirect(httpCodes.redirect, redirectLocation.pathname + redirectLocation.search);
                e = new Error('Redirect');
                e.code = httpCodes.redirect;
                return reject(e);
            }
 
            // TODO Find how to test
            Iif (error) {
                error.code = httpCodes.notImplemented;
                return reject(error);
            }
 
            // This is hard 404 which means router failed to load any page
            if (!renderProps) {
                e = new Error('Route Not Found');
                e.code = httpCodes.notFound;
                return reject(e);
            }
 
            resolve();
 
        });
 
    })).then(function getInitialPropsOfComponent() {
 
        store = options.createStore ? options.createStore({req: req, res: res}) : null;
 
        Cmp = renderProps.components[renderProps.components.length - 1]; // it used to be .WrappedComponent but it's useless, users can find it themselves if needed
 
        // console.log('Found Component', Cmp.getInitialProps);
 
        return new Promise(function(resolve) {
            resolve((Cmp && Cmp.getInitialProps) ? Cmp.getInitialProps({
                location: renderProps.location,
                params: renderProps.params,
                req: req,
                res: res,
                store: store
            }) : null);
        }).catch(function(e) {
            return {initialError: e.message || e.toString()};
        });
 
    }).then(function renderApp(props) {
 
        initialProps = props || {}; // client relies on truthy value of server-rendered props
 
        // console.log('Setting context initial props', initialProps);
        // console.log('Store state before rendering', store.getState());
 
        var routerContext = React.createElement(RouterContext, renderProps);
 
        return {
            html: renderToString(
                React.createElement(
                    WrapperProvider,
                    {initialProps: initialProps},
                    (store
                        ? React.createElement(Provider, {store: store}, routerContext)
                        : routerContext)
                )
            )
        };
 
    }).catch(function renderErrorHandler(e) {
 
        if (isRedirect(res) || e.static) throw e;
 
        // If we end up here it means server-side error that can't be handled by application
        // By returning an object we are recovering from error
        return {
            error: e
        };
 
    }).then(function renderAndSendHtml(result) {
 
        if (result.error) {
            result.error = setErrorStatus(res, result.error);
        } else {
            res.status(Cmp && !Cmp.notFound ? httpCodes.ok : httpCodes.notFound);
        }
 
        res.send(renderHTML(
            lib.extends({
                component: Cmp,
                error: null,
                initialProps: initialProps,
                html: '',
                renderProps: renderProps,
                req: req,
                res: res,
                store: store,
                template: template
            }, result), // appends error or html
            options
        ));
 
    }).catch(function finalErrorHandler(e) {
 
        if (isRedirect(res) || e.static) return;
 
        e = setErrorStatus(res, e);
 
        res.send(errorTemplate({
            error: e,
            req: req,
            res: res,
            template: template
        }));
 
        return e; // re-throw to make it unhandled?
 
    });
 
}
 
exports.middleware = middleware;
exports.errorTemplate = errorTemplate;
exports.renderHTML = renderHTML;
exports.waitForTemplate = waitForTemplate;