-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.ts
220 lines (208 loc) · 6.5 KB
/
fetch.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { getDB } from "./dbConnect";
interface IFetch {
fetchArgs: string;
lastAttemptedFetchAt: Date;
lastSuccessfulFetchAt: Date;
lastFailedFetchAt: Date;
lastResult: string;
lastError: string;
}
// DB-backed fetch function that will return stale stuff
const rawDbFetch = async <T = string>(
input: string | URL,
init?: RequestInit | null,
options?: {
parseJson?: boolean;
/**
* The given number will be converted to an integer by rounding down.
* By default, no maximum age is set and the preview session finishes
* when the client shuts down (browser is closed).
*/
maxAge?: number;
},
): Promise<T> => {
const sanitizedInit = init && {
...init,
headers: init?.headers ? { ...init.headers, cookie: undefined } : undefined,
};
const fetchArgs = JSON.stringify({ input, init: sanitizedInit });
const filter = { fetchArgs };
const now = new Date();
const db = await getDB();
const fetchesCollection = db.collection<IFetch>("fetches");
const fetchRow = await fetchesCollection.findOne(filter);
let result: string | null = null;
let parsedResult: T | null = null;
let error: unknown;
if (fetchRow) {
// In seconds
let stale = false;
if (typeof options?.maxAge === "number") {
const rowAge = Math.floor(
(Number(now) - Number(fetchRow.lastSuccessfulFetchAt)) / 1000,
);
if (rowAge > options.maxAge || options.maxAge === 0) stale = true;
}
if (!stale) {
if (fetchRow.lastResult) {
result = fetchRow.lastResult;
parsedResult = (
options?.parseJson === false ? result : JSON.parse(result)
) as T;
// console.info(`DB HIT ${String(input)}`);
}
} else {
console.info(`DB STALE ${String(input)}`);
}
}
try {
if (!result) {
await fetchesCollection.updateOne(
filter,
{ $set: { lastAttemptedFetchAt: now } },
{ upsert: true },
);
console.info(
`DB FETCHING ${String(input)} ${
process.env.NODE_ENV === "production"
? ""
: new Error("").stack
?.trim()
.replace(
/at process.processTicksAndRejections \(node:internal\/process\/task_queues:95:5\)/g,
"",
)
.replace(/^Error/g, "")
.replace(/webpack-internal:\/\/\/\(rsc\)\//g, "")
.replace(/:\d+:\d+/g, "")
.trim() || ""
} `.trim(),
);
const response = await fetch(input, {
signal: AbortSignal.timeout(10000),
...init,
next: { revalidate: 0 },
});
if (
response.status !== 200 &&
// Don't consider 401s from the toplogger ascends endpoint as errors because
// some users have their accounts set up to not allow access
!(
String(input).includes("api.toplogger.nu/v1/ascends.json") &&
response.status === 401
) &&
// Don't consider 404s from the toplogger holds endpoint as errors because
// sometimes holds are removed from the database and i don't know
// what i can do about it
!(String(input).match(/\/holds\//) && response.status === 404)
) {
error = await response.text();
await fetchesCollection.updateOne(filter, {
$set: {
lastError: String(error),
lastFailedFetchAt: now,
},
});
} else {
result = await response.text();
parsedResult = (
options?.parseJson === false ? result : JSON.parse(result)
) as T;
if (
String(input).includes("www.fitocracy.com") &&
// @ts-expect-error - don't know how to fix this
(("success" in parsedResult &&
"error" in parsedResult &&
parsedResult.success === false) ||
// @ts-expect-error - don't know how to fix this
("error" in parsedResult && parsedResult.error))
) {
error = parsedResult.error;
parsedResult = null;
await fetchesCollection.updateOne(filter, {
$set: {
lastError: String(error),
lastFailedFetchAt: now,
},
});
} else {
await fetchesCollection.updateOne(filter, {
$set: {
lastResult: result,
lastSuccessfulFetchAt: now,
},
});
}
}
}
} catch (err: unknown) {
error = err;
}
if (error) {
if (fetchRow && fetchRow.lastResult) {
result = fetchRow.lastResult;
parsedResult = (
options?.parseJson === false ? result : JSON.parse(result)
) as T;
console.warn(`DB FETCH FAILED, USED STALE DATA ${String(input)}`);
} else {
console.warn(`DB FETCH FAILED, NO STALE DATA ${String(input)}`);
}
console.error(error);
}
if (!parsedResult) {
if (error instanceof Error) throw error;
if (typeof error === "string") {
let parsedError: unknown;
try {
parsedError = JSON.parse(error);
} catch {
/* empty */
}
// eslint-disable-next-line @typescript-eslint/only-throw-error
if (parsedError) throw parsedError;
throw new Error(error);
}
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw error || new Error("???");
}
return parsedResult;
};
const dbFetchCache = new Map<
RequestInfo | URL,
{ promise: Promise<unknown>; date: Date }
>();
export const cachedDbFetch = async <T>(
input: string | URL,
init?: RequestInit | null,
options?: {
parseJson?: boolean;
/**
* The given number will be converted to an integer by rounding down.
* By default, no maximum age is set and the preview session finishes
* when the client shuts down (browser is closed).
*/
maxAge?: number;
},
): Promise<T> => {
const key = JSON.stringify({ input, init });
let entry = dbFetchCache.get(key);
if (
!entry ||
(typeof options?.maxAge !== "undefined" &&
(options.maxAge === 0 ||
Math.floor((Number(new Date()) - Number(entry.date)) / 1000) >
options.maxAge))
) {
// console.info(`cachedDbFetch MISS ${String(input)}`);
const promise = rawDbFetch<T>(input, init, options).catch(() =>
dbFetchCache.delete(key),
);
entry = { promise, date: new Date() };
dbFetchCache.set(key, entry);
} else {
// console.info(`cachedDbFetch HIT ${String(input)}`);
}
return entry.promise as Promise<T>;
};
export const dbFetch = cachedDbFetch;