diigo-cache: fill from a webkey page over Cap'n Web
Starting a fill no longer POSTs the cache id to /_fill. Instead: - the created page links the cache's webkey, /cache/#<cache id>: a skeleton page, the same for every key, that reads the key from the fragment (never sent to the server or in a Referer) - its script trades the key in-band for a capability over Cap'n Web at /api: CacheApi.cache(key) returns a CacheCap facet (fill, status, fingerprint, the public companion's fill) and never the DO's wiring methods; malformed and unknown keys get one refusal that never echoes the key, and each refusal is logged without it - then calls startFill(): trade and call pipeline into one round trip - the worker serves capnweb's browser build itself at /_capnweb.js (a wrangler Text rule), so no third-party script sits beside the key The /_fill route and its forms are gone. Tests run the real capnweb client against the worker (fetch routed to worker.fetch). Generated-by: Claude Opus 5.5
This commit is contained in:
1 parent
fbe2538345
commit
8c221abe6f
9 files changed
+397
-112
No files matched your search
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* @file the Cap'n Web API at /api: trade a cache's webkey for a capability.
|
||||
*
|
||||
* The browser holds the key in a URL fragment (`/cache/#<key>`, see
|
||||
* docs/webkeys.md), so it never reaches the server in a URL. The skeleton
|
||||
* page passes it *in-band* — `api.cache(key)` — and gets back a stub for a
|
||||
* {@link CacheCap}: a facet exposing only what the holder should do (fill,
|
||||
* status, the public companion), never the DO's wiring methods.
|
||||
* Authorization is in-band, so cross-origin calls are safe (capnweb's
|
||||
* note on `newWorkersRpcResponse`); every argument is checked at runtime,
|
||||
* since TypeScript types are erased on the wire.
|
||||
*
|
||||
* @see https://github.com/cloudflare/capnweb
|
||||
*/
|
||||
|
||||
import { RpcTarget } from "capnweb";
|
||||
import type { DiigoCache, FillStatus, PublicCache } from "./cache.js";
|
||||
import { idFromWebkey, isWebkey, shashKey } from "./webkeys.js";
|
||||
|
||||
/** the bindings the API reaches (structural) */
|
||||
export type ApiEnv = {
|
||||
CACHE_DO: DurableObjectNamespace<DiigoCache>;
|
||||
PUBLIC_DO: DurableObjectNamespace<PublicCache>;
|
||||
};
|
||||
|
||||
/** the fill-job surface of one cache (private or public) */
|
||||
type Fillable = {
|
||||
startFill(opts?: { pagesPerStep?: number }): Promise<FillStatus>;
|
||||
fillStatus(): Promise<FillStatus | undefined>;
|
||||
stopFill(): Promise<FillStatus>;
|
||||
};
|
||||
|
||||
/** a cache's fill job, and nothing else */
|
||||
export class FillCap extends RpcTarget {
|
||||
#cache: Fillable;
|
||||
|
||||
constructor(cache: Fillable) {
|
||||
super();
|
||||
this.#cache = cache;
|
||||
}
|
||||
|
||||
/** start (or rejoin) the job; `pagesPerStep` 1–50 */
|
||||
async startFill(opts: unknown = {}): Promise<FillStatus> {
|
||||
const step = (opts as { pagesPerStep?: unknown } | null)?.pagesPerStep;
|
||||
if (step !== undefined && typeof step !== "number") throw new Error("pagesPerStep: expected a number");
|
||||
return this.#cache.startFill(step === undefined ? {} : { pagesPerStep: step });
|
||||
}
|
||||
|
||||
/** the job's progress, or undefined if none was started */
|
||||
async fillStatus(): Promise<FillStatus | undefined> {
|
||||
return this.#cache.fillStatus();
|
||||
}
|
||||
|
||||
/** stop the job; cached pages stay */
|
||||
async stopFill(): Promise<FillStatus> {
|
||||
return this.#cache.stopFill();
|
||||
}
|
||||
}
|
||||
|
||||
/** what the holder of a private cache's key may do */
|
||||
export class CacheCap extends FillCap {
|
||||
#key: string;
|
||||
#publicId: string | undefined;
|
||||
#env: ApiEnv;
|
||||
|
||||
constructor(env: ApiEnv, key: string, cache: Fillable, publicId: string | undefined) {
|
||||
super(cache);
|
||||
this.#env = env;
|
||||
this.#key = key;
|
||||
this.#publicId = publicId;
|
||||
}
|
||||
|
||||
/** a prefix of the key's one-way hash: names it without granting it */
|
||||
async fingerprint(): Promise<string> {
|
||||
return (await shashKey(this.#key)).slice(0, 8);
|
||||
}
|
||||
|
||||
/** whether a public companion exists (see {@link publicCache}) */
|
||||
hasPublic(): boolean {
|
||||
return this.#publicId !== undefined;
|
||||
}
|
||||
|
||||
/** the public companion's fill job; refused if never published (a
|
||||
* stub, not a maybe-null, so calls on it pipeline) */
|
||||
publicCache(): FillCap {
|
||||
if (!this.#publicId) throw new Error("no public cache: publish this cache first");
|
||||
const ns = this.#env.PUBLIC_DO;
|
||||
return new FillCap(ns.get(ns.idFromString(idFromWebkey(this.#publicId))));
|
||||
}
|
||||
}
|
||||
|
||||
/** the main interface at /api */
|
||||
export class CacheApi extends RpcTarget {
|
||||
#env: ApiEnv;
|
||||
|
||||
constructor(env: ApiEnv) {
|
||||
super();
|
||||
this.#env = env;
|
||||
}
|
||||
|
||||
/**
|
||||
* trade a cache's webkey for its capability. Refused keys get one
|
||||
* message for malformed and unknown alike, which never echoes the key.
|
||||
*/
|
||||
async cache(key: unknown): Promise<CacheCap> {
|
||||
const refused = () => new Error("no such cache");
|
||||
if (typeof key !== "string" || !isWebkey(key, 256)) {
|
||||
console.log({ event: "api", refused: "cache", reason: "malformed key" });
|
||||
throw refused();
|
||||
}
|
||||
const ns = this.#env.CACHE_DO;
|
||||
let stub;
|
||||
try {
|
||||
// a well-formed id from another namespace throws here
|
||||
stub = ns.get(ns.idFromString(idFromWebkey(key)));
|
||||
} catch (err) {
|
||||
// the key is never logged: it is the bearer secret
|
||||
console.log({ event: "api", refused: "cache", reason: String(err) });
|
||||
throw refused();
|
||||
}
|
||||
const wiring = await stub.wiring();
|
||||
if (!wiring) {
|
||||
console.log({ event: "api", refused: "cache", reason: "not wired" });
|
||||
throw refused();
|
||||
}
|
||||
return new CacheCap(this.#env, key, stub, wiring.publicId);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user