2018-03-25 21:47:01 +01:00
|
|
|
const express = require('express')
|
2018-04-21 15:06:08 +01:00
|
|
|
const shrinkRay = require('shrink-ray-current')
|
2018-02-09 06:29:29 +00:00
|
|
|
const sapper = require('sapper')
|
|
|
|
const serveStatic = require('serve-static')
|
2018-03-25 21:47:01 +01:00
|
|
|
const app = express()
|
2018-04-15 23:39:45 +01:00
|
|
|
const helmet = require('helmet')
|
2018-04-14 23:50:16 +01:00
|
|
|
|
|
|
|
const headScriptChecksum = require('./inline-script-checksum').checksum
|
2018-01-06 23:51:25 +00:00
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
const { PORT = 4002 } = process.env
|
2018-01-06 23:51:25 +00:00
|
|
|
|
2018-02-08 16:22:14 +00:00
|
|
|
// this allows us to do e.g. `fetch('/_api/blog')` on the server
|
2018-02-09 06:29:29 +00:00
|
|
|
const fetch = require('node-fetch')
|
2018-01-06 23:51:25 +00:00
|
|
|
global.fetch = (url, opts) => {
|
2018-04-18 02:38:14 +01:00
|
|
|
if (url[0] === '/') {
|
|
|
|
url = `http://localhost:${PORT}${url}`
|
|
|
|
}
|
2018-02-09 06:29:29 +00:00
|
|
|
return fetch(url, opts)
|
|
|
|
}
|
2018-01-06 23:51:25 +00:00
|
|
|
|
2018-04-18 02:38:14 +01:00
|
|
|
const debugPaths = ['/report.html', '/stats.json']
|
|
|
|
|
|
|
|
const debugOnly = (fn) => (req, res, next) => (
|
|
|
|
!~debugPaths.indexOf(req.path) ? next() : fn(req, res, next)
|
|
|
|
)
|
|
|
|
|
|
|
|
const nonDebugOnly = (fn) => (req, res, next) => (
|
|
|
|
~debugPaths.indexOf(req.path) ? next() : fn(req, res, next)
|
|
|
|
)
|
|
|
|
|
2018-08-30 05:42:57 +01:00
|
|
|
app.use(shrinkRay({ threshold: 0 }))
|
2018-01-06 23:51:25 +00:00
|
|
|
|
2018-04-18 02:38:14 +01:00
|
|
|
// report.html needs to have CSP disable because it has inline scripts
|
|
|
|
app.use(debugOnly(helmet()))
|
|
|
|
app.use(nonDebugOnly(helmet({
|
2018-04-15 23:39:45 +01:00
|
|
|
contentSecurityPolicy: {
|
|
|
|
directives: {
|
|
|
|
scriptSrc: [`'self'`, `'sha256-${headScriptChecksum}'`],
|
|
|
|
workerSrc: [`'self'`],
|
|
|
|
styleSrc: [`'self'`, `'unsafe-inline'`],
|
|
|
|
frameSrc: [`'none'`],
|
|
|
|
objectSrc: [`'none'`],
|
|
|
|
manifestSrc: [`'self'`]
|
|
|
|
}
|
2018-04-20 05:38:22 +01:00
|
|
|
},
|
|
|
|
referrerPolicy: {
|
|
|
|
policy: 'no-referrer'
|
2018-04-14 23:50:16 +01:00
|
|
|
}
|
2018-04-18 02:38:14 +01:00
|
|
|
})))
|
2018-04-14 23:50:16 +01:00
|
|
|
|
2018-03-21 03:46:37 +00:00
|
|
|
app.use(serveStatic('assets', {
|
|
|
|
setHeaders: (res) => {
|
2018-03-26 05:31:40 +01:00
|
|
|
res.setHeader('Cache-Control', 'public,max-age=600')
|
2018-03-21 03:46:37 +00:00
|
|
|
}
|
|
|
|
}))
|
2018-01-06 23:51:25 +00:00
|
|
|
|
2018-04-18 02:38:14 +01:00
|
|
|
debugPaths.forEach(debugPath => {
|
|
|
|
app.use(debugPath, express.static(`.sapper/client${debugPath}`))
|
|
|
|
})
|
2018-03-25 21:47:01 +01:00
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
app.use(sapper())
|
2018-01-06 23:51:25 +00:00
|
|
|
|
|
|
|
app.listen(PORT, () => {
|
2018-02-09 06:29:29 +00:00
|
|
|
console.log(`listening on port ${PORT}`)
|
|
|
|
})
|
2018-05-25 03:59:48 +01:00
|
|
|
|
|
|
|
// Handle SIGINT (source: https://git.io/vhJgF)
|
|
|
|
process.on('SIGINT', function () {
|
|
|
|
process.exit(0)
|
|
|
|
})
|