17
17
'use strict' ;
18
18
19
19
const assert = require ( 'assert' ) ;
20
- const renderer = require ( './renderer' ) ;
21
- const chromeLauncher = require ( 'chrome-launcher' ) ;
22
- const express = require ( 'express' ) ;
23
20
const fs = require ( 'fs' ) ;
24
- const compression = require ( 'compression' ) ;
25
- const path = require ( 'path' ) ;
26
21
const https = require ( 'https' ) ;
27
- const app = express ( ) ;
28
- const cache = require ( './cache' ) ;
22
+ const path = require ( 'path' ) ;
23
+ const url = require ( 'url' ) ;
24
+ const chromeLauncher = require ( 'chrome-launcher' ) ;
25
+ const compression = require ( 'compression' ) ;
26
+ const express = require ( 'express' ) ;
29
27
const now = require ( 'performance-now' ) ;
30
28
const uuidv4 = require ( 'uuid/v4' ) ;
29
+ const cache = require ( './cache' ) ;
30
+ const renderer = require ( './renderer' ) ;
31
+
32
+ const app = express ( ) ;
33
+
34
+ const CONFIG_PATH = path . resolve ( __dirname , '../config.json' ) ;
35
+ const PROGRESS_BAR_PATH = path . resolve ( __dirname , '../node_modules/progress-bar-element/progress-bar.html' ) ;
36
+ const PORT = process . env . PORT || '3000' ;
31
37
32
- // Load config from config.json if it exists.
33
38
let config = { } ;
34
- const configPath = path . resolve ( __dirname , '../config.json' ) ;
35
39
36
- if ( fs . existsSync ( configPath ) ) {
37
- config = JSON . parse ( fs . readFileSync ( configPath ) ) ;
40
+ // Load config from config.json if it exists.
41
+ if ( fs . existsSync ( CONFIG_PATH ) ) {
42
+ config = JSON . parse ( fs . readFileSync ( CONFIG_PATH ) ) ;
38
43
assert ( config instanceof Object ) ;
39
44
}
40
45
@@ -55,21 +60,24 @@ app.setConfig = (newConfig) => {
55
60
} ;
56
61
57
62
app . use ( compression ( ) ) ;
58
-
59
- app . use ( '/node_modules' , express . static ( path . resolve ( __dirname , '../node_modules' ) ) ) ;
63
+ app . use ( '/progress-bar.html' , express . static ( PROGRESS_BAR_PATH ) ) ;
60
64
61
65
app . get ( '/' , ( request , response ) => {
62
66
response . sendFile ( path . resolve ( __dirname , 'index.html' ) ) ;
63
67
} ) ;
64
68
65
- function isRestricted ( url ) {
66
- if ( ! config [ 'renderOnly' ] )
67
- return false ;
69
+ function isRestricted ( urlReq ) {
70
+ const protocol = ( url . parse ( urlReq ) . protocol || '' ) ;
71
+
72
+ if ( ! protocol . match ( / ^ h t t p s ? / ) ) return true ;
73
+ if ( ! config [ 'renderOnly' ] ) return false ;
74
+
68
75
for ( let i = 0 ; i < config [ 'renderOnly' ] . length ; i ++ ) {
69
- if ( url . startsWith ( config [ 'renderOnly' ] [ i ] ) ) {
76
+ if ( urlReq . startsWith ( config [ 'renderOnly' ] [ i ] ) ) {
70
77
return false ;
71
78
}
72
79
}
80
+
73
81
return true ;
74
82
}
75
83
@@ -100,10 +108,8 @@ app.get('/render/:url(*)', async(request, response) => {
100
108
response . status ( result . status ) . send ( result . body ) ;
101
109
track ( 'render' , now ( ) - start ) ;
102
110
} catch ( err ) {
103
- let message = `Cannot render ${ request . params . url } ` ;
104
- if ( err && err . message )
105
- message += ` - "${ err . message } "` ;
106
- response . status ( 400 ) . send ( message ) ;
111
+ response . status ( 400 ) . send ( 'Cannot render requested URL' ) ;
112
+ console . error ( err ) ;
107
113
}
108
114
} ) ;
109
115
@@ -124,19 +130,16 @@ app.get('/screenshot/:url(*)', async(request, response) => {
124
130
response . end ( img ) ;
125
131
track ( 'screenshot' , now ( ) - start ) ;
126
132
} catch ( err ) {
127
- let message = `Cannot render ${ request . params . url } ` ;
128
- if ( err && err . message )
129
- message += ` - "${ err . message } "` ;
130
- response . status ( 400 ) . send ( message ) ;
133
+ response . status ( 400 ) . send ( 'Cannot render requested URL' ) ;
134
+ console . error ( err ) ;
131
135
}
132
136
} ) ;
133
137
134
138
app . get ( '/_ah/health' , ( request , response ) => response . send ( 'OK' ) ) ;
135
139
136
- app . get ( '/_ah/ stop' , async ( request , response ) => {
140
+ app . stop = async ( ) => {
137
141
await config . chrome . kill ( ) ;
138
- response . send ( 'OK' ) ;
139
- } ) ;
142
+ } ;
140
143
141
144
const appPromise = chromeLauncher . launch ( {
142
145
chromeFlags : [ '--headless' , '--disable-gpu' , '--remote-debugging-address=0.0.0.0' ] ,
@@ -147,10 +150,9 @@ const appPromise = chromeLauncher.launch({
147
150
config . port = chrome . port ;
148
151
// Don't open a port when running from inside a module (eg. tests). Importing
149
152
// module can control this.
150
- const port = process . env . PORT || '3000' ;
151
153
if ( ! module . parent ) {
152
- app . listen ( port , function ( ) {
153
- console . log ( 'Listening on port' , port ) ;
154
+ app . listen ( PORT , function ( ) {
155
+ console . log ( 'Listening on port' , PORT ) ;
154
156
} ) ;
155
157
}
156
158
return app ;
@@ -162,6 +164,7 @@ const appPromise = chromeLauncher.launch({
162
164
163
165
164
166
let exceptionCount = 0 ;
167
+
165
168
async function logUncaughtError ( error ) {
166
169
console . error ( 'Uncaught exception' ) ;
167
170
console . error ( error ) ;
@@ -170,7 +173,7 @@ async function logUncaughtError(error) {
170
173
if ( exceptionCount > 5 ) {
171
174
console . log ( `Detected ${ exceptionCount } errors, shutting instance down` ) ;
172
175
if ( config && config . chrome )
173
- await config . chrome . kill ( ) ;
176
+ await app . stop ( ) ;
174
177
process . exit ( 1 ) ;
175
178
}
176
179
}
0 commit comments