main
1// FlagsCache is the cache-enabled entrypoint (one ruleset, one tag). FlagControl is the RPC write API.
2// Invalidate runs inside FlagsCache: Workers Cache scopes tags to the entrypoint that calls invalidate.
3import { WorkerEntrypoint } from 'cloudflare:workers';
4import { chainGet, dig } from '../absent.ts';
5import { getConfig, setRollout, setTargeting } from './api.ts';
6import { serveFlags, type FlagCacheEnv } from './cache.ts';
7import { r2Store, type InvalidateResult, type R2BucketLike } from './store.ts';
8import { FLAGS_CACHE_TAG } from './ttl.ts';
9import type { ReadResult, Targeting, WriteResult } from './types.ts';
11export type FlagEnv = {
12 FLAGS: R2BucketLike | null;
13 FLAGS_MAX_AGE_S: string | null;
14 FLAGS_SWR_S: string | null;
15};
17type InvalidateCall = { tags: string[] };
19function successFalse(result: unknown): boolean {
20 const success = chainGet(result, 'success');
21 if (success.tag === 'none') return false;
22 return success.value === false;
23}
25/** `error.message`, including the throw when the entry itself is null. A missing message is empty. */
26function readMessage(error: unknown): unknown {
27 if (error === null || (typeof error !== 'object' && typeof error !== 'function')) {
28 return (error as { message: unknown }).message;
29 }
30 const message = chainGet(error, 'message');
31 if (message.tag === 'none') return '';
32 return message.value;
33}
35function failureReason(result: unknown): string {
36 const errors = chainGet(result, 'errors');
37 if (errors.tag === 'none') return '';
38 const mapped = (errors.value as { map(fn: (error: unknown) => unknown): unknown }).map(error => readMessage(error));
39 const filtered = (mapped as { filter(fn: (message: unknown) => boolean): { join(sep: string): string } }).filter(message => !!message);
40 return filtered.join('; ');
41}
43function fromPlatform(result: unknown): InvalidateResult {
44 if (!result || successFalse(result)) {
45 const reason = failureReason(result);
46 return { kind: 'failed', reason: reason.length > 0 ? reason : 'invalidate failed' };
47 }
48 return { kind: 'ok' };
49}
51async function callInvalidate(ctx: unknown): Promise<unknown> {
52 const cache = chainGet(ctx, 'cache');
53 if (cache.tag === 'none') return null;
54 const invalidate = chainGet(cache.value, 'invalidate');
55 if (invalidate.tag === 'none') return null;
56 const fn = invalidate.value as (this: unknown, options: InvalidateCall) => Promise<unknown>;
57 return fn.call(cache.value, { tags: [FLAGS_CACHE_TAG] });
58}
60export class FlagsCache extends WorkerEntrypoint<FlagEnv> {
61 override async fetch(request: Request): Promise<Response> {
62 // FlagCacheEnv still spells optional bindings. Null is the same miss: envBinding maps both to none.
63 return serveFlags(request, this.env as FlagCacheEnv, this.env.FLAGS ? r2Store(this.env.FLAGS) : null);
64 }
66 async invalidate(): Promise<InvalidateResult> {
67 try {
68 return fromPlatform(await callInvalidate(this.ctx));
69 } catch (error) {
70 return { kind: 'failed', reason: error instanceof Error ? error.message : 'invalidate failed' };
71 }
72 }
73}
75async function invalidateFrom(ctx: unknown): Promise<InvalidateResult> {
76 const invalidate = dig(ctx, ['exports', 'FlagsCache', 'invalidate']);
77 if (invalidate.tag === 'none' || !invalidate.value) {
78 return { kind: 'failed', reason: 'FlagsCache entrypoint is not configured' };
79 }
80 try {
81 const fn = invalidate.value as () => Promise<InvalidateResult>;
82 return await fn();
83 } catch (error) {
84 return { kind: 'failed', reason: error instanceof Error ? error.message : 'invalidate failed' };
85 }
86}
88export class FlagControl extends WorkerEntrypoint<FlagEnv> {
89 async getConfig(): Promise<ReadResult> {
90 if (!this.env.FLAGS) return { kind: 'unavailable', reason: 'FLAGS binding is not configured' };
91 return getConfig(r2Store(this.env.FLAGS));
92 }
94 async setRollout(flag: string, basisPoints: number, expectedRevision: string): Promise<WriteResult> {
95 if (!this.env.FLAGS) return { kind: 'unavailable', reason: 'FLAGS binding is not configured' };
96 return setRollout(r2Store(this.env.FLAGS), { invalidate: () => invalidateFrom(this.ctx) }, flag, basisPoints, expectedRevision);
97 }
99 async setTargeting(flag: string, targeting: Targeting, expectedRevision: string): Promise<WriteResult> {
100 if (!this.env.FLAGS) return { kind: 'unavailable', reason: 'FLAGS binding is not configured' };
101 return setTargeting(r2Store(this.env.FLAGS), { invalidate: () => invalidateFrom(this.ctx) }, flag, targeting, expectedRevision);
102 }
103}
Diagnostics4 git calls · 25 ms
CallStatusSizeTime025 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020040 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=19 git.response.bytes=41349 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=526b305930b8ef382f2d7a1f9c0238730a585921 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=41349 git.pkt.payload.max=41254 git.sideband.band1.bytes=41253 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=61 git.pack.bytes=41253 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c git.pack.full=61Open pack map
fetch2001.4 KiB · hit19 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1478 git.response.cache=HIT git.response.duration_ms=19 git.fetch.wants=e26af699fdd92281e0a31499396df649a2dd47ed git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1478 git.pkt.payload.max=1457 git.sideband.band1.bytes=1456 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
- Ref
- refs/heads/main
- Commit
- 526b305930b8ef382f2d7a1f9c0238730a585921
- cf-ray
- a5e2bd90bc94cfc3
- Request
- 868ffe40…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format