forked from apache/couchdb-fauxton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevserver.js
137 lines (115 loc) · 3.45 KB
/
devserver.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
const spawn = require('child_process').spawn;
const fs = require("fs");
const webpack = require('webpack');
const WebpackDev = require('webpack-dev-server');
const config = require('./webpack.config.dev.js');
const httpProxy = require('http-proxy');
const path = require('path');
const loadSettings = function () {
let fileName = './settings.json.default.json';
if (fs.existsSync('./settings.json')) {
fileName = './settings.json';
}
return require(fileName).couchserver || {
port: process.env.FAUXTON_PORT || 8000,
contentSecurityPolicy: true,
proxy: {
target: process.env.COUCH_HOST || 'http://127.0.0.1:5984',
changeOrigin: false
}
};
};
const settings = loadSettings();
const devSetup = function (cb) {
console.info('setup dev environment');
let cmd = 'devSetupWithClean';
if (settings.noClean) {
cmd = 'devSetup';
}
const isOnWindows = process.platform === 'win32';
const gruntCmd = isOnWindows ? 'grunt.cmd' : 'grunt';
const grunt = spawn(gruntCmd, [cmd]);
grunt.stdout.on('data', (data) => {
console.info(data.toString());
});
grunt.stderr.on('error', (data) => {
console.info('Setup error:', data.toString());
});
grunt.on('close', (code) => {
console.info('dev setup finished with code', code);
if (code === 0) {
cb();
}
});
};
const defaultHeaderValue = "default-src 'self'; child-src 'self' blob: https://blog.couchdb.org; img-src 'self' data:; font-src 'self'; " +
"script-src 'self'; style-src 'self'; object-src 'none';";
function getCspHeaders () {
if (!settings.contentSecurityPolicy) {
return;
}
const cspHeader = settings.contentSecurityPolicyHeader || defaultHeaderValue;
return {
'Content-Security-Policy': cspHeader
};
}
const runWebpackServer = function () {
const proxy = httpProxy.createServer({
secure: false,
changeOrigin: true,
target: settings.proxy.target
});
proxy.on('proxyRes', function (proxyRes) {
if (proxyRes.headers['set-cookie']) {
proxyRes.headers['set-cookie'][0] = proxyRes.headers["set-cookie"][0].replace('Secure', '');
}
});
proxy.on('error', function () {
// don't explode on cancelled requests
});
const options = {
static: {
directory: path.join(__dirname, '/dist/debug/')
},
host: '0.0.0.0',
port: process.env.FAUXTON_PORT || 8000,
client: {
overlay: true,
},
hot: false,
historyApiFallback: false,
allowedHosts: "auto",
devMiddleware: {
stats: {
colors: true,
},
},
headers: getCspHeaders(),
setupMiddlewares: (middlewares, devServer) => {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}
middlewares.unshift(
{
name: "proxy-to-couchdb",
middleware: ('*', (req, res, next) => {
const accept = req.headers.accept ? req.headers.accept.split(',') : '';
if (/application\/json/.test(accept[0]) || /multipart\/form-data/.test(accept[0])) {
proxy.web(req, res);
return;
}
next();
}),
}
);
return middlewares;
},
};
const compiler = webpack(config);
const server = new WebpackDev(options, compiler);
server.startCallback(() => {
console.info('listening on', options.host, options.port);
console.info('Starting first compile. This will take about 10 seconds...');
});
};
devSetup(runWebpackServer);