Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0872577

Browse files
committedFeb 26, 2016
Enforce a consistent coding style using eslint
1 parent e8f0e64 commit 0872577

9 files changed

+96
-70
lines changed
 

‎.eslintrc

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"env": {
3+
"node": true
4+
},
5+
"rules": {
6+
"array-bracket-spacing": [2, "never"],
7+
"block-scoped-var": 2,
8+
"brace-style": [2, "1tbs", {"allowSingleLine": true}],
9+
"comma-dangle": [2, "always-multiline"],
10+
"computed-property-spacing": [2, "never"],
11+
"curly": 2,
12+
"eol-last": 2,
13+
"eqeqeq": [2, "smart"],
14+
"max-len": [1, 125],
15+
"new-cap": 1,
16+
"no-extend-native": 2,
17+
"no-mixed-spaces-and-tabs": 2,
18+
"no-trailing-spaces": 2,
19+
"no-undef": 2,
20+
"no-unused-vars": 1,
21+
"no-use-before-define": [2, "nofunc"],
22+
"object-curly-spacing": [2, "never"],
23+
"quotes": [2, "single", "avoid-escape"],
24+
"semi": [2, "always"],
25+
"keyword-spacing": 2,
26+
"space-unary-ops": 2
27+
}
28+
}

‎.jshintrc

-7
This file was deleted.

‎lib/cors-anywhere.js

+18-19
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ function proxyRequest(req, res, proxy) {
104104
/**
105105
* "Allow observer to modify headers or abort response"
106106
* https://github.com/nodejitsu/node-http-proxy/blob/v1.11.1/lib/http-proxy/passes/web-incoming.js#L147
107-
*
107+
*
108108
* This method modifies the response headers of the proxied response.
109109
* If a redirect is detected, the response is not sent to the client,
110110
* and a new request is initiated.
111-
*
111+
*
112112
* @param response {ClientRequest} The response of the proxied request
113113
* @param req {IncomingMessage} Incoming HTTP request, augmented with property corsAnywhereRequestState
114114
* @param req.corsAnywhereRequestState {object}
@@ -122,7 +122,6 @@ function proxyRequest(req, res, proxy) {
122122
* @this {HttpProxy}
123123
*/
124124
function onProxyResponse(response, req, res) {
125-
/* jshint validthis:true */
126125
var proxy = this;
127126
var requestState = req.corsAnywhereRequestState;
128127

@@ -145,7 +144,7 @@ function onProxyResponse(response, req, res) {
145144
// cancel redirects.
146145
// Set header for debugging purposes. Do not try to parse it!
147146
res.setHeader('X-CORS-Redirect-' + requestState.redirectCount_, statusCode + ' ' + locationHeader);
148-
147+
149148
req.method = 'GET';
150149
req.headers['content-length'] = '0';
151150
delete req.headers['content-type'];
@@ -160,15 +159,15 @@ function onProxyResponse(response, req, res) {
160159
res.setHeader = res.writeHead = function noop() {};
161160
response.on = function noop2() {};
162161
response.pipe = function(res) {
163-
res.setHeader = setHeader;
164-
res.writeHead = writeHead;
165-
// Trigger proxyReq.abort() (this is not of any imporance, it's just used to stop wasting resources.)
166-
// https://github.com/nodejitsu/node-http-proxy/blob/v1.11.1/lib/http-proxy/passes/web-incoming.js#L125-L128
167-
req.emit('aborted');
168-
// Remove all listeners (=reset events to initial state)
169-
req.removeAllListeners();
170-
// Initiate a new proxy request.
171-
proxyRequest(req, res, proxy);
162+
res.setHeader = setHeader;
163+
res.writeHead = writeHead;
164+
// Trigger proxyReq.abort() (this is not of any imporance, it's just used to stop wasting resources.)
165+
// https://github.com/nodejitsu/node-http-proxy/blob/v1.11.1/lib/http-proxy/passes/web-incoming.js#L125-L128
166+
req.emit('aborted');
167+
// Remove all listeners (=reset events to initial state)
168+
req.removeAllListeners();
169+
// Initiate a new proxy request.
170+
proxyRequest(req, res, proxy);
172171
};
173172
return;
174173
}
@@ -200,12 +199,12 @@ function parseURL(req_url) {
200199
return null;
201200
}
202201
if (!match[1]) {
203-
// scheme is omitted.
202+
// Scheme is omitted.
204203
if (req_url.lastIndexOf('//', 0) === -1) {
205204
// "//" is omitted.
206205
req_url = '//' + req_url;
207206
}
208-
req_url = (match[4] == '443' ? 'https:' : 'http:') + req_url;
207+
req_url = (match[4] === '443' ? 'https:' : 'http:') + req_url;
209208
}
210209
return url.parse(req_url);
211210
}
@@ -219,7 +218,7 @@ var getHandler = exports.getHandler = function(options, proxy) {
219218
originWhitelist: [], // If non-empty, requests not from an origin in this list will be blocked.
220219
requireHeader: null, // Require a header to be set?
221220
removeHeaders: [], // Strip these request headers.
222-
setHeaders: {} // Set these request headers.
221+
setHeaders: {}, // Set these request headers.
223222
};
224223
if (options) {
225224
Object.keys(corsAnywhere).forEach(function(option) {
@@ -248,7 +247,7 @@ var getHandler = exports.getHandler = function(options, proxy) {
248247

249248
return function(req, res) {
250249
var cors_headers = withCORS({}, req);
251-
if (req.method == 'OPTIONS') {
250+
if (req.method === 'OPTIONS') {
252251
// Pre-flight request. Reply successfully:
253252
res.writeHead(200, cors_headers);
254253
res.end();
@@ -320,7 +319,7 @@ var getHandler = exports.getHandler = function(options, proxy) {
320319
location: location,
321320
getProxyForUrl: corsAnywhere.getProxyForUrl,
322321
maxRedirects: corsAnywhere.maxRedirects,
323-
proxyBaseUrl: proxyBaseUrl
322+
proxyBaseUrl: proxyBaseUrl,
324323
};
325324

326325
proxyRequest(req, res, proxy);
@@ -330,7 +329,7 @@ var getHandler = exports.getHandler = function(options, proxy) {
330329
// Create server with default and given values
331330
// Creator still needs to call .listen()
332331
exports.createServer = function createServer(options) {
333-
if (!options) options = {};
332+
options = options || {};
334333

335334
// Default options:
336335
var httpProxyOptions = {

‎lib/regexp-top-level-domain.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
22
"name": "cors-anywhere",
33
"version": "0.3.0",
4-
"description": "CORS Anywhere is a reverse proxy which adds CORS headers to the proxied request. Request URL is taken from the path",
4+
"description": "CORS Anywhere is a reverse proxy which adds CORS headers to the proxied request. Request URL is taken from the path",
55
"license": "MIT",
66
"author": "Rob Wu <rob@robwu.nl>",
77
"repository": {
8-
"type": "git",
9-
"url": "https://github.com/Rob--W/cors-anywhere.git"
8+
"type": "git",
9+
"url": "https://github.com/Rob--W/cors-anywhere.git"
1010
},
1111
"bugs": {
12-
"url": "https://github.com/Rob--W/cors-anywhere/issues/",
13-
"email": "rob@robwu.nl"
12+
"url": "https://github.com/Rob--W/cors-anywhere/issues/",
13+
"email": "rob@robwu.nl"
1414
},
1515
"keywords": [
16-
"cors",
17-
"cross-domain",
18-
"http-proxy",
19-
"proxy",
20-
"heroku"
16+
"cors",
17+
"cross-domain",
18+
"http-proxy",
19+
"proxy",
20+
"heroku"
2121
],
2222
"main": "./lib/cors-anywhere.js",
2323
"dependencies": {
@@ -26,11 +26,13 @@
2626
"requires-port": "1.0.0"
2727
},
2828
"devDependencies": {
29+
"eslint": "^2.2.0",
2930
"mocha": "~2.2.4",
3031
"nock": "~1.9.0",
3132
"supertest": "~0.15.0"
3233
},
3334
"scripts": {
35+
"lint": "eslint .",
3436
"test": "./node_modules/.bin/mocha ./test/test*.js --reporter spec"
3537
},
3638
"engines": {

‎server.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ var originBlacklist = (process.env.CORSANYWHERE_BLACKLIST || '').split(',');
1010

1111
var cors_proxy = require('./lib/cors-anywhere');
1212
cors_proxy.createServer({
13-
originBlacklist: originBlacklist,
14-
requireHeader: ['origin', 'x-requested-with'],
15-
removeHeaders: [
16-
'cookie',
17-
'cookie2',
18-
// Strip Heroku-specific headers
19-
'x-heroku-queue-wait-time',
20-
'x-heroku-queue-depth',
21-
'x-heroku-dynos-in-use',
22-
'x-request-start'
23-
],
24-
httpProxyOptions: {
25-
// Do not add X-Forwarded-For, etc. headers, because Heroku already adds it.
26-
xfwd: false
27-
}
13+
originBlacklist: originBlacklist,
14+
requireHeader: ['origin', 'x-requested-with'],
15+
removeHeaders: [
16+
'cookie',
17+
'cookie2',
18+
// Strip Heroku-specific headers
19+
'x-heroku-queue-wait-time',
20+
'x-heroku-queue-depth',
21+
'x-heroku-dynos-in-use',
22+
'x-request-start',
23+
],
24+
httpProxyOptions: {
25+
// Do not add X-Forwarded-For, etc. headers, because Heroku already adds it.
26+
xfwd: false,
27+
},
2828
}).listen(port, host, function() {
29-
console.log('Running CORS Anywhere on ' + host + ':' + port);
29+
console.log('Running CORS Anywhere on ' + host + ':' + port);
3030
});

‎test/setup.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function echoheaders(origin) {
66
nock(origin)
77
.persist()
88
.get('/echoheaders')
9-
.reply(function(uri) {
9+
.reply(function() {
1010
var headers = this.req.headers;
1111
var excluded_headers = [
1212
'accept-encoding',
@@ -49,18 +49,18 @@ nock('http://example.com')
4949

5050
.get('/redirecttarget')
5151
.reply(200, 'redirect target', {
52-
'Some-header': 'value'
52+
'Some-header': 'value',
5353
})
5454

5555
.head('/redirect')
5656
.reply(302, '', {
57-
'Location': '/redirecttarget'
57+
Location: '/redirecttarget',
5858
})
5959

6060
.get('/redirect')
6161
.reply(302, 'redirecting...', {
6262
'header at redirect': 'should not be here',
63-
'Location': '/redirecttarget'
63+
Location: '/redirecttarget',
6464
})
6565

6666
.get('/redirectposttarget')
@@ -71,22 +71,22 @@ nock('http://example.com')
7171

7272
.post('/redirectpost')
7373
.reply(302, 'redirecting...', {
74-
'Location': '/redirectposttarget'
74+
Location: '/redirectposttarget',
7575
})
7676

7777
.post('/redirect307')
7878
.reply(307, 'redirecting...', {
79-
'Location': '/redirectposttarget'
79+
Location: '/redirectposttarget',
8080
})
8181

8282
.get('/redirect2redirect')
8383
.reply(302, 'redirecting to redirect...', {
84-
'Location': '/redirect'
84+
Location: '/redirect',
8585
})
8686

8787
.get('/redirectloop')
8888
.reply(302, 'redirecting ad infinitum...', {
89-
'Location': '/redirectloop'
89+
Location: '/redirectloop',
9090
})
9191

9292
.get('/proxyerror')

‎test/test-memory.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-env mocha */
12
// Run this specific test using:
23
// npm test -- -f memory
34
var http = require('http');

‎test/test.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-env mocha */
12
require('./setup');
23

34
var createServer = require('../').createServer;
@@ -8,7 +9,7 @@ var fs = require('fs');
89
var assert = require('assert');
910

1011
var helpTextPath = path.join(__dirname, '../lib/help.txt');
11-
var helpText = fs.readFileSync(helpTextPath, { encoding: 'utf8' });
12+
var helpText = fs.readFileSync(helpTextPath, {encoding: 'utf8'});
1213

1314
request.Test.prototype.expectJSON = function(json, done) {
1415
this.expect(function(res) {
@@ -132,7 +133,7 @@ describe('Basic functionality', function() {
132133
.post('/example.com/echopost')
133134
.attach('file', path.join(__dirname, 'dummy.txt'))
134135
.expect('Access-Control-Allow-Origin', '*')
135-
.expect(/\r\nContent-Disposition: form-data; name="file"; filename="dummy.txt"\r\nContent-Type: text\/plain\r\n\r\ndummy content\n\r\n/, done);
136+
.expect(/\r\nContent-Disposition: form-data; name="file"; filename="dummy.txt"\r\nContent-Type: text\/plain\r\n\r\ndummy content\n\r\n/, done); // eslint-disable-line max-len
136137
});
137138

138139
it('HEAD with redirect should be followed', function(done) {
@@ -494,7 +495,7 @@ describe('setHeaders', function() {
494495
.expect('Access-Control-Allow-Origin', '*')
495496
.expectJSON({
496497
host: 'example.com',
497-
'x-powered-by': 'CORS Anywhere'
498+
'x-powered-by': 'CORS Anywhere',
498499
}, done);
499500
});
500501

@@ -505,7 +506,7 @@ describe('setHeaders', function() {
505506
.expect('Access-Control-Allow-Origin', '*')
506507
.expectJSON({
507508
host: 'example.com',
508-
'x-powered-by': 'CORS Anywhere'
509+
'x-powered-by': 'CORS Anywhere',
509510
}, done);
510511
});
511512
});
@@ -527,7 +528,7 @@ describe('setHeaders + removeHeaders', function() {
527528
.expect('Access-Control-Allow-Origin', '*')
528529
.expectJSON({
529530
host: 'example.com',
530-
'x-powered-by': 'CORS Anywhere'
531+
'x-powered-by': 'CORS Anywhere',
531532
}, done);
532533
});
533534

@@ -538,7 +539,7 @@ describe('setHeaders + removeHeaders', function() {
538539
.expect('Access-Control-Allow-Origin', '*')
539540
.expectJSON({
540541
host: 'example.com',
541-
'x-powered-by': 'CORS Anywhere'
542+
'x-powered-by': 'CORS Anywhere',
542543
}, done);
543544
});
544545
});
@@ -547,8 +548,8 @@ describe('httpProxyOptions.xfwd=false', function() {
547548
before(function() {
548549
cors_anywhere = createServer({
549550
httpProxyOptions: {
550-
xfwd: false
551-
}
551+
xfwd: false,
552+
},
552553
});
553554
cors_anywhere_port = cors_anywhere.listen(0).address().port;
554555
});
@@ -577,8 +578,8 @@ describe('httpProxyOptions.getProxyForUrl', function() {
577578

578579
cors_anywhere = createServer({
579580
httpProxyOptions: {
580-
xfwd: false
581-
}
581+
xfwd: false,
582+
},
582583
});
583584
cors_anywhere_port = cors_anywhere.listen(0).address().port;
584585
});

0 commit comments

Comments
 (0)
Please sign in to comment.