2018-12-11 16:31:48 +01:00
|
|
|
import * as sapper from '../__sapper__/server.js'
|
2018-03-25 22:47:01 +02:00
|
|
|
const express = require('express')
|
2018-11-11 20:31:32 +01:00
|
|
|
const compression = require('compression')
|
2018-02-09 07:29:29 +01:00
|
|
|
const serveStatic = require('serve-static')
|
2018-03-25 22:47:01 +02:00
|
|
|
const app = express()
|
2018-04-16 00:39:45 +02:00
|
|
|
const helmet = require('helmet')
|
2018-12-11 16:31:48 +01:00
|
|
|
const uuidv4 = require('uuid/v4')
|
2018-04-15 00:50:16 +02:00
|
|
|
|
2018-12-11 16:31:48 +01:00
|
|
|
const headScriptChecksum = require('../inline-script-checksum').checksum
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-02-09 07:29:29 +01:00
|
|
|
const { PORT = 4002 } = process.env
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-02-08 17:22:14 +01:00
|
|
|
// this allows us to do e.g. `fetch('/_api/blog')` on the server
|
2018-02-09 07:29:29 +01:00
|
|
|
const fetch = require('node-fetch')
|
2018-01-07 00:51:25 +01:00
|
|
|
global.fetch = (url, opts) => {
|
2018-04-18 03:38:14 +02:00
|
|
|
if (url[0] === '/') {
|
|
|
|
url = `http://localhost:${PORT}${url}`
|
|
|
|
}
|
2018-02-09 07:29:29 +01:00
|
|
|
return fetch(url, opts)
|
|
|
|
}
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-04-18 03:38:14 +02: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-11-11 20:31:32 +01:00
|
|
|
app.use(compression({ threshold: 0 }))
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-12-11 16:31:48 +01:00
|
|
|
app.use((req, res, next) => {
|
|
|
|
res.locals.nonce = uuidv4()
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
2018-04-18 03:38:14 +02:00
|
|
|
// report.html needs to have CSP disable because it has inline scripts
|
|
|
|
app.use(debugOnly(helmet()))
|
|
|
|
app.use(nonDebugOnly(helmet({
|
2018-04-16 00:39:45 +02:00
|
|
|
contentSecurityPolicy: {
|
|
|
|
directives: {
|
2018-12-11 16:31:48 +01:00
|
|
|
scriptSrc: [
|
|
|
|
`'self'`,
|
|
|
|
`'sha256-${headScriptChecksum}'`,
|
|
|
|
(req, res) => `'nonce-${res.locals.nonce}'`
|
|
|
|
],
|
2018-04-16 00:39:45 +02:00
|
|
|
workerSrc: [`'self'`],
|
|
|
|
styleSrc: [`'self'`, `'unsafe-inline'`],
|
|
|
|
frameSrc: [`'none'`],
|
|
|
|
objectSrc: [`'none'`],
|
|
|
|
manifestSrc: [`'self'`]
|
|
|
|
}
|
2018-04-20 06:38:22 +02:00
|
|
|
},
|
|
|
|
referrerPolicy: {
|
|
|
|
policy: 'no-referrer'
|
2018-04-15 00:50:16 +02:00
|
|
|
}
|
2018-04-18 03:38:14 +02:00
|
|
|
})))
|
2018-04-15 00:50:16 +02:00
|
|
|
|
2018-12-11 16:31:48 +01:00
|
|
|
app.use(serveStatic('static', {
|
2018-03-21 04:46:37 +01:00
|
|
|
setHeaders: (res) => {
|
2018-03-26 06:31:40 +02:00
|
|
|
res.setHeader('Cache-Control', 'public,max-age=600')
|
2018-03-21 04:46:37 +01:00
|
|
|
}
|
|
|
|
}))
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-04-18 03:38:14 +02:00
|
|
|
debugPaths.forEach(debugPath => {
|
2018-12-11 16:31:48 +01:00
|
|
|
app.use(debugPath, express.static(`__sapper__/build/client${debugPath}`))
|
2018-04-18 03:38:14 +02:00
|
|
|
})
|
2018-03-25 22:47:01 +02:00
|
|
|
|
2018-12-11 16:31:48 +01:00
|
|
|
app.use(sapper.middleware())
|
2018-01-07 00:51:25 +01:00
|
|
|
|
|
|
|
app.listen(PORT, () => {
|
2018-02-09 07:29:29 +01:00
|
|
|
console.log(`listening on port ${PORT}`)
|
|
|
|
})
|
2018-05-25 04:59:48 +02:00
|
|
|
|
|
|
|
// Handle SIGINT (source: https://git.io/vhJgF)
|
|
|
|
process.on('SIGINT', function () {
|
|
|
|
process.exit(0)
|
|
|
|
})
|