copilotz 1.0.0-rc.4 → 1.0.0-rc.5

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.
Files changed (214) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.md +2 -2
  3. package/hosts/orgs/mod.d.ts +21 -1
  4. package/hosts/orgs/mod.js +81 -2
  5. package/hosts/web/node/serve.js +5 -2
  6. package/hosts/web/routes.d.ts +62 -5
  7. package/hosts/web/routes.js +135 -34
  8. package/hosts/web/server.d.ts +12 -2
  9. package/hosts/web/server.js +4 -3
  10. package/package.json +3 -3
  11. package/plugins/access/directory/actions/host.d.ts +2 -0
  12. package/plugins/access/directory/actions/host.js +55 -0
  13. package/plugins/access/directory/actions/org.js +2 -1
  14. package/plugins/access/directory/collections/org-host.d.ts +61 -0
  15. package/plugins/access/directory/collections/org-host.js +29 -0
  16. package/plugins/access/directory/mod.d.ts +3 -1
  17. package/plugins/access/directory/mod.js +7 -2
  18. package/plugins/billing/usage/actions/quote.d.ts +1 -0
  19. package/plugins/billing/usage/actions/quote.js +42 -0
  20. package/plugins/billing/usage/mod.d.ts +2 -0
  21. package/plugins/billing/usage/mod.js +4 -0
  22. package/plugins/compute/apps/actions/build.d.ts +1 -0
  23. package/plugins/compute/apps/actions/build.js +93 -0
  24. package/plugins/compute/apps/actions/list.d.ts +1 -0
  25. package/plugins/compute/apps/actions/list.js +13 -0
  26. package/plugins/compute/apps/actions/meter.d.ts +1 -0
  27. package/plugins/compute/apps/actions/meter.js +43 -0
  28. package/plugins/compute/apps/actions/publish.d.ts +1 -0
  29. package/plugins/compute/apps/actions/publish.js +36 -0
  30. package/plugins/compute/apps/actions/redeploy.d.ts +1 -0
  31. package/plugins/compute/apps/actions/redeploy.js +27 -0
  32. package/plugins/compute/apps/actions/remove.d.ts +1 -0
  33. package/plugins/compute/apps/actions/remove.js +47 -0
  34. package/plugins/compute/apps/adapters/cloudrun.d.ts +90 -0
  35. package/plugins/compute/apps/adapters/cloudrun.js +341 -0
  36. package/plugins/compute/apps/adapters/local.d.ts +15 -0
  37. package/plugins/compute/apps/adapters/local.js +139 -0
  38. package/plugins/compute/apps/adapters/tar.d.ts +29 -0
  39. package/plugins/compute/apps/adapters/tar.js +178 -0
  40. package/plugins/compute/apps/collections/app.d.ts +136 -0
  41. package/plugins/compute/apps/collections/app.js +58 -0
  42. package/plugins/compute/apps/contract.d.ts +71 -0
  43. package/plugins/compute/apps/contract.js +8 -0
  44. package/plugins/compute/apps/mod.d.ts +42 -0
  45. package/plugins/compute/apps/mod.js +65 -0
  46. package/plugins/compute/apps/resources/tools.d.ts +10 -0
  47. package/plugins/compute/apps/resources/tools.js +63 -0
  48. package/plugins/compute/apps/shared/archive.d.ts +24 -0
  49. package/plugins/compute/apps/shared/archive.js +69 -0
  50. package/plugins/compute/apps/shared/claims.d.ts +28 -0
  51. package/plugins/compute/apps/shared/claims.js +18 -0
  52. package/plugins/compute/apps/shared/errors.d.ts +4 -0
  53. package/plugins/compute/apps/shared/errors.js +4 -0
  54. package/plugins/compute/apps/shared/host.d.ts +19 -0
  55. package/plugins/compute/apps/shared/host.js +19 -0
  56. package/plugins/compute/apps/shared/label.d.ts +21 -0
  57. package/plugins/compute/apps/shared/label.js +51 -0
  58. package/plugins/compute/apps/shared/meter.d.ts +17 -0
  59. package/plugins/compute/apps/shared/meter.js +29 -0
  60. package/plugins/compute/apps/shared/publish.d.ts +39 -0
  61. package/plugins/compute/apps/shared/publish.js +113 -0
  62. package/plugins/compute/apps/shared/settings.d.ts +49 -0
  63. package/plugins/compute/apps/shared/settings.js +22 -0
  64. package/plugins/compute/apps/views/web/apps.d.ts +52 -0
  65. package/plugins/compute/apps/views/web/apps.js +48 -0
  66. package/plugins/compute/apps/views/web/publish.d.ts +24 -0
  67. package/plugins/compute/apps/views/web/publish.js +156 -0
  68. package/plugins/compute/apps/views/web/space.d.ts +15 -0
  69. package/plugins/compute/apps/views/web/space.js +126 -0
  70. package/plugins/compute/apps/views/web/style.d.ts +6 -0
  71. package/plugins/compute/apps/views/web/style.js +102 -0
  72. package/plugins/compute/apps/web.d.ts +16 -0
  73. package/plugins/compute/apps/web.js +29 -0
  74. package/plugins/compute/cloud/actions/create.d.ts +1 -0
  75. package/plugins/compute/cloud/actions/create.js +90 -0
  76. package/plugins/compute/cloud/actions/daily.d.ts +1 -0
  77. package/plugins/compute/cloud/actions/daily.js +75 -0
  78. package/plugins/compute/cloud/actions/destroy.d.ts +1 -0
  79. package/plugins/compute/cloud/actions/destroy.js +71 -0
  80. package/plugins/compute/cloud/actions/org.d.ts +1 -0
  81. package/plugins/compute/cloud/actions/org.js +21 -0
  82. package/plugins/compute/cloud/actions/pool-tick.d.ts +4 -0
  83. package/plugins/compute/cloud/actions/pool-tick.js +91 -0
  84. package/plugins/compute/cloud/actions/pool.d.ts +3 -0
  85. package/plugins/compute/cloud/actions/pool.js +98 -0
  86. package/plugins/compute/cloud/actions/release.d.ts +1 -0
  87. package/plugins/compute/cloud/actions/release.js +58 -0
  88. package/plugins/compute/cloud/actions/remote.d.ts +10 -0
  89. package/plugins/compute/cloud/actions/remote.js +24 -0
  90. package/plugins/compute/cloud/actions/tick.d.ts +1 -0
  91. package/plugins/compute/cloud/actions/tick.js +95 -0
  92. package/plugins/compute/cloud/actions/touch.d.ts +1 -0
  93. package/plugins/compute/cloud/actions/touch.js +32 -0
  94. package/plugins/compute/cloud/actions/vms.d.ts +7 -0
  95. package/plugins/compute/cloud/actions/vms.js +201 -0
  96. package/plugins/compute/cloud/actions/wake.d.ts +1 -0
  97. package/plugins/compute/cloud/actions/wake.js +147 -0
  98. package/plugins/compute/cloud/adapters/gce/index.d.ts +73 -0
  99. package/plugins/compute/cloud/adapters/gce/index.js +242 -0
  100. package/plugins/compute/cloud/adapters/local.d.ts +48 -0
  101. package/plugins/compute/cloud/adapters/local.js +179 -0
  102. package/plugins/compute/cloud/collections/cloud-computer.d.ts +109 -0
  103. package/plugins/compute/cloud/collections/cloud-computer.js +82 -0
  104. package/plugins/compute/cloud/collections/pool-vm.d.ts +79 -0
  105. package/plugins/compute/cloud/collections/pool-vm.js +36 -0
  106. package/plugins/compute/cloud/contract.d.ts +236 -0
  107. package/plugins/compute/cloud/contract.js +67 -0
  108. package/plugins/compute/cloud/mod.d.ts +92 -0
  109. package/plugins/compute/cloud/mod.js +185 -0
  110. package/plugins/compute/cloud/shared/google.d.ts +75 -0
  111. package/plugins/compute/cloud/shared/google.js +218 -0
  112. package/plugins/compute/cloud/shared/host.d.ts +11 -0
  113. package/plugins/compute/cloud/shared/host.js +29 -0
  114. package/plugins/compute/cloud/shared/meter.d.ts +29 -0
  115. package/plugins/compute/cloud/shared/meter.js +59 -0
  116. package/plugins/compute/cloud/shared/names.d.ts +10 -0
  117. package/plugins/compute/cloud/shared/names.js +11 -0
  118. package/plugins/compute/cloud/shared/people.d.ts +24 -0
  119. package/plugins/compute/cloud/shared/people.js +19 -0
  120. package/plugins/compute/cloud/shared/records.d.ts +18 -0
  121. package/plugins/compute/cloud/shared/records.js +8 -0
  122. package/plugins/compute/cloud/shared/settings.d.ts +75 -0
  123. package/plugins/compute/cloud/shared/settings.js +48 -0
  124. package/plugins/compute/cloud/shared/time.d.ts +19 -0
  125. package/plugins/compute/cloud/shared/time.js +31 -0
  126. package/plugins/compute/cloud/views/web/cloud.d.ts +72 -0
  127. package/plugins/compute/cloud/views/web/cloud.js +112 -0
  128. package/plugins/compute/cloud/views/web/computer.d.ts +31 -0
  129. package/plugins/compute/cloud/views/web/computer.js +185 -0
  130. package/plugins/compute/cloud/views/web/new.d.ts +25 -0
  131. package/plugins/compute/cloud/views/web/new.js +89 -0
  132. package/plugins/compute/cloud/views/web/style.d.ts +6 -0
  133. package/plugins/compute/cloud/views/web/style.js +84 -0
  134. package/plugins/compute/cloud/web.d.ts +19 -0
  135. package/plugins/compute/cloud/web.js +54 -0
  136. package/plugins/compute/computers/actions/allowed.d.ts +1 -0
  137. package/plugins/compute/computers/actions/allowed.js +46 -0
  138. package/plugins/compute/computers/actions/enrollment.d.ts +1 -0
  139. package/plugins/compute/computers/actions/enrollment.js +49 -0
  140. package/plugins/compute/computers/actions/preview.d.ts +7 -0
  141. package/plugins/compute/computers/actions/preview.js +157 -0
  142. package/plugins/compute/computers/actions/provision.d.ts +1 -0
  143. package/plugins/compute/computers/actions/provision.js +49 -0
  144. package/plugins/compute/computers/actions/running.d.ts +1 -0
  145. package/plugins/compute/computers/actions/running.js +33 -0
  146. package/plugins/compute/computers/actions/scope.d.ts +1 -0
  147. package/plugins/compute/computers/actions/scope.js +41 -0
  148. package/plugins/compute/computers/actions/view.d.ts +1 -0
  149. package/plugins/compute/computers/actions/view.js +29 -0
  150. package/plugins/compute/computers/actions/work.js +27 -2
  151. package/plugins/compute/computers/collections/computer.d.ts +23 -0
  152. package/plugins/compute/computers/collections/computer.js +6 -0
  153. package/plugins/compute/computers/collections/desk.d.ts +15 -0
  154. package/plugins/compute/computers/collections/desk.js +2 -0
  155. package/plugins/compute/computers/collections/preview.d.ts +109 -0
  156. package/plugins/compute/computers/collections/preview.js +52 -0
  157. package/plugins/compute/computers/contract.d.ts +7 -0
  158. package/plugins/compute/computers/contract.js +7 -0
  159. package/plugins/compute/computers/mod.d.ts +6 -1
  160. package/plugins/compute/computers/mod.js +29 -4
  161. package/plugins/compute/computers/program/version.d.ts +1 -1
  162. package/plugins/compute/computers/program/version.js +1 -1
  163. package/plugins/compute/computers/resources/downloads.js +1 -1
  164. package/plugins/compute/computers/resources/previews.d.ts +48 -0
  165. package/plugins/compute/computers/resources/previews.js +386 -0
  166. package/plugins/compute/computers/resources/tools.js +42 -2
  167. package/plugins/compute/computers/shared/ask.d.ts +3 -1
  168. package/plugins/compute/computers/shared/ask.js +8 -2
  169. package/plugins/compute/computers/shared/desks.d.ts +1 -1
  170. package/plugins/compute/computers/shared/desks.js +1 -0
  171. package/plugins/compute/computers/shared/settings.d.ts +25 -4
  172. package/plugins/compute/computers/shared/settings.js +9 -0
  173. package/plugins/compute/computers/shared/wake.d.ts +41 -0
  174. package/plugins/compute/computers/shared/wake.js +61 -0
  175. package/plugins/compute/computers/shared/workloads.d.ts +46 -0
  176. package/plugins/compute/computers/shared/workloads.js +4 -0
  177. package/plugins/compute/computers/views/context.js +8 -1
  178. package/plugins/compute/computers/views/web/add.d.ts +41 -0
  179. package/plugins/compute/computers/views/web/add.js +58 -0
  180. package/plugins/compute/computers/views/web/asked.d.ts +19 -5
  181. package/plugins/compute/computers/views/web/asked.js +40 -9
  182. package/plugins/compute/computers/views/web/asks.js +19 -3
  183. package/plugins/compute/computers/views/web/computer.d.ts +4 -1
  184. package/plugins/compute/computers/views/web/computer.js +13 -2
  185. package/plugins/compute/computers/views/web/connect.d.ts +18 -3
  186. package/plugins/compute/computers/views/web/connect.js +52 -35
  187. package/plugins/compute/computers/views/web/desks.d.ts +16 -8
  188. package/plugins/compute/computers/views/web/desks.js +54 -31
  189. package/plugins/compute/computers/views/web/jobs.d.ts +15 -6
  190. package/plugins/compute/computers/views/web/jobs.js +40 -20
  191. package/plugins/compute/computers/views/web/kinds.d.ts +80 -0
  192. package/plugins/compute/computers/views/web/kinds.js +27 -0
  193. package/plugins/compute/computers/views/web/mark.d.ts +7 -2
  194. package/plugins/compute/computers/views/web/mark.js +2 -2
  195. package/plugins/compute/computers/views/web/page.d.ts +12 -2
  196. package/plugins/compute/computers/views/web/page.js +48 -26
  197. package/plugins/compute/computers/views/web/previews.d.ts +49 -0
  198. package/plugins/compute/computers/views/web/previews.js +47 -0
  199. package/plugins/compute/computers/views/web/serve.d.ts +45 -0
  200. package/plugins/compute/computers/views/web/serve.js +204 -0
  201. package/plugins/compute/computers/views/web/space-tab.d.ts +3 -2
  202. package/plugins/compute/computers/views/web/space-tab.js +18 -7
  203. package/plugins/compute/computers/views/web/style.d.ts +1 -1
  204. package/plugins/compute/computers/views/web/style.js +87 -2
  205. package/plugins/compute/computers/views/web/workspace.d.ts +13 -2
  206. package/plugins/compute/computers/views/web/workspace.js +48 -6
  207. package/plugins/compute/computers/web.d.ts +11 -1
  208. package/plugins/compute/computers/web.js +11 -1
  209. package/plugins/compute/mod.d.ts +8 -1
  210. package/plugins/compute/mod.js +8 -1
  211. package/plugins/compute/web.d.ts +13 -0
  212. package/plugins/compute/web.js +13 -0
  213. package/runtime/owner.d.ts +13 -1
  214. package/runtime/owner.js +27 -0
package/CHANGELOG.md CHANGED
@@ -1,3 +1,69 @@
1
+ ## 1.0.0-rc.5 — 2026-10-30
2
+
3
+ - **Cloud computers** (plugin `cloud`, see design/cloud-computers.md): computers the app hosts for
4
+ its people. A cloud computer is a disk; a VM from a pool serves it for one wake, then goes.
5
+ `cloud.create` `{ size, in?, name? }` makes one (an org's owners and admins, or its members when
6
+ the settings say, under the org's monthly `budgetUsd`); work for one that's asleep wakes it (a few
7
+ seconds; about 30 from cold); unused for `warmMinutes` (10) it's released, and after `coldDays`
8
+ (7) it goes cold, kept as a snapshot. A person with it open (`computer.view`, every few minutes
9
+ from its panel) keeps it awake up to `viewMinutes` (120). Its agents ask their person nothing
10
+ first (the `cloud` settings' `approve`, `[]` by default, in place of the computers plugin's;
11
+ publishing an app is always asked). `cloud.destroy`, `cloud.org`, queries `cloudComputer.mine` and
12
+ `cloudComputer.sizes` (each size priced an hour). Hosts are adapters: Compute Engine (`gceHost`:
13
+ managed instance groups as pools, pd-balanced disks, VMs admitted by their Google identity tokens)
14
+ and a local one for development and tests. Metered under `computer` as `cloud/<size>` (VM
15
+ seconds), `cloud/disk` and `cloud/snapshot` (GB-hours).
16
+ - **Cloud pools** (plugin `cloudPool`): the pools' VMs, where they connect (the `pool` worker kind),
17
+ and the Actions `cloud` reaches them through: `pool.take`, `pool.serve`, `pool.stop`, `pool.drop`,
18
+ `pool.renew`, and `pool.tick` each minute (pools sized by the hour). Each VM given is leased:
19
+ renewed each minute while its computer uses it, deleted once nobody does. A VM serving a computer
20
+ connects again as itself when its socket drops, until it's told to stop; then, or gone, never
21
+ again. One runtime has both plugins; an app of many orgs runs `cloudPool` once (its directory's
22
+ runtime, needing nothing else) and gives each org's runtime `remotePool(call, org)`, the same
23
+ Actions forwarded there, each org touching only its own VMs.
24
+ - **Computer program:** `computer pool --app <url>`, a free VM of an app's pool: it waits to serve a
25
+ computer, mounts its disk, and runs it, connected at the address it's told (its org's). Given the
26
+ app's socket (`wss://…`), its identity is still for the app's https origin. scripts/cloud has a
27
+ Google Cloud project's setup and the pool VMs' startup script.
28
+ - **Previews:** what a job serves, at `https://<computer>-preview.<domain>`, carried to the computer
29
+ over its own connection (no inbound network), streamed both ways (`computer.http`). For a space's
30
+ members (a signed cookie, given by the app) or anyone with the link. `computer.preview`,
31
+ `computer.previews`, `computer.unpreview`, and the agents' `preview` tool. Settings
32
+ `computers.previews` `{ domain, key }`. WebSockets aren't carried yet.
33
+ - **Apps** (plugin `apps`): what a job serves, published to run on its own at
34
+ `https://<label>.copilotz.app`. The space's folder is archived by its computer
35
+ (`computer.archive`), built (its Dockerfile, else buildpacks) and deployed as a service that
36
+ scales to zero. `app.publish` (and the agents' `publish` tool, always asked), `app.list`,
37
+ `app.redeploy`, `app.remove`; builds and running metered under `app`. Hosts: Cloud Run
38
+ (`cloudRun`) and a local one (`localApps`).
39
+ - **Apps' safety:** services and images are named `cz-app-<name>` (an app never replaces another
40
+ service in its project); builds run as `cz-build@<project>` (`buildServiceAccount`), never the
41
+ project's default service account, as they run the app's own Dockerfile. `app.publish`'s `port`
42
+ wins over its job's.
43
+ - **Previews' safety:** a preview's `Set-Cookie` loses its `Domain` (no preview sets cookies for the
44
+ domain's other hosts); a request with several `cz_preview` cookies gets in when any is its host's.
45
+ - **Jobs** can say their `port` (a preview's) and be kept (`keep`): a kept job runs again when its
46
+ computer wakes.
47
+ - **A person's panel** (`computer.work`): `wake` wakes a computer that sleeps (its page opened);
48
+ what only reads (`jobs.list`, `jobs.logs`, `files.list`, `files.read`, `search`) neither wakes it
49
+ nor keeps it awake, so a page left open lets it sleep (`computer.view` keeps it awake while
50
+ viewed, up to the limit). A computer the app makes says what its agents ask about
51
+ (`computer.provision`'s `approve`, kept on it and its desks).
52
+ - **On the web** (`copilotz/plugins/compute/web`: `computersWeb`, `cloudWeb`, `appsWeb`):
53
+ - adding a computer is one choice: a cloud computer (its sizes, priced an hour), your own machine,
54
+ or one of yours;
55
+ - a cloud computer shows its state, how long it's been awake and about what that came to; asleep,
56
+ waking it, with honest progress; deleting it, asked first;
57
+ - a job serving a port can be previewed (members, or anyone with the link), opened, copied and
58
+ published as an app (live as it builds), and a space has an Apps tab;
59
+ - jobs can be kept running.
60
+ - **Web host:** routes by host (`{ host }` beside `{ path }`); a trusted front (`front`: a secret
61
+ header and the host it forwards) for apps behind a proxy; orgs claim hosts in the directory
62
+ (`orgHost`), resolved at `/__copilotz/hosts/resolve`.
63
+ - **Runtime:** `runtime.workers.dispatch(kind, id, workload, body?)`, for host code (a route)
64
+ streaming work to a connected worker.
65
+ - **Usage:** `usage.quote` prices quantities as a use would be, without recording one.
66
+
1
67
  ## 1.0.0-rc.4 — 2026-10-30
2
68
 
3
69
  - **Usage: every use has a price.** It's the cost (a margin of 0) unless the app's price book says
package/README.md CHANGED
@@ -22,7 +22,7 @@ bun add copilotz # Bun
22
22
  deno add jsr:@copilotz/copilotz # Deno (or `deno add npm:copilotz`)
23
23
  ```
24
24
 
25
- On Deno, name it `copilotz` in `deno.json` (`"copilotz": "jsr:@copilotz/copilotz@^1.0.0-rc.4"`), and
25
+ On Deno, name it `copilotz` in `deno.json` (`"copilotz": "jsr:@copilotz/copilotz@^1.0.0-rc.5"`), and
26
26
  the imports below are the same everywhere. Some pieces need a package of their own, installed beside
27
27
  it: `esbuild` to bundle the web host on Node, `@electric-sql/pglite` for PostgreSQL in the process,
28
28
  and `@types/react` for the front ends' types.
@@ -204,7 +204,7 @@ and an app serves it from there by default:
204
204
  import { computerDownloads } from "copilotz/plugins/compute/downloads";
205
205
 
206
206
  plugins: [...plugins, await computerDownloads()], // this Copilotz's release
207
- plugins: [...plugins, await computerDownloads({ dir: "dist/computer/1.0.0-rc.4" })], // its own: /computer/download/
207
+ plugins: [...plugins, await computerDownloads({ dir: "dist/computer/1.0.0-rc.5" })], // its own: /computer/download/
208
208
  plugins: [...plugins, await computerDownloads({ url: "https://downloads.example.com/computer/" })], // or elsewhere
209
209
  ```
210
210
 
@@ -27,6 +27,20 @@
27
27
  * another schema); `GET …/<id>/export` (its rows, as SQL); `POST
28
28
  * …/<id>/delete` `{ confirm: <its name> }`. The app does the rows'
29
29
  * part (`operations`). Staff enter any org with `/orgs/enter`.
30
+ * - Hosts orgs claimed (`claimHost` in an org's `directory` resource: a
31
+ * preview's `<id>-preview.copilotz.app`, an app's): with the app's own
32
+ * hosts named (`hosts`), a request to any other host is the org's that
33
+ * claimed it, answered by its runtime's routes by host alone (never its
34
+ * page or socket); a host nobody claimed is a 404. Which org a host is
35
+ * kept a minute, forgotten here as soon as an org claims or releases it.
36
+ * - `GET /__copilotz/hosts/resolve?host=<h>`: whose a host is, `{ org,
37
+ * target? }` (or 404), for the app's front only (it carries `front`'s
38
+ * secret): the front sends a host with a target (an app's service)
39
+ * straight there.
40
+ *
41
+ * Behind a front (a Cloudflare worker), `front: { secret }`: a request
42
+ * carrying the secret (`x-copilotz-front`) is for the host it forwards
43
+ * (`x-forwarded-host`); any other's forwarded headers are ignored.
30
44
  *
31
45
  * The app says how to open an org: its store (at the org's placement, as
32
46
  * its tenant), its plugins with the host's `here` (the org's name, and how
@@ -43,7 +57,13 @@ export type OrgInfo = {
43
57
  schema: string;
44
58
  };
45
59
  };
46
- export type OrgsHostOptions = Pick<WebServerOptions, "title" | "theme" | "google" | "dev" | "assets"> & {
60
+ export type OrgsHostOptions = Pick<WebServerOptions, "title" | "theme" | "google" | "dev" | "assets" | "front"> & {
61
+ /**
62
+ * The app's own hosts (`compass.copilotz.com`; patterns as routes' are,
63
+ * `*.run.app`): a request to any other is for a host an org claimed.
64
+ * Unset, every host is the app's, and claims route nothing.
65
+ */
66
+ hosts?: string[];
47
67
  /** The directory's runtime: identity, a sign-in method, and the directory plugin. */
48
68
  directory: Runtime;
49
69
  /**
package/hosts/orgs/mod.js CHANGED
@@ -1,6 +1,11 @@
1
1
  import { definePlugin } from "../../runtime/mod.js";
2
2
  import { createTickets } from "../web/tickets.js";
3
3
  import { cookieOf, setCookie, socket, webServer, } from "../web/server.js";
4
+ import { hostMatches, hostOf, hostServer, vouched } from "../web/routes.js";
5
+ /** How long which org a host is stays known here (claims and releases here forget it at once). */
6
+ const CLAIMS_MS = 60_000;
7
+ /** Where the front asks whose a host is. */
8
+ const RESOLVE = "/__copilotz/hosts/resolve";
4
9
  const DIRECTORY = "dir";
5
10
  const CURRENT = "org";
6
11
  const MONTH = 30 * 86_400;
@@ -38,7 +43,22 @@ export function orgsHost(options) {
38
43
  const cells = new Map();
39
44
  /** The cells open now, for what must find one at once (a socket upgrade on Node). */
40
45
  const live = new Map();
41
- /** What an org reaches of the directory: invitations, and who leaves. */
46
+ /** Whether a host is the app's own (every host is, when the app names none). */
47
+ const own = (host) => !options.hosts || options.hosts.some((pattern) => hostMatches(pattern, host));
48
+ /** Which org each host is, as the directory says, kept a minute: every request asks. */
49
+ const claims = new Map();
50
+ const claimOf = (host) => {
51
+ const kept = claims.get(host);
52
+ if (kept && kept.until > Date.now())
53
+ return kept.claim;
54
+ const claim = directory.query("orgHost.resolve", { host });
55
+ claims.set(host, { claim, until: Date.now() + CLAIMS_MS });
56
+ claim.catch(() => claims.delete(host));
57
+ return claim;
58
+ };
59
+ /** A host as claims keep it: lowercase, no trailing dot. */
60
+ const plainHost = (host) => String(host ?? "").trim().toLowerCase().replace(/\.$/, "");
61
+ /** What an org reaches of the directory: invitations, who leaves, and the hosts it claims. */
42
62
  const here = (org) => definePlugin({
43
63
  name: "org-here",
44
64
  resources: {
@@ -51,6 +71,29 @@ export function orgsHost(options) {
51
71
  rename: (name) => directory.call("org.rename", { org: org.id, name }).then(() => {
52
72
  known.delete(org.id);
53
73
  }),
74
+ /**
75
+ * Makes a host the org's: its requests are its routes' by host (a
76
+ * preview's), or, with a `target`, the front sends them there (an
77
+ * app's service). Refused for the app's own hosts, and another org's.
78
+ */
79
+ claimHost: async (host, claim = {}) => {
80
+ const name = plainHost(host);
81
+ if (options.hosts && own(name)) {
82
+ throw Object.assign(new Error(`${name} is the app's own`), { code: "forbidden" });
83
+ }
84
+ await directory.call("host.claim", {
85
+ org: org.id,
86
+ host: name,
87
+ ...(claim.target ? { target: claim.target } : {}),
88
+ });
89
+ claims.delete(name);
90
+ },
91
+ /** The host is no longer the org's. */
92
+ releaseHost: async (host) => {
93
+ const name = plainHost(host);
94
+ await directory.call("host.release", { org: org.id, host: name });
95
+ claims.delete(name);
96
+ },
54
97
  },
55
98
  },
56
99
  });
@@ -75,12 +118,19 @@ export function orgsHost(options) {
75
118
  ...(options.google ? { google: options.google } : {}),
76
119
  ...(options.dev ? { dev: options.dev } : {}),
77
120
  ...(options.assets ? { assets: options.assets } : {}),
121
+ ...(options.front ? { front: options.front } : {}),
78
122
  cookie: sessionOf(org.id),
79
123
  signIn: false,
80
124
  base: baseOf(org.id),
81
125
  socket: socketOf(org.id),
82
126
  });
83
- const made = { org, runtime, tickets, serve, last: Date.now() };
127
+ const hosted = hostServer(runtime, async (request) => {
128
+ const secret = cookieOf(request, sessionOf(org.id));
129
+ return secret
130
+ ? await runtime.query("credential.actor", { secret })
131
+ : null;
132
+ }, options.front ? { front: options.front } : {});
133
+ const made = { org, runtime, tickets, serve, hosted, last: Date.now() };
84
134
  live.set(org.id, made);
85
135
  return made;
86
136
  })();
@@ -291,10 +341,36 @@ export function orgsHost(options) {
291
341
  }
292
342
  return json({ error: "Not found" }, { status: 404 });
293
343
  };
344
+ /** Whose a host is, for the front alone: `{ org, target? }`, or 404. */
345
+ const resolve = async (request, url) => {
346
+ if (!vouched(request, options.front)) {
347
+ return json({ error: "The front's only" }, { status: 403 });
348
+ }
349
+ const claim = await claimOf(plainHost(url.searchParams.get("host") ?? ""));
350
+ return claim ? json(claim) : json({ error: "Nobody's host" }, { status: 404 });
351
+ };
352
+ /** A request to a host an org claimed: its routes by host answer it, or nothing does. */
353
+ const claimed = async (request, host) => {
354
+ const claim = await claimOf(host);
355
+ const org = claim ? await orgInfo(claim.org) : null;
356
+ if (!org)
357
+ return json({ error: "Not found" }, { status: 404 });
358
+ if (org.suspended)
359
+ return json({ error: "This org is suspended." }, { status: 403 });
360
+ const cell = await cellOf(org);
361
+ cell.last = Date.now();
362
+ return await (cell.hosted(request) ?? json({ error: "Not found" }, { status: 404 }));
363
+ };
294
364
  const serve = async (request) => {
295
365
  const url = new URL(request.url);
296
366
  const route = `${request.method} ${url.pathname}`;
297
367
  try {
368
+ if (request.method === "GET" && url.pathname === RESOLVE)
369
+ return await resolve(request, url);
370
+ // Another host than the app's: an org's that claimed it, and nothing of the app's.
371
+ const host = hostOf(request, options.front);
372
+ if (!own(host))
373
+ return await claimed(request, host);
298
374
  const at = addressed(url.pathname);
299
375
  if (at) {
300
376
  const org = await orgInfo(at.org);
@@ -489,6 +565,9 @@ export function orgsHost(options) {
489
565
  * before its socket connects; one not open yet is a retry for the client.
490
566
  */
491
567
  openRuntime(request) {
568
+ // A host an org claimed has no socket of the app's.
569
+ if (!own(hostOf(request, options.front)))
570
+ return undefined;
492
571
  const named = addressed(new URL(request.url).pathname)?.org;
493
572
  const org = named ?? cookieOf(request, CURRENT);
494
573
  const cell = org ? live.get(org) : undefined;
@@ -1,4 +1,5 @@
1
1
  import { serve as oxian } from "oxianjs/adapters/node";
2
+ import { hostRouted } from "../routes.js";
2
3
  import { socket, webServer } from "../server.js";
3
4
  /**
4
5
  * Serves the web host for a runtime that declares the `socket` transport
@@ -7,10 +8,12 @@ import { socket, webServer } from "../server.js";
7
8
  export async function serve(options) {
8
9
  const { hostname, port, signal, ...web } = options;
9
10
  const hypervisor = web.runtime.hypervisor;
11
+ const routed = hostRouted(web.runtime, web.front ? { front: web.front } : {});
10
12
  return await listen({
11
- // Every request for the socket goes to the Hypervisor, so `webServer` never upgrades one.
13
+ // Every request for the socket goes to the Hypervisor, so `webServer` never upgrades one;
14
+ // a host a route takes (a preview's) has no socket of the app's, though.
12
15
  fetch: webServer({ ...web, upgrade: () => new Response(null, { status: 426 }) }),
13
- hypervisor: () => hypervisor,
16
+ hypervisor: (request) => routed(request) ? undefined : hypervisor,
14
17
  hostname,
15
18
  port,
16
19
  signal,
@@ -10,28 +10,67 @@
10
10
  * the host's process: files as they are (downloads), or an API that must
11
11
  * stream, choose call ids or keep a request's secrets out of the log. It's
12
12
  * given the runtime and who the request's session signs in (`Served`).
13
+ * - `{ host, serve }` (or `action`): every request to a host, whatever its
14
+ * path, and never the app's page, code or socket there: what a computer
15
+ * serves (a preview), an app of the app's own. `host` is a pattern: a host
16
+ * exactly (`docs.example.com`), or `*<suffix>`, any first label ending so
17
+ * (`*-preview.copilotz.app` takes `abc123-preview.copilotz.app`, not
18
+ * `a.b-preview.copilotz.app`). It may be a function of the app's
19
+ * resources, for a domain its settings name, answering nothing for no
20
+ * route. A port in a pattern must match too; without one, any port does.
13
21
  *
14
22
  * `methods` limits which methods it takes (all by default). Paths the host
15
23
  * serves itself can't be taken.
16
24
  *
25
+ * The host a request is for is its own (`Host`), or, from the app's front
26
+ * (a Cloudflare worker before it, with `front: { secret }`), the one the
27
+ * front says (`x-forwarded-host`), when it carries the front's secret
28
+ * (`x-copilotz-front`). Anyone else's forwarded headers are ignored.
29
+ *
17
30
  * ```ts
18
31
  * resources: { routes: { "whatsapp.hook": { path: "/hooks/whatsapp", action: "whatsapp.hook" } } }
19
32
  * ```
20
33
  */
21
- import type { Actor, Infer, Runtime } from "../../runtime/mod.js";
22
- export type Route = {
34
+ import type { Actor, Infer, Resources, Runtime } from "../../runtime/mod.js";
35
+ export type Route = ({
23
36
  path: string;
37
+ } | {
38
+ host: string | ((resources: Resources) => string | undefined);
39
+ }) & {
24
40
  methods?: string[];
25
41
  } & ({
26
42
  action: string;
27
43
  } | {
28
44
  serve(request: Request, host: Served): Response | Promise<Response>;
29
45
  });
30
- /** What a `serve` route is given: the runtime, and who the request's session signs in. */
46
+ /**
47
+ * What a `serve` route is given: the runtime, who the request's session
48
+ * signs in, and the host the request is for (as the front says, when it's
49
+ * trusted: not always `request.url`'s).
50
+ */
31
51
  export type Served = {
32
52
  runtime: Runtime;
33
53
  whom(): Promise<Actor | null>;
54
+ host: string;
34
55
  };
56
+ /** The app's front (a Cloudflare worker before it): the secret it vouches for requests with. */
57
+ export type Front = {
58
+ secret: string;
59
+ };
60
+ /** The header the front puts its secret in. */
61
+ export declare const FRONT = "x-copilotz-front";
62
+ /** Whether the front vouches for a request: it carries the front's secret. */
63
+ export declare const vouched: (request: Request, front?: Front) => boolean;
64
+ /**
65
+ * The host a request is for, lowercased: the front's word for it
66
+ * (`x-forwarded-host`) when the front vouches for the request, else its own.
67
+ */
68
+ export declare function hostOf(request: Request, front?: Front): string;
69
+ /**
70
+ * Whether a host matches a pattern: exactly, or `*<suffix>` for any first
71
+ * label ending so. Without a port in the pattern, the host's is ignored.
72
+ */
73
+ export declare function hostMatches(pattern: string, host: string): boolean;
35
74
  /**
36
75
  * What an Action a route calls is given, the request read: its schema, for
37
76
  * the Action's `input`. The path asked for, and its query's parameters (the
@@ -82,8 +121,26 @@ export type HttpAnswer = {
82
121
  };
83
122
  /** Who calls when nobody's signed in. */
84
123
  export declare const VISITOR: Actor;
124
+ /**
125
+ * Whether a route by host takes a request: for what must decide at once,
126
+ * before anything asynchronous (Node's socket upgrade).
127
+ */
128
+ export declare function hostRouted(runtime: Runtime, options?: {
129
+ front?: Front;
130
+ }): (request: Request) => boolean;
131
+ /**
132
+ * Answers the requests a route by host takes (all of a host's), and only
133
+ * those; undefined for any other. For a host serving many orgs, a host an
134
+ * org claimed is its routes' alone.
135
+ */
136
+ export declare function hostServer(runtime: Runtime, whom: (request: Request) => Promise<Actor | null>, options?: {
137
+ front?: Front;
138
+ }): (request: Request) => Promise<Response> | undefined;
85
139
  /**
86
140
  * Answers the requests a plugin's route takes; undefined for any other. `whom`
87
- * says who the request's session signs in, if anyone.
141
+ * says who the request's session signs in, if anyone. A request to a host a
142
+ * route takes is that route's, whatever its path; any other goes by path.
88
143
  */
89
- export declare function routeServer(runtime: Runtime, whom: (request: Request) => Promise<Actor | null>): (request: Request) => Promise<Response> | undefined;
144
+ export declare function routeServer(runtime: Runtime, whom: (request: Request) => Promise<Actor | null>, options?: {
145
+ front?: Front;
146
+ }): (request: Request) => Promise<Response> | undefined;
@@ -1,3 +1,41 @@
1
+ /** The header the front puts its secret in. */
2
+ export const FRONT = "x-copilotz-front";
3
+ /** Whether two strings are the same, taking as long whatever their first difference. */
4
+ const same = (a, b) => {
5
+ let differ = a.length ^ b.length;
6
+ for (let i = 0; i < Math.max(a.length, b.length); i++) {
7
+ differ |= (a.charCodeAt(i) || 0) ^ (b.charCodeAt(i) || 0);
8
+ }
9
+ return differ === 0;
10
+ };
11
+ /** Whether the front vouches for a request: it carries the front's secret. */
12
+ export const vouched = (request, front) => {
13
+ const said = request.headers.get(FRONT);
14
+ return !!front?.secret && said !== null && same(said, front.secret);
15
+ };
16
+ /**
17
+ * The host a request is for, lowercased: the front's word for it
18
+ * (`x-forwarded-host`) when the front vouches for the request, else its own.
19
+ */
20
+ export function hostOf(request, front) {
21
+ const forwarded = vouched(request, front)
22
+ ? request.headers.get("x-forwarded-host")?.split(",")[0].trim()
23
+ : undefined;
24
+ return (forwarded || new URL(request.url).host).toLowerCase();
25
+ }
26
+ /**
27
+ * Whether a host matches a pattern: exactly, or `*<suffix>` for any first
28
+ * label ending so. Without a port in the pattern, the host's is ignored.
29
+ */
30
+ export function hostMatches(pattern, host) {
31
+ const wanted = pattern.toLowerCase();
32
+ const asked = /:\d+$/.test(wanted) ? host : host.replace(/:\d+$/, "");
33
+ if (!wanted.startsWith("*"))
34
+ return asked === wanted;
35
+ const suffix = wanted.slice(1);
36
+ const first = asked.slice(0, asked.length - suffix.length);
37
+ return asked.endsWith(suffix) && first.length > 0 && !first.includes(".");
38
+ }
1
39
  /**
2
40
  * What an Action a route calls is given, the request read: its schema, for
3
41
  * the Action's `input`. The path asked for, and its query's parameters (the
@@ -36,7 +74,8 @@ const plain = (status, text) => new Response(text, { status, headers: { "content
36
74
  async function read(request, url) {
37
75
  const headers = {};
38
76
  request.headers.forEach((value, name) => {
39
- if (name !== "cookie")
77
+ // The front's secret is the host's, never a plugin's to see (or log).
78
+ if (name !== "cookie" && name !== FRONT)
40
79
  headers[name] = value;
41
80
  });
42
81
  const asked = {
@@ -79,54 +118,116 @@ function answer(value) {
79
118
  },
80
119
  });
81
120
  }
82
- /**
83
- * Answers the requests a plugin's route takes; undefined for any other. `whom`
84
- * says who the request's session signs in, if anyone.
85
- */
86
- export function routeServer(runtime, whom) {
87
- const routes = Object.entries((runtime.describe().resources.routes ?? {})).filter((entry) => !!entry[1]);
121
+ /** A runtime's routes, checked: those by path, the longest first, and those by host. */
122
+ function declared(runtime) {
123
+ const resources = runtime.describe().resources;
124
+ const routes = Object.entries((resources.routes ?? {}))
125
+ .filter((entry) => !!entry[1]);
126
+ const paths = [];
127
+ const hosts = [];
88
128
  for (const [name, route] of routes) {
129
+ if ("host" in route) {
130
+ const pattern = typeof route.host === "function" ? route.host(resources) : route.host;
131
+ if (pattern === undefined)
132
+ continue;
133
+ if (!/^\*?[a-z0-9.-]+(:\d+)?$/i.test(pattern)) {
134
+ throw new Error(`routes.${name}: "${pattern}" isn't a host, or *<suffix>`);
135
+ }
136
+ hosts.push({ name, route, pattern });
137
+ continue;
138
+ }
89
139
  if (!route.path.startsWith("/")) {
90
140
  throw new Error(`routes.${name}: its path must start with "/"`);
91
141
  }
92
142
  if (OWN.some((own) => route.path.startsWith(own) || own.startsWith(route.path))) {
93
143
  throw new Error(`routes.${name}: "${route.path}" is the web host's own`);
94
144
  }
145
+ paths.push({ name, route });
146
+ }
147
+ // The longest path first, so a more specific route wins; an exact host before a pattern.
148
+ paths.sort((a, b) => b.route.path.length - a.route.path.length);
149
+ hosts.sort((a, b) => Number(a.pattern.startsWith("*")) - Number(b.pattern.startsWith("*")) ||
150
+ b.pattern.length - a.pattern.length);
151
+ return { paths, hosts };
152
+ }
153
+ /** Answers a request as a route does: its `serve`, or a call to its Action. */
154
+ async function answered(runtime, name, route, request, url, served) {
155
+ if ("serve" in route)
156
+ return await route.serve(request, served);
157
+ const asked = await read(request, url);
158
+ if (asked instanceof Response)
159
+ return asked;
160
+ const actor = (await served.whom().catch(() => null)) ?? VISITOR;
161
+ try {
162
+ return answer(await runtime.call(route.action, asked, { actor }));
163
+ }
164
+ catch (error) {
165
+ const code = error.code;
166
+ if (code && STATUS[code]) {
167
+ return plain(STATUS[code], error instanceof Error ? error.message : String(error));
168
+ }
169
+ console.error(`routes.${name}:`, error instanceof Error ? error.message : error);
170
+ return plain(500, "Something went wrong");
95
171
  }
96
- // The longest path first, so a more specific route wins.
97
- routes.sort(([, a], [, b]) => b.path.length - a.path.length);
98
- const match = (url, method) => routes.find(([, route]) => (route.path.endsWith("/")
172
+ }
173
+ /**
174
+ * Whether a route by host takes a request: for what must decide at once,
175
+ * before anything asynchronous (Node's socket upgrade).
176
+ */
177
+ export function hostRouted(runtime, options = {}) {
178
+ const { hosts } = declared(runtime);
179
+ return (request) => {
180
+ if (!hosts.length)
181
+ return false;
182
+ const host = hostOf(request, options.front);
183
+ return hosts.some((h) => hostMatches(h.pattern, host));
184
+ };
185
+ }
186
+ /**
187
+ * Answers the requests a route by host takes (all of a host's), and only
188
+ * those; undefined for any other. For a host serving many orgs, a host an
189
+ * org claimed is its routes' alone.
190
+ */
191
+ export function hostServer(runtime, whom, options = {}) {
192
+ const { hosts } = declared(runtime);
193
+ return (request) => {
194
+ if (!hosts.length)
195
+ return undefined;
196
+ const host = hostOf(request, options.front);
197
+ const found = hosts.find((h) => hostMatches(h.pattern, host) && (!h.route.methods || h.route.methods.includes(request.method)));
198
+ if (!found)
199
+ return undefined;
200
+ return answered(runtime, found.name, found.route, request, new URL(request.url), {
201
+ runtime,
202
+ whom: () => whom(request),
203
+ host,
204
+ });
205
+ };
206
+ }
207
+ /**
208
+ * Answers the requests a plugin's route takes; undefined for any other. `whom`
209
+ * says who the request's session signs in, if anyone. A request to a host a
210
+ * route takes is that route's, whatever its path; any other goes by path.
211
+ */
212
+ export function routeServer(runtime, whom, options = {}) {
213
+ const { paths } = declared(runtime);
214
+ const hosted = hostServer(runtime, whom, options);
215
+ const match = (url, method) => paths.find(({ route }) => (route.path.endsWith("/")
99
216
  ? url.pathname.startsWith(route.path)
100
217
  : url.pathname === route.path) &&
101
218
  (!route.methods || route.methods.includes(method)));
102
219
  return (request) => {
220
+ const byHost = hosted(request);
221
+ if (byHost)
222
+ return byHost;
103
223
  const url = new URL(request.url);
104
224
  const found = match(url, request.method);
105
225
  if (!found)
106
226
  return undefined;
107
- const [name, route] = found;
108
- return (async () => {
109
- if ("serve" in route) {
110
- return await route.serve(request, {
111
- runtime,
112
- whom: () => whom(request),
113
- });
114
- }
115
- const asked = await read(request, url);
116
- if (asked instanceof Response)
117
- return asked;
118
- const actor = (await whom(request).catch(() => null)) ?? VISITOR;
119
- try {
120
- return answer(await runtime.call(route.action, asked, { actor }));
121
- }
122
- catch (error) {
123
- const code = error.code;
124
- if (code && STATUS[code]) {
125
- return plain(STATUS[code], error instanceof Error ? error.message : String(error));
126
- }
127
- console.error(`routes.${name}:`, error instanceof Error ? error.message : error);
128
- return plain(500, "Something went wrong");
129
- }
130
- })();
227
+ return answered(runtime, found.name, found.route, request, url, {
228
+ runtime,
229
+ whom: () => whom(request),
230
+ host: hostOf(request, options.front),
231
+ });
131
232
  };
132
233
  }
@@ -25,7 +25,9 @@
25
25
  * (chat's `file.content`), with ranges for audio; `?download=1` saves it
26
26
  * (see `files.ts`).
27
27
  * - Plugins' own paths, their `routes` resource: a webhook, an OAuth callback,
28
- * an API, a download (see `routes.ts`).
28
+ * an API, a download (see `routes.ts`). A request to a host a route takes
29
+ * (a preview's) is that route's alone, whatever its path: never the page,
30
+ * the code or the socket.
29
31
  * - `GET /manifest.webmanifest` and `GET /sw.js`: the app, installable: its
30
32
  * manifest (name, colours, icons drawn from the theme's mark) and its
31
33
  * service worker, which keeps the code at hand and offers new versions
@@ -44,6 +46,7 @@
44
46
  import type { Runtime } from "../../runtime/mod.js";
45
47
  import { type Asset } from "./assets.js";
46
48
  import type { Theme } from "./theme/default.js";
49
+ import { type Front } from "./routes.js";
47
50
  import type { Tickets } from "./tickets.js";
48
51
  export type WebServerOptions = {
49
52
  runtime: Runtime;
@@ -95,9 +98,16 @@ export type WebServerOptions = {
95
98
  * under `/assets/copilotz/`, which is the host's own.
96
99
  */
97
100
  assets?: Record<string, Asset>;
101
+ /**
102
+ * The app's front (a Cloudflare worker before it): a request carrying its
103
+ * secret (`x-copilotz-front`) is for the host it forwards
104
+ * (`x-forwarded-host`), which routes by host go by. Without it, or
105
+ * without the secret, forwarded headers are ignored.
106
+ */
107
+ front?: Front;
98
108
  };
99
109
  export type { Asset };
100
- export { type HttpAnswer, type HttpRequest, httpRequest, type Route, type Served, VISITOR, } from "./routes.js";
110
+ export { type Front, hostMatches, hostOf, type HttpAnswer, type HttpRequest, httpRequest, type Route, type Served, VISITOR, } from "./routes.js";
101
111
  /**
102
112
  * Bundled code: its files by name (`deno bundle --code-splitting --outdir`'s),
103
113
  * the entry and the chunks it imports by relative path, and which one the
@@ -7,7 +7,7 @@ import { HOST_ASSETS, hostFiles } from "./theme/files/mod.js";
7
7
  import { routeServer } from "./routes.js";
8
8
  import { ownPath } from "./theme/own-path.js";
9
9
  import { pictureOf } from "./theme/picture.js";
10
- export { httpRequest, VISITOR, } from "./routes.js";
10
+ export { hostMatches, hostOf, httpRequest, VISITOR, } from "./routes.js";
11
11
  /** Where the code is served. */
12
12
  const CODE = "/app/";
13
13
  const COOKIE = "session";
@@ -137,7 +137,7 @@ export function webServer(options) {
137
137
  return secret ? await runtime.query("credential.actor", { secret }) : null;
138
138
  };
139
139
  const files = fileServer(runtime, whom);
140
- const routes = routeServer(runtime, whom);
140
+ const routes = routeServer(runtime, whom, options.front ? { front: options.front } : {});
141
141
  /** The service worker, for this version of the code: its version is the code's own. */
142
142
  let worker;
143
143
  const serviceWorker = () => worker ??= code().then(async (made) => workerOf({
@@ -152,7 +152,8 @@ export function webServer(options) {
152
152
  headers: { "set-cookie": setCookie(url, session, 30 * 86_400, name) },
153
153
  });
154
154
  return async (request) => {
155
- const asset = assets(request) ?? files(request) ?? routes(request);
155
+ // Routes first: a host a route takes is all its own (the host's paths are never routes').
156
+ const asset = routes(request) ?? assets(request) ?? files(request);
156
157
  if (asset)
157
158
  return await asset;
158
159
  const url = new URL(request.url);