Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow caching of CORS headers by setting Access-Control-Max-Age header #77

Merged
merged 3 commits into from
Jul 16, 2017

Conversation

gnjack
Copy link
Contributor

@gnjack gnjack commented Jul 14, 2017

Add maxAge config option. If set, an Access-Control-Max-Age header with this value (in seconds) will be added. This will allow the results of a preflight request to be cached by the browser, avoiding a preflight request being made for every request.
Defaults to not set.

Example: createServer({ maxAge: 600 })
Allow CORS preflight requests to be cached by the browser for 10 minutes.

@coveralls
Copy link

coveralls commented Jul 14, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 881392c on gnjack:maxAge into 70400ab on Rob--W:master.

Copy link
Owner

@Rob--W Rob--W left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR! The functionality, documentation and tests look great.
If you address the few nits, then I can merge it.

Could you rename maxAge to corsMaxAge?

@@ -50,8 +50,11 @@ function isValidHostName(hostname) {
* @param headers {object} Response headers
* @param request {ServerRequest}
*/
function withCORS(headers, request) {
function withCORS(headers, request, maxAge) {
headers['access-control-allow-origin'] = '*';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the following instead of passing an extra parameter to withCORS:
var maxAge = req.corsAnywhereRequestState.location;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. To do this, I had to move the definition of req.corsAnywhereRequestState to before the call to withCORS in the getHandler function.

@@ -234,6 +237,7 @@ function getHandler(options, proxy) {
requireHeader: null, // Require a header to be set?
removeHeaders: [], // Strip these request headers.
setHeaders: {}, // Set these request headers.
maxAge: null, // If set, an Access-Control-Max-Age header with this value (in seconds) will be added.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default to 0 please, based on the following substeps from https://fetch.spec.whatwg.org/#cors-preflight-fetch

  1. Let max-age be the result of extracting header list values given `Access-Control-Max-Age` and response’s header list.
  2. If max-age is failure or null, then set max-age to zero.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Also updated the tests and readme to note that the default behaviour is now an Access-Control-Max-Age header set to 0.

test/test.js Outdated
.get('/example.com')
.expect('Access-Control-Allow-Origin', '*')
.expect('Access-Control-Max-Age', '600')
.expect(200, 'Response from example.com', done);;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace the two ;; with one ;.

README.md Outdated
@@ -107,6 +107,8 @@ proxy requests. The following options are supported:
Example: `["cookie"]`
* dictionary of lowercase strings `setHeaders` - Set headers for the request (overwrites existing ones).
Example: `{"x-powered-by": "CORS Anywhere"}`
* number `maxAge` - If set, an Access-Control-Max-Age header with this value (in seconds) will be added.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add two spaces at the end, then markdown will show a forced line break.

And use "response header" instead of just "header".

Set corsAnywhereRequestState before calling withCORS and use the state instead of a parameter to get corsMaxAge.
@gnjack
Copy link
Contributor Author

gnjack commented Jul 14, 2017

Thanks @Rob--W - I have now implemented all of the above suggested changes.

@coveralls
Copy link

coveralls commented Jul 14, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 10df7c9 on gnjack:maxAge into 70400ab on Rob--W:master.

README.md Outdated
@@ -107,6 +107,9 @@ proxy requests. The following options are supported:
Example: `["cookie"]`
* dictionary of lowercase strings `setHeaders` - Set headers for the request (overwrites existing ones).
Example: `{"x-powered-by": "CORS Anywhere"}`
* number `corsMaxAge` - If set, an Access-Control-Max-Age request header with this value (in seconds) will be added.
Defaults to 0.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can be removed `"If set, ..." implies that the header is not set if not specified, which is already clear.

@@ -52,6 +52,10 @@ function isValidHostName(hostname) {
*/
function withCORS(headers, request) {
headers['access-control-allow-origin'] = '*';
var corsMaxAge = request.corsAnywhereRequestState.corsMaxAge;
if (corsMaxAge != null) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to if (corsMaxAge) {.

test/test.js Outdated
@@ -50,6 +50,7 @@ describe('Basic functionality', function() {
.get('/')
.type('text/plain')
.expect('Access-Control-Allow-Origin', '*')
.expect('Access-Control-Max-Age', '0')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The header should not be added by default. The user agent (browser) will assume 0 if not set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry. I just misinterpreted your suggestion to set the default corsMaxAge to 0 instead of null to mean we should send an Access-Control-Max-Age header with the value of 0 by default.

Actually, the default corsMaxAge should be 0 which will mean no header is sent, correct?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's correct.

@coveralls
Copy link

coveralls commented Jul 14, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 49d429d on gnjack:maxAge into 70400ab on Rob--W:master.

@Rob--W Rob--W merged commit 143eff1 into Rob--W:master Jul 16, 2017
@gnjack gnjack deleted the maxAge branch November 8, 2017 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants