feat: migrate to cloudflare d1 and queues

This commit is contained in:
2025-11-28 16:32:35 +08:00
parent 00e1f82d85
commit bd958fb1ab
19 changed files with 294 additions and 276 deletions

View File

@@ -1,10 +1,22 @@
import { DurableObject, env } from "cloudflare:workers";
import { GraphQLClient } from "graphql-request";
import { graphql, type ResultOf } from "gql.tada";
import { print } from "graphql";
import { z } from "zod";
import { GetTitleQuery, _fetchTitleFromAnilist } from "~/libs/anilist/getTitle";
import { sleep } from "~/libs/sleep.ts";
import type { Title } from "~/types/title";
import { MediaFragment } from "~/types/title/mediaFragment";
const GetTitleQuery = graphql(
`
query GetTitle($id: Int!) {
Media(id: $id) {
...Media
}
}
`,
[MediaFragment],
);
const nextAiringEpisodeSchema = z.nullable(
z.object({
@@ -29,12 +41,13 @@ export class AnilistDurableObject extends DurableObject {
switch (operationName) {
case "GetTitle": {
const { variables } = body;
const cache = await this.state.storage.get(variables.id);
if (cache) {
return new Response(JSON.stringify(cache), {
headers: { "Content-Type": "application/json" },
});
}
const storageKey = variables.id;
const cache = await this.state.storage.get(storageKey);
// if (cache) {
// return new Response(JSON.stringify(cache), {
// headers: { "Content-Type": "application/json" },
// });
// }
const anilistResponse = await this.fetchTitleFromAnilist(
variables.id,
@@ -45,7 +58,7 @@ export class AnilistDurableObject extends DurableObject {
);
const airingAt = (nextAiringEpisode?.airingAt ?? 0) * 1000;
await this.state.storage.put(variables.id, anilistResponse);
await this.state.storage.put(storageKey, anilistResponse);
if (airingAt) {
await this.state.storage.setAlarm(airingAt);
await this.state.storage.put(`alarm:${variables.id}`, airingAt);
@@ -63,7 +76,6 @@ export class AnilistDurableObject extends DurableObject {
async alarm() {
const now = Date.now();
const alarms = await this.state.storage.list({ prefix: "alarm:" });
console.log("alarm", now, alarms);
for (const [id, ttl] of Object.entries(alarms)) {
if (now >= ttl) {
await this.state.storage.delete(id);
@@ -75,30 +87,70 @@ export class AnilistDurableObject extends DurableObject {
id: number,
token?: string | undefined,
): Promise<Title | undefined> {
const client = new GraphQLClient("https://graphql.anilist.co/");
const headers = new Headers();
const headers: any = {
"Content-Type": "application/json",
};
if (token) {
headers.append("Authorization", `Bearer ${token}`);
headers["Authorization"] = `Bearer ${token}`;
}
return client
.request(GetTitleQuery, { id }, headers)
.then((data) => data?.Media ?? undefined)
.catch((error) => {
if (error.message.includes("Not Found")) {
return undefined;
}
if (error.response?.status === 429) {
console.log(
"429, retrying in",
error.response.headers.get("Retry-After"),
);
return sleep(
Number(error.response.headers.get("Retry-After")!) * 1000,
).then(() => this.fetchTitleFromAnilist(id, token));
}
const response = await fetch("http://localhost:3000/proxy", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
url: "https://graphql.anilist.co/",
method: "POST",
headers, // Pass the original headers here
data: {
operationName: "GetTitle",
query: print(GetTitleQuery),
variables: { id },
},
}),
});
throw error;
});
// 1. Handle Rate Limiting (429)
if (response.status === 429) {
const retryAfter = response.headers.get("Retry-After");
console.log("429, retrying in", retryAfter);
await sleep(Number(retryAfter || 1) * 1000); // specific fallback or ensure logic
return this.fetchTitleFromAnilist(id, token);
}
// 2. Handle HTTP Errors (like 404 or 500)
if (!response.ok) {
// If it is specifically a 404 Not Found HTTP status
if (response.status === 404) {
return undefined;
}
console.error(JSON.stringify(await response.json(), null, 2));
// Throw for other HTTP errors to be caught by caller
throw new Error(`HTTP Error: ${response.status} ${response.statusText}`);
}
// 3. Parse JSON
// We cast this to ResultOf<typeof GetTitleQuery> to maintain Tada type safety
const result = (await response.json().then((json) => json.data)) as {
data?: ResultOf<typeof GetTitleQuery>;
errors?: any[];
};
// 4. Handle GraphQL Specific Errors (Anilist might return 200 OK but include errors)
if (result.errors && result.errors.length > 0) {
const errorMessage = JSON.stringify(result.errors);
if (errorMessage.includes("Not Found")) {
return undefined;
}
throw new Error(`GraphQL Error: ${errorMessage}`);
}
return result.data?.Media ?? undefined;
}
}