Skip to content

Commit

Permalink
Enforce a consistent coding style using eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob--W committed Feb 26, 2016
1 parent e8f0e64 commit 0872577
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 70 deletions.
28 changes: 28 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"env": {
"node": true
},
"rules": {
"array-bracket-spacing": [2, "never"],
"block-scoped-var": 2,
"brace-style": [2, "1tbs", {"allowSingleLine": true}],
"comma-dangle": [2, "always-multiline"],
"computed-property-spacing": [2, "never"],
"curly": 2,
"eol-last": 2,
"eqeqeq": [2, "smart"],
"max-len": [1, 125],
"new-cap": 1,
"no-extend-native": 2,
"no-mixed-spaces-and-tabs": 2,
"no-trailing-spaces": 2,
"no-undef": 2,
"no-unused-vars": 1,
"no-use-before-define": [2, "nofunc"],
"object-curly-spacing": [2, "never"],
"quotes": [2, "single", "avoid-escape"],
"semi": [2, "always"],
"keyword-spacing": 2,
"space-unary-ops": 2
}
}
7 changes: 0 additions & 7 deletions .jshintrc

This file was deleted.

37 changes: 18 additions & 19 deletions lib/cors-anywhere.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ function proxyRequest(req, res, proxy) {
/**
* "Allow observer to modify headers or abort response"
* https://github.com/nodejitsu/node-http-proxy/blob/v1.11.1/lib/http-proxy/passes/web-incoming.js#L147
*
*
* This method modifies the response headers of the proxied response.
* If a redirect is detected, the response is not sent to the client,
* and a new request is initiated.
*
*
* @param response {ClientRequest} The response of the proxied request
* @param req {IncomingMessage} Incoming HTTP request, augmented with property corsAnywhereRequestState
* @param req.corsAnywhereRequestState {object}
Expand All @@ -122,7 +122,6 @@ function proxyRequest(req, res, proxy) {
* @this {HttpProxy}
*/
function onProxyResponse(response, req, res) {
/* jshint validthis:true */
var proxy = this;
var requestState = req.corsAnywhereRequestState;

Expand All @@ -145,7 +144,7 @@ function onProxyResponse(response, req, res) {
// cancel redirects.
// Set header for debugging purposes. Do not try to parse it!
res.setHeader('X-CORS-Redirect-' + requestState.redirectCount_, statusCode + ' ' + locationHeader);

req.method = 'GET';
req.headers['content-length'] = '0';
delete req.headers['content-type'];
Expand All @@ -160,15 +159,15 @@ function onProxyResponse(response, req, res) {
res.setHeader = res.writeHead = function noop() {};
response.on = function noop2() {};
response.pipe = function(res) {
res.setHeader = setHeader;
res.writeHead = writeHead;
// Trigger proxyReq.abort() (this is not of any imporance, it's just used to stop wasting resources.)
// https://github.com/nodejitsu/node-http-proxy/blob/v1.11.1/lib/http-proxy/passes/web-incoming.js#L125-L128
req.emit('aborted');
// Remove all listeners (=reset events to initial state)
req.removeAllListeners();
// Initiate a new proxy request.
proxyRequest(req, res, proxy);
res.setHeader = setHeader;
res.writeHead = writeHead;
// Trigger proxyReq.abort() (this is not of any imporance, it's just used to stop wasting resources.)
// https://github.com/nodejitsu/node-http-proxy/blob/v1.11.1/lib/http-proxy/passes/web-incoming.js#L125-L128
req.emit('aborted');
// Remove all listeners (=reset events to initial state)
req.removeAllListeners();
// Initiate a new proxy request.
proxyRequest(req, res, proxy);
};
return;
}
Expand Down Expand Up @@ -200,12 +199,12 @@ function parseURL(req_url) {
return null;
}
if (!match[1]) {
// scheme is omitted.
// Scheme is omitted.
if (req_url.lastIndexOf('//', 0) === -1) {
// "//" is omitted.
req_url = '//' + req_url;
}
req_url = (match[4] == '443' ? 'https:' : 'http:') + req_url;
req_url = (match[4] === '443' ? 'https:' : 'http:') + req_url;
}
return url.parse(req_url);
}
Expand All @@ -219,7 +218,7 @@ var getHandler = exports.getHandler = function(options, proxy) {
originWhitelist: [], // If non-empty, requests not from an origin in this list will be blocked.
requireHeader: null, // Require a header to be set?
removeHeaders: [], // Strip these request headers.
setHeaders: {} // Set these request headers.
setHeaders: {}, // Set these request headers.
};
if (options) {
Object.keys(corsAnywhere).forEach(function(option) {
Expand Down Expand Up @@ -248,7 +247,7 @@ var getHandler = exports.getHandler = function(options, proxy) {

return function(req, res) {
var cors_headers = withCORS({}, req);
if (req.method == 'OPTIONS') {
if (req.method === 'OPTIONS') {
// Pre-flight request. Reply successfully:
res.writeHead(200, cors_headers);
res.end();
Expand Down Expand Up @@ -320,7 +319,7 @@ var getHandler = exports.getHandler = function(options, proxy) {
location: location,
getProxyForUrl: corsAnywhere.getProxyForUrl,
maxRedirects: corsAnywhere.maxRedirects,
proxyBaseUrl: proxyBaseUrl
proxyBaseUrl: proxyBaseUrl,
};

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

// Default options:
var httpProxyOptions = {
Expand Down
2 changes: 2 additions & 0 deletions lib/regexp-top-level-domain.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"name": "cors-anywhere",
"version": "0.3.0",
"description": "CORS Anywhere is a reverse proxy which adds CORS headers to the proxied request. Request URL is taken from the path",
"description": "CORS Anywhere is a reverse proxy which adds CORS headers to the proxied request. Request URL is taken from the path",
"license": "MIT",
"author": "Rob Wu <[email protected]>",
"repository": {
"type": "git",
"url": "https://github.com/Rob--W/cors-anywhere.git"
"type": "git",
"url": "https://github.com/Rob--W/cors-anywhere.git"
},
"bugs": {
"url": "https://github.com/Rob--W/cors-anywhere/issues/",
"email": "[email protected]"
"url": "https://github.com/Rob--W/cors-anywhere/issues/",
"email": "[email protected]"
},
"keywords": [
"cors",
"cross-domain",
"http-proxy",
"proxy",
"heroku"
"cors",
"cross-domain",
"http-proxy",
"proxy",
"heroku"
],
"main": "./lib/cors-anywhere.js",
"dependencies": {
Expand All @@ -26,11 +26,13 @@
"requires-port": "1.0.0"
},
"devDependencies": {
"eslint": "^2.2.0",
"mocha": "~2.2.4",
"nock": "~1.9.0",
"supertest": "~0.15.0"
},
"scripts": {
"lint": "eslint .",
"test": "./node_modules/.bin/mocha ./test/test*.js --reporter spec"
},
"engines": {
Expand Down
32 changes: 16 additions & 16 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ var originBlacklist = (process.env.CORSANYWHERE_BLACKLIST || '').split(',');

var cors_proxy = require('./lib/cors-anywhere');
cors_proxy.createServer({
originBlacklist: originBlacklist,
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: [
'cookie',
'cookie2',
// Strip Heroku-specific headers
'x-heroku-queue-wait-time',
'x-heroku-queue-depth',
'x-heroku-dynos-in-use',
'x-request-start'
],
httpProxyOptions: {
// Do not add X-Forwarded-For, etc. headers, because Heroku already adds it.
xfwd: false
}
originBlacklist: originBlacklist,
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: [
'cookie',
'cookie2',
// Strip Heroku-specific headers
'x-heroku-queue-wait-time',
'x-heroku-queue-depth',
'x-heroku-dynos-in-use',
'x-request-start',
],
httpProxyOptions: {
// Do not add X-Forwarded-For, etc. headers, because Heroku already adds it.
xfwd: false,
},
}).listen(port, host, function() {
console.log('Running CORS Anywhere on ' + host + ':' + port);
console.log('Running CORS Anywhere on ' + host + ':' + port);
});
16 changes: 8 additions & 8 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function echoheaders(origin) {
nock(origin)
.persist()
.get('/echoheaders')
.reply(function(uri) {
.reply(function() {
var headers = this.req.headers;
var excluded_headers = [
'accept-encoding',
Expand Down Expand Up @@ -49,18 +49,18 @@ nock('http://example.com')

.get('/redirecttarget')
.reply(200, 'redirect target', {
'Some-header': 'value'
'Some-header': 'value',
})

.head('/redirect')
.reply(302, '', {
'Location': '/redirecttarget'
Location: '/redirecttarget',
})

.get('/redirect')
.reply(302, 'redirecting...', {
'header at redirect': 'should not be here',
'Location': '/redirecttarget'
Location: '/redirecttarget',
})

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

.post('/redirectpost')
.reply(302, 'redirecting...', {
'Location': '/redirectposttarget'
Location: '/redirectposttarget',
})

.post('/redirect307')
.reply(307, 'redirecting...', {
'Location': '/redirectposttarget'
Location: '/redirectposttarget',
})

.get('/redirect2redirect')
.reply(302, 'redirecting to redirect...', {
'Location': '/redirect'
Location: '/redirect',
})

.get('/redirectloop')
.reply(302, 'redirecting ad infinitum...', {
'Location': '/redirectloop'
Location: '/redirectloop',
})

.get('/proxyerror')
Expand Down
1 change: 1 addition & 0 deletions test/test-memory.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-env mocha */
// Run this specific test using:
// npm test -- -f memory
var http = require('http');
Expand Down
Loading

0 comments on commit 0872577

Please sign in to comment.