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
@@ -0,0 +1,23 @@
1
+ /**
2
+ * A map whose entries each last `ms` from when they're set (set again, from
3
+ * then): past it, one is gone to `get` at once, and from memory within a
4
+ * sweep. Every entry lasts as long, so they pass in the order they were set,
5
+ * and a sweep stops at the first that hasn't: it never walks them all. Its
6
+ * timer runs only while anything's kept, never keeps a process alive
7
+ * (unref'd), and stops on `close`.
8
+ *
9
+ * What a host keeps a while: tickets, resume credentials, counts per minute.
10
+ */
11
+ export type Expiring<K, V> = {
12
+ get(key: K): V | undefined;
13
+ set(key: K, value: V): void;
14
+ delete(key: K): void;
15
+ /** How many are kept now, past ones the next sweep drops included. */
16
+ size(): number;
17
+ /** Drops them all, and stops its timer. */
18
+ close(): void;
19
+ };
20
+ /** Entries kept `ms` each; swept every `every` ms (`ms`, up to a minute, unless said). */
21
+ export declare function expiring<K, V>(ms: number, options?: {
22
+ every?: number;
23
+ }): Expiring<K, V>;
@@ -0,0 +1,64 @@
1
+ /**
2
+ * A map whose entries each last `ms` from when they're set (set again, from
3
+ * then): past it, one is gone to `get` at once, and from memory within a
4
+ * sweep. Every entry lasts as long, so they pass in the order they were set,
5
+ * and a sweep stops at the first that hasn't: it never walks them all. Its
6
+ * timer runs only while anything's kept, never keeps a process alive
7
+ * (unref'd), and stops on `close`.
8
+ *
9
+ * What a host keeps a while: tickets, resume credentials, counts per minute.
10
+ */
11
+ // deno-lint-ignore no-explicit-any
12
+ const g = globalThis;
13
+ /** A timer that never keeps the process alive: Node's and Bun's `unref`, Deno's `unrefTimer`. */
14
+ const unref = (timer) => {
15
+ if (typeof timer.unref === "function") {
16
+ timer.unref();
17
+ }
18
+ else if (typeof timer === "number" && g.Deno?.unrefTimer)
19
+ g.Deno.unrefTimer(timer);
20
+ };
21
+ /** Entries kept `ms` each; swept every `every` ms (`ms`, up to a minute, unless said). */
22
+ export function expiring(ms, options = {}) {
23
+ const kept = new Map();
24
+ const every = options.every ?? Math.min(ms, 60_000);
25
+ let timer;
26
+ const stop = () => {
27
+ if (timer !== undefined)
28
+ clearInterval(timer);
29
+ timer = undefined;
30
+ };
31
+ const sweep = () => {
32
+ const now = Date.now();
33
+ for (const [key, entry] of kept) {
34
+ if (entry.until > now)
35
+ break;
36
+ kept.delete(key);
37
+ }
38
+ if (!kept.size)
39
+ stop();
40
+ };
41
+ return {
42
+ get(key) {
43
+ const entry = kept.get(key);
44
+ return entry && entry.until > Date.now() ? entry.value : undefined;
45
+ },
46
+ set(key, value) {
47
+ // Set again, it goes last: the order stays the order they pass in.
48
+ kept.delete(key);
49
+ kept.set(key, { value, until: Date.now() + ms });
50
+ if (timer === undefined)
51
+ unref(timer = setInterval(sweep, every));
52
+ },
53
+ delete(key) {
54
+ kept.delete(key);
55
+ if (!kept.size)
56
+ stop();
57
+ },
58
+ size: () => kept.size,
59
+ close() {
60
+ kept.clear();
61
+ stop();
62
+ },
63
+ };
64
+ }
@@ -12,20 +12,43 @@
12
12
  * another, whatever their secrets. An app serving many orgs (`hosts/orgs`)
13
13
  * gives each org's runtime its own origin (`https://app.example/o/acme`):
14
14
  * with one origin for all, an org's links would open another's files.
15
+ *
16
+ * Nothing is signed with the secret itself: each use has a key of its own,
17
+ * derived from it (`keyOf`, HKDF): links one, web visitors' cookies another,
18
+ * so what one signs the other never takes. To change the secret, give the
19
+ * one before it as `previous` for a while: what it signed still opens (a
20
+ * link until it expires, a visitor's cookie while it lasts), and only the new
21
+ * one signs. That's for a secret changed on schedule: one that leaked is
22
+ * never given as `previous` (whoever holds it could sign anything the app
23
+ * takes while it's there). It goes at once, and what it signed with it.
15
24
  */
16
25
  import type { Resources } from "./define.js";
17
- /** The `links` resource: the app's public origin (https), and the secret links are signed with. */
26
+ /**
27
+ * The `links` resource: the app's public origin (https), the secret links
28
+ * are signed with, and the one before it (`previous`), while what it signed
29
+ * should still open: never a secret that leaked.
30
+ */
18
31
  export type LinksSettings = {
19
32
  origin: string;
20
33
  secret: string;
34
+ previous?: string;
21
35
  };
22
36
  /** The app's links settings, when it has both; a secret too short to sign with is refused. */
23
37
  export declare function linksOf(resources: Resources): LinksSettings | undefined;
38
+ /**
39
+ * The key for one use of the app's secret (`use`: "links", "visitors"), an
40
+ * HMAC-SHA256 key derived from it with HKDF-SHA256 (the use is its info):
41
+ * each use's signatures are its own, and none is made with the secret itself.
42
+ */
43
+ export declare function keyOf(secret: string, use: string): Promise<CryptoKey>;
44
+ /** The app's secrets that what it signed may be signed with: its own, then the one before it. */
45
+ export declare const secretsOf: (links: LinksSettings) => string[];
24
46
  /** A link to `path` (with `params`) at the app's origin, good for `ms` (an hour unless said). */
25
47
  export declare function signLink(links: LinksSettings, path: string, params?: Record<string, string>, ms?: number): Promise<string>;
26
48
  /**
27
- * The params of a link the app signed (`links`: its origin and secret), as
28
- * it was opened here (its path and query), when it's whole and unexpired;
29
- * else undefined. Compared in constant time (Web Crypto's `verify`).
49
+ * The params of a link the app signed (`links`: its origin and secret, or
50
+ * the one before it), as it was opened here (its path and query), when it's
51
+ * whole and unexpired; else undefined. Compared in constant time (Web
52
+ * Crypto's `verify`).
30
53
  */
31
54
  export declare function verifyLink(links: LinksSettings, path: string, query: Record<string, string>): Promise<Record<string, string> | undefined>;
package/runtime/links.js CHANGED
@@ -9,11 +9,47 @@ export function linksOf(resources) {
9
9
  const set = resources.links;
10
10
  if (!set?.origin || !set.secret)
11
11
  return undefined;
12
- if (encoder.encode(set.secret).length < SECRET) {
13
- throw invalid(`links.secret is at least ${SECRET} bytes: make a longer one.`);
12
+ for (const name of ["secret", "previous"]) {
13
+ const secret = set[name];
14
+ if (secret === undefined || (name === "previous" && !secret))
15
+ continue;
16
+ if (typeof secret !== "string" || encoder.encode(secret).length < SECRET) {
17
+ throw invalid(`links.${name} is at least ${SECRET} bytes: make a longer one.`);
18
+ }
14
19
  }
15
- return { origin: set.origin.replace(/\/+$/, ""), secret: set.secret };
20
+ return {
21
+ origin: set.origin.replace(/\/+$/, ""),
22
+ secret: set.secret,
23
+ ...(set.previous ? { previous: set.previous } : {}),
24
+ };
16
25
  }
26
+ /** Each use's key, by its secret: derived once. */
27
+ const derived = new Map();
28
+ /**
29
+ * The key for one use of the app's secret (`use`: "links", "visitors"), an
30
+ * HMAC-SHA256 key derived from it with HKDF-SHA256 (the use is its info):
31
+ * each use's signatures are its own, and none is made with the secret itself.
32
+ */
33
+ export function keyOf(secret, use) {
34
+ const id = `${use}\n${secret}`;
35
+ let key = derived.get(id);
36
+ if (!key) {
37
+ key = crypto.subtle.importKey("raw", encoder.encode(secret), "HKDF", false, ["deriveKey"])
38
+ .then((material) => crypto.subtle.deriveKey({
39
+ name: "HKDF",
40
+ hash: "SHA-256",
41
+ salt: encoder.encode("copilotz"),
42
+ info: encoder.encode(use),
43
+ }, material, { name: "HMAC", hash: "SHA-256", length: 256 }, false, ["sign", "verify"]));
44
+ derived.set(id, key);
45
+ }
46
+ return key;
47
+ }
48
+ /** The app's secrets that what it signed may be signed with: its own, then the one before it. */
49
+ export const secretsOf = (links) => [
50
+ links.secret,
51
+ ...(links.previous ? [links.previous] : []),
52
+ ];
17
53
  /**
18
54
  * What's signed: the origin, a line break (which neither an origin nor a
19
55
  * path holds), the path, then the params (`exp` among them) in order, never
@@ -21,9 +57,6 @@ export function linksOf(resources) {
21
57
  */
22
58
  const signedText = (origin, path, params) => `${origin}\n${path}?${Object.keys(params).filter((k) => k !== "sig").sort()
23
59
  .map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`).join("&")}`;
24
- const key = (secret, use) => crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, [
25
- use,
26
- ]);
27
60
  const base64url = (bytes) => btoa(String.fromCharCode(...bytes)).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
28
61
  const bytesOf = (text) => {
29
62
  try {
@@ -36,14 +69,15 @@ const bytesOf = (text) => {
36
69
  /** A link to `path` (with `params`) at the app's origin, good for `ms` (an hour unless said). */
37
70
  export async function signLink(links, path, params = {}, ms = HOUR) {
38
71
  const all = { ...params, exp: String(Math.floor((Date.now() + ms) / 1000)) };
39
- const mac = await crypto.subtle.sign("HMAC", await key(links.secret, "sign"), encoder.encode(signedText(links.origin, path, all)));
72
+ const mac = await crypto.subtle.sign("HMAC", await keyOf(links.secret, "links"), encoder.encode(signedText(links.origin, path, all)));
40
73
  const query = new URLSearchParams({ ...all, sig: base64url(new Uint8Array(mac)) });
41
74
  return `${links.origin}${path}?${query}`;
42
75
  }
43
76
  /**
44
- * The params of a link the app signed (`links`: its origin and secret), as
45
- * it was opened here (its path and query), when it's whole and unexpired;
46
- * else undefined. Compared in constant time (Web Crypto's `verify`).
77
+ * The params of a link the app signed (`links`: its origin and secret, or
78
+ * the one before it), as it was opened here (its path and query), when it's
79
+ * whole and unexpired; else undefined. Compared in constant time (Web
80
+ * Crypto's `verify`).
47
81
  */
48
82
  export async function verifyLink(links, path, query) {
49
83
  const { sig, ...params } = query;
@@ -52,7 +86,11 @@ export async function verifyLink(links, path, query) {
52
86
  if (!given || !links.secret || !Number.isFinite(exp) || exp * 1000 < Date.now()) {
53
87
  return undefined;
54
88
  }
55
- const ok = await crypto.subtle.verify("HMAC", await key(links.secret, "verify"), given, encoder.encode(signedText(links.origin, path, params)));
89
+ const text = encoder.encode(signedText(links.origin, path, params));
90
+ let ok = false;
91
+ for (const secret of secretsOf(links)) {
92
+ ok ||= await crypto.subtle.verify("HMAC", await keyOf(secret, "links"), given, text);
93
+ }
56
94
  if (!ok)
57
95
  return undefined;
58
96
  const { exp: _, ...rest } = params;
package/runtime/memory.js CHANGED
@@ -9,8 +9,42 @@ import { checkBlobId } from "./blobs.js";
9
9
  const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
10
10
  const edgeKey = (e) => `${e.from}\u0000${e.type}\u0000${e.to}`;
11
11
  const duplicateKey = (collection, id) => Object.assign(new Error(`Duplicate _id "${id}" in "${collection}"`), { code: "duplicate_key" });
12
+ /** A condition made of operators (`{ $in: … }`), as opposed to a value to equal. */
13
+ const operators = (condition) => condition !== null && typeof condition === "object" && !Array.isArray(condition);
14
+ /**
15
+ * The ids a filter keeps its documents to, when it does: an `_id` equal to
16
+ * a string (or `$eq` one), or `$in` a list of strings, at its top or in one
17
+ * of its top-level `$and` clauses. Undefined when it doesn't: every document
18
+ * is looked at. Either way, what's found must still match the whole filter.
19
+ */
20
+ function pinned(filter) {
21
+ const condition = filter._id;
22
+ if (typeof condition === "string")
23
+ return [condition];
24
+ if (operators(condition)) {
25
+ if (typeof condition.$eq === "string")
26
+ return [condition.$eq];
27
+ const list = condition.$in;
28
+ if (Array.isArray(list) && list.every((id) => typeof id === "string"))
29
+ return list;
30
+ }
31
+ const clauses = filter.$and;
32
+ for (const clause of Array.isArray(clauses) ? clauses : []) {
33
+ const ids = pinned(clause);
34
+ if (ids)
35
+ return ids;
36
+ }
37
+ return undefined;
38
+ }
12
39
  export function memoryStore() {
13
40
  const collections = new Map();
41
+ /**
42
+ * Where each committed document stands in its collection's order (its
43
+ * Map's): numbered when its id comes in, forgotten when it goes. Documents
44
+ * found by id are put back in that order.
45
+ */
46
+ const places = new Map();
47
+ let placed = 0;
14
48
  const edges = new Map();
15
49
  const blobs = new Map();
16
50
  // By collection, document and field.
@@ -21,21 +55,62 @@ export function memoryStore() {
21
55
  const positions = new Map();
22
56
  let queue = Promise.resolve();
23
57
  const reader = (layer) => {
24
- const docsOf = (collection) => {
25
- const merged = new Map(collections.get(collection) ?? []);
26
- for (const [id, doc] of layer?.docs.get(collection) ?? []) {
27
- if (doc)
28
- merged.set(id, doc);
29
- else
30
- merged.delete(id);
58
+ /**
59
+ * A collection's documents as the layer leaves them, in order: the
60
+ * committed ones (one the layer changed, in its place; one it deleted,
61
+ * gone), then those only the layer has, as it made them. Nothing copied.
62
+ */
63
+ function* docsOf(collection) {
64
+ const committed = collections.get(collection);
65
+ const staged = layer?.docs.get(collection);
66
+ for (const [id, doc] of committed ?? []) {
67
+ if (!staged?.has(id))
68
+ yield doc;
69
+ else if (staged.get(id))
70
+ yield staged.get(id);
31
71
  }
32
- return [...merged.values()];
72
+ for (const [id, doc] of staged ?? [])
73
+ if (doc && !committed?.has(id))
74
+ yield doc;
75
+ }
76
+ /** The documents there of these ids, each looked up (the layer first), in the collection's order. */
77
+ const byIds = (collection, ids) => {
78
+ const committed = collections.get(collection);
79
+ const staged = layer?.docs.get(collection);
80
+ const found = [...new Set(ids)].flatMap((id) => {
81
+ const doc = staged?.has(id) ? staged.get(id) : committed?.get(id);
82
+ return doc ? [{ id, doc }] : [];
83
+ });
84
+ if (found.length < 2)
85
+ return found.map((f) => f.doc);
86
+ // Committed ones where they stand; those only the layer has after them, in its order.
87
+ const order = places.get(collection);
88
+ const late = new Map([...staged?.keys() ?? []].map((id, i) => [id, i]));
89
+ const placeOf = (id) => order?.get(id) ?? placed + 1 + late.get(id);
90
+ return found.sort((a, b) => placeOf(a.id) - placeOf(b.id)).map((f) => f.doc);
33
91
  };
34
- const find = async (collection, filter = {}, options = {}) => {
92
+ /**
93
+ * What a filter matches, sorted and paged (the stored documents, not
94
+ * copies): by id when it keeps to some, else looking at every one;
95
+ * without a sort, it stops once the page is full.
96
+ */
97
+ const matching = (collection, filter = {}, options = {}) => {
35
98
  check(filter);
36
- return await Promise.resolve(sortAndPage(docsOf(collection).filter((doc) => matches(doc, filter)), options)
37
- .map((doc) => structuredClone(doc)));
99
+ const ids = pinned(filter);
100
+ const enough = !options.sort && options.limit !== undefined
101
+ ? (options.skip ?? 0) + options.limit
102
+ : Infinity;
103
+ const found = [];
104
+ for (const doc of ids ? byIds(collection, ids) : docsOf(collection)) {
105
+ if (found.length >= enough)
106
+ break;
107
+ if (matches(doc, filter))
108
+ found.push(doc);
109
+ }
110
+ return sortAndPage(found, options);
38
111
  };
112
+ // Async, as every store's: a filter it can't run rejects, never throws.
113
+ const find = async (collection, filter = {}, options = {}) => await Promise.resolve(matching(collection, filter, options).map((doc) => structuredClone(doc)));
39
114
  const vectorOf = (key) => layer?.vectors.has(key) ? layer.vectors.get(key) ?? undefined : vectors.get(key);
40
115
  return {
41
116
  find,
@@ -50,7 +125,7 @@ export function memoryStore() {
50
125
  return scored.sort((a, b) => b._score - a._score).slice(0, options.limit ?? 10);
51
126
  },
52
127
  findOne: async (collection, filter, options) => (await find(collection, filter, { ...options, limit: 1 }))[0] ?? null,
53
- count: async (collection, filter) => (await find(collection, filter)).length,
128
+ count: async (collection, filter) => await Promise.resolve(matching(collection, filter).length),
54
129
  // The reference itself, over what the filter finds.
55
130
  aggregate: async (collection, plan) => aggregate(await find(collection, plan.match), plan),
56
131
  edges: (query) => {
@@ -207,11 +282,23 @@ export function memoryStore() {
207
282
  if (owner !== epoch)
208
283
  throw fenced();
209
284
  for (const [collection, docs] of layer.docs) {
210
- if (!collections.has(collection))
285
+ if (!collections.has(collection)) {
211
286
  collections.set(collection, new Map());
287
+ places.set(collection, new Map());
288
+ }
212
289
  const target = collections.get(collection);
213
- for (const [id, doc] of docs)
214
- doc ? target.set(id, doc) : target.delete(id);
290
+ const order = places.get(collection);
291
+ for (const [id, doc] of docs) {
292
+ if (!doc) {
293
+ target.delete(id);
294
+ order.delete(id);
295
+ continue;
296
+ }
297
+ // A new id goes last, as a Map puts it; one already there keeps its place.
298
+ if (!target.has(id))
299
+ order.set(id, ++placed);
300
+ target.set(id, doc);
301
+ }
215
302
  }
216
303
  for (const [key, edge] of layer.edges)
217
304
  edge ? edges.set(key, edge) : edges.delete(key);
package/runtime/mod.d.ts CHANGED
@@ -2,12 +2,14 @@ export * from "./define.js";
2
2
  export { createRuntime, type Runtime } from "./runtime.js";
3
3
  export { createOwner, type Description, type Feed, type Owner, type Stats, type Transport, } from "./owner.js";
4
4
  export { createExecutor, type Executor } from "./executor.js";
5
+ export { type BrowserLimits, CONTACT_READ, type ContactsPool, contactsPool, LISTENER, } from "./browsers.js";
5
6
  export type { Observation, Observed, ObserveOptions, Output } from "./observe.js";
6
7
  export type { Collection, Cursor, DeleteResult, Unacknowledged, UpdateResult, WithId, WriteOptions, } from "./collection.js";
7
8
  export type { Actor, Doc, Edge, Event, Executed, Inspected, Row, Store } from "./store.js";
8
9
  export { type Maintenance, MAINTENANCE_SQL, redacted, type Written } from "./maintain.js";
9
10
  export { personOf, principalOf } from "./actor.js";
10
- export { linksOf, type LinksSettings, signLink, verifyLink } from "./links.js";
11
+ export { keyOf, linksOf, type LinksSettings, secretsOf, signLink, verifyLink } from "./links.js";
12
+ export { type Caller, callerFrom, callerProblem, type CallerSettings, countedOf, type Network, networksOf, privateAddress, SCALE, scaleOf, UNVOUCHED, } from "./caller.js";
11
13
  export { contacts, failure, type FailureOptions, forbidden, inPerson, invalid, isPublic, people, peopleOrContacts, } from "./errors.js";
12
14
  export type { Accumulator, Expr, Pipeline, Stage } from "./aggregate.js";
13
15
  export { type Attributes, BUCKETS, type Counter, createMetrics, type Exporter, type Gauge, type Histogram, type Metric, type Metrics, noMetrics, type Point, type Snapshot, } from "./metrics.js";
package/runtime/mod.js CHANGED
@@ -2,9 +2,11 @@ export * from "./define.js";
2
2
  export { createRuntime } from "./runtime.js";
3
3
  export { createOwner, } from "./owner.js";
4
4
  export { createExecutor } from "./executor.js";
5
+ export { CONTACT_READ, contactsPool, LISTENER, } from "./browsers.js";
5
6
  export { MAINTENANCE_SQL, redacted } from "./maintain.js";
6
7
  export { personOf, principalOf } from "./actor.js";
7
- export { linksOf, signLink, verifyLink } from "./links.js";
8
+ export { keyOf, linksOf, secretsOf, signLink, verifyLink } from "./links.js";
9
+ export { callerFrom, callerProblem, countedOf, networksOf, privateAddress, SCALE, scaleOf, UNVOUCHED, } from "./caller.js";
8
10
  export { contacts, failure, forbidden, inPerson, invalid, isPublic, people, peopleOrContacts, } from "./errors.js";
9
11
  export { BUCKETS, createMetrics, noMetrics, } from "./metrics.js";
10
12
  export { manualClock, systemClock, TIMERS, } from "./schedules.js";
@@ -23,6 +23,18 @@ export declare function get(doc: unknown, path: string): unknown;
23
23
  * document, as the SQL stores do when they translate the filter.
24
24
  */
25
25
  export declare function check(filter: Filter): void;
26
+ /**
27
+ * What a filter names, if anything: a field of its own (top-level) it
28
+ * matches by equality to some strings (a chat's `thread`, an `_id`), the
29
+ * fewest. A document it matches has one of them there (`valuesAt`), so a
30
+ * change to any other can't concern it.
31
+ */
32
+ export declare function namedBy(filter: Filter): {
33
+ field: string;
34
+ values: string[];
35
+ } | undefined;
36
+ /** The strings a document has in a field of its own: its value, or those of an array there. */
37
+ export declare function valuesAt(doc: unknown, field: string): string[];
26
38
  export declare function matches(doc: unknown, filter?: Filter): boolean;
27
39
  /** Orders documents by a MongoDB sort; ties compare equal (callers keep insertion order). */
28
40
  export declare const compareBy: (sort?: Record<string, 1 | -1>) => (a: unknown, b: unknown) => number;
package/runtime/mongo.js CHANGED
@@ -186,6 +186,50 @@ export function check(filter) {
186
186
  condition(value);
187
187
  }
188
188
  }
189
+ /**
190
+ * The strings a condition equals (a string, `$eq`, `$in` of strings): a
191
+ * document it matches has one of them in that field, or in an array there.
192
+ */
193
+ const equalTo = (condition) => {
194
+ if (typeof condition === "string")
195
+ return [condition];
196
+ if (!condition || typeof condition !== "object" || Array.isArray(condition))
197
+ return undefined;
198
+ if (Object.keys(condition).length !== 1)
199
+ return undefined;
200
+ const { $eq, $in } = condition;
201
+ if (typeof $eq === "string")
202
+ return [$eq];
203
+ return Array.isArray($in) && $in.every((v) => typeof v === "string") ? $in : undefined;
204
+ };
205
+ /** The most values a filter is known by (`namedBy`): a longer `$in` names nothing. */
206
+ const NAMED_MOST = 256;
207
+ /**
208
+ * What a filter names, if anything: a field of its own (top-level) it
209
+ * matches by equality to some strings (a chat's `thread`, an `_id`), the
210
+ * fewest. A document it matches has one of them there (`valuesAt`), so a
211
+ * change to any other can't concern it.
212
+ */
213
+ export function namedBy(filter) {
214
+ let best;
215
+ for (const [field, condition] of Object.entries(filter)) {
216
+ if (field.startsWith("$") || field.includes("."))
217
+ continue;
218
+ const values = equalTo(condition);
219
+ if (!values || values.length > NAMED_MOST)
220
+ continue;
221
+ if (!best || values.length < best.values.length)
222
+ best = { field, values };
223
+ }
224
+ return best;
225
+ }
226
+ /** The strings a document has in a field of its own: its value, or those of an array there. */
227
+ export function valuesAt(doc, field) {
228
+ const value = doc[field];
229
+ if (typeof value === "string")
230
+ return [value];
231
+ return Array.isArray(value) ? value.filter((v) => typeof v === "string") : [];
232
+ }
189
233
  export function matches(doc, filter = {}) {
190
234
  return Object.entries(filter).every(([key, condition]) => {
191
235
  if (key === "$and")
@@ -17,7 +17,7 @@
17
17
  * and the live text it replaces never show both, or neither). One that takes
18
18
  * too long (`WAVE_MS`) doesn't hold the others back.
19
19
  */
20
- import { matches } from "./mongo.js";
20
+ import { matches, namedBy, valuesAt } from "./mongo.js";
21
21
  import { invalid } from "./registry.js";
22
22
  import { failure, PASSES, reread, viewCollections, viewOf } from "./views.js";
23
23
  /** The longest the parts of one commit wait for each other before telling what they have. */
@@ -61,6 +61,53 @@ const idOf = (item) => typeof item === "object" && item && typeof item._id === "
61
61
  export function createObservers(env) {
62
62
  /** Parts by the collections they read. */
63
63
  const readers = new Map();
64
+ /** Of those, parts with a read of the collection that names nothing (`namedBy`). */
65
+ const wide = new Map();
66
+ /** Parts with a read that names values of a field: by collection, field and value. */
67
+ const named = new Map();
68
+ /** Files (or takes out) a part under what one of its reads names, or with the wide ones. */
69
+ const file = (p, dep, add) => {
70
+ const by = dep.filter && namedBy(dep.filter);
71
+ if (!by) {
72
+ if (add) {
73
+ if (!wide.has(dep.collection))
74
+ wide.set(dep.collection, new Set());
75
+ wide.get(dep.collection).add(p);
76
+ }
77
+ else {
78
+ wide.get(dep.collection)?.delete(p);
79
+ if (wide.get(dep.collection)?.size === 0)
80
+ wide.delete(dep.collection);
81
+ }
82
+ return;
83
+ }
84
+ if (add && !named.has(dep.collection))
85
+ named.set(dep.collection, new Map());
86
+ const fields = named.get(dep.collection);
87
+ if (!fields)
88
+ return;
89
+ if (add && !fields.has(by.field))
90
+ fields.set(by.field, new Map());
91
+ const values = fields.get(by.field);
92
+ if (!values)
93
+ return;
94
+ for (const value of by.values) {
95
+ if (add) {
96
+ if (!values.has(value))
97
+ values.set(value, new Set());
98
+ values.get(value).add(p);
99
+ }
100
+ else {
101
+ values.get(value)?.delete(p);
102
+ if (values.get(value)?.size === 0)
103
+ values.delete(value);
104
+ }
105
+ }
106
+ if (!values.size)
107
+ fields.delete(by.field);
108
+ if (!fields.size)
109
+ named.delete(dep.collection);
110
+ };
64
111
  // Runs in flight, for `idle`.
65
112
  let running = 0;
66
113
  let settled = Promise.withResolvers();
@@ -132,7 +179,7 @@ export function createObservers(env) {
132
179
  const context = (p, run, db, kind, hints) => ({
133
180
  db,
134
181
  resources: run.source.resources,
135
- renderer: p.medium,
182
+ medium: p.medium,
136
183
  kind,
137
184
  ...(p.viewer ? { viewer: p.viewer } : {}),
138
185
  ...(p.actor ? { actor: p.actor } : {}),
@@ -239,13 +286,16 @@ export function createObservers(env) {
239
286
  };
240
287
  const depend = (p, deps) => {
241
288
  const before = p.deps;
242
- for (const { collection } of before)
243
- readers.get(collection)?.delete(p);
289
+ for (const dep of before) {
290
+ readers.get(dep.collection)?.delete(p);
291
+ file(p, dep, false);
292
+ }
244
293
  p.deps = deps;
245
- for (const { collection } of deps) {
246
- if (!readers.has(collection))
247
- readers.set(collection, new Set());
248
- readers.get(collection).add(p);
294
+ for (const dep of deps) {
295
+ if (!readers.has(dep.collection))
296
+ readers.set(dep.collection, new Set());
297
+ readers.get(dep.collection).add(p);
298
+ file(p, dep, true);
249
299
  }
250
300
  // What nothing reads now.
251
301
  for (const { collection } of before) {
@@ -359,10 +409,33 @@ export function createObservers(env) {
359
409
  changed(seen) {
360
410
  const due = new Set();
361
411
  for (const [collection, changes] of seen) {
362
- for (const p of readers.get(collection) ?? []) {
412
+ // Only parts a change could concern are matched against it: those whose reads name
413
+ // nothing, and those naming what a changed document has (before or after).
414
+ const looked = new Set();
415
+ const look = (p) => {
416
+ if (looked.has(p))
417
+ return;
418
+ looked.add(p);
363
419
  if (p.deps.some((dep) => dep.collection === collection && touches(dep, changes))) {
364
420
  due.add(p);
365
421
  }
422
+ };
423
+ for (const p of wide.get(collection) ?? [])
424
+ look(p);
425
+ const fields = named.get(collection);
426
+ if (!fields)
427
+ continue;
428
+ for (const { before, after } of changes) {
429
+ for (const doc of [before, after]) {
430
+ if (!doc)
431
+ continue;
432
+ for (const [field, values] of fields) {
433
+ for (const value of valuesAt(doc, field)) {
434
+ for (const p of values.get(value) ?? [])
435
+ look(p);
436
+ }
437
+ }
438
+ }
366
439
  }
367
440
  }
368
441
  again(due);
package/runtime/ops.d.ts CHANGED
@@ -121,7 +121,15 @@ export type CancelOp = {
121
121
  processor: string;
122
122
  key: string;
123
123
  };
124
- export type Op = StoreOp | TimerOp | CancelOp;
124
+ /** Ends an actor's browsers (see `Owner.disconnect`). */
125
+ export type DisconnectOp = {
126
+ op: "disconnect";
127
+ actor: {
128
+ kind: string;
129
+ id: string;
130
+ };
131
+ };
132
+ export type Op = StoreOp | TimerOp | CancelOp | DisconnectOp;
125
133
  export declare const isRead: (op: Op) => op is ReadOp;
126
134
  /** Runs a read against committed state, or inside a transaction against its own writes. */
127
135
  export declare function read(reader: Reader, op: ReadOp): Promise<unknown>;