|
1 | | -/** |
2 | | - * |
3 | | - * @param {Promise<Response>} originalResponse |
4 | | - * @returns {Promise<Response>} |
5 | | - */ |
6 | | -async function addCorsHeaders(originalResponse) { |
7 | | - let response = await originalResponse; |
8 | | - // Recreate the response so you can modify the headers |
9 | | - |
10 | | - response = new Response(response.body, response); |
11 | | - // Set CORS headers |
12 | | - |
13 | | - response.headers.set('Access-Control-Allow-Origin', '*'); |
14 | | - |
15 | | - // Append to/Add Vary header so browser will cache response correctly |
16 | | - response.headers.append('Vary', 'Origin'); |
17 | | - |
18 | | - return response; |
19 | | -} |
20 | | - |
21 | | -addEventListener('fetch', (event) => { |
22 | | - const urlObject = new URL(event.request.url); |
23 | | - let useCors = true; |
24 | | - |
25 | | - urlObject.hostname = 'webflow.bitrise.io'; |
26 | | - |
27 | | - if (urlObject.pathname.match(/^\/changelog\/api\/(.+\.json)$/)) { |
28 | | - urlObject.hostname = 'discuss.bitrise.io'; |
29 | | - urlObject.pathname = urlObject.pathname.replace(/^\/changelog\/api\/(.+\.json)$/, '/$1'); |
30 | | - useCors = true; |
31 | | - } else if (urlObject.pathname.match(/^\/changelog\.json$/)) { |
32 | | - urlObject.hostname = 'web-cdn.bitrise.io'; |
33 | | - urlObject.pathname = 'changelog.json'; |
34 | | - useCors = true; |
35 | | - } else if (urlObject.pathname.match(/^\/changelog_latest\.json$/)) { |
36 | | - urlObject.hostname = 'web-cdn.bitrise.io'; |
37 | | - urlObject.pathname = 'changelog_latest.json'; |
38 | | - useCors = true; |
39 | | - } else if (urlObject.pathname.match(/^\/changelog\.xml$/)) { |
40 | | - urlObject.hostname = 'web-cdn.bitrise.io'; |
41 | | - urlObject.pathname = 'changelog.xml'; |
42 | | - useCors = true; |
43 | | - } else if (urlObject.pathname.match(/^\/changelog\/.+/)) { |
44 | | - urlObject.pathname = '/changelog/topic'; |
45 | | - } |
46 | | - |
47 | | - if (useCors) { |
48 | | - event.respondWith(addCorsHeaders(fetch(urlObject))); |
49 | | - } else { |
50 | | - event.respondWith(fetch(urlObject)); |
51 | | - } |
52 | | -}); |
| 1 | +export default { |
| 2 | + async fetch(request) { |
| 3 | + const urlObject = new URL(request.url); |
| 4 | + |
| 5 | + let useCors = false; |
| 6 | + let transformBody = null; |
| 7 | + urlObject.hostname = 'webflow.bitrise.io'; |
| 8 | + |
| 9 | + if (urlObject.pathname.match(/^\/changelog\/api\/(.+\.json)$/)) { |
| 10 | + urlObject.hostname = 'discuss.bitrise.io'; |
| 11 | + urlObject.pathname = urlObject.pathname.replace(/^\/changelog\/api\/(.+\.json)$/, '/$1'); |
| 12 | + useCors = true; |
| 13 | + } else if (urlObject.pathname.match(/^\/changelog\.json$/)) { |
| 14 | + urlObject.hostname = 'web-cdn.bitrise.io'; |
| 15 | + urlObject.pathname = 'changelog.json'; |
| 16 | + useCors = true; |
| 17 | + } else if (urlObject.pathname.match(/^\/changelog_latest\.json$/)) { |
| 18 | + urlObject.hostname = 'web-cdn.bitrise.io'; |
| 19 | + urlObject.pathname = 'changelog_latest.json'; |
| 20 | + useCors = true; |
| 21 | + } else if (urlObject.pathname.match(/^\/changelog\.xml$/)) { |
| 22 | + urlObject.hostname = 'web-cdn.bitrise.io'; |
| 23 | + urlObject.pathname = 'changelog.xml'; |
| 24 | + } else if (urlObject.pathname.match(/^\/changelog\/.+/)) { |
| 25 | + const originalPath = urlObject.pathname; |
| 26 | + urlObject.pathname = '/changelog/topic'; |
| 27 | + transformBody = (data) => data.replace('https://bitrise.io/changelog/topic', `https://bitrise.io${originalPath}`); |
| 28 | + } |
| 29 | + |
| 30 | + if (!useCors && !transformBody) { |
| 31 | + return fetch(urlObject); |
| 32 | + } |
| 33 | + |
| 34 | + let response = await fetch(urlObject); |
| 35 | + const responseBody = await response.text(); |
| 36 | + response = new Response(responseBody, response); |
| 37 | + |
| 38 | + if (transformBody) { |
| 39 | + response.body = transformBody(responseBody); |
| 40 | + } |
| 41 | + |
| 42 | + if (useCors) { |
| 43 | + // Set CORS headers |
| 44 | + response.headers.set('Access-Control-Allow-Origin', '*'); |
| 45 | + // Append to/Add Vary header so browser will cache response correctly |
| 46 | + response.headers.append('Vary', 'Origin'); |
| 47 | + } |
| 48 | + |
| 49 | + return response; |
| 50 | + }, |
| 51 | +}; |
0 commit comments