@cloudhal/cloudflare 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -66,13 +66,14 @@ import { R2BlobStore } from "@cloudhal/storage/r2";
66
66
  import { browserRenderingRenderer } from "./browser-render.js";
67
67
  import { buildRoomClass, createDurableBuildHub } from "./build-rooms.js";
68
68
  import { adminComments, commentsOn, flushCommentPurges } from "./comments.js";
69
- import { siteGateSupport } from "./features.js";
69
+ import { featureRoute, siteSupport } from "./features.js";
70
70
  import { adminMail, } from "./mail.js";
71
71
  import { purgeSiteCache } from "./purge.js";
72
72
  import {
73
73
 
74
+ cronTick,
74
75
  followUpDelay,
75
- releasePurger,
76
+ publishNowPurge,
76
77
  requestBackstop,
77
78
  runAdminTick,
78
79
 
@@ -342,8 +343,8 @@ export function createAdminWorker(options = {}) {
342
343
  let migrated = false;
343
344
  /** The request backstop's isolate state (RFC-0008 §8.6); plain numbers. */
344
345
  const backstop = { lastCheck: 0, lastRun: 0 };
345
- /** The gates version the site Worker confirmed (RFC-0008 §7.7, RFC-0042 §3.6); 0: none yet. */
346
- let gatesConfirmed = 0;
346
+ /** What the site Worker confirmed: gates (RFC-0008 §7.7, RFC-0042 §3.6), fields (RFC-0045). */
347
+ const confirmed = { gates: {}, fields: {} };
347
348
  const ensureMigrated = async (sql ) => {
348
349
  if (migrated) return;
349
350
  try {
@@ -438,18 +439,9 @@ export function createAdminWorker(options = {}) {
438
439
  blobs,
439
440
  blocks,
440
441
  ...storeExtras(options, env, sql),
441
- // S15: `availability` is refused until the site Worker says it enforces gates.
442
- gates: siteGateSupport({
443
- fetch: siteBinding
444
- ? (u, init) => siteBinding.fetch(u, init )
445
- : undefined,
446
- token: purgeToken,
447
- confirmed: () => gatesConfirmed,
448
- remember: (version) => {
449
- gatesConfirmed = version;
450
- },
451
- log,
452
- }),
442
+ // S15 and RFC-0045 §1: `availability`, and reference and repeater fields, are refused
443
+ // until the site Worker serving the site says it enforces gates and reads those fields.
444
+ ...siteSupport({ route: (id) => featureRoute(env, id), token: purgeToken, confirmed, log }),
453
445
  onPublished: async (site, change) => {
454
446
  const binding = routedSite(env);
455
447
  if (!binding || !env.CLOUDHAL_PURGE_TOKEN) return;
@@ -537,7 +529,7 @@ export function createAdminWorker(options = {}) {
537
529
  // RFC-0008 §12–§13: the schedule routes and tools; "Publish now" purges like a tick.
538
530
  schedules: {
539
531
  store: new SqlScheduleStore(sql),
540
- purge: releasePurger(env, sql, { log }),
532
+ ...publishNowPurge(env, sql, log),
541
533
  automationsEnabled: env.CLOUDHAL_AUTOMATIONS !== "off",
542
534
  },
543
535
  scheduler: {
@@ -571,8 +563,7 @@ export function createAdminWorker(options = {}) {
571
563
  await ensureMigrated(sql);
572
564
  const tick = (now ) =>
573
565
  runAdminTick(env, {
574
- trigger: "cron",
575
- now,
566
+ ...cronTick(controller, env, now),
576
567
  log,
577
568
  analytics: (s, at) => adminAnalytics(env, s, { now: () => at, log }),
578
569
  commentsOn: commentsOn(env),
@@ -0,0 +1,8 @@
1
+ import type { SystemJobDefinition } from "@cloudhal/schedules";
2
+ import type { SqlDriver } from "@cloudhal/storage";
3
+ /** The next five-minute mark plus 2:30 strictly after `after` (UTC). */
4
+ export declare const everyFiveMinutesOffset: (after: Date) => Date;
5
+ export declare function entryRefsJob(sql: SqlDriver, o: {
6
+ paid: boolean;
7
+ log: (line: string) => void;
8
+ }): SystemJobDefinition;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * RFC-0045 §2.4, rollout step 7: the admin Worker's `refs.rebuild` system job. It runs the
3
+ * one-time per-site `entry_refs` rebuild (`rebuildEntryRefs`: per node, anchored on each node's
4
+ * read hash, resumable by cursor, never a site-wide `DELETE`) every five minutes, offset from
5
+ * `lists.backfill`, until every site is done; from then on a run costs its two discovery reads.
6
+ * It is a safety net: the gate (§1) means no reference value existed before the types were
7
+ * enabled, so it should find nothing to change.
8
+ *
9
+ * The declared `cost` is the run's statement budget, which `rebuildEntryRefs` enforces on every
10
+ * statement it issues, so the tick's meter never sees more.
11
+ */
12
+ import { rebuildEntryRefs } from "@cloudhal/core-ts";
13
+
14
+
15
+
16
+ const EVERY_MS = 5 * 60_000;
17
+ /** Half-way between `lists.backfill`'s marks, so the two rarely share a tick's budget. */
18
+ const OFFSET_MS = 150_000;
19
+
20
+ /** The next five-minute mark plus 2:30 strictly after `after` (UTC). */
21
+ export const everyFiveMinutesOffset = (after ) =>
22
+ new Date((Math.floor((after.getTime() - OFFSET_MS) / EVERY_MS) + 1) * EVERY_MS + OFFSET_MS);
23
+
24
+ export function entryRefsJob(
25
+ sql ,
26
+ o ,
27
+ ) {
28
+ // Discovery is two statements, a chunk of 100 entries about five; the free tick allows 40 in all.
29
+ const budget = o.paid
30
+ ? { entries: 2000, ms: 20_000, statements: 150 }
31
+ : { entries: 200, ms: 5_000, statements: 12 };
32
+ return {
33
+ name: "refs.rebuild",
34
+ cost: budget.statements,
35
+ next: everyFiveMinutesOffset,
36
+ async run() {
37
+ const r = await rebuildEntryRefs(sql, { budget });
38
+ const written = r.sites.reduce((n, s) => n + s.written, 0);
39
+ const waiting = r.sites.filter((s) => !s.done).length;
40
+ if (r.sites.length > 0)
41
+ o.log(
42
+ `refs rebuild: ${written} entr${written === 1 ? "y" : "ies"} checked, ${r.sites.length - waiting} site(s) done, ${waiting} not yet${r.exhausted ? " (more next run)" : ""}`,
43
+ );
44
+ },
45
+ };
46
+ }
@@ -8,6 +8,7 @@
8
8
  import { type GateSupport } from "@cloudhal/core-ts/availability";
9
9
  import { type FieldTypeSupport } from "@cloudhal/core-ts/fields";
10
10
  import type { SiteFetch } from "./purge.js";
11
+ import { type SiteRoutesEnv } from "./site-routes.js";
11
12
  /** The features the site Worker reports, or `undefined` when it does not answer (older Workers). */
12
13
  export declare function siteFeatures(fetch: SiteFetch, token: string): Promise<{
13
14
  gates?: number;
@@ -30,8 +31,9 @@ export declare function siteGateSupport(options: {
30
31
  /**
31
32
  * RFC-0045 §1: the store's reference and repeater capability for one admin Worker isolate, the
32
33
  * twin of `siteGateSupport`: yes once the site Worker answers `{ "fields": 2 }` or more (kept as
33
- * a plain value); a refusal or an error is asked again. Built in rollout step 2; nothing passes it
34
- * to a store until step 7 enables the types.
34
+ * a plain value); a refusal or an error is asked again. Built in rollout step 2; the admin Worker's
35
+ * store takes it from rollout step 7 (`siteSupport`), so a site Worker older than step 3 keeps the
36
+ * types refused.
35
37
  */
36
38
  export declare function siteFieldTypeSupport(options: {
37
39
  readonly fetch: SiteFetch | undefined;
@@ -40,3 +42,36 @@ export declare function siteFieldTypeSupport(options: {
40
42
  readonly remember: () => void;
41
43
  readonly log?: (line: string) => void;
42
44
  }): FieldTypeSupport;
45
+ /**
46
+ * What one admin Worker isolate has confirmed so far, per site Worker binding (`SITE`, `SITE_1`,
47
+ * …; RFC-0034 §4): each attached Worker may run another renderer version, so a confirmation from
48
+ * one never stands for another. Plain values, nothing awaited across requests (ADR-0012).
49
+ */
50
+ export interface SiteConfirmations {
51
+ /** The gates version each site Worker reported; absent: none yet. */
52
+ gates: Record<string, number>;
53
+ /** The site Workers that reported `fields: 2` or more (RFC-0045). */
54
+ fields: Record<string, true>;
55
+ }
56
+ /** The site Worker serving one site: its binding's name and a fetch to it. */
57
+ export interface SiteWorkerRoute {
58
+ readonly key: string;
59
+ readonly fetch: SiteFetch;
60
+ }
61
+ /**
62
+ * The store's capabilities, each asked of the site Worker that serves the site in question
63
+ * (`route`) and remembered for that Worker in `confirmed`: time gates (S15) and reference and
64
+ * repeater fields (RFC-0045 §1). A site with no site Worker is refused both, never asked; a
65
+ * refusal or a failed probe is not remembered, so it is asked again.
66
+ */
67
+ export declare function siteSupport(options: {
68
+ readonly route: (siteId: string) => SiteWorkerRoute | undefined;
69
+ readonly token: string | undefined;
70
+ readonly confirmed: SiteConfirmations;
71
+ readonly log?: (line: string) => void;
72
+ }): {
73
+ gates: GateSupport;
74
+ fieldTypes: FieldTypeSupport;
75
+ };
76
+ /** `siteSupport`'s route on the admin Worker: the binding that serves the site (RFC-0034 §4). */
77
+ export declare function featureRoute(env: SiteRoutesEnv, siteId: string): SiteWorkerRoute | undefined;
@@ -8,6 +8,7 @@
8
8
  import { AVAILABILITY_GATES_VERSION, } from "@cloudhal/core-ts/availability";
9
9
  import { FIELDS_FEATURE_VERSION, } from "@cloudhal/core-ts/fields";
10
10
 
11
+ import { siteWorkerFor } from "./site-routes.js";
11
12
 
12
13
  const FEATURES_URL = "https://site.internal/__cloudhal/features";
13
14
 
@@ -78,8 +79,9 @@ export function siteGateSupport(options
78
79
  /**
79
80
  * RFC-0045 §1: the store's reference and repeater capability for one admin Worker isolate, the
80
81
  * twin of `siteGateSupport`: yes once the site Worker answers `{ "fields": 2 }` or more (kept as
81
- * a plain value); a refusal or an error is asked again. Built in rollout step 2; nothing passes it
82
- * to a store until step 7 enables the types.
82
+ * a plain value); a refusal or an error is asked again. Built in rollout step 2; the admin Worker's
83
+ * store takes it from rollout step 7 (`siteSupport`), so a site Worker older than step 3 keeps the
84
+ * types refused.
83
85
  */
84
86
  export function siteFieldTypeSupport(options
85
87
 
@@ -107,3 +109,77 @@ export function siteFieldTypeSupport(options
107
109
  },
108
110
  };
109
111
  }
112
+
113
+ /**
114
+ * What one admin Worker isolate has confirmed so far, per site Worker binding (`SITE`, `SITE_1`,
115
+ * …; RFC-0034 §4): each attached Worker may run another renderer version, so a confirmation from
116
+ * one never stands for another. Plain values, nothing awaited across requests (ADR-0012).
117
+ */
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+ /** The site Worker serving one site: its binding's name and a fetch to it. */
126
+
127
+
128
+
129
+
130
+
131
+ /**
132
+ * The store's capabilities, each asked of the site Worker that serves the site in question
133
+ * (`route`) and remembered for that Worker in `confirmed`: time gates (S15) and reference and
134
+ * repeater fields (RFC-0045 §1). A site with no site Worker is refused both, never asked; a
135
+ * refusal or a failed probe is not remembered, so it is asked again.
136
+ */
137
+ export function siteSupport(options
138
+
139
+
140
+
141
+
142
+ ) {
143
+ const { route, token, confirmed, log } = options;
144
+ const common = (siteId ) => {
145
+ const r = route(siteId);
146
+ return { r, base: { fetch: r?.fetch, token, ...(log ? { log } : {}) } };
147
+ };
148
+ return {
149
+ gates: {
150
+ enforced: (siteId, version) => {
151
+ const { r, base } = common(siteId);
152
+ return siteGateSupport({
153
+ ...base,
154
+ confirmed: () => (r ? (confirmed.gates[r.key] ?? 0) : 0),
155
+ remember: (v) => {
156
+ if (r) confirmed.gates[r.key] = v;
157
+ },
158
+ }).enforced(siteId, version);
159
+ },
160
+ },
161
+ fieldTypes: {
162
+ supported: (siteId) => {
163
+ const { r, base } = common(siteId);
164
+ return siteFieldTypeSupport({
165
+ ...base,
166
+ confirmed: () => (r ? confirmed.fields[r.key] === true : false),
167
+ remember: () => {
168
+ if (r) confirmed.fields[r.key] = true;
169
+ },
170
+ }).supported(siteId);
171
+ },
172
+ },
173
+ };
174
+ }
175
+
176
+ /** `siteSupport`'s route on the admin Worker: the binding that serves the site (RFC-0034 §4). */
177
+ export function featureRoute(env , siteId ) {
178
+ const w = siteWorkerFor(env, siteId);
179
+ if (!w) return undefined;
180
+ const fetcher = w.fetcher;
181
+ return {
182
+ key: w.key,
183
+ fetch: (u, init) => fetcher.fetch(u, init ) ,
184
+ };
185
+ }
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import { createApi } from "@cloudhal/api";
9
9
  import { SiteGraphStore } from "@cloudhal/core-ts";
10
+ import { FIELDS_SUPPORTED } from "@cloudhal/core-ts/fields";
10
11
  import {
11
12
 
12
13
  FetcherBlobStore,
@@ -135,7 +136,9 @@ export function playgroundSiteClass(options )
135
136
  own,
136
137
  )
137
138
  : own;
138
- const store = new SiteGraphStore({ sql, blobs });
139
+ // The session's site is rendered by this Worker's own renderer (`options.render`), which
140
+ // reads reference and repeater fields (RFC-0045 §1, rollout step 7).
141
+ const store = new SiteGraphStore({ sql, blobs, fieldTypes: FIELDS_SUPPORTED });
139
142
  await store.init();
140
143
  let siteId = await this.state.storage.get ("siteId");
141
144
  if (!siteId) {
@@ -95,7 +95,30 @@ export interface AdminTickOptions {
95
95
  readonly commentsOn: boolean;
96
96
  /** Keeps a deferred purge's flush alive after the tick (the handler's `waitUntil`). */
97
97
  readonly waitUntil?: ((p: Promise<unknown>) => void) | undefined;
98
+ /**
99
+ * RFC-0045: the R2 calls release purges may still make in this invocation, shared by every tick
100
+ * it runs (the cron's follow-up); absent: one allowance per tick.
101
+ */
102
+ readonly releaseBlobs?: {
103
+ left: number;
104
+ } | undefined;
98
105
  }
106
+ /** "Publish now"'s purge, and the deployment's tick budget that sizes its blob calls (RFC-0045). */
107
+ export declare function publishNowPurge(env: SchedulerEnv, sql: SqlDriver, log: (line: string) => void): {
108
+ purge: ReleasePurger;
109
+ tickBudget: TickBudget;
110
+ };
111
+ /**
112
+ * A cron tick's trigger and clock, with RFC-0045's R2 allowance for release purges: one per
113
+ * invocation (`controller`), so the cron's follow-up tick shares it (the cap is per invocation).
114
+ */
115
+ export declare function cronTick(controller: object, env: SchedulerEnv, now: Date): {
116
+ trigger: "cron";
117
+ now: Date;
118
+ releaseBlobs: {
119
+ left: number;
120
+ };
121
+ };
99
122
  /** One tick of the admin Worker (cron, alarm, request backstop or the operator's run-due). */
100
123
  export declare function runAdminTick(env: SchedulerEnv, o: AdminTickOptions): Promise<TickReport>;
101
124
  /**
@@ -30,6 +30,7 @@ import { SqlCommentStore } from "@cloudhal/comments/sql";
30
30
  import {
31
31
 
32
32
 
33
+ releaseBlobAllowance,
33
34
  SiteGraphStore,
34
35
  searchProviderFromEnv,
35
36
  siteCacheTag,
@@ -63,6 +64,7 @@ import { SqlScheduleStore } from "@cloudhal/schedules/sql";
63
64
  import { D1Driver } from "@cloudhal/storage/d1";
64
65
  import { R2BlobStore } from "@cloudhal/storage/r2";
65
66
  import { flushCommentPurges, runCommentsHourly } from "./comments.js";
67
+ import { entryRefsJob } from "./entry-refs-job.js";
66
68
  import { listValuesJob } from "./list-values-job.js";
67
69
  import { PURGE_MAX_REQUESTS, PURGE_TAGS_PER_REQUEST } from "./purge.js";
68
70
  import { attachedSite, bindingFor, routedSite } from "./site-routes.js";
@@ -272,6 +274,7 @@ export function adminSystemJobs(
272
274
  },
273
275
  },
274
276
  listValuesJob(sql, { paid: env.CLOUDHAL_TICK_BUDGET === "paid", log }),
277
+ entryRefsJob(sql, { paid: env.CLOUDHAL_TICK_BUDGET === "paid", log }),
275
278
  oauthClientsPruneJob(sql, log),
276
279
  ];
277
280
  if (o.commentsOn)
@@ -380,10 +383,35 @@ export function adminBuiltinInputs(
380
383
 
381
384
 
382
385
 
386
+
387
+
388
+
389
+
390
+
383
391
 
384
392
 
385
393
  const sleep = (ms ) => new Promise((resolve) => setTimeout(resolve, ms));
386
394
 
395
+ /** "Publish now"'s purge, and the deployment's tick budget that sizes its blob calls (RFC-0045). */
396
+ export function publishNowPurge(env , sql , log ) {
397
+ return { purge: releasePurger(env, sql, { log }), tickBudget: tickBudget(env) };
398
+ }
399
+
400
+ const invocationBlobs = new WeakMap ();
401
+
402
+ /**
403
+ * A cron tick's trigger and clock, with RFC-0045's R2 allowance for release purges: one per
404
+ * invocation (`controller`), so the cron's follow-up tick shares it (the cap is per invocation).
405
+ */
406
+ export function cronTick(controller , env , now ) {
407
+ let releaseBlobs = invocationBlobs.get(controller);
408
+ if (!releaseBlobs) {
409
+ releaseBlobs = { left: releaseBlobAllowance(tickBudget(env)) };
410
+ invocationBlobs.set(controller, releaseBlobs);
411
+ }
412
+ return { trigger: "cron" , now, releaseBlobs };
413
+ }
414
+
387
415
  /** One tick of the admin Worker (cron, alarm, request backstop or the operator's run-due). */
388
416
  export function runAdminTick(env , o ) {
389
417
  const sql = new D1Driver(env.DB);
@@ -425,6 +453,7 @@ export function runAdminTick(env , o )
425
453
  log: o.log,
426
454
  }),
427
455
  purge: ({ counted }) => releasePurger(env, counted, { log: o.log, wake: flush }),
456
+ ...(o.releaseBlobs ? { releaseBlobs: o.releaseBlobs } : {}),
428
457
  automationsEnabled: env.CLOUDHAL_AUTOMATIONS !== "off",
429
458
  // RFC-0008 §11.1: the built-in jobs read analytics' 404 rows and the site's own feed.
430
459
  builtinInputs: ({ counted }) => adminBuiltinInputs(env, o.analytics(counted, o.now)),
@@ -14,6 +14,15 @@ export interface SiteRoutesEnv {
14
14
  }
15
15
  /** The Worker that serves `siteId`: its own binding when one is routed, else `SITE`. */
16
16
  export declare function bindingFor(env: SiteRoutesEnv, siteId: string | undefined): Fetcher | undefined;
17
+ /**
18
+ * The Worker that serves `siteId` and its binding's name (`SITE`, `SITE_1`, …): what the admin
19
+ * Worker asks about features (RFC-0008 §7.7, RFC-0045 §1), and keys its confirmations by, since
20
+ * each attached Worker may run another renderer version.
21
+ */
22
+ export declare function siteWorkerFor(env: SiteRoutesEnv, siteId: string): {
23
+ key: string;
24
+ fetcher: Fetcher;
25
+ } | undefined;
17
26
  /** True when `siteId` is served by an attached Worker (not the installation's own). */
18
27
  export declare function attachedSite(env: SiteRoutesEnv, siteId: string): boolean;
19
28
  /**
@@ -33,6 +33,21 @@ export function bindingFor(env , siteId )
33
33
  return isFetcher(bound) ? bound : env.SITE;
34
34
  }
35
35
 
36
+ /**
37
+ * The Worker that serves `siteId` and its binding's name (`SITE`, `SITE_1`, …): what the admin
38
+ * Worker asks about features (RFC-0008 §7.7, RFC-0045 §1), and keys its confirmations by, since
39
+ * each attached Worker may run another renderer version.
40
+ */
41
+ export function siteWorkerFor(
42
+ env ,
43
+ siteId ,
44
+ ) {
45
+ const name = routesOf(env)[siteId];
46
+ const bound = name ? (env )[name] : undefined;
47
+ if (name && isFetcher(bound)) return { key: name, fetcher: bound };
48
+ return env.SITE ? { key: "SITE", fetcher: env.SITE } : undefined;
49
+ }
50
+
36
51
  /** True when `siteId` is served by an attached Worker (not the installation's own). */
37
52
  export function attachedSite(env , siteId ) {
38
53
  return routesOf(env)[siteId] !== undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudhal/cloudflare",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "description": "Cloudhal on Cloudflare: the admin Worker (API, auth, MCP, Studio) over D1 + R2",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://cloudhal.dev",
@@ -48,19 +48,19 @@
48
48
  "README.md"
49
49
  ],
50
50
  "dependencies": {
51
- "@cloudhal/agent-runtime": "0.8.0",
52
- "@cloudhal/analytics": "0.8.0",
53
- "@cloudhal/api": "0.8.0",
54
- "@cloudhal/comments": "0.8.0",
55
- "@cloudhal/core-ts": "0.8.0",
56
- "@cloudhal/mcp-server": "0.8.0",
57
- "@cloudhal/schedules": "0.8.0",
58
- "@cloudhal/storage": "0.8.0",
51
+ "@cloudhal/agent-runtime": "0.10.0",
52
+ "@cloudhal/analytics": "0.10.0",
53
+ "@cloudhal/api": "0.10.0",
54
+ "@cloudhal/comments": "0.10.0",
55
+ "@cloudhal/core-ts": "0.10.0",
56
+ "@cloudhal/mcp-server": "0.10.0",
57
+ "@cloudhal/schedules": "0.10.0",
58
+ "@cloudhal/storage": "0.10.0",
59
59
  "zod": "^4.6.7"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@cloudflare/workers-types": "^5.20261007.1",
63
- "@cloudhal/astro-renderer": "0.8.0",
63
+ "@cloudhal/astro-renderer": "0.10.0",
64
64
  "@types/node": "^26.6.5",
65
65
  "typescript": "7.0.2",
66
66
  "vitest": "5.1.0",