-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add support for Cloudflare Request Attributes #1
base: master
Are you sure you want to change the base?
Conversation
Cloudflare workers now includes [Request Attributes](https://developers.cloudflare.com/workers/reference/request-attributes/). These attributes are accessible every request from the cf object. i.e: `request.cf`. This PR allows us to set and get the request attribute from `request.cf`. To set a cloudflare request attribute: ```js const request = new Request('https://example.com', {cf: {country: 'AUS'}}) ``` To access the request attribute: ```js request.cf ```
@@ -338,6 +338,7 @@ export function Request(input, options) { | |||
this.mode = options.mode || this.mode || null | |||
this.signal = options.signal || this.signal | |||
this.referrer = null | |||
this.cf = options.cf || {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to be stricter about what is possible on the cf
object otherwise the behaviour doesn't align with the Cloudflare implementation. My proposal here is that we drop any keys that are not defined in the controlling Cloudflare feature docs to ensure that if a key is set, it will remove it and cannot be asserted against. Without this, someone could any key to this object, test against it but not have it do anything within the worker environment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great start ⭐️ I think we need to be stricter on the implementation of what is available on this object though to prevent unnoticed regressions with the custom cf
features.
Can we also update the name of this module to be something like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, but I wonder if we could use Object.defineProperty instead to add the Cloudflare property? Perhaps a simple jest fetch wrapper?
It would be good to add a section to the docs/readme about the Cloudflare specific features we're adding.
@@ -410,6 +410,11 @@ exercise.forEach(function(exerciseMode) { | |||
}) | |||
}) | |||
|
|||
test('construct with Cloudflare Request Attribute', function() { | |||
var request = new Request('https://fetch.spec.whatwg.org/', {cf: {country: 'AUS'}}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: country code should be AU
.
@@ -338,6 +338,7 @@ export function Request(input, options) { | |||
this.mode = options.mode || this.mode || null | |||
this.signal = options.signal || this.signal | |||
this.referrer = null | |||
this.cf = options.cf || {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
Cloudflare workers now includes Request Attributes. These attributes are accessible every request on a cloudflare worker via
request.cf
.This PR allows us to set and get the request attribute from
request.cf
which we can use when writing our tests for a cloudflare worker script.To set a cloudflare request attribute:
To access the request attribute:
Example of how it works during testing:
https://github.com/envato/sites-cloudflare-worker/pull/3/files#diff-feb5a52860b926eb3cbf73e6758f50baR7