let originalURL = new URL(request.url)
let authUrl = new URL(request.url)
authUrl.pathname = "/api-authorization-page"
let authResponse = await fetch(authUrl, {
Stick originalURL into your dead CPU time between await and fetch.
let authResponse = fetch(authUrl, { blah: blah});
let originalURL = new URL(request.url);
authResponse = await authResponse;
Second, I dont think non-CF dev ever timed it, but “await cache.match(req)” is synchronous blocking I/O, somewhere CF wrote about it in their blog, but the cache is not always on the same rack sever’s SSDs/RAM ( A Tour Inside CloudFlare's Latest Generation Servers ) so “cdn cache” is a trip inside a POP over ethernet/infiniband. many microseconds/upto a ms or 2 or 3.
If I would write it, I would call fetch(’/api-authorization-page’) and cache.match(url), same time, WITHOUT await. If await fetch(’/api-authorization-page’) returns 200, then await on cache.match(url), which is latency free by now since by now “cache.match(url)” in sitting in RAM in your worker process.
Only in an auth failed/error/exceptional situation, you would throw away the free (cpu/ram/IO you dont pay for) cache api result. Bad auth client has NO IDEA cache API pulled up his forbidden content and threw it away.
Although this is sketchy as **** and not FBI/CIA-resistant, have CF to origin be cleartext HTTP, NOT HTTPS, and whitelist incoming connections to Cloudflare’s publish IP ranges. Also check the incoming header “Cf-Worker” origin side to make sure YOUR worker is connecting to your origin and not a 3rd party (hacker) worker Is the CF-Worker header from worker fetch() reliable? - #2 by KentonVarda Workers fetch API adds unwanted headers Tons of secret headers on outgoing Fetch .
As others said, you need to write a JS global API auth key cache, so after the “first” (per worker process) auth check, additional client reqs hit the bearer token cache, not to origin, unless you have extreme security design requirements (revoking a key/bearer token on password change must be “effective” within milliseconds, globally). You could also blocking check to origin token first time, put in JS global var cache, then further client reqs, if token found in JS global var cache, return (blocking) cache asset or (blocking) from origin asset, so req >=2 has 1 round trip either to origin for asset or lightning fast (1-2ms) RT to CF POP cache, BUTTTTTTT, BUTTTTTT also no-await, or event.waitUntil call fetch() to check the token each time, then wipe the JS global var toke cache slot. Your ex-good, now-malicious client only succeeded getting 1 unauthorized HTTP req to client side before his account was terminated.
You also brought up DNS query latency, is your origin server’s DNS name orange clouded or fetch() is calling to a “3rd party” non-CF origin from CF’s eyes. What is the TTL of the DNS query to your origin server? are you using CF nameservers or AWS/random register or dynamic dns (cable modem) provider?
Also in addition to using a JS global var as a cache for bearer tokens, you know cache API can be used as a poor man’s KV? except its per-POP/per-DC. This way you save fetch() to origin latency check, even if your customer is changing between wifi/cell constantly (launching new worker process on every couple requests, or launches a new worker process on every unique TCP connection and the 2nd browser to CF TCP connection hits another CF front end server or goes down a different BGP link or gets load balanced to another server by CF’s juniper router or worker #1 is using cpu, not idle, not IO blocked, so a 2nd worker spawns instantly on same front end server and both stay alive getting round robin load balanced so there is always 1 compiled, but idled worker available), or CF infrastructure kills your worker process every 5-10 seconds because your on the “free plan”.
Also I wonder how efficient CF’s OUTGOING TLS implementation is. And how about your TLS config? Without a TLS session resume ID or resume ticket, the origin server must send the FULL 5-15 KB TLS cert every time to CF edge server on every incoming TLS TCP connection from CF to origin. Again, do you clear text HTTP with IP whitelist between CF edge and origin? CF has the argo product to stop the TLS overhead AFAIK. Linux/Windows VPN app on origin server, making 24/7 outgoing connection to CF edge, no public IP required, and then all CF edge to origin requests go down the VPN connection cleartext inside encrypted VPN, and your HTTP server process only sees loopback 127.0.0.1 as the client IP.