-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (69 loc) · 2.49 KB
/
index.js
File metadata and controls
88 lines (69 loc) · 2.49 KB
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
/* jshint node: true */
'use strict';
var path = require('path');
module.exports = {
name: 'ember-capture',
includedCommands: function() {
return {
capture: require('./lib/commands/capture')
}
},
included: function (app, parentAddon) {
var target = (parentAddon || app);
this._super.included(target);
if (app.tests) {
app.import(path.join('vendor', 'ember-capture', 'capture-client.js'), {
type: 'test',
prepend: true
});
}
},
contentFor: function(type, config, content) {
if (this.contentForMethods[type]) {
return this.contentForMethods[type](config, content);
}
},
contentForMethods: {
"test-body": function(config, content) {
for (var i = 0; i < content.length; i++) {
if (~content[i].toString().indexOf('ember-testing-container')) {
content = content.splice(i,1);
}
}
}
},
serverMiddleware: function(config) {
var app = config.app;
var options = config.options;
var project = options.project;
// Switch on and off based upon the environment.
if (options.environment !== 'capture') { return; }
app.use(function(req, res, next) {
var appConfig = project.config(options.environment);
// If we're not using CSP headers, bail.
if (!appConfig || !appConfig.contentSecurityPolicyHeader || !appConfig.contentSecurityPolicy) {
next();
return;
}
var header = appConfig.contentSecurityPolicyHeader;
var headerConfig = appConfig.contentSecurityPolicy;
var normalizedHost = (options.captureHost ? options.captureHost : 'localhost');
var captureServerURL = 'http://' + normalizedHost + ':' + options.capturePort + '/';
// Make sure it exists.
headerConfig['connect-src'] = headerConfig['connect-src'] || '';
// Add ourselves to the list.
headerConfig['connect-src'] = headerConfig['connect-src'] + ' ' + captureServerURL;
// Collapse all of the values back into the header.
var headerValue = Object.keys(headerConfig).reduce(function(memo, value) {
return memo + value + ' ' + headerConfig[value] + '; ';
}, '');
res.removeHeader("Content-Security-Policy");
res.removeHeader("X-Content-Security-Policy");
res.removeHeader('Content-Security-Policy-Report-Only');
res.removeHeader('X-Content-Security-Policy-Report-Only');
res.setHeader(header, headerValue);
res.setHeader('X-' + header, headerValue);
next();
});
}
};