Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@hono/zod-validator": "^0.4.3",
"@stremlist/shared": "workspace:*",
"@supabase/supabase-js": "^2.95.3",
"@vercel/functions": "^3.9.5",
"@vercel/related-projects": "^1.0.0",
"hono": "^4.11.9",
"linkedom": "^0.18.9",
Expand Down
25 changes: 25 additions & 0 deletions apps/backend/src/__tests__/watchlist-crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { describe, it, expect, beforeEach, vi } from "vitest";

import app from "../index.js";

const backgroundMocks = vi.hoisted(() => ({
scheduleBackgroundTask: vi.fn(),
}));
const prewarmMocks = vi.hoisted(() => ({
prewarmWatchlists: vi.fn(),
}));

vi.mock("../lib/background", () => backgroundMocks);
vi.mock("../services/watchlist-prewarm", () => prewarmMocks);

vi.mock("../lib/supabase", async () => {
return await import("./helpers/mock-supabase.js");
});
Expand Down Expand Up @@ -91,6 +101,9 @@ describe("Watchlist CRUD via API", () => {
beforeEach(() => {
db.reset();
seedUser(OWNER);
backgroundMocks.scheduleBackgroundTask.mockReset();
prewarmMocks.prewarmWatchlists.mockReset();
prewarmMocks.prewarmWatchlists.mockResolvedValue(undefined);
});

// ---- GET /:userId/config ----
Expand Down Expand Up @@ -178,6 +191,17 @@ describe("Watchlist CRUD via API", () => {

expect(data.watchlists[0].imdbUserId).toBe(OWNER);
expect(data.watchlists[1].imdbUserId).toBe(OTHER_IMDB);

expect(backgroundMocks.scheduleBackgroundTask).toHaveBeenCalledOnce();
const task = backgroundMocks.scheduleBackgroundTask.mock.calls[0][0] as
| (() => Promise<void>)
| undefined;
expect(task).toBeTypeOf("function");
await task?.();
expect(prewarmMocks.prewarmWatchlists).toHaveBeenCalledWith(
OWNER,
data.watchlists,
);
});

it("preserves IDs when updating sort order", async () => {
Expand Down Expand Up @@ -269,6 +293,7 @@ describe("Watchlist CRUD via API", () => {
expect(res.status).toBe(400);
const data = await res.json();
expect(data.error).toContain("unique");
expect(backgroundMocks.scheduleBackgroundTask).not.toHaveBeenCalled();
});

it("rejects empty watchlist array", async () => {
Expand Down
98 changes: 98 additions & 0 deletions apps/backend/src/lib/__tests__/background.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { Hono } from "hono";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

const vercelMocks = vi.hoisted(() => ({
waitUntil: vi.fn(),
}));

vi.mock("@vercel/functions", () => vercelMocks);

import { scheduleBackgroundTask } from "../background";

describe("scheduleBackgroundTask", () => {
const originalVercel = process.env.VERCEL;

beforeEach(() => {
vercelMocks.waitUntil.mockReset();
delete process.env.VERCEL;
});

afterEach(() => {
if (originalVercel === undefined) {
delete process.env.VERCEL;
} else {
process.env.VERCEL = originalVercel;
}
});

it("registers the task with Vercel so it can finish after the response", async () => {
process.env.VERCEL = "1";
const task = vi.fn().mockResolvedValue(undefined);

scheduleBackgroundTask(task);

expect(vercelMocks.waitUntil).toHaveBeenCalledOnce();
const promise = vercelMocks.waitUntil.mock.calls[0][0] as Promise<unknown>;
await promise;
expect(task).toHaveBeenCalledOnce();
});

it("runs the task locally without registering it with Vercel", async () => {
const task = vi.fn().mockResolvedValue(undefined);

scheduleBackgroundTask(task);
await vi.waitFor(() => {
expect(task).toHaveBeenCalledOnce();
});

expect(vercelMocks.waitUntil).not.toHaveBeenCalled();
});

it("does not delay the HTTP response while the task is pending", async () => {
let finishTask: (() => void) | undefined;
const pendingTask = new Promise<void>((resolve) => {
finishTask = resolve;
});
const task = vi.fn(() => pendingTask);
const app = new Hono().get("/probe", (c) => {
scheduleBackgroundTask(task);
return c.json({ ok: true });
});

const response = await app.request("/probe");

expect(response.status).toBe(200);
await expect(response.json()).resolves.toEqual({ ok: true });
expect(task).toHaveBeenCalledOnce();
expect(vercelMocks.waitUntil).not.toHaveBeenCalled();

finishTask?.();
await pendingTask;
});

it.each([
[
"synchronous",
() => {
throw new Error("sync failure");
},
],
["asynchronous", () => Promise.reject(new Error("async failure"))],
])(
"logs a %s task failure without an unhandled rejection",
async (_, task) => {
const error = vi
.spyOn(console, "error")
.mockImplementation(() => undefined);

scheduleBackgroundTask(task);

await vi.waitFor(() => {
expect(error).toHaveBeenCalledWith(
"Background task failed:",
expect.any(Error),
);
});
},
);
});
15 changes: 15 additions & 0 deletions apps/backend/src/lib/background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { waitUntil } from "@vercel/functions";

type BackgroundTask = () => Promise<unknown>;

export function scheduleBackgroundTask(task: BackgroundTask): void {
const promise = Promise.resolve()
.then(task)
.catch((error: unknown) => {
console.error("Background task failed:", error);
});

if (process.env.VERCEL) {
waitUntil(promise);
}
}
9 changes: 9 additions & 0 deletions apps/backend/src/routes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@stremlist/shared";
import { Hono } from "hono";
import { z } from "zod";
import { scheduleBackgroundTask } from "../lib/background";
import { resend } from "../lib/resend";
import { supabase } from "../lib/supabase";
import {
Expand All @@ -25,6 +26,7 @@ import {
setUserRpdbApiKey,
} from "../services/user";
import { getWatchlistByConfig } from "../services/watchlist";
import { prewarmWatchlists } from "../services/watchlist-prewarm";

const REFRESH_COOLDOWN_MS =
(Number.isFinite(Number(process.env.REFRESH_COOLDOWN_SECONDS))
Expand Down Expand Up @@ -190,6 +192,13 @@ const api = new Hono()
setUserRpdbApiKey(userId, normalizedRpdbApiKey),
]);

// A fresh installation already has a seeded watchlist ID, so an
// "ID-less rows only" check would miss its first scrape. Queue every
// saved watchlist and let the normal cache-first path skip warm entries.
scheduleBackgroundTask(() =>
prewarmWatchlists(userId, updatedWatchlists),
);

return c.json({ ok: true, watchlists: updatedWatchlists });
},
)
Expand Down
19 changes: 18 additions & 1 deletion apps/backend/src/routes/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { BASE_MANIFEST, ADDON_VERSION } from "@stremlist/shared";
import {
BASE_MANIFEST,
ADDON_VERSION,
IMDB_USER_ID_PATTERN,
} from "@stremlist/shared";
import type { StremioManifest } from "@stremlist/shared";
import { Hono } from "hono";
import { buildManifestCatalogs } from "../services/stremio-catalogs";
Expand Down Expand Up @@ -30,6 +34,19 @@ manifest.get("/:userId/manifest.json", async (c) => {
const userId = c.req.param("userId");
console.log(`Serving user-specific manifest for: ${userId}`);

if (!IMDB_USER_ID_PATTERN.test(userId)) {
return c.json(
{
...structuredClone(BASE_MANIFEST),
behaviorHints: {
configurable: true,
configurationRequired: true,
},
},
400,
);
}

try {
await ensureUser(userId);
const savedRpdbApiKey = await getUserRpdbApiKey(userId);
Expand Down
Loading
Loading