@cloudhal/cloudflare 0.9.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,7 +66,7 @@ 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 {
@@ -343,8 +343,8 @@ export function createAdminWorker(options = {}) {
343
343
  let migrated = false;
344
344
  /** The request backstop's isolate state (RFC-0008 §8.6); plain numbers. */
345
345
  const backstop = { lastCheck: 0, lastRun: 0 };
346
- /** The gates version the site Worker confirmed (RFC-0008 §7.7, RFC-0042 §3.6); 0: none yet. */
347
- 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: {} };
348
348
  const ensureMigrated = async (sql ) => {
349
349
  if (migrated) return;
350
350
  try {
@@ -439,18 +439,9 @@ export function createAdminWorker(options = {}) {
439
439
  blobs,
440
440
  blocks,
441
441
  ...storeExtras(options, env, sql),
442
- // S15: `availability` is refused until the site Worker says it enforces gates.
443
- gates: siteGateSupport({
444
- fetch: siteBinding
445
- ? (u, init) => siteBinding.fetch(u, init )
446
- : undefined,
447
- token: purgeToken,
448
- confirmed: () => gatesConfirmed,
449
- remember: (version) => {
450
- gatesConfirmed = version;
451
- },
452
- log,
453
- }),
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 }),
454
445
  onPublished: async (site, change) => {
455
446
  const binding = routedSite(env);
456
447
  if (!binding || !env.CLOUDHAL_PURGE_TOKEN) return;
@@ -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) {
@@ -64,6 +64,7 @@ import { SqlScheduleStore } from "@cloudhal/schedules/sql";
64
64
  import { D1Driver } from "@cloudhal/storage/d1";
65
65
  import { R2BlobStore } from "@cloudhal/storage/r2";
66
66
  import { flushCommentPurges, runCommentsHourly } from "./comments.js";
67
+ import { entryRefsJob } from "./entry-refs-job.js";
67
68
  import { listValuesJob } from "./list-values-job.js";
68
69
  import { PURGE_MAX_REQUESTS, PURGE_TAGS_PER_REQUEST } from "./purge.js";
69
70
  import { attachedSite, bindingFor, routedSite } from "./site-routes.js";
@@ -273,6 +274,7 @@ export function adminSystemJobs(
273
274
  },
274
275
  },
275
276
  listValuesJob(sql, { paid: env.CLOUDHAL_TICK_BUDGET === "paid", log }),
277
+ entryRefsJob(sql, { paid: env.CLOUDHAL_TICK_BUDGET === "paid", log }),
276
278
  oauthClientsPruneJob(sql, log),
277
279
  ];
278
280
  if (o.commentsOn)
@@ -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.9.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.9.0",
52
- "@cloudhal/analytics": "0.9.0",
53
- "@cloudhal/api": "0.9.0",
54
- "@cloudhal/comments": "0.9.0",
55
- "@cloudhal/core-ts": "0.9.0",
56
- "@cloudhal/mcp-server": "0.9.0",
57
- "@cloudhal/schedules": "0.9.0",
58
- "@cloudhal/storage": "0.9.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.9.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",