@vercel/board 0.32.0 → 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -20
- package/dist/{access-CY5xVMIK.mjs → access-CdBcYPQH.mjs} +170 -13
- package/dist/access.d.mts +4 -5
- package/dist/access.mjs +5 -5
- package/dist/{actors-Bm-ojWWT.mjs → actors-MrIEr9LI.mjs} +3 -3
- package/dist/auth-DjoJygbG.mjs +599 -0
- package/dist/{auth-K8dY1nBY.d.mts → auth-aaoKt5DK.d.mts} +133 -100
- package/dist/backend-local.d.mts +28 -0
- package/dist/backend-local.mjs +2 -0
- package/dist/{backend-DxE9wGKm.d.mts → backend-u1sVvybh.d.mts} +108 -55
- package/dist/{chat-provider-DkFp2hrh.mjs → chat-provider-9hCT654s.mjs} +2 -2
- package/dist/{chat-provider-BChYrrA7.d.mts → chat-provider-De0iDnUB.d.mts} +2 -3
- package/dist/cli-commands.d.mts +1 -1
- package/dist/cli-commands.mjs +1 -1
- package/dist/cli-embedded.d.mts +2 -2
- package/dist/cli-embedded.mjs +1 -1
- package/dist/cli.mjs +6 -6
- package/dist/{commands-DsnQKTdB.mjs → commands-B8oZEByR.mjs} +37 -52
- package/dist/{commands-CpyPV_tk.d.mts → commands-BkVMdXP1.d.mts} +9 -6
- package/dist/{conformance-3RBKxwbp.mjs → conformance-gqYBkXFS.mjs} +79 -86
- package/dist/conformance.d.mts +1 -1
- package/dist/conformance.mjs +1 -1
- package/dist/{engine-DurYIRt8.mjs → engine-rtOVWsiF.mjs} +6 -8
- package/dist/{errors-CJXhhzvT.mjs → errors-D8ifv16L.mjs} +75 -6
- package/dist/{events-lujPA3gQ.mjs → events-IQg0k281.mjs} +2 -2
- package/dist/exports-Bx2dGpzr.mjs +3252 -0
- package/dist/exports-uwRl5qIj.d.mts +4 -0
- package/dist/{external-D3p5T57R.mjs → external-CYe424fO.mjs} +1 -1
- package/dist/index-D6mK0Ial.d.mts +11 -0
- package/dist/index.d.mts +10 -9
- package/dist/index.mjs +21 -8
- package/dist/index.node.d.mts +6 -7
- package/dist/index.node.mjs +3 -3
- package/dist/inspect/app.js +95 -95
- package/dist/{internal-CvsrprnU-zZMpDiRY.mjs → internal-CvsrprnU-CEPOUpAv.mjs} +63 -43
- package/dist/local-CPz22MQ8.mjs +9225 -0
- package/dist/{main-CcYDo9Hb.mjs → main-DScDKwQN.mjs} +129 -54
- package/dist/{backend-a2/store-memory.mjs → memory-7RRnLa4X.mjs} +3 -3
- package/dist/middleware-BjYpk4yq.mjs +103 -0
- package/dist/{provider-gynDUoyW.d.mts → provider-hB8QNrwm.d.mts} +369 -3
- package/dist/{client-COfYrgfV.d.mts → routes-De4W6lEs.d.mts} +997 -367
- package/dist/{schemas-DBmUO6pr.mjs → schemas-BG205xds.mjs} +2 -5
- package/dist/{serve-Bplieh0C.mjs → serve-B1noQRkn.mjs} +33 -24
- package/dist/{node-BgeGcwX0.mjs → server-Ov6GBJ8j.mjs} +419 -6
- package/dist/{serving-Y7s7VDjB.mjs → serving-2s7lqSBS.mjs} +1 -1
- package/dist/{sql-E1K5g_ih.mjs → sql-BQzKUqQt.mjs} +349 -114
- package/dist/sqlite-DCtZFEVm.mjs +30 -0
- package/dist/{sqlite-db-CzcD_FW8.mjs → sqlite-db-qLTs52Ny.mjs} +2 -3
- package/dist/{store-codec-pAG2oPgQ-SqDXlPBR.mjs → store-N8PXxDAS-BdL4Sq-f.mjs} +6 -2
- package/dist/{store-memory-B0PkVSuQ.mjs → store-memory-CKQVEzXN.mjs} +2 -4
- package/dist/store-memory-OdGsc-t_.mjs +2 -0
- package/dist/{store-polling-BZt2Rf3D-Div-bJ_D.mjs → store-polling-BZt2Rf3D-oh3aU_An.mjs} +1 -1
- package/dist/store-sqlite-CTxAVCcz.mjs +2 -0
- package/dist/{store-sqlite-D85t3tE7.mjs → store-sqlite-D6fIszST.mjs} +3 -4
- package/dist/sync-chat-sdk.d.mts +2 -2
- package/dist/sync-chat-sdk.mjs +2 -2
- package/dist/sync-github.d.mts +2 -2
- package/dist/sync-github.mjs +2 -2
- package/dist/sync-linear.d.mts +3 -3
- package/dist/sync-linear.mjs +2 -2
- package/dist/sync-slack.d.mts +3 -3
- package/dist/sync-slack.mjs +4 -4
- package/dist/sync.d.mts +2 -2
- package/dist/sync.mjs +2 -2
- package/dist/{watchers-hash-CurCzwhe.mjs → targets-aMdbp84G.mjs} +134 -16
- package/dist/{tokens-C7kYyrAG.mjs → tokens-LHLrnV_P.mjs} +15 -43
- package/docs/01-board/01-mounting.md +245 -288
- package/docs/01-board/03-identity.md +42 -50
- package/docs/01-board/04-messages.md +15 -14
- package/docs/01-board/05-reading.md +3 -3
- package/docs/01-board/06-watchers.md +58 -105
- package/docs/01-board/07-schedules.md +21 -41
- package/docs/01-board/08-middleware.md +11 -17
- package/docs/01-board/09-access.md +187 -343
- package/docs/01-board/10-local.md +81 -0
- package/docs/01-quickstart.md +49 -126
- package/docs/02-sync/01-overview.md +25 -80
- package/docs/02-sync/02-functions.md +10 -16
- package/docs/02-sync/03-slack.md +13 -26
- package/docs/02-sync/04-github.md +8 -20
- package/docs/02-sync/07-linear.md +6 -10
- package/docs/03-guides/02-inspector.md +3 -3
- package/docs/03-guides/03-ci.md +88 -128
- package/docs/03-guides/04-triage.md +7 -15
- package/docs/03-guides/05-agent-factory.md +29 -36
- package/docs/04-reference/01-cli.md +113 -38
- package/docs/04-reference/02-board.md +79 -82
- package/docs/04-reference/03-http.md +138 -69
- package/docs/04-reference/05-conformance.md +32 -46
- package/docs/index.md +41 -65
- package/package.json +12 -83
- package/dist/a2-DiDc77Uf.mjs +0 -4238
- package/dist/access-DPcrKIH_.d.mts +0 -37
- package/dist/auth-8pRZnF1M.mjs +0 -393
- package/dist/backend-a2/blob-vercel.d.mts +0 -8
- package/dist/backend-a2/blob-vercel.mjs +0 -81
- package/dist/backend-a2/contract.d.mts +0 -828
- package/dist/backend-a2/contract.mjs +0 -5
- package/dist/backend-a2/scheduler-local.d.mts +0 -11
- package/dist/backend-a2/scheduler-local.mjs +0 -2
- package/dist/backend-a2/scheduler-qstash.d.mts +0 -79
- package/dist/backend-a2/scheduler-qstash.mjs +0 -522
- package/dist/backend-a2/scheduler-vercel.d.mts +0 -38
- package/dist/backend-a2/scheduler-vercel.mjs +0 -204
- package/dist/backend-a2/store-memory.d.mts +0 -11
- package/dist/backend-a2/store-postgres.d.mts +0 -38
- package/dist/backend-a2/store-postgres.mjs +0 -2977
- package/dist/backend-a2/store-redis-http.d.mts +0 -17
- package/dist/backend-a2/store-redis-http.mjs +0 -520
- package/dist/backend-a2/store-redis.d.mts +0 -31
- package/dist/backend-a2/store-redis.mjs +0 -88
- package/dist/backend-a2/store-sqlite.d.mts +0 -12
- package/dist/backend-a2/store-sqlite.mjs +0 -31
- package/dist/backend-a2.d.mts +0 -2633
- package/dist/backend-a2.mjs +0 -3
- package/dist/backend-http-CaN06CaD.d.mts +0 -672
- package/dist/backend-http-CoaCghuJ.mjs +0 -18
- package/dist/backend-http-l5A8UmL-.mjs +0 -585
- package/dist/backend-http.d.mts +0 -10
- package/dist/backend-http.mjs +0 -2
- package/dist/backend-http.node.d.mts +0 -10
- package/dist/backend-http.node.mjs +0 -2
- package/dist/backend-vercel-BE65okBC.mjs +0 -86
- package/dist/backend-vercel-CVxoTNF2.mjs +0 -1744
- package/dist/backend-vercel-jg-6PAgD.d.mts +0 -674
- package/dist/backend-vercel.d.mts +0 -9
- package/dist/backend-vercel.mjs +0 -11
- package/dist/backend-vercel.node.d.mts +0 -17
- package/dist/backend-vercel.node.mjs +0 -2
- package/dist/blob-Bx8QPWEd.d.mts +0 -40
- package/dist/blob-WbB2DJKn.mjs +0 -201
- package/dist/errors-2vGs49Db.d.mts +0 -16
- package/dist/exports-BnUZuRm9.mjs +0 -1389
- package/dist/exports-u5mApVvn.d.mts +0 -5
- package/dist/files-SniSmeEr.mjs +0 -47
- package/dist/folds-DLUzfibJ.d.mts +0 -558
- package/dist/folds-DsWfIftc.mjs +0 -208
- package/dist/index-UXpTPWNc.d.mts +0 -11
- package/dist/index.default-Dp8gexln.mjs +0 -12
- package/dist/local-C76Fy5ad.mjs +0 -44
- package/dist/middleware-BavYSDUa.mjs +0 -62
- package/dist/platform-B4TnJtWu-Do3agKr5.mjs +0 -32
- package/dist/project-4Vl7_uy5.mjs +0 -124
- package/dist/redis-tables-BFRj_SEJ.mjs +0 -3895
- package/dist/reducer-Bswr11ar-P324o9vn.mjs +0 -74
- package/dist/reducer-CYPHsSmd-BXjGiptm.d.mts +0 -170
- package/dist/reducers-DQVSP3kk.mjs +0 -538
- package/dist/retryable-lazy-DZWmHpii-DpTc5PIP.mjs +0 -19
- package/dist/scheduler-task-X1SToDjP-D2Il2rln.mjs +0 -69
- package/dist/server-B9gu0qi1-DI1WvcVK.d.mts +0 -391
- package/dist/server-DX7mM6nm-D7N_7bug.mjs +0 -3611
- package/dist/server-wIpAEjpX.mjs +0 -17
- package/dist/session-clear-XJYpmUpg-2q8l_FJR.mjs +0 -63
- package/dist/store-DEzcHZOA-B-5vhxef.d.mts +0 -540
- package/dist/store-N8PXxDAS-DWWgv-Vz.mjs +0 -5
- package/dist/store-memory-4IBl0Ztu.mjs +0 -2
- package/dist/store-presence-polling-JuAy2Ck2-Dh-g_KYM.mjs +0 -109
- package/dist/store-sqlite-iBTYYKBj.mjs +0 -2
- package/dist/stream-buffer-DrTyiIuo-Be10yOBR.mjs +0 -53
- package/dist/targets-Cgjs29T0.mjs +0 -33
- package/dist/types-CmrFjpml.d.mts +0 -102
- package/dist/wait-until-Dw4XrIDQ.mjs +0 -42
- package/docs/01-board/02-database.md +0 -258
- package/docs/04-reference/04-log.md +0 -247
- package/sql/postgres/board.sql +0 -145
- package/sql/postgres/notifications.sql +0 -24
- package/sql/postgres/schema.sql +0 -451
|
@@ -1,1389 +0,0 @@
|
|
|
1
|
-
import { t as ATTACHMENT_BYTES_MAX } from "./files-SniSmeEr.mjs";
|
|
2
|
-
import "./schemas-DBmUO6pr.mjs";
|
|
3
|
-
import { A as RegisterBody, B as ThreadsQuery, C as LabelBody, D as PostBody, E as MessagesQuery, F as SyncBody, H as TypingBody, I as SyncCopiesQuery, L as SyncFindQuery, M as SchedulesQuery, N as SourceJoinBody, O as ReactBody, P as SourceReadQuery, R as SyncImportBody, S as InboxQuery, T as MatchesQuery, U as UploadStartBody, V as TokenBody, _ as EventsQuery, a as METHODS, b as InboxNextQuery, c as queryObject, d as VERSION, f as SYNC, g as EditBody, h as DeleteBody, j as ScheduleBody, k as Ref, l as readCapped, m as CreateChannelBody, o as createRouter, p as AckBody, s as openapi, u as readJson, v as GrantBody, w as LinkedQuery, x as InboxPushBody, y as InboxAckBody, z as SyncReceiptBody } from "./watchers-hash-CurCzwhe.mjs";
|
|
4
|
-
import { a as forbidden, c as toErrorResponse, d as createBoardClient, l as unauthorized, n as RejectedError, o as json, r as badRequest, s as notFound, t as HttpError, u as BoardError } from "./errors-CJXhhzvT.mjs";
|
|
5
|
-
import { r as derivedId } from "./wait-until-Dw4XrIDQ.mjs";
|
|
6
|
-
import { a as scopeOf, i as parseScope, r as compileAccess } from "./access-CY5xVMIK.mjs";
|
|
7
|
-
import { i as resolveActor, r as claimsOf, t as KNOWN_AGENTS } from "./actors-Bm-ojWWT.mjs";
|
|
8
|
-
import { S as verifiesTokens, a as VERCEL_ISSUER, f as grantIssuers, h as issuersOf, l as createAuthority, p as hostPrincipal, u as fromTokens, y as tokenHash } from "./auth-8pRZnF1M.mjs";
|
|
9
|
-
import { r as runMiddleware, t as DroppedError } from "./middleware-BavYSDUa.mjs";
|
|
10
|
-
import { n as syncTarget, r as withEmailMentions, t as mentionsEmails } from "./targets-Cgjs29T0.mjs";
|
|
11
|
-
//#region src/server/policy.ts
|
|
12
|
-
/**
|
|
13
|
-
* The policy a board's options say: verifying requests, and `access` over them and over the
|
|
14
|
-
* principals its backend stores. `keeps`: the backend keeps the board, so the defaults let its
|
|
15
|
-
* people and own deployments in when `access` is left out.
|
|
16
|
-
*/
|
|
17
|
-
function createPolicy(options, keeps, serving, trust = null) {
|
|
18
|
-
/** Whether requests are served at all: a board on `http()` serves none without an `access` of its own. */
|
|
19
|
-
const open = keeps || options.access !== void 0;
|
|
20
|
-
const access = options.access ?? [];
|
|
21
|
-
const grants = trust && typeof access !== "function" ? () => trust.grants() : null;
|
|
22
|
-
/** The board's own Vercel project and team, from `project` or the environment, asked once. */
|
|
23
|
-
let own = null;
|
|
24
|
-
const project = () => (own ??= (async () => {
|
|
25
|
-
if (!serving) return {
|
|
26
|
-
id: options.project?.id ?? null,
|
|
27
|
-
teamId: options.project?.teamId ?? null
|
|
28
|
-
};
|
|
29
|
-
const found = await serving();
|
|
30
|
-
const [id, teamId] = await Promise.all([found.ownProjectId(options.project), found.ownTeamId(options.project)]);
|
|
31
|
-
return {
|
|
32
|
-
id,
|
|
33
|
-
teamId
|
|
34
|
-
};
|
|
35
|
-
})()).catch((error) => {
|
|
36
|
-
own = null;
|
|
37
|
-
throw error;
|
|
38
|
-
});
|
|
39
|
-
/** What discovery says the board's project is, as a request shows it: clients mint tokens with it. */
|
|
40
|
-
async function discoveryIdentity(headers) {
|
|
41
|
-
if (!serving) return {
|
|
42
|
-
projectId: options.project?.id ?? null,
|
|
43
|
-
teamId: options.project?.teamId ?? null
|
|
44
|
-
};
|
|
45
|
-
return (await serving()).discoveryIdentity(headers, options.project);
|
|
46
|
-
}
|
|
47
|
-
let tokens = null;
|
|
48
|
-
/** The verifier: `authenticate`, or `oidcTokens()` where this build has it. */
|
|
49
|
-
const verifier = () => options.authenticate ? Promise.resolve(options.authenticate) : tokens ??= serving ? serving().then((found) => found.oidcTokens()) : Promise.reject(new HttpError(501, "no_verifier", "this build of board verifies no tokens: pass board({ authenticate })"));
|
|
50
|
-
const authority = createAuthority({
|
|
51
|
-
access,
|
|
52
|
-
project,
|
|
53
|
-
hosts: options.authenticate !== void 0 && !verifiesTokens(options.authenticate),
|
|
54
|
-
grants
|
|
55
|
-
});
|
|
56
|
-
const ruleIssuers = typeof access === "function" ? [] : issuersOf(access);
|
|
57
|
-
/** One for every request, so a verifier keeps what it built: its issuers change only when grants name new ones. */
|
|
58
|
-
const context = {
|
|
59
|
-
project,
|
|
60
|
-
issuers: ruleIssuers
|
|
61
|
-
};
|
|
62
|
-
/**
|
|
63
|
-
* Trusts the issuers grants name, before a token from one is verified: only for a token from an
|
|
64
|
-
* issuer neither Vercel, GitHub, nor a rule is, so most requests read no grants for it.
|
|
65
|
-
*/
|
|
66
|
-
async function learnIssuers(credential) {
|
|
67
|
-
if (!grants) return;
|
|
68
|
-
const iss = issuerOf(credential);
|
|
69
|
-
if (iss === null || !iss.startsWith("https://") || iss === "https://token.actions.githubusercontent.com" || VERCEL_ISSUER.test(iss) || ruleIssuers.includes(iss)) return;
|
|
70
|
-
const named = grantIssuers(await grants());
|
|
71
|
-
const issuers = [...ruleIssuers, ...named.filter((issuer) => !ruleIssuers.includes(issuer))];
|
|
72
|
-
if (issuers.join("\n") !== context.issuers.join("\n")) context.issuers = issuers;
|
|
73
|
-
}
|
|
74
|
-
/** Who a board token is: the principal that minted it, narrowed, until it's revoked or expires. */
|
|
75
|
-
async function tokenPrincipal(secret) {
|
|
76
|
-
if (!trust) throw unauthorized("this board issues no tokens: send a board token to the board that minted it");
|
|
77
|
-
const found = await trust.token(await tokenHash(secret));
|
|
78
|
-
if (!found || found.expiresAt.getTime() <= Date.now()) throw unauthorized("invalid token: revoked, expired, or not this board's");
|
|
79
|
-
return {
|
|
80
|
-
...found.minter,
|
|
81
|
-
expiresAt: found.expiresAt,
|
|
82
|
-
token: found.id,
|
|
83
|
-
pinned: found.pinned
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
/** Verified principals by credential, until they expire, so a stream's polls don't verify from scratch. */
|
|
87
|
-
const verified = /* @__PURE__ */ new Map();
|
|
88
|
-
/** And by request, for one with no credential (a session cookie), for as long as it's served. */
|
|
89
|
-
const perRequest = /* @__PURE__ */ new WeakMap();
|
|
90
|
-
/** Who a request's credential is: a 401 or the host's own rejection when it's no one. */
|
|
91
|
-
async function principalOf(request) {
|
|
92
|
-
const live = (principal) => principal.expiresAt === null || principal.expiresAt.getTime() > Date.now();
|
|
93
|
-
const known = perRequest.get(request);
|
|
94
|
-
if (known) {
|
|
95
|
-
const principal = await known;
|
|
96
|
-
if (live(principal)) return principal;
|
|
97
|
-
}
|
|
98
|
-
const key = request.credential === null ? null : [
|
|
99
|
-
request.audience,
|
|
100
|
-
request.credential,
|
|
101
|
-
request.headers.get("x-vercel-oidc-token") ?? ""
|
|
102
|
-
].join("");
|
|
103
|
-
const cached = key === null ? void 0 : verified.get(key);
|
|
104
|
-
if (cached && cached.until > Date.now()) {
|
|
105
|
-
perRequest.set(request, cached.principal);
|
|
106
|
-
return cached.principal;
|
|
107
|
-
}
|
|
108
|
-
const principal = verify(request);
|
|
109
|
-
perRequest.set(request, principal);
|
|
110
|
-
if (key !== null) principal.then((found) => {
|
|
111
|
-
if (found.facts?.issuer === "host" || found.expiresAt === null || found.token !== void 0) return;
|
|
112
|
-
if (verified.size >= 1e3) verified.delete(verified.keys().next().value);
|
|
113
|
-
verified.set(key, {
|
|
114
|
-
principal,
|
|
115
|
-
until: found.expiresAt.getTime()
|
|
116
|
-
});
|
|
117
|
-
}, () => perRequest.delete(request));
|
|
118
|
-
return principal;
|
|
119
|
-
}
|
|
120
|
-
async function verify(request) {
|
|
121
|
-
if (request.credential?.startsWith("board_")) return tokenPrincipal(request.credential);
|
|
122
|
-
if (request.credential !== null) await learnIssuers(request.credential);
|
|
123
|
-
const authenticate = await verifier();
|
|
124
|
-
let result;
|
|
125
|
-
try {
|
|
126
|
-
result = await authenticate({
|
|
127
|
-
...request,
|
|
128
|
-
board: context
|
|
129
|
-
});
|
|
130
|
-
} catch (error) {
|
|
131
|
-
if (error instanceof Response) throw new RejectedError(error);
|
|
132
|
-
throw error;
|
|
133
|
-
}
|
|
134
|
-
if (result instanceof Response) throw new RejectedError(result);
|
|
135
|
-
if (result === null || result === void 0) throw unauthorized("missing bearer token");
|
|
136
|
-
return fromTokens(result) ? result : hostPrincipal(result);
|
|
137
|
-
}
|
|
138
|
-
return {
|
|
139
|
-
open,
|
|
140
|
-
project,
|
|
141
|
-
/** The OIDC issuers the rules name. */
|
|
142
|
-
issuers: ruleIssuers,
|
|
143
|
-
/** Whether grants decide anything here: not where `access` is one function, which decides alone. */
|
|
144
|
-
takesGrants: typeof access !== "function",
|
|
145
|
-
discoveryIdentity,
|
|
146
|
-
/**
|
|
147
|
-
* Who a request is, and what it may do now: verified, under the rules, with the profile it
|
|
148
|
-
* claims. Each call works its access out again; its principal is verified once per credential
|
|
149
|
-
* until it expires.
|
|
150
|
-
*/
|
|
151
|
-
async identify(request) {
|
|
152
|
-
const found = await principalOf(request);
|
|
153
|
-
const claims = found.pinned ? pinnedClaims(found.pinned, request.claims) : request.claims;
|
|
154
|
-
const scope = found.scope === void 0 ? claims.scope ?? void 0 : claims.scope === null ? found.scope : scopeOf(compileAccess(parseScope(found.scope)).narrow(parseScope(claims.scope)));
|
|
155
|
-
const principal = scope === void 0 ? found : {
|
|
156
|
-
...found,
|
|
157
|
-
scope
|
|
158
|
-
};
|
|
159
|
-
const { author, actor } = resolveActor(principal, claims, request.audience);
|
|
160
|
-
return {
|
|
161
|
-
principal: {
|
|
162
|
-
...principal,
|
|
163
|
-
access: await authority.grant(principal)
|
|
164
|
-
},
|
|
165
|
-
author,
|
|
166
|
-
actor
|
|
167
|
-
};
|
|
168
|
-
},
|
|
169
|
-
/** What a stored principal (a schedule's creator, a watcher's registrant) may do now: null once nothing, its board token's revoking included. */
|
|
170
|
-
async accessNow(principal) {
|
|
171
|
-
if (principal.token !== void 0 && !await trust?.live(principal.token)) return null;
|
|
172
|
-
return authority.accessNow(principal);
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
/** A board token's pins, over what the request says: its profile and session win, and a person's name is the minter's. */
|
|
177
|
-
function pinnedClaims(pinned, claims) {
|
|
178
|
-
return {
|
|
179
|
-
...claims,
|
|
180
|
-
...pinned.profile !== null ? {
|
|
181
|
-
profile: pinned.profile,
|
|
182
|
-
avatar: pinned.avatar
|
|
183
|
-
} : {},
|
|
184
|
-
...pinned.session !== null ? { session: pinned.session } : {},
|
|
185
|
-
handle: pinned.handle,
|
|
186
|
-
name: pinned.name
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
/** A JWT's `iss`, read but not verified: which issuer's keys would check it. Null for anything else. */
|
|
190
|
-
function issuerOf(credential) {
|
|
191
|
-
const payload = credential.split(".")[1];
|
|
192
|
-
if (payload === void 0) return null;
|
|
193
|
-
try {
|
|
194
|
-
const json = atob(payload.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(payload.length / 4) * 4, "="));
|
|
195
|
-
const { iss } = JSON.parse(json);
|
|
196
|
-
return typeof iss === "string" ? iss : null;
|
|
197
|
-
} catch {
|
|
198
|
-
return null;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
//#endregion
|
|
202
|
-
//#region src/contract/logos.ts
|
|
203
|
-
const LOGOS = {
|
|
204
|
-
claude: "m4.7144 15.9555 4.7174-2.6471.079-.2307-.079-.1275h-.2307l-.7893-.0486-2.6956-.0729-2.3375-.0971-2.2646-.1214-.5707-.1215-.5343-.7042.0546-.3522.4797-.3218.686.0608 1.5179.1032 2.2767.1578 1.6514.0972 2.4468.255h.3886l.0546-.1579-.1336-.0971-.1032-.0972L6.973 9.8356l-2.55-1.6879-1.3356-.9714-.7225-.4918-.3643-.4614-.1578-1.0078.6557-.7225.8803.0607.2246.0607.8925.686 1.9064 1.4754 2.4893 1.8336.3643.3035.1457-.1032.0182-.0728-.164-.2733-1.3539-2.4467-1.445-2.4893-.6435-1.032-.17-.6194c-.0607-.255-.1032-.4674-.1032-.7285L6.287.1335 6.6997 0l.9957.1336.419.3642.6192 1.4147 1.0018 2.2282 1.5543 3.0296.4553.8985.2429.8318.091.255h.1579v-.1457l.1275-1.706.2368-2.0947.2307-2.6957.0789-.7589.3764-.9107.7468-.4918.5828.2793.4797.686-.0668.4433-.2853 1.8517-.5586 2.9021-.3643 1.9429h.2125l.2429-.2429.9835-1.3053 1.6514-2.0643.7286-.8196.85-.9046.5464-.4311h1.0321l.759 1.1293-.34 1.1657-1.0625 1.3478-.8804 1.1414-1.2628 1.7-.7893 1.36.0729.1093.1882-.0183 2.8535-.607 1.5421-.2794 1.8396-.3157.8318.3886.091.3946-.3278.8075-1.967.4857-2.3072.4614-3.4364.8136-.0425.0304.0486.0607 1.5482.1457.6618.0364h1.621l3.0175.2247.7892.522.4736.6376-.079.4857-1.2142.6193-1.6393-.3886-3.825-.9107-1.3113-.3279h-.1822v.1093l1.0929 1.0686 2.0035 1.8092 2.5075 2.3314.1275.5768-.3218.4554-.34-.0486-2.2039-1.6575-.85-.7468-1.9246-1.621h-.1275v.17l.4432.6496 2.3436 3.5214.1214 1.0807-.17.3521-.6071.2125-.6679-.1214-1.3721-1.9246L14.38 17.959l-1.1414-1.9428-.1397.079-.674 7.2552-.3156.3703-.7286.2793-.6071-.4614-.3218-.7468.3218-1.4753.3886-1.9246.3157-1.53.2853-1.9004.17-.6314-.0121-.0425-.1397.0182-1.4328 1.9672-2.1796 2.9446-1.7243 1.8456-.4128.164-.7164-.3704.0667-.6618.4008-.5889 2.386-3.0357 1.4389-1.882.929-1.0868-.0062-.1579h-.0546l-6.3385 4.1164-1.1293.1457-.4857-.4554.0608-.7467.2307-.2429 1.9064-1.3114Z",
|
|
205
|
-
openai: "M22.2819 9.8211a5.9847 5.9847 0 0 0-.5157-4.9108 6.0462 6.0462 0 0 0-6.5098-2.9A6.0651 6.0651 0 0 0 4.9807 4.1818a5.9847 5.9847 0 0 0-3.9977 2.9 6.0462 6.0462 0 0 0 .7427 7.0966 5.98 5.98 0 0 0 .511 4.9107 6.051 6.051 0 0 0 6.5146 2.9001A5.9847 5.9847 0 0 0 13.2599 24a6.0557 6.0557 0 0 0 5.7718-4.2058 5.9894 5.9894 0 0 0 3.9977-2.9001 6.0557 6.0557 0 0 0-.7475-7.0729zm-9.022 12.6081a4.4755 4.4755 0 0 1-2.8764-1.0408l.1419-.0804 4.7783-2.7582a.7948.7948 0 0 0 .3927-.6813v-6.7369l2.02 1.1686a.071.071 0 0 1 .038.052v5.5826a4.504 4.504 0 0 1-4.4945 4.4944zm-9.6607-4.1254a4.4708 4.4708 0 0 1-.5346-3.0137l.142.0852 4.783 2.7582a.7712.7712 0 0 0 .7806 0l5.8428-3.3685v2.3324a.0804.0804 0 0 1-.0332.0615L9.74 19.9502a4.4992 4.4992 0 0 1-6.1408-1.6464zM2.3408 7.8956a4.485 4.485 0 0 1 2.3655-1.9728V11.6a.7664.7664 0 0 0 .3879.6765l5.8144 3.3543-2.0201 1.1685a.0757.0757 0 0 1-.071 0l-4.8303-2.7865A4.504 4.504 0 0 1 2.3408 7.872zm16.5963 3.8558L13.1038 8.364 15.1192 7.2a.0757.0757 0 0 1 .071 0l4.8303 2.7913a4.4944 4.4944 0 0 1-.6765 8.1042v-5.6772a.79.79 0 0 0-.407-.667zm2.0107-3.0231l-.142-.0852-4.7735-2.7818a.7759.7759 0 0 0-.7854 0L9.409 9.2297V6.8974a.0662.0662 0 0 1 .0284-.0615l4.8303-2.7866a4.4992 4.4992 0 0 1 6.6802 4.66zM8.3065 12.863l-2.02-1.1638a.0804.0804 0 0 1-.038-.0567V6.0742a4.4992 4.4992 0 0 1 7.3757-3.4537l-.142.0805L8.704 5.459a.7948.7948 0 0 0-.3927.6813zm1.0976-2.3654l2.602-1.4998 2.6069 1.4998v2.9994l-2.5974 1.4997-2.6067-1.4997Z",
|
|
206
|
-
x: "M14.234 10.162 22.977 0h-2.072l-7.591 8.824L7.251 0H.258l9.168 13.343L.258 24H2.33l8.016-9.318L16.749 24h6.993zm-2.837 3.299-.929-1.329L3.076 1.56h3.182l5.965 8.532.929 1.329 7.754 11.09h-3.182z",
|
|
207
|
-
slack: "M5.042 15.165a2.528 2.528 0 0 1-2.52 2.523A2.528 2.528 0 0 1 0 15.165a2.527 2.527 0 0 1 2.522-2.52h2.52v2.52zM6.313 15.165a2.527 2.527 0 0 1 2.521-2.52 2.527 2.527 0 0 1 2.521 2.52v6.313A2.528 2.528 0 0 1 8.834 24a2.528 2.528 0 0 1-2.521-2.522v-6.313zM8.834 5.042a2.528 2.528 0 0 1-2.521-2.52A2.528 2.528 0 0 1 8.834 0a2.528 2.528 0 0 1 2.521 2.522v2.52H8.834zM8.834 6.313a2.528 2.528 0 0 1 2.521 2.521 2.528 2.528 0 0 1-2.521 2.521H2.522A2.528 2.528 0 0 1 0 8.834a2.528 2.528 0 0 1 2.522-2.521h6.312zM18.956 8.834a2.528 2.528 0 0 1 2.522-2.521A2.528 2.528 0 0 1 24 8.834a2.528 2.528 0 0 1-2.522 2.521h-2.522V8.834zM17.688 8.834a2.528 2.528 0 0 1-2.523 2.521 2.527 2.527 0 0 1-2.52-2.521V2.522A2.527 2.527 0 0 1 15.165 0a2.528 2.528 0 0 1 2.523 2.522v6.312zM15.165 18.956a2.528 2.528 0 0 1 2.523 2.522A2.528 2.528 0 0 1 15.165 24a2.527 2.527 0 0 1-2.52-2.522v-2.522h2.52zM15.165 17.688a2.527 2.527 0 0 1-2.52-2.523 2.526 2.526 0 0 1 2.52-2.52h6.313A2.527 2.527 0 0 1 24 15.165a2.528 2.528 0 0 1-2.522 2.523h-6.313z",
|
|
208
|
-
github: "M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12",
|
|
209
|
-
whatsapp: "M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413Z"
|
|
210
|
-
};
|
|
211
|
-
//#endregion
|
|
212
|
-
//#region src/server/avatars.ts
|
|
213
|
-
/** Each known agent's mark on its color. */
|
|
214
|
-
const STYLES = {
|
|
215
|
-
claude: {
|
|
216
|
-
background: "#D97757",
|
|
217
|
-
mark: { path: LOGOS.claude }
|
|
218
|
-
},
|
|
219
|
-
codex: {
|
|
220
|
-
background: "#000000",
|
|
221
|
-
mark: { path: LOGOS.openai }
|
|
222
|
-
},
|
|
223
|
-
grok: {
|
|
224
|
-
background: "#000000",
|
|
225
|
-
mark: { path: LOGOS.x }
|
|
226
|
-
},
|
|
227
|
-
pi: {
|
|
228
|
-
background: "#1f1f1f",
|
|
229
|
-
mark: { glyph: "π" }
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
/** A built-in avatar for a detected agent; other names get their initial. */
|
|
233
|
-
function avatarSvg(name) {
|
|
234
|
-
const style = STYLES[name] ?? {
|
|
235
|
-
background: "#666666",
|
|
236
|
-
mark: { glyph: name.slice(0, 1).toUpperCase() }
|
|
237
|
-
};
|
|
238
|
-
const mark = "path" in style.mark ? `<path transform="translate(30 30) scale(2.8333)" fill="#ffffff" d="${style.mark.path}"/>` : `<text x="64" y="86" font-family="ui-sans-serif,system-ui,sans-serif" font-size="66" font-weight="600" text-anchor="middle" fill="#ffffff">${style.mark.glyph}</text>`;
|
|
239
|
-
return `<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 128 128"><rect width="128" height="128" rx="28" fill="${style.background}"/>${mark}</svg>`;
|
|
240
|
-
}
|
|
241
|
-
//#endregion
|
|
242
|
-
//#region src/server/serve.ts
|
|
243
|
-
/** The methods whose answer is something new. */
|
|
244
|
-
const CREATED = /* @__PURE__ */ new Set([
|
|
245
|
-
"post",
|
|
246
|
-
"schedule",
|
|
247
|
-
"upload",
|
|
248
|
-
"uploadFinish"
|
|
249
|
-
]);
|
|
250
|
-
/** An email, as `--mentions` names one: sync looks its platform user up. */
|
|
251
|
-
const EMAIL = /^[^\s@]+@[^\s@]+$/;
|
|
252
|
-
/** How long a filter's email lookup holds. */
|
|
253
|
-
const EMAIL_TTL_MS = 3e5;
|
|
254
|
-
/** Shown inline only when a browser can't run it: everything else downloads. */
|
|
255
|
-
const INLINE = /^(image\/(png|jpeg|gif|webp|avif)|video\/(mp4|webm)|text\/plain)$/;
|
|
256
|
-
/**
|
|
257
|
-
* The file, sandboxed: it can't run as a page on the board's origin, and only
|
|
258
|
-
* types a browser can't execute display inline.
|
|
259
|
-
*/
|
|
260
|
-
function attachmentResponse({ attachment, data }) {
|
|
261
|
-
const disposition = INLINE.test(attachment.type) ? "inline" : "attachment";
|
|
262
|
-
const body = data instanceof Uint8Array ? new Blob([data]) : data;
|
|
263
|
-
return new Response(body, { headers: {
|
|
264
|
-
"content-type": attachment.type,
|
|
265
|
-
"content-length": String(attachment.size),
|
|
266
|
-
"content-disposition": `${disposition}; filename*=UTF-8''${encodeURIComponent(attachment.name)}`,
|
|
267
|
-
"content-security-policy": "sandbox; default-src 'none'",
|
|
268
|
-
"x-content-type-options": "nosniff",
|
|
269
|
-
"cache-control": "private, max-age=31536000, immutable"
|
|
270
|
-
} });
|
|
271
|
-
}
|
|
272
|
-
/** A method's input, from the request: its path's `:id` or `:key`, its query, or its body, validated. */
|
|
273
|
-
async function inputOf(name, request, url, params) {
|
|
274
|
-
const id = params.id ?? "";
|
|
275
|
-
const key = params.key ?? "";
|
|
276
|
-
const body = () => readJson(request);
|
|
277
|
-
switch (name) {
|
|
278
|
-
case "discovery":
|
|
279
|
-
case "me":
|
|
280
|
-
case "channels":
|
|
281
|
-
case "actors":
|
|
282
|
-
case "watchers":
|
|
283
|
-
case "grants":
|
|
284
|
-
case "tokens": return {};
|
|
285
|
-
case "message":
|
|
286
|
-
case "posted":
|
|
287
|
-
case "synced":
|
|
288
|
-
case "cancelSchedule":
|
|
289
|
-
case "revokeGrant":
|
|
290
|
-
case "revokeToken":
|
|
291
|
-
case "attachment":
|
|
292
|
-
case "uploadFinish": return { id };
|
|
293
|
-
case "messages": return MessagesQuery.parse(queryObject(url));
|
|
294
|
-
case "threads": return ThreadsQuery.parse(queryObject(url));
|
|
295
|
-
case "schedules": return SchedulesQuery.parse(queryObject(url));
|
|
296
|
-
case "matches": return MatchesQuery.parse(queryObject(url));
|
|
297
|
-
case "events": {
|
|
298
|
-
const query = EventsQuery.parse(queryObject(url));
|
|
299
|
-
const last = request.headers.get("last-event-id");
|
|
300
|
-
if (query.after === void 0 && last && Ref.safeParse(last).success) query.after = last;
|
|
301
|
-
return query;
|
|
302
|
-
}
|
|
303
|
-
case "post": return PostBody.parse(await body());
|
|
304
|
-
case "createChannel": return CreateChannelBody.parse(await body());
|
|
305
|
-
case "schedule": return ScheduleBody.parse(await body());
|
|
306
|
-
case "createGrant": return GrantBody.parse(await body());
|
|
307
|
-
case "createToken": return TokenBody.parse(await body());
|
|
308
|
-
case "register": return RegisterBody.parse(await body());
|
|
309
|
-
case "edit": return {
|
|
310
|
-
id,
|
|
311
|
-
...EditBody.parse(await body())
|
|
312
|
-
};
|
|
313
|
-
case "delete": return {
|
|
314
|
-
id,
|
|
315
|
-
...DeleteBody.parse(await body())
|
|
316
|
-
};
|
|
317
|
-
case "react": return {
|
|
318
|
-
id,
|
|
319
|
-
...ReactBody.parse(await body())
|
|
320
|
-
};
|
|
321
|
-
case "label": return {
|
|
322
|
-
id,
|
|
323
|
-
...LabelBody.parse(await body())
|
|
324
|
-
};
|
|
325
|
-
case "ack": return {
|
|
326
|
-
id,
|
|
327
|
-
...AckBody.parse(await body())
|
|
328
|
-
};
|
|
329
|
-
case "upload": {
|
|
330
|
-
const header = request.headers.get("x-board-filename");
|
|
331
|
-
let name = null;
|
|
332
|
-
try {
|
|
333
|
-
name = header === null ? null : decodeURIComponent(header);
|
|
334
|
-
} catch {
|
|
335
|
-
throw badRequest("`x-board-filename` is not URI-encoded");
|
|
336
|
-
}
|
|
337
|
-
return {
|
|
338
|
-
data: await readCapped(request, ATTACHMENT_BYTES_MAX),
|
|
339
|
-
name,
|
|
340
|
-
type: request.headers.get("content-type")
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
case "uploadStart": return UploadStartBody.parse(await body());
|
|
344
|
-
case "syncImport": return SyncImportBody.parse(await body());
|
|
345
|
-
case "syncReceipt": return SyncReceiptBody.parse(await body());
|
|
346
|
-
case "syncThread": return { thread: params.thread ?? "" };
|
|
347
|
-
case "syncFind": return SyncFindQuery.parse(queryObject(url));
|
|
348
|
-
case "syncCopies": return SyncCopiesQuery.parse(queryObject(url));
|
|
349
|
-
case "inboxPush": return {
|
|
350
|
-
key,
|
|
351
|
-
...InboxPushBody.parse(await body())
|
|
352
|
-
};
|
|
353
|
-
case "inboxNext": return {
|
|
354
|
-
key,
|
|
355
|
-
...InboxNextQuery.parse(queryObject(url))
|
|
356
|
-
};
|
|
357
|
-
case "inboxAck": return {
|
|
358
|
-
key,
|
|
359
|
-
id,
|
|
360
|
-
...InboxAckBody.parse(await body())
|
|
361
|
-
};
|
|
362
|
-
case "inbox": return {
|
|
363
|
-
key,
|
|
364
|
-
...InboxQuery.parse(queryObject(url))
|
|
365
|
-
};
|
|
366
|
-
case "typing": return {
|
|
367
|
-
id,
|
|
368
|
-
...TypingBody.parse(await body()) ?? {}
|
|
369
|
-
};
|
|
370
|
-
case "readSource": return {
|
|
371
|
-
key,
|
|
372
|
-
...SourceReadQuery.parse(queryObject(url))
|
|
373
|
-
};
|
|
374
|
-
case "joinSource": return {
|
|
375
|
-
key,
|
|
376
|
-
...SourceJoinBody.parse(await body())
|
|
377
|
-
};
|
|
378
|
-
case "linked": return LinkedQuery.parse(queryObject(url));
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
/** A platform user, as the middleware is told who's writing. */
|
|
382
|
-
const externalActor = (author) => ({
|
|
383
|
-
id: author.id,
|
|
384
|
-
kind: "external",
|
|
385
|
-
handle: author.handle,
|
|
386
|
-
name: author.name,
|
|
387
|
-
avatar: author.avatar
|
|
388
|
-
});
|
|
389
|
-
/** A write's caller, as the middleware is told: where it came from. The middleware's own handle and sync's lookups skip it. */
|
|
390
|
-
function sourceOf(caller) {
|
|
391
|
-
const via = caller.origin.via;
|
|
392
|
-
if (via?.kind === "middleware" || via?.kind === "engine") return null;
|
|
393
|
-
return via ?? { kind: "api" };
|
|
394
|
-
}
|
|
395
|
-
/** The middleware's own handle: its writes skip the middleware. */
|
|
396
|
-
const MIDDLEWARE = {
|
|
397
|
-
self: "middleware",
|
|
398
|
-
origin: { via: { kind: "middleware" } }
|
|
399
|
-
};
|
|
400
|
-
/** board()'s own code, for what it looks up itself. */
|
|
401
|
-
const OWN = {
|
|
402
|
-
self: null,
|
|
403
|
-
origin: { via: {
|
|
404
|
-
kind: "client",
|
|
405
|
-
name: null
|
|
406
|
-
} }
|
|
407
|
-
};
|
|
408
|
-
/**
|
|
409
|
-
* The board, serving the protocol over HTTP on its backend: it routes and validates each request,
|
|
410
|
-
* verifies its caller, applies `access`, runs the middleware, and calls the backend's method of the
|
|
411
|
-
* same name with the caller it verified. Sync runs here too, for requests and for its own code.
|
|
412
|
-
*/
|
|
413
|
-
async function createServe(context) {
|
|
414
|
-
const { options, backend, runtime, policy } = context;
|
|
415
|
-
const entries = options.sync ?? {};
|
|
416
|
-
const engine = Object.keys(entries).length > 0 ? (await runtime.sync()).createSyncEngine(entries) : null;
|
|
417
|
-
const streamMaxMs = options.streamMaxMs ?? 28e4;
|
|
418
|
-
const route = createRouter(backend.routes, options.basePath);
|
|
419
|
-
const { state, ownUrl, clientAs, handleOf } = context;
|
|
420
|
-
const watch = options.watch ?? {};
|
|
421
|
-
const middleware = options.middleware ?? [];
|
|
422
|
-
/** What a request says about who it is, none of it verified yet: its bearer token, its claims, and the board's URL. */
|
|
423
|
-
function requestOf(request, audience) {
|
|
424
|
-
return {
|
|
425
|
-
credential: (request.headers.get("authorization") ?? "").match(/^Bearer\s+(.+)$/i)?.[1] ?? null,
|
|
426
|
-
claims: claimsOf(request.headers),
|
|
427
|
-
audience,
|
|
428
|
-
headers: request.headers
|
|
429
|
-
};
|
|
430
|
-
}
|
|
431
|
-
/** A request's caller, verified: who it is, and what `access` lets it do now. */
|
|
432
|
-
async function callerOf(request) {
|
|
433
|
-
return {
|
|
434
|
-
identity: await policy.identify(request),
|
|
435
|
-
origin: { via: { kind: "api" } }
|
|
436
|
-
};
|
|
437
|
-
}
|
|
438
|
-
/** What a board that serves no requests answers them: a board on `http()` with no `access` of its own. */
|
|
439
|
-
const servesOnly = () => new HttpError(404, "not_found", `${ownUrl() ?? "this board"} answers only wake-ups and webhooks`);
|
|
440
|
-
/**
|
|
441
|
-
* `@[ada@vercel.com]`: an email a synced platform knows becomes that user's mention, and the
|
|
442
|
-
* board learns who they are; the rest stay plain text.
|
|
443
|
-
*/
|
|
444
|
-
async function withEmails(text) {
|
|
445
|
-
return (await withEmailMentions(text, engine ? learnEmail : void 0)).text;
|
|
446
|
-
}
|
|
447
|
-
/**
|
|
448
|
-
* The synced platform's user with this email, whom the board learns: a post can mention only
|
|
449
|
-
* an actor it knows, and a platform mention of them resolves only once it does.
|
|
450
|
-
*/
|
|
451
|
-
async function learnEmail(email) {
|
|
452
|
-
const actor = await engine.findByEmail(email);
|
|
453
|
-
if (actor) await backend.syncImport(engine.caller, {
|
|
454
|
-
op: "seen",
|
|
455
|
-
author: {
|
|
456
|
-
...actor,
|
|
457
|
-
isBot: false
|
|
458
|
-
}
|
|
459
|
-
}, new AbortController().signal);
|
|
460
|
-
return actor;
|
|
461
|
-
}
|
|
462
|
-
/** Emails filters named, as sync found them, for a few minutes: `tail` and `wait` ask at every page. */
|
|
463
|
-
const mentioned = /* @__PURE__ */ new Map();
|
|
464
|
-
/**
|
|
465
|
-
* `--mentions ada@vercel.com`: the synced platform's user with that email, for a caller that may
|
|
466
|
-
* read somewhere. Anything else, and an email sync doesn't know, goes on as it came.
|
|
467
|
-
*/
|
|
468
|
-
async function withMentioned(caller, query) {
|
|
469
|
-
const email = query.mentions?.toLowerCase();
|
|
470
|
-
if (!engine || email === void 0 || !EMAIL.test(email)) return query;
|
|
471
|
-
const access = accessOf(caller);
|
|
472
|
-
if (access !== null && !access.anywhere("read")) return query;
|
|
473
|
-
let found = mentioned.get(email);
|
|
474
|
-
if (!found || Date.now() - found.at > EMAIL_TTL_MS) {
|
|
475
|
-
if (mentioned.size >= 1e3) mentioned.clear();
|
|
476
|
-
found = {
|
|
477
|
-
at: Date.now(),
|
|
478
|
-
actor: learnEmail(email).catch(() => null)
|
|
479
|
-
};
|
|
480
|
-
mentioned.set(email, found);
|
|
481
|
-
}
|
|
482
|
-
const actor = await found.actor;
|
|
483
|
-
return actor ? {
|
|
484
|
-
...query,
|
|
485
|
-
mentions: actor.id
|
|
486
|
-
} : query;
|
|
487
|
-
}
|
|
488
|
-
/**
|
|
489
|
-
* A sync target as the board that syncs it resolves it. One for an entry this board doesn't
|
|
490
|
-
* have goes on as it is, for the sync that does, unless this board syncs: then it's a mistake.
|
|
491
|
-
*/
|
|
492
|
-
async function resolveTarget(target) {
|
|
493
|
-
if (typeof target !== "string") return target;
|
|
494
|
-
const at = target.indexOf(":");
|
|
495
|
-
if (at <= 0) throw badRequest(`sync targets look like <key>:<channel or thread>, got: ${target}`);
|
|
496
|
-
const key = target.slice(0, at);
|
|
497
|
-
if (engine && entries[key]) return engine.normalizeTarget(target);
|
|
498
|
-
if (engine) throw badRequest(`no sync entry named ${key}`);
|
|
499
|
-
return syncTarget(target);
|
|
500
|
-
}
|
|
501
|
-
/**
|
|
502
|
-
* What a verified caller may do, as board()'s `access` said when it verified them: board() checks
|
|
503
|
-
* it before it does anything for them past calling the backend (a platform call, an email lookup,
|
|
504
|
-
* sync's own write, reading an upload, the middleware), so a caller it won't let in starts none of
|
|
505
|
-
* it. Null for this deployment's own code, which may do everything.
|
|
506
|
-
*/
|
|
507
|
-
const accessOf = (caller) => "identity" in caller ? caller.identity.principal.access : null;
|
|
508
|
-
/**
|
|
509
|
-
* Whether board() may work for a write of `caller`'s that lands in `channel`, or in the thread of
|
|
510
|
-
* message `re`, which it must read: `post` there. One it may not goes to the backend as it came,
|
|
511
|
-
* for the backend to say why.
|
|
512
|
-
*/
|
|
513
|
-
async function mayPost(caller, where, signal) {
|
|
514
|
-
const access = accessOf(caller);
|
|
515
|
-
if (access === null) return {
|
|
516
|
-
access: null,
|
|
517
|
-
channel: where.channel ?? ""
|
|
518
|
-
};
|
|
519
|
-
const channel = where.channel ?? (where.re === void 0 ? void 0 : (await lookup(caller, where.re, signal))?.channel);
|
|
520
|
-
if (channel === void 0) return null;
|
|
521
|
-
return access.can("post", channel) ? {
|
|
522
|
-
access,
|
|
523
|
-
channel
|
|
524
|
-
} : null;
|
|
525
|
-
}
|
|
526
|
-
/** Who this deployment's own code is on its backend, asked once each: the middleware is told who's writing. */
|
|
527
|
-
const selves = /* @__PURE__ */ new Map();
|
|
528
|
-
async function whoIs(caller, signal) {
|
|
529
|
-
if ("identity" in caller) return {
|
|
530
|
-
principal: caller.identity.principal.id,
|
|
531
|
-
actor: caller.identity.actor
|
|
532
|
-
};
|
|
533
|
-
const key = JSON.stringify([
|
|
534
|
-
caller.origin.via?.kind ?? "",
|
|
535
|
-
caller.self,
|
|
536
|
-
caller.profile ?? null
|
|
537
|
-
]);
|
|
538
|
-
let found = selves.get(key);
|
|
539
|
-
if (!found) {
|
|
540
|
-
selves.set(key, found = backend.me(caller, {}, signal).then((me) => ({
|
|
541
|
-
principal: me.principal,
|
|
542
|
-
actor: {
|
|
543
|
-
id: me.actor,
|
|
544
|
-
kind: me.kind,
|
|
545
|
-
handle: me.handle,
|
|
546
|
-
name: me.name,
|
|
547
|
-
avatar: me.avatar
|
|
548
|
-
}
|
|
549
|
-
})));
|
|
550
|
-
found.catch(() => selves.delete(key));
|
|
551
|
-
}
|
|
552
|
-
return found;
|
|
553
|
-
}
|
|
554
|
-
let middlewareBoard = null;
|
|
555
|
-
/** Runs the middleware on a write, then `commit`s what it passed on: a `DroppedError` when it drops it. */
|
|
556
|
-
const screen = (write, author, source, commit) => runMiddleware(middleware, {
|
|
557
|
-
write,
|
|
558
|
-
author,
|
|
559
|
-
source,
|
|
560
|
-
board: () => middlewareBoard ??= handleOf(MIDDLEWARE, null)
|
|
561
|
-
}, commit);
|
|
562
|
-
/** Whether a lookup found what it looked for: a 404 says it didn't. */
|
|
563
|
-
async function found(lookup) {
|
|
564
|
-
try {
|
|
565
|
-
await lookup();
|
|
566
|
-
return true;
|
|
567
|
-
} catch (error) {
|
|
568
|
-
if (error instanceof HttpError && error.status === 404) return false;
|
|
569
|
-
throw error;
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
/** Files a write carries, as the board stored them: each one `caller` may have, or the backend's 404. */
|
|
573
|
-
async function attachmentsOf(caller, ids, signal) {
|
|
574
|
-
const attachments = [];
|
|
575
|
-
for (const id of new Set(ids ?? [])) {
|
|
576
|
-
const { attachment, data } = await backend.attachment(caller, { id }, signal);
|
|
577
|
-
if (data instanceof ReadableStream) await data.cancel().catch(() => {});
|
|
578
|
-
attachments.push(attachment);
|
|
579
|
-
}
|
|
580
|
-
return attachments;
|
|
581
|
-
}
|
|
582
|
-
/**
|
|
583
|
-
* A post through the middleware, then `send`: once board() knows it would land (a reply's root is
|
|
584
|
-
* there, and the caller may post, label, and sync where it does), with the files it may have. A
|
|
585
|
-
* retry of one that landed stands, whatever the middleware would say now; one that wouldn't land
|
|
586
|
-
* goes as it came, for the backend to say why.
|
|
587
|
-
*/
|
|
588
|
-
async function screenPost(caller, input, signal, send) {
|
|
589
|
-
const source = sourceOf(caller);
|
|
590
|
-
if (middleware.length === 0 || source === null) return send(input);
|
|
591
|
-
if (input.id !== void 0 && await found(() => backend.posted(caller, { id: input.id }, signal))) return send(input);
|
|
592
|
-
let channel = input.channel;
|
|
593
|
-
let re = null;
|
|
594
|
-
if (input.re !== void 0) {
|
|
595
|
-
const root = await lookup(caller, input.re, signal);
|
|
596
|
-
if (!root) return send(input);
|
|
597
|
-
channel = root.channel;
|
|
598
|
-
re = root.re ?? root.id;
|
|
599
|
-
}
|
|
600
|
-
const access = accessOf(caller);
|
|
601
|
-
const labels = Object.keys(input.labels ?? {}).length > 0;
|
|
602
|
-
const syncs = input.re === void 0 && (input.sync?.length ?? 0) > 0;
|
|
603
|
-
if (channel === void 0 || access && (!access.can("post", channel) || labels && !access.can("label", channel) || syncs && !access.can("sync", channel))) return send(input);
|
|
604
|
-
const attachments = await attachmentsOf(caller, input.attachments, signal);
|
|
605
|
-
const { actor } = await whoIs(caller, signal);
|
|
606
|
-
return screen({
|
|
607
|
-
kind: "post",
|
|
608
|
-
channel,
|
|
609
|
-
re,
|
|
610
|
-
text: input.text,
|
|
611
|
-
labels: input.labels ?? {},
|
|
612
|
-
attachments
|
|
613
|
-
}, actor, source, (write) => send({
|
|
614
|
-
...input,
|
|
615
|
-
text: write.text,
|
|
616
|
-
labels: write.labels
|
|
617
|
-
}));
|
|
618
|
-
}
|
|
619
|
-
/**
|
|
620
|
-
* A schedule, on a backend whose sends post elsewhere: its message through the middleware once,
|
|
621
|
-
* as the caller's post of it, since its text and labels never change and no send comes back here.
|
|
622
|
-
*/
|
|
623
|
-
function screenSchedule(caller, input, signal) {
|
|
624
|
-
const { channel, re, text, labels, noSync, attachments } = input;
|
|
625
|
-
return screenPost(caller, {
|
|
626
|
-
...channel !== void 0 ? { channel } : {},
|
|
627
|
-
...re !== void 0 ? { re } : {},
|
|
628
|
-
text,
|
|
629
|
-
...labels ? { labels } : {},
|
|
630
|
-
...noSync !== void 0 ? { noSync } : {},
|
|
631
|
-
...attachments ? { attachments } : {}
|
|
632
|
-
}, signal, (screened) => backend.schedule(caller, {
|
|
633
|
-
...input,
|
|
634
|
-
text: screened.text,
|
|
635
|
-
...screened.labels ? { labels: screened.labels } : {}
|
|
636
|
-
}, signal));
|
|
637
|
-
}
|
|
638
|
-
/**
|
|
639
|
-
* The caller's own message, still there, where it may post: what an edit of it changes. One it
|
|
640
|
-
* can't read may still be its own, as board()'s own code finds it, only if it was written there
|
|
641
|
-
* without reading too: one its principal wrote where it reads (a repo's workflows share one) is
|
|
642
|
-
* as missing to it as one that isn't there.
|
|
643
|
-
*/
|
|
644
|
-
async function ownMessage(caller, id, signal) {
|
|
645
|
-
const access = accessOf(caller);
|
|
646
|
-
const readable = await lookup(caller, id, signal);
|
|
647
|
-
const message = readable ?? (access !== null ? await lookup(OWN, id, signal) : null);
|
|
648
|
-
if (!message || message.deleted || access && !access.can("post", message.channel)) return null;
|
|
649
|
-
if (!readable && !message.blind) return null;
|
|
650
|
-
return message.by.principal === (await whoIs(caller, signal)).principal ? message : null;
|
|
651
|
-
}
|
|
652
|
-
/** An edit through the middleware, then `send`: only the author's own, of a message that's still there, reaches it. */
|
|
653
|
-
async function screenEdit(caller, input, signal, send) {
|
|
654
|
-
const source = sourceOf(caller);
|
|
655
|
-
if (middleware.length === 0 || source === null) return send(input);
|
|
656
|
-
const message = await ownMessage(caller, input.id, signal);
|
|
657
|
-
if (!message) return send(input);
|
|
658
|
-
const { actor } = await whoIs(caller, signal);
|
|
659
|
-
return screen({
|
|
660
|
-
kind: "edit",
|
|
661
|
-
id: message.id,
|
|
662
|
-
channel: message.channel,
|
|
663
|
-
re: message.re,
|
|
664
|
-
text: input.text
|
|
665
|
-
}, actor, source, (write) => send({
|
|
666
|
-
...input,
|
|
667
|
-
text: write.text
|
|
668
|
-
}));
|
|
669
|
-
}
|
|
670
|
-
/**
|
|
671
|
-
* Where an import came from, as the middleware is told: the platform event sync says it's from,
|
|
672
|
-
* or what its entry and origin say. Null when it names an entry its caller doesn't sync, which
|
|
673
|
-
* the backend refuses.
|
|
674
|
-
*/
|
|
675
|
-
async function importSource(caller, body, found, signal) {
|
|
676
|
-
const key = body.source?.key ?? body.from;
|
|
677
|
-
if (body.source) {
|
|
678
|
-
if (!("identity" in caller ? (await backend.watchers(caller, {}, signal)).watchers.some((registration) => registration.owner === caller.identity.principal.id && registration.sync.some((entry) => entry.key === key)) : entries[body.source.key] !== void 0)) return null;
|
|
679
|
-
}
|
|
680
|
-
const origin = (body.op === "post" ? body.origin : null) ?? found?.origin ?? null;
|
|
681
|
-
return {
|
|
682
|
-
kind: "sync",
|
|
683
|
-
key: key ?? "",
|
|
684
|
-
platform: body.author.id.slice(0, Math.max(0, body.author.id.indexOf(":"))),
|
|
685
|
-
channel: body.source?.channel ?? found?.origin?.channel ?? origin?.channel ?? "",
|
|
686
|
-
thread: body.source?.thread ?? (origin ? origin.subthread ?? origin.thread : "")
|
|
687
|
-
};
|
|
688
|
-
}
|
|
689
|
-
/** The board message an import changes: the one it names, or the one its platform IDs are on the board as. */
|
|
690
|
-
async function importTarget(caller, body, signal) {
|
|
691
|
-
let id = body.message ?? null;
|
|
692
|
-
if (body.origin !== void 0) {
|
|
693
|
-
const { thread, message } = await backend.syncFind(caller, {
|
|
694
|
-
thread: body.origin.thread,
|
|
695
|
-
...body.origin.message !== void 0 ? { message: body.origin.message } : {}
|
|
696
|
-
}, signal);
|
|
697
|
-
id = body.origin.message !== void 0 ? message : thread === null ? null : (await backend.syncThread(caller, { thread }, signal)).root;
|
|
698
|
-
}
|
|
699
|
-
return id === null ? null : lookup(caller, id, signal);
|
|
700
|
-
}
|
|
701
|
-
/**
|
|
702
|
-
* An import, through the middleware when it's a post or an edit that would land: what the
|
|
703
|
-
* middleware passed on, or what it responded when it dropped it, for sync to tell the author on
|
|
704
|
-
* the platform. A retry of one that landed stands.
|
|
705
|
-
*/
|
|
706
|
-
async function syncImport(caller, body, signal) {
|
|
707
|
-
const send = (patched) => backend.syncImport(caller, patched, signal);
|
|
708
|
-
if (middleware.length === 0 || caller.origin.via?.kind === "middleware" || body.op !== "post" && body.op !== "edit") return send(body);
|
|
709
|
-
const access = accessOf(caller);
|
|
710
|
-
if (access && (!access.anywhere("sync") || "identity" in caller && caller.identity.principal.scope !== void 0)) return send(body);
|
|
711
|
-
const dropped = async (commit) => {
|
|
712
|
-
try {
|
|
713
|
-
return await commit();
|
|
714
|
-
} catch (error) {
|
|
715
|
-
if (!(error instanceof DroppedError)) throw error;
|
|
716
|
-
return {
|
|
717
|
-
message: null,
|
|
718
|
-
dropped: [...error.responses]
|
|
719
|
-
};
|
|
720
|
-
}
|
|
721
|
-
};
|
|
722
|
-
if (body.op === "post") {
|
|
723
|
-
if (body.id !== void 0 && (await backend.syncFind(caller, { post: body.id }, signal)).message !== null) return send(body);
|
|
724
|
-
let channel = body.channel;
|
|
725
|
-
let re = null;
|
|
726
|
-
if (body.re !== void 0) {
|
|
727
|
-
const root = await lookup(caller, body.re, signal);
|
|
728
|
-
if (!root) return send(body);
|
|
729
|
-
channel = root.channel;
|
|
730
|
-
re = root.re ?? root.id;
|
|
731
|
-
}
|
|
732
|
-
if (channel === void 0 || access && !access.can("sync", channel)) return send(body);
|
|
733
|
-
const source = await importSource(caller, body, null, signal);
|
|
734
|
-
if (!source) return send(body);
|
|
735
|
-
const attachments = await attachmentsOf(caller, body.attachments, signal);
|
|
736
|
-
return dropped(async () => screen({
|
|
737
|
-
kind: "post",
|
|
738
|
-
channel,
|
|
739
|
-
re,
|
|
740
|
-
text: body.text,
|
|
741
|
-
labels: body.labels ?? {},
|
|
742
|
-
attachments
|
|
743
|
-
}, externalActor(body.author), source, async (write) => send({
|
|
744
|
-
...body,
|
|
745
|
-
text: write.text,
|
|
746
|
-
labels: write.labels
|
|
747
|
-
})));
|
|
748
|
-
}
|
|
749
|
-
if (body.origin === void 0 === (body.message === void 0)) return send(body);
|
|
750
|
-
const found = await importTarget(caller, body, signal);
|
|
751
|
-
if (!found || found.deleted || access && !access.can("sync", found.channel) || body.message !== void 0 && found.by.principal !== body.author.id) return send(body);
|
|
752
|
-
const source = await importSource(caller, body, found, signal);
|
|
753
|
-
if (!source) return send(body);
|
|
754
|
-
return dropped(async () => screen({
|
|
755
|
-
kind: "edit",
|
|
756
|
-
id: found.id,
|
|
757
|
-
channel: found.channel,
|
|
758
|
-
re: found.re,
|
|
759
|
-
text: body.text
|
|
760
|
-
}, externalActor(body.author), source, async (write) => send({
|
|
761
|
-
...body,
|
|
762
|
-
text: write.text
|
|
763
|
-
})));
|
|
764
|
-
}
|
|
765
|
-
/** A message as `caller` may read it, or null. */
|
|
766
|
-
async function lookup(caller, id, signal) {
|
|
767
|
-
try {
|
|
768
|
-
const { message, replies } = await backend.message(caller, { id }, signal);
|
|
769
|
-
return [message, ...replies].find((candidate) => candidate.id === id) ?? null;
|
|
770
|
-
} catch (error) {
|
|
771
|
-
if (error instanceof HttpError && error.status === 404) return null;
|
|
772
|
-
throw error;
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
/**
|
|
776
|
-
* A post, with the emails it mentions resolved once the caller may post there (and sync, for
|
|
777
|
-
* targets it names), through the middleware.
|
|
778
|
-
*/
|
|
779
|
-
async function post(caller, input, signal) {
|
|
780
|
-
const send = (poster, body) => screenPost(poster, body, signal, (patched) => backend.post(poster, patched, signal));
|
|
781
|
-
if (!engine) return send(caller, input.sync?.length ? {
|
|
782
|
-
...input,
|
|
783
|
-
sync: await Promise.all(input.sync.map(resolveTarget))
|
|
784
|
-
} : input);
|
|
785
|
-
const allowed = (input.sync?.length ?? 0) > 0 || mentionsEmails(input.text) ? await mayPost(caller, input, signal) : null;
|
|
786
|
-
if (!allowed) return send(caller, input);
|
|
787
|
-
const { access, channel } = allowed;
|
|
788
|
-
const body = input.re === void 0 && (input.sync?.length ?? 0) > 0 && (access === null || access.can("sync", channel)) ? {
|
|
789
|
-
...input,
|
|
790
|
-
sync: await Promise.all(input.sync.map(resolveTarget))
|
|
791
|
-
} : input;
|
|
792
|
-
const text = await withEmails(body.text);
|
|
793
|
-
const answer = await send(caller, {
|
|
794
|
-
...body,
|
|
795
|
-
text
|
|
796
|
-
});
|
|
797
|
-
return text !== input.text && access !== null && !access.readable(answer.message.channel) ? {
|
|
798
|
-
...answer,
|
|
799
|
-
message: {
|
|
800
|
-
...answer.message,
|
|
801
|
-
text: input.text
|
|
802
|
-
}
|
|
803
|
-
} : answer;
|
|
804
|
-
}
|
|
805
|
-
/**
|
|
806
|
-
* An import's answer, as a sync callback's client gets it: the message it wrote or changed, or the
|
|
807
|
-
* middleware's responses in the event's platform thread and a `DroppedError` when it dropped it.
|
|
808
|
-
*/
|
|
809
|
-
async function landed(caller, answer) {
|
|
810
|
-
if (!answer.dropped) return answer.message;
|
|
811
|
-
for (const text of answer.dropped) await caller.platform.respond(text);
|
|
812
|
-
throw new DroppedError(answer.dropped);
|
|
813
|
-
}
|
|
814
|
-
/**
|
|
815
|
-
* What a sync callback's client does, on any backend. Its own writes are imports, as the platform
|
|
816
|
-
* user the event came from, which the middleware sees as any import; everything else, its reads
|
|
817
|
-
* included, it does as sync. A change it names by ID is an import that names it so.
|
|
818
|
-
*/
|
|
819
|
-
async function asPlatform(name, caller, input, signal) {
|
|
820
|
-
const me = engine.caller;
|
|
821
|
-
const { author, key } = caller.platform;
|
|
822
|
-
const { via } = caller.origin;
|
|
823
|
-
const source = {
|
|
824
|
-
key,
|
|
825
|
-
channel: via.channel,
|
|
826
|
-
thread: via.thread
|
|
827
|
-
};
|
|
828
|
-
const change = async (body) => {
|
|
829
|
-
if (await landed(caller, await syncImport(me, body, signal)) === null) throw notFound(`message not found: #${body.message}`, "message_not_found");
|
|
830
|
-
return { ok: true };
|
|
831
|
-
};
|
|
832
|
-
switch (name) {
|
|
833
|
-
case "post": return importPost(caller, input, signal);
|
|
834
|
-
case "edit": {
|
|
835
|
-
const { id, text, revision } = input;
|
|
836
|
-
return change({
|
|
837
|
-
op: "edit",
|
|
838
|
-
author,
|
|
839
|
-
from: null,
|
|
840
|
-
message: id,
|
|
841
|
-
text: await withEmails(text),
|
|
842
|
-
source,
|
|
843
|
-
...revision !== void 0 ? { revision } : {}
|
|
844
|
-
});
|
|
845
|
-
}
|
|
846
|
-
case "delete": {
|
|
847
|
-
const { id, revision } = input;
|
|
848
|
-
return change({
|
|
849
|
-
op: "delete",
|
|
850
|
-
author,
|
|
851
|
-
from: null,
|
|
852
|
-
message: id,
|
|
853
|
-
...revision !== void 0 ? { revision } : {}
|
|
854
|
-
});
|
|
855
|
-
}
|
|
856
|
-
case "react": {
|
|
857
|
-
const { id, emoji, remove } = input;
|
|
858
|
-
return change({
|
|
859
|
-
op: "react",
|
|
860
|
-
author,
|
|
861
|
-
from: null,
|
|
862
|
-
message: id,
|
|
863
|
-
emoji,
|
|
864
|
-
...remove !== void 0 ? { remove } : {}
|
|
865
|
-
});
|
|
866
|
-
}
|
|
867
|
-
case "label": {
|
|
868
|
-
const { id, set, unset, if: expected } = input;
|
|
869
|
-
return change({
|
|
870
|
-
op: "labels",
|
|
871
|
-
author,
|
|
872
|
-
from: null,
|
|
873
|
-
message: id,
|
|
874
|
-
...set ? { set } : {},
|
|
875
|
-
...unset ? { unset } : {},
|
|
876
|
-
...expected ? { if: expected } : {}
|
|
877
|
-
});
|
|
878
|
-
}
|
|
879
|
-
default: return invoke(name, me, input, signal);
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
/** A sync callback's post, as an import by its platform user: once per client ID, which the backend sees to. */
|
|
883
|
-
async function importPost(caller, input, signal) {
|
|
884
|
-
const me = engine.caller;
|
|
885
|
-
const { author, key } = caller.platform;
|
|
886
|
-
const { via } = caller.origin;
|
|
887
|
-
const read = async (id) => {
|
|
888
|
-
const message = await lookup(me, id, signal);
|
|
889
|
-
if (!message) throw notFound(`message not found: #${id}`, "message_not_found");
|
|
890
|
-
return message;
|
|
891
|
-
};
|
|
892
|
-
if (input.id !== void 0) {
|
|
893
|
-
const found = (await backend.syncFind(me, { post: input.id }, signal)).message;
|
|
894
|
-
if (found !== null) return { message: await read(found) };
|
|
895
|
-
}
|
|
896
|
-
const targets = await Promise.all((input.sync ?? []).map(resolveTarget));
|
|
897
|
-
const message = await landed(caller, await syncImport(me, {
|
|
898
|
-
op: "post",
|
|
899
|
-
author,
|
|
900
|
-
from: null,
|
|
901
|
-
origin: null,
|
|
902
|
-
source: {
|
|
903
|
-
key,
|
|
904
|
-
channel: via.channel,
|
|
905
|
-
thread: via.thread
|
|
906
|
-
},
|
|
907
|
-
...input.channel !== void 0 ? { channel: input.channel } : { re: input.re },
|
|
908
|
-
text: await withEmails(input.text),
|
|
909
|
-
...input.labels ? { labels: input.labels } : {},
|
|
910
|
-
...input.noSync ? { noSync: true } : {},
|
|
911
|
-
...input.attachments?.length ? { attachments: input.attachments } : {},
|
|
912
|
-
...input.id !== void 0 ? { id: input.id } : {},
|
|
913
|
-
...input.after !== void 0 ? { after: input.after } : {}
|
|
914
|
-
}, signal));
|
|
915
|
-
for (const target of targets) await backend.syncReceipt(me, {
|
|
916
|
-
kind: "thread",
|
|
917
|
-
message,
|
|
918
|
-
target: target.target,
|
|
919
|
-
targetKind: target.kind,
|
|
920
|
-
cause: "request"
|
|
921
|
-
}, signal);
|
|
922
|
-
return { message: await read(message) };
|
|
923
|
-
}
|
|
924
|
-
/** The protocol method `name`, as board() serves it: what it posts and edits, and the filters it reads by, with their emails resolved. */
|
|
925
|
-
async function invoke(name, caller, input, signal) {
|
|
926
|
-
if (name === "messages" || name === "threads" || name === "events") input = await withMentioned(caller, input);
|
|
927
|
-
switch (name) {
|
|
928
|
-
case "post": return post(caller, input, signal);
|
|
929
|
-
case "edit": {
|
|
930
|
-
const edit = input;
|
|
931
|
-
const send = (body) => screenEdit(caller, body, signal, (patched) => backend.edit(caller, patched, signal));
|
|
932
|
-
if (!engine || !mentionsEmails(edit.text) || !await mayPost(caller, { re: edit.id }, signal)) return send(edit);
|
|
933
|
-
return send({
|
|
934
|
-
...edit,
|
|
935
|
-
text: await withEmails(edit.text)
|
|
936
|
-
});
|
|
937
|
-
}
|
|
938
|
-
case "syncImport": return syncImport(caller, input, signal);
|
|
939
|
-
case "grants":
|
|
940
|
-
case "createGrant":
|
|
941
|
-
case "revokeGrant":
|
|
942
|
-
if (backend.keeps && !policy.takesGrants) throw new HttpError(501, "no_grants", "this board's access is one function, which decides alone: it keeps no grants");
|
|
943
|
-
return backend[name].call(backend, caller, input, signal);
|
|
944
|
-
case "schedule": {
|
|
945
|
-
const body = input;
|
|
946
|
-
const schedule = (made) => backend.sendsSchedulesHere === false ? screenSchedule(caller, made, signal) : backend.schedule(caller, made, signal);
|
|
947
|
-
const allowed = engine && mentionsEmails(body.text) ? await mayPost(caller, body, signal) : null;
|
|
948
|
-
if (!allowed) return schedule(body);
|
|
949
|
-
const text = await withEmails(body.text);
|
|
950
|
-
const answer = await schedule({
|
|
951
|
-
...body,
|
|
952
|
-
text
|
|
953
|
-
});
|
|
954
|
-
const { access } = allowed;
|
|
955
|
-
return text !== body.text && access !== null && !access.readable(answer.schedule.channel) ? {
|
|
956
|
-
...answer,
|
|
957
|
-
schedule: {
|
|
958
|
-
...answer.schedule,
|
|
959
|
-
text: body.text
|
|
960
|
-
}
|
|
961
|
-
} : answer;
|
|
962
|
-
}
|
|
963
|
-
case "inbox": {
|
|
964
|
-
const query = input;
|
|
965
|
-
if (engine && entries[query.key] && query.channel !== void 0) {
|
|
966
|
-
const access = accessOf(caller);
|
|
967
|
-
if (access === null || access.anywhere("sync")) return backend.inbox(caller, {
|
|
968
|
-
...query,
|
|
969
|
-
channel: await engine.inboxChannel(query.key, query.channel)
|
|
970
|
-
}, signal);
|
|
971
|
-
}
|
|
972
|
-
return backend.inbox(caller, query, signal);
|
|
973
|
-
}
|
|
974
|
-
default: {
|
|
975
|
-
const method = backend[name];
|
|
976
|
-
if (typeof method !== "function") throw notFound(`no route: ${name}`);
|
|
977
|
-
return method.call(backend, caller, input, signal);
|
|
978
|
-
}
|
|
979
|
-
}
|
|
980
|
-
}
|
|
981
|
-
/** board()'s own routes for a caller: sync's, which run where board() does. */
|
|
982
|
-
async function serve(name, caller, request, url, params) {
|
|
983
|
-
const signal = request.signal;
|
|
984
|
-
switch (name) {
|
|
985
|
-
case "sync": {
|
|
986
|
-
const body = SyncBody.parse(await readJson(request));
|
|
987
|
-
const id = params.id ?? "";
|
|
988
|
-
const access = accessOf(caller);
|
|
989
|
-
if (access && engine) {
|
|
990
|
-
const message = await lookup(caller, id, signal);
|
|
991
|
-
if (!message) throw notFound(`message not found: #${id}`, "message_not_found");
|
|
992
|
-
if (!access.can("sync", message.channel)) throw forbidden(`this token can't sync in ${message.channel}`);
|
|
993
|
-
}
|
|
994
|
-
const target = await resolveTarget(body.target);
|
|
995
|
-
const { attached } = await backend.syncReceipt(caller, {
|
|
996
|
-
kind: "thread",
|
|
997
|
-
message: id,
|
|
998
|
-
target: target.target,
|
|
999
|
-
targetKind: target.kind,
|
|
1000
|
-
cause: "request"
|
|
1001
|
-
}, signal);
|
|
1002
|
-
return json({
|
|
1003
|
-
target: target.target,
|
|
1004
|
-
attached: attached ?? true
|
|
1005
|
-
});
|
|
1006
|
-
}
|
|
1007
|
-
default: throw notFound(`no route: ${request.method} ${url.pathname}`);
|
|
1008
|
-
}
|
|
1009
|
-
}
|
|
1010
|
-
const synced = Object.fromEntries(METHODS.map((name) => [name, name === "syncImport" ? syncImport : (caller, input, signal) => backend[name].call(backend, caller, input, signal)]));
|
|
1011
|
-
engine?.bind({
|
|
1012
|
-
backend: synced,
|
|
1013
|
-
client: (platform, trigger) => clientAs({
|
|
1014
|
-
platform: {
|
|
1015
|
-
author: platform.author,
|
|
1016
|
-
key: platform.key,
|
|
1017
|
-
respond: platform.respond
|
|
1018
|
-
},
|
|
1019
|
-
origin: { via: platform.via }
|
|
1020
|
-
}, trigger)
|
|
1021
|
-
});
|
|
1022
|
-
/**
|
|
1023
|
-
* Server-sent events, from the backend's long-poll: each event's `id` is its cursor, so
|
|
1024
|
-
* `Last-Event-ID` resumes exactly. A request's caller is worked out again for each page, so what
|
|
1025
|
-
* `access` says now holds, and a token that runs out ends it.
|
|
1026
|
-
*/
|
|
1027
|
-
async function sse(caller, query, request, identify) {
|
|
1028
|
-
const first = await backend.events(caller, {
|
|
1029
|
-
...query,
|
|
1030
|
-
wait: 0
|
|
1031
|
-
}, request.signal);
|
|
1032
|
-
const encoder = new TextEncoder();
|
|
1033
|
-
const controller = new AbortController();
|
|
1034
|
-
const deadline = Date.now() + streamMaxMs;
|
|
1035
|
-
const signal = AbortSignal.any([
|
|
1036
|
-
request.signal,
|
|
1037
|
-
controller.signal,
|
|
1038
|
-
AbortSignal.timeout(streamMaxMs)
|
|
1039
|
-
]);
|
|
1040
|
-
const body = new ReadableStream({
|
|
1041
|
-
async start(stream) {
|
|
1042
|
-
const send = (text) => {
|
|
1043
|
-
try {
|
|
1044
|
-
stream.enqueue(encoder.encode(text));
|
|
1045
|
-
} catch {}
|
|
1046
|
-
};
|
|
1047
|
-
const ping = setInterval(() => send(": ping\n\n"), 15e3);
|
|
1048
|
-
try {
|
|
1049
|
-
send(": connected\n\n");
|
|
1050
|
-
let page = first;
|
|
1051
|
-
let after;
|
|
1052
|
-
for (;;) {
|
|
1053
|
-
for (const event of page.events) send(`id: ${event.cursor}\nevent: board\ndata: ${JSON.stringify(event)}\n\n`);
|
|
1054
|
-
if (page.cursor !== (page.events.at(-1)?.cursor ?? after)) send(`id: ${page.cursor}\n\n`);
|
|
1055
|
-
after = page.cursor;
|
|
1056
|
-
const left = deadline - Date.now();
|
|
1057
|
-
if (signal.aborted || left <= 0) break;
|
|
1058
|
-
page = await backend.events(identify ? await identify() : caller, {
|
|
1059
|
-
...query,
|
|
1060
|
-
after,
|
|
1061
|
-
wait: Math.min(300, left / 1e3)
|
|
1062
|
-
}, signal);
|
|
1063
|
-
}
|
|
1064
|
-
} catch (error) {
|
|
1065
|
-
if (!signal.aborted) console.error("[board] feed failed", error);
|
|
1066
|
-
} finally {
|
|
1067
|
-
clearInterval(ping);
|
|
1068
|
-
try {
|
|
1069
|
-
stream.close();
|
|
1070
|
-
} catch {}
|
|
1071
|
-
}
|
|
1072
|
-
},
|
|
1073
|
-
cancel() {
|
|
1074
|
-
controller.abort();
|
|
1075
|
-
}
|
|
1076
|
-
});
|
|
1077
|
-
return new Response(body, { headers: {
|
|
1078
|
-
"content-type": "text/event-stream",
|
|
1079
|
-
"cache-control": "no-cache, no-transform",
|
|
1080
|
-
connection: "keep-alive"
|
|
1081
|
-
} });
|
|
1082
|
-
}
|
|
1083
|
-
/**
|
|
1084
|
-
* Discovery, as a request reaches the board: who the board is to it (the URL a token must be minted
|
|
1085
|
-
* for, and the project people mint them with), which is board()'s to say, and the sync entries
|
|
1086
|
-
* accounts link on. Those are the backend's, where it keeps the board; a board on `http()` lists its own.
|
|
1087
|
-
*/
|
|
1088
|
-
async function discovery(request, url, signal) {
|
|
1089
|
-
const { projectId, teamId } = await policy.discoveryIdentity(request.headers);
|
|
1090
|
-
const sync = backend.keeps ? (await backend.discovery(OWN, {}, signal)).sync : Object.entries(entries).map(([key, entry]) => ({
|
|
1091
|
-
key,
|
|
1092
|
-
platform: entry.platform
|
|
1093
|
-
}));
|
|
1094
|
-
return {
|
|
1095
|
-
version: VERSION,
|
|
1096
|
-
url,
|
|
1097
|
-
audience: url,
|
|
1098
|
-
team_id: teamId,
|
|
1099
|
-
project_id: projectId,
|
|
1100
|
-
sync
|
|
1101
|
-
};
|
|
1102
|
-
}
|
|
1103
|
-
async function dispatch(request, injected) {
|
|
1104
|
-
try {
|
|
1105
|
-
const url = new URL(request.url);
|
|
1106
|
-
const match = route(request.method, url.pathname);
|
|
1107
|
-
if (!match) throw notFound(`no route: ${request.method} ${url.pathname}`);
|
|
1108
|
-
const { target } = match;
|
|
1109
|
-
if (!injected && !match.root && (state.learned === null || state.learned.startsWith(`${match.base}/`))) state.learned = match.base;
|
|
1110
|
-
const boardUrl = (options.url ?? `${url.origin}${match.base}`).replace(/\/+$/, "");
|
|
1111
|
-
if (target.kind === "board" && target.name === "webhook") {
|
|
1112
|
-
if (!engine || !entries[match.params.key ?? ""]) throw notFound(`no sync entry named ${match.params.key}`);
|
|
1113
|
-
return await engine.handleWebhook(match.params.key, request);
|
|
1114
|
-
}
|
|
1115
|
-
if (target.kind === "board" && target.name === "avatar") {
|
|
1116
|
-
const name = match.param ?? "";
|
|
1117
|
-
if (!KNOWN_AGENTS.includes(name)) throw notFound(`no avatar: ${name}`);
|
|
1118
|
-
return new Response(avatarSvg(name), { headers: {
|
|
1119
|
-
"content-type": "image/svg+xml",
|
|
1120
|
-
"cache-control": "public, max-age=86400"
|
|
1121
|
-
} });
|
|
1122
|
-
}
|
|
1123
|
-
if (target.kind === "board" && target.name === "openapi") return json(openapi());
|
|
1124
|
-
if (target.kind === "backend") return await target.route.handle(request, {
|
|
1125
|
-
caller: injected && "platform" in injected ? engine.caller : injected,
|
|
1126
|
-
param: match.param
|
|
1127
|
-
});
|
|
1128
|
-
if (!injected && !policy.open) throw servesOnly();
|
|
1129
|
-
if (!injected && target.kind === "protocol" && target.name === "discovery") return json(await discovery(request, boardUrl, request.signal));
|
|
1130
|
-
if (target.kind === "protocol" && typeof backend[target.name] !== "function") throw notFound(`no route: ${request.method} ${url.pathname}`);
|
|
1131
|
-
const own = injected && "platform" in injected ? engine.caller : injected;
|
|
1132
|
-
const asked = own ? null : requestOf(request, boardUrl);
|
|
1133
|
-
const caller = own ?? await callerOf(asked);
|
|
1134
|
-
if (target.kind === "board") return await serve(target.name, caller, request, url, match.params);
|
|
1135
|
-
const input = await inputOf(target.name, request, url, match.params);
|
|
1136
|
-
if (target.name === "events" && (request.headers.get("accept") ?? "").includes("text/event-stream")) return await sse(caller, await withMentioned(caller, input), request, asked ? () => callerOf(asked) : null);
|
|
1137
|
-
const output = injected && "platform" in injected ? await asPlatform(target.name, injected, input, request.signal) : await invoke(target.name, caller, input, request.signal);
|
|
1138
|
-
if (target.name === "attachment") return attachmentResponse(output);
|
|
1139
|
-
const made = CREATED.has(target.name) || target.name === "createChannel" && output.created;
|
|
1140
|
-
return json(output, made ? 201 : 200);
|
|
1141
|
-
} catch (error) {
|
|
1142
|
-
return toErrorResponse(error);
|
|
1143
|
-
}
|
|
1144
|
-
}
|
|
1145
|
-
return {
|
|
1146
|
-
dispatch,
|
|
1147
|
-
async inject(request, caller, trigger, writes) {
|
|
1148
|
-
if (trigger !== null && request.method === "POST" && new URL(request.url).pathname.endsWith("/messages")) {
|
|
1149
|
-
const body = await request.json() ?? {};
|
|
1150
|
-
body.id ??= await derivedId("board-inbound", trigger, "client", String(writes.made++));
|
|
1151
|
-
request = new Request(request.url, {
|
|
1152
|
-
method: "POST",
|
|
1153
|
-
headers: request.headers,
|
|
1154
|
-
body: JSON.stringify(body)
|
|
1155
|
-
});
|
|
1156
|
-
}
|
|
1157
|
-
return dispatch(request, caller);
|
|
1158
|
-
},
|
|
1159
|
-
async run(id, event, { trigger, attempt }) {
|
|
1160
|
-
if (id === "@sync" && engine) return engine.deliver(event, { attempt });
|
|
1161
|
-
const watcher = watch[id];
|
|
1162
|
-
if (!watcher) throw new Error(`this board has no watcher ${id}`);
|
|
1163
|
-
await watcher.run({
|
|
1164
|
-
event,
|
|
1165
|
-
board: handleOf({
|
|
1166
|
-
self: id,
|
|
1167
|
-
origin: { via: {
|
|
1168
|
-
kind: "watcher",
|
|
1169
|
-
id
|
|
1170
|
-
} }
|
|
1171
|
-
}, trigger)
|
|
1172
|
-
});
|
|
1173
|
-
},
|
|
1174
|
-
async receive(key, item, { id, attempt }) {
|
|
1175
|
-
if (!engine) throw new Error(`this board has no sync, so nothing reads ${key}'s inbox`);
|
|
1176
|
-
await engine.receive(key, item, {
|
|
1177
|
-
id,
|
|
1178
|
-
attempt
|
|
1179
|
-
});
|
|
1180
|
-
},
|
|
1181
|
-
pull: () => engine ? engine.pull() : Promise.resolve(0),
|
|
1182
|
-
async readSource(key, target, limit, after) {
|
|
1183
|
-
if (!engine || !entries[key]) throw notFound(`this board doesn't run sync entry ${key}: read it where its sync runs`, "not_synced_here");
|
|
1184
|
-
return engine.readSource(key, target, limit, after);
|
|
1185
|
-
},
|
|
1186
|
-
async joinSource(key, target) {
|
|
1187
|
-
if (!engine || !entries[key]) throw notFound(`this board doesn't run sync entry ${key}: join it where its sync runs`, "not_synced_here");
|
|
1188
|
-
return engine.joinSource(key, target);
|
|
1189
|
-
},
|
|
1190
|
-
typing: async (messages, status) => engine ? engine.typing(messages, status) : []
|
|
1191
|
-
};
|
|
1192
|
-
}
|
|
1193
|
-
//#endregion
|
|
1194
|
-
//#region src/server/board.ts
|
|
1195
|
-
/** How long a handle's live events wait for the next, in seconds, before asking again. */
|
|
1196
|
-
const EVENTS_WAIT_S = 25;
|
|
1197
|
-
/**
|
|
1198
|
-
* A board for this project, on `backend`: `a2({ store, … })` keeps it in this deployment, and
|
|
1199
|
-
* `http(url)` uses another board that speaks the protocol. What it returns is the handle: its
|
|
1200
|
-
* methods act as the board's caller, and `fetch` serves the protocol over HTTP. Create it once,
|
|
1201
|
-
* at module scope, or one per tenant for a host.
|
|
1202
|
-
*/
|
|
1203
|
-
function createBoard(options, runtime) {
|
|
1204
|
-
const backend = options.backend ?? runtime.http();
|
|
1205
|
-
const policy = createPolicy(options, backend.keeps, runtime.serving, backend.trust ?? null);
|
|
1206
|
-
const watch = options.watch ?? {};
|
|
1207
|
-
const entries = options.sync ?? {};
|
|
1208
|
-
const state = { learned: null };
|
|
1209
|
-
/** Under the configured mount, so the router accepts the paths an in-process client builds. */
|
|
1210
|
-
const inProcess = `http://in-process.board${(options.basePath ?? "").replace(/\/+$/, "")}`;
|
|
1211
|
-
/**
|
|
1212
|
-
* This board's own URL, where a board elsewhere wakes it: `url`, or on Vercel its production
|
|
1213
|
-
* domain (a preview's own URL there), and never a request's Host, which its sender chooses.
|
|
1214
|
-
*/
|
|
1215
|
-
function ownUrl() {
|
|
1216
|
-
if (options.url) return options.url.replace(/\/+$/, "");
|
|
1217
|
-
const env = typeof process === "undefined" ? void 0 : process.env;
|
|
1218
|
-
const domain = env?.VERCEL_ENV === "production" ? env.VERCEL_PROJECT_PRODUCTION_URL : env?.VERCEL_URL;
|
|
1219
|
-
const base = options.basePath ?? state.learned;
|
|
1220
|
-
return domain && base !== null ? `https://${domain}${base}`.replace(/\/+$/, "") : null;
|
|
1221
|
-
}
|
|
1222
|
-
let loading = null;
|
|
1223
|
-
/** The board's pipeline, made once, when the board first serves, watches, syncs, or a handle calls it. */
|
|
1224
|
-
function server() {
|
|
1225
|
-
loading ??= createServe(context);
|
|
1226
|
-
loading.catch(() => loading = null);
|
|
1227
|
-
return loading;
|
|
1228
|
-
}
|
|
1229
|
-
function clientAs(caller, trigger) {
|
|
1230
|
-
const writes = { made: 0 };
|
|
1231
|
-
return createBoardClient({
|
|
1232
|
-
url: inProcess,
|
|
1233
|
-
token: "in-process",
|
|
1234
|
-
fetch: (async (input, init) => (await server()).inject(new Request(input, init), caller, trigger, writes))
|
|
1235
|
-
});
|
|
1236
|
-
}
|
|
1237
|
-
const serve = async (request) => (await server()).dispatch(request, null);
|
|
1238
|
-
/** A handle on `client`, which `as` gives other profiles of. */
|
|
1239
|
-
function handle(client, url) {
|
|
1240
|
-
const made = {
|
|
1241
|
-
...client,
|
|
1242
|
-
fetch: serve,
|
|
1243
|
-
as
|
|
1244
|
-
};
|
|
1245
|
-
if (url) Object.defineProperty(made, "url", {
|
|
1246
|
-
get: url,
|
|
1247
|
-
enumerable: true
|
|
1248
|
-
});
|
|
1249
|
-
return made;
|
|
1250
|
-
}
|
|
1251
|
-
/**
|
|
1252
|
-
* A handle through board(), as `caller`: a post's `id` is its own, derived under who it writes as,
|
|
1253
|
-
* so a retry posts once and no request can take it first. A verified caller's are a backend's,
|
|
1254
|
-
* derived already (a schedule's send's), so they go as they are.
|
|
1255
|
-
*/
|
|
1256
|
-
function handleOf(caller, trigger) {
|
|
1257
|
-
const client = clientAs(caller, trigger);
|
|
1258
|
-
if ("identity" in caller) return handle({
|
|
1259
|
-
...client,
|
|
1260
|
-
events: (query, opts) => events(client, query, opts)
|
|
1261
|
-
}, () => ownUrl() ?? inProcess);
|
|
1262
|
-
const via = caller.origin.via?.kind ?? "client";
|
|
1263
|
-
const id = (given) => derivedId(`board-${via === "watcher" ? "watch" : via}`, caller.self ?? "", given);
|
|
1264
|
-
const owned = async (input) => typeof input === "string" || input.id === void 0 ? input : {
|
|
1265
|
-
...input,
|
|
1266
|
-
id: await id(input.id)
|
|
1267
|
-
};
|
|
1268
|
-
return handle({
|
|
1269
|
-
...client,
|
|
1270
|
-
post: async (channel, input) => client.post(channel, await owned(input)),
|
|
1271
|
-
reply: async (re, input) => client.reply(re, await owned(input)),
|
|
1272
|
-
posted: async (given) => client.posted(await id(given)),
|
|
1273
|
-
request: (path, init = {}) => {
|
|
1274
|
-
const { as: profile, ...rest } = init;
|
|
1275
|
-
return profile !== void 0 ? as(profile).request(path, rest) : client.request(path, rest);
|
|
1276
|
-
},
|
|
1277
|
-
events: (query, opts) => events(client, query, opts)
|
|
1278
|
-
}, () => ownUrl() ?? inProcess);
|
|
1279
|
-
}
|
|
1280
|
-
const profileCaller = (profile) => {
|
|
1281
|
-
const name = profile === null ? null : typeof profile === "string" ? profile : profile.name;
|
|
1282
|
-
return {
|
|
1283
|
-
self: name,
|
|
1284
|
-
profile,
|
|
1285
|
-
origin: { via: {
|
|
1286
|
-
kind: "client",
|
|
1287
|
-
name
|
|
1288
|
-
} }
|
|
1289
|
-
};
|
|
1290
|
-
};
|
|
1291
|
-
function as(profile) {
|
|
1292
|
-
return handleOf(profileCaller(profile), null);
|
|
1293
|
-
}
|
|
1294
|
-
const context = {
|
|
1295
|
-
options,
|
|
1296
|
-
backend,
|
|
1297
|
-
runtime,
|
|
1298
|
-
policy,
|
|
1299
|
-
state,
|
|
1300
|
-
ownUrl,
|
|
1301
|
-
clientAs,
|
|
1302
|
-
handleOf
|
|
1303
|
-
};
|
|
1304
|
-
const subscriptions = Object.fromEntries(Object.entries(watch).map(([id, watcher]) => [id, {
|
|
1305
|
-
kind: "tail",
|
|
1306
|
-
tail: watcher.tail,
|
|
1307
|
-
when: watcher.when ?? "matches"
|
|
1308
|
-
}]));
|
|
1309
|
-
if (Object.keys(entries).length > 0) subscriptions[SYNC] = { kind: "sync" };
|
|
1310
|
-
/** This board's sync entries, as its sync and discovery see them. */
|
|
1311
|
-
const ownEntries = Object.entries(entries).map(([key, entry]) => ({
|
|
1312
|
-
key,
|
|
1313
|
-
platform: entry.platform
|
|
1314
|
-
}));
|
|
1315
|
-
const binding = {
|
|
1316
|
-
subscriptions,
|
|
1317
|
-
run: async (id, event, meta) => (await server()).run(id, event, meta),
|
|
1318
|
-
receive: async (key, item, meta) => (await server()).receive(key, item, meta),
|
|
1319
|
-
pull: async () => ownEntries.length > 0 ? (await server()).pull() : 0,
|
|
1320
|
-
sync: ownEntries,
|
|
1321
|
-
readSource: async (key, target, limit, after) => (await server()).readSource(key, target, limit, after),
|
|
1322
|
-
joinSource: async (key, target) => (await server()).joinSource(key, target),
|
|
1323
|
-
locate: (url) => {
|
|
1324
|
-
const read = /* @__PURE__ */ new Map();
|
|
1325
|
-
for (const entry of Object.values(entries)) {
|
|
1326
|
-
const found = entry.locate(url);
|
|
1327
|
-
if (found) read.set(JSON.stringify([
|
|
1328
|
-
entry.platform,
|
|
1329
|
-
found.thread,
|
|
1330
|
-
found.message
|
|
1331
|
-
]), {
|
|
1332
|
-
platform: entry.platform,
|
|
1333
|
-
...found
|
|
1334
|
-
});
|
|
1335
|
-
}
|
|
1336
|
-
return [...read.values()];
|
|
1337
|
-
},
|
|
1338
|
-
linkOf: async (key, thread, message) => {
|
|
1339
|
-
const entry = Object.hasOwn(entries, key) ? entries[key] : void 0;
|
|
1340
|
-
if (!entry || !thread.startsWith(`${key}:`)) return null;
|
|
1341
|
-
return entry.url(thread.slice(key.length + 1), message).catch(() => null);
|
|
1342
|
-
},
|
|
1343
|
-
typing: async (messages, status) => (await server()).typing(messages, status),
|
|
1344
|
-
accessNow: (principal) => policy.accessNow(principal),
|
|
1345
|
-
project: () => policy.project(),
|
|
1346
|
-
issuers: () => policy.issuers,
|
|
1347
|
-
handle: (caller) => handleOf(caller, null),
|
|
1348
|
-
url: ownUrl
|
|
1349
|
-
};
|
|
1350
|
-
backend.bind(binding);
|
|
1351
|
-
return handleOf({
|
|
1352
|
-
self: null,
|
|
1353
|
-
origin: { via: {
|
|
1354
|
-
kind: "client",
|
|
1355
|
-
name: null
|
|
1356
|
-
} }
|
|
1357
|
-
}, null);
|
|
1358
|
-
}
|
|
1359
|
-
/**
|
|
1360
|
-
* Live events for a handle, from the backend's long-poll: each page from the last cursor it saw,
|
|
1361
|
-
* waiting for the next. It backs off after a failure, up to 30 seconds; a 4xx throws.
|
|
1362
|
-
*/
|
|
1363
|
-
async function* events(client, query = {}, opts = {}) {
|
|
1364
|
-
let after = query.after;
|
|
1365
|
-
let failures = 0;
|
|
1366
|
-
while (!opts.signal?.aborted) {
|
|
1367
|
-
let page;
|
|
1368
|
-
try {
|
|
1369
|
-
page = await client.poll({
|
|
1370
|
-
...query,
|
|
1371
|
-
...after !== void 0 ? { after } : {},
|
|
1372
|
-
wait: EVENTS_WAIT_S
|
|
1373
|
-
}, opts.signal);
|
|
1374
|
-
} catch (error) {
|
|
1375
|
-
if (opts.signal?.aborted) return;
|
|
1376
|
-
if (error instanceof BoardError && error.status >= 400 && error.status < 500) throw error;
|
|
1377
|
-
await new Promise((resolve) => setTimeout(resolve, Math.min(3e4, 500 * 2 ** failures++)));
|
|
1378
|
-
continue;
|
|
1379
|
-
}
|
|
1380
|
-
failures = 0;
|
|
1381
|
-
for (const event of page.events) {
|
|
1382
|
-
yield event;
|
|
1383
|
-
if (opts.signal?.aborted) return;
|
|
1384
|
-
}
|
|
1385
|
-
after = page.cursor;
|
|
1386
|
-
}
|
|
1387
|
-
}
|
|
1388
|
-
//#endregion
|
|
1389
|
-
export { createBoard as t };
|