copilotz 1.0.0-rc.4 → 1.0.0-rc.6
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.
- package/CHANGELOG.md +93 -0
- package/README.md +2 -2
- package/hosts/orgs/mod.d.ts +21 -1
- package/hosts/orgs/mod.js +81 -2
- package/hosts/web/node/serve.js +5 -2
- package/hosts/web/routes.d.ts +62 -5
- package/hosts/web/routes.js +135 -34
- package/hosts/web/server.d.ts +12 -2
- package/hosts/web/server.js +4 -3
- package/package.json +3 -3
- package/plugins/access/directory/actions/host.d.ts +2 -0
- package/plugins/access/directory/actions/host.js +55 -0
- package/plugins/access/directory/actions/org.js +2 -1
- package/plugins/access/directory/collections/org-host.d.ts +61 -0
- package/plugins/access/directory/collections/org-host.js +29 -0
- package/plugins/access/directory/mod.d.ts +3 -1
- package/plugins/access/directory/mod.js +7 -2
- package/plugins/access/spaces/actions/org.js +2 -0
- package/plugins/billing/usage/actions/quote.d.ts +1 -0
- package/plugins/billing/usage/actions/quote.js +42 -0
- package/plugins/billing/usage/mod.d.ts +2 -0
- package/plugins/billing/usage/mod.js +4 -0
- package/plugins/compute/apps/actions/build.d.ts +1 -0
- package/plugins/compute/apps/actions/build.js +93 -0
- package/plugins/compute/apps/actions/list.d.ts +1 -0
- package/plugins/compute/apps/actions/list.js +13 -0
- package/plugins/compute/apps/actions/meter.d.ts +1 -0
- package/plugins/compute/apps/actions/meter.js +43 -0
- package/plugins/compute/apps/actions/publish.d.ts +1 -0
- package/plugins/compute/apps/actions/publish.js +38 -0
- package/plugins/compute/apps/actions/redeploy.d.ts +1 -0
- package/plugins/compute/apps/actions/redeploy.js +27 -0
- package/plugins/compute/apps/actions/remove.d.ts +1 -0
- package/plugins/compute/apps/actions/remove.js +47 -0
- package/plugins/compute/apps/adapters/cloudrun.d.ts +90 -0
- package/plugins/compute/apps/adapters/cloudrun.js +341 -0
- package/plugins/compute/apps/adapters/local.d.ts +15 -0
- package/plugins/compute/apps/adapters/local.js +139 -0
- package/plugins/compute/apps/adapters/tar.d.ts +29 -0
- package/plugins/compute/apps/adapters/tar.js +178 -0
- package/plugins/compute/apps/collections/app.d.ts +136 -0
- package/plugins/compute/apps/collections/app.js +58 -0
- package/plugins/compute/apps/contract.d.ts +71 -0
- package/plugins/compute/apps/contract.js +8 -0
- package/plugins/compute/apps/mod.d.ts +42 -0
- package/plugins/compute/apps/mod.js +65 -0
- package/plugins/compute/apps/resources/tools.d.ts +10 -0
- package/plugins/compute/apps/resources/tools.js +66 -0
- package/plugins/compute/apps/shared/archive.d.ts +24 -0
- package/plugins/compute/apps/shared/archive.js +69 -0
- package/plugins/compute/apps/shared/claims.d.ts +28 -0
- package/plugins/compute/apps/shared/claims.js +18 -0
- package/plugins/compute/apps/shared/errors.d.ts +4 -0
- package/plugins/compute/apps/shared/errors.js +4 -0
- package/plugins/compute/apps/shared/host.d.ts +19 -0
- package/plugins/compute/apps/shared/host.js +19 -0
- package/plugins/compute/apps/shared/label.d.ts +21 -0
- package/plugins/compute/apps/shared/label.js +51 -0
- package/plugins/compute/apps/shared/meter.d.ts +17 -0
- package/plugins/compute/apps/shared/meter.js +29 -0
- package/plugins/compute/apps/shared/publish.d.ts +47 -0
- package/plugins/compute/apps/shared/publish.js +134 -0
- package/plugins/compute/apps/shared/settings.d.ts +49 -0
- package/plugins/compute/apps/shared/settings.js +22 -0
- package/plugins/compute/apps/views/web/apps.d.ts +54 -0
- package/plugins/compute/apps/views/web/apps.js +48 -0
- package/plugins/compute/apps/views/web/publish.d.ts +24 -0
- package/plugins/compute/apps/views/web/publish.js +158 -0
- package/plugins/compute/apps/views/web/space.d.ts +15 -0
- package/plugins/compute/apps/views/web/space.js +126 -0
- package/plugins/compute/apps/views/web/style.d.ts +6 -0
- package/plugins/compute/apps/views/web/style.js +102 -0
- package/plugins/compute/apps/web.d.ts +16 -0
- package/plugins/compute/apps/web.js +29 -0
- package/plugins/compute/cloud/actions/create.d.ts +1 -0
- package/plugins/compute/cloud/actions/create.js +78 -0
- package/plugins/compute/cloud/actions/daily.d.ts +1 -0
- package/plugins/compute/cloud/actions/daily.js +75 -0
- package/plugins/compute/cloud/actions/destroy.d.ts +1 -0
- package/plugins/compute/cloud/actions/destroy.js +71 -0
- package/plugins/compute/cloud/actions/may.d.ts +1 -0
- package/plugins/compute/cloud/actions/may.js +15 -0
- package/plugins/compute/cloud/actions/org.d.ts +1 -0
- package/plugins/compute/cloud/actions/org.js +21 -0
- package/plugins/compute/cloud/actions/pool-tick.d.ts +4 -0
- package/plugins/compute/cloud/actions/pool-tick.js +96 -0
- package/plugins/compute/cloud/actions/pool.d.ts +3 -0
- package/plugins/compute/cloud/actions/pool.js +98 -0
- package/plugins/compute/cloud/actions/release.d.ts +1 -0
- package/plugins/compute/cloud/actions/release.js +58 -0
- package/plugins/compute/cloud/actions/remote.d.ts +10 -0
- package/plugins/compute/cloud/actions/remote.js +24 -0
- package/plugins/compute/cloud/actions/tick.d.ts +1 -0
- package/plugins/compute/cloud/actions/tick.js +95 -0
- package/plugins/compute/cloud/actions/touch.d.ts +1 -0
- package/plugins/compute/cloud/actions/touch.js +32 -0
- package/plugins/compute/cloud/actions/vms.d.ts +9 -0
- package/plugins/compute/cloud/actions/vms.js +213 -0
- package/plugins/compute/cloud/actions/wake.d.ts +1 -0
- package/plugins/compute/cloud/actions/wake.js +147 -0
- package/plugins/compute/cloud/adapters/gce/index.d.ts +73 -0
- package/plugins/compute/cloud/adapters/gce/index.js +242 -0
- package/plugins/compute/cloud/adapters/local.d.ts +48 -0
- package/plugins/compute/cloud/adapters/local.js +188 -0
- package/plugins/compute/cloud/collections/cloud-computer.d.ts +109 -0
- package/plugins/compute/cloud/collections/cloud-computer.js +89 -0
- package/plugins/compute/cloud/collections/pool-vm.d.ts +79 -0
- package/plugins/compute/cloud/collections/pool-vm.js +36 -0
- package/plugins/compute/cloud/collections/pool-want.d.ts +94 -0
- package/plugins/compute/cloud/collections/pool-want.js +27 -0
- package/plugins/compute/cloud/contract.d.ts +236 -0
- package/plugins/compute/cloud/contract.js +67 -0
- package/plugins/compute/cloud/mod.d.ts +95 -0
- package/plugins/compute/cloud/mod.js +194 -0
- package/plugins/compute/cloud/processors/mark.d.ts +9 -0
- package/plugins/compute/cloud/processors/mark.js +13 -0
- package/plugins/compute/cloud/shared/google.d.ts +75 -0
- package/plugins/compute/cloud/shared/google.js +218 -0
- package/plugins/compute/cloud/shared/host.d.ts +11 -0
- package/plugins/compute/cloud/shared/host.js +29 -0
- package/plugins/compute/cloud/shared/may.d.ts +13 -0
- package/plugins/compute/cloud/shared/may.js +23 -0
- package/plugins/compute/cloud/shared/meter.d.ts +29 -0
- package/plugins/compute/cloud/shared/meter.js +59 -0
- package/plugins/compute/cloud/shared/names.d.ts +10 -0
- package/plugins/compute/cloud/shared/names.js +11 -0
- package/plugins/compute/cloud/shared/people.d.ts +24 -0
- package/plugins/compute/cloud/shared/people.js +19 -0
- package/plugins/compute/cloud/shared/records.d.ts +25 -0
- package/plugins/compute/cloud/shared/records.js +10 -0
- package/plugins/compute/cloud/shared/settings.d.ts +75 -0
- package/plugins/compute/cloud/shared/settings.js +48 -0
- package/plugins/compute/cloud/shared/time.d.ts +19 -0
- package/plugins/compute/cloud/shared/time.js +31 -0
- package/plugins/compute/cloud/views/web/cloud.d.ts +78 -0
- package/plugins/compute/cloud/views/web/cloud.js +121 -0
- package/plugins/compute/cloud/views/web/computer.d.ts +37 -0
- package/plugins/compute/cloud/views/web/computer.js +195 -0
- package/plugins/compute/cloud/views/web/new.d.ts +27 -0
- package/plugins/compute/cloud/views/web/new.js +94 -0
- package/plugins/compute/cloud/views/web/style.d.ts +6 -0
- package/plugins/compute/cloud/views/web/style.js +84 -0
- package/plugins/compute/cloud/web.d.ts +19 -0
- package/plugins/compute/cloud/web.js +60 -0
- package/plugins/compute/computers/actions/allowed.d.ts +1 -0
- package/plugins/compute/computers/actions/allowed.js +46 -0
- package/plugins/compute/computers/actions/enrollment.d.ts +1 -0
- package/plugins/compute/computers/actions/enrollment.js +49 -0
- package/plugins/compute/computers/actions/mark.d.ts +1 -0
- package/plugins/compute/computers/actions/mark.js +36 -0
- package/plugins/compute/computers/actions/preview.d.ts +4 -0
- package/plugins/compute/computers/actions/preview.js +163 -0
- package/plugins/compute/computers/actions/provision.d.ts +1 -0
- package/plugins/compute/computers/actions/provision.js +49 -0
- package/plugins/compute/computers/actions/running.d.ts +1 -0
- package/plugins/compute/computers/actions/running.js +33 -0
- package/plugins/compute/computers/actions/scope.d.ts +1 -0
- package/plugins/compute/computers/actions/scope.js +45 -0
- package/plugins/compute/computers/actions/view.d.ts +1 -0
- package/plugins/compute/computers/actions/view.js +29 -0
- package/plugins/compute/computers/actions/work.js +27 -2
- package/plugins/compute/computers/collections/computer.d.ts +69 -0
- package/plugins/compute/computers/collections/computer.js +16 -0
- package/plugins/compute/computers/collections/desk.d.ts +53 -0
- package/plugins/compute/computers/collections/desk.js +8 -0
- package/plugins/compute/computers/collections/preview.d.ts +111 -0
- package/plugins/compute/computers/collections/preview.js +58 -0
- package/plugins/compute/computers/contract.d.ts +7 -0
- package/plugins/compute/computers/contract.js +7 -0
- package/plugins/compute/computers/mod.d.ts +6 -1
- package/plugins/compute/computers/mod.js +31 -4
- package/plugins/compute/computers/program/version.d.ts +1 -1
- package/plugins/compute/computers/program/version.js +1 -1
- package/plugins/compute/computers/resources/downloads.js +1 -1
- package/plugins/compute/computers/resources/previews.d.ts +48 -0
- package/plugins/compute/computers/resources/previews.js +386 -0
- package/plugins/compute/computers/resources/tools.js +42 -2
- package/plugins/compute/computers/shared/ask.d.ts +3 -1
- package/plugins/compute/computers/shared/ask.js +8 -2
- package/plugins/compute/computers/shared/desks.d.ts +1 -1
- package/plugins/compute/computers/shared/desks.js +2 -0
- package/plugins/compute/computers/shared/preview-url.d.ts +5 -0
- package/plugins/compute/computers/shared/preview-url.js +5 -0
- package/plugins/compute/computers/shared/settings.d.ts +25 -4
- package/plugins/compute/computers/shared/settings.js +9 -0
- package/plugins/compute/computers/shared/wake.d.ts +41 -0
- package/plugins/compute/computers/shared/wake.js +61 -0
- package/plugins/compute/computers/shared/workloads.d.ts +46 -0
- package/plugins/compute/computers/shared/workloads.js +4 -0
- package/plugins/compute/computers/views/context.js +8 -1
- package/plugins/compute/computers/views/web/add.d.ts +41 -0
- package/plugins/compute/computers/views/web/add.js +60 -0
- package/plugins/compute/computers/views/web/asked.d.ts +19 -5
- package/plugins/compute/computers/views/web/asked.js +40 -9
- package/plugins/compute/computers/views/web/asks.js +19 -3
- package/plugins/compute/computers/views/web/computer.d.ts +4 -1
- package/plugins/compute/computers/views/web/computer.js +13 -2
- package/plugins/compute/computers/views/web/connect.d.ts +18 -3
- package/plugins/compute/computers/views/web/connect.js +52 -35
- package/plugins/compute/computers/views/web/desks.d.ts +16 -8
- package/plugins/compute/computers/views/web/desks.js +54 -31
- package/plugins/compute/computers/views/web/jobs.d.ts +15 -6
- package/plugins/compute/computers/views/web/jobs.js +40 -20
- package/plugins/compute/computers/views/web/kinds.d.ts +92 -0
- package/plugins/compute/computers/views/web/kinds.js +36 -0
- package/plugins/compute/computers/views/web/mark.d.ts +7 -2
- package/plugins/compute/computers/views/web/mark.js +2 -2
- package/plugins/compute/computers/views/web/page.d.ts +12 -2
- package/plugins/compute/computers/views/web/page.js +48 -26
- package/plugins/compute/computers/views/web/previews.d.ts +44 -0
- package/plugins/compute/computers/views/web/previews.js +33 -0
- package/plugins/compute/computers/views/web/serve.d.ts +45 -0
- package/plugins/compute/computers/views/web/serve.js +168 -0
- package/plugins/compute/computers/views/web/space-tab.d.ts +3 -2
- package/plugins/compute/computers/views/web/space-tab.js +27 -7
- package/plugins/compute/computers/views/web/style.d.ts +1 -1
- package/plugins/compute/computers/views/web/style.js +91 -2
- package/plugins/compute/computers/views/web/workspace.d.ts +13 -2
- package/plugins/compute/computers/views/web/workspace.js +49 -6
- package/plugins/compute/computers/web.d.ts +11 -1
- package/plugins/compute/computers/web.js +11 -1
- package/plugins/compute/mod.d.ts +8 -1
- package/plugins/compute/mod.js +8 -1
- package/plugins/compute/web.d.ts +13 -0
- package/plugins/compute/web.js +13 -0
- package/runtime/owner.d.ts +13 -1
- package/runtime/owner.js +27 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,96 @@
|
|
|
1
|
+
## 1.0.0-rc.6 — 2026-10-30
|
|
2
|
+
|
|
3
|
+
- **Computer program:** an app on another machine over plain `ws:` (an `http://` address that isn't
|
|
4
|
+
localhost) fails at once, saying to use https, instead of waiting to connect forever: Oxian
|
|
5
|
+
refuses plain `ws:` but to this machine.
|
|
6
|
+
- **Cloud pools:** a pool grown for a wake isn't shrunk while its VM boots. `pool.take` records what
|
|
7
|
+
it waits for (`poolWant`), and `pool.tick` sizes each pool to its floor, the takes of the last 2
|
|
8
|
+
minutes and the wakes still waiting. Before, the next minute's sizing set the pool back to its
|
|
9
|
+
floor, deleting the VM a wake had grown it for before it connected.
|
|
10
|
+
- **Cloud computers:** `cloudComputer.sizes` also says whether the person asking may make one now
|
|
11
|
+
(`create`, and `why` not: `admins`, `budget`), and the warm and view minutes; `cloud.may` says
|
|
12
|
+
just that. The web's "Use a cloud computer" choice shows why it can't be picked, and the copy says
|
|
13
|
+
the app's minutes.
|
|
14
|
+
- **Cloud computers shared into a space:** each computer's state is kept on it and its desks
|
|
15
|
+
(`status`: `{ kind: "cloud", state, size }`, set with the new system Action `computer.mark`, as
|
|
16
|
+
the cloud plugin sees it change), so the space's members see how anyone's is (asleep, waking,
|
|
17
|
+
awake), not "offline".
|
|
18
|
+
- **Previews:** `preview.here` records carry their address (`url`); the web no longer works it out.
|
|
19
|
+
`computer.preview` and `computer.scope` take a `desk` (one of the space's), and so does
|
|
20
|
+
`app.publish`: the web makes previews and publishes apps from the computer it's on, not the
|
|
21
|
+
space's default.
|
|
22
|
+
- **Apps:** an agent's `publish` asks its person with where the app will be ("wants to publish Shop
|
|
23
|
+
at https://shop.copilotz.app").
|
|
24
|
+
- **`org.me`** isn't recorded (it only reads), so reads (queries, Views) ask it too.
|
|
25
|
+
- **The local cloud host** is one per app and account, whichever executor asks: with several
|
|
26
|
+
executors, a VM one started was turned away by another.
|
|
27
|
+
|
|
28
|
+
## 1.0.0-rc.5 — 2026-10-30
|
|
29
|
+
|
|
30
|
+
- **Cloud computers** (plugin `cloud`, see design/cloud-computers.md): computers the app hosts for
|
|
31
|
+
its people. A cloud computer is a disk; a VM from a pool serves it for one wake, then goes.
|
|
32
|
+
`cloud.create` `{ size, in?, name? }` makes one (an org's owners and admins, or its members when
|
|
33
|
+
the settings say, under the org's monthly `budgetUsd`); work for one that's asleep wakes it (a few
|
|
34
|
+
seconds; about 30 from cold); unused for `warmMinutes` (10) it's released, and after `coldDays`
|
|
35
|
+
(7) it goes cold, kept as a snapshot. A person with it open (`computer.view`, every few minutes
|
|
36
|
+
from its panel) keeps it awake up to `viewMinutes` (120). Its agents ask their person nothing
|
|
37
|
+
first (the `cloud` settings' `approve`, `[]` by default, in place of the computers plugin's;
|
|
38
|
+
publishing an app is always asked). `cloud.destroy`, `cloud.org`, queries `cloudComputer.mine` and
|
|
39
|
+
`cloudComputer.sizes` (each size priced an hour). Hosts are adapters: Compute Engine (`gceHost`:
|
|
40
|
+
managed instance groups as pools, pd-balanced disks, VMs admitted by their Google identity tokens)
|
|
41
|
+
and a local one for development and tests. Metered under `computer` as `cloud/<size>` (VM
|
|
42
|
+
seconds), `cloud/disk` and `cloud/snapshot` (GB-hours).
|
|
43
|
+
- **Cloud pools** (plugin `cloudPool`): the pools' VMs, where they connect (the `pool` worker kind),
|
|
44
|
+
and the Actions `cloud` reaches them through: `pool.take`, `pool.serve`, `pool.stop`, `pool.drop`,
|
|
45
|
+
`pool.renew`, and `pool.tick` each minute (pools sized by the hour). Each VM given is leased:
|
|
46
|
+
renewed each minute while its computer uses it, deleted once nobody does. A VM serving a computer
|
|
47
|
+
connects again as itself when its socket drops, until it's told to stop; then, or gone, never
|
|
48
|
+
again. One runtime has both plugins; an app of many orgs runs `cloudPool` once (its directory's
|
|
49
|
+
runtime, needing nothing else) and gives each org's runtime `remotePool(call, org)`, the same
|
|
50
|
+
Actions forwarded there, each org touching only its own VMs.
|
|
51
|
+
- **Computer program:** `computer pool --app <url>`, a free VM of an app's pool: it waits to serve a
|
|
52
|
+
computer, mounts its disk, and runs it, connected at the address it's told (its org's). Given the
|
|
53
|
+
app's socket (`wss://…`), its identity is still for the app's https origin. scripts/cloud has a
|
|
54
|
+
Google Cloud project's setup and the pool VMs' startup script.
|
|
55
|
+
- **Previews:** what a job serves, at `https://<computer>-preview.<domain>`, carried to the computer
|
|
56
|
+
over its own connection (no inbound network), streamed both ways (`computer.http`). For a space's
|
|
57
|
+
members (a signed cookie, given by the app) or anyone with the link. `computer.preview`,
|
|
58
|
+
`computer.previews`, `computer.unpreview`, and the agents' `preview` tool. Settings
|
|
59
|
+
`computers.previews` `{ domain, key }`. WebSockets aren't carried yet.
|
|
60
|
+
- **Apps** (plugin `apps`): what a job serves, published to run on its own at
|
|
61
|
+
`https://<label>.copilotz.app`. The space's folder is archived by its computer
|
|
62
|
+
(`computer.archive`), built (its Dockerfile, else buildpacks) and deployed as a service that
|
|
63
|
+
scales to zero. `app.publish` (and the agents' `publish` tool, always asked), `app.list`,
|
|
64
|
+
`app.redeploy`, `app.remove`; builds and running metered under `app`. Hosts: Cloud Run
|
|
65
|
+
(`cloudRun`) and a local one (`localApps`).
|
|
66
|
+
- **Apps' safety:** services and images are named `cz-app-<name>` (an app never replaces another
|
|
67
|
+
service in its project); builds run as `cz-build@<project>` (`buildServiceAccount`), never the
|
|
68
|
+
project's default service account, as they run the app's own Dockerfile. `app.publish`'s `port`
|
|
69
|
+
wins over its job's.
|
|
70
|
+
- **Previews' safety:** a preview's `Set-Cookie` loses its `Domain` (no preview sets cookies for the
|
|
71
|
+
domain's other hosts); a request with several `cz_preview` cookies gets in when any is its host's.
|
|
72
|
+
- **Jobs** can say their `port` (a preview's) and be kept (`keep`): a kept job runs again when its
|
|
73
|
+
computer wakes.
|
|
74
|
+
- **A person's panel** (`computer.work`): `wake` wakes a computer that sleeps (its page opened);
|
|
75
|
+
what only reads (`jobs.list`, `jobs.logs`, `files.list`, `files.read`, `search`) neither wakes it
|
|
76
|
+
nor keeps it awake, so a page left open lets it sleep (`computer.view` keeps it awake while
|
|
77
|
+
viewed, up to the limit). A computer the app makes says what its agents ask about
|
|
78
|
+
(`computer.provision`'s `approve`, kept on it and its desks).
|
|
79
|
+
- **On the web** (`copilotz/plugins/compute/web`: `computersWeb`, `cloudWeb`, `appsWeb`):
|
|
80
|
+
- adding a computer is one choice: a cloud computer (its sizes, priced an hour), your own machine,
|
|
81
|
+
or one of yours;
|
|
82
|
+
- a cloud computer shows its state, how long it's been awake and about what that came to; asleep,
|
|
83
|
+
waking it, with honest progress; deleting it, asked first;
|
|
84
|
+
- a job serving a port can be previewed (members, or anyone with the link), opened, copied and
|
|
85
|
+
published as an app (live as it builds), and a space has an Apps tab;
|
|
86
|
+
- jobs can be kept running.
|
|
87
|
+
- **Web host:** routes by host (`{ host }` beside `{ path }`); a trusted front (`front`: a secret
|
|
88
|
+
header and the host it forwards) for apps behind a proxy; orgs claim hosts in the directory
|
|
89
|
+
(`orgHost`), resolved at `/__copilotz/hosts/resolve`.
|
|
90
|
+
- **Runtime:** `runtime.workers.dispatch(kind, id, workload, body?)`, for host code (a route)
|
|
91
|
+
streaming work to a connected worker.
|
|
92
|
+
- **Usage:** `usage.quote` prices quantities as a use would be, without recording one.
|
|
93
|
+
|
|
1
94
|
## 1.0.0-rc.4 — 2026-10-30
|
|
2
95
|
|
|
3
96
|
- **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.
|
|
25
|
+
On Deno, name it `copilotz` in `deno.json` (`"copilotz": "jsr:@copilotz/copilotz@^1.0.0-rc.6"`), 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.
|
|
207
|
+
plugins: [...plugins, await computerDownloads({ dir: "dist/computer/1.0.0-rc.6" })], // its own: /computer/download/
|
|
208
208
|
plugins: [...plugins, await computerDownloads({ url: "https://downloads.example.com/computer/" })], // or elsewhere
|
|
209
209
|
```
|
|
210
210
|
|
package/hosts/orgs/mod.d.ts
CHANGED
|
@@ -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
|
-
/**
|
|
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
|
|
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;
|
package/hosts/web/node/serve.js
CHANGED
|
@@ -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,
|
package/hosts/web/routes.d.ts
CHANGED
|
@@ -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
|
-
/**
|
|
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
|
|
144
|
+
export declare function routeServer(runtime: Runtime, whom: (request: Request) => Promise<Actor | null>, options?: {
|
|
145
|
+
front?: Front;
|
|
146
|
+
}): (request: Request) => Promise<Response> | undefined;
|
package/hosts/web/routes.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
}
|
package/hosts/web/server.d.ts
CHANGED
|
@@ -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
|