-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
227 lines (173 loc) · 6.29 KB
/
server.js
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
/*
* pub server.js
*
* launches (express) server after resolving opts
* invoke directly via node command line, or via require()
* see: https://nodejs.org/api/modules.html#modules_accessing_the_main_module
*
* Copyright (c) 2015-2024 Jürgen Leschner - github.com/jldec - MIT license
*/
var debug = require('debug')('pub:server');
var osType = require('os').type();
var exec = require('child_process').exec;
var events = require('events');
var path = require('path');
var u = require('pub-util');
var asyncbuilder = require('asyncbuilder');
if (require.main === module) {
pubServer().run();
}
else {
module.exports = pubServer;
}
u.inherits(pubServer, events.EventEmitter);
//--//--//--//--//--//--//--//--//--//--//--//--//
function pubServer(opts) {
if (!(this instanceof pubServer)) return new pubServer(opts);
events.EventEmitter.call(this);
var server = this;
// pass platform-specific ./node_modules path to resolve-opts for builtin packages
server.opts = opts = require('pub-resolve-opts')(opts, path.join(__dirname, 'node_modules'));
opts.production = opts.production || (process.env.NODE_ENV === 'production');
opts.port = opts.port || process.env.PORT || '3001';
opts.appUrl = opts.appUrl || process.env.APP || ('http://localhost:' + opts.port);
server.run = run;
var generator = server.generator = require('pub-generator')(opts);
u.each(opts.serverPlugins.reverse(), function(plugin) {
debug('server plugin:', plugin.inspect());
require(plugin.path)(server);
});
u.each(opts.generatorPlugins.reverse(), function(plugin) {
debug('generator plugin:', plugin.path, plugin.inspect());
require(plugin.path)(generator);
});
var log = opts.log;
//--//--//--//--//--//--//--//--//--//--//--//
function run() {
if (opts.outputOnly) {
// only support single output for now
var output = opts.outputs[0];
if (!output) return log('No output configured.');
if (output.overrideOpts) {
log('output only - override opts', output.overrideOpts);
u.assign(opts, output.overrideOpts);
}
generator.load(function(err) {
if (err) return log(err);
outputAll(output, function(err) {
if (err) { log(err); }
generator.unload();
});
});
return;
}
if (opts.migrate) {
// only support single output for now
let output = opts.outputs[1];
if (!output) return log('No 2nd output (opts.output[1]) configured.');
generator.load(function(err) {
if (err) return log(err);
generator.migratePages(output, function(err) {
if (err) { log(err); }
generator.unload();
});
});
return;
}
generator.load(function(err) {
if (err) return log(err);
if (opts.logPages) {
generator.logPages();
generator.unload();
return;
}
require('./server/watch-sources')(generator);
generator.listen(true);
if (!opts['no-server']) { expressApp(); }
});
}
function outputAll(output, cb) {
var ab = asyncbuilder(function(err, a) {
if (err) return cb(err);
log('output %s pages, %s scripts, %s statics', a[0].length, a[1].length, a[2].length);
var filemap = u.flatten(a);
// check for duplicates assuming case-insensitive output file system
var dup$ = {};
u.each(filemap, function(file) {
if (dup$[file.path.toLowerCase()]) return log('WARNING: duplicate file in output:', file.path);
dup$[file.path.toLowerCase()] = 1;
});
if (output.fileMap) {
output.src.put( [ { path:'/filemap.json', text:JSON.stringify(filemap,null,2) } ], function(err) {
if (err) log(err);
});
}
cb();
});
var pagesDone = ab.asyncAppend();
var scriptsDone = ab.asyncAppend();
var staticsDone = ab.asyncAppend();
ab.complete();
generator.outputPages(output, pagesDone);
require('./server/serve-scripts')(opts).outputAll(output, generator, scriptsDone);
// wait for static scan to complete before starting output
var statics = require('./server/serve-statics')(opts, function(){
statics.outputAll(output, staticsDone);
});
}
function expressApp() {
server.app = require('express')();
server.http = require('http').Server(server.app);
require('./server/serve-sockets')(server);
server.app.disable('x-powered-by');
// see https://expressjs.com/en/guide/behind-proxies.html - boolean type sensitive
server.app.set('trust proxy', opts['trust proxy'] || false);
// log(err) shouldn't throw anymore
log.logger.noErrors = true;
// sessions come with optional redis logger
server.sessions = require('pub-serve-sessions')(server);
log('starting up', opts.appUrl, (opts.production ? 'production' : 'non-production'));
// other default middleware
var bodyParser = require('body-parser');
server.app.use(bodyParser.json());
server.app.use(bodyParser.urlencoded({ extended: false }));
server.app.use(require('compression')());
server.emit('init-app-first');
if (!(opts.auth && opts.auth.disabled)) {
server.sessions.authorizeRoutes();
}
server.statics = require('./server/serve-statics')(opts).serveRoutes(server);
if (!opts.staticOnly) {
server.scripts = require('./server/serve-scripts')(opts).serveRoutes(server);
require('./server/serve-pages')(server);
}
server.emit('init-app-last');
var limit = 10;
var tried = 1;
server.http.on('error', function(err) {
if (err.code === 'EADDRINUSE' && tried < limit) {
server.http.close();
opts.port = (parseInt(opts.port) || 3001) + 1;
log('port in use, trying', opts.port);
server.http.listen(opts.port, '0.0.0.0');
tried ++;
}
});
require('./server/handle-errors')(server);
server.http.listen(opts.port, '0.0.0.0');
log('port', opts.port);
process.on('SIGTERM', function() {
log('shutting down');
server.http.close(function() {
generator.unload();
server.emit('shutdown');
process.exit();
});
});
setTimeout(function() {
if (opts.cli && opts.openBrowser && osType.match(/Darwin/)) {
exec('open "http://localhost:"' + opts.port);
}
}, 100);
}
}