Skip to content

Commit 45f94b2

Browse files
authored
Merge pull request #392 from the-hideout/normal-fetch-timeout
Use normal fetch timeout signal
2 parents 0879c77 + 94d75eb commit 45f94b2

File tree

7 files changed

+15
-41
lines changed

7 files changed

+15
-41
lines changed

http/cloudflare-kv.mjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { EventEmitter } from 'node:events';
22

3-
import fetchWithTimeout from '../utils/fetch-with-timeout.mjs';
4-
53
const completeEmitter = new EventEmitter();
64

75
const accountId = '424ad63426a1ae47d559873f929eb9fc';
@@ -28,13 +26,13 @@ const checkQueue = async () => {
2826
const url = `https://api.cloudflare.com/client/v4/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${kvName}`;
2927
let response;
3028
try {
31-
response = await fetchWithTimeout(url, {
29+
response = await fetch(url, {
3230
method: 'GET',
3331
headers: {
3432
'Content-Type': 'application/json',
3533
Authorization: `Bearer ${process.env.CLOUDFLARE_TOKEN}`,
3634
},
37-
timeout: 9000,
35+
signal: AbortSignal.timeout(9000),
3836
});
3937
completeEmitter.emit(kvName, response);
4038
} catch (error) {

index.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import schema from './schema.mjs';
2424
import graphqlUtil from './utils/graphql-util.mjs';
2525
import graphQLOptions from './utils/graphql-options.mjs';
2626
import cacheMachine from './utils/cache-machine.mjs';
27-
import fetchWithTimeout from './utils/fetch-with-timeout.mjs';
2827

2928
import { getNightbotResponse, useNightbotOnUrl } from './plugins/plugin-nightbot.mjs';
3029
import { getTwitchResponse } from './plugins/plugin-twitch.mjs';
@@ -114,7 +113,7 @@ async function graphqlHandler(request, env, ctx) {
114113
originUrl.protocol = env.ORIGIN_PROTOCOL;
115114
}
116115
console.log(`Querying origin server ${originUrl}`);
117-
const originResponse = await fetchWithTimeout(originUrl, {
116+
const originResponse = await fetch(originUrl, {
118117
method: 'POST',
119118
body: JSON.stringify({
120119
query,
@@ -124,7 +123,7 @@ async function graphqlHandler(request, env, ctx) {
124123
'Content-Type': 'application/json',
125124
'cache-check-complete': 'true',
126125
},
127-
timeout: 20000
126+
signal: AbortSignal.timeout(20000),
128127
});
129128
if (originResponse.status !== 200) {
130129
throw new Error(`${originResponse.status} ${await originResponse.text()}`);

plugins/plugin-graphql-origin.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Pass the request to an origin server if USE_ORIGIN is set to 'true'
2-
import fetchWithTimeout from '../utils/fetch-with-timeout.mjs';
32

43
export default function useGraphQLOrigin(env) {
54
return {
@@ -12,13 +11,13 @@ export default function useGraphQLOrigin(env) {
1211
if (env.ORIGIN_OVERRIDE) {
1312
originUrl.host = env.ORIGIN_OVERRIDE;
1413
}
15-
const queryResult = await fetchWithTimeout(originUrl, {
14+
const queryResult = await fetch(originUrl, {
1615
method: request.method,
1716
body: JSON.stringify(params),
1817
headers: {
1918
'Content-Type': 'application/json',
2019
},
21-
timeout: 20000
20+
signal: AbortSignal.timeout(20000),
2221
});
2322
if (queryResult.status !== 200) {
2423
throw new Error(`${queryResult.status} ${queryResult.statusText}: ${await queryResult.text()}`);

plugins/plugin-lite-api.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import DataSource from '../datasources/index.mjs';
22
import graphqlUtil from '../utils/graphql-util.mjs';
33
import cacheMachine from '../utils/cache-machine.mjs';
4-
import fetchWithTimeout from '../utils/fetch-with-timeout.mjs';
54

65
export const liteApiPathRegex = /\/api\/v1(?<gameMode>\/\w+)?\/(?<endpoint>item[\w\/]*)/;
76

@@ -74,7 +73,7 @@ export async function getLiteApiResponse(request, url, env, serverContext) {
7473
if (env.ORIGIN_OVERRIDE) {
7574
originUrl.host = env.ORIGIN_OVERRIDE;
7675
}
77-
const response = await fetchWithTimeout(originUrl, {
76+
const response = await fetch(originUrl, {
7877
method: 'POST',
7978
body: JSON.stringify({
8079
q,
@@ -87,7 +86,7 @@ export async function getLiteApiResponse(request, url, env, serverContext) {
8786
headers: {
8887
'cache-check-complete': 'true',
8988
},
90-
timeout: 20000
89+
signal: AbortSignal.timeout(20000),
9190
});
9291
if (response.status !== 200) {
9392
throw new Error(`${response.status} ${await response.text()}`);

plugins/plugin-nightbot.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import DataSource from '../datasources/index.mjs';
22
import cacheMachine from '../utils/cache-machine.mjs';
33
import graphqlUtil from '../utils/graphql-util.mjs';
4-
import fetchWithTimeout from '../utils/fetch-with-timeout.mjs';
54

65
function capitalize(s) {
76
return s && s[0].toUpperCase() + s.slice(1);
@@ -63,12 +62,12 @@ export async function getNightbotResponse(request, url, env, serverContext) {
6362
if (env.ORIGIN_OVERRIDE) {
6463
originUrl.host = env.ORIGIN_OVERRIDE;
6564
}
66-
const response = await fetchWithTimeout(originUrl, {
65+
const response = await fetch(originUrl, {
6766
method: 'GET',
6867
headers: {
6968
'cache-check-complete': 'true',
7069
},
71-
timeout: 20000
70+
signal: AbortSignal.timeout(20000),
7271
});
7372
if (response.status !== 200) {
7473
throw new Error(`${response.status} ${await response.text()}`);

utils/cache-machine.mjs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import fetchWithTimeout from './fetch-with-timeout.mjs';
2-
31
// cache url
42
const cacheUrl = 'https://cache.tarkov.dev'
53

@@ -74,10 +72,11 @@ const cacheMachine = {
7472
return false;
7573
}
7674

77-
const response = await fetchWithTimeout(`${cacheUrl}/api/cache?key=${key}`, {
75+
const response = await fetch(`${cacheUrl}/api/cache?key=${key}`, {
7876
headers: {
7977
'Authorization': `Basic ${env.CACHE_BASIC_AUTH}`
80-
},
78+
},
79+
signal: AbortSignal.timeout(5000),
8180
});
8281
cacheFailCount = 0;
8382
if (response.status === 200) {
@@ -125,14 +124,14 @@ const cacheMachine = {
125124
console.log(`Caching ${body.length} byte response for ${env.ENVIRONMENT} environment${ttl ? ` for ${ttl} seconds` : ''}`);
126125

127126
// Update the cache
128-
const response = await fetchWithTimeout(`${cacheUrl}/api/cache`, {
127+
const response = await fetch(`${cacheUrl}/api/cache`, {
129128
body: JSON.stringify({ key, value: body, ttl }),
130129
method: 'POST',
131130
headers: {
132131
'content-type': 'application/json;charset=UTF-8',
133132
'Authorization': `Basic ${env.CACHE_BASIC_AUTH}`
134133
},
135-
timeout: 20000,
134+
signal: AbortSignal.timeout(20000),
136135
});
137136
console.log('Response cached');
138137
response.body.cancel();

utils/fetch-with-timeout.mjs

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)