copilotz 1.0.0-rc.21 → 1.0.0-rc.23

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 (211) hide show
  1. package/CHANGELOG.md +320 -1
  2. package/README.md +217 -87
  3. package/hosts/orgs/mod.d.ts +33 -13
  4. package/hosts/orgs/mod.js +183 -84
  5. package/hosts/web/address.d.ts +11 -0
  6. package/hosts/web/address.js +46 -0
  7. package/hosts/web/assets.js +11 -5
  8. package/hosts/web/entrances.d.ts +250 -0
  9. package/hosts/web/entrances.js +362 -0
  10. package/hosts/web/host.d.ts +15 -3
  11. package/hosts/web/host.js +31 -18
  12. package/hosts/web/mod.d.ts +4 -2
  13. package/hosts/web/mod.js +6 -0
  14. package/hosts/web/node/bundle.d.ts +5 -0
  15. package/hosts/web/node/bundle.js +1 -1
  16. package/hosts/web/node/mod.d.ts +6 -4
  17. package/hosts/web/node/mod.js +6 -4
  18. package/hosts/web/node/serve.d.ts +20 -23
  19. package/hosts/web/node/serve.js +10 -21
  20. package/hosts/web/routes.d.ts +23 -17
  21. package/hosts/web/routes.js +56 -32
  22. package/hosts/web/server.d.ts +89 -44
  23. package/hosts/web/server.js +208 -108
  24. package/hosts/web/signin.d.ts +7 -10
  25. package/hosts/web/signin.js +6 -3
  26. package/hosts/web/surface.d.ts +20 -0
  27. package/hosts/web/surface.js +42 -0
  28. package/hosts/web/theme/css.d.ts +2 -1
  29. package/hosts/web/theme/css.js +9 -2
  30. package/hosts/web/tickets.d.ts +14 -1
  31. package/hosts/web/tickets.js +30 -7
  32. package/hosts/web/widget.d.ts +31 -0
  33. package/hosts/web/widget.js +429 -0
  34. package/hosts/web/widgets.d.ts +43 -0
  35. package/hosts/web/widgets.js +117 -0
  36. package/package.json +5 -1
  37. package/plugins/access/contacts/actions/forget.js +3 -1
  38. package/plugins/access/contacts/actions/start.js +18 -8
  39. package/plugins/access/contacts/collections/contact.d.ts +2 -2
  40. package/plugins/access/contacts/collections/contact.js +1 -1
  41. package/plugins/access/contacts/contract.d.ts +5 -5
  42. package/plugins/access/contacts/contract.js +5 -5
  43. package/plugins/access/contacts/mod.d.ts +4 -0
  44. package/plugins/access/contacts/mod.js +4 -0
  45. package/plugins/access/contacts/views/contact.js +1 -0
  46. package/plugins/access/dev/actions/sign-in.d.ts +1 -0
  47. package/plugins/access/dev/actions/sign-in.js +38 -0
  48. package/plugins/access/dev/mod.d.ts +17 -0
  49. package/plugins/access/dev/mod.js +31 -0
  50. package/plugins/access/dev/shared/nearby.d.ts +2 -0
  51. package/plugins/access/dev/shared/nearby.js +27 -0
  52. package/plugins/access/directory/actions/invite.js +2 -2
  53. package/plugins/access/directory/actions/org.js +11 -8
  54. package/plugins/access/directory/collections/credit-code.d.ts +1 -1
  55. package/plugins/access/google/actions/sign-in.js +2 -0
  56. package/plugins/access/google/mod.d.ts +4 -2
  57. package/plugins/access/google/mod.js +15 -2
  58. package/plugins/access/identity/actions/session.js +5 -3
  59. package/plugins/access/identity/mod.d.ts +4 -2
  60. package/plugins/access/identity/mod.js +17 -2
  61. package/plugins/access/mod.d.ts +7 -0
  62. package/plugins/access/mod.js +1 -0
  63. package/plugins/access/spaces/shared/declared.d.ts +1 -1
  64. package/plugins/access/spaces/shared/rules.d.ts +16 -5
  65. package/plugins/access/spaces/shared/rules.js +74 -6
  66. package/plugins/agents/chat/actions/send.d.ts +21 -0
  67. package/plugins/agents/chat/actions/send.js +72 -13
  68. package/plugins/agents/chat/collections/file.d.ts +15 -0
  69. package/plugins/agents/chat/collections/file.js +12 -3
  70. package/plugins/agents/chat/collections/live.d.ts +11 -0
  71. package/plugins/agents/chat/collections/live.js +11 -1
  72. package/plugins/agents/chat/collections/message.d.ts +1 -1
  73. package/plugins/agents/chat/collections/wait.d.ts +1 -1
  74. package/plugins/agents/chat/contract.d.ts +3 -2
  75. package/plugins/agents/chat/contract.js +1 -0
  76. package/plugins/agents/chat/mod.d.ts +9 -3
  77. package/plugins/agents/chat/mod.js +28 -7
  78. package/plugins/agents/chat/processors/failed.js +8 -3
  79. package/plugins/agents/chat/processors/turn.js +6 -3
  80. package/plugins/agents/chat/shared/agents.js +11 -2
  81. package/plugins/agents/chat/shared/live.d.ts +4 -2
  82. package/plugins/agents/chat/shared/live.js +2 -1
  83. package/plugins/agents/chat/shared/types.d.ts +8 -0
  84. package/plugins/agents/history/collections/outline.d.ts +2 -2
  85. package/plugins/agents/openapi/shared/files.js +2 -0
  86. package/plugins/agents/routines/collections/routine.d.ts +1 -1
  87. package/plugins/billing/credits/collections/plan.d.ts +2 -2
  88. package/plugins/billing/credits/collections/room.d.ts +1 -1
  89. package/plugins/billing/usage/collections/rollup.d.ts +2 -2
  90. package/plugins/billing/usage/collections/usage.d.ts +2 -2
  91. package/plugins/compute/cloud/actions/pool.js +2 -0
  92. package/plugins/compute/cloud/actions/remote.js +4 -2
  93. package/plugins/compute/cloud/actions/vms.js +2 -0
  94. package/plugins/compute/cloud/collections/pool-vm.d.ts +1 -1
  95. package/plugins/compute/computers/actions/admit.js +2 -0
  96. package/plugins/compute/computers/actions/code.js +2 -0
  97. package/plugins/compute/computers/actions/enrollment.js +2 -0
  98. package/plugins/compute/computers/actions/work.js +3 -0
  99. package/plugins/compute/computers/program/version.d.ts +1 -1
  100. package/plugins/compute/computers/program/version.js +1 -1
  101. package/plugins/compute/computers/resources/downloads.js +1 -1
  102. package/plugins/compute/computers/resources/previews.js +5 -2
  103. package/plugins/interfaces/maintenance/mod.d.ts +3 -1
  104. package/plugins/interfaces/maintenance/mod.js +3 -1
  105. package/plugins/interfaces/mcp/actions/call.d.ts +4 -0
  106. package/plugins/interfaces/mcp/actions/call.js +120 -18
  107. package/plugins/interfaces/mcp/actions/conversation.d.ts +1 -0
  108. package/plugins/interfaces/mcp/actions/conversation.js +49 -0
  109. package/plugins/interfaces/mcp/actions/prune.js +5 -3
  110. package/plugins/interfaces/mcp/collections/opening.d.ts +24 -0
  111. package/plugins/interfaces/mcp/collections/opening.js +28 -0
  112. package/plugins/interfaces/mcp/mod.d.ts +27 -3
  113. package/plugins/interfaces/mcp/mod.js +42 -7
  114. package/plugins/interfaces/mcp/resources/server.d.ts +13 -4
  115. package/plugins/interfaces/mcp/resources/server.js +163 -21
  116. package/plugins/interfaces/mcp/resources/tools.d.ts +88 -12
  117. package/plugins/interfaces/mcp/resources/tools.js +113 -19
  118. package/plugins/interfaces/mcp/resources/widgets.d.ts +161 -0
  119. package/plugins/interfaces/mcp/resources/widgets.js +246 -0
  120. package/plugins/interfaces/mcp/shared/keys.d.ts +25 -19
  121. package/plugins/interfaces/mcp/shared/keys.js +55 -9
  122. package/plugins/interfaces/mcp/shared/settings.d.ts +70 -37
  123. package/plugins/interfaces/mcp/shared/settings.js +64 -39
  124. package/plugins/interfaces/mcp/views/offered.d.ts +1 -1
  125. package/plugins/interfaces/mod.d.ts +2 -1
  126. package/plugins/interfaces/mod.js +2 -1
  127. package/plugins/interfaces/voice/collections/blocked.d.ts +2 -2
  128. package/plugins/interfaces/voice/collections/call.d.ts +3 -3
  129. package/plugins/interfaces/voice/contract.d.ts +1 -1
  130. package/plugins/interfaces/voice/mod.d.ts +3 -3
  131. package/plugins/interfaces/voice/mod.js +3 -3
  132. package/plugins/interfaces/voice/resources/relay.d.ts +5 -3
  133. package/plugins/interfaces/voice/resources/relay.js +17 -6
  134. package/plugins/interfaces/voice/resources/tools.js +2 -0
  135. package/plugins/interfaces/voice/shared/settings.d.ts +5 -0
  136. package/plugins/interfaces/voice/views/call.d.ts +18 -11
  137. package/plugins/interfaces/voice/views/call.js +15 -3
  138. package/plugins/interfaces/voice/views/message.d.ts +1 -1
  139. package/plugins/interfaces/web/actions/arrive.d.ts +1 -0
  140. package/plugins/interfaces/web/actions/arrive.js +120 -0
  141. package/plugins/interfaces/web/actions/prune.d.ts +1 -0
  142. package/plugins/interfaces/web/actions/prune.js +51 -0
  143. package/plugins/interfaces/web/actions/sending.d.ts +1 -0
  144. package/plugins/interfaces/web/actions/sending.js +222 -0
  145. package/plugins/interfaces/web/actions/spent.d.ts +2 -0
  146. package/plugins/interfaces/web/actions/spent.js +59 -0
  147. package/plugins/interfaces/web/collections/rate.d.ts +122 -0
  148. package/plugins/interfaces/web/collections/rate.js +37 -0
  149. package/plugins/interfaces/web/collections/visitor.d.ts +110 -0
  150. package/plugins/interfaces/web/collections/visitor.js +64 -0
  151. package/plugins/interfaces/web/contract.d.ts +4 -0
  152. package/plugins/interfaces/web/contract.js +1 -0
  153. package/plugins/interfaces/web/lines.d.ts +36 -0
  154. package/plugins/interfaces/web/lines.js +78 -0
  155. package/plugins/interfaces/web/processors/forget.d.ts +10 -0
  156. package/plugins/interfaces/web/processors/forget.js +14 -0
  157. package/plugins/interfaces/web/resources/entrance.d.ts +47 -0
  158. package/plugins/interfaces/web/resources/entrance.js +83 -0
  159. package/plugins/interfaces/web/resources/spend.d.ts +2 -0
  160. package/plugins/interfaces/web/resources/spend.js +62 -0
  161. package/plugins/interfaces/web/shared/budget.d.ts +65 -0
  162. package/plugins/interfaces/web/shared/budget.js +134 -0
  163. package/plugins/interfaces/web/shared/lines.d.ts +158 -0
  164. package/plugins/interfaces/web/shared/lines.js +156 -0
  165. package/plugins/interfaces/web/shared/secret.d.ts +33 -0
  166. package/plugins/interfaces/web/shared/secret.js +86 -0
  167. package/plugins/interfaces/whatsapp/collections/phone.d.ts +1 -1
  168. package/plugins/interfaces/whatsapp/mod.d.ts +1 -1
  169. package/plugins/interfaces/whatsapp/mod.js +1 -1
  170. package/plugins/interfaces/whatsapp/views/message.d.ts +1 -1
  171. package/plugins/models/contract.d.ts +1 -1
  172. package/plugins/models/shared/generate.js +13 -4
  173. package/plugins/models/shared/transcribe.js +7 -1
  174. package/plugins/models/shared/use.d.ts +16 -2
  175. package/plugins/models/shared/use.js +20 -0
  176. package/runtime/browsers.d.ts +153 -17
  177. package/runtime/browsers.js +336 -30
  178. package/runtime/cache.js +25 -5
  179. package/runtime/caller.d.ts +117 -0
  180. package/runtime/caller.js +215 -0
  181. package/runtime/client.d.ts +47 -3
  182. package/runtime/client.js +132 -42
  183. package/runtime/define.d.ts +30 -9
  184. package/runtime/executor.js +7 -6
  185. package/runtime/expiring.d.ts +23 -0
  186. package/runtime/expiring.js +64 -0
  187. package/runtime/links.d.ts +27 -4
  188. package/runtime/links.js +49 -11
  189. package/runtime/memory.js +102 -15
  190. package/runtime/mod.d.ts +3 -1
  191. package/runtime/mod.js +3 -1
  192. package/runtime/mongo.d.ts +12 -0
  193. package/runtime/mongo.js +44 -0
  194. package/runtime/observe.js +82 -9
  195. package/runtime/ops.d.ts +9 -1
  196. package/runtime/owner.d.ts +14 -1
  197. package/runtime/owner.js +78 -23
  198. package/runtime/reads.js +2 -2
  199. package/runtime/registry.js +9 -3
  200. package/runtime/runtime.d.ts +7 -0
  201. package/runtime/runtime.js +1 -0
  202. package/runtime/timers.js +2 -0
  203. package/runtime/views.d.ts +1 -1
  204. package/runtime/views.js +2 -2
  205. package/runtime/wire.d.ts +1 -1
  206. package/stores/sql/postgres/maintenance.d.ts +8 -1
  207. package/stores/sql/postgres/maintenance.js +76 -9
  208. package/stores/sql/postgres/mod.d.ts +12 -4
  209. package/stores/sql/postgres/mod.js +21 -8
  210. package/plugins/interfaces/mcp/shared/caller.d.ts +0 -45
  211. package/plugins/interfaces/mcp/shared/caller.js +0 -99
package/hosts/orgs/mod.js CHANGED
@@ -1,6 +1,8 @@
1
- import { definePlugin } from "../../runtime/mod.js";
1
+ import { contactsPool, definePlugin } from "../../runtime/mod.js";
2
2
  import { createTickets } from "../web/tickets.js";
3
- import { cookieOf, setCookie, socket, webServer, } from "../web/server.js";
3
+ import { carry } from "../web/address.js";
4
+ import { socket, webServer } from "../web/server.js";
5
+ import { cookieOf, entrancesOf, fromHere, fromPage, hostCookie, jsonOf, legacyName, refusal, retire, setCookie, signInsOf, siteOf, whomOf, } from "../web/entrances.js";
4
6
  import { hostMatches, hostOf, hostServer, vouched } from "../web/routes.js";
5
7
  /** How long which org a host is stays known here (claims and releases here forget it at once). */
6
8
  const CLAIMS_MS = 60_000;
@@ -8,6 +10,8 @@ const CLAIMS_MS = 60_000;
8
10
  const RESOLVE = "/__copilotz/hosts/resolve";
9
11
  const DIRECTORY = "dir";
10
12
  const CURRENT = "org";
13
+ /** How the directory's session cookie says who it is, and ends (identity's session). */
14
+ const SESSION = { query: "credential.actor", end: "session.end" };
11
15
  const MONTH = 30 * 86_400;
12
16
  const sessionOf = (org) => `s_${org}`;
13
17
  /** Where an org is reached by its address: `/o/<org>`. */
@@ -18,31 +22,60 @@ const baseOf = (org) => `/o/${encodeURIComponent(org)}`;
18
22
  * a socket). Its runtime takes sockets there and at `/oxian` (`given.transports`).
19
23
  */
20
24
  const socketOf = (org) => `${baseOf(org)}${socket.config.path}`;
25
+ /** The paths the orgs host answers itself, not an org's. */
26
+ const ours = (path) => path.startsWith("/signin/") || path === "/signout" || path === "/orgs" ||
27
+ path.startsWith("/orgs/") || path.startsWith("/console/");
21
28
  /** The org a path addresses (`/o/<org>/…`), and the path within it. */
22
29
  const addressed = (path) => {
23
30
  const found = /^\/o\/([^/]+)(\/.*)?$/.exec(path);
24
31
  return found ? { org: decodeURIComponent(found[1]), path: found[2] ?? "/" } : undefined;
25
32
  };
33
+ /** What the host answers itself is someone's: never kept by a browser or a cache. */
26
34
  const json = (value, init = {}) => {
27
- const headers = new Headers({ "content-type": "application/json" });
35
+ const headers = new Headers({ "content-type": "application/json", "cache-control": "no-store" });
28
36
  for (const cookie of init.cookies ?? [])
29
37
  headers.append("set-cookie", cookie);
30
38
  return new Response(JSON.stringify(value), { status: init.status ?? 200, headers });
31
39
  };
32
- const loopback = (url) => ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname);
40
+ /**
41
+ * The name each cookie an org's entrances declare goes by here: its session
42
+ * `s_<org>`, any other `<name>_<org>`, so one org's never reads another's
43
+ * (each then the host's alone: `__Host-` wherever it's Secure).
44
+ */
45
+ const cookiesOf = (org) => (name) => name === "session" ? sessionOf(org) : `${name}_${org}`;
46
+ /** An answer with these cookies set too, never kept. */
47
+ const withSetCookies = (answered, cookies) => {
48
+ if (!cookies.length)
49
+ return answered;
50
+ const headers = new Headers(answered.headers);
51
+ headers.set("cache-control", "no-store");
52
+ for (const cookie of cookies)
53
+ headers.append("set-cookie", cookie);
54
+ return new Response(answered.body, { status: answered.status, headers });
55
+ };
33
56
  /** A request with these cookies set too, as the org's web host will read them. */
34
57
  const withCookies = (request, cookies) => {
35
58
  const kept = (request.headers.get("cookie") ?? "").split(/;\s*/).filter((c) => c && !Object.keys(cookies).some((name) => c.startsWith(`${name}=`)));
36
59
  const headers = new Headers(request.headers);
37
60
  headers.set("cookie", [...kept, ...Object.entries(cookies).map(([n, v]) => `${n}=${v}`)].join("; "));
38
- return new Request(request, { headers });
61
+ return carry(new Request(request, { headers }), request);
39
62
  };
40
63
  export function orgsHost(options) {
41
64
  const { directory } = options;
65
+ // The ways to sign in the directory declares, checked now: a bad declaration stops the start.
66
+ const signIns = signInsOf(directory, entrancesOf(directory));
42
67
  const idleMs = options.idleMs ?? 30 * 60_000;
43
68
  const cells = new Map();
69
+ /** Every org's contacts' connections, counted together: they share the listener with people. */
70
+ const pool = contactsPool();
44
71
  /** The cells open now, for what must find one at once (a socket upgrade on Node). */
45
72
  const live = new Map();
73
+ /** The address a request is for, as the front says (`siteOf`): what the cookies go by. */
74
+ const siteFor = (request) => siteOf(request, options.front);
75
+ /** One of the host's own cookies a request carries, by its name here (`hostCookie`). */
76
+ const ownCookie = (request, name) => cookieOf(request, hostCookie(siteFor(request), name));
77
+ /** One of the host's own cookies, set (or cleared, `maxAge` 0) under its name here. */
78
+ const keep = (site, name, value, maxAge) => setCookie(site, value, maxAge, hostCookie(site, name));
46
79
  /** Whether a host is the app's own (every host is, when the app names none). */
47
80
  const own = (host) => !options.hosts || options.hosts.some((pattern) => hostMatches(pattern, host));
48
81
  /** Which org each host is, as the directory says, kept a minute: every request asks. */
@@ -109,6 +142,7 @@ export function orgsHost(options) {
109
142
  plugins: [here(org)],
110
143
  admit: tickets.admit,
111
144
  transports: [socket, { type: "websocket", config: { path: socketOf(org.id) } }],
145
+ browsers: { pool },
112
146
  });
113
147
  const serve = webServer({
114
148
  runtime,
@@ -117,21 +151,18 @@ export function orgsHost(options) {
117
151
  upgrade: (request) => options.upgrade(runtime, request),
118
152
  ...(options.title ? { title: options.title } : {}),
119
153
  ...(options.theme ? { theme: options.theme } : {}),
120
- ...(options.google ? { google: options.google } : {}),
121
- ...(options.dev ? { dev: options.dev } : {}),
122
154
  ...(options.assets ? { assets: options.assets } : {}),
123
155
  ...(options.front ? { front: options.front } : {}),
124
- cookie: sessionOf(org.id),
125
- signIn: false,
156
+ cookie: cookiesOf(org.id),
157
+ // People sign in at the directory; an org's page shows its ways to.
158
+ signIn: { at: directory },
126
159
  base: baseOf(org.id),
127
160
  socket: socketOf(org.id),
128
161
  });
129
- const hosted = hostServer(runtime, async (request) => {
130
- const secret = cookieOf(request, sessionOf(org.id));
131
- return secret
132
- ? await runtime.query("credential.actor", { secret })
133
- : null;
134
- }, options.front ? { front: options.front } : {});
162
+ const hosted = hostServer(runtime, whomOf(runtime, {
163
+ cookie: cookiesOf(org.id),
164
+ ...(options.front ? { front: options.front } : {}),
165
+ }), options.front ? { front: options.front } : {});
135
166
  const made = { org, runtime, tickets, serve, hosted, last: Date.now() };
136
167
  live.set(org.id, made);
137
168
  return made;
@@ -153,7 +184,7 @@ export function orgsHost(options) {
153
184
  };
154
185
  /** Who the directory session signs in, with what they verified. */
155
186
  const signedIn = async (request) => {
156
- const secret = cookieOf(request, DIRECTORY);
187
+ const secret = ownCookie(request, DIRECTORY);
157
188
  if (!secret)
158
189
  return null;
159
190
  const actor = await directory.query("credential.actor", { secret });
@@ -165,7 +196,7 @@ export function orgsHost(options) {
165
196
  return { person: actor.id, email };
166
197
  };
167
198
  /** Goes into an org: the directory lets them in; the org knows them, and gives them a session. */
168
- const enter = async (request, url, who, org, asStaff = false) => {
199
+ const enter = async (request, who, org, asStaff = false) => {
169
200
  const { org: info, role, staff } = await directory.call("org.enter", {
170
201
  org,
171
202
  person: who.person,
@@ -186,7 +217,7 @@ export function orgsHost(options) {
186
217
  await cell.runtime.call("org.join", { person: who.person, ...(role ? { role } : {}) });
187
218
  }
188
219
  // A session they hold already, for this org and this person, stays.
189
- const held = cookieOf(request, sessionOf(info.id));
220
+ const held = ownCookie(request, sessionOf(info.id));
190
221
  const kept = held
191
222
  ? await cell.runtime.query("credential.actor", { secret: held })
192
223
  : null;
@@ -196,18 +227,66 @@ export function orgsHost(options) {
196
227
  person: who.person,
197
228
  ...(staff ? { staff: true } : {}),
198
229
  })).session;
230
+ const site = siteFor(request);
199
231
  return {
200
232
  org: info,
201
233
  session,
202
234
  cookies: [
203
- setCookie(url, session, MONTH, sessionOf(info.id)),
204
- setCookie(url, info.id, MONTH, CURRENT),
235
+ keep(site, sessionOf(info.id), session, MONTH),
236
+ keep(site, CURRENT, info.id, MONTH),
205
237
  ],
206
238
  };
207
239
  };
240
+ /**
241
+ * Retires the host's own cookies a request holds under their names from
242
+ * before they were the host's alone (`dir`, `org`; see `retire`): the
243
+ * directory's session ends, and both go. The clearing `Set-Cookie`s.
244
+ */
245
+ const retired = async (request) => {
246
+ const site = siteFor(request);
247
+ const cleared = await Promise.all([
248
+ retire(directory, request, site, hostCookie(site, DIRECTORY), SESSION),
249
+ retire(directory, request, site, hostCookie(site, CURRENT)),
250
+ ]);
251
+ return cleared.filter((cookie) => cookie !== undefined);
252
+ };
253
+ /**
254
+ * Signs out of every org a request holds a session for (`s_<org>`, by its
255
+ * name here or from before it was the host's alone): each session ends in
256
+ * its org, when that org is open here (signing out never opens one: any
257
+ * request may name orgs in its cookies), and the browsers connected as
258
+ * whoever it was go. Every one's cookie goes. The clearing `Set-Cookie`s.
259
+ */
260
+ const signedOutOfOrgs = async (request) => {
261
+ const site = siteFor(request);
262
+ const names = [hostCookie(site, sessionOf(""))];
263
+ const legacy = legacyName(names[0]);
264
+ if (legacy)
265
+ names.push(legacy);
266
+ const held = (request.headers.get("cookie") ?? "").split(/;\s*/).flatMap((c) => {
267
+ const at = c.indexOf("=");
268
+ const [name, secret] = [c.slice(0, at), c.slice(at + 1)];
269
+ const prefix = at > 0 ? names.find((n) => name.startsWith(n)) : undefined;
270
+ return prefix ? [{ name, secret, org: name.slice(prefix.length) }] : [];
271
+ });
272
+ await Promise.all(held.map(async ({ org, secret }) => {
273
+ const cell = org ? live.get(org) : undefined;
274
+ if (!cell)
275
+ return;
276
+ const { runtime } = cell;
277
+ const actor = await runtime.query("credential.actor", { secret })
278
+ .catch(() => null);
279
+ if (!actor)
280
+ return;
281
+ await runtime.call("session.end", { secret }, { actor }).catch(() => { });
282
+ runtime.disconnect(actor);
283
+ cell.tickets.end?.(actor);
284
+ }).map((ended) => ended.catch(() => { })));
285
+ return held.map(({ name }) => setCookie(site, "", 0, name));
286
+ };
208
287
  /** Whether the session a request holds in an org is staff's. */
209
288
  const heldAsStaff = async (request, org) => {
210
- const secret = cookieOf(request, sessionOf(org));
289
+ const secret = ownCookie(request, sessionOf(org));
211
290
  const info = secret ? await orgInfo(org) : null;
212
291
  if (!info || info.suspended)
213
292
  return false;
@@ -218,7 +297,7 @@ export function orgsHost(options) {
218
297
  };
219
298
  /** Whether a request's session for an org's cell is still good there. */
220
299
  const heldIn = async (cell, request) => {
221
- const secret = cookieOf(request, sessionOf(cell.org.id));
300
+ const secret = ownCookie(request, sessionOf(cell.org.id));
222
301
  return !!secret && !!await cell.runtime.query("credential.actor", { secret }).catch(() => null);
223
302
  };
224
303
  /** The org someone goes back to: the one they were in last (not `not`), or their personal one. */
@@ -234,7 +313,7 @@ export function orgsHost(options) {
234
313
  });
235
314
  return org;
236
315
  };
237
- const signIn = async (request, url, person, session) => {
316
+ const signIn = async (request, person, session) => {
238
317
  const who = {
239
318
  person,
240
319
  email: (await directory.call("person.email", {
@@ -244,24 +323,26 @@ export function orgsHost(options) {
244
323
  const profile = await directory.query("person.profile", { person })
245
324
  .catch(() => null);
246
325
  const org = await landing(person, profile?.name ?? who.email ?? person);
247
- const entered = await enter(request, url, who, org);
326
+ const entered = await enter(request, who, org);
248
327
  return json({ person, org: entered.org }, {
249
- cookies: [setCookie(url, session, MONTH, DIRECTORY), ...entered.cookies],
328
+ cookies: [keep(siteFor(request), DIRECTORY, session, MONTH), ...entered.cookies],
250
329
  });
251
330
  };
252
- /** The lobby: the page before anyone's signed in (it shows sign-in). */
331
+ /**
332
+ * The lobby: the page before anyone's signed in (it shows the directory's
333
+ * ways to sign in). Nobody connects there (no tickets), and no cookie is
334
+ * anyone to the directory's routes there.
335
+ */
253
336
  const lobby = webServer({
254
337
  runtime: directory,
255
- tickets: createTickets(),
256
338
  code: options.code,
257
339
  upgrade: () => new Response("Sign in first", { status: 401 }),
258
340
  ...(options.title ? { title: options.title } : {}),
259
341
  ...(options.theme ? { theme: options.theme } : {}),
260
- ...(options.google ? { google: options.google } : {}),
261
- ...(options.dev ? { dev: options.dev } : {}),
262
342
  ...(options.assets ? { assets: options.assets } : {}),
263
- cookie: "none",
264
- signIn: false,
343
+ ...(options.front ? { front: options.front } : {}),
344
+ cookie: () => "none",
345
+ signIn: { at: directory },
265
346
  });
266
347
  /** Tells the directory how an org is (its people, spaces, when it was last used). */
267
348
  const report = (cell) => cell.runtime.call("org.summary").then((summary) => directory.call("org.report", {
@@ -393,6 +474,28 @@ export function orgsHost(options) {
393
474
  if (!own(host))
394
475
  return await claimed(request, host);
395
476
  const at = addressed(url.pathname);
477
+ // What it answers itself to a POST is the app's own page's to ask (an org's, its host's).
478
+ if (request.method === "POST" && !at && ours(url.pathname) && !fromPage(request, options.front)) {
479
+ return json({ error: "Not from this app's page" }, { status: 403 });
480
+ }
481
+ if (request.method === "POST" && url.pathname.startsWith("/signin/")) {
482
+ const method = signIns.get(url.pathname.slice("/signin/".length));
483
+ if (method && (!method.local || fromHere(request, options.front))) {
484
+ const asked = await jsonOf(request);
485
+ if (!asked) {
486
+ return json({ error: "Send a JSON object, of 16 KiB at most" }, { status: 400 });
487
+ }
488
+ let signed;
489
+ try {
490
+ signed = await directory.call(method.action, asked);
491
+ }
492
+ catch (error) {
493
+ const said = refusal(error, "Couldn't sign in");
494
+ return json({ error: said.error }, { status: said.status });
495
+ }
496
+ return await signIn(request, signed.person, signed.session);
497
+ }
498
+ }
396
499
  if (at) {
397
500
  const org = await orgInfo(at.org);
398
501
  if (!org)
@@ -406,35 +509,12 @@ export function orgsHost(options) {
406
509
  return await options.upgrade(cell.runtime, request);
407
510
  const inner = new URL(url);
408
511
  inner.pathname = at.path;
409
- return await cell.serve(new Request(inner, request));
512
+ const answered = await cell.serve(carry(new Request(inner, request), request));
513
+ return request.method === "POST" && at.path === "/connect"
514
+ ? withSetCookies(answered, await retired(request))
515
+ : answered;
410
516
  }
411
517
  switch (route) {
412
- case "POST /signin/google": {
413
- if (!options.google)
414
- break;
415
- const { idToken } = await request.json();
416
- const { person, session } = await directory.call("google.sign-in", { idToken });
417
- return await signIn(request, url, person, session);
418
- }
419
- case "POST /signin/dev": {
420
- if (!options.dev || !loopback(url))
421
- break;
422
- const { name } = await request.json();
423
- const person = String(name ?? "").trim().toLowerCase();
424
- const email = /^[a-z0-9._+-]{1,64}@[a-z0-9.-]{1,64}$/.test(person);
425
- if (!email && !/^[a-z0-9._-]{1,40}$/.test(person)) {
426
- return json({ error: "A name (letters, digits, . _ -) or an email" }, { status: 400 });
427
- }
428
- await directory.call("person.set", {
429
- person,
430
- ...(email ? { email: person } : {}),
431
- name: email ? person.split("@")[0] : person,
432
- });
433
- const { session } = await directory.call("session.start", {
434
- person,
435
- });
436
- return await signIn(request, url, person, session);
437
- }
438
518
  case "GET /orgs": {
439
519
  const who = await signedIn(request);
440
520
  if (!who)
@@ -448,7 +528,7 @@ export function orgsHost(options) {
448
528
  ? directory.call("invite.mine", { email: who.email })
449
529
  : Promise.resolve({ invites: [] }),
450
530
  ]);
451
- const current = cookieOf(request, CURRENT) ?? null;
531
+ const current = ownCookie(request, CURRENT) ?? null;
452
532
  return json({
453
533
  person: who.person,
454
534
  email: who.email,
@@ -463,7 +543,7 @@ export function orgsHost(options) {
463
543
  const who = await signedIn(request);
464
544
  if (!who)
465
545
  return json({ error: "Not signed in" }, { status: 401 });
466
- const { name } = await request.json();
546
+ const { name } = (await jsonOf(request) ?? {});
467
547
  if (!String(name ?? "").trim())
468
548
  return json({ error: "A name" }, { status: 400 });
469
549
  const { org } = await directory.call("org.create", {
@@ -471,41 +551,55 @@ export function orgsHost(options) {
471
551
  kind: "team",
472
552
  by: who.person,
473
553
  });
474
- const entered = await enter(request, url, who, org);
554
+ const entered = await enter(request, who, org);
475
555
  return json({ org: entered.org }, { cookies: entered.cookies });
476
556
  }
477
557
  case "POST /orgs/enter": {
478
558
  const who = await signedIn(request);
479
559
  if (!who)
480
560
  return json({ error: "Not signed in" }, { status: 401 });
481
- const { org, staff } = await request.json();
482
- const entered = await enter(request, url, who, String(org), staff === true);
561
+ const { org, staff } = (await jsonOf(request) ?? {});
562
+ if (typeof org !== "string" || !org)
563
+ return json({ error: "Which org" }, { status: 400 });
564
+ const entered = await enter(request, who, org, staff === true);
483
565
  return json({ org: entered.org }, { cookies: entered.cookies });
484
566
  }
485
567
  case "POST /orgs/invites/answer": {
486
568
  const who = await signedIn(request);
487
569
  if (!who?.email)
488
570
  return json({ error: "Not signed in" }, { status: 401 });
489
- const { invite, accept } = await request.json();
571
+ const { invite, accept } = (await jsonOf(request) ?? {});
572
+ if (typeof invite !== "string") {
573
+ return json({ error: "Which invitation" }, { status: 400 });
574
+ }
490
575
  const answered = await directory.call("invite.answer", { invite, person: who.person, email: who.email, accept: !!accept });
491
576
  if (!answered.joined)
492
577
  return json({ joined: false });
493
- const entered = await enter(request, url, who, answered.org);
578
+ const entered = await enter(request, who, answered.org);
494
579
  return json({ joined: true, org: entered.org }, { cookies: entered.cookies });
495
580
  }
496
581
  case "POST /signout": {
497
- const secret = cookieOf(request, DIRECTORY);
582
+ const secret = ownCookie(request, DIRECTORY);
498
583
  const who = await signedIn(request);
499
584
  if (secret && who) {
500
- await directory.call("session.end", { session: secret }, {
585
+ await directory.call("session.end", { secret }, {
501
586
  actor: { kind: "person", id: who.person },
502
587
  }).catch(() => { });
503
588
  }
504
- const cookies = [setCookie(url, "", 0, DIRECTORY), setCookie(url, "", 0, CURRENT)];
505
- for (const c of (request.headers.get("cookie") ?? "").split(/;\s*/)) {
506
- const name = c.split("=")[0];
507
- if (name.startsWith("s_"))
508
- cookies.push(setCookie(url, "", 0, name));
589
+ const site = siteFor(request);
590
+ const cookies = [
591
+ keep(site, DIRECTORY, "", 0),
592
+ keep(site, CURRENT, "", 0),
593
+ // Every org's session it holds ends and goes too, and what's held from before.
594
+ ...await signedOutOfOrgs(request),
595
+ ...await retired(request),
596
+ ];
597
+ // Their browsers in every org open now go: one comes back only with a cookie that's someone.
598
+ if (who) {
599
+ for (const cell of live.values()) {
600
+ cell.runtime.disconnect({ kind: "person", id: who.person });
601
+ cell.tickets.end?.({ kind: "person", id: who.person });
602
+ }
509
603
  }
510
604
  return json({}, { cookies });
511
605
  }
@@ -547,19 +641,20 @@ export function orgsHost(options) {
547
641
  return json({ error: "Not found" }, { status: 404 });
548
642
  // Anything else is the org they're in, whose own web host checks its session. Without
549
643
  // one (signed in, but in no org yet, or its cookie gone), they're entered first.
550
- let current = cookieOf(request, CURRENT);
644
+ let current = ownCookie(request, CURRENT);
551
645
  let cookies = [];
552
646
  let forwarded = request;
553
- if (!current || !cookieOf(request, sessionOf(current))) {
647
+ if (!current || !ownCookie(request, sessionOf(current))) {
554
648
  const who = await signedIn(request);
555
649
  if (!who)
556
650
  return await lobby(request);
557
- const entered = await enter(request, url, who, current ?? await landing(who.person, who.email ?? who.person)).catch(async () => await enter(request, url, who, await landing(who.person, who.person)));
651
+ const entered = await enter(request, who, current ?? await landing(who.person, who.email ?? who.person)).catch(async () => await enter(request, who, await landing(who.person, who.person)));
558
652
  current = entered.org.id;
559
653
  cookies = entered.cookies;
654
+ const site = siteFor(request);
560
655
  forwarded = withCookies(request, {
561
- [CURRENT]: entered.org.id,
562
- [sessionOf(entered.org.id)]: entered.session,
656
+ [hostCookie(site, CURRENT)]: entered.org.id,
657
+ [hostCookie(site, sessionOf(entered.org.id))]: entered.session,
563
658
  });
564
659
  }
565
660
  const org = await orgInfo(current);
@@ -576,10 +671,11 @@ export function orgsHost(options) {
576
671
  const who = await signedIn(request);
577
672
  if (!who)
578
673
  return await lobby(request);
579
- const elsewhere = await enter(request, url, who, org.suspended ? await landing(who.person, who.person, org.id) : org.id)
580
- .catch(async () => await enter(request, url, who, await landing(who.person, who.person, org.id)));
674
+ const elsewhere = await enter(request, who, org.suspended ? await landing(who.person, who.person, org.id) : org.id)
675
+ .catch(async () => await enter(request, who, await landing(who.person, who.person, org.id)));
581
676
  const headers = new Headers({
582
677
  location: url.pathname === "/" ? "/" : url.pathname + url.search,
678
+ "cache-control": "no-store",
583
679
  });
584
680
  for (const cookie of elsewhere.cookies)
585
681
  headers.append("set-cookie", cookie);
@@ -587,18 +683,21 @@ export function orgsHost(options) {
587
683
  }
588
684
  cell.last = Date.now();
589
685
  const answered = await cell.serve(forwarded);
686
+ if (request.method === "POST" && url.pathname === "/connect") {
687
+ cookies.push(...await retired(request));
688
+ }
590
689
  if (!cookies.length)
591
690
  return answered;
691
+ // Its session's cookies go with it: never kept.
592
692
  const headers = new Headers(answered.headers);
693
+ headers.set("cache-control", "no-store");
593
694
  for (const cookie of cookies)
594
695
  headers.append("set-cookie", cookie);
595
696
  return new Response(answered.body, { status: answered.status, headers });
596
697
  }
597
698
  catch (error) {
598
- const code = error.code;
599
- return json({ error: error instanceof Error ? error.message : String(error) }, {
600
- status: code === "forbidden" ? 403 : 400,
601
- });
699
+ const said = refusal(error, "Something went wrong");
700
+ return json({ error: said.error }, { status: said.status });
602
701
  }
603
702
  };
604
703
  return {
@@ -613,7 +712,7 @@ export function orgsHost(options) {
613
712
  if (!own(hostOf(request, options.front)))
614
713
  return undefined;
615
714
  const named = addressed(new URL(request.url).pathname)?.org;
616
- const org = named ?? cookieOf(request, CURRENT);
715
+ const org = named ?? ownCookie(request, CURRENT);
617
716
  const cell = org ? live.get(org) : undefined;
618
717
  // An org named but closed (a computer connecting first) opens for its retry.
619
718
  if (!cell && named) {
@@ -0,0 +1,11 @@
1
+ /** The address a request came from, as its socket says; undefined when nothing says. */
2
+ export declare function clientAddress(request: Request): string | undefined;
3
+ /**
4
+ * Says where a request came from: the socket's peer address, as the app's
5
+ * own listener saw it (never a header's). Returns the request.
6
+ */
7
+ export declare function setClientAddress(request: Request, address: string): Request;
8
+ /** A request made from another keeps its address. */
9
+ export declare function carry(to: Request, from: Request): Request;
10
+ /** Whether an address is this machine's own: 127.0.0.0/8, `::1`, or IPv4's written as IPv6. */
11
+ export declare function loopbackAddress(address: string | undefined): boolean;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Where a request came from, as the socket says: the peer address the
3
+ * listener saw (Oxian's adapters keep it on the request they make, from the
4
+ * socket: `getClientAddress`), never a header anyone may send. What the web
5
+ * host goes by for what only this machine may do (`local` sign-in) and, for
6
+ * an app without a front that says who calls, what a web line counts its
7
+ * visitors by.
8
+ *
9
+ * A request the host makes from another (the orgs host's, for an org) keeps
10
+ * the address (`carry`). An app serving with a listener of its own sets it
11
+ * (`setClientAddress(request, info.remoteAddr.hostname)` with `Deno.serve`);
12
+ * without one, a request comes from nowhere known: never this machine, and
13
+ * no visitor is let in by address.
14
+ */
15
+ import { getClientAddress } from "oxianjs/http";
16
+ /** Addresses set here, for requests a listener didn't make (or made, then the host copied). */
17
+ const set = new WeakMap();
18
+ /** The address a request came from, as its socket says; undefined when nothing says. */
19
+ export function clientAddress(request) {
20
+ return getClientAddress(request) ?? set.get(request);
21
+ }
22
+ /**
23
+ * Says where a request came from: the socket's peer address, as the app's
24
+ * own listener saw it (never a header's). Returns the request.
25
+ */
26
+ export function setClientAddress(request, address) {
27
+ set.set(request, address);
28
+ return request;
29
+ }
30
+ /** A request made from another keeps its address. */
31
+ export function carry(to, from) {
32
+ const address = clientAddress(from);
33
+ if (address !== undefined)
34
+ set.set(to, address);
35
+ return to;
36
+ }
37
+ /** Whether an address is this machine's own: 127.0.0.0/8, `::1`, or IPv4's written as IPv6. */
38
+ export function loopbackAddress(address) {
39
+ if (!address)
40
+ return false;
41
+ const plain = address.replace(/^\[|\]$/g, "").replace(/%.*$/, "").toLowerCase();
42
+ if (plain === "::1" || plain === "0:0:0:0:0:0:0:1")
43
+ return true;
44
+ const four = /^(?:::ffff:)?(\d{1,3})\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.exec(plain);
45
+ return !!four && Number(four[1]) === 127;
46
+ }
@@ -37,12 +37,17 @@
37
37
  /** Where assets are served. */
38
38
  export const ASSETS = "/assets/";
39
39
  /** What each extension is served as, and whether it's kept for good. */
40
+ /**
41
+ * The types served, and how. Fonts and their stylesheets are anyone's to load (`shared`: any
42
+ * origin may, as Google's are): an app's widget, in an outside assistant's sandbox on another
43
+ * origin, takes the app's fonts from here, and a font from another origin needs CORS.
44
+ */
40
45
  const TYPES = new Map([
41
- ["css", { type: "text/css; charset=utf-8" }],
42
- ["woff2", { type: "font/woff2", immutable: true }],
43
- ["woff", { type: "font/woff", immutable: true }],
44
- ["ttf", { type: "font/ttf", immutable: true }],
45
- ["otf", { type: "font/otf", immutable: true }],
46
+ ["css", { type: "text/css; charset=utf-8", shared: true }],
47
+ ["woff2", { type: "font/woff2", immutable: true, shared: true }],
48
+ ["woff", { type: "font/woff", immutable: true, shared: true }],
49
+ ["ttf", { type: "font/ttf", immutable: true, shared: true }],
50
+ ["otf", { type: "font/otf", immutable: true, shared: true }],
46
51
  ["svg", { type: "image/svg+xml" }],
47
52
  ["png", { type: "image/png" }],
48
53
  ]);
@@ -102,6 +107,7 @@ export function assetServer(assets = {}) {
102
107
  const headers = {
103
108
  "cache-control": file.kind.immutable ? "public, max-age=31536000, immutable" : "no-cache",
104
109
  etag: tag,
110
+ ...(file.kind.shared ? { "access-control-allow-origin": "*" } : {}),
105
111
  ...SAFE,
106
112
  };
107
113
  if (fresh(request.headers.get("if-none-match"), tag)) {