-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
ember-fastboot
executable file
·93 lines (73 loc) · 2.36 KB
/
ember-fastboot
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
#!/usr/bin/env node
'use strict';
/* eslint-env node */
const FastBootAppServer = require('fastboot-app-server');
const ExpressHTTPServer = require('fastboot-app-server/src/express-http-server');
const parseArgs = require('minimist');
const express = require('express');
const { URL } = require('url');
const enforce = require('express-sslify');
// Provide a title to the process in `ps`
process.title = 'ember-fastboot-server';
let argOptions = {
default: { port: 3000, host: '::' }
};
let options = parseArgs(process.argv.slice(2), argOptions);
let distPath = options._[0];
if (!distPath) {
console.error(
`You must call ember-fastboot with the path of a fastboot-dist directory:
ember-fastboot fastboot-dist`
);
process.exit(1);
}
const serverOptions = {
port: options.port,
distPath,
gzip: false // Let Fastly take care of compression, reducing load on the fastboot
};
const httpServer = new ExpressHTTPServer(serverOptions);
const app = httpServer.app;
app.use(
enforce.HTTPS({
trustProtoHeader: true,
trustXForwardedHostHeader: true
})
);
app.use(
express.static(distPath, {
setHeaders(res, path) {
if (!path.endsWith('index.html')) {
res.setHeader('Cache-Control', 'public, max-age=365000000, immutable');
}
res.removeHeader('X-Powered-By');
}
})
);
/** We rewrite the 307 location header into a relativeURL so that our special setup is handled */
app.use(function(req, res, next) {
const originalSendFn = res.send;
res.send = function() {
if (res.hasHeader('location')) {
let originalLocation = res.getHeader('location');
// FastBoot broke us once by removing the protocol so adding a check for safety
if (originalLocation.startsWith('//')) {
originalLocation = `http:${originalLocation}`;
}
let relativeURL = '/ember/2.14/namespaces/Ember';
try {
relativeURL = new URL(originalLocation).pathname;
} catch (e) {
console.log(`Original location value: ${originalLocation}`);
console.log(e);
}
res.setHeader('location', relativeURL);
arguments[0] = arguments[0].replace(new RegExp(originalLocation, 'g'), relativeURL);
}
originalSendFn.apply(res, arguments);
};
res.removeHeader('X-Powered-By');
next();
});
let server = new FastBootAppServer(Object.assign({ httpServer }, serverOptions));
server.start();