forked from strengthenbesiege/Lookout-Quant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrace.js
36 lines (36 loc) · 1.04 KB
/
race.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"use strict";
/**
* Proxy: return the fastest response from given list of origins.
*
* The main idea is that real-world latency depends on a lot on the exact time and location.
* So the best strategy to optimize for latency would be to try several origins,
* all at the same time and only return the first received response.
*
* WARNING: this will also return the first error or non 200 response as well. So it is
* not a good-idea(TM), to use this for any kind of high-availability or redundant setup.
*/
/**
* Cloudflare Worker entrypoint
*/
if (typeof addEventListener === 'function') {
addEventListener('fetch', (e) => {
// work around as strict typescript check doesn't allow e to be of type FetchEvent
const fe = e;
const url = new URL(fe.request.url);
const origins = url.searchParams.getAll('o');
const responses = origins.map(o => fetch(o));
fe.respondWith(Promise.race(responses));
});
}
© 2022 GitHub, Inc.
Terms
Privacy
Security
Status
Docs
Contact GitHub
#
API
Training
Blog
About