Compare commits
24 Commits
eb6dc545e2
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a821a2c98 | |||
| 734ce25aa2 | |||
| fe3e4b65f4 | |||
| cd19f39005 | |||
| 0b237d542b | |||
| c01e005afb | |||
| e5d9d62be2 | |||
| 8d63d4fa5e | |||
| 07bece1f6c | |||
| 2ed38e92bc | |||
| 26ca15d4aa | |||
| 4c96f58cb0 | |||
| b64bd4fc26 | |||
| 4c2d0a9177 | |||
| dc60a1e045 | |||
| 6570c25617 | |||
| 6f795bdde0 | |||
| 243c279ca9 | |||
| 286824e3a1 | |||
| b26d22ad91 | |||
| 3c5685dbdb | |||
| c527a6eac5 | |||
| f16ac80b7e | |||
| cd04a75b06 |
@@ -1,16 +0,0 @@
|
|||||||
node_modules
|
|
||||||
Dockerfile*
|
|
||||||
docker-compose*
|
|
||||||
.dockerignore
|
|
||||||
.git
|
|
||||||
.gitignore
|
|
||||||
README.md
|
|
||||||
LICENSE
|
|
||||||
.vscode
|
|
||||||
Makefile
|
|
||||||
helm-charts
|
|
||||||
.env
|
|
||||||
.dev.vars
|
|
||||||
.editorconfig
|
|
||||||
.idea
|
|
||||||
coverage*
|
|
||||||
41
Dockerfile
41
Dockerfile
@@ -1,41 +0,0 @@
|
|||||||
# use the official Bun image
|
|
||||||
# see all versions at https://hub.docker.com/r/oven/bun/tags
|
|
||||||
FROM oven/bun:1 as base
|
|
||||||
WORKDIR /usr/app
|
|
||||||
|
|
||||||
# install dependencies into temp directory
|
|
||||||
# this will cache them and speed up future builds
|
|
||||||
FROM base AS install
|
|
||||||
RUN mkdir -p /tmp/dev
|
|
||||||
COPY package.json bun.lockb /tmp/dev/
|
|
||||||
RUN cd /tmp/dev && bun install --frozen-lockfile
|
|
||||||
|
|
||||||
# install with --production (exclude devDependencies)
|
|
||||||
RUN mkdir -p /tmp/prod
|
|
||||||
COPY package.json bun.lockb /tmp/prod/
|
|
||||||
RUN cd /tmp/prod && bun install --frozen-lockfile --production
|
|
||||||
|
|
||||||
# copy node_modules from temp directory
|
|
||||||
# then copy all (non-ignored) project files into the image
|
|
||||||
FROM base AS prerelease
|
|
||||||
COPY --from=install /tmp/dev/node_modules node_modules
|
|
||||||
COPY . .
|
|
||||||
|
|
||||||
# [optional] tests & build
|
|
||||||
ENV NODE_ENV=production
|
|
||||||
RUN bun test
|
|
||||||
RUN bun build --compile src/index.ts --outfile=aniplay
|
|
||||||
|
|
||||||
# copy production dependencies and source code into final image
|
|
||||||
FROM base AS release
|
|
||||||
COPY --from=install /tmp/prod/node_modules node_modules
|
|
||||||
COPY --from=prerelease /usr/app/src ./src
|
|
||||||
COPY --from=prerelease /usr/app/package.json .
|
|
||||||
COPY --from=prerelease /usr/app/tsconfig.json .
|
|
||||||
# TODO: uncomment once v2 is ready
|
|
||||||
# COPY --from=prerelease /usr/app/drizzle.config.ts .
|
|
||||||
|
|
||||||
# run the app
|
|
||||||
USER bun
|
|
||||||
EXPOSE 3000
|
|
||||||
ENTRYPOINT [ "bun", "run", "prod:server" ]
|
|
||||||
70
README.md
70
README.md
@@ -1,12 +1,72 @@
|
|||||||
```
|
# Aniplay API
|
||||||
npm install
|
|
||||||
npm run dev
|
API for [Aniplay](https://github.com/silverAndroid/aniplay), built with Cloudflare Workers, Hono, and Drizzle ORM.
|
||||||
|
|
||||||
|
## Tech Stack
|
||||||
|
|
||||||
|
- **Cloudflare Workers**: Serverless execution environment.
|
||||||
|
- **Hono**: Ultrafast web framework (OpenAPI).
|
||||||
|
- **GraphQL**: Used internally for communicating with the [AniList](https://anilist.co) API.
|
||||||
|
- **Drizzle ORM**: TypeScript ORM for D1 (Cloudflare's serverless SQL database).
|
||||||
|
- **Vitest**: Testing framework.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
- **Node.js**
|
||||||
|
- **pnpm**: Package manager.
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
1. **Installation**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm install
|
||||||
```
|
```
|
||||||
|
|
||||||
|
2. **Environment Setup**
|
||||||
|
Generate the environment types:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm exec wrangler types
|
||||||
```
|
```
|
||||||
npm run deploy
|
|
||||||
|
3. **Database Setup**
|
||||||
|
Apply migrations to the local D1 database:
|
||||||
|
```bash
|
||||||
|
pnpm exec wrangler d1 migrations apply aniplay
|
||||||
```
|
```
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
If a route is internal-only or doesn't need to appear on the OpenAPI spec (that's autogenerated by Hono), use the `Hono` class. Otherwise, use the `OpenAPIHono` class from `@hono/zod-openapi`.
|
### Running Locally
|
||||||
|
|
||||||
|
Start the development server:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm run dev
|
||||||
|
```
|
||||||
|
|
||||||
|
### Testing
|
||||||
|
|
||||||
|
Run the tests using Vitest:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm test
|
||||||
|
```
|
||||||
|
|
||||||
|
## Deployment
|
||||||
|
|
||||||
|
Deploy to Cloudflare Workers:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm run deploy
|
||||||
|
```
|
||||||
|
|
||||||
|
## Project Structure
|
||||||
|
|
||||||
|
- `src/controllers`: API route handlers (titles, episodes, search, etc.)
|
||||||
|
- `src/libs`: Shared utilities and logic (AniList integration, background tasks)
|
||||||
|
- `src/middleware`: Middleware handlers (authentication, authorization, etc.)
|
||||||
|
- `src/models`: Database schema and models
|
||||||
|
- `src/scripts`: Utility scripts for maintenance and setup
|
||||||
|
- `src/types`: TypeScript type definitions
|
||||||
|
|||||||
@@ -7,8 +7,6 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "wrangler dev src/index.ts --port 8080",
|
"dev": "wrangler dev src/index.ts --port 8080",
|
||||||
"deploy": "wrangler deploy --minify src/index.ts",
|
"deploy": "wrangler deploy --minify src/index.ts",
|
||||||
"env:generate": "tsx src/scripts/generateEnv.ts",
|
|
||||||
"env:verify": "tsx src/scripts/verifyEnv.ts",
|
|
||||||
"db:generate": "drizzle-kit generate",
|
"db:generate": "drizzle-kit generate",
|
||||||
"db:migrate": "drizzle-kit migrate",
|
"db:migrate": "drizzle-kit migrate",
|
||||||
"test": "vitest",
|
"test": "vitest",
|
||||||
@@ -19,8 +17,8 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@hono/swagger-ui": "^0.5.1",
|
"@hono/swagger-ui": "^0.5.1",
|
||||||
"@hono/zod-openapi": "^0.19.5",
|
"@hono/zod-openapi": "^1.1.6",
|
||||||
"@hono/zod-validator": "^0.2.2",
|
"@hono/zod-validator": "^0.7.6",
|
||||||
"drizzle-orm": "^0.44.7",
|
"drizzle-orm": "^0.44.7",
|
||||||
"gql.tada": "^1.8.10",
|
"gql.tada": "^1.8.10",
|
||||||
"graphql": "^16.12.0",
|
"graphql": "^16.12.0",
|
||||||
@@ -29,10 +27,11 @@
|
|||||||
"jose": "^5.10.0",
|
"jose": "^5.10.0",
|
||||||
"lodash.mapkeys": "^4.6.0",
|
"lodash.mapkeys": "^4.6.0",
|
||||||
"luxon": "^3.6.1",
|
"luxon": "^3.6.1",
|
||||||
"zod": "^3.24.3"
|
"zod": "^4.2.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cloudflare/vitest-pool-workers": "^0.10.15",
|
"@cloudflare/vitest-pool-workers": "^0.10.15",
|
||||||
|
"@graphql-typed-document-node/core": "^3.2.0",
|
||||||
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
|
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
|
||||||
"@types/lodash.mapkeys": "^4.6.9",
|
"@types/lodash.mapkeys": "^4.6.9",
|
||||||
"@types/luxon": "^3.6.2",
|
"@types/luxon": "^3.6.2",
|
||||||
|
|||||||
69
pnpm-lock.yaml
generated
69
pnpm-lock.yaml
generated
@@ -11,11 +11,11 @@ importers:
|
|||||||
specifier: ^0.5.1
|
specifier: ^0.5.1
|
||||||
version: 0.5.2(hono@4.10.8)
|
version: 0.5.2(hono@4.10.8)
|
||||||
"@hono/zod-openapi":
|
"@hono/zod-openapi":
|
||||||
specifier: ^0.19.5
|
specifier: ^1.1.6
|
||||||
version: 0.19.10(hono@4.10.8)(zod@3.25.76)
|
version: 1.1.6(hono@4.10.8)(zod@4.2.1)
|
||||||
"@hono/zod-validator":
|
"@hono/zod-validator":
|
||||||
specifier: ^0.2.2
|
specifier: ^0.7.6
|
||||||
version: 0.2.2(hono@4.10.8)(zod@3.25.76)
|
version: 0.7.6(hono@4.10.8)(zod@4.2.1)
|
||||||
drizzle-orm:
|
drizzle-orm:
|
||||||
specifier: ^0.44.7
|
specifier: ^0.44.7
|
||||||
version: 0.44.7
|
version: 0.44.7
|
||||||
@@ -41,12 +41,15 @@ importers:
|
|||||||
specifier: ^3.6.1
|
specifier: ^3.6.1
|
||||||
version: 3.7.2
|
version: 3.7.2
|
||||||
zod:
|
zod:
|
||||||
specifier: ^3.24.3
|
specifier: ^4.2.1
|
||||||
version: 3.25.76
|
version: 4.2.1
|
||||||
devDependencies:
|
devDependencies:
|
||||||
"@cloudflare/vitest-pool-workers":
|
"@cloudflare/vitest-pool-workers":
|
||||||
specifier: ^0.10.15
|
specifier: ^0.10.15
|
||||||
version: 0.10.15(@vitest/runner@3.2.4)(@vitest/snapshot@3.2.4)(vitest@3.2.4)
|
version: 0.10.15(@vitest/runner@3.2.4)(@vitest/snapshot@3.2.4)(vitest@3.2.4)
|
||||||
|
"@graphql-typed-document-node/core":
|
||||||
|
specifier: ^3.2.0
|
||||||
|
version: 3.2.0(graphql@16.12.0)
|
||||||
"@trivago/prettier-plugin-sort-imports":
|
"@trivago/prettier-plugin-sort-imports":
|
||||||
specifier: ^4.3.0
|
specifier: ^4.3.0
|
||||||
version: 4.3.0(prettier@3.7.4)
|
version: 4.3.0(prettier@3.7.4)
|
||||||
@@ -135,13 +138,13 @@ packages:
|
|||||||
graphql: ^15.5.0 || ^16.0.0 || ^17.0.0
|
graphql: ^15.5.0 || ^16.0.0 || ^17.0.0
|
||||||
typescript: ^5.0.0
|
typescript: ^5.0.0
|
||||||
|
|
||||||
"@asteasolutions/zod-to-openapi@7.3.4":
|
"@asteasolutions/zod-to-openapi@8.2.0":
|
||||||
resolution:
|
resolution:
|
||||||
{
|
{
|
||||||
integrity: sha512-/2rThQ5zPi9OzVwes6U7lK1+Yvug0iXu25olp7S0XsYmOqnyMfxH7gdSQjn/+DSOHRg7wnotwGJSyL+fBKdnEA==,
|
integrity: sha512-u05zNUirlukJAf9oEHmxSF31L1XQhz9XdpVILt7+xhrz65oQqBpiOWFkGvRWL0IpjOUJ878idKoNmYPxrFnkeg==,
|
||||||
}
|
}
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
zod: ^3.20.2
|
zod: ^4.0.0
|
||||||
|
|
||||||
"@babel/code-frame@7.27.1":
|
"@babel/code-frame@7.27.1":
|
||||||
resolution:
|
resolution:
|
||||||
@@ -1408,29 +1411,20 @@ packages:
|
|||||||
peerDependencies:
|
peerDependencies:
|
||||||
hono: "*"
|
hono: "*"
|
||||||
|
|
||||||
"@hono/zod-openapi@0.19.10":
|
"@hono/zod-openapi@1.1.6":
|
||||||
resolution:
|
resolution:
|
||||||
{
|
{
|
||||||
integrity: sha512-dpoS6DenvoJyvxtQ7Kd633FRZ/Qf74+4+o9s+zZI8pEqnbjdF/DtxIib08WDpCaWabMEJOL5TXpMgNEZvb7hpA==,
|
integrity: sha512-wEdG1MlCWAnngRVPKZJ/dv5P/b5UL3di/+SLX0Cuuc8hJ6Gf8L3vDMXcXywSYAwxK8iiatF7HoTxJ96gtckLpQ==,
|
||||||
}
|
}
|
||||||
engines: { node: ">=16.0.0" }
|
engines: { node: ">=16.0.0" }
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
hono: ">=4.3.6"
|
hono: ">=4.3.6"
|
||||||
zod: ">=3.0.0"
|
zod: ^4.0.0
|
||||||
|
|
||||||
"@hono/zod-validator@0.2.2":
|
"@hono/zod-validator@0.7.6":
|
||||||
resolution:
|
resolution:
|
||||||
{
|
{
|
||||||
integrity: sha512-dSDxaPV70Py8wuIU2QNpoVEIOSzSXZ/6/B/h4xA7eOMz7+AarKTSGV8E6QwrdcCbBLkpqfJ4Q2TmBO0eP1tCBQ==,
|
integrity: sha512-Io1B6d011Gj1KknV4rXYz4le5+5EubcWEU/speUjuw9XMMIaP3n78yXLhjd2A3PXaXaUwEAluOiAyLqhBEJgsw==,
|
||||||
}
|
|
||||||
peerDependencies:
|
|
||||||
hono: ">=3.9.0"
|
|
||||||
zod: ^3.19.1
|
|
||||||
|
|
||||||
"@hono/zod-validator@0.7.5":
|
|
||||||
resolution:
|
|
||||||
{
|
|
||||||
integrity: sha512-n4l4hutkfYU07PzRUHBOVzUEn38VSfrh+UVE5d0w4lyfWDOEhzxIupqo5iakRiJL44c3vTuFJBvcmUl8b9agIA==,
|
|
||||||
}
|
}
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
hono: ">=3.9.0"
|
hono: ">=3.9.0"
|
||||||
@@ -4134,6 +4128,12 @@ packages:
|
|||||||
integrity: sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==,
|
integrity: sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zod@4.2.1:
|
||||||
|
resolution:
|
||||||
|
{
|
||||||
|
integrity: sha512-0wZ1IRqGGhMP76gLqz8EyfBXKk0J2qo2+H3fi4mcUP/KtTocoX08nmIAHl1Z2kJIZbZee8KOpBCSNPRgauucjw==,
|
||||||
|
}
|
||||||
|
|
||||||
zx@8.1.5:
|
zx@8.1.5:
|
||||||
resolution:
|
resolution:
|
||||||
{
|
{
|
||||||
@@ -4153,10 +4153,10 @@ snapshots:
|
|||||||
graphql: 16.12.0
|
graphql: 16.12.0
|
||||||
typescript: 5.9.3
|
typescript: 5.9.3
|
||||||
|
|
||||||
"@asteasolutions/zod-to-openapi@7.3.4(zod@3.25.76)":
|
"@asteasolutions/zod-to-openapi@8.2.0(zod@4.2.1)":
|
||||||
dependencies:
|
dependencies:
|
||||||
openapi3-ts: 4.5.0
|
openapi3-ts: 4.5.0
|
||||||
zod: 3.25.76
|
zod: 4.2.1
|
||||||
|
|
||||||
"@babel/code-frame@7.27.1":
|
"@babel/code-frame@7.27.1":
|
||||||
dependencies:
|
dependencies:
|
||||||
@@ -4702,23 +4702,18 @@ snapshots:
|
|||||||
dependencies:
|
dependencies:
|
||||||
hono: 4.10.8
|
hono: 4.10.8
|
||||||
|
|
||||||
"@hono/zod-openapi@0.19.10(hono@4.10.8)(zod@3.25.76)":
|
"@hono/zod-openapi@1.1.6(hono@4.10.8)(zod@4.2.1)":
|
||||||
dependencies:
|
dependencies:
|
||||||
"@asteasolutions/zod-to-openapi": 7.3.4(zod@3.25.76)
|
"@asteasolutions/zod-to-openapi": 8.2.0(zod@4.2.1)
|
||||||
"@hono/zod-validator": 0.7.5(hono@4.10.8)(zod@3.25.76)
|
"@hono/zod-validator": 0.7.6(hono@4.10.8)(zod@4.2.1)
|
||||||
hono: 4.10.8
|
hono: 4.10.8
|
||||||
openapi3-ts: 4.5.0
|
openapi3-ts: 4.5.0
|
||||||
zod: 3.25.76
|
zod: 4.2.1
|
||||||
|
|
||||||
"@hono/zod-validator@0.2.2(hono@4.10.8)(zod@3.25.76)":
|
"@hono/zod-validator@0.7.6(hono@4.10.8)(zod@4.2.1)":
|
||||||
dependencies:
|
dependencies:
|
||||||
hono: 4.10.8
|
hono: 4.10.8
|
||||||
zod: 3.25.76
|
zod: 4.2.1
|
||||||
|
|
||||||
"@hono/zod-validator@0.7.5(hono@4.10.8)(zod@3.25.76)":
|
|
||||||
dependencies:
|
|
||||||
hono: 4.10.8
|
|
||||||
zod: 3.25.76
|
|
||||||
|
|
||||||
"@img/sharp-darwin-arm64@0.33.5":
|
"@img/sharp-darwin-arm64@0.33.5":
|
||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
@@ -6298,6 +6293,8 @@ snapshots:
|
|||||||
|
|
||||||
zod@3.25.76: {}
|
zod@3.25.76: {}
|
||||||
|
|
||||||
|
zod@4.2.1: {}
|
||||||
|
|
||||||
zx@8.1.5:
|
zx@8.1.5:
|
||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
"@types/fs-extra": 11.0.4
|
"@types/fs-extra": 11.0.4
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ const UserSchema = z.object({
|
|||||||
}),
|
}),
|
||||||
statistics: z.object({
|
statistics: z.object({
|
||||||
minutesWatched: z.number().openapi({ type: "integer", format: "int64" }),
|
minutesWatched: z.number().openapi({ type: "integer", format: "int64" }),
|
||||||
episodesWatched: z.number().int(),
|
episodesWatched: z.int(),
|
||||||
count: z.number().int(),
|
count: z.int(),
|
||||||
meanScore: z.number().openapi({ type: "number", format: "float" }),
|
meanScore: z.number().openapi({ type: "number", format: "float" }),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
@@ -129,15 +129,11 @@ app.openapi(route, async (c) => {
|
|||||||
let hasNextPage = true;
|
let hasNextPage = true;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
const stub = env.ANILIST_DO.getByName(user.name!);
|
const { mediaList, pageInfo } = await getWatchingTitles(
|
||||||
const { mediaList, pageInfo } = await stub
|
|
||||||
.getTitles(
|
|
||||||
user.name!,
|
user.name!,
|
||||||
currentPage++,
|
currentPage++,
|
||||||
["CURRENT", "PLANNING", "PAUSED", "REPEATING"],
|
|
||||||
aniListToken,
|
aniListToken,
|
||||||
)
|
).then((data) => data!);
|
||||||
.then((data) => data!);
|
|
||||||
if (!mediaList) {
|
if (!mediaList) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ app.openapi(route, async (c) => {
|
|||||||
isComplete,
|
isComplete,
|
||||||
);
|
);
|
||||||
if (isComplete) {
|
if (isComplete) {
|
||||||
await updateWatchStatus(c.req, deviceId, aniListId, "COMPLETED");
|
await updateWatchStatus(deviceId, aniListId, "COMPLETED");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ type AiringSchedule = {
|
|||||||
id: number;
|
id: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function getUpcomingTitlesFromAnilist(req: HonoRequest) {
|
export async function getUpcomingTitlesFromAnilist() {
|
||||||
const durableObjectId = env.ANILIST_DO.idFromName("GLOBAL");
|
const durableObjectId = env.ANILIST_DO.idFromName("GLOBAL");
|
||||||
const stub = env.ANILIST_DO.get(durableObjectId);
|
const stub = env.ANILIST_DO.get(durableObjectId);
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ import { getUpcomingTitlesFromAnilist } from "./anilist";
|
|||||||
|
|
||||||
const app = new Hono();
|
const app = new Hono();
|
||||||
|
|
||||||
app.post("/", async (c) => {
|
export async function checkUpcomingTitles() {
|
||||||
const titles = await getUpcomingTitlesFromAnilist(c.req);
|
const titles = await getUpcomingTitlesFromAnilist();
|
||||||
|
|
||||||
await Promise.allSettled(
|
await Promise.allSettled(
|
||||||
titles.map(async (title) => {
|
titles.map(async (title) => {
|
||||||
@@ -44,6 +44,10 @@ app.post("/", async (c) => {
|
|||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
app.post("/", async (c) => {
|
||||||
|
await checkUpcomingTitles();
|
||||||
|
|
||||||
return c.json(SuccessResponse, 200);
|
return c.json(SuccessResponse, 200);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ const route = createRoute({
|
|||||||
path: "/",
|
path: "/",
|
||||||
request: {
|
request: {
|
||||||
query: z.object({
|
query: z.object({
|
||||||
limit: z
|
limit: z.coerce
|
||||||
.number({ coerce: true })
|
.number()
|
||||||
.int()
|
.int()
|
||||||
.default(10)
|
.default(10)
|
||||||
.describe("The number of titles to return"),
|
.describe("The number of titles to return"),
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ export async function fetchPopularTitlesFromAnilist(
|
|||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case "upcoming":
|
case "upcoming":
|
||||||
data = await stub.nextSeasonPopular(next.season, next.year, limit);
|
data = await stub.nextSeasonPopular(next.season, next.year, page, limit);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unknown category: ${category}`);
|
throw new Error(`Unknown category: ${category}`);
|
||||||
|
|||||||
@@ -22,12 +22,12 @@ const route = createRoute({
|
|||||||
path: "/{category}",
|
path: "/{category}",
|
||||||
request: {
|
request: {
|
||||||
query: z.object({
|
query: z.object({
|
||||||
limit: z
|
limit: z.coerce
|
||||||
.number({ coerce: true })
|
.number()
|
||||||
.int()
|
.int()
|
||||||
.default(10)
|
.prefault(10)
|
||||||
.describe("The number of titles to return"),
|
.describe("The number of titles to return"),
|
||||||
page: z.number({ coerce: true }).int().min(1).default(1),
|
page: z.coerce.number().int().min(1).prefault(1),
|
||||||
}),
|
}),
|
||||||
params: z.object({ category: PopularCategory }),
|
params: z.object({ category: PopularCategory }),
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -17,8 +17,8 @@ const route = createRoute({
|
|||||||
request: {
|
request: {
|
||||||
query: z.object({
|
query: z.object({
|
||||||
query: z.string(),
|
query: z.string(),
|
||||||
page: z.number({ coerce: true }).int().min(1).default(1),
|
page: z.coerce.number().int().min(1).prefault(1),
|
||||||
limit: z.number({ coerce: true }).int().default(10),
|
limit: z.coerce.number().int().prefault(10),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
responses: {
|
responses: {
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ describe('requests the "/title" route', () => {
|
|||||||
headers: new Headers({ "x-anilist-token": "asd" }),
|
headers: new Headers({ "x-anilist-token": "asd" }),
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(await response.json()).toMatchSnapshot();
|
await expect(response.json()).resolves.toMatchSnapshot();
|
||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -63,7 +63,7 @@ describe('requests the "/title" route', () => {
|
|||||||
|
|
||||||
const response = await app.request("/title?id=10");
|
const response = await app.request("/title?id=10");
|
||||||
|
|
||||||
expect(await response.json()).toMatchSnapshot();
|
await expect(response.json()).resolves.toMatchSnapshot();
|
||||||
expect(response.status).toBe(200);
|
expect(response.status).toBe(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ describe('requests the "/title" route', () => {
|
|||||||
|
|
||||||
const response = await app.request("/title?id=-1");
|
const response = await app.request("/title?id=-1");
|
||||||
|
|
||||||
expect(await response.json()).toEqual({ success: false });
|
await expect(response.json()).resolves.toEqual({ success: false });
|
||||||
expect(response.status).toBe(404);
|
expect(response.status).toBe(404);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
|||||||
|
|
||||||
import { fetchTitleFromAnilist } from "~/libs/anilist/getTitle";
|
import { fetchTitleFromAnilist } from "~/libs/anilist/getTitle";
|
||||||
import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources";
|
import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources";
|
||||||
|
import { userProfileMiddleware } from "~/middleware/userProfile";
|
||||||
import {
|
import {
|
||||||
AniListIdQuerySchema,
|
AniListIdQuerySchema,
|
||||||
ErrorResponse,
|
ErrorResponse,
|
||||||
@@ -9,6 +10,7 @@ import {
|
|||||||
SuccessResponseSchema,
|
SuccessResponseSchema,
|
||||||
} from "~/types/schema";
|
} from "~/types/schema";
|
||||||
import { Title } from "~/types/title";
|
import { Title } from "~/types/title";
|
||||||
|
import type { User } from "~/types/user";
|
||||||
|
|
||||||
const app = new OpenAPIHono();
|
const app = new OpenAPIHono();
|
||||||
|
|
||||||
@@ -40,6 +42,7 @@ const route = createRoute({
|
|||||||
description: "Title could not be found",
|
description: "Title could not be found",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
middleware: [userProfileMiddleware],
|
||||||
});
|
});
|
||||||
|
|
||||||
app.openapi(route, async (c) => {
|
app.openapi(route, async (c) => {
|
||||||
@@ -55,7 +58,12 @@ app.openapi(route, async (c) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { result: title, errorOccurred } = await fetchFromMultipleSources([
|
const { result: title, errorOccurred } = await fetchFromMultipleSources([
|
||||||
() => fetchTitleFromAnilist(aniListId, aniListToken ?? undefined),
|
() =>
|
||||||
|
fetchTitleFromAnilist(
|
||||||
|
aniListId,
|
||||||
|
(c.get("user") as User)?.id,
|
||||||
|
aniListToken ?? undefined,
|
||||||
|
),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (errorOccurred) {
|
if (errorOccurred) {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
||||||
import type { HonoRequest } from "hono";
|
|
||||||
|
|
||||||
import { AnilistUpdateType } from "~/libs/anilist/updateType.ts";
|
import { AnilistUpdateType } from "~/libs/anilist/updateType.ts";
|
||||||
import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode";
|
import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode";
|
||||||
@@ -22,7 +21,6 @@ const UpdateWatchStatusRequest = z.object({
|
|||||||
deviceId: z.string(),
|
deviceId: z.string(),
|
||||||
watchStatus: WatchStatus.nullable(),
|
watchStatus: WatchStatus.nullable(),
|
||||||
titleId: AniListIdSchema,
|
titleId: AniListIdSchema,
|
||||||
isRetrying: z.boolean().optional().default(false),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const route = createRoute({
|
const route = createRoute({
|
||||||
@@ -64,7 +62,6 @@ const route = createRoute({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export async function updateWatchStatus(
|
export async function updateWatchStatus(
|
||||||
req: HonoRequest,
|
|
||||||
deviceId: string,
|
deviceId: string,
|
||||||
titleId: number,
|
titleId: number,
|
||||||
watchStatus: WatchStatus | null,
|
watchStatus: WatchStatus | null,
|
||||||
@@ -82,14 +79,8 @@ export async function updateWatchStatus(
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.openapi(route, async (c) => {
|
app.openapi(route, async (c) => {
|
||||||
const {
|
const { deviceId, watchStatus, titleId } =
|
||||||
deviceId,
|
await c.req.json<typeof UpdateWatchStatusRequest._type>();
|
||||||
watchStatus,
|
|
||||||
titleId,
|
|
||||||
isRetrying = false,
|
|
||||||
} = await c.req.json<typeof UpdateWatchStatusRequest._type>();
|
|
||||||
const aniListToken = c.req.header("X-AniList-Token");
|
|
||||||
|
|
||||||
// Check if we should use mock data
|
// Check if we should use mock data
|
||||||
const { useMockData } = await import("~/libs/useMockData");
|
const { useMockData } = await import("~/libs/useMockData");
|
||||||
if (useMockData()) {
|
if (useMockData()) {
|
||||||
@@ -97,26 +88,29 @@ app.openapi(route, async (c) => {
|
|||||||
return c.json(SuccessResponse, { status: 200 });
|
return c.json(SuccessResponse, { status: 200 });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isRetrying) {
|
|
||||||
try {
|
try {
|
||||||
await updateWatchStatus(c.req, deviceId, titleId, watchStatus);
|
await updateWatchStatus(deviceId, titleId, watchStatus);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error setting watch status");
|
console.error("Error setting watch status");
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return c.json(ErrorResponse, { status: 500 });
|
return c.json(ErrorResponse, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
const aniListToken = c.req.header("X-AniList-Token");
|
||||||
|
if (aniListToken) {
|
||||||
await queueTask(
|
await queueTask(
|
||||||
"ANILIST_UPDATES",
|
"ANILIST_UPDATES",
|
||||||
{
|
{
|
||||||
deviceId,
|
[AnilistUpdateType.UpdateWatchStatus]: {
|
||||||
watchStatus,
|
aniListToken,
|
||||||
titleId,
|
titleId,
|
||||||
|
watchStatus,
|
||||||
|
},
|
||||||
updateType: AnilistUpdateType.UpdateWatchStatus,
|
updateType: AnilistUpdateType.UpdateWatchStatus,
|
||||||
},
|
},
|
||||||
{ req: c.req, scheduleConfig: { delay: { minute: 1 } } },
|
{ req: c.req, scheduleConfig: { delay: { minute: 1 } } },
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return c.json(SuccessResponse, { status: 200 });
|
return c.json(SuccessResponse, { status: 200 });
|
||||||
});
|
});
|
||||||
|
|||||||
101
src/index.ts
101
src/index.ts
@@ -1,12 +1,16 @@
|
|||||||
import { swaggerUI } from "@hono/swagger-ui";
|
import { swaggerUI } from "@hono/swagger-ui";
|
||||||
import { OpenAPIHono } from "@hono/zod-openapi";
|
import { OpenAPIHono } from "@hono/zod-openapi";
|
||||||
|
import { Duration, type DurationLike } from "luxon";
|
||||||
|
|
||||||
import { maybeUpdateLastConnectedAt } from "~/controllers/maybeUpdateLastConnectedAt";
|
import { onNewEpisode } from "~/controllers/internal/new-episode";
|
||||||
|
import { AnilistUpdateType } from "~/libs/anilist/updateType";
|
||||||
|
import { calculateExponentialBackoff } from "~/libs/calculateExponentialBackoff";
|
||||||
import type { QueueName } from "~/libs/tasks/queueName.ts";
|
import type { QueueName } from "~/libs/tasks/queueName.ts";
|
||||||
|
import {
|
||||||
import { onNewEpisode } from "./controllers/internal/new-episode";
|
MAX_QUEUE_DELAY_SECONDS,
|
||||||
import { AnilistUpdateType } from "./libs/anilist/updateType";
|
type QueueBody,
|
||||||
import type { QueueBody } from "./libs/tasks/queueTask";
|
} from "~/libs/tasks/queueTask";
|
||||||
|
import { maybeUpdateLastConnectedAt } from "~/middleware/maybeUpdateLastConnectedAt";
|
||||||
|
|
||||||
export const app = new OpenAPIHono<{ Bindings: Env }>();
|
export const app = new OpenAPIHono<{ Bindings: Env }>();
|
||||||
|
|
||||||
@@ -73,50 +77,101 @@ app.get("/docs", swaggerUI({ url: "/openapi.json" }));
|
|||||||
export default {
|
export default {
|
||||||
fetch: app.fetch,
|
fetch: app.fetch,
|
||||||
async queue(batch) {
|
async queue(batch) {
|
||||||
switch (batch.queue as QueueName) {
|
onMessageQueue(batch, async (message, queueName) => {
|
||||||
|
switch (queueName) {
|
||||||
case "ANILIST_UPDATES":
|
case "ANILIST_UPDATES":
|
||||||
for (const message of (
|
const anilistUpdateBody =
|
||||||
batch as MessageBatch<QueueBody["ANILIST_UPDATES"]>
|
message.body as QueueBody["ANILIST_UPDATES"];
|
||||||
).messages) {
|
console.log("queue run", message.body);
|
||||||
switch (message.body.updateType) {
|
switch (anilistUpdateBody.updateType) {
|
||||||
case AnilistUpdateType.UpdateWatchStatus:
|
case AnilistUpdateType.UpdateWatchStatus:
|
||||||
if (!message.body[AnilistUpdateType.UpdateWatchStatus]) {
|
if (!anilistUpdateBody[AnilistUpdateType.UpdateWatchStatus]) {
|
||||||
throw new Error(
|
console.error(
|
||||||
`Discarding update, unknown body ${JSON.stringify(message.body)}`,
|
`Discarding update, unknown body ${JSON.stringify(message.body)}`,
|
||||||
);
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { updateWatchStatusOnAnilist } =
|
const { updateWatchStatusOnAnilist } =
|
||||||
await import("~/controllers/watch-status/anilist");
|
await import("~/controllers/watch-status/anilist");
|
||||||
const payload = message.body[AnilistUpdateType.UpdateWatchStatus];
|
const payload =
|
||||||
|
anilistUpdateBody[AnilistUpdateType.UpdateWatchStatus];
|
||||||
await updateWatchStatusOnAnilist(
|
await updateWatchStatusOnAnilist(
|
||||||
payload.titleId,
|
payload.titleId,
|
||||||
payload.watchStatus,
|
payload.watchStatus,
|
||||||
payload.aniListToken,
|
payload.aniListToken,
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
}
|
default:
|
||||||
|
throw new Error(
|
||||||
message.ack();
|
`Unhandled update type: ${anilistUpdateBody.updateType}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "NEW_EPISODE":
|
case "NEW_EPISODE":
|
||||||
for (const message of (batch as MessageBatch<QueueBody["NEW_EPISODE"]>)
|
const newEpisodeBody = message.body as QueueBody["NEW_EPISODE"];
|
||||||
.messages) {
|
|
||||||
await onNewEpisode(
|
await onNewEpisode(
|
||||||
message.body.aniListId,
|
newEpisodeBody.aniListId,
|
||||||
message.body.episodeNumber,
|
newEpisodeBody.episodeNumber,
|
||||||
);
|
);
|
||||||
message.ack();
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error(`Unhandled queue name: ${queueName}`);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
async scheduled(event, env, ctx) {
|
async scheduled(event, env, ctx) {
|
||||||
|
switch (event.cron) {
|
||||||
|
case "0 */12 * * *":
|
||||||
const { processDelayedTasks } =
|
const { processDelayedTasks } =
|
||||||
await import("~/libs/tasks/processDelayedTasks");
|
await import("~/libs/tasks/processDelayedTasks");
|
||||||
await processDelayedTasks(env, ctx);
|
await processDelayedTasks(env);
|
||||||
|
break;
|
||||||
|
case "0 18 * * *":
|
||||||
|
const { checkUpcomingTitles } =
|
||||||
|
await import("~/controllers/internal/upcoming-titles");
|
||||||
|
await checkUpcomingTitles();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error(`Unhandled cron: ${event.cron}`);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
} satisfies ExportedHandler<Env>;
|
} satisfies ExportedHandler<Env>;
|
||||||
|
|
||||||
|
const retryDelayConfig: Partial<
|
||||||
|
Record<QueueName, { min: DurationLike; max: DurationLike }>
|
||||||
|
> = {
|
||||||
|
NEW_EPISODE: {
|
||||||
|
min: Duration.fromObject({ hours: 1 }),
|
||||||
|
max: Duration.fromObject({ hours: 12 }),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
function onMessageQueue<QN extends QueueName>(
|
||||||
|
messageBatch: MessageBatch<unknown>,
|
||||||
|
callback: (message: Message<QueueBody[QN]>, queueName: QN) => void,
|
||||||
|
) {
|
||||||
|
for (const message of messageBatch.messages) {
|
||||||
|
try {
|
||||||
|
callback(message as Message<QueueBody[QN]>, messageBatch.queue as QN);
|
||||||
|
message.ack();
|
||||||
|
} catch (error) {
|
||||||
|
console.error(
|
||||||
|
`Failed to process message ${message.id} for queue ${messageBatch.queue} with body ${JSON.stringify(message.body)}`,
|
||||||
|
);
|
||||||
|
console.error(error);
|
||||||
|
message.retry({
|
||||||
|
delaySeconds: Math.min(
|
||||||
|
calculateExponentialBackoff({
|
||||||
|
attempt: message.attempts,
|
||||||
|
baseMin: retryDelayConfig[messageBatch.queue as QN]?.min,
|
||||||
|
absCap: retryDelayConfig[messageBatch.queue as QN]?.max,
|
||||||
|
}),
|
||||||
|
MAX_QUEUE_DELAY_SECONDS,
|
||||||
|
),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export { AnilistDurableObject as AnilistDo } from "~/libs/anilist/anilist-do.ts";
|
export { AnilistDurableObject as AnilistDo } from "~/libs/anilist/anilist-do.ts";
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
import type { TypedDocumentNode } from "@graphql-typed-document-node/core";
|
||||||
import { DurableObject } from "cloudflare:workers";
|
import { DurableObject } from "cloudflare:workers";
|
||||||
import { print } from "graphql";
|
import { print } from "graphql";
|
||||||
|
import { DateTime } from "luxon";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -7,6 +9,7 @@ import {
|
|||||||
GetNextEpisodeAiringAtQuery,
|
GetNextEpisodeAiringAtQuery,
|
||||||
GetPopularTitlesQuery,
|
GetPopularTitlesQuery,
|
||||||
GetTitleQuery,
|
GetTitleQuery,
|
||||||
|
GetTitleUserDataQuery,
|
||||||
GetTrendingTitlesQuery,
|
GetTrendingTitlesQuery,
|
||||||
GetUpcomingTitlesQuery,
|
GetUpcomingTitlesQuery,
|
||||||
GetUserProfileQuery,
|
GetUserProfileQuery,
|
||||||
@@ -17,12 +20,13 @@ import {
|
|||||||
SearchQuery,
|
SearchQuery,
|
||||||
} from "~/libs/anilist/queries";
|
} from "~/libs/anilist/queries";
|
||||||
import { sleep } from "~/libs/sleep.ts";
|
import { sleep } from "~/libs/sleep.ts";
|
||||||
|
import type { Title } from "~/types/title";
|
||||||
|
|
||||||
const nextAiringEpisodeSchema = z.nullable(
|
const nextAiringEpisodeSchema = z.nullable(
|
||||||
z.object({
|
z.object({
|
||||||
episode: z.number().int(),
|
episode: z.int(),
|
||||||
airingAt: z.number().int(),
|
airingAt: z.int(),
|
||||||
timeUntilAiring: z.number().int(),
|
timeUntilAiring: z.int(),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -37,30 +41,54 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
return new Response("Not found", { status: 404 });
|
return new Response("Not found", { status: 404 });
|
||||||
}
|
}
|
||||||
|
|
||||||
async getTitle(id: number, token?: string) {
|
async getTitle(
|
||||||
return this.handleCachedRequest(
|
id: number,
|
||||||
|
userId?: number,
|
||||||
|
token?: string,
|
||||||
|
): Promise<Title | null> {
|
||||||
|
const promises: Promise<any>[] = [
|
||||||
|
this.handleCachedRequest(
|
||||||
`title:${id}`,
|
`title:${id}`,
|
||||||
async () => {
|
async () => {
|
||||||
const anilistResponse = await this.fetchFromAnilist(
|
const anilistResponse = await this.fetchFromAnilist(GetTitleQuery, {
|
||||||
GetTitleQuery,
|
id,
|
||||||
{ id },
|
});
|
||||||
token,
|
|
||||||
);
|
|
||||||
return anilistResponse?.Media ?? null;
|
return anilistResponse?.Media ?? null;
|
||||||
},
|
},
|
||||||
(media) => {
|
(media) => {
|
||||||
if (!media) return undefined;
|
if (!media) return undefined;
|
||||||
|
|
||||||
// Cast to any to access fragment fields without unmasking
|
// Cast to any to access fragment fields without unmasking
|
||||||
const nextAiringEpisode = nextAiringEpisodeSchema.parse(
|
const nextAiringEpisode = nextAiringEpisodeSchema.parse(
|
||||||
(media as any)?.nextAiringEpisode,
|
(media as any)?.nextAiringEpisode,
|
||||||
);
|
);
|
||||||
const airingAt = (nextAiringEpisode?.airingAt ?? 0) * 1000;
|
return nextAiringEpisode?.airingAt
|
||||||
if (airingAt) {
|
? DateTime.fromMillis(nextAiringEpisode?.airingAt)
|
||||||
return airingAt - Date.now();
|
: undefined;
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
},
|
},
|
||||||
|
),
|
||||||
|
];
|
||||||
|
promises.push(
|
||||||
|
userId
|
||||||
|
? this.handleCachedRequest(
|
||||||
|
`title:${id}:${userId}`,
|
||||||
|
async () => {
|
||||||
|
const anilistResponse = await this.fetchFromAnilist(
|
||||||
|
GetTitleUserDataQuery,
|
||||||
|
{ id },
|
||||||
|
{ token },
|
||||||
);
|
);
|
||||||
|
return anilistResponse?.Media ?? null;
|
||||||
|
},
|
||||||
|
DateTime.now().plus({ days: 1 }),
|
||||||
|
)
|
||||||
|
: Promise.resolve({ mediaListEntry: null }),
|
||||||
|
);
|
||||||
|
|
||||||
|
return Promise.all(promises).then(([title, userTitle]) => ({
|
||||||
|
...title,
|
||||||
|
...userTitle,
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
async getNextEpisodeAiringAt(id: number) {
|
async getNextEpisodeAiringAt(id: number) {
|
||||||
@@ -72,7 +100,7 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
});
|
});
|
||||||
return data?.Media;
|
return data?.Media;
|
||||||
},
|
},
|
||||||
60 * 60 * 1000,
|
DateTime.now().plus({ hours: 1 }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,7 +115,7 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
});
|
});
|
||||||
return data?.Page;
|
return data?.Page;
|
||||||
},
|
},
|
||||||
60 * 60 * 1000,
|
DateTime.now().plus({ hours: 1 }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,8 +128,7 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
) {
|
) {
|
||||||
return this.handleCachedRequest(
|
return this.handleCachedRequest(
|
||||||
`popular:${JSON.stringify({ season, seasonYear, nextSeason, nextYear, limit })}`,
|
`popular:${JSON.stringify({ season, seasonYear, nextSeason, nextYear, limit })}`,
|
||||||
async () => {
|
() => {
|
||||||
console.log(nextSeason, nextYear, print(BrowsePopularQuery));
|
|
||||||
return this.fetchFromAnilist(BrowsePopularQuery, {
|
return this.fetchFromAnilist(BrowsePopularQuery, {
|
||||||
season,
|
season,
|
||||||
seasonYear,
|
seasonYear,
|
||||||
@@ -110,21 +137,27 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
limit,
|
limit,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
24 * 60 * 60 * 1000,
|
DateTime.now().plus({ days: 1 }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async nextSeasonPopular(nextSeason: any, nextYear: number, limit: number) {
|
async nextSeasonPopular(
|
||||||
|
nextSeason: any,
|
||||||
|
nextYear: number,
|
||||||
|
page: number,
|
||||||
|
limit: number,
|
||||||
|
) {
|
||||||
return this.handleCachedRequest(
|
return this.handleCachedRequest(
|
||||||
`next_season:${JSON.stringify({ nextSeason, nextYear, limit })}`,
|
`next_season:${JSON.stringify({ nextSeason, nextYear, page, limit })}`,
|
||||||
async () => {
|
async () => {
|
||||||
return this.fetchFromAnilist(NextSeasonPopularQuery, {
|
return this.fetchFromAnilist(NextSeasonPopularQuery, {
|
||||||
nextSeason,
|
nextSeason,
|
||||||
nextYear,
|
nextYear,
|
||||||
limit,
|
limit,
|
||||||
});
|
page,
|
||||||
|
}).then((data) => data?.Page);
|
||||||
},
|
},
|
||||||
24 * 60 * 60 * 1000,
|
DateTime.now().plus({ days: 1 }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,15 +170,14 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
return this.handleCachedRequest(
|
return this.handleCachedRequest(
|
||||||
`popular:${JSON.stringify({ page, limit, season, seasonYear })}`,
|
`popular:${JSON.stringify({ page, limit, season, seasonYear })}`,
|
||||||
async () => {
|
async () => {
|
||||||
const data = await this.fetchFromAnilist(GetPopularTitlesQuery, {
|
return this.fetchFromAnilist(GetPopularTitlesQuery, {
|
||||||
page,
|
page,
|
||||||
limit,
|
limit,
|
||||||
season,
|
season,
|
||||||
seasonYear,
|
seasonYear,
|
||||||
});
|
}).then((data) => data?.Page);
|
||||||
return data?.Page;
|
|
||||||
},
|
},
|
||||||
24 * 60 * 60 * 1000,
|
DateTime.now().plus({ days: 1 }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +191,7 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
});
|
});
|
||||||
return data?.Page;
|
return data?.Page;
|
||||||
},
|
},
|
||||||
24 * 60 * 60 * 1000,
|
DateTime.now().plus({ days: 1 }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,7 +210,7 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
});
|
});
|
||||||
return data?.Page;
|
return data?.Page;
|
||||||
},
|
},
|
||||||
24 * 60 * 60 * 1000,
|
DateTime.now().plus({ days: 1 }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,10 +218,10 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
return this.handleCachedRequest(
|
return this.handleCachedRequest(
|
||||||
`user:${token}`,
|
`user:${token}`,
|
||||||
async () => {
|
async () => {
|
||||||
const data = await this.fetchFromAnilist(GetUserQuery, {}, token);
|
const data = await this.fetchFromAnilist(GetUserQuery, {}, { token });
|
||||||
return data?.Viewer;
|
return data?.Viewer;
|
||||||
},
|
},
|
||||||
60 * 60 * 24 * 30 * 1000,
|
DateTime.now().plus({ days: 30 }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,11 +232,11 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
const data = await this.fetchFromAnilist(
|
const data = await this.fetchFromAnilist(
|
||||||
GetUserProfileQuery,
|
GetUserProfileQuery,
|
||||||
{ token },
|
{ token },
|
||||||
token,
|
{ token },
|
||||||
);
|
);
|
||||||
return data?.Viewer;
|
return data?.Viewer;
|
||||||
},
|
},
|
||||||
60 * 60 * 24 * 30 * 1000,
|
DateTime.now().plus({ days: 30 }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,7 +248,7 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
const data = await this.fetchFromAnilist(
|
const data = await this.fetchFromAnilist(
|
||||||
MarkEpisodeAsWatchedMutation,
|
MarkEpisodeAsWatchedMutation,
|
||||||
{ titleId, episodeNumber },
|
{ titleId, episodeNumber },
|
||||||
token,
|
{ token },
|
||||||
);
|
);
|
||||||
return data?.SaveMediaListEntry;
|
return data?.SaveMediaListEntry;
|
||||||
}
|
}
|
||||||
@@ -225,7 +257,7 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
const data = await this.fetchFromAnilist(
|
const data = await this.fetchFromAnilist(
|
||||||
MarkTitleAsWatchedMutation,
|
MarkTitleAsWatchedMutation,
|
||||||
{ titleId },
|
{ titleId },
|
||||||
token,
|
{ token },
|
||||||
);
|
);
|
||||||
return data?.SaveMediaListEntry;
|
return data?.SaveMediaListEntry;
|
||||||
}
|
}
|
||||||
@@ -234,7 +266,7 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
async handleCachedRequest<T>(
|
async handleCachedRequest<T>(
|
||||||
key: string,
|
key: string,
|
||||||
fetcher: () => Promise<T>,
|
fetcher: () => Promise<T>,
|
||||||
ttl?: number | ((data: T) => number | undefined),
|
ttl?: DateTime | ((data: T) => DateTime | undefined),
|
||||||
) {
|
) {
|
||||||
const cache = await this.state.storage.get(key);
|
const cache = await this.state.storage.get(key);
|
||||||
console.debug(`Retrieving request ${key} from cache:`, cache != null);
|
console.debug(`Retrieving request ${key} from cache:`, cache != null);
|
||||||
@@ -246,9 +278,8 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
await this.state.storage.put(key, result);
|
await this.state.storage.put(key, result);
|
||||||
|
|
||||||
const calculatedTtl = typeof ttl === "function" ? ttl(result) : ttl;
|
const calculatedTtl = typeof ttl === "function" ? ttl(result) : ttl;
|
||||||
|
if (calculatedTtl) {
|
||||||
if (calculatedTtl && calculatedTtl > 0) {
|
const alarmTime = calculatedTtl.toMillis();
|
||||||
const alarmTime = Date.now() + calculatedTtl;
|
|
||||||
await this.state.storage.setAlarm(alarmTime);
|
await this.state.storage.setAlarm(alarmTime);
|
||||||
await this.state.storage.put(`alarm:${key}`, alarmTime);
|
await this.state.storage.put(`alarm:${key}`, alarmTime);
|
||||||
}
|
}
|
||||||
@@ -259,11 +290,13 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
async alarm() {
|
async alarm() {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const alarms = await this.state.storage.list({ prefix: "alarm:" });
|
const alarms = await this.state.storage.list({ prefix: "alarm:" });
|
||||||
|
console.debug(`Retrieved alarms from cache:`, Object.entries(alarms));
|
||||||
for (const [key, ttl] of Object.entries(alarms)) {
|
for (const [key, ttl] of Object.entries(alarms)) {
|
||||||
if (now >= ttl) {
|
if (now >= ttl) {
|
||||||
// The key in alarms is `alarm:${storageKey}`
|
// The key in alarms is `alarm:${storageKey}`
|
||||||
// We want to delete the storageKey
|
// We want to delete the storageKey
|
||||||
const storageKey = key.replace("alarm:", "");
|
const storageKey = key.replace("alarm:", "");
|
||||||
|
console.debug(`Deleting storage key ${storageKey} & alarm ${key}`);
|
||||||
await this.state.storage.delete(storageKey);
|
await this.state.storage.delete(storageKey);
|
||||||
await this.state.storage.delete(key);
|
await this.state.storage.delete(key);
|
||||||
}
|
}
|
||||||
@@ -271,10 +304,13 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fetchFromAnilist<Result = any, Variables = any>(
|
async fetchFromAnilist<Result = any, Variables = any>(
|
||||||
queryString: string,
|
query: TypedDocumentNode<Result, Variables>,
|
||||||
variables: Variables,
|
variables: Variables,
|
||||||
token?: string | undefined,
|
{
|
||||||
): Promise<Result> {
|
token,
|
||||||
|
shouldRetryOnRateLimit = true,
|
||||||
|
}: { token?: string | undefined; shouldRetryOnRateLimit?: boolean } = {},
|
||||||
|
): Promise<Result | undefined> {
|
||||||
const headers: any = {
|
const headers: any = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
};
|
};
|
||||||
@@ -285,7 +321,7 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
|
|
||||||
// Use the query passed in, or fallback if needed (though we expect it to be passed)
|
// Use the query passed in, or fallback if needed (though we expect it to be passed)
|
||||||
// We print the query to string
|
// We print the query to string
|
||||||
// const queryString = print(query);
|
const queryString = print(query);
|
||||||
|
|
||||||
const response = await fetch(`${this.env.PROXY_URL}/proxy`, {
|
const response = await fetch(`${this.env.PROXY_URL}/proxy`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -304,14 +340,17 @@ export class AnilistDurableObject extends DurableObject {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 1. Handle Rate Limiting (429)
|
// 1. Handle Rate Limiting (429)
|
||||||
if (response.status === 429) {
|
if (shouldRetryOnRateLimit && response.status === 429) {
|
||||||
const retryAfter = await response
|
const retryAfter = await response
|
||||||
.json()
|
.json<{ headers: Record<string, string> }>()
|
||||||
.then(({ headers }) => new Headers(headers).get("Retry-After"));
|
.then(({ headers }) => new Headers(headers).get("Retry-After"));
|
||||||
console.log("429, retrying in", retryAfter);
|
console.log("429, retrying in", retryAfter);
|
||||||
|
|
||||||
await sleep(Number(retryAfter || 1) * 1000); // specific fallback or ensure logic
|
await sleep(Number(retryAfter || 1) * 1000); // specific fallback or ensure logic
|
||||||
return this.fetchFromAnilist(query, variables, token);
|
return this.fetchFromAnilist(query, variables, {
|
||||||
|
token,
|
||||||
|
shouldRetryOnRateLimit: false,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Handle HTTP Errors (like 404 or 500)
|
// 2. Handle HTTP Errors (like 404 or 500)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import type { Title } from "~/types/title";
|
|||||||
|
|
||||||
export async function fetchTitleFromAnilist(
|
export async function fetchTitleFromAnilist(
|
||||||
id: number,
|
id: number,
|
||||||
|
userId?: number | undefined,
|
||||||
token?: string | undefined,
|
token?: string | undefined,
|
||||||
): Promise<Title | undefined> {
|
): Promise<Title | undefined> {
|
||||||
if (useMockData()) {
|
if (useMockData()) {
|
||||||
@@ -17,8 +18,7 @@ export async function fetchTitleFromAnilist(
|
|||||||
);
|
);
|
||||||
const stub = env.ANILIST_DO.get(durableObjectId);
|
const stub = env.ANILIST_DO.get(durableObjectId);
|
||||||
|
|
||||||
const data = await stub.getTitle(id, token);
|
const data = await stub.getTitle(id, userId, token);
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,18 @@ export const GetTitleQuery = graphql(
|
|||||||
[MediaFragment],
|
[MediaFragment],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export const GetTitleUserDataQuery = graphql(`
|
||||||
|
query GetTitleUserData($id: Int!) {
|
||||||
|
Media(id: $id) {
|
||||||
|
mediaListEntry {
|
||||||
|
id
|
||||||
|
progress
|
||||||
|
status
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
|
||||||
export const SearchQuery = graphql(
|
export const SearchQuery = graphql(
|
||||||
`
|
`
|
||||||
query Search($query: String!, $page: Int!, $limit: Int!) {
|
query Search($query: String!, $page: Int!, $limit: Int!) {
|
||||||
@@ -247,8 +259,9 @@ export const NextSeasonPopularQuery = graphql(
|
|||||||
$nextSeason: MediaSeason
|
$nextSeason: MediaSeason
|
||||||
$nextYear: Int
|
$nextYear: Int
|
||||||
$limit: Int!
|
$limit: Int!
|
||||||
|
$page: Int!
|
||||||
) {
|
) {
|
||||||
Page(page: 1, perPage: $limit) {
|
Page(page: $page, perPage: $limit) {
|
||||||
media(
|
media(
|
||||||
season: $nextSeason
|
season: $nextSeason
|
||||||
seasonYear: $nextYear
|
seasonYear: $nextYear
|
||||||
|
|||||||
53
src/libs/calculateExponentialBackoff.ts
Normal file
53
src/libs/calculateExponentialBackoff.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import { Duration, type DurationLike } from "luxon";
|
||||||
|
|
||||||
|
interface CalculateExponentialBackoffOptions {
|
||||||
|
attempt: number;
|
||||||
|
baseMin?: DurationLike;
|
||||||
|
absCap?: DurationLike;
|
||||||
|
fuzzFactor?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a backoff time where both the Minimum floor and Maximum ceiling
|
||||||
|
* are "fuzzed" with jitter to prevent clustering at the edges.
|
||||||
|
*
|
||||||
|
* @param attempt - The current retry attempt (0-indexed).
|
||||||
|
* @param baseMin - The nominal minimum wait time (default: 1s).
|
||||||
|
* @param absCap - The absolute maximum wait time (default: 60s).
|
||||||
|
* @param fuzzFactor - How much to wobble the edges (0.1 = +/- 10%).
|
||||||
|
*
|
||||||
|
* @returns A random duration between the nominal minimum and maximum, in seconds.
|
||||||
|
*/
|
||||||
|
export function calculateExponentialBackoff({
|
||||||
|
attempt,
|
||||||
|
baseMin: baseMinDuration = Duration.fromObject({ minutes: 1 }),
|
||||||
|
absCap: absCapDuration = Duration.fromObject({ hours: 1 }),
|
||||||
|
fuzzFactor = 0.2,
|
||||||
|
}: CalculateExponentialBackoffOptions): number {
|
||||||
|
const baseMin = Duration.fromDurationLike(baseMinDuration).as("seconds");
|
||||||
|
const absCap = Duration.fromDurationLike(absCapDuration).as("seconds");
|
||||||
|
|
||||||
|
// 1. Calculate nominal boundaries
|
||||||
|
// Example: If baseMin is 1s, the nominal boundaries are 1s, 2s, 4s, 8s... (The 'ceiling' grows exponentially)
|
||||||
|
const nominalMin = baseMin;
|
||||||
|
const nominalCeiling = Math.min(baseMin * Math.pow(2, attempt), absCap);
|
||||||
|
|
||||||
|
// 2. Fuzz the Min (The Floor)
|
||||||
|
// Example: If min is 1s and fuzz is 0.2, the floor becomes random between 0.8s and 1.2s
|
||||||
|
const minFuzz = nominalMin * fuzzFactor;
|
||||||
|
const fuzzedMin = nominalMin + (Math.random() * 2 * minFuzz - minFuzz);
|
||||||
|
|
||||||
|
// 3. Fuzz the Max (The Ceiling)
|
||||||
|
// Example: If ceiling is 4s (and fuzz is 0.2), it becomes random between 3.2s and 4.8s
|
||||||
|
const maxFuzz = nominalCeiling * fuzzFactor;
|
||||||
|
const fuzzedCeiling =
|
||||||
|
nominalCeiling + (Math.random() * 2 * maxFuzz - maxFuzz);
|
||||||
|
|
||||||
|
// Safety: Ensure we don't return a negative number or cross boundaries weirdly
|
||||||
|
// (e.g. if fuzz makes min > max, we swap or clamp)
|
||||||
|
const safeMin = Math.max(0, fuzzedMin);
|
||||||
|
const safeMax = Math.max(safeMin, fuzzedCeiling);
|
||||||
|
|
||||||
|
// 4. Return random value in the new fuzzy range
|
||||||
|
return safeMin + Math.random() * (safeMax - safeMin);
|
||||||
|
}
|
||||||
@@ -3,11 +3,13 @@ import mapKeys from "lodash.mapkeys";
|
|||||||
|
|
||||||
import { Case, changeStringCase } from "../changeStringCase";
|
import { Case, changeStringCase } from "../changeStringCase";
|
||||||
|
|
||||||
export function getAdminSdkCredentials(env: Cloudflare.Env = cloudflareEnv) {
|
export function getAdminSdkCredentials(
|
||||||
|
env: Cloudflare.Env = cloudflareEnv,
|
||||||
|
): AdminSdkCredentials {
|
||||||
return mapKeys(
|
return mapKeys(
|
||||||
JSON.parse(env.ADMIN_SDK_JSON) as AdminSdkCredentials,
|
JSON.parse(env.ADMIN_SDK_JSON) as AdminSdkCredentials,
|
||||||
(_, key) => changeStringCase(key, Case.snake_case, Case.camelCase),
|
(_, key) => changeStringCase(key, Case.snake_case, Case.camelCase),
|
||||||
);
|
) satisfies AdminSdkCredentials;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AdminSdkCredentials {
|
export interface AdminSdkCredentials {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { DateTime } from "luxon";
|
|||||||
|
|
||||||
import type { DelayedTaskMetadata } from "./delayedTask";
|
import type { DelayedTaskMetadata } from "./delayedTask";
|
||||||
import { deserializeDelayedTask } from "./delayedTask";
|
import { deserializeDelayedTask } from "./delayedTask";
|
||||||
import { MAX_DELAY_SECONDS, queueTask } from "./queueTask";
|
import { MAX_QUEUE_DELAY_SECONDS, queueTask } from "./queueTask";
|
||||||
|
|
||||||
const RETRY_ALERT_THRESHOLD = 3;
|
const RETRY_ALERT_THRESHOLD = 3;
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ export async function processDelayedTasks(env: Cloudflare.Env): Promise<void> {
|
|||||||
console.log(`Found ${keys.length} delayed tasks to check`);
|
console.log(`Found ${keys.length} delayed tasks to check`);
|
||||||
|
|
||||||
const currentTime = Math.floor(Date.now() / 1000);
|
const currentTime = Math.floor(Date.now() / 1000);
|
||||||
const maxQueueTime = currentTime + MAX_DELAY_SECONDS;
|
const maxQueueTime = currentTime + MAX_QUEUE_DELAY_SECONDS;
|
||||||
|
|
||||||
let processedCount = 0;
|
let processedCount = 0;
|
||||||
let queuedCount = 0;
|
let queuedCount = 0;
|
||||||
|
|||||||
@@ -81,8 +81,8 @@ describe("queueTask - delayed task handling", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("tasks with delay > 9 hours", () => {
|
describe("tasks with delay > 12 hours", () => {
|
||||||
it("stores task in KV when delay exceeds 9 hours", async () => {
|
it("stores task in KV when delay exceeds 12 hours", async () => {
|
||||||
await queueTask(
|
await queueTask(
|
||||||
"NEW_EPISODE",
|
"NEW_EPISODE",
|
||||||
{ aniListId: 111, episodeNumber: 4 },
|
{ aniListId: 111, episodeNumber: 4 },
|
||||||
@@ -98,12 +98,12 @@ describe("queueTask - delayed task handling", () => {
|
|||||||
expect(queueSendSpy).not.toHaveBeenCalled();
|
expect(queueSendSpy).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("stores task in KV when delay is 9 hours + 1 second", async () => {
|
it("stores task in KV when delay is 12 hours + 1 second", async () => {
|
||||||
await queueTask(
|
await queueTask(
|
||||||
"NEW_EPISODE",
|
"NEW_EPISODE",
|
||||||
{ aniListId: 222, episodeNumber: 5 },
|
{ aniListId: 222, episodeNumber: 5 },
|
||||||
{
|
{
|
||||||
scheduleConfig: { delay: { hours: 9, seconds: 1 } },
|
scheduleConfig: { delay: { hours: 12, seconds: 1 } },
|
||||||
env: mockEnv,
|
env: mockEnv,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -176,7 +176,7 @@ describe("queueTask - delayed task handling", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("epoch time scheduling", () => {
|
describe("epoch time scheduling", () => {
|
||||||
it("queues directly when epoch time is within 9 hours", async () => {
|
it("queues directly when epoch time is within 12 hours", async () => {
|
||||||
const futureTime = Math.floor(Date.now() / 1000) + 3600; // 1 hour from now
|
const futureTime = Math.floor(Date.now() / 1000) + 3600; // 1 hour from now
|
||||||
|
|
||||||
await queueTask(
|
await queueTask(
|
||||||
@@ -192,7 +192,7 @@ describe("queueTask - delayed task handling", () => {
|
|||||||
expect(kvPutSpy).not.toHaveBeenCalled();
|
expect(kvPutSpy).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("stores in KV when epoch time is beyond 9 hours", async () => {
|
it("stores in KV when epoch time is beyond 12 hours", async () => {
|
||||||
const futureTime = Math.floor(Date.now() / 1000) + 24 * 3600; // 24 hours from now
|
const futureTime = Math.floor(Date.now() / 1000) + 24 * 3600; // 24 hours from now
|
||||||
|
|
||||||
await queueTask(
|
await queueTask(
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ interface QueueTaskOptionalArgs {
|
|||||||
env?: Cloudflare.Env;
|
env?: Cloudflare.Env;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const MAX_DELAY_SECONDS = Duration.fromObject({ hours: 9 }).as(
|
export const MAX_QUEUE_DELAY_SECONDS = Duration.fromObject({ hours: 12 }).as(
|
||||||
"seconds",
|
"seconds",
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -46,8 +46,8 @@ export async function queueTask(
|
|||||||
req?.header(),
|
req?.header(),
|
||||||
);
|
);
|
||||||
|
|
||||||
// If delay exceeds 9 hours, store in KV for later processing
|
// If delay exceeds 12 hours, store in KV for later processing
|
||||||
if (scheduleTime > MAX_DELAY_SECONDS) {
|
if (scheduleTime > MAX_QUEUE_DELAY_SECONDS) {
|
||||||
if (!env || !env.DELAYED_TASKS) {
|
if (!env || !env.DELAYED_TASKS) {
|
||||||
throw new Error("DELAYED_TASKS KV namespace not available");
|
throw new Error("DELAYED_TASKS KV namespace not available");
|
||||||
}
|
}
|
||||||
@@ -79,7 +79,18 @@ export async function queueTask(
|
|||||||
// Otherwise, queue directly
|
// Otherwise, queue directly
|
||||||
const contentType =
|
const contentType =
|
||||||
headers["Content-Type"] === "application/json" ? "json" : "text";
|
headers["Content-Type"] === "application/json" ? "json" : "text";
|
||||||
if (!env) {
|
if (env) {
|
||||||
|
console.debug(
|
||||||
|
`Queueing task in queue ${queueName}: ${JSON.stringify(body)}`,
|
||||||
|
);
|
||||||
|
await env[queueName].send(
|
||||||
|
{ body, headers },
|
||||||
|
{
|
||||||
|
contentType,
|
||||||
|
delaySeconds: scheduleTime,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} else {
|
||||||
const Cloudflare = await import("cloudflare").then(
|
const Cloudflare = await import("cloudflare").then(
|
||||||
({ Cloudflare }) => Cloudflare,
|
({ Cloudflare }) => Cloudflare,
|
||||||
);
|
);
|
||||||
@@ -103,14 +114,6 @@ export async function queueTask(
|
|||||||
delay_seconds: scheduleTime,
|
delay_seconds: scheduleTime,
|
||||||
account_id: env.CLOUDFLARE_ACCOUNT_ID,
|
account_id: env.CLOUDFLARE_ACCOUNT_ID,
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
await env[queueName].send(
|
|
||||||
{ body, headers },
|
|
||||||
{
|
|
||||||
contentType,
|
|
||||||
delaySeconds: scheduleTime,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function buildTask(
|
function buildTask(
|
||||||
@@ -122,6 +125,7 @@ function buildTask(
|
|||||||
let scheduleTime: number = 0;
|
let scheduleTime: number = 0;
|
||||||
if (scheduleConfig) {
|
if (scheduleConfig) {
|
||||||
const { delay, epochTime } = scheduleConfig;
|
const { delay, epochTime } = scheduleConfig;
|
||||||
|
console.log("scheduleConfig", scheduleConfig);
|
||||||
if (epochTime) {
|
if (epochTime) {
|
||||||
console.log("epochTime", epochTime);
|
console.log("epochTime", epochTime);
|
||||||
scheduleTime = DateTime.fromSeconds(epochTime)
|
scheduleTime = DateTime.fromSeconds(epochTime)
|
||||||
@@ -132,6 +136,9 @@ function buildTask(
|
|||||||
scheduleTime = Duration.fromDurationLike(delay).as("second");
|
scheduleTime = Duration.fromDurationLike(delay).as("second");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const authorizationHeader = headers?.["X-Anilist-Token"]
|
||||||
|
? { Authorization: `Bearer ${headers["X-Anilist-Token"]}` }
|
||||||
|
: {};
|
||||||
|
|
||||||
switch (queueName) {
|
switch (queueName) {
|
||||||
case "ANILIST_UPDATES":
|
case "ANILIST_UPDATES":
|
||||||
@@ -140,8 +147,8 @@ function buildTask(
|
|||||||
body,
|
body,
|
||||||
scheduleTime,
|
scheduleTime,
|
||||||
headers: {
|
headers: {
|
||||||
|
...authorizationHeader,
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"X-Anilist-Token": headers?.["X-Anilist-Token"],
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
default:
|
default:
|
||||||
|
|||||||
25
src/middleware/userProfile.ts
Normal file
25
src/middleware/userProfile.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { createMiddleware } from "hono/factory";
|
||||||
|
|
||||||
|
import type { User } from "~/types/user";
|
||||||
|
|
||||||
|
export const userProfileMiddleware = createMiddleware<
|
||||||
|
Cloudflare.Env & {
|
||||||
|
Variables: {
|
||||||
|
user: User;
|
||||||
|
};
|
||||||
|
Bindings: Env;
|
||||||
|
}
|
||||||
|
>(async (c, next) => {
|
||||||
|
const aniListToken = await c.req.header("X-AniList-Token");
|
||||||
|
if (!aniListToken) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await c.env.ANILIST_DO.getByName("GLOBAL").getUser(aniListToken);
|
||||||
|
if (!user) {
|
||||||
|
return c.json({ error: "User not found" }, 401);
|
||||||
|
}
|
||||||
|
|
||||||
|
c.set("user", user);
|
||||||
|
return next();
|
||||||
|
});
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
import { Project } from "ts-morph";
|
|
||||||
import { $ } from "zx";
|
|
||||||
|
|
||||||
import { logStep } from "~/libs/logStep";
|
|
||||||
|
|
||||||
await logStep(
|
|
||||||
'Re-generating "env.d.ts"',
|
|
||||||
() => $`wrangler types src/types/env.d.ts`.quiet(),
|
|
||||||
"Generated env.d.ts",
|
|
||||||
);
|
|
||||||
|
|
||||||
const secretNames = await logStep(
|
|
||||||
"Fetching secrets from Cloudflare",
|
|
||||||
async (): Promise<string[]> => {
|
|
||||||
const { stdout } = await $`wrangler secret list`.quiet();
|
|
||||||
return JSON.parse(stdout.toString()).map(
|
|
||||||
(secret: { name: string; type: "secret_text" }) => secret.name,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
"Fetched secrets",
|
|
||||||
);
|
|
||||||
|
|
||||||
const project = new Project({});
|
|
||||||
|
|
||||||
const envSourceFile = project.addSourceFileAtPath("src/types/env.d.ts");
|
|
||||||
envSourceFile.insertImportDeclaration(2, {
|
|
||||||
isTypeOnly: true,
|
|
||||||
moduleSpecifier: "hono",
|
|
||||||
namedImports: ["Env as HonoEnv"],
|
|
||||||
});
|
|
||||||
envSourceFile
|
|
||||||
.getInterfaceOrThrow("Env")
|
|
||||||
.addExtends(["HonoEnv", "Record<string, unknown>"]);
|
|
||||||
envSourceFile.getInterfaceOrThrow("Env").addProperties(
|
|
||||||
secretNames.map((name) => ({
|
|
||||||
name,
|
|
||||||
type: `string`,
|
|
||||||
})),
|
|
||||||
);
|
|
||||||
|
|
||||||
await project.save();
|
|
||||||
|
|
||||||
await logStep(
|
|
||||||
"Formatting env.d.ts",
|
|
||||||
() => $`prettier --write src/types/env.d.ts`.quiet(),
|
|
||||||
"Formatted env.d.ts",
|
|
||||||
);
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
import { readFile } from "fs/promises";
|
|
||||||
import { $, sleep } from "zx";
|
|
||||||
|
|
||||||
import { logStep } from "~/libs/logStep";
|
|
||||||
|
|
||||||
await $`cp src/types/env.d.ts /tmp/env.d.ts`.quiet();
|
|
||||||
|
|
||||||
await logStep(
|
|
||||||
'Generating "env.d.ts"',
|
|
||||||
// @ts-ignore
|
|
||||||
() => import("./generateEnv"),
|
|
||||||
"Generated env.d.ts",
|
|
||||||
);
|
|
||||||
|
|
||||||
await logStep("Comparing env.d.ts", async () => {
|
|
||||||
function filterComments(content: Buffer) {
|
|
||||||
return content
|
|
||||||
.toString()
|
|
||||||
.split("\n")
|
|
||||||
.filter((line) => !line.trim().startsWith("//"))
|
|
||||||
.join("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
const currentFileContent = filterComments(await readFile("/tmp/env.d.ts"));
|
|
||||||
const generatedFileContent = filterComments(
|
|
||||||
await readFile("src/types/env.d.ts"),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (currentFileContent === generatedFileContent) {
|
|
||||||
console.log("env.d.ts is up to date");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const isCI = process.env["IS_CI"] === "true";
|
|
||||||
const vcsCommand = isCI ? "git" : "sl";
|
|
||||||
await $`${vcsCommand} diff src/types/env.d.ts`.stdio("inherit");
|
|
||||||
// add 1 second to make sure spawn completes
|
|
||||||
await sleep(1000);
|
|
||||||
throw new Error("env.d.ts is out of date");
|
|
||||||
});
|
|
||||||
@@ -9,7 +9,7 @@ export const FetchUrlResponseSchema = z.object({
|
|||||||
audio: z.array(z.object({ url: z.string(), lang: z.string() })),
|
audio: z.array(z.object({ url: z.string(), lang: z.string() })),
|
||||||
intro: SkippableSchema,
|
intro: SkippableSchema,
|
||||||
outro: SkippableSchema,
|
outro: SkippableSchema,
|
||||||
headers: z.record(z.string()).optional(),
|
headers: z.record(z.string(), z.string()).optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type FetchUrlResponse = z.infer<typeof FetchUrlResponse> & {
|
export type FetchUrlResponse = z.infer<typeof FetchUrlResponse> & {
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ export const Episode = z.object({
|
|||||||
title: z.string().nullish(),
|
title: z.string().nullish(),
|
||||||
img: z.string().nullish(),
|
img: z.string().nullish(),
|
||||||
description: z.string().nullish(),
|
description: z.string().nullish(),
|
||||||
rating: z.number().int().nullish(),
|
rating: z.int().nullish(),
|
||||||
updatedAt: z.number().int().default(0).openapi({ format: "int64" }),
|
updatedAt: z.int().prefault(0).openapi({ format: "int64" }),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type EpisodesResponse = z.infer<typeof EpisodesResponse>;
|
export type EpisodesResponse = z.infer<typeof EpisodesResponse>;
|
||||||
|
|||||||
@@ -24,9 +24,9 @@ export const ErrorResponseSchema = z.object({
|
|||||||
success: z.literal(false).openapi({ type: "boolean" }),
|
success: z.literal(false).openapi({ type: "boolean" }),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const NullableNumberSchema = z.number().int().nullable();
|
export const NullableNumberSchema = z.int().nullable();
|
||||||
|
|
||||||
export const AniListIdSchema = z.number().int().openapi({ format: "int64" });
|
export const AniListIdSchema = z.int().openapi({ format: "int64" });
|
||||||
export const AniListIdQuerySchema = z
|
export const AniListIdQuerySchema = z
|
||||||
.string()
|
.string()
|
||||||
.openapi({ type: "integer", format: "int64" });
|
.openapi({ type: "integer", format: "int64" });
|
||||||
|
|||||||
@@ -8,17 +8,17 @@ export type Title = z.infer<typeof Title>;
|
|||||||
export const Title = z.object({
|
export const Title = z.object({
|
||||||
nextAiringEpisode: z.nullable(
|
nextAiringEpisode: z.nullable(
|
||||||
z.object({
|
z.object({
|
||||||
episode: z.number().int(),
|
episode: z.int(),
|
||||||
airingAt: z.number().int().openapi({ format: "int64" }),
|
airingAt: z.int().openapi({ format: "int64" }),
|
||||||
timeUntilAiring: z.number().int().openapi({ format: "int64" }),
|
timeUntilAiring: z.int().openapi({ format: "int64" }),
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
mediaListEntry: z.nullable(
|
mediaListEntry: z.nullable(
|
||||||
z.object({
|
z.object({
|
||||||
status: z.nullable(WatchStatus),
|
status: z.nullable(WatchStatus),
|
||||||
progress: NullableNumberSchema,
|
progress: NullableNumberSchema,
|
||||||
id: z.number().int(),
|
id: z.int(),
|
||||||
updatedAt: z.number().int().openapi({ format: "int64" }).optional(),
|
updatedAt: z.int().openapi({ format: "int64" }).optional(),
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
countryOfOrigin: countryCodeSchema,
|
countryOfOrigin: countryCodeSchema,
|
||||||
@@ -50,5 +50,5 @@ export const Title = z.object({
|
|||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
idMal: NullableNumberSchema,
|
idMal: NullableNumberSchema,
|
||||||
id: z.number().int().openapi({ format: "int64" }),
|
id: z.int().openapi({ format: "int64" }),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -21,11 +21,6 @@ export const MediaFragment = graphql(`
|
|||||||
medium
|
medium
|
||||||
}
|
}
|
||||||
countryOfOrigin
|
countryOfOrigin
|
||||||
mediaListEntry {
|
|
||||||
id
|
|
||||||
progress
|
|
||||||
status
|
|
||||||
}
|
|
||||||
nextAiringEpisode {
|
nextAiringEpisode {
|
||||||
timeUntilAiring
|
timeUntilAiring
|
||||||
airingAt
|
airingAt
|
||||||
|
|||||||
@@ -3,12 +3,18 @@ import { z } from "zod";
|
|||||||
export type User = z.infer<typeof User>;
|
export type User = z.infer<typeof User>;
|
||||||
export const User = z
|
export const User = z
|
||||||
.object({
|
.object({
|
||||||
|
id: z.number().openapi({ type: "integer", format: "int64" }),
|
||||||
|
name: z.string(),
|
||||||
|
})
|
||||||
|
.optional()
|
||||||
|
.nullable();
|
||||||
|
|
||||||
|
export type UserProfile = z.infer<typeof UserProfile>;
|
||||||
|
export const UserProfile = z.object({
|
||||||
statistics: z.object({
|
statistics: z.object({
|
||||||
minutesWatched: z.number().openapi({ type: "integer", format: "int64" }),
|
minutesWatched: z.number().openapi({ type: "integer", format: "int64" }),
|
||||||
episodesWatched: z.number().openapi({ type: "integer", format: "int64" }),
|
episodesWatched: z.number().openapi({ type: "integer", format: "int64" }),
|
||||||
count: z
|
count: z.int(),
|
||||||
.number()
|
|
||||||
.int() /* .openapi({ type: "integer", format: "int64" }) */,
|
|
||||||
meanScore: z.number().openapi({ type: "number", format: "float" }),
|
meanScore: z.number().openapi({ type: "number", format: "float" }),
|
||||||
}),
|
}),
|
||||||
id: z.number().openapi({ type: "integer", format: "int64" }),
|
id: z.number().openapi({ type: "integer", format: "int64" }),
|
||||||
@@ -17,6 +23,4 @@ export const User = z
|
|||||||
medium: z.string(),
|
medium: z.string(),
|
||||||
large: z.string(),
|
large: z.string(),
|
||||||
}),
|
}),
|
||||||
})
|
});
|
||||||
.optional()
|
|
||||||
.nullable();
|
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ id = "c8db249d8ee7462b91f9c374321776e4"
|
|||||||
preview_id = "ff38240eb2aa4b1388c705f4974f5aec"
|
preview_id = "ff38240eb2aa4b1388c705f4974f5aec"
|
||||||
|
|
||||||
[triggers]
|
[triggers]
|
||||||
crons = ["0 */9 * * *"]
|
crons = ["0 */12 * * *", "0 18 * * *"]
|
||||||
|
|
||||||
[[d1_databases]]
|
[[d1_databases]]
|
||||||
binding = "DB"
|
binding = "DB"
|
||||||
|
|||||||
Reference in New Issue
Block a user