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,5 +1,41 @@
import { Constants } from './constants';
const invalidateTokenCache = (event: FetchEvent, cache: Cache) => {
console.log('Invalidating token cache');
event && event.waitUntil(cache.delete(guestTokenRequestCacheDummy));
}
const tokenHeaders: { [header: string]: string } = {
Authorization: Constants.GUEST_BEARER_TOKEN,
...Constants.BASE_HEADERS
};
const guestTokenRequest = new Request(
`${Constants.TWITTER_API_ROOT}/1.1/guest/activate.json`,
{
method: 'POST',
headers: tokenHeaders,
cf: {
cacheEverything: true,
cacheTtl: Constants.GUEST_TOKEN_MAX_AGE
},
body: ''
}
);
/* 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. */
const guestTokenRequestCacheDummy = new Request(
`${Constants.TWITTER_API_ROOT}/1.1/guest/activate.json`,
{
method: 'GET',
cf: {
cacheEverything: true,
cacheTtl: Constants.GUEST_TOKEN_MAX_AGE
}
}
);
export const fetchUsingGuest = async (
status: string,
event: FetchEvent
@ -7,37 +43,6 @@ export const fetchUsingGuest = async (
let apiAttempts = 0;
let newTokenGenerated = false;
const tokenHeaders: { [header: string]: string } = {
Authorization: Constants.GUEST_BEARER_TOKEN,
...Constants.BASE_HEADERS
};
const guestTokenRequest = new Request(
`${Constants.TWITTER_API_ROOT}/1.1/guest/activate.json`,
{
method: 'POST',
headers: tokenHeaders,
cf: {
cacheEverything: true,
cacheTtl: Constants.GUEST_TOKEN_MAX_AGE
},
body: ''
}
);
/* 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. */
const guestTokenRequestCacheDummy = new Request(
`${Constants.TWITTER_API_ROOT}/1.1/guest/activate.json`,
{
method: 'GET',
cf: {
cacheEverything: true,
cacheTtl: Constants.GUEST_TOKEN_MAX_AGE
}
}
);
const cache = caches.default;
while (apiAttempts < 10) {
@ -75,7 +80,7 @@ export const fetchUsingGuest = async (
This can effectively mean virtually unlimited (read) access to Twitter's API,
which is very funny. */
activate = await fetch(guestTokenRequest);
activate = await fetch(guestTokenRequest.clone());
}
/* 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.
It's uncommon, but it happens */
console.error('Unknown error while fetching conversation from API');
event && event.waitUntil(cache.delete(guestTokenRequestCacheDummy));
invalidateTokenCache(event, cache);
newTokenGenerated = true;
continue;
}
@ -135,7 +140,7 @@ export const fetchUsingGuest = async (
/* Running out of requests within our rate limit, let's purge the cache */
if (remainingRateLimit < 20) {
console.log(`Purging token on this edge due to low rate limit remaining`);
event && event.waitUntil(cache.delete(guestTokenRequestCacheDummy));
invalidateTokenCache(event, cache);
}
if (