@vercel/board 0.52.0 → 0.54.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 +4 -3
- package/dist/{access-DdojoPZm.mjs → access-HzqFpgL_.mjs} +1 -1
- package/dist/access.d.mts +3 -3
- package/dist/access.mjs +5 -5
- package/dist/{actors-CDkNgWUL.mjs → actors-D-u6bitG.mjs} +2 -2
- package/dist/{auth-zPqFJocD.mjs → auth-D9ycDQK0.mjs} +34 -12
- package/dist/{auth-C4VxPLYz.d.mts → auth-ULldUvir.d.mts} +8 -2
- package/dist/{backend-Da_YEltI.d.mts → backend-BHN6GTjB.d.mts} +8 -7
- package/dist/backend-local.d.mts +1 -1
- package/dist/backend-local.mjs +1 -1
- package/dist/{chat-provider-BHyX6H8H.d.mts → chat-provider-F7UqWVZK.d.mts} +2 -2
- package/dist/cli-commands.d.mts +1 -1
- package/dist/cli-commands.mjs +1 -1
- package/dist/cli-embedded.d.mts +32 -3
- package/dist/cli-embedded.mjs +4 -2
- package/dist/cli.mjs +6 -6
- package/dist/{client-C4-r10SA.d.mts → client-CFHunEFn.d.mts} +16 -4
- package/dist/{commands-B_gbi2sU.mjs → commands-B5SXeo4W.mjs} +14 -9
- package/dist/{commands-B6pgzwTI.d.mts → commands-DLPIX7Jw.d.mts} +3 -1
- package/dist/{conformance-DW4-9Dwf.mjs → conformance-CyQG0jky.mjs} +56 -4
- package/dist/conformance.d.mts +3 -4
- package/dist/conformance.mjs +1 -1
- package/dist/{engine-D8YAsQS_.mjs → engine-C8wCzkw-.mjs} +2 -2
- package/dist/errors-DDfXnZ2b.mjs +1958 -0
- package/dist/{exports-BUtR_AHa.d.mts → exports-BBmjHyCB.d.mts} +4 -4
- package/dist/{exports-DI-MtN01.mjs → exports-JUhj5Bl3.mjs} +127 -77
- package/dist/{index-BTuTaBaK.d.mts → index-Ce3SDZ3Y.d.mts} +2 -2
- package/dist/index.d.mts +6 -6
- package/dist/index.mjs +4 -4
- package/dist/index.node.d.mts +7 -7
- package/dist/index.node.mjs +4 -4
- package/dist/inspect/app.js +69 -69
- package/dist/inspect/engine.js +3 -3
- package/dist/inspect-BN7V9e-M.mjs +2 -0
- package/dist/{inspect-BJR-BsUv.mjs → inspect-CrVs-iEi.mjs} +2 -2
- package/dist/inspect.d.mts +1 -1
- package/dist/inspect.mjs +1 -1
- package/dist/{local-B376y9u3.mjs → local-RoMUU6QX.mjs} +39 -23
- package/dist/{main-DfJNxck4.mjs → main-B_gzTjBR.mjs} +370 -102
- package/dist/{middleware-iVTNf5Su.mjs → middleware-BY4qqIh3.mjs} +1 -1
- package/dist/{provider-CGicSMdu.d.mts → provider-DgGp4Zej.d.mts} +2 -2
- package/dist/{routes-ChGBn0K3.d.mts → routes-DNmQSOvQ.d.mts} +36 -3
- package/dist/{serve-D8j7O2Tc.mjs → serve-Cz1NynhG.mjs} +6 -6
- package/dist/{server--BLSj6fd.mjs → server-CdBXNUxf.mjs} +68 -11
- package/dist/{serving-aOQsBok0.mjs → serving-C1mhZQqH.mjs} +1 -1
- package/dist/{shell-qrJL3Z_R.mjs → shell-Cqn38XUs.mjs} +1 -1
- package/dist/sync-chat-sdk.d.mts +2 -2
- package/dist/sync-github.d.mts +2 -2
- package/dist/sync-linear.d.mts +3 -3
- package/dist/sync-slack.d.mts +3 -3
- package/dist/sync.d.mts +2 -2
- package/dist/{task-batch-6UHphYfl.mjs → task-batch-CVamHB-W.mjs} +4 -5
- package/dist/{tokens-C-T0B1uF.mjs → tokens-B3gXkQCs.mjs} +2 -2
- package/dist/{version-NfsFHDfg.mjs → version-DBaKHEZS.mjs} +1 -1
- package/docs/01-board/01-mounting.md +8 -6
- package/docs/01-board/03-identity.md +1 -1
- package/docs/01-board/05-reading.md +34 -7
- package/docs/01-board/06-watchers.md +2 -1
- package/docs/01-board/09-access.md +11 -2
- package/docs/01-board/11-tasks.md +28 -5
- package/docs/01-quickstart.md +19 -5
- package/docs/03-guides/03-ci.md +71 -43
- package/docs/03-guides/04-triage.md +4 -4
- package/docs/04-reference/01-cli.md +84 -22
- package/docs/04-reference/02-board.md +19 -10
- package/docs/04-reference/03-http.md +14 -5
- package/docs/04-reference/05-conformance.md +1 -1
- package/docs/index.md +7 -5
- package/package.json +2 -2
- package/dist/errors-DbCKqGXp.mjs +0 -851
- package/dist/inspect-CYfZ0vva.mjs +0 -2
- package/dist/routes-C85Sbhtm.mjs +0 -1067
|
@@ -0,0 +1,1958 @@
|
|
|
1
|
+
import { a as DIRECT_UPLOAD_BYTES_MAX, f as typeOf, i as ATTACHMENT_BYTES_MAX, n as formatProfileHeader } from "./profile-header-O8wC8yk8.mjs";
|
|
2
|
+
import { C as SentTaskNote, M as platformAvatar, N as storable, S as SentTaskHoldKind, _ as NewLabels, c as EDITED_HOLD, d as LabelKey, f as LabelValue, g as MessageText, k as TaskState, l as Emoji, m as MESSAGE_TEXT_MAX, o as CHANNEL_PATTERN_MAX, p as Labels, r as AttachmentId, s as ChannelPath, t as ActorId, w as SentText } from "./schemas-CUSTEFIy.mjs";
|
|
3
|
+
import { a as Link, i as ExternalTarget, n as ExternalEvent, o as Origin, t as ExternalAuthor } from "./external-DkWafjn-.mjs";
|
|
4
|
+
import { r as normalizeEmoji } from "./emoji-CTLpvH-A.mjs";
|
|
5
|
+
import { ZodError, z } from "zod";
|
|
6
|
+
//#region src/api/routes.ts
|
|
7
|
+
/**
|
|
8
|
+
* A message's, schedule's, or cursor's ID: a string clients pass back as they got it, and never
|
|
9
|
+
* parse, compare, or count with. Each backend picks its own; a board on `local()` writes decimals,
|
|
10
|
+
* and `s12` for a schedule.
|
|
11
|
+
*/
|
|
12
|
+
const Ref = z.string().min(1).max(128);
|
|
13
|
+
/** The message's `revision` as it was read: if it's been edited or deleted since, 409 `revision_conflict`. */
|
|
14
|
+
const Revision = z.number().int().positive();
|
|
15
|
+
const id = Ref;
|
|
16
|
+
/** A boolean query parameter: `true` or `1`, `false` or `0`. */
|
|
17
|
+
const flag = z.enum([
|
|
18
|
+
"true",
|
|
19
|
+
"false",
|
|
20
|
+
"1",
|
|
21
|
+
"0"
|
|
22
|
+
]).transform((value) => value === "true" || value === "1");
|
|
23
|
+
const repeated = z.union([z.string(), z.array(z.string())]).transform((value) => Array.isArray(value) ? value : [value]);
|
|
24
|
+
/**
|
|
25
|
+
* `pr` → `['pr', null]` (has the label), `status=open` → `['status', ['open']]`,
|
|
26
|
+
* `state=open|draft` → either value.
|
|
27
|
+
*/
|
|
28
|
+
const LabelFilter = z.string().regex(/^[a-z0-9][a-z0-9_.-]*(=.*)?$/, "label filters look like key, key=value, or key=a|b").transform((value) => {
|
|
29
|
+
const at = value.indexOf("=");
|
|
30
|
+
return at === -1 ? [value, null] : [value.slice(0, at), value.slice(at + 1).split("|")];
|
|
31
|
+
});
|
|
32
|
+
/** A sync target as the board that syncs it resolved it: a platform channel to start a thread in, or a thread to join. */
|
|
33
|
+
const ResolvedSyncTarget = z.object({
|
|
34
|
+
target: ExternalTarget,
|
|
35
|
+
kind: z.enum(["channel", "thread"])
|
|
36
|
+
});
|
|
37
|
+
/** Shared by `/threads` and `/events`. */
|
|
38
|
+
const Filters = z.object({
|
|
39
|
+
channel: z.string().optional(),
|
|
40
|
+
re: id.optional(),
|
|
41
|
+
by: z.string().optional(),
|
|
42
|
+
mentions: z.string().optional(),
|
|
43
|
+
label: repeated.pipe(z.array(LabelFilter)).optional(),
|
|
44
|
+
/**
|
|
45
|
+
* Where messages came from: a sync key such as `slack`, or `board` for the board itself. On Vercel's board, another
|
|
46
|
+
* project's key is `<project>/<key>`, by its name (up to 100 characters) or its ID.
|
|
47
|
+
*/
|
|
48
|
+
from: z.string().min(1).max(165).optional(),
|
|
49
|
+
/** Case-insensitive words the text contains. */
|
|
50
|
+
text: z.string().min(1).max(200).optional()
|
|
51
|
+
});
|
|
52
|
+
/**
|
|
53
|
+
* One channel's messages across its threads, oldest first: the newest `limit`, or with `after`, a cursor a read of
|
|
54
|
+
* this channel answered, the ones posted since. The channel is one channel by its whole name, never a pattern: a board
|
|
55
|
+
* lists no messages across channels.
|
|
56
|
+
*/
|
|
57
|
+
const ChannelMessagesQuery = z.strictObject({
|
|
58
|
+
channel: ChannelPath,
|
|
59
|
+
after: Ref.optional(),
|
|
60
|
+
limit: z.coerce.number().int().min(1).max(500).optional()
|
|
61
|
+
});
|
|
62
|
+
/**
|
|
63
|
+
* A page of the board's channels: those whose path starts with `prefix` (`dev/` is every channel under `dev`, and
|
|
64
|
+
* not `dev` itself), `limit` of them, after a page's `next`.
|
|
65
|
+
*/
|
|
66
|
+
const ChannelsQuery = z.strictObject({
|
|
67
|
+
prefix: z.string().min(1).max(501).optional(),
|
|
68
|
+
after: z.string().min(1).max(1024).optional(),
|
|
69
|
+
limit: z.coerce.number().int().min(1).max(500).optional()
|
|
70
|
+
});
|
|
71
|
+
/** The board's actors, or with `id`, only the ones it names: someone it doesn't have is left out. */
|
|
72
|
+
const ActorsQuery = z.strictObject({ id: repeated.pipe(z.array(z.string().min(1).max(300)).min(1).max(100)).optional() });
|
|
73
|
+
/**
|
|
74
|
+
* A thread read in part: the root, and of its replies the ones after one of its messages (`after`), `limit` of
|
|
75
|
+
* them. With `after` they're the first after it, to read on from; without, the newest.
|
|
76
|
+
*/
|
|
77
|
+
const ThreadQuery = z.strictObject({
|
|
78
|
+
after: id.optional(),
|
|
79
|
+
limit: z.coerce.number().int().min(1).max(500).optional()
|
|
80
|
+
});
|
|
81
|
+
/**
|
|
82
|
+
* `limit` counts threads: each thread with a matching message, by its latest reply. `before` is a page's
|
|
83
|
+
* `older`, for the threads last active before it; it pages backward, so it takes no `after`.
|
|
84
|
+
*/
|
|
85
|
+
const ThreadsQuery = Filters.extend({
|
|
86
|
+
after: Ref.optional(),
|
|
87
|
+
before: z.string().min(1).max(1024).optional(),
|
|
88
|
+
limit: z.coerce.number().int().min(1).max(500).optional()
|
|
89
|
+
}).refine((query) => query.before === void 0 || query.after === void 0, {
|
|
90
|
+
message: "`before` pages back from a page's `older`, and `after` reads forward: pass one",
|
|
91
|
+
path: ["before"]
|
|
92
|
+
});
|
|
93
|
+
/**
|
|
94
|
+
* A read of the feed after `after`, or from now. `last` reads its newest events instead, oldest first, with the
|
|
95
|
+
* feed's end as the cursor: it answers at once, so it takes no `after` or `wait`.
|
|
96
|
+
*/
|
|
97
|
+
const EventsQuery = Filters.extend({
|
|
98
|
+
after: Ref.optional(),
|
|
99
|
+
wait: z.coerce.number().min(0).max(300).optional(),
|
|
100
|
+
all: flag.optional(),
|
|
101
|
+
last: z.coerce.number().int().min(1).max(100).optional()
|
|
102
|
+
}).refine((query) => query.last === void 0 || query.after === void 0 && query.wait === void 0, {
|
|
103
|
+
message: "`last` reads the newest events at once: it takes no `after` or `wait`",
|
|
104
|
+
path: ["last"]
|
|
105
|
+
});
|
|
106
|
+
const PostBody = z.object({
|
|
107
|
+
channel: ChannelPath.optional(),
|
|
108
|
+
re: Ref.optional(),
|
|
109
|
+
/** May be empty when the message has attachments. */
|
|
110
|
+
text: z.string().default(""),
|
|
111
|
+
labels: NewLabels.optional(),
|
|
112
|
+
noSync: z.boolean().optional(),
|
|
113
|
+
/**
|
|
114
|
+
* External targets a new thread starts with: `slack:#incidents`, `github:owner/repo:12`. The
|
|
115
|
+
* board that syncs a target resolves it to `{ target, kind }`, which a board elsewhere takes as is.
|
|
116
|
+
*/
|
|
117
|
+
sync: z.array(z.union([z.string().min(3).max(300), ResolvedSyncTarget])).max(10).optional(),
|
|
118
|
+
id: z.string().min(1).max(200).refine(storable, "id has a NUL character or a lone surrogate").optional(),
|
|
119
|
+
/** IDs from `POST /attachments`. */
|
|
120
|
+
attachments: z.array(AttachmentId).optional(),
|
|
121
|
+
/**
|
|
122
|
+
* A reply's precondition: a message in its thread, the newest as the caller read it. If another
|
|
123
|
+
* has come after it, nothing is posted, and the board answers 409 `newer_message`.
|
|
124
|
+
*/
|
|
125
|
+
after: Ref.optional()
|
|
126
|
+
}).refine((body) => body.text.trim().length > 0 || (body.attachments?.length ?? 0) > 0, {
|
|
127
|
+
message: "message text is empty",
|
|
128
|
+
path: ["text"]
|
|
129
|
+
}).refine((body) => body.channel === void 0 !== (body.re === void 0), { message: "a post needs `channel`; a reply needs `re`" }).refine((body) => !(body.noSync && body.sync?.length), { message: "`noSync` and `sync` contradict each other" }).refine((body) => body.after === void 0 || body.re !== void 0, {
|
|
130
|
+
message: "`after` is for replies: it names the newest message in `re`'s thread",
|
|
131
|
+
path: ["after"]
|
|
132
|
+
});
|
|
133
|
+
/** `30m`, `1d`: a number and a unit, `s`, `m`, `h`, or `d`. */
|
|
134
|
+
const Duration = z.string().regex(/^\d+(\.\d+)?(s|m|h|d)$/, "durations look like 30s, 30m, 12h, or 1d");
|
|
135
|
+
const Instant = z.string().datetime({ offset: true });
|
|
136
|
+
/**
|
|
137
|
+
* A message to post later, as the caller: once (`in` or `at`), or repeating
|
|
138
|
+
* (`every`, starting `at` or `in` from now, or one `every` from now), until
|
|
139
|
+
* `until`, `for` after the first send, or `times` sends. A repeat with no stop
|
|
140
|
+
* runs until it's canceled.
|
|
141
|
+
*/
|
|
142
|
+
const ScheduleBody = z.object({
|
|
143
|
+
channel: ChannelPath.optional(),
|
|
144
|
+
re: Ref.optional(),
|
|
145
|
+
text: z.string().default(""),
|
|
146
|
+
labels: NewLabels.optional(),
|
|
147
|
+
noSync: z.boolean().optional(),
|
|
148
|
+
attachments: z.array(AttachmentId).optional(),
|
|
149
|
+
in: Duration.optional(),
|
|
150
|
+
at: Instant.optional(),
|
|
151
|
+
every: Duration.optional(),
|
|
152
|
+
until: Instant.optional(),
|
|
153
|
+
for: Duration.optional(),
|
|
154
|
+
times: z.number().int().positive().optional()
|
|
155
|
+
}).refine((body) => body.text.trim().length > 0 || (body.attachments?.length ?? 0) > 0, {
|
|
156
|
+
message: "message text is empty",
|
|
157
|
+
path: ["text"]
|
|
158
|
+
}).refine((body) => body.channel === void 0 !== (body.re === void 0), { message: "a schedule needs `channel` for a new thread, or `re` for a reply" }).refine((body) => body.in !== void 0 || body.at !== void 0 || body.every !== void 0, { message: "say when: `in`, `at`, or `every`" }).refine((body) => !(body.in !== void 0 && body.at !== void 0), { message: "`in` and `at` contradict each other" }).refine((body) => [
|
|
159
|
+
body.until,
|
|
160
|
+
body.for,
|
|
161
|
+
body.times
|
|
162
|
+
].filter((v) => v !== void 0).length <= 1, { message: "pick one stop: `until`, `for`, or `times`" }).refine((body) => body.every !== void 0 || body.until === void 0 && body.for === void 0 && body.times === void 0, { message: "`until`, `for`, and `times` stop a repeat: add `every`" });
|
|
163
|
+
const SchedulesQuery = z.object({
|
|
164
|
+
/** Any message in a thread: the schedules that reply in it. */
|
|
165
|
+
re: id.optional(),
|
|
166
|
+
channel: z.string().optional(),
|
|
167
|
+
/** An actor ID or handle: the schedules it created. */
|
|
168
|
+
by: z.string().optional(),
|
|
169
|
+
/** Include done and canceled schedules. */
|
|
170
|
+
all: flag.optional()
|
|
171
|
+
});
|
|
172
|
+
/** A channel, or with a trailing `/` it and every channel under it: `work`, `work/`. As long as a grant's pattern for either. */
|
|
173
|
+
const ChannelOrSubtree = z.string().max(CHANNEL_PATTERN_MAX).regex(/^[a-z0-9][a-z0-9_-]*(?:\/[a-z0-9][a-z0-9_-]*)*\/?$/, "name a channel, or a channel and its subtree (work/)");
|
|
174
|
+
/**
|
|
175
|
+
* How long a claim holds its task, in seconds, 900 when left out. How short and how long one may
|
|
176
|
+
* be is the backend's to say: the local board takes 30 to 3600, and a board gives what's asked up
|
|
177
|
+
* to its own cap, never under 300.
|
|
178
|
+
*/
|
|
179
|
+
const Lease = z.number();
|
|
180
|
+
/** What a claim's own writes carry: the `epoch` and `token` its claim answered. */
|
|
181
|
+
const Claimed = {
|
|
182
|
+
epoch: z.number().int().positive(),
|
|
183
|
+
token: z.string().min(1).max(128)
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* A claim's token can be the caller's own: a secret it can derive again anywhere, as an HMAC-SHA256
|
|
187
|
+
* of its run's ID in base64url, so no store has to remember it. It's 32 bytes in base64url, as
|
|
188
|
+
* Vercel's API takes one. Claiming a task its live claim holds with that claim's token answers the
|
|
189
|
+
* claim as it is.
|
|
190
|
+
*/
|
|
191
|
+
const ClaimToken = { token: z.string().regex(/^[A-Za-z0-9_-]{43}$/, "a claim token is 32 bytes in base64url: 43 characters").optional() };
|
|
192
|
+
/**
|
|
193
|
+
* A new thread that's a task (`channel` and `text`, with `labels`, `noSync`, `attachments`, and an
|
|
194
|
+
* `id` as a post takes them), or the thread `re` roots made one. `needs` are the tasks it waits for,
|
|
195
|
+
* `for` the one actor of its owner's that claims it, and `claim` claims it in the same write, for
|
|
196
|
+
* `lease` seconds.
|
|
197
|
+
*/
|
|
198
|
+
const CreateTaskBody = z.object({
|
|
199
|
+
channel: ChannelPath.optional(),
|
|
200
|
+
re: Ref.optional(),
|
|
201
|
+
/** As long as a post's text may be: the backend says how long that is. */
|
|
202
|
+
text: z.string().optional(),
|
|
203
|
+
labels: NewLabels.optional(),
|
|
204
|
+
noSync: z.boolean().optional(),
|
|
205
|
+
/** IDs from `POST /attachments`, as a post takes them. */
|
|
206
|
+
attachments: z.array(AttachmentId).optional(),
|
|
207
|
+
/** How many tasks one may wait for is the backend's to say. */
|
|
208
|
+
needs: z.array(Ref).optional(),
|
|
209
|
+
for: ActorId.nullable().optional(),
|
|
210
|
+
claim: z.boolean().optional(),
|
|
211
|
+
lease: Lease.optional(),
|
|
212
|
+
...ClaimToken,
|
|
213
|
+
id: z.string().min(1).max(200).refine(storable, "id has a NUL character or a lone surrogate").optional()
|
|
214
|
+
}).refine((body) => body.channel === void 0 !== (body.re === void 0), { message: "a task needs `channel` and `text` for a new thread, or `re` for a thread that's there" }).refine((body) => body.channel === void 0 || (body.text ?? "").trim().length > 0 || (body.attachments?.length ?? 0) > 0, {
|
|
215
|
+
message: "message text is empty",
|
|
216
|
+
path: ["text"]
|
|
217
|
+
}).refine((body) => body.text === void 0 || storable(body.text), {
|
|
218
|
+
message: "message text has a NUL character or a lone surrogate",
|
|
219
|
+
path: ["text"]
|
|
220
|
+
}).refine((body) => body.re === void 0 || body.text === void 0 && body.labels === void 0 && body.noSync === void 0 && body.attachments === void 0 && body.id === void 0, { message: "`text`, `labels`, `noSync`, `attachments`, and `id` are a new thread's: `re` makes a task of a thread as it is" }).refine((body) => body.lease === void 0 || body.claim === true, {
|
|
221
|
+
message: "`lease` goes with `claim`",
|
|
222
|
+
path: ["lease"]
|
|
223
|
+
}).refine((body) => body.token === void 0 || body.claim === true, {
|
|
224
|
+
message: "`token` goes with `claim`",
|
|
225
|
+
path: ["token"]
|
|
226
|
+
});
|
|
227
|
+
/** A digest of what a batch asks for: what tells the same request under an `id` from another. */
|
|
228
|
+
const BatchRequest = z.string().min(1).max(64);
|
|
229
|
+
/** A task another task of the same batch waits for, by the `key` it goes by there. */
|
|
230
|
+
const TaskKey = z.string().min(1).refine(storable, "key has a NUL character or a lone surrogate");
|
|
231
|
+
/**
|
|
232
|
+
* Several new threads in one channel, each a task, made in one write: all of them or none. A task is
|
|
233
|
+
* what `POST /tasks` starts a thread with, less `claim`. `key` names it for the batch's others, and
|
|
234
|
+
* `needs` takes a task's ID, or `{ key }` for one of the batch, before or after it. `id` names the
|
|
235
|
+
* batch: a retry with it answers the same tasks. At most `TASK_BATCH_MAX` tasks, 400
|
|
236
|
+
* `batch_too_large` past that, which a board that holds fewer in one write answers too.
|
|
237
|
+
*/
|
|
238
|
+
const CreateTasksBody = z.object({
|
|
239
|
+
channel: ChannelPath,
|
|
240
|
+
tasks: z.array(z.object({
|
|
241
|
+
key: TaskKey.optional(),
|
|
242
|
+
/** May be empty when the task's root has attachments. As long as a post's text may be: the backend says how long that is. */
|
|
243
|
+
text: z.string(),
|
|
244
|
+
labels: NewLabels.optional(),
|
|
245
|
+
noSync: z.boolean().optional(),
|
|
246
|
+
/** IDs from `POST /attachments`, as a post takes them. */
|
|
247
|
+
attachments: z.array(AttachmentId).optional(),
|
|
248
|
+
/** How many tasks one may wait for is the backend's to say. */
|
|
249
|
+
needs: z.array(z.union([Ref, z.object({ key: TaskKey }).strict()])).optional(),
|
|
250
|
+
for: ActorId.nullable().optional()
|
|
251
|
+
}).refine((task) => task.text.trim().length > 0 || (task.attachments?.length ?? 0) > 0, {
|
|
252
|
+
message: "message text is empty",
|
|
253
|
+
path: ["text"]
|
|
254
|
+
}).refine((task) => storable(task.text), {
|
|
255
|
+
message: "message text has a NUL character or a lone surrogate",
|
|
256
|
+
path: ["text"]
|
|
257
|
+
})).min(1).max(100),
|
|
258
|
+
id: z.string().min(1).max(200).refine(storable, "id has a NUL character or a lone surrogate").optional(),
|
|
259
|
+
/**
|
|
260
|
+
* From a board in front of this one: its digest of what the batch asked for as it was given, before
|
|
261
|
+
* its own middleware changed anything, so both boards tell a repeat by the same request. A board
|
|
262
|
+
* with none in front makes its own. It says nothing of who writes or what: a caller that sends one
|
|
263
|
+
* changes only which of its own requests its own `id` answers.
|
|
264
|
+
*/
|
|
265
|
+
request: BatchRequest.optional()
|
|
266
|
+
});
|
|
267
|
+
/**
|
|
268
|
+
* `request` is a board's digest of what a batch asked for, as it was given: with it, the batch
|
|
269
|
+
* answers only that request, and any other under the `id` is 409 `duplicate_id`.
|
|
270
|
+
*/
|
|
271
|
+
const CreatedTasksQuery = z.object({ request: BatchRequest.optional() });
|
|
272
|
+
/** Which tasks to list: open ones unless `state` says `all` or names one. A claim whose lease has run out counts as `ready`. */
|
|
273
|
+
const TasksQuery = z.object({
|
|
274
|
+
channel: z.string().optional(),
|
|
275
|
+
state: z.union([z.enum(["open", "all"]), TaskState]).optional(),
|
|
276
|
+
label: repeated.pipe(z.array(LabelFilter)).optional(),
|
|
277
|
+
/** Who wrote the root: a handle or an actor ID, as on `/messages`. */
|
|
278
|
+
by: z.string().optional(),
|
|
279
|
+
/** Whose task it is: a handle or a principal ID. */
|
|
280
|
+
owner: z.string().optional(),
|
|
281
|
+
/** The actor it's assigned to: a handle or an actor ID. */
|
|
282
|
+
for: z.string().optional(),
|
|
283
|
+
/** A task's ID: the tasks that wait for it. */
|
|
284
|
+
needs: Ref.optional(),
|
|
285
|
+
/** A `cursor` an earlier page reported. */
|
|
286
|
+
after: Ref.optional(),
|
|
287
|
+
limit: z.coerce.number().int().min(1).max(500).optional(),
|
|
288
|
+
/** The `limit` last active instead of the first made, the latest active last: the board reads to the end, and takes no `after`. */
|
|
289
|
+
latest: flag.optional()
|
|
290
|
+
}).refine((query) => !query.latest || query.after === void 0, {
|
|
291
|
+
message: "`latest` reads every task to find the last active: it takes no `after`",
|
|
292
|
+
path: ["latest"]
|
|
293
|
+
});
|
|
294
|
+
const ClaimTaskBody = z.object({
|
|
295
|
+
lease: Lease.optional(),
|
|
296
|
+
...ClaimToken
|
|
297
|
+
}).optional();
|
|
298
|
+
/**
|
|
299
|
+
* Claims the oldest ready task in `channel` that has every label in `label`: one assigned to the
|
|
300
|
+
* caller first, then an unassigned one it owns, or whose owner `from` names by ID. `wait`
|
|
301
|
+
* holds the request up to that many seconds for one.
|
|
302
|
+
*/
|
|
303
|
+
const ClaimReadyTaskBody = z.object({
|
|
304
|
+
channel: ChannelOrSubtree,
|
|
305
|
+
label: Labels.optional(),
|
|
306
|
+
/** How many owners one claim takes from is the backend's to say. */
|
|
307
|
+
from: z.array(z.string().min(1).max(256)).optional(),
|
|
308
|
+
lease: Lease.optional(),
|
|
309
|
+
wait: z.number().min(0).max(300).optional(),
|
|
310
|
+
...ClaimToken
|
|
311
|
+
});
|
|
312
|
+
const RenewTaskBody = z.object({
|
|
313
|
+
...Claimed,
|
|
314
|
+
lease: Lease.optional()
|
|
315
|
+
});
|
|
316
|
+
const ReleaseTaskBody = z.object(Claimed);
|
|
317
|
+
const CompleteTaskBody = z.object({
|
|
318
|
+
...Claimed,
|
|
319
|
+
result: SentTaskNote.optional()
|
|
320
|
+
});
|
|
321
|
+
const FailTaskBody = z.object({
|
|
322
|
+
...Claimed,
|
|
323
|
+
reason: SentTaskNote
|
|
324
|
+
});
|
|
325
|
+
/** A hold for a person. A claimed task's comes from its claim, with the claim's `epoch` and `token`. Kind `edited` is the board's own, so none asks for it. */
|
|
326
|
+
const BlockTaskBody = z.object({
|
|
327
|
+
reason: SentTaskNote,
|
|
328
|
+
kind: SentTaskHoldKind.refine((kind) => kind !== EDITED_HOLD, "`edited` is the board's own kind, for a task whose root someone else edited: name another").optional(),
|
|
329
|
+
epoch: Claimed.epoch.optional(),
|
|
330
|
+
token: Claimed.token.optional()
|
|
331
|
+
}).refine((body) => body.epoch === void 0 === (body.token === void 0), { message: "a claim is its `epoch` and its `token`: pass both, or neither" });
|
|
332
|
+
/** The one actor of its owner's that claims the task, or null for anyone who may. */
|
|
333
|
+
const AssignTaskBody = z.object({ for: ActorId.nullable() });
|
|
334
|
+
/** The tasks it waits for now, in place of what it waited for: none, for a task that waits for nothing. How many it may wait for is the backend's to say. */
|
|
335
|
+
const TaskNeedsBody = z.object({ needs: z.array(Ref) });
|
|
336
|
+
const CancelTaskBody = z.object({ reason: SentTaskNote });
|
|
337
|
+
const CanGrant = z.union([z.literal("all"), z.array(z.string().min(1).max(16)).max(8)]);
|
|
338
|
+
const ClaimValue = z.string().min(1).max(1e3);
|
|
339
|
+
const Environments = z.array(z.string().min(1).max(100)).min(1).max(16);
|
|
340
|
+
/**
|
|
341
|
+
* Who a grant names, as people write it: a user by email (or, where the board can't look one up,
|
|
342
|
+
* by ID), a project by name or ID, a repository by `owner/name`. The board resolves names and keeps
|
|
343
|
+
* IDs; a board that verifies tokens itself takes IDs only. A `user` names `email` or `userId`.
|
|
344
|
+
* Nothing else: a key it doesn't know (a misspelled `workflows`, say) is a 400, never dropped,
|
|
345
|
+
* since dropping it would name more than was meant.
|
|
346
|
+
*/
|
|
347
|
+
const PrincipalInput = z.discriminatedUnion("kind", [
|
|
348
|
+
z.strictObject({
|
|
349
|
+
kind: z.literal("user"),
|
|
350
|
+
email: z.string().min(3).max(320).optional(),
|
|
351
|
+
userId: z.string().min(1).max(200).optional()
|
|
352
|
+
}),
|
|
353
|
+
z.strictObject({
|
|
354
|
+
kind: z.literal("project"),
|
|
355
|
+
project: z.string().min(1).max(200),
|
|
356
|
+
environments: Environments.optional()
|
|
357
|
+
}),
|
|
358
|
+
z.strictObject({
|
|
359
|
+
kind: z.literal("github"),
|
|
360
|
+
repository: z.string().min(3).max(200),
|
|
361
|
+
workflow: z.string().min(1).max(300).optional(),
|
|
362
|
+
branch: z.string().min(1).max(255).optional(),
|
|
363
|
+
event: z.string().min(1).max(64).optional()
|
|
364
|
+
}),
|
|
365
|
+
z.strictObject({
|
|
366
|
+
kind: z.literal("oidc"),
|
|
367
|
+
issuer: z.string().min(1).max(1e3),
|
|
368
|
+
claims: z.record(z.string().min(1).max(200), z.union([ClaimValue, z.array(ClaimValue).min(1).max(32)]))
|
|
369
|
+
}),
|
|
370
|
+
z.strictObject({ kind: z.literal("all-users") }),
|
|
371
|
+
z.strictObject({
|
|
372
|
+
kind: z.literal("all-projects"),
|
|
373
|
+
environments: Environments.optional()
|
|
374
|
+
}),
|
|
375
|
+
z.strictObject({
|
|
376
|
+
kind: z.literal("app"),
|
|
377
|
+
app: z.string().min(1).max(200)
|
|
378
|
+
}),
|
|
379
|
+
z.strictObject({ kind: z.literal("all-apps") }),
|
|
380
|
+
z.strictObject({ kind: z.literal("everyone") })
|
|
381
|
+
]);
|
|
382
|
+
/** What a grant lets its principals do: a map from channel pattern to `'all'` or a list, `[]` for none. Kept as written. */
|
|
383
|
+
const GrantCan = z.record(z.string().min(1).max(CHANNEL_PATTERN_MAX), CanGrant);
|
|
384
|
+
/**
|
|
385
|
+
* Says to go ahead with a grant change its caller couldn't take back: one that takes their own
|
|
386
|
+
* `manage` at a pattern it names, which is 409 `grant_lockout` without it.
|
|
387
|
+
*/
|
|
388
|
+
const LockMeOut = z.boolean().optional();
|
|
389
|
+
/** A grant: who it names, and one `can` for all of them. The same principals again are another grant. */
|
|
390
|
+
const GrantSpec = z.strictObject({
|
|
391
|
+
principals: z.array(PrincipalInput).min(1).max(32),
|
|
392
|
+
can: GrantCan
|
|
393
|
+
});
|
|
394
|
+
/** What `POST /grants` takes: a grant, and `lockMeOut` to make one its caller couldn't take back. */
|
|
395
|
+
const GrantBody = GrantSpec.extend({ lockMeOut: LockMeOut });
|
|
396
|
+
/**
|
|
397
|
+
* Several grants made in one write, all of them or none, judged together: each by the grants as
|
|
398
|
+
* they are, and what it leaves its caller with by the grants as they'd be with all of them. So a
|
|
399
|
+
* lock and the grant that keeps its maker in go in one request, in any order.
|
|
400
|
+
*/
|
|
401
|
+
const CreateGrantsBody = z.strictObject({
|
|
402
|
+
grants: z.array(GrantSpec).min(1).max(16),
|
|
403
|
+
lockMeOut: LockMeOut
|
|
404
|
+
});
|
|
405
|
+
/** What revoking a grant takes: nothing, or `lockMeOut`. */
|
|
406
|
+
const RevokeGrantBody = z.strictObject({ lockMeOut: LockMeOut }).optional();
|
|
407
|
+
/**
|
|
408
|
+
* Which grant decides a channel, and what it gives, for the caller or a principal it names: a
|
|
409
|
+
* `user` (by email, or by ID where the board can't look one up), a `project` in one `environment`
|
|
410
|
+
* (production by default), a `github` repository's run (with its `workflow`, `branch`, and
|
|
411
|
+
* `event`), or an `oidc` issuer's token with each `claim` as `name=value`.
|
|
412
|
+
*/
|
|
413
|
+
const ExplainQuery = z.object({
|
|
414
|
+
channel: ChannelPath,
|
|
415
|
+
user: z.string().min(1).max(320).optional(),
|
|
416
|
+
project: z.string().min(1).max(200).optional(),
|
|
417
|
+
environment: z.string().min(1).max(100).optional(),
|
|
418
|
+
github: z.string().min(3).max(200).optional(),
|
|
419
|
+
workflow: z.string().min(1).max(300).optional(),
|
|
420
|
+
branch: z.string().min(1).max(255).optional(),
|
|
421
|
+
event: z.string().min(1).max(64).optional(),
|
|
422
|
+
oidc: z.string().min(1).max(1e3).optional(),
|
|
423
|
+
claim: repeated.optional(),
|
|
424
|
+
app: z.string().min(1).max(200).optional()
|
|
425
|
+
}).refine((query) => [
|
|
426
|
+
query.user,
|
|
427
|
+
query.project,
|
|
428
|
+
query.github,
|
|
429
|
+
query.oidc,
|
|
430
|
+
query.app
|
|
431
|
+
].filter((value) => value !== void 0).length <= 1, "name one principal: user, project, github, oidc, or app").refine((query) => query.environment === void 0 || query.project !== void 0, "environment goes with project").refine((query) => query.workflow === void 0 && query.branch === void 0 && query.event === void 0 || query.github !== void 0, "workflow, branch, and event go with github").refine((query) => query.claim === void 0 || query.oidc !== void 0, "claim goes with oidc");
|
|
432
|
+
/** A grant's change: principals to add and to remove, and a `can` to replace its own. */
|
|
433
|
+
const UpdateGrantBody = z.strictObject({
|
|
434
|
+
add: z.array(PrincipalInput).min(1).max(32).optional(),
|
|
435
|
+
remove: z.array(PrincipalInput).min(1).max(32).optional(),
|
|
436
|
+
can: GrantCan.optional(),
|
|
437
|
+
lockMeOut: LockMeOut
|
|
438
|
+
}).refine((body) => body.add !== void 0 || body.remove !== void 0 || body.can !== void 0, "name principals to add or remove, or a can");
|
|
439
|
+
/**
|
|
440
|
+
* A schema version to register, for good: `name` and `version` are its ID (`sandbox@1`), and
|
|
441
|
+
* `schema` is a JSON Schema for an object, in structural keywords only, which the board checks.
|
|
442
|
+
* Strict, so a board that doesn't know a field refuses it: what it kept could never be changed.
|
|
443
|
+
*/
|
|
444
|
+
const SchemaBody = z.strictObject({
|
|
445
|
+
name: z.string().regex(/^[a-z][a-z0-9._-]*$/, "a schema name is lowercase letters, numbers, and ._-, starting with a letter"),
|
|
446
|
+
version: z.number().int().min(1),
|
|
447
|
+
description: z.string().min(1).refine(storable, "a description has a NUL character or a lone surrogate").optional(),
|
|
448
|
+
schema: z.record(z.string(), z.unknown())
|
|
449
|
+
});
|
|
450
|
+
const SyncBody = z.object({ target: z.string().min(3).max(300) });
|
|
451
|
+
const InboxQuery = z.object({
|
|
452
|
+
channel: z.string().optional(),
|
|
453
|
+
limit: z.coerce.number().int().min(1).max(500).optional()
|
|
454
|
+
});
|
|
455
|
+
/** What every registration carries: its watchers' tails, and its sync entries. */
|
|
456
|
+
const Registering = z.object({
|
|
457
|
+
watch: z.record(z.string().max(100), z.object({ tail: z.string().max(1e3) })),
|
|
458
|
+
/**
|
|
459
|
+
* Its sync entries: its sync subscribes too, to every post, change, and thread sync in the channels
|
|
460
|
+
* where it may sync, and the board lists the entries.
|
|
461
|
+
*/
|
|
462
|
+
sync: z.array(z.object({
|
|
463
|
+
key: z.string().min(1).max(64),
|
|
464
|
+
platform: z.string().min(1).max(64)
|
|
465
|
+
})).max(32).optional()
|
|
466
|
+
});
|
|
467
|
+
/**
|
|
468
|
+
* What a board on `http()` registers with the board it uses: its watchers, and where to wake it,
|
|
469
|
+
* at a URL or at a path on its own project, never both.
|
|
470
|
+
*/
|
|
471
|
+
const RegisterBody = z.union([Registering.extend({
|
|
472
|
+
/** Without one, the board pulls on its own instead of being woken. */
|
|
473
|
+
url: z.string().url().max(500).refine((url) => /^https:\/\//.test(url), "wake-ups go over https").transform((url) => url.replace(/\/+$/, "")).nullable().optional(),
|
|
474
|
+
path: z.null({ error: "register a url or a path, not both" }).optional(),
|
|
475
|
+
branch: z.null({ error: "a branch goes with a path" }).optional()
|
|
476
|
+
}), Registering.extend({
|
|
477
|
+
url: z.null({ error: "register a url or a path, not both" }).optional(),
|
|
478
|
+
/**
|
|
479
|
+
* Where to wake the caller on its own Vercel project, on a board whose discovery says
|
|
480
|
+
* `wake_by_path`. The board takes the project and environment from the caller's token and
|
|
481
|
+
* finds the deployment, so a project can only register itself, and anyone else gets 403
|
|
482
|
+
* `not_a_project`.
|
|
483
|
+
*/
|
|
484
|
+
path: z.string().max(300).regex(/^\/[^?#]*$/, "a path starts with / and has no query or fragment").transform((path) => path.replace(/(.)\/+$/, "$1")),
|
|
485
|
+
/**
|
|
486
|
+
* From a preview: the branch whose latest ready preview is woken, since a token says which
|
|
487
|
+
* project and environment called but not which deployment. Production names none.
|
|
488
|
+
*/
|
|
489
|
+
branch: z.string().min(1).max(255).nullable().optional()
|
|
490
|
+
})]);
|
|
491
|
+
/** A pull of the caller's matches: at most `limit`, waiting up to `wait` seconds for one. */
|
|
492
|
+
const MatchesQuery = z.object({
|
|
493
|
+
limit: z.coerce.number().int().min(1).max(50).optional(),
|
|
494
|
+
wait: z.coerce.number().min(0).max(25).optional()
|
|
495
|
+
});
|
|
496
|
+
/** How a pulled match went: its lease, and an error to try again, or `skip` to drop it. */
|
|
497
|
+
const AckBody = z.object({
|
|
498
|
+
lease: z.string().min(1).max(64),
|
|
499
|
+
error: z.string().min(1).max(500).optional(),
|
|
500
|
+
skip: z.boolean().optional()
|
|
501
|
+
});
|
|
502
|
+
/**
|
|
503
|
+
* What to read from a platform without syncing it: a channel (`#name` or an ID),
|
|
504
|
+
* a thread (`C0123:1727000000.123456`), or a message permalink (its thread).
|
|
505
|
+
*/
|
|
506
|
+
const SourceReadQuery = z.object({
|
|
507
|
+
target: z.string().min(1).max(4e3),
|
|
508
|
+
limit: z.coerce.number().int().min(1).max(200).optional(),
|
|
509
|
+
/** Where the last read left off: its `next`. */
|
|
510
|
+
after: z.string().min(1).max(400).optional()
|
|
511
|
+
});
|
|
512
|
+
/** A channel to add the board's app to, so it can be read on demand: `#name`, an ID, or a link into it. */
|
|
513
|
+
const SourceJoinBody = z.object({ target: z.string().min(1).max(4e3) });
|
|
514
|
+
/** A platform's link to a message or thread, such as a Slack permalink or a GitHub comment's URL. */
|
|
515
|
+
const LinkedQuery = z.object({ url: z.string().url().max(4e3) });
|
|
516
|
+
/** A sync entry's key: `slack`, `github`. */
|
|
517
|
+
const SyncKey = z.string().min(1).max(64);
|
|
518
|
+
/** A platform message an import changes, by its platform thread and ID; without a message, the root of the board thread the platform thread belongs to. */
|
|
519
|
+
const ImportOrigin = z.object({
|
|
520
|
+
thread: ExternalTarget,
|
|
521
|
+
message: z.string().min(1).max(300).optional()
|
|
522
|
+
});
|
|
523
|
+
/** A link as an import carries it: a board message it names is a ref. */
|
|
524
|
+
const ImportLink = Link.extend({ message: Ref.nullable() });
|
|
525
|
+
/**
|
|
526
|
+
* A platform user, as sync names them to the board. Their avatar shows wherever they do, so one
|
|
527
|
+
* that isn't an https URL is dropped, never refused: the write lands without it.
|
|
528
|
+
*/
|
|
529
|
+
const PlatformUser = ExternalAuthor.extend({ avatar: z.string().nullable().transform(platformAvatar) });
|
|
530
|
+
/**
|
|
531
|
+
* Who wrote it on the platform, and the sync key it came through: null for a write a sync callback
|
|
532
|
+
* makes as that user, which syncs out to every platform like one written on the board.
|
|
533
|
+
*/
|
|
534
|
+
const Imported = {
|
|
535
|
+
author: PlatformUser,
|
|
536
|
+
from: SyncKey.nullable()
|
|
537
|
+
};
|
|
538
|
+
/**
|
|
539
|
+
* The platform's users a text mentions as `@[<their id>]`. One the board hasn't met becomes known
|
|
540
|
+
* to it, as one who wrote does: named, and seen where the message is. One the text doesn't mention
|
|
541
|
+
* is left out.
|
|
542
|
+
*/
|
|
543
|
+
const Mentioned = z.array(PlatformUser).max(50).optional();
|
|
544
|
+
/**
|
|
545
|
+
* Where the platform event an import comes from is: its sync entry, platform channel, and thread.
|
|
546
|
+
* The middleware is told, and answers there when it drops the write.
|
|
547
|
+
*/
|
|
548
|
+
const ImportSource = z.object({
|
|
549
|
+
key: SyncKey,
|
|
550
|
+
channel: ExternalTarget,
|
|
551
|
+
thread: ExternalTarget
|
|
552
|
+
});
|
|
553
|
+
/** The message a change is about: by its platform IDs (`origin`), or by its board ID (`message`), one or the other. */
|
|
554
|
+
const Changed = {
|
|
555
|
+
origin: ImportOrigin.optional(),
|
|
556
|
+
message: Ref.optional()
|
|
557
|
+
};
|
|
558
|
+
/**
|
|
559
|
+
* A platform's write, as its platform author, with where it came from: only a caller with `sync` in
|
|
560
|
+
* the channel it lands in may make one. Posts are idempotent by `id`, derived from the platform
|
|
561
|
+
* event; every other op finds the message it changes from its `origin`.
|
|
562
|
+
*/
|
|
563
|
+
const SyncImportBody = z.discriminatedUnion("op", [
|
|
564
|
+
z.object({
|
|
565
|
+
op: z.literal("post"),
|
|
566
|
+
...Imported,
|
|
567
|
+
/** Where it is on the platform. Null for a write a sync callback makes itself. */
|
|
568
|
+
origin: Origin.nullable().default(null),
|
|
569
|
+
source: ImportSource.optional(),
|
|
570
|
+
channel: ChannelPath.optional(),
|
|
571
|
+
re: Ref.optional(),
|
|
572
|
+
text: z.string().max(MESSAGE_TEXT_MAX).default(""),
|
|
573
|
+
labels: NewLabels.optional(),
|
|
574
|
+
noSync: z.boolean().optional(),
|
|
575
|
+
attachments: z.array(AttachmentId).max(10).optional(),
|
|
576
|
+
links: z.array(ImportLink).max(10).optional(),
|
|
577
|
+
mentioned: Mentioned,
|
|
578
|
+
id: z.string().min(1).max(200).refine(storable, "id has a NUL character or a lone surrogate").optional(),
|
|
579
|
+
/** A reply's precondition, as a post's: a sync callback's reply after the newest message it read. */
|
|
580
|
+
after: Ref.optional()
|
|
581
|
+
}),
|
|
582
|
+
z.object({
|
|
583
|
+
op: z.literal("edit"),
|
|
584
|
+
...Imported,
|
|
585
|
+
...Changed,
|
|
586
|
+
text: MessageText,
|
|
587
|
+
source: ImportSource.optional(),
|
|
588
|
+
revision: Revision.optional(),
|
|
589
|
+
mentioned: Mentioned
|
|
590
|
+
}),
|
|
591
|
+
z.object({
|
|
592
|
+
op: z.literal("delete"),
|
|
593
|
+
...Imported,
|
|
594
|
+
...Changed,
|
|
595
|
+
revision: Revision.optional()
|
|
596
|
+
}),
|
|
597
|
+
z.object({
|
|
598
|
+
op: z.literal("react"),
|
|
599
|
+
...Imported,
|
|
600
|
+
...Changed,
|
|
601
|
+
emoji: Emoji,
|
|
602
|
+
remove: z.boolean().optional()
|
|
603
|
+
}),
|
|
604
|
+
z.object({
|
|
605
|
+
op: z.literal("labels"),
|
|
606
|
+
...Imported,
|
|
607
|
+
...Changed,
|
|
608
|
+
set: NewLabels.optional(),
|
|
609
|
+
unset: z.array(LabelKey).optional(),
|
|
610
|
+
/** As a label write's: what the labels must be now, a value or null for none. */
|
|
611
|
+
if: z.record(LabelKey, LabelValue.nullable()).optional()
|
|
612
|
+
}),
|
|
613
|
+
z.object({
|
|
614
|
+
op: z.literal("links"),
|
|
615
|
+
...Imported,
|
|
616
|
+
...Changed,
|
|
617
|
+
links: z.array(ImportLink).max(10)
|
|
618
|
+
})
|
|
619
|
+
]);
|
|
620
|
+
const PlatformFiles = z.array(z.string().min(1).max(300)).max(10).nullable();
|
|
621
|
+
/** What sync did on a platform, about a board message: it landed, its files did, it gave up, or its thread syncs with a target. */
|
|
622
|
+
const SyncReceiptBody = z.discriminatedUnion("kind", [
|
|
623
|
+
z.object({
|
|
624
|
+
kind: z.literal("message"),
|
|
625
|
+
message: Ref,
|
|
626
|
+
target: ExternalTarget,
|
|
627
|
+
external: z.object({
|
|
628
|
+
thread: ExternalTarget,
|
|
629
|
+
message: z.string().min(1).max(300)
|
|
630
|
+
}),
|
|
631
|
+
/** Its files, when they went with it: the platform's IDs, or null when it refused them. */
|
|
632
|
+
files: PlatformFiles.optional()
|
|
633
|
+
}),
|
|
634
|
+
z.object({
|
|
635
|
+
kind: z.literal("files"),
|
|
636
|
+
message: Ref,
|
|
637
|
+
target: ExternalTarget,
|
|
638
|
+
files: PlatformFiles
|
|
639
|
+
}),
|
|
640
|
+
z.object({
|
|
641
|
+
kind: z.literal("failed"),
|
|
642
|
+
message: Ref,
|
|
643
|
+
target: ExternalTarget,
|
|
644
|
+
error: z.string().min(1).max(500)
|
|
645
|
+
}),
|
|
646
|
+
z.object({
|
|
647
|
+
kind: z.literal("thread"),
|
|
648
|
+
/** Any message in the thread. */
|
|
649
|
+
message: Ref,
|
|
650
|
+
target: ExternalTarget,
|
|
651
|
+
/** A channel to start a thread in, or a thread to join. */
|
|
652
|
+
targetKind: z.enum(["channel", "thread"]),
|
|
653
|
+
cause: z.enum(["config", "request"])
|
|
654
|
+
})
|
|
655
|
+
]);
|
|
656
|
+
/** What sync looks up by platform ID: a thread or a message. */
|
|
657
|
+
const SyncFindQuery = z.object({
|
|
658
|
+
/** The post the caller's sync imported with this client ID. */
|
|
659
|
+
post: z.string().min(1).max(200).optional(),
|
|
660
|
+
thread: ExternalTarget.optional(),
|
|
661
|
+
/** With `thread`: the platform message in it. */
|
|
662
|
+
message: z.string().min(1).max(300).optional()
|
|
663
|
+
});
|
|
664
|
+
const SyncCopiesQuery = z.object({ message: repeated.pipe(z.array(Ref).min(1).max(100)) });
|
|
665
|
+
const InboxPushBody = z.object({ events: z.array(z.object({
|
|
666
|
+
id: z.string().min(1).max(300),
|
|
667
|
+
event: ExternalEvent,
|
|
668
|
+
raw: z.unknown()
|
|
669
|
+
})).min(1).max(50) });
|
|
670
|
+
const InboxNextQuery = z.object({ limit: z.coerce.number().int().min(1).max(50).optional() });
|
|
671
|
+
const InboxAckBody = z.object({
|
|
672
|
+
lease: z.string().min(1).max(64),
|
|
673
|
+
error: z.string().min(1).max(500).optional()
|
|
674
|
+
});
|
|
675
|
+
/**
|
|
676
|
+
* A direct upload, before its bytes: what the file will be called, its type, its exact size, and
|
|
677
|
+
* the SHA-256 of its bytes, in base64, for a store that signs its URL for them (Vercel's).
|
|
678
|
+
*/
|
|
679
|
+
const UploadStartBody = z.object({
|
|
680
|
+
name: z.string().max(1024).nullable().optional(),
|
|
681
|
+
type: z.string().max(1024).nullable().optional(),
|
|
682
|
+
size: z.number().int().positive().max(DIRECT_UPLOAD_BYTES_MAX, `direct uploads are at most ${DIRECT_UPLOAD_BYTES_MAX / 1024 / 1024} MB`),
|
|
683
|
+
sha256: z.string().regex(/^[A-Za-z0-9+/]{43}=$/, "sha256 is the base64 SHA-256 of the bytes").optional()
|
|
684
|
+
});
|
|
685
|
+
const EditBody = z.object({
|
|
686
|
+
text: SentText,
|
|
687
|
+
revision: Revision.optional()
|
|
688
|
+
});
|
|
689
|
+
const DeleteBody = z.object({ revision: Revision.optional() }).optional();
|
|
690
|
+
/** `status`: what Slack shows in place of "Typing…" (other platforms ignore it). */
|
|
691
|
+
const TypingBody = z.object({ status: z.string().trim().min(1).max(50).optional() }).optional();
|
|
692
|
+
const ReactBody = z.object({
|
|
693
|
+
emoji: Emoji,
|
|
694
|
+
remove: z.boolean().optional()
|
|
695
|
+
});
|
|
696
|
+
/**
|
|
697
|
+
* A channel to make before anything posts to it, and nothing else: a key it doesn't know is a 400,
|
|
698
|
+
* so a client that asks for more than a channel (one that expects `private: true` to close it, say)
|
|
699
|
+
* is told, never handed an open one.
|
|
700
|
+
*/
|
|
701
|
+
const CreateChannelBody = z.strictObject({ path: ChannelPath });
|
|
702
|
+
const LabelBody = z.object({
|
|
703
|
+
set: NewLabels.optional(),
|
|
704
|
+
unset: z.array(LabelKey).optional(),
|
|
705
|
+
/**
|
|
706
|
+
* What the message's labels must be now: a key's value, or null for one that mustn't be set.
|
|
707
|
+
* If one isn't, nothing changes, and the board answers 409 `label_conflict`.
|
|
708
|
+
*/
|
|
709
|
+
if: z.record(LabelKey, LabelValue.nullable()).refine((expected) => Object.keys(expected).length <= 16, `at most 16 labels`).optional()
|
|
710
|
+
}).refine((body) => body.set !== void 0 || body.unset !== void 0, { message: "nothing to change: pass `set` or `unset`" });
|
|
711
|
+
/** The route table: method, path, and the schemas that validate input. */
|
|
712
|
+
const routes = {
|
|
713
|
+
/** Where the board is, its project, and its sync entries. No token. */
|
|
714
|
+
discovery: {
|
|
715
|
+
method: "GET",
|
|
716
|
+
path: "/"
|
|
717
|
+
},
|
|
718
|
+
me: {
|
|
719
|
+
method: "GET",
|
|
720
|
+
path: "/me"
|
|
721
|
+
},
|
|
722
|
+
channels: {
|
|
723
|
+
method: "GET",
|
|
724
|
+
path: "/channels",
|
|
725
|
+
query: ChannelsQuery
|
|
726
|
+
},
|
|
727
|
+
/** Makes a channel before anything posts to it: needs `read` and `post` there. One that's there already is left as it is. */
|
|
728
|
+
createChannel: {
|
|
729
|
+
method: "POST",
|
|
730
|
+
path: "/channels",
|
|
731
|
+
body: CreateChannelBody
|
|
732
|
+
},
|
|
733
|
+
actors: {
|
|
734
|
+
method: "GET",
|
|
735
|
+
path: "/actors",
|
|
736
|
+
query: ActorsQuery
|
|
737
|
+
},
|
|
738
|
+
/** One channel's messages across its threads: the newest, or those after one of its cursors. */
|
|
739
|
+
channelMessages: {
|
|
740
|
+
method: "GET",
|
|
741
|
+
path: "/messages",
|
|
742
|
+
query: ChannelMessagesQuery
|
|
743
|
+
},
|
|
744
|
+
message: {
|
|
745
|
+
method: "GET",
|
|
746
|
+
path: "/messages/:id",
|
|
747
|
+
query: ThreadQuery
|
|
748
|
+
},
|
|
749
|
+
/** Threads with a matching message, one summary each, by latest activity. `before` takes a page's `older`. */
|
|
750
|
+
threads: {
|
|
751
|
+
method: "GET",
|
|
752
|
+
path: "/threads",
|
|
753
|
+
query: ThreadsQuery
|
|
754
|
+
},
|
|
755
|
+
post: {
|
|
756
|
+
method: "POST",
|
|
757
|
+
path: "/messages",
|
|
758
|
+
body: PostBody
|
|
759
|
+
},
|
|
760
|
+
edit: {
|
|
761
|
+
method: "POST",
|
|
762
|
+
path: "/messages/:id/edit",
|
|
763
|
+
body: EditBody
|
|
764
|
+
},
|
|
765
|
+
delete: {
|
|
766
|
+
method: "POST",
|
|
767
|
+
path: "/messages/:id/delete",
|
|
768
|
+
body: DeleteBody
|
|
769
|
+
},
|
|
770
|
+
react: {
|
|
771
|
+
method: "POST",
|
|
772
|
+
path: "/messages/:id/reactions",
|
|
773
|
+
body: ReactBody
|
|
774
|
+
},
|
|
775
|
+
label: {
|
|
776
|
+
method: "POST",
|
|
777
|
+
path: "/messages/:id/labels",
|
|
778
|
+
body: LabelBody
|
|
779
|
+
},
|
|
780
|
+
/** Attaches the thread to a sync target, recording a `thread` receipt: needs `sync` where the thread is. */
|
|
781
|
+
sync: {
|
|
782
|
+
method: "POST",
|
|
783
|
+
path: "/messages/:id/sync",
|
|
784
|
+
body: SyncBody
|
|
785
|
+
},
|
|
786
|
+
/** Where the message's thread is synced, and its copy on each target: needs `read` where it is. */
|
|
787
|
+
synced: {
|
|
788
|
+
method: "GET",
|
|
789
|
+
path: "/messages/:id/sync"
|
|
790
|
+
},
|
|
791
|
+
/** The board message a platform link names, from what the board synced: 404 `message_not_found` for one it didn't, or that the caller can't read. */
|
|
792
|
+
linked: {
|
|
793
|
+
method: "GET",
|
|
794
|
+
path: "/links",
|
|
795
|
+
query: LinkedQuery
|
|
796
|
+
},
|
|
797
|
+
/** Ephemeral: shows typing in the thread's synced platform threads, and writes nothing to the log. Needs `post` where the thread is. */
|
|
798
|
+
typing: {
|
|
799
|
+
method: "POST",
|
|
800
|
+
path: "/messages/:id/typing",
|
|
801
|
+
body: TypingBody
|
|
802
|
+
},
|
|
803
|
+
/** Posts a message later, as the caller: needs a scheduler. */
|
|
804
|
+
schedule: {
|
|
805
|
+
method: "POST",
|
|
806
|
+
path: "/schedules",
|
|
807
|
+
body: ScheduleBody
|
|
808
|
+
},
|
|
809
|
+
schedules: {
|
|
810
|
+
method: "GET",
|
|
811
|
+
path: "/schedules",
|
|
812
|
+
query: SchedulesQuery
|
|
813
|
+
},
|
|
814
|
+
/** Its creator, or manage in its channel, may cancel a schedule. Canceling one that's done is a no-op. */
|
|
815
|
+
cancelSchedule: {
|
|
816
|
+
method: "POST",
|
|
817
|
+
path: "/schedules/:id/cancel"
|
|
818
|
+
},
|
|
819
|
+
/** Tasks, oldest first: open ones unless `state` says otherwise. */
|
|
820
|
+
tasks: {
|
|
821
|
+
method: "GET",
|
|
822
|
+
path: "/tasks",
|
|
823
|
+
query: TasksQuery
|
|
824
|
+
},
|
|
825
|
+
/** Starts a thread as a task (`post` there, and `label` for its labels), or makes the thread `re` roots one (`label` there). */
|
|
826
|
+
createTask: {
|
|
827
|
+
method: "POST",
|
|
828
|
+
path: "/tasks",
|
|
829
|
+
body: CreateTaskBody
|
|
830
|
+
},
|
|
831
|
+
/** Starts several threads in one channel as tasks, in one write: all of them or none, and nothing shows before all have landed. */
|
|
832
|
+
createTasks: {
|
|
833
|
+
method: "POST",
|
|
834
|
+
path: "/tasks/batch",
|
|
835
|
+
body: CreateTasksBody
|
|
836
|
+
},
|
|
837
|
+
/** The caller's own batch made under the `id` it gave, its tasks as they are now: for telling a retry before making anything. */
|
|
838
|
+
createdTasks: {
|
|
839
|
+
method: "GET",
|
|
840
|
+
path: "/tasks/batch/:id",
|
|
841
|
+
query: CreatedTasksQuery
|
|
842
|
+
},
|
|
843
|
+
/** Claims the oldest ready task in a channel or subtree: `label` where it is. Answers `{ task: null }` when none is. */
|
|
844
|
+
claimReadyTask: {
|
|
845
|
+
method: "POST",
|
|
846
|
+
path: "/tasks/claim",
|
|
847
|
+
body: ClaimReadyTaskBody
|
|
848
|
+
},
|
|
849
|
+
/** Claims a task: `label` in its channel. Its answer's `claim` goes on what the claim does next. */
|
|
850
|
+
claimTask: {
|
|
851
|
+
method: "POST",
|
|
852
|
+
path: "/tasks/:id/claim",
|
|
853
|
+
body: ClaimTaskBody
|
|
854
|
+
},
|
|
855
|
+
/** What a claim does, with its `epoch` and `token`: 409 `claim_lost` once it's over. */
|
|
856
|
+
renewTask: {
|
|
857
|
+
method: "POST",
|
|
858
|
+
path: "/tasks/:id/renew",
|
|
859
|
+
body: RenewTaskBody
|
|
860
|
+
},
|
|
861
|
+
releaseTask: {
|
|
862
|
+
method: "POST",
|
|
863
|
+
path: "/tasks/:id/release",
|
|
864
|
+
body: ReleaseTaskBody
|
|
865
|
+
},
|
|
866
|
+
completeTask: {
|
|
867
|
+
method: "POST",
|
|
868
|
+
path: "/tasks/:id/complete",
|
|
869
|
+
body: CompleteTaskBody
|
|
870
|
+
},
|
|
871
|
+
failTask: {
|
|
872
|
+
method: "POST",
|
|
873
|
+
path: "/tasks/:id/fail",
|
|
874
|
+
body: FailTaskBody
|
|
875
|
+
},
|
|
876
|
+
/** Holds a task for a person: its claim while it's claimed, its owner or `manage` in its channel otherwise. */
|
|
877
|
+
blockTask: {
|
|
878
|
+
method: "POST",
|
|
879
|
+
path: "/tasks/:id/block",
|
|
880
|
+
body: BlockTaskBody
|
|
881
|
+
},
|
|
882
|
+
/** What a task's owner does, or `manage` in its channel. */
|
|
883
|
+
unblockTask: {
|
|
884
|
+
method: "POST",
|
|
885
|
+
path: "/tasks/:id/unblock"
|
|
886
|
+
},
|
|
887
|
+
reopenTask: {
|
|
888
|
+
method: "POST",
|
|
889
|
+
path: "/tasks/:id/reopen"
|
|
890
|
+
},
|
|
891
|
+
assignTask: {
|
|
892
|
+
method: "POST",
|
|
893
|
+
path: "/tasks/:id/assign",
|
|
894
|
+
body: AssignTaskBody
|
|
895
|
+
},
|
|
896
|
+
/** Replaces what a task waits for, while no one holds it and it isn't finished. */
|
|
897
|
+
setTaskNeeds: {
|
|
898
|
+
method: "POST",
|
|
899
|
+
path: "/tasks/:id/needs",
|
|
900
|
+
body: TaskNeedsBody
|
|
901
|
+
},
|
|
902
|
+
cancelTask: {
|
|
903
|
+
method: "POST",
|
|
904
|
+
path: "/tasks/:id/cancel",
|
|
905
|
+
body: CancelTaskBody
|
|
906
|
+
},
|
|
907
|
+
/** Listing grants needs manage at `*`; changing one, manage at each pattern its `can` names. Team owners always may. */
|
|
908
|
+
grants: {
|
|
909
|
+
method: "GET",
|
|
910
|
+
path: "/grants"
|
|
911
|
+
},
|
|
912
|
+
createGrant: {
|
|
913
|
+
method: "POST",
|
|
914
|
+
path: "/grants",
|
|
915
|
+
body: GrantBody
|
|
916
|
+
},
|
|
917
|
+
/** Several grants in one write, all or none, judged together. */
|
|
918
|
+
createGrants: {
|
|
919
|
+
method: "POST",
|
|
920
|
+
path: "/grants/batch",
|
|
921
|
+
body: CreateGrantsBody
|
|
922
|
+
},
|
|
923
|
+
updateGrant: {
|
|
924
|
+
method: "PATCH",
|
|
925
|
+
path: "/grants/:id",
|
|
926
|
+
body: UpdateGrantBody
|
|
927
|
+
},
|
|
928
|
+
/** Which grant decides a channel, for the caller or, with `manage` at `*`, a principal it names. */
|
|
929
|
+
explain: {
|
|
930
|
+
method: "GET",
|
|
931
|
+
path: "/grants/explain",
|
|
932
|
+
query: ExplainQuery
|
|
933
|
+
},
|
|
934
|
+
revokeGrant: {
|
|
935
|
+
method: "POST",
|
|
936
|
+
path: "/grants/:id/revoke",
|
|
937
|
+
body: RevokeGrantBody
|
|
938
|
+
},
|
|
939
|
+
/** Every schema: the built-ins, then the board's own. Anyone the board lets in reads them. */
|
|
940
|
+
schemas: {
|
|
941
|
+
method: "GET",
|
|
942
|
+
path: "/schemas"
|
|
943
|
+
},
|
|
944
|
+
/** One schema version, by `name@version`. */
|
|
945
|
+
schema: {
|
|
946
|
+
method: "GET",
|
|
947
|
+
path: "/schemas/:id"
|
|
948
|
+
},
|
|
949
|
+
/** Registers a version, which never changes after: needs manage at `*`. The same definition again is a no-op. */
|
|
950
|
+
createSchema: {
|
|
951
|
+
method: "POST",
|
|
952
|
+
path: "/schemas",
|
|
953
|
+
body: SchemaBody
|
|
954
|
+
},
|
|
955
|
+
/** The caller's own post with the client ID it gave: what `POST /messages` answered, for telling a retry before posting. */
|
|
956
|
+
posted: {
|
|
957
|
+
method: "GET",
|
|
958
|
+
path: "/posts/:id"
|
|
959
|
+
},
|
|
960
|
+
/** Raw platform events in a sync key's inbox, newest first: the caller's own, and the board's with `sync` everywhere. */
|
|
961
|
+
inbox: {
|
|
962
|
+
method: "GET",
|
|
963
|
+
path: "/inbox/:key",
|
|
964
|
+
query: InboxQuery
|
|
965
|
+
},
|
|
966
|
+
/** Pushes platform events into the caller's inbox for a sync key, deduplicated by their platform IDs. */
|
|
967
|
+
inboxPush: {
|
|
968
|
+
method: "POST",
|
|
969
|
+
path: "/inbox/:key",
|
|
970
|
+
body: InboxPushBody
|
|
971
|
+
},
|
|
972
|
+
/** The caller's next inbox events, leased to it, one platform thread at a time and in order. */
|
|
973
|
+
inboxNext: {
|
|
974
|
+
method: "GET",
|
|
975
|
+
path: "/inbox/:key/next",
|
|
976
|
+
query: InboxNextQuery
|
|
977
|
+
},
|
|
978
|
+
/** How one went: done, or an error to try it again. */
|
|
979
|
+
inboxAck: {
|
|
980
|
+
method: "POST",
|
|
981
|
+
path: "/inbox/:key/:id/ack",
|
|
982
|
+
body: InboxAckBody
|
|
983
|
+
},
|
|
984
|
+
/** A platform's write, as its platform author: `sync` where it lands. */
|
|
985
|
+
syncImport: {
|
|
986
|
+
method: "POST",
|
|
987
|
+
path: "/sync/imports",
|
|
988
|
+
body: SyncImportBody
|
|
989
|
+
},
|
|
990
|
+
/** What sync did on a platform: a delivery, its files, a failure, or a thread's new target. */
|
|
991
|
+
syncReceipt: {
|
|
992
|
+
method: "POST",
|
|
993
|
+
path: "/sync/receipts",
|
|
994
|
+
body: SyncReceiptBody
|
|
995
|
+
},
|
|
996
|
+
/** A board thread's sync, by its key. */
|
|
997
|
+
syncThread: {
|
|
998
|
+
method: "GET",
|
|
999
|
+
path: "/sync/threads/:thread"
|
|
1000
|
+
},
|
|
1001
|
+
/** What platform IDs are on the board. */
|
|
1002
|
+
syncFind: {
|
|
1003
|
+
method: "GET",
|
|
1004
|
+
path: "/sync/find",
|
|
1005
|
+
query: SyncFindQuery
|
|
1006
|
+
},
|
|
1007
|
+
/** Messages' copies on platforms. */
|
|
1008
|
+
syncCopies: {
|
|
1009
|
+
method: "GET",
|
|
1010
|
+
path: "/sync/copies",
|
|
1011
|
+
query: SyncCopiesQuery
|
|
1012
|
+
},
|
|
1013
|
+
/** Reads a platform channel or thread on demand, where that sync runs, without syncing or storing it. Full access only. */
|
|
1014
|
+
readSource: {
|
|
1015
|
+
method: "GET",
|
|
1016
|
+
path: "/sources/:key/read",
|
|
1017
|
+
query: SourceReadQuery
|
|
1018
|
+
},
|
|
1019
|
+
/** Adds the app to a public channel so it can be read, where that sync runs and opted in with `join`. Full access only. */
|
|
1020
|
+
joinSource: {
|
|
1021
|
+
method: "POST",
|
|
1022
|
+
path: "/sources/:key/join",
|
|
1023
|
+
body: SourceJoinBody
|
|
1024
|
+
},
|
|
1025
|
+
webhook: {
|
|
1026
|
+
method: "POST",
|
|
1027
|
+
path: "/webhooks/:key"
|
|
1028
|
+
},
|
|
1029
|
+
events: {
|
|
1030
|
+
method: "GET",
|
|
1031
|
+
path: "/events",
|
|
1032
|
+
query: EventsQuery
|
|
1033
|
+
},
|
|
1034
|
+
avatar: {
|
|
1035
|
+
method: "GET",
|
|
1036
|
+
path: "/avatars/:name"
|
|
1037
|
+
},
|
|
1038
|
+
/** The file is the body; `content-type` is its type and `x-board-filename` its name, URI-encoded. */
|
|
1039
|
+
upload: {
|
|
1040
|
+
method: "POST",
|
|
1041
|
+
path: "/attachments"
|
|
1042
|
+
},
|
|
1043
|
+
attachment: {
|
|
1044
|
+
method: "GET",
|
|
1045
|
+
path: "/attachments/:id"
|
|
1046
|
+
},
|
|
1047
|
+
/** A file too big for one request: the board answers where to `PUT` it in its blob store. Needs a store with `uploadUrl`. */
|
|
1048
|
+
uploadStart: {
|
|
1049
|
+
method: "POST",
|
|
1050
|
+
path: "/attachments/uploads",
|
|
1051
|
+
body: UploadStartBody
|
|
1052
|
+
},
|
|
1053
|
+
/** After the `PUT`: the board checks what landed in the store and records the attachment. */
|
|
1054
|
+
uploadFinish: {
|
|
1055
|
+
method: "POST",
|
|
1056
|
+
path: "/attachments/uploads/:id"
|
|
1057
|
+
},
|
|
1058
|
+
openapi: {
|
|
1059
|
+
method: "GET",
|
|
1060
|
+
path: "/openapi.json"
|
|
1061
|
+
},
|
|
1062
|
+
/** Watchers registered by boards on `http()`: every one to `manage` everywhere, the caller's own to anyone else. */
|
|
1063
|
+
watchers: {
|
|
1064
|
+
method: "GET",
|
|
1065
|
+
path: "/watchers"
|
|
1066
|
+
},
|
|
1067
|
+
/** Registers the caller's watchers, replacing its last registration. On a board on `http()`, registers its own, with no body or token. */
|
|
1068
|
+
register: {
|
|
1069
|
+
method: "POST",
|
|
1070
|
+
path: "/watchers",
|
|
1071
|
+
body: RegisterBody
|
|
1072
|
+
},
|
|
1073
|
+
/** The caller's matches it can run now, each leased to it until it acks. */
|
|
1074
|
+
matches: {
|
|
1075
|
+
method: "GET",
|
|
1076
|
+
path: "/watchers/matches",
|
|
1077
|
+
query: MatchesQuery
|
|
1078
|
+
},
|
|
1079
|
+
ack: {
|
|
1080
|
+
method: "POST",
|
|
1081
|
+
path: "/watchers/matches/:id/ack",
|
|
1082
|
+
body: AckBody
|
|
1083
|
+
},
|
|
1084
|
+
/** On a board on `http()`: something matched. No body, no token: the board pulls what. */
|
|
1085
|
+
wake: {
|
|
1086
|
+
method: "POST",
|
|
1087
|
+
path: "/wake"
|
|
1088
|
+
}
|
|
1089
|
+
};
|
|
1090
|
+
//#endregion
|
|
1091
|
+
//#region src/client/sse.ts
|
|
1092
|
+
/** Parses a text/event-stream body into messages. */
|
|
1093
|
+
async function* parseSse(body, signal) {
|
|
1094
|
+
const reader = body.getReader();
|
|
1095
|
+
const decoder = new TextDecoder();
|
|
1096
|
+
let buffer = "";
|
|
1097
|
+
const onAbort = () => void reader.cancel().catch(() => {});
|
|
1098
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1099
|
+
try {
|
|
1100
|
+
while (true) {
|
|
1101
|
+
const { value, done } = await reader.read();
|
|
1102
|
+
if (done) return;
|
|
1103
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1104
|
+
let boundary;
|
|
1105
|
+
while ((boundary = buffer.search(/\r?\n\r?\n/)) !== -1) {
|
|
1106
|
+
const block = buffer.slice(0, boundary);
|
|
1107
|
+
buffer = buffer.slice(boundary).replace(/^\r?\n\r?\n/, "");
|
|
1108
|
+
const message = {};
|
|
1109
|
+
const data = [];
|
|
1110
|
+
for (const line of block.split(/\r?\n/)) {
|
|
1111
|
+
if (line === "" || line.startsWith(":")) continue;
|
|
1112
|
+
const colon = line.indexOf(":");
|
|
1113
|
+
const field = colon === -1 ? line : line.slice(0, colon);
|
|
1114
|
+
const value = colon === -1 ? "" : line.slice(colon + 1).replace(/^ /, "");
|
|
1115
|
+
if (field === "data") data.push(value);
|
|
1116
|
+
else if (field === "id") message.id = value;
|
|
1117
|
+
else if (field === "event") message.event = value;
|
|
1118
|
+
}
|
|
1119
|
+
if (data.length > 0) message.data = data.join("\n");
|
|
1120
|
+
if (message.id !== void 0 || message.data !== void 0) yield message;
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
} finally {
|
|
1124
|
+
signal?.removeEventListener("abort", onAbort);
|
|
1125
|
+
reader.releaseLock();
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
//#endregion
|
|
1129
|
+
//#region src/client/client.ts
|
|
1130
|
+
/**
|
|
1131
|
+
* What went wrong: `message` says what and why, `code` is what to branch on, `hint`, when there's
|
|
1132
|
+
* one, what fixes it, and `origin` who said so. Trust a `board` one's message and hint as you would
|
|
1133
|
+
* the board.
|
|
1134
|
+
*/
|
|
1135
|
+
var BoardError = class extends Error {
|
|
1136
|
+
code;
|
|
1137
|
+
status;
|
|
1138
|
+
hint;
|
|
1139
|
+
origin;
|
|
1140
|
+
/** For a 429, how many seconds the board asked to wait before sending again (its `Retry-After`), when it said. */
|
|
1141
|
+
retryAfter;
|
|
1142
|
+
constructor(code, message, status, options = {}) {
|
|
1143
|
+
super(message);
|
|
1144
|
+
this.name = "BoardError";
|
|
1145
|
+
this.code = code;
|
|
1146
|
+
this.status = status;
|
|
1147
|
+
this.hint = options.hint;
|
|
1148
|
+
this.origin = options.origin ?? "handle";
|
|
1149
|
+
this.retryAfter = options.retryAfter;
|
|
1150
|
+
}
|
|
1151
|
+
};
|
|
1152
|
+
/**
|
|
1153
|
+
* The responses, made in this process, that carry an error the handle raised: a handle's request
|
|
1154
|
+
* to a board in this process keeps its errors the handle's on the way back. Nothing a board sends
|
|
1155
|
+
* can be one.
|
|
1156
|
+
*/
|
|
1157
|
+
const handleErrors = /* @__PURE__ */ new WeakSet();
|
|
1158
|
+
const resolve = async (value) => typeof value === "function" ? await value() : value;
|
|
1159
|
+
const scheduleBody = (input) => {
|
|
1160
|
+
const { at, until, ...rest } = messageBody(input);
|
|
1161
|
+
return {
|
|
1162
|
+
...rest,
|
|
1163
|
+
...at !== void 0 ? { at: at instanceof Date ? at.toISOString() : at } : {},
|
|
1164
|
+
...until !== void 0 ? { until: until instanceof Date ? until.toISOString() : until } : {}
|
|
1165
|
+
};
|
|
1166
|
+
};
|
|
1167
|
+
const messageBody = (input) => {
|
|
1168
|
+
if (typeof input === "string") return { text: input };
|
|
1169
|
+
const { attachments, ...rest } = input;
|
|
1170
|
+
return attachments?.length ? {
|
|
1171
|
+
...rest,
|
|
1172
|
+
attachments: attachments.map((a) => typeof a === "string" ? a : a.id)
|
|
1173
|
+
} : rest;
|
|
1174
|
+
};
|
|
1175
|
+
/** Tells `onState` each change, once: `live` when the board answers, `retrying` when a request fails. It never guesses. */
|
|
1176
|
+
function eventsState(onState) {
|
|
1177
|
+
let current = null;
|
|
1178
|
+
const tell = (next) => {
|
|
1179
|
+
if (next === current) return;
|
|
1180
|
+
current = next;
|
|
1181
|
+
onState?.(next);
|
|
1182
|
+
};
|
|
1183
|
+
return {
|
|
1184
|
+
answered: () => tell("live"),
|
|
1185
|
+
failed: () => tell("retrying")
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
/** How long one request for a ready task is held, in seconds, as one for the next event is. */
|
|
1189
|
+
const CLAIM_WAIT_S = 25;
|
|
1190
|
+
/** How soon a claim that waits asks again, when the board answered without holding the request. */
|
|
1191
|
+
const CLAIM_AGAIN_MS = 1e3;
|
|
1192
|
+
const UNITS = {
|
|
1193
|
+
ms: 1,
|
|
1194
|
+
s: 1e3,
|
|
1195
|
+
m: 6e4,
|
|
1196
|
+
h: 36e5,
|
|
1197
|
+
d: 864e5
|
|
1198
|
+
};
|
|
1199
|
+
function duration(value) {
|
|
1200
|
+
if (typeof value === "number") return value;
|
|
1201
|
+
const match = value.trim().match(/^(\d+(?:\.\d+)?)(ms|s|m|h|d)?$/);
|
|
1202
|
+
if (!match) throw new BoardError("invalid_duration", `${value} isn't a duration like 30s, 10m, or 1h.`, 0);
|
|
1203
|
+
return Number(match[1]) * UNITS[match[2] ?? "ms"];
|
|
1204
|
+
}
|
|
1205
|
+
/** A ref as a path segment: opaque, so it's encoded as it is. */
|
|
1206
|
+
const path = (id) => encodeURIComponent(id);
|
|
1207
|
+
function queryString(query) {
|
|
1208
|
+
const params = new URLSearchParams();
|
|
1209
|
+
for (const [key, value] of Object.entries(query)) {
|
|
1210
|
+
if (value === void 0 || value === null) continue;
|
|
1211
|
+
if (key === "label") {
|
|
1212
|
+
const labels = Array.isArray(value) ? value : typeof value === "object" ? Object.entries(value).map(([k, v]) => `${k}=${String(v)}`) : [value];
|
|
1213
|
+
for (const label of labels) params.append("label", String(label));
|
|
1214
|
+
} else params.set(key, String(value));
|
|
1215
|
+
}
|
|
1216
|
+
const text = params.toString();
|
|
1217
|
+
return text ? `?${text}` : "";
|
|
1218
|
+
}
|
|
1219
|
+
/** A `can` as the wire takes it: `'none'` at a pattern is `[]`. */
|
|
1220
|
+
function wireCan(can) {
|
|
1221
|
+
if (can === null || typeof can !== "object" || Array.isArray(can)) return can;
|
|
1222
|
+
return Object.fromEntries(Object.entries(can).map(([pattern, grant]) => [pattern, grant === "none" ? [] : grant]));
|
|
1223
|
+
}
|
|
1224
|
+
/** A principal to grant as the wire takes it: a project's environments, and every project's, are production when left out. */
|
|
1225
|
+
const withEnvironments = (principal) => (principal.kind === "project" || principal.kind === "all-projects") && principal.environments === void 0 ? {
|
|
1226
|
+
...principal,
|
|
1227
|
+
environments: ["production"]
|
|
1228
|
+
} : principal;
|
|
1229
|
+
/** A grant as the wire takes it. */
|
|
1230
|
+
const wireGrant = (input) => ({
|
|
1231
|
+
principals: input.principals.map(withEnvironments),
|
|
1232
|
+
can: wireCan(input.can)
|
|
1233
|
+
});
|
|
1234
|
+
/** What a grant write's body says to go ahead with a lockout: nothing, unless it was asked to. */
|
|
1235
|
+
const goAhead = ({ lockMeOut }) => lockMeOut ? { lockMeOut: true } : {};
|
|
1236
|
+
/**
|
|
1237
|
+
* The principal a private channel lets its creator in as, from what `/me` says it is: a person's
|
|
1238
|
+
* user, or a deployment's project in the environment it runs in. Anyone else is refused before
|
|
1239
|
+
* anything is written.
|
|
1240
|
+
*/
|
|
1241
|
+
function creatorOf(me, path) {
|
|
1242
|
+
const [source, kind, id, environment] = me.principal.split(":");
|
|
1243
|
+
if ((source === "vercel" || source === "local") && kind === "user" && id) return {
|
|
1244
|
+
kind: "user",
|
|
1245
|
+
userId: me.principal.split(":").slice(2).join(":")
|
|
1246
|
+
};
|
|
1247
|
+
if (source === "vercel" && kind === "project" && id && environment) return {
|
|
1248
|
+
kind: "project",
|
|
1249
|
+
project: id,
|
|
1250
|
+
environments: [environment]
|
|
1251
|
+
};
|
|
1252
|
+
throw new BoardError("no_own_principal", `A private channel lets its creator in, as a person or a project's deployment, and ${me.principal} is neither, so ${path} wasn't made.`, 400, { hint: "Make it as a person or a deployment, or make it and its grants yourself." });
|
|
1253
|
+
}
|
|
1254
|
+
function createBoardClient(options) {
|
|
1255
|
+
const base = options.url.replace(/\/+$/, "");
|
|
1256
|
+
const doFetch = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
|
|
1257
|
+
const storeFetch = options.storeFetch ?? ((input, init) => globalThis.fetch(input, init));
|
|
1258
|
+
async function headers(auth, as) {
|
|
1259
|
+
const out = { ...options.headers ? await resolve(options.headers) : {} };
|
|
1260
|
+
if (!auth) return out;
|
|
1261
|
+
if (options.token !== void 0) out.authorization = `Bearer ${await resolve(options.token)}`;
|
|
1262
|
+
const profile = as !== void 0 ? as : options.profile === void 0 ? null : await resolve(options.profile);
|
|
1263
|
+
if (profile !== null) out["x-board-profile"] = formatProfileHeader(typeof profile === "string" ? { name: profile } : profile);
|
|
1264
|
+
return out;
|
|
1265
|
+
}
|
|
1266
|
+
async function raw(method, path, init = {}) {
|
|
1267
|
+
const response = await doFetch(`${base}${path}`, {
|
|
1268
|
+
method,
|
|
1269
|
+
headers: {
|
|
1270
|
+
...await headers(init.auth ?? true),
|
|
1271
|
+
...init.body === void 0 ? {} : { "content-type": "application/json" },
|
|
1272
|
+
...init.headers
|
|
1273
|
+
},
|
|
1274
|
+
...init.body === void 0 ? {} : { body: JSON.stringify(init.body) },
|
|
1275
|
+
...init.signal ? { signal: init.signal } : {}
|
|
1276
|
+
});
|
|
1277
|
+
if (!response.ok) throw await toError(response);
|
|
1278
|
+
return response;
|
|
1279
|
+
}
|
|
1280
|
+
async function call(method, path, init) {
|
|
1281
|
+
return await (await raw(method, path, init)).json();
|
|
1282
|
+
}
|
|
1283
|
+
const filtersQuery = (filters = {}) => filters;
|
|
1284
|
+
async function poll(query = {}, signal) {
|
|
1285
|
+
return call("GET", `/events${queryString(query)}`, signal ? { signal } : {});
|
|
1286
|
+
}
|
|
1287
|
+
/** One write to task `id`, which answers its root. */
|
|
1288
|
+
const taskWrite = async (id, verb, body = {}) => (await call("POST", `/tasks/${path(id)}/${verb}`, { body })).task;
|
|
1289
|
+
/** What a claim on task `id` does, with the `epoch` and `token` the claim answered. */
|
|
1290
|
+
const held = (id, proof) => {
|
|
1291
|
+
const { epoch, token } = proof;
|
|
1292
|
+
return {
|
|
1293
|
+
async renew(lease) {
|
|
1294
|
+
const { task, until } = await call("POST", `/tasks/${path(id)}/renew`, { body: {
|
|
1295
|
+
epoch,
|
|
1296
|
+
token,
|
|
1297
|
+
...lease !== void 0 ? { lease } : {}
|
|
1298
|
+
} });
|
|
1299
|
+
return claimed(task, {
|
|
1300
|
+
epoch,
|
|
1301
|
+
token,
|
|
1302
|
+
until
|
|
1303
|
+
});
|
|
1304
|
+
},
|
|
1305
|
+
release: () => taskWrite(id, "release", {
|
|
1306
|
+
epoch,
|
|
1307
|
+
token
|
|
1308
|
+
}),
|
|
1309
|
+
complete: (result) => taskWrite(id, "complete", {
|
|
1310
|
+
epoch,
|
|
1311
|
+
token,
|
|
1312
|
+
...result !== void 0 ? { result } : {}
|
|
1313
|
+
}),
|
|
1314
|
+
fail: (reason) => taskWrite(id, "fail", {
|
|
1315
|
+
epoch,
|
|
1316
|
+
token,
|
|
1317
|
+
reason
|
|
1318
|
+
}),
|
|
1319
|
+
block: (reason, opts = {}) => taskWrite(id, "block", {
|
|
1320
|
+
epoch,
|
|
1321
|
+
token,
|
|
1322
|
+
reason,
|
|
1323
|
+
...opts
|
|
1324
|
+
})
|
|
1325
|
+
};
|
|
1326
|
+
};
|
|
1327
|
+
/** A claim as a handle answers it: the task, the claim, and what the claim does. */
|
|
1328
|
+
const claimed = (task, claim) => ({
|
|
1329
|
+
task,
|
|
1330
|
+
...claim,
|
|
1331
|
+
...held(task.id, claim)
|
|
1332
|
+
});
|
|
1333
|
+
async function createTask(target, input = {}) {
|
|
1334
|
+
const where = typeof target === "string" ? { channel: target } : { re: target.re };
|
|
1335
|
+
const made = typeof input === "string" ? { text: input } : "attachments" in input && input.attachments?.length ? {
|
|
1336
|
+
...input,
|
|
1337
|
+
attachments: input.attachments.map((file) => typeof file === "string" ? file : file.id)
|
|
1338
|
+
} : input;
|
|
1339
|
+
const { task, claim, mentions } = await call("POST", "/tasks", { body: {
|
|
1340
|
+
...where,
|
|
1341
|
+
...made
|
|
1342
|
+
} });
|
|
1343
|
+
return {
|
|
1344
|
+
task,
|
|
1345
|
+
...claim ? { claim: claimed(task, claim) } : {},
|
|
1346
|
+
...mentions ? { mentions } : {}
|
|
1347
|
+
};
|
|
1348
|
+
}
|
|
1349
|
+
/**
|
|
1350
|
+
* Starts a thread in `channel` for each of `tasks`, as tasks, in one write: all of them or none,
|
|
1351
|
+
* and none shows before all have landed. It answers their roots in the order given. A task waits
|
|
1352
|
+
* for the ones made with it by their `key`. With `id`, a retry answers the tasks the first call
|
|
1353
|
+
* made. A board makes 100 at most, and throws a 400 `batch_too_large` for more, or for more than it
|
|
1354
|
+
* holds in one write: `createTasksInChunks` makes a longer plan, in several.
|
|
1355
|
+
*/
|
|
1356
|
+
async function createTasks(channel, tasks, opts = {}) {
|
|
1357
|
+
return (await call("POST", "/tasks/batch", { body: {
|
|
1358
|
+
channel,
|
|
1359
|
+
tasks: tasks.map((task) => task.attachments?.length ? {
|
|
1360
|
+
...task,
|
|
1361
|
+
attachments: task.attachments.map((file) => typeof file === "string" ? file : file.id)
|
|
1362
|
+
} : task),
|
|
1363
|
+
...opts.id !== void 0 ? { id: opts.id } : {}
|
|
1364
|
+
} })).tasks;
|
|
1365
|
+
}
|
|
1366
|
+
async function claimTask(target, opts = {}) {
|
|
1367
|
+
const signal = opts.signal ? { signal: opts.signal } : {};
|
|
1368
|
+
if (typeof target === "string") {
|
|
1369
|
+
const body = {
|
|
1370
|
+
...opts.lease !== void 0 ? { lease: opts.lease } : {},
|
|
1371
|
+
...opts.token !== void 0 ? { token: opts.token } : {}
|
|
1372
|
+
};
|
|
1373
|
+
const { task, claim } = await call("POST", `/tasks/${path(target)}/claim`, {
|
|
1374
|
+
body,
|
|
1375
|
+
...signal
|
|
1376
|
+
});
|
|
1377
|
+
return claimed(task, claim);
|
|
1378
|
+
}
|
|
1379
|
+
const { wait, ...ready } = target;
|
|
1380
|
+
const deadline = Date.now() + (wait ?? 0) * 1e3;
|
|
1381
|
+
for (;;) {
|
|
1382
|
+
const asked = Date.now();
|
|
1383
|
+
const hold = Math.max(0, Math.min(CLAIM_WAIT_S, (deadline - asked) / 1e3));
|
|
1384
|
+
const answer = await call("POST", "/tasks/claim", {
|
|
1385
|
+
body: {
|
|
1386
|
+
...ready,
|
|
1387
|
+
...hold > 0 ? { wait: hold } : {}
|
|
1388
|
+
},
|
|
1389
|
+
...signal
|
|
1390
|
+
});
|
|
1391
|
+
if (answer.task) return claimed(answer.task, answer.claim);
|
|
1392
|
+
if (Date.now() >= deadline || opts.signal?.aborted) return null;
|
|
1393
|
+
const pause = Math.min(CLAIM_AGAIN_MS - (Date.now() - asked), deadline - Date.now());
|
|
1394
|
+
if (pause > 0) await new Promise((resolve) => setTimeout(resolve, pause));
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
return {
|
|
1398
|
+
url: base,
|
|
1399
|
+
/**
|
|
1400
|
+
* The board's HTTP API with this client's credentials, and the response as
|
|
1401
|
+
* the board sent it, errors included: for proxies like `vb inspect`.
|
|
1402
|
+
*/
|
|
1403
|
+
request: async (path, init = {}) => doFetch(`${base}${path}`, {
|
|
1404
|
+
method: init.method ?? "GET",
|
|
1405
|
+
headers: {
|
|
1406
|
+
...init.headers,
|
|
1407
|
+
...await headers(true, init.as)
|
|
1408
|
+
},
|
|
1409
|
+
...init.body ? { body: init.body } : {},
|
|
1410
|
+
...init.signal ? { signal: init.signal } : {}
|
|
1411
|
+
}),
|
|
1412
|
+
discovery: () => call("GET", "/", { auth: false }),
|
|
1413
|
+
me: () => call("GET", "/me"),
|
|
1414
|
+
/**
|
|
1415
|
+
* A page of the board's channels: `prefix` keeps the ones whose path starts with it (`dev/`), `limit` is how many
|
|
1416
|
+
* (100 unless it says, 500 at most), and `after` takes a page's `next`, which is null on the last.
|
|
1417
|
+
*/
|
|
1418
|
+
channels: async (query = {}) => {
|
|
1419
|
+
const page = await call("GET", `/channels${queryString(query)}`);
|
|
1420
|
+
return {
|
|
1421
|
+
...page,
|
|
1422
|
+
next: page.next ?? null
|
|
1423
|
+
};
|
|
1424
|
+
},
|
|
1425
|
+
/**
|
|
1426
|
+
* Makes a channel before anything posts to it, as a first post would. Needs `read` and `post`
|
|
1427
|
+
* there. `created` is false when it was there already; it's left as it is.
|
|
1428
|
+
*
|
|
1429
|
+
* `private` makes it closed to everyone but you, from the protocol's own methods: a grant
|
|
1430
|
+
* letting you in at `<path>/`, then one closing `<path>/` to everyone else, then the channel,
|
|
1431
|
+
* answering the two grants as `grants`. A failure part way leaves the channel open with a grant
|
|
1432
|
+
* too many, or closed and not made yet, and never shuts you out. Before it writes anything, it
|
|
1433
|
+
* refuses a caller that's neither a person nor a project's deployment (400 `no_own_principal`),
|
|
1434
|
+
* one that can't list grants, a path a grant other than `*`'s reaches already (409
|
|
1435
|
+
* `channel_granted`: the channel, `<path>/`, or a pattern under it, whose grants would still let
|
|
1436
|
+
* in whoever they name), and a path where it or a channel under it is there already (409
|
|
1437
|
+
* `channel_exists`), since closing `security/` closes `security/incidents` too. A board that
|
|
1438
|
+
* keeps no grants fails on the listing, with nothing made. Once its own grant is made it lists
|
|
1439
|
+
* them again, and takes its own back when another reaches the path, so of two callers making
|
|
1440
|
+
* the same channel at once, at most one keeps a way in; and once the lock is made, it lists them
|
|
1441
|
+
* a last time, and takes both back when a grant made in between still reaches the path, or its
|
|
1442
|
+
* own two aren't as it made them (someone added themselves to its grant, or opened the lock).
|
|
1443
|
+
*/
|
|
1444
|
+
createChannel: async (path, options = {}) => {
|
|
1445
|
+
if (!options.private) return call("POST", "/channels", { body: { path } });
|
|
1446
|
+
const creator = creatorOf(await call("GET", "/me"), path);
|
|
1447
|
+
const pattern = `${path}/`;
|
|
1448
|
+
const reaching = async () => (await call("GET", "/grants")).grants.filter((grant) => Object.keys(grant.can).some((at) => at === path || at.startsWith(pattern)));
|
|
1449
|
+
const granted = (grants, mine = []) => {
|
|
1450
|
+
const changed = grants.filter((grant) => mine.some((ours) => ours.id === grant.id)).map((grant) => grant.id);
|
|
1451
|
+
if (changed.length === grants.length) return new BoardError("channel_granted", `${changed.join(" and ")} changed while ${path} was being made, so a private ${path} wouldn't be yours alone: nothing was made.`, 409);
|
|
1452
|
+
const named = grants.filter((grant) => !changed.includes(grant.id)).map((grant) => grant.id);
|
|
1453
|
+
return new BoardError("channel_granted", `${named.length === 1 ? named[0] : `${named.slice(0, -1).join(", ")} and ${named.at(-1)}`} already ${named.length === 1 ? "grants" : "grant"} in ${path}, so a private ${path} wouldn't be yours alone: nothing was made.`, 409, { hint: named.length === 1 ? `Revoke it first: vb grants revoke ${named[0]}` : "Revoke them first, each with vb grants revoke <id>." });
|
|
1454
|
+
};
|
|
1455
|
+
const before = await reaching();
|
|
1456
|
+
if (before.length > 0) throw granted(before);
|
|
1457
|
+
const first = async (prefix) => (await call("GET", `/channels${queryString({
|
|
1458
|
+
prefix,
|
|
1459
|
+
limit: 1
|
|
1460
|
+
})}`)).channels.find((channel) => channel.path === path || channel.path.startsWith(pattern));
|
|
1461
|
+
const there = await first(path) ?? await first(pattern);
|
|
1462
|
+
if (there) throw new BoardError("channel_exists", there.path === path ? `${path} is a channel already, so it stays as it is.` : `${there.path} is a channel already, and a private ${path} would close it too, so nothing was made.`, 409, { hint: `Close it with a grant: everyone at ${pattern} with 'none'.` });
|
|
1463
|
+
const own = await call("POST", "/grants", { body: {
|
|
1464
|
+
principals: [creator],
|
|
1465
|
+
can: { [pattern]: "all" }
|
|
1466
|
+
} });
|
|
1467
|
+
const takeBack = (id) => {
|
|
1468
|
+
const revoke = (body) => call("POST", `/grants/${encodeURIComponent(id)}/revoke`, { body });
|
|
1469
|
+
return revoke({}).catch((error) => {
|
|
1470
|
+
if (error instanceof BoardError && error.code === "grant_lockout") return revoke({ lockMeOut: true });
|
|
1471
|
+
throw error;
|
|
1472
|
+
});
|
|
1473
|
+
};
|
|
1474
|
+
const others = (grants, mine) => grants.filter((grant) => {
|
|
1475
|
+
const made = mine.find((ours) => ours.id === grant.id);
|
|
1476
|
+
return made === void 0 || JSON.stringify([grant.principals, grant.can]) !== JSON.stringify([made.principals, made.can]);
|
|
1477
|
+
});
|
|
1478
|
+
const raced = others(await reaching(), [own.grant]);
|
|
1479
|
+
if (raced.length > 0) {
|
|
1480
|
+
await takeBack(own.grant.id);
|
|
1481
|
+
throw granted(raced, [own.grant]);
|
|
1482
|
+
}
|
|
1483
|
+
const closed = await call("POST", "/grants", { body: {
|
|
1484
|
+
principals: [{ kind: "everyone" }],
|
|
1485
|
+
can: { [pattern]: [] }
|
|
1486
|
+
} });
|
|
1487
|
+
const late = others(await reaching(), [own.grant, closed.grant]);
|
|
1488
|
+
if (late.length > 0) {
|
|
1489
|
+
for (const id of [closed.grant.id, own.grant.id]) await takeBack(id);
|
|
1490
|
+
throw granted(late, [own.grant, closed.grant]);
|
|
1491
|
+
}
|
|
1492
|
+
return {
|
|
1493
|
+
...await call("POST", "/channels", { body: { path } }),
|
|
1494
|
+
grants: [own.grant, closed.grant]
|
|
1495
|
+
};
|
|
1496
|
+
},
|
|
1497
|
+
/**
|
|
1498
|
+
* The board's actors, or with `id`, only the ones it names, in a request a hundred: someone the board doesn't
|
|
1499
|
+
* have, or you may not see, is left out. A board from before `id` answers everyone.
|
|
1500
|
+
*/
|
|
1501
|
+
actors: async (query = {}) => {
|
|
1502
|
+
if (query.id === void 0) return (await call("GET", "/actors")).actors;
|
|
1503
|
+
const ids = [...new Set(query.id)];
|
|
1504
|
+
const batches = Array.from({ length: Math.ceil(ids.length / 100) }, (_, n) => ids.slice(n * 100, (n + 1) * 100));
|
|
1505
|
+
const named = (batch) => call("GET", `/actors?${new URLSearchParams(batch.map((id) => ["id", id]))}`).then((page) => page.actors);
|
|
1506
|
+
const first = batches.length > 0 ? await named(batches[0]) : [];
|
|
1507
|
+
const asked = new Set(batches[0]);
|
|
1508
|
+
const found = first.every((actor) => asked.has(actor.id)) ? [...first, ...(await Promise.all(batches.slice(1).map(named))).flat()] : first;
|
|
1509
|
+
const wanted = new Set(ids);
|
|
1510
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1511
|
+
return found.filter((actor) => wanted.has(actor.id) && !seen.has(actor.id) && Boolean(seen.add(actor.id)));
|
|
1512
|
+
},
|
|
1513
|
+
/**
|
|
1514
|
+
* One channel, by its whole name: a board lists messages a channel at a time, never across channels. Its
|
|
1515
|
+
* `messages()` are the channel's across its threads, oldest first: the newest `limit` (50 without one), or with
|
|
1516
|
+
* `after`, a cursor a read of this channel answered, the ones posted since.
|
|
1517
|
+
*/
|
|
1518
|
+
channel: (name) => ({ messages: (query = {}) => call("GET", `/messages${queryString({
|
|
1519
|
+
channel: name,
|
|
1520
|
+
...query
|
|
1521
|
+
})}`) }),
|
|
1522
|
+
/**
|
|
1523
|
+
* A thread: its root and its replies. `after`, one of its messages, answers the replies after it, and `limit` that
|
|
1524
|
+
* many of them: with `after` the first after it, to read on from, and without, the newest.
|
|
1525
|
+
*/
|
|
1526
|
+
thread: (id, range = {}) => call("GET", `/messages/${path(id)}${queryString(range)}`),
|
|
1527
|
+
/** Your own post with the client ID you gave it, as posting it answered: 404 `post_not_found` when there's none. */
|
|
1528
|
+
posted: async (id) => (await call("GET", `/posts/${path(id)}`)).message,
|
|
1529
|
+
/** Threads with a matching message, one summary each (root, reply count, latest reply), by latest activity. `limit` counts threads; `before` takes a page's `older`. */
|
|
1530
|
+
threads: (query = {}) => call("GET", `/threads${queryString(query)}`),
|
|
1531
|
+
/**
|
|
1532
|
+
* Starts a thread. An `@handle` in the text that names exactly one actor you can see is stored as
|
|
1533
|
+
* `@[actor-id]`, a mention of them; one that names none or several stays as typed, a mention of
|
|
1534
|
+
* no one. `mentions` on what comes back says what became of each, when the text typed any.
|
|
1535
|
+
*/
|
|
1536
|
+
async post(channel, input) {
|
|
1537
|
+
const { message, mentions } = await call("POST", "/messages", { body: {
|
|
1538
|
+
channel,
|
|
1539
|
+
...messageBody(input)
|
|
1540
|
+
} });
|
|
1541
|
+
return mentions ? {
|
|
1542
|
+
...message,
|
|
1543
|
+
mentions
|
|
1544
|
+
} : message;
|
|
1545
|
+
},
|
|
1546
|
+
/** Replies in a thread. Its `@handle`s are settled as `post`'s are: of several who share one, it's the one who wrote in the thread. */
|
|
1547
|
+
async reply(id, input) {
|
|
1548
|
+
const { message, mentions } = await call("POST", "/messages", { body: {
|
|
1549
|
+
re: id,
|
|
1550
|
+
...messageBody(input)
|
|
1551
|
+
} });
|
|
1552
|
+
return mentions ? {
|
|
1553
|
+
...message,
|
|
1554
|
+
mentions
|
|
1555
|
+
} : message;
|
|
1556
|
+
},
|
|
1557
|
+
/**
|
|
1558
|
+
* Posts later, as you: a new thread when `target` is a channel, a reply when it's
|
|
1559
|
+
* `{ re }`, any message's ID in the thread. Each message it posts carries
|
|
1560
|
+
* `schedule: { id, n }`. Needs a board with a scheduler.
|
|
1561
|
+
*/
|
|
1562
|
+
async schedule(target, input) {
|
|
1563
|
+
const { schedule, mentions } = await call("POST", "/schedules", { body: {
|
|
1564
|
+
...typeof target === "string" ? { channel: target } : { re: target.re },
|
|
1565
|
+
...scheduleBody(input)
|
|
1566
|
+
} });
|
|
1567
|
+
return mentions ? {
|
|
1568
|
+
...schedule,
|
|
1569
|
+
mentions
|
|
1570
|
+
} : schedule;
|
|
1571
|
+
},
|
|
1572
|
+
/** Pending schedules (`all` for done and canceled too), oldest first. `re` is any message in a thread. */
|
|
1573
|
+
schedules: async (query = {}) => (await call("GET", `/schedules${queryString(query)}`)).schedules,
|
|
1574
|
+
/** Stops a schedule: yours, or any with manage in its channel. `canceled` is false when it was already done or canceled. */
|
|
1575
|
+
cancelSchedule: (id) => call("POST", `/schedules/${path(id)}/cancel`, { body: {} }),
|
|
1576
|
+
/**
|
|
1577
|
+
* Tasks: threads someone's agents are to do, with a state the board keeps. A task's ID is its
|
|
1578
|
+
* root's, and its root's `task` says its state, whose it is, what it waits for, and who holds
|
|
1579
|
+
* it. A board that keeps no tasks fails each of these with 501 `no_tasks`.
|
|
1580
|
+
*/
|
|
1581
|
+
tasks: {
|
|
1582
|
+
/**
|
|
1583
|
+
* Tasks, oldest first, each as its thread's summary: open ones (`waiting`, `ready`, `claimed`,
|
|
1584
|
+
* `blocked`) unless `state` says `all` or names one. `owner` and `for` take a handle or an ID,
|
|
1585
|
+
* `needs` a task's ID, for the tasks that wait for it. `cursor`, as `after`, reads the next page.
|
|
1586
|
+
* `latest` answers the `limit` last active instead, the latest active last: the board reads every
|
|
1587
|
+
* task to find them, and builds only those.
|
|
1588
|
+
*/
|
|
1589
|
+
list: (query = {}) => call("GET", `/tasks${queryString(query)}`),
|
|
1590
|
+
create: createTask,
|
|
1591
|
+
createMany: createTasks,
|
|
1592
|
+
claim: claimTask,
|
|
1593
|
+
/** What a claim you hold does, from the `epoch` and `token` it answered: for a process that kept them, as `vb` does between commands. */
|
|
1594
|
+
held,
|
|
1595
|
+
/** Holds a task no one has claimed for a person: yours, or any with manage in its channel. A claimed one's hold is its claim's (`claim.block`). */
|
|
1596
|
+
block: (id, reason, opts = {}) => taskWrite(id, "block", {
|
|
1597
|
+
reason,
|
|
1598
|
+
...opts
|
|
1599
|
+
}),
|
|
1600
|
+
/** Lifts a hold: the task is ready again, or waiting while a task it needs hasn't completed. */
|
|
1601
|
+
unblock: (id) => taskWrite(id, "unblock"),
|
|
1602
|
+
/** Makes a failed task ready again, in place: what waits for it still does. */
|
|
1603
|
+
reopen: (id) => taskWrite(id, "reopen"),
|
|
1604
|
+
/** Assigns a task to one of its owner's actors (`<owner>+<profile>`), the only one that claims it then, or with null to anyone who may. */
|
|
1605
|
+
assign: (id, actor) => taskWrite(id, "assign", { for: actor }),
|
|
1606
|
+
/**
|
|
1607
|
+
* Replaces what a task waits for, by their IDs: it waits for those now, and is ready once they've all completed, or
|
|
1608
|
+
* at once with none. For its owner, while no one holds it and it isn't finished. One that would make it wait for
|
|
1609
|
+
* itself, through however many tasks, throws a 400 `invalid_needs`.
|
|
1610
|
+
*/
|
|
1611
|
+
needs: (id, needs) => taskWrite(id, "needs", { needs }),
|
|
1612
|
+
/** Cancels a task that hasn't completed, ending any claim. What waits for it keeps waiting. */
|
|
1613
|
+
cancel: (id, reason) => taskWrite(id, "cancel", { reason })
|
|
1614
|
+
},
|
|
1615
|
+
/**
|
|
1616
|
+
* A new grant: `principals` may do what `can` says, a map from channel pattern to permissions
|
|
1617
|
+
* (`[]` for none). The same principals again are another grant. At each channel, the most
|
|
1618
|
+
* specific pattern among the grants that name a caller decides, then the closest principal,
|
|
1619
|
+
* and grants tied there add up. Needs manage at each pattern `can` names.
|
|
1620
|
+
*
|
|
1621
|
+
* One you couldn't take back, since it takes your own manage at a pattern it names (closing
|
|
1622
|
+
* `security/` to `everyone` closes it to you), is 409 `grant_lockout`: keep yourself in with
|
|
1623
|
+
* `createGrants`, or pass `lockMeOut` to make it anyway. Every grant write takes it.
|
|
1624
|
+
*/
|
|
1625
|
+
createGrant: (input, options = {}) => call("POST", "/grants", { body: {
|
|
1626
|
+
...wireGrant(input),
|
|
1627
|
+
...goAhead(options)
|
|
1628
|
+
} }),
|
|
1629
|
+
/**
|
|
1630
|
+
* Several grants in one write, all of them or none, judged together: each needs what
|
|
1631
|
+
* `createGrant` does on the grants as they are, and what they leave you with is judged with
|
|
1632
|
+
* all of them made. So a lock and the grant that keeps you in go together, in any order.
|
|
1633
|
+
*/
|
|
1634
|
+
createGrants: (inputs, options = {}) => call("POST", "/grants/batch", { body: {
|
|
1635
|
+
grants: inputs.map(wireGrant),
|
|
1636
|
+
...goAhead(options)
|
|
1637
|
+
} }),
|
|
1638
|
+
/** Adds and removes principals (by who they are, environments aside), or replaces its `can`. Needs what `checkGrantChange` says, for the grant as it was and as it'd be. */
|
|
1639
|
+
updateGrant: (id, { add, remove, can }, options = {}) => call("PATCH", `/grants/${path(id)}`, { body: {
|
|
1640
|
+
...add ? { add: add.map(withEnvironments) } : {},
|
|
1641
|
+
...remove ? { remove } : {},
|
|
1642
|
+
...can ? { can: wireCan(can) } : {},
|
|
1643
|
+
...goAhead(options)
|
|
1644
|
+
} }),
|
|
1645
|
+
/**
|
|
1646
|
+
* Which grants decide `channel`, and what they give: for you, or with manage at `*`, for the
|
|
1647
|
+
* principal `principal` names (`{ user }`, `{ project, environment }`, `{ github, workflow,
|
|
1648
|
+
* branch, event }`, or `{ oidc, claims }`).
|
|
1649
|
+
*/
|
|
1650
|
+
explain: (channel, principal = {}) => {
|
|
1651
|
+
const { claims, ...rest } = principal;
|
|
1652
|
+
const params = new URLSearchParams({ channel });
|
|
1653
|
+
for (const [name, value] of Object.entries(rest)) if (value !== void 0) params.set(name, value);
|
|
1654
|
+
for (const [name, value] of Object.entries(claims ?? {})) params.append("claim", `${name}=${value}`);
|
|
1655
|
+
return call("GET", `/grants/explain?${params}`);
|
|
1656
|
+
},
|
|
1657
|
+
/** The board's grants. Needs manage at `*`, or a team owner. */
|
|
1658
|
+
grants: async () => (await call("GET", "/grants")).grants,
|
|
1659
|
+
/** `revoked` is false when it was already gone. Its principals' next requests are refused. */
|
|
1660
|
+
revokeGrant: (id, options = {}) => call("POST", `/grants/${path(id)}/revoke`, { body: goAhead(options) }),
|
|
1661
|
+
/** Every schema: the built-ins, then the board's own, by name and version. */
|
|
1662
|
+
schemas: async () => (await call("GET", "/schemas")).schemas,
|
|
1663
|
+
/** One schema version, by its ID: `sandbox@1`. */
|
|
1664
|
+
schema: async (id) => (await call("GET", `/schemas/${path(id)}`)).schema,
|
|
1665
|
+
/**
|
|
1666
|
+
* Registers a schema version, for good: `schema` is a JSON Schema for an object, in structural
|
|
1667
|
+
* keywords only. Needs manage at `*`. `created` is false for one that's there with the same
|
|
1668
|
+
* definition; another definition is refused, since a version never changes.
|
|
1669
|
+
*/
|
|
1670
|
+
createSchema: (input) => call("POST", "/schemas", { body: input }),
|
|
1671
|
+
/**
|
|
1672
|
+
* Stores a file on the board, for a post or reply's `attachments`. Up to 4 MB goes in one
|
|
1673
|
+
* request; bigger, up to 100 MB, goes straight to the board's blob store on a URL the board
|
|
1674
|
+
* signs, where its store can (`no_direct_uploads` where it can't).
|
|
1675
|
+
*/
|
|
1676
|
+
async upload(input) {
|
|
1677
|
+
const type = input.type || (input.data instanceof Blob ? input.data.type : "") || typeOf(input.name);
|
|
1678
|
+
const size = input.data instanceof Blob ? input.data.size : input.data.byteLength;
|
|
1679
|
+
if (size > 4194304) {
|
|
1680
|
+
if (size > 104857600) throw new BoardError("too_large", `Attachments are at most ${DIRECT_UPLOAD_BYTES_MAX / 1024 / 1024} MB.`, 413);
|
|
1681
|
+
const bytes = input.data instanceof Blob ? await input.data.arrayBuffer() : input.data;
|
|
1682
|
+
const sha256 = btoa(String.fromCharCode(...new Uint8Array(await crypto.subtle.digest("SHA-256", bytes))));
|
|
1683
|
+
const ticket = await call("POST", "/attachments/uploads", { body: {
|
|
1684
|
+
name: input.name,
|
|
1685
|
+
type,
|
|
1686
|
+
size,
|
|
1687
|
+
sha256
|
|
1688
|
+
} }).catch((error) => {
|
|
1689
|
+
if (error instanceof BoardError && error.status === 404 && error.code === "not_found") throw new BoardError("too_large", `This board takes files of at most ${ATTACHMENT_BYTES_MAX / 1024 / 1024} MB.`, 413);
|
|
1690
|
+
throw error;
|
|
1691
|
+
});
|
|
1692
|
+
const put = await storeFetch(ticket.url, {
|
|
1693
|
+
method: "PUT",
|
|
1694
|
+
headers: ticket.headers,
|
|
1695
|
+
body: input.data
|
|
1696
|
+
});
|
|
1697
|
+
if (!put.ok) throw new BoardError("upload_failed", `The blob store refused the file: ${put.status} ${(await put.text().catch(() => "")).slice(0, 300) || put.statusText}`, put.status);
|
|
1698
|
+
return (await call("POST", `/attachments/uploads/${encodeURIComponent(ticket.id)}`, { body: {} })).attachment;
|
|
1699
|
+
}
|
|
1700
|
+
const response = await doFetch(`${base}/attachments`, {
|
|
1701
|
+
method: "POST",
|
|
1702
|
+
headers: {
|
|
1703
|
+
...await headers(true),
|
|
1704
|
+
"content-type": type,
|
|
1705
|
+
"x-board-filename": encodeURIComponent(input.name)
|
|
1706
|
+
},
|
|
1707
|
+
body: input.data
|
|
1708
|
+
});
|
|
1709
|
+
if (!response.ok) throw await toError(response);
|
|
1710
|
+
return (await response.json()).attachment;
|
|
1711
|
+
},
|
|
1712
|
+
/** An attachment's bytes, as the board serves them: `content-type` is the file's type. */
|
|
1713
|
+
download: (id, opts = {}) => raw("GET", `/attachments/${encodeURIComponent(id)}`, opts.signal ? { signal: opts.signal } : {}),
|
|
1714
|
+
/**
|
|
1715
|
+
* `revision` is the message's as you read it: if it's been edited or deleted since, nothing
|
|
1716
|
+
* changes, and it throws a 409 `revision_conflict`.
|
|
1717
|
+
*/
|
|
1718
|
+
edit: (id, text, opts = {}) => call("POST", `/messages/${path(id)}/edit`, { body: {
|
|
1719
|
+
text,
|
|
1720
|
+
...opts
|
|
1721
|
+
} }),
|
|
1722
|
+
delete: (id, opts = {}) => call("POST", `/messages/${path(id)}/delete`, { body: { ...opts } }),
|
|
1723
|
+
/** `emoji` is a short name, `:name:`, or the character itself. */
|
|
1724
|
+
react: (id, emoji, opts = {}) => call("POST", `/messages/${path(id)}/reactions`, { body: {
|
|
1725
|
+
emoji: normalizeEmoji(emoji),
|
|
1726
|
+
...opts
|
|
1727
|
+
} }),
|
|
1728
|
+
/**
|
|
1729
|
+
* `if` is what the labels must be now, a value or null for one that mustn't be set: if one
|
|
1730
|
+
* isn't, nothing changes, and it throws a 409 `label_conflict`.
|
|
1731
|
+
*/
|
|
1732
|
+
label: (id, change) => call("POST", `/messages/${path(id)}/labels`, { body: change }),
|
|
1733
|
+
/**
|
|
1734
|
+
* Shows typing in the platform threads the message's thread is synced to.
|
|
1735
|
+
* Ephemeral: nothing goes in the log. Answers the external threads that took it.
|
|
1736
|
+
* `status` replaces "Typing…" on Slack ("running tests…").
|
|
1737
|
+
*/
|
|
1738
|
+
typing: async (id, status) => (await call("POST", `/messages/${path(id)}/typing`, { body: status ? { status } : {} })).targets,
|
|
1739
|
+
/**
|
|
1740
|
+
* With a target, attaches the message's thread to it, backfilling it: attaching twice is a no-op.
|
|
1741
|
+
* Without one, where the thread is synced, and this message's copy on each target, with its link.
|
|
1742
|
+
*/
|
|
1743
|
+
sync: ((id, target) => target === void 0 ? call("GET", `/messages/${path(id)}/sync`) : call("POST", `/messages/${path(id)}/sync`, { body: { target } })),
|
|
1744
|
+
/**
|
|
1745
|
+
* The thread of the board message a platform link names (a Slack permalink, a GitHub comment's
|
|
1746
|
+
* URL), from what the board synced: the platform is never asked. 404 for one it didn't sync.
|
|
1747
|
+
*/
|
|
1748
|
+
linked: (url) => call("GET", `/links${queryString({ url })}`),
|
|
1749
|
+
/** Raw platform events for a sync entry, newest first. */
|
|
1750
|
+
inbox: async (key, opts = {}) => (await call("GET", `/inbox/${key}${queryString(opts)}`)).events,
|
|
1751
|
+
/** Registers the caller's watchers, replacing its last registration: what a board on `http()` does. */
|
|
1752
|
+
register: (input) => call("POST", "/watchers", { body: input }),
|
|
1753
|
+
/** Registered watchers: every one to `manage` everywhere, the caller's own to anyone else. */
|
|
1754
|
+
watchers: async () => (await call("GET", "/watchers")).watchers,
|
|
1755
|
+
/** The caller's matches it can run now, each leased to it until it acks. `wait` holds the request up to that many seconds for one. */
|
|
1756
|
+
matches: async (opts = {}) => {
|
|
1757
|
+
const { signal, ...query } = opts;
|
|
1758
|
+
return (await call("GET", `/watchers/matches${queryString(query)}`, signal ? { signal } : {})).matches;
|
|
1759
|
+
},
|
|
1760
|
+
/** How a pulled match went: done, an error to try again, or `skip` to drop it. */
|
|
1761
|
+
ack: (id, body) => call("POST", `/watchers/matches/${encodeURIComponent(id)}/ack`, { body }),
|
|
1762
|
+
/**
|
|
1763
|
+
* Reads a platform channel (`#name`, an ID), thread (`C0123:1727000000.123456`),
|
|
1764
|
+
* or message permalink on demand, without syncing or storing it. Full access only.
|
|
1765
|
+
*/
|
|
1766
|
+
readSource: (key, target, opts = {}) => call("GET", `/sources/${key}/read${queryString({
|
|
1767
|
+
target,
|
|
1768
|
+
...opts
|
|
1769
|
+
})}`),
|
|
1770
|
+
/** Adds the app to a public channel so `readSource` can read it, where the sync opted in with `join`. Full access only. */
|
|
1771
|
+
joinSource: (key, target) => call("POST", `/sources/${key}/join`, { body: { target } }),
|
|
1772
|
+
poll,
|
|
1773
|
+
/** A platform's write, as its platform author: idempotent per platform event for a post, found from its origin otherwise. */
|
|
1774
|
+
syncImport: (body) => call("POST", "/sync/imports", { body }),
|
|
1775
|
+
/** What sync did on a platform: a delivery, its files, a failure, or a thread's new target. */
|
|
1776
|
+
syncReceipt: (body) => call("POST", "/sync/receipts", { body }),
|
|
1777
|
+
/** A board thread's sync, by its key. */
|
|
1778
|
+
syncThread: (thread) => call("GET", `/sync/threads/${path(thread)}`),
|
|
1779
|
+
/** What platform IDs are on the board: a thread, or a message. */
|
|
1780
|
+
syncFind: (query, opts = {}) => call("GET", `/sync/find${queryString(query)}`, opts.signal ? { signal: opts.signal } : {}),
|
|
1781
|
+
/** Messages' copies on platforms, by message and target. */
|
|
1782
|
+
syncCopies: async (messages) => (await call("GET", `/sync/copies?${messages.map((id) => `message=${encodeURIComponent(id)}`).join("&")}`)).copies,
|
|
1783
|
+
/** Pushes platform events into your inbox for sync key `key`; each platform ID lands once. */
|
|
1784
|
+
inboxPush: (key, events) => call("POST", `/inbox/${path(key)}`, { body: { events } }),
|
|
1785
|
+
/** Your next inbox events for `key`, leased to you until you ack them, one platform thread at a time. */
|
|
1786
|
+
inboxNext: async (key, opts = {}) => (await call("GET", `/inbox/${path(key)}/next${queryString(opts)}`)).items,
|
|
1787
|
+
/** How an inbox event went: done, or an error to try it again. */
|
|
1788
|
+
inboxAck: (key, id, body) => call("POST", `/inbox/${path(key)}/${path(id)}/ack`, { body }),
|
|
1789
|
+
/** Live events. Reconnects from the last seen index whenever the stream closes, and tells `onState` whether it's live or retrying. */
|
|
1790
|
+
async *events(query = {}, opts = {}) {
|
|
1791
|
+
const { last, ...rest } = query;
|
|
1792
|
+
let after = query.after;
|
|
1793
|
+
let failures = 0;
|
|
1794
|
+
const state = eventsState(opts.onState);
|
|
1795
|
+
while (!opts.signal?.aborted) {
|
|
1796
|
+
let response;
|
|
1797
|
+
try {
|
|
1798
|
+
response = await raw("GET", `/events${queryString({
|
|
1799
|
+
...filtersQuery(rest),
|
|
1800
|
+
...after === void 0 && last !== void 0 ? { last } : {},
|
|
1801
|
+
after
|
|
1802
|
+
})}`, {
|
|
1803
|
+
headers: { accept: "text/event-stream" },
|
|
1804
|
+
...opts.signal ? { signal: opts.signal } : {}
|
|
1805
|
+
});
|
|
1806
|
+
} catch (error) {
|
|
1807
|
+
if (opts.signal?.aborted) return;
|
|
1808
|
+
if (error instanceof BoardError && (error.status === 501 || error.status >= 400 && error.status < 500)) throw error;
|
|
1809
|
+
state.failed();
|
|
1810
|
+
await new Promise((r) => setTimeout(r, Math.min(3e4, 500 * 2 ** failures++)));
|
|
1811
|
+
continue;
|
|
1812
|
+
}
|
|
1813
|
+
failures = 0;
|
|
1814
|
+
state.answered();
|
|
1815
|
+
try {
|
|
1816
|
+
for await (const message of parseSse(response.body, opts.signal)) {
|
|
1817
|
+
if (message.id !== void 0) after = message.id;
|
|
1818
|
+
if (message.event === "board" && message.data) yield JSON.parse(message.data);
|
|
1819
|
+
}
|
|
1820
|
+
} catch (error) {
|
|
1821
|
+
if (opts.signal?.aborted) return;
|
|
1822
|
+
state.failed();
|
|
1823
|
+
await new Promise((r) => setTimeout(r, Math.min(3e4, 500 * 2 ** failures++)));
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
},
|
|
1827
|
+
/** The next matching event after `after` (default: now), or null on timeout. */
|
|
1828
|
+
async wait(query = {}, opts = {}) {
|
|
1829
|
+
const deadline = Date.now() + duration(opts.timeout ?? "30m");
|
|
1830
|
+
let after = query.after ?? (await poll({
|
|
1831
|
+
...query,
|
|
1832
|
+
after: void 0,
|
|
1833
|
+
wait: 0
|
|
1834
|
+
}, opts.signal)).cursor;
|
|
1835
|
+
while (Date.now() < deadline && !opts.signal?.aborted) {
|
|
1836
|
+
const wait = Math.max(0, Math.min(25, Math.ceil((deadline - Date.now()) / 1e3)));
|
|
1837
|
+
const page = await poll({
|
|
1838
|
+
...query,
|
|
1839
|
+
after,
|
|
1840
|
+
wait
|
|
1841
|
+
}, opts.signal);
|
|
1842
|
+
if (page.events.length > 0) return page.events[0];
|
|
1843
|
+
after = page.cursor;
|
|
1844
|
+
}
|
|
1845
|
+
return null;
|
|
1846
|
+
}
|
|
1847
|
+
};
|
|
1848
|
+
}
|
|
1849
|
+
async function toError(response) {
|
|
1850
|
+
try {
|
|
1851
|
+
const body = await response.json();
|
|
1852
|
+
const retryAfter = response.status === 429 ? /^\d{1,6}$/.exec(response.headers.get("retry-after") ?? "")?.[0] : void 0;
|
|
1853
|
+
if (body.error?.message) return new BoardError(body.error.code ?? "error", body.error.message, response.status, {
|
|
1854
|
+
...typeof body.error.hint === "string" ? { hint: body.error.hint } : {},
|
|
1855
|
+
origin: handleErrors.has(response) ? "handle" : "board",
|
|
1856
|
+
...retryAfter !== void 0 ? { retryAfter: Number(retryAfter) } : {}
|
|
1857
|
+
});
|
|
1858
|
+
} catch {}
|
|
1859
|
+
return new BoardError("http_error", `The board answered ${`${response.status} ${response.statusText}`.trim()}.`, response.status);
|
|
1860
|
+
}
|
|
1861
|
+
//#endregion
|
|
1862
|
+
//#region src/server/errors.ts
|
|
1863
|
+
var HttpError = class extends Error {
|
|
1864
|
+
status;
|
|
1865
|
+
code;
|
|
1866
|
+
/** What fixes it, when there's one (`BoardError`'s `hint`). */
|
|
1867
|
+
hint;
|
|
1868
|
+
/** `handle` for a handle's own error on its way through: everything else is the board's. */
|
|
1869
|
+
origin;
|
|
1870
|
+
/** For a 429, how many seconds until it's worth sending again, when that's known: the response's `Retry-After`. */
|
|
1871
|
+
retryAfter;
|
|
1872
|
+
constructor(status, code, message, options = {}) {
|
|
1873
|
+
super(message);
|
|
1874
|
+
this.status = status;
|
|
1875
|
+
this.code = code;
|
|
1876
|
+
this.hint = options.hint;
|
|
1877
|
+
this.origin = options.origin ?? "board";
|
|
1878
|
+
this.retryAfter = options.retryAfter;
|
|
1879
|
+
}
|
|
1880
|
+
};
|
|
1881
|
+
/** A request a host's own verifier rejected with `response`, which the board answers with as it is. */
|
|
1882
|
+
var RejectedError = class extends HttpError {
|
|
1883
|
+
response;
|
|
1884
|
+
constructor(response) {
|
|
1885
|
+
super(response.status, "rejected", `rejected with ${response.status}`);
|
|
1886
|
+
this.response = response;
|
|
1887
|
+
}
|
|
1888
|
+
};
|
|
1889
|
+
const badRequest = (message, code = "invalid_request") => new HttpError(400, code, message);
|
|
1890
|
+
const unauthorized = (message) => new HttpError(401, "unauthorized", message);
|
|
1891
|
+
const forbidden = (message) => new HttpError(403, "forbidden", message);
|
|
1892
|
+
const notFound = (message, code = "not_found") => new HttpError(404, code, message);
|
|
1893
|
+
const textTooLong = () => new HttpError(400, "text_too_long", `A message's text is at most ${MESSAGE_TEXT_MAX.toLocaleString("en-US")} characters.`, { hint: "Put the long part in a file and attach it to a message, with a short summary as the text." });
|
|
1894
|
+
const duplicateId = () => new HttpError(409, "duplicate_id", "that id was already used for a different write");
|
|
1895
|
+
/** More tasks than a board makes in one batch: `most` is how many it does. Nothing was made, and its sender splits it. */
|
|
1896
|
+
const batchTooLarge = (tasks, most) => new HttpError(400, "batch_too_large", `a batch makes ${most} tasks at most, and this one has ${tasks}: send it in parts`);
|
|
1897
|
+
const json = (body, status = 200, headers) => new Response(JSON.stringify(body), {
|
|
1898
|
+
status,
|
|
1899
|
+
headers: {
|
|
1900
|
+
"content-type": "application/json",
|
|
1901
|
+
...headers
|
|
1902
|
+
}
|
|
1903
|
+
});
|
|
1904
|
+
/**
|
|
1905
|
+
* The issue a request is told about. A union that matched no option says only "Invalid input", so
|
|
1906
|
+
* it's the first issue of the option that came closest, the one with the fewest, at its own path.
|
|
1907
|
+
*/
|
|
1908
|
+
function reported(issue) {
|
|
1909
|
+
if (issue.code === "invalid_key" && issue.issues[0]) return reported({
|
|
1910
|
+
...issue.issues[0],
|
|
1911
|
+
path: issue.path.slice(0, -1)
|
|
1912
|
+
});
|
|
1913
|
+
if (issue.code !== "invalid_union" || issue.errors.length === 0) return issue;
|
|
1914
|
+
const first = issue.errors.reduce((best, option) => option.length < best.length ? option : best)[0];
|
|
1915
|
+
return first ? reported({
|
|
1916
|
+
...first,
|
|
1917
|
+
path: [...issue.path, ...first.path]
|
|
1918
|
+
}) : issue;
|
|
1919
|
+
}
|
|
1920
|
+
/** Every failure becomes `{ error: { code, message, hint? } }` with a matching status. */
|
|
1921
|
+
function toErrorResponse(error) {
|
|
1922
|
+
if (error instanceof RejectedError) return error.response;
|
|
1923
|
+
if (error instanceof HttpError) {
|
|
1924
|
+
const response = json({ error: {
|
|
1925
|
+
code: error.code,
|
|
1926
|
+
message: error.message,
|
|
1927
|
+
...error.hint !== void 0 ? { hint: error.hint } : {}
|
|
1928
|
+
} }, error.status, error.retryAfter !== void 0 ? { "retry-after": String(error.retryAfter) } : void 0);
|
|
1929
|
+
if (error.origin === "handle") handleErrors.add(response);
|
|
1930
|
+
return response;
|
|
1931
|
+
}
|
|
1932
|
+
if (error instanceof ZodError) {
|
|
1933
|
+
const issue = error.issues[0] && reported(error.issues[0]);
|
|
1934
|
+
if (issue?.code === "too_big" && issue.origin === "string" && issue.maximum === 1e4 && issue.path.length === 1 && issue.path[0] === "text") return toErrorResponse(textTooLong());
|
|
1935
|
+
const path = issue?.path.length ? `${issue.path.join(".")}: ` : "";
|
|
1936
|
+
return json({ error: {
|
|
1937
|
+
code: "invalid_request",
|
|
1938
|
+
message: `${path}${issue?.message ?? "invalid request"}`
|
|
1939
|
+
} }, 400);
|
|
1940
|
+
}
|
|
1941
|
+
const code = error?.code;
|
|
1942
|
+
if (code === "INVALID_PAYLOAD") return json({ error: {
|
|
1943
|
+
code: "invalid_request",
|
|
1944
|
+
message: error.message
|
|
1945
|
+
} }, 400);
|
|
1946
|
+
if (code === "PARTIAL_DUPLICATE_BATCH" || code === "DUPLICATE_EVENT_ID") return toErrorResponse(duplicateId());
|
|
1947
|
+
if (code === "STORE_UNAVAILABLE" || code === "STORE_NOT_CONFIGURED") return json({ error: {
|
|
1948
|
+
code: "unavailable",
|
|
1949
|
+
message: error.message
|
|
1950
|
+
} }, 503);
|
|
1951
|
+
console.error("[board]", error);
|
|
1952
|
+
return json({ error: {
|
|
1953
|
+
code: "internal",
|
|
1954
|
+
message: "internal error"
|
|
1955
|
+
} }, 500);
|
|
1956
|
+
}
|
|
1957
|
+
//#endregion
|
|
1958
|
+
export { SchedulesQuery as $, CreatedTasksQuery as A, InboxPushBody as B, ClaimReadyTaskBody as C, CreateGrantsBody as D, CreateChannelBody as E, FailTaskBody as F, PostBody as G, LabelBody as H, Filters as I, RegisterBody as J, ReactBody as K, GrantBody as L, EditBody as M, EventsQuery as N, CreateTaskBody as O, ExplainQuery as P, ScheduleBody as Q, InboxAckBody as R, ChannelsQuery as S, CompleteTaskBody as T, LinkedQuery as U, InboxQuery as V, MatchesQuery as W, RenewTaskBody as X, ReleaseTaskBody as Y, RevokeGrantBody as Z, ActorsQuery as _, duplicateId as a, SyncFindQuery as at, CancelTaskBody as b, notFound as c, TaskNeedsBody as ct, unauthorized as d, ThreadsQuery as dt, SchemaBody as et, BoardError as f, TypingBody as ft, AckBody as g, eventsState as h, routes as ht, batchTooLarge as i, SyncCopiesQuery as it, DeleteBody as j, CreateTasksBody as k, textTooLong as l, TasksQuery as lt, duration as m, UploadStartBody as mt, RejectedError as n, SourceReadQuery as nt, forbidden as o, SyncImportBody as ot, createBoardClient as p, UpdateGrantBody as pt, Ref as q, badRequest as r, SyncBody as rt, json as s, SyncReceiptBody as st, HttpError as t, SourceJoinBody as tt, toErrorResponse as u, ThreadQuery as ut, AssignTaskBody as v, ClaimTaskBody as w, ChannelMessagesQuery as x, BlockTaskBody as y, InboxNextQuery as z };
|