Moved some headers out of fetch function

This commit is contained in:
dangered wolf 2022-08-18 16:49:38 -04:00
parent 397f5eeb0e
commit a410c0c044
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58

View file

@ -1,18 +1,16 @@
import { Constants } from './constants'; import { Constants } from './constants';
export const fetchUsingGuest = async ( const invalidateTokenCache = (event: FetchEvent, cache: Cache) => {
status: string, console.log('Invalidating token cache');
event: FetchEvent event && event.waitUntil(cache.delete(guestTokenRequestCacheDummy));
): Promise<TimelineBlobPartial> => { }
let apiAttempts = 0;
let newTokenGenerated = false;
const tokenHeaders: { [header: string]: string } = { const tokenHeaders: { [header: string]: string } = {
Authorization: Constants.GUEST_BEARER_TOKEN, Authorization: Constants.GUEST_BEARER_TOKEN,
...Constants.BASE_HEADERS ...Constants.BASE_HEADERS
}; };
const guestTokenRequest = new Request( const guestTokenRequest = new Request(
`${Constants.TWITTER_API_ROOT}/1.1/guest/activate.json`, `${Constants.TWITTER_API_ROOT}/1.1/guest/activate.json`,
{ {
method: 'POST', method: 'POST',
@ -23,9 +21,9 @@ export const fetchUsingGuest = async (
}, },
body: '' body: ''
} }
); );
/* A dummy version of the request only used for Cloudflare caching purposes. /* A dummy version of the request only used for Cloudflare caching purposes.
The reason it exists at all is because Cloudflare won't cache POST requests. */ The reason it exists at all is because Cloudflare won't cache POST requests. */
const guestTokenRequestCacheDummy = new Request( const guestTokenRequestCacheDummy = new Request(
`${Constants.TWITTER_API_ROOT}/1.1/guest/activate.json`, `${Constants.TWITTER_API_ROOT}/1.1/guest/activate.json`,
@ -36,7 +34,14 @@ export const fetchUsingGuest = async (
cacheTtl: Constants.GUEST_TOKEN_MAX_AGE cacheTtl: Constants.GUEST_TOKEN_MAX_AGE
} }
} }
); );
export const fetchUsingGuest = async (
status: string,
event: FetchEvent
): Promise<TimelineBlobPartial> => {
let apiAttempts = 0;
let newTokenGenerated = false;
const cache = caches.default; const cache = caches.default;
@ -75,7 +80,7 @@ export const fetchUsingGuest = async (
This can effectively mean virtually unlimited (read) access to Twitter's API, This can effectively mean virtually unlimited (read) access to Twitter's API,
which is very funny. */ which is very funny. */
activate = await fetch(guestTokenRequest); activate = await fetch(guestTokenRequest.clone());
} }
/* Let's grab that guest_token so we can use it */ /* Let's grab that guest_token so we can use it */
@ -123,7 +128,7 @@ export const fetchUsingGuest = async (
/* We'll usually only hit this if we get an invalid response from Twitter. /* We'll usually only hit this if we get an invalid response from Twitter.
It's uncommon, but it happens */ It's uncommon, but it happens */
console.error('Unknown error while fetching conversation from API'); console.error('Unknown error while fetching conversation from API');
event && event.waitUntil(cache.delete(guestTokenRequestCacheDummy)); invalidateTokenCache(event, cache);
newTokenGenerated = true; newTokenGenerated = true;
continue; continue;
} }
@ -135,7 +140,7 @@ export const fetchUsingGuest = async (
/* Running out of requests within our rate limit, let's purge the cache */ /* Running out of requests within our rate limit, let's purge the cache */
if (remainingRateLimit < 20) { if (remainingRateLimit < 20) {
console.log(`Purging token on this edge due to low rate limit remaining`); console.log(`Purging token on this edge due to low rate limit remaining`);
event && event.waitUntil(cache.delete(guestTokenRequestCacheDummy)); invalidateTokenCache(event, cache);
} }
if ( if (