wintercg / fetch

WinterCG Fetch Standard
https://fetch.spec.wintercg.org/
Other
24 stars 0 forks source link

Passing platform-specific meta information with the `Request`/`Response` instances #8

Open maxshirshin opened 2 years ago

maxshirshin commented 2 years ago

Platforms like Cloudflare Workers or Shopify Oxygen may need to add additional information to requests/responses. Cloudflare Workers has a proprietary .cf property which includes fields like geolocation data etc. Shopify's Oxygen runtime has similar needs but uses custom HTTP headers to pass the data.

Since the standard doesn't offer any way to pass extra meta information along with Request/Response instances, it would be great to explore these and similar scenarios to see how the extra meta data may be added. There likely were no use cases for this in the browser world, but on the server side, the situation is different.

Using extra HTTP custom headers might be the most obvious approach, but it has a clear downside that headers have tight size limits, aren't very suitable for holding complex data, and can't contain anything that isn't directly serialisable into a string.

A custom property like .meta or .metadata (which should hold a JavaScript object, leaving its fields up to the vendor implementation) would be more flexible but needs to be standardised.

jimmywarting commented 2 years ago

I do not think Request or Response should have a custom meta property... there are other ways to go around it... like emitting a event that may include both a cf and a request property { cf: { ... }, request: new Request( ... ) }

there is also the possibility to hook in other meta property and bind it to request without actually modifying the request... you could use WeakMap to bind other meta data to it.

const wm = new WeakMap()
const req = new Request( ... )
const cf = { country: 'sweden' }

wm.set(req, cf)
wm.get(req) // return custom meta associated to the request 
blittle commented 2 years ago

This might belong in the environment-metadata work stream.