diigo-cache: the public page only reads
/public/<username> reads through the cache's viewer() facet (PageViewer: stored pages, knownTotal, fill progress — the reader alone), so it can't fetch from diigo or write storage. Behavior change, agreed beforehand: an unfetched page no longer fills itself on a miss; it says "not fetched yet" (JSON: stored: false), and the owner fills via the webkey page. The HIT/MISS line becomes "stored <when>". Strangers can no longer make the cache spend the owner's diigo session; the README's deployment concern shrinks to /api. Tests: viewing an unfetched page makes no webapp call; the page appears once stored; fill progress shows after one fill step. Generated-by: Claude Opus 5.5
This commit is contained in:
1 parent
a17d287e1d
commit
d7190abaf8
5 files changed
+54
-33
No files matched your search
@@ -2,7 +2,7 @@ import { env } from "cloudflare:workers";
|
||||
import { evictDurableObject, runInDurableObject } from "cloudflare:test";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import worker from "../src/index.js";
|
||||
import { toWebkey } from "../src/webkeys.js";
|
||||
import { idFromWebkey, toWebkey } from "../src/webkeys.js";
|
||||
import { FETCH_BUDGET } from "../src/query.js";
|
||||
import { PAGE_SIZE } from "../src/cache.js";
|
||||
|
||||
@@ -225,22 +225,28 @@ describe("worker routes", () => {
|
||||
const call = (path: string, init?: RequestInit) =>
|
||||
worker.fetch(new Request(`https://diigo-cache.test${path}`, init), env as never);
|
||||
|
||||
it("wires a public cache whose page fills itself on a miss", async () => {
|
||||
it("the public page only reads: an unfetched page says so; the owner's fill makes it appear", async () => {
|
||||
const wired = await call("/_wire", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ webappId: WEBAPP_ID, mode: "public" }),
|
||||
});
|
||||
expect(wired.status).toBe(200);
|
||||
const { username } = (await wired.json()) as { username: string };
|
||||
const { username, publicId } = (await wired.json()) as { username: string; publicId: string };
|
||||
expect(username).toBe("alice_99");
|
||||
|
||||
// page 2 was never fetched by anyone: the public cache asks the webapp
|
||||
// page 2 was never fetched: the public page can't fetch it
|
||||
const before = await testEnv.WEBAPP.calls();
|
||||
const unfetched = await (await call(`/public/${username}?page=2`)).text();
|
||||
expect(unfetched).toContain('<form method="get"');
|
||||
expect(unfetched).toContain("<strong>not fetched yet</strong>");
|
||||
expect(await testEnv.WEBAPP.calls()).toBe(before);
|
||||
|
||||
// the owner's side stores it (here, directly on the public cache)
|
||||
const pub = testEnv.PUBLIC_DO.get(testEnv.PUBLIC_DO.idFromString(idFromWebkey(publicId)));
|
||||
await pub.page(2, PAGE_SIZE);
|
||||
const html = await (await call(`/public/${username}?page=2`)).text();
|
||||
expect(html).toContain('<form method="get"');
|
||||
expect(html).toContain("<strong>cache: MISS");
|
||||
const again = await (await call(`/public/${username}?page=2`)).text();
|
||||
expect(again).toContain("<strong>cache: HIT");
|
||||
expect(html).toMatch(/<strong>stored \d{4}-\d\d-\d\dT[\d:.]+Z<\/strong>/);
|
||||
// PAGE_SIZE 100: page 2 is bookmarks 101..200
|
||||
expect(html).toContain(">bookmark 130<");
|
||||
// the date comes first, in toISOString() form
|
||||
@@ -249,8 +255,9 @@ describe("worker routes", () => {
|
||||
|
||||
const json = (await (
|
||||
await call(`/public/${username}?page=2`, { headers: { accept: "application/json" } })
|
||||
).json()) as { pages: number; items: unknown[] };
|
||||
).json()) as { pages: number; stored: boolean; items: unknown[] };
|
||||
expect(json.pages).toBe(3);
|
||||
expect(json.stored).toBe(true);
|
||||
expect(json.items).toHaveLength(67); // 101..200 minus its 33 multiples of 3
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user