1
+ 'use strict' ;
2
+
3
+ Object . defineProperty ( exports , "__esModule" , {
4
+ value : true
5
+ } ) ;
6
+
7
+ var _regenerator = require ( 'babel-runtime/regenerator' ) ;
8
+
9
+ var _regenerator2 = _interopRequireDefault ( _regenerator ) ;
10
+
11
+ var _asyncToGenerator2 = require ( 'babel-runtime/helpers/asyncToGenerator' ) ;
12
+
13
+ var _asyncToGenerator3 = _interopRequireDefault ( _asyncToGenerator2 ) ;
14
+
15
+ var _lodash = require ( 'lodash' ) ;
16
+
17
+ var _lodash2 = _interopRequireDefault ( _lodash ) ;
18
+
19
+ var _http = require ( 'http' ) ;
20
+
21
+ var _http2 = _interopRequireDefault ( _http ) ;
22
+
23
+ require ( 'raf/polyfill' ) ;
24
+
25
+ var _server = require ( './server' ) ;
26
+
27
+ var _server2 = _interopRequireDefault ( _server ) ;
28
+
29
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
30
+
31
+ /**
32
+ * Normalizes a port into a number, string, or false.
33
+ * @param {String } value Port name or number.
34
+ * @return Port number (Number), name (String), or false.
35
+ */
36
+
37
+
38
+ /* Polyfill required by ReactJS. */
39
+ /**
40
+ * Standard web servers.
41
+ */
42
+
43
+ function normalizePort ( value ) {
44
+ var port = _lodash2 . default . toNumber ( value ) ;
45
+ if ( _lodash2 . default . isFinite ( port ) ) return port ; /* port number */
46
+ if ( ! _lodash2 . default . isNumber ( port ) ) return value ; /* named pipe */
47
+ return false ;
48
+ }
49
+
50
+ /**
51
+ * Creates and starts a new webserver.
52
+ * @param {Object } webpackConfig The Webpack config that was used to build the
53
+ * frontend bundle.
54
+ * @param {config } options Additional options:
55
+ * - Application {Function} - Optional. The root ReactJS component of the app
56
+ * to use for server-side rendering;
57
+ * - devMode {Boolean} - Whether the server should be started in dev mode;
58
+ * - favicon {String} - Path of the favicon to be used by the server;
59
+ * - logger {Object} - Optional. The logger to use. By default, the console
60
+ * is used (which is not a good decision performancewise, but it will be
61
+ * changed soon);
62
+ * - beforeRender {Function} - The hook into server-side rendering. It will get
63
+ * incoming request as the argument and it should return a promise that will
64
+ * resolve to the object with the following fields all optional:
65
+ * - config {Object} - Config object to be injected into the page;
66
+ * - extraScripts {Array} - Any additional scripts to be injected into
67
+ * HTML template;
68
+ * - store {Object} - Redux store, which state should be used for server-side
69
+ * rendering, if it is performed, and also injected into HTML template as
70
+ * the initial state.
71
+ * - onExpressJsSetup {Function} - Custom setup of ExpressJS server.
72
+ * - port {String} - Optional. The port to listen (number or name). If not
73
+ * specified the value will be taken from PORT environmental variable, or
74
+ * default to 3000 otherwise.
75
+ * @return {Promise } Resolves to the result object has two fields:
76
+ * - express {Object} - ExpressJS server;
77
+ * - http {Object} - NodeJS HTTP server.
78
+ */
79
+
80
+ exports . default = function ( ) {
81
+ var _ref = ( 0 , _asyncToGenerator3 . default ) ( /*#__PURE__*/ _regenerator2 . default . mark ( function _callee ( webpackConfig , options ) {
82
+ var ops , expressServer , httpServer ;
83
+ return _regenerator2 . default . wrap ( function _callee$ ( _context ) {
84
+ while ( 1 ) {
85
+ switch ( _context . prev = _context . next ) {
86
+ case 0 :
87
+ /* Options normalization. */
88
+ ops = options ? _lodash2 . default . clone ( options ) : { } ;
89
+
90
+ ops . port = normalizePort ( ops . port || process . env . PORT || 3000 ) ;
91
+ _lodash2 . default . defaults ( ops , {
92
+ logger : console
93
+ } ) ;
94
+
95
+ /* Creates servers, resolves and sets the port. */
96
+ _context . next = 5 ;
97
+ return ( 0 , _server2 . default ) ( webpackConfig , ops ) ;
98
+
99
+ case 5 :
100
+ expressServer = _context . sent ;
101
+ httpServer = _http2 . default . createServer ( expressServer ) ;
102
+
103
+ /* Sets error handler for HTTP server. */
104
+
105
+ httpServer . on ( 'error' , function ( error ) {
106
+ if ( error . syscall !== 'listen' ) throw error ;
107
+ var bind = _lodash2 . default . isString ( ops . port ) ? 'Pipe ' + ops . port : 'Port ' + ops . port ;
108
+
109
+ /* Human-readable message for some specific listen errors. */
110
+ switch ( error . code ) {
111
+ case 'EACCES' :
112
+ ops . logger . error ( bind + ' requires elevated privileges' ) ;
113
+ process . exit ( 1 ) ;
114
+ break ;
115
+ case 'EADDRINUSE' :
116
+ ops . logger . error ( bind + ' is already in use' ) ;
117
+ process . exit ( 1 ) ;
118
+ break ;
119
+ default :
120
+ throw error ;
121
+ }
122
+ } ) ;
123
+
124
+ /* Listening event handler for HTTP server. */
125
+ httpServer . on ( 'listening' , function ( ) {
126
+ var addr = httpServer . address ( ) ;
127
+ var bind = _lodash2 . default . isString ( addr ) ? 'pipe ' + addr : 'port ' + addr . port ;
128
+ ops . logger . info ( 'Server listening on ' + bind + ' in ' + process . env . NODE_ENV + ' mode' ) ;
129
+ } ) ;
130
+
131
+ httpServer . listen ( ops . port ) ;
132
+
133
+ return _context . abrupt ( 'return' , {
134
+ expressServer : expressServer ,
135
+ httpServer : httpServer
136
+ } ) ;
137
+
138
+ case 11 :
139
+ case 'end' :
140
+ return _context . stop ( ) ;
141
+ }
142
+ }
143
+ } , _callee , this ) ;
144
+ } ) ) ;
145
+
146
+ function launch ( _x , _x2 ) {
147
+ return _ref . apply ( this , arguments ) ;
148
+ }
149
+
150
+ return launch ;
151
+ } ( ) ;
0 commit comments