mirror of
https://github.com/d0zingcat/cors-anywhere.git
synced 2026-05-13 15:09:25 +00:00
Enforce a consistent coding style using eslint
This commit is contained in:
28
.eslintrc
Normal file
28
.eslintrc
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"node": true,
|
||||
"eqnull": true,
|
||||
"undef": true,
|
||||
"unused": "vars",
|
||||
"mocha": true
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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) {
|
||||
@@ -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();
|
||||
@@ -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);
|
||||
@@ -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 = {
|
||||
|
||||
File diff suppressed because one or more lines are too long
20
package.json
20
package.json
@@ -5,19 +5,19 @@
|
||||
"license": "MIT",
|
||||
"author": "Rob Wu <rob@robwu.nl>",
|
||||
"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": "rob@robwu.nl"
|
||||
"url": "https://github.com/Rob--W/cors-anywhere/issues/",
|
||||
"email": "rob@robwu.nl"
|
||||
},
|
||||
"keywords": [
|
||||
"cors",
|
||||
"cross-domain",
|
||||
"http-proxy",
|
||||
"proxy",
|
||||
"heroku"
|
||||
"cors",
|
||||
"cross-domain",
|
||||
"http-proxy",
|
||||
"proxy",
|
||||
"heroku"
|
||||
],
|
||||
"main": "./lib/cors-anywhere.js",
|
||||
"dependencies": {
|
||||
@@ -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": {
|
||||
|
||||
32
server.js
32
server.js
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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',
|
||||
@@ -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')
|
||||
@@ -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')
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-env mocha */
|
||||
// Run this specific test using:
|
||||
// npm test -- -f memory
|
||||
var http = require('http');
|
||||
|
||||
21
test/test.js
21
test/test.js
@@ -1,3 +1,4 @@
|
||||
/* eslint-env mocha */
|
||||
require('./setup');
|
||||
|
||||
var createServer = require('../').createServer;
|
||||
@@ -8,7 +9,7 @@ var fs = require('fs');
|
||||
var assert = require('assert');
|
||||
|
||||
var helpTextPath = path.join(__dirname, '../lib/help.txt');
|
||||
var helpText = fs.readFileSync(helpTextPath, { encoding: 'utf8' });
|
||||
var helpText = fs.readFileSync(helpTextPath, {encoding: 'utf8'});
|
||||
|
||||
request.Test.prototype.expectJSON = function(json, done) {
|
||||
this.expect(function(res) {
|
||||
@@ -132,7 +133,7 @@ describe('Basic functionality', function() {
|
||||
.post('/example.com/echopost')
|
||||
.attach('file', path.join(__dirname, 'dummy.txt'))
|
||||
.expect('Access-Control-Allow-Origin', '*')
|
||||
.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);
|
||||
.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
|
||||
});
|
||||
|
||||
it('HEAD with redirect should be followed', function(done) {
|
||||
@@ -494,7 +495,7 @@ describe('setHeaders', function() {
|
||||
.expect('Access-Control-Allow-Origin', '*')
|
||||
.expectJSON({
|
||||
host: 'example.com',
|
||||
'x-powered-by': 'CORS Anywhere'
|
||||
'x-powered-by': 'CORS Anywhere',
|
||||
}, done);
|
||||
});
|
||||
|
||||
@@ -505,7 +506,7 @@ describe('setHeaders', function() {
|
||||
.expect('Access-Control-Allow-Origin', '*')
|
||||
.expectJSON({
|
||||
host: 'example.com',
|
||||
'x-powered-by': 'CORS Anywhere'
|
||||
'x-powered-by': 'CORS Anywhere',
|
||||
}, done);
|
||||
});
|
||||
});
|
||||
@@ -527,7 +528,7 @@ describe('setHeaders + removeHeaders', function() {
|
||||
.expect('Access-Control-Allow-Origin', '*')
|
||||
.expectJSON({
|
||||
host: 'example.com',
|
||||
'x-powered-by': 'CORS Anywhere'
|
||||
'x-powered-by': 'CORS Anywhere',
|
||||
}, done);
|
||||
});
|
||||
|
||||
@@ -538,7 +539,7 @@ describe('setHeaders + removeHeaders', function() {
|
||||
.expect('Access-Control-Allow-Origin', '*')
|
||||
.expectJSON({
|
||||
host: 'example.com',
|
||||
'x-powered-by': 'CORS Anywhere'
|
||||
'x-powered-by': 'CORS Anywhere',
|
||||
}, done);
|
||||
});
|
||||
});
|
||||
@@ -547,8 +548,8 @@ describe('httpProxyOptions.xfwd=false', function() {
|
||||
before(function() {
|
||||
cors_anywhere = createServer({
|
||||
httpProxyOptions: {
|
||||
xfwd: false
|
||||
}
|
||||
xfwd: false,
|
||||
},
|
||||
});
|
||||
cors_anywhere_port = cors_anywhere.listen(0).address().port;
|
||||
});
|
||||
@@ -577,8 +578,8 @@ describe('httpProxyOptions.getProxyForUrl', function() {
|
||||
|
||||
cors_anywhere = createServer({
|
||||
httpProxyOptions: {
|
||||
xfwd: false
|
||||
}
|
||||
xfwd: false,
|
||||
},
|
||||
});
|
||||
cors_anywhere_port = cors_anywhere.listen(0).address().port;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user