Skip to content

Commit 62d83b8

Browse files
committed
feat(cors): Add new option 'cors' for adding HTTP access control (CORS) to assets served by Browsersync
1 parent f5c3910 commit 62d83b8

3 files changed

Lines changed: 76 additions & 6 deletions

File tree

lib/default-config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,14 @@ module.exports = {
241241
*/
242242
browser: "default",
243243

244+
/**
245+
* Add HTTP access control (CORS) headers to assets served by Browsersync
246+
* @property cors
247+
* @type boolean
248+
* @default false
249+
*/
250+
cors: false,
251+
244252
/**
245253
* Requires an internet connection - useful for services such as [Typekit](https://typekit.com/)
246254
* as it allows you to configure domains such as `*.xip.io` in your kit settings

lib/server/utils.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ var utils = {
4848
getBaseApp: function (bs, options, scripts) {
4949

5050
var app = connect();
51-
52-
/**
53-
* Add the proxy Middleware to the end of the stack
54-
*/
55-
app.stack.push(
51+
var defaultMiddlewares = [
5652
{
5753
id: "Browsersync IE8 Support",
5854
route: "",
@@ -73,7 +69,21 @@ var utils = {
7369
route: bs.options.getIn(["scriptPaths", "path"]),
7470
handle: scripts
7571
}
76-
);
72+
];
73+
74+
if (options.get('cors')) {
75+
defaultMiddlewares.unshift({
76+
id: 'Browsersync CORS support',
77+
route: '',
78+
handle: utils.getCorsMiddlewware()
79+
})
80+
}
81+
82+
/**
83+
* Add the proxy Middleware to the end of the stack
84+
*/
85+
86+
app.stack.push.apply(app.stack, defaultMiddlewares);
7787

7888
/**
7989
* Add user-provided middlewares
@@ -118,6 +128,24 @@ var utils = {
118128
bs.snippetMw.opts.rules.push(rule);
119129
}
120130
return bs.snippetMw.middleware;
131+
},
132+
getCorsMiddlewware: function () {
133+
134+
return function (req, res, next) {
135+
// Website you wish to allow to connect
136+
res.setHeader('Access-Control-Allow-Origin', '*');
137+
138+
// Request methods you wish to allow
139+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
140+
141+
// Request headers you wish to allow
142+
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
143+
144+
// Set to true if you need the website to include cookies in the requests sent
145+
// to the API (e.g. in case you use sessions)
146+
res.setHeader('Access-Control-Allow-Credentials', true);
147+
next();
148+
}
121149
}
122150
};
123151

test/specs/e2e/e2e.options.cors.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use strict";
2+
3+
var browserSync = require("../../../");
4+
5+
var assert = require("chai").assert;
6+
var sinon = require("sinon");
7+
var request = require("supertest");
8+
9+
describe("e2e options test (cors)", function () {
10+
it("Adds cors middleware", function (done) {
11+
browserSync.reset();
12+
var config = {
13+
server: {
14+
baseDir: "test/fixtures"
15+
},
16+
open: false,
17+
logLevel: "silent",
18+
cors: true
19+
};
20+
browserSync(config, function (err, bs) {
21+
request(bs.server)
22+
.get("/index.html")
23+
.expect(200)
24+
.end(function (err, res) {
25+
assert.equal(res.headers['access-control-allow-origin'], '*');
26+
assert.equal(res.headers['access-control-allow-methods'], 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
27+
assert.equal(res.headers['access-control-allow-headers'], 'X-Requested-With,content-type');
28+
assert.equal(res.headers['access-control-allow-credentials'], 'true');
29+
bs.cleanup();
30+
done();
31+
});
32+
});
33+
});
34+
});

0 commit comments

Comments
 (0)