@vercel/board 0.48.1 → 0.49.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/dist/{access-C06pIr0M.mjs → access-B8DnrnHa.mjs} +29 -17
- package/dist/access.d.mts +2 -2
- package/dist/access.mjs +5 -5
- package/dist/{actors-Dkyg-oo0.mjs → actors-DewJKyqy.mjs} +4 -3
- package/dist/{auth-B9-wXQ-x.mjs → auth-CEU06Nyp.mjs} +2 -2
- package/dist/{auth-li3lKwJG.d.mts → auth-Z9-Khvvw.d.mts} +1 -1
- package/dist/{backend-UBP6HimX.d.mts → backend-BzCXxVZ-.d.mts} +19 -5
- package/dist/backend-local.d.mts +1 -1
- package/dist/backend-local.mjs +1 -1
- package/dist/{chat-provider-kNWd6MQC.mjs → chat-provider-BS_fr61g.mjs} +2 -2
- package/dist/{chat-provider-iN0DFsSE.d.mts → chat-provider-DQ1GHm4o.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 +2 -2
- package/dist/cli-embedded.mjs +3 -3
- package/dist/cli.mjs +6 -6
- package/dist/client-C2T4eaXd.d.mts +544 -0
- package/dist/{commands-CJsOA-hf.d.mts → commands-Cm4sODIu.d.mts} +12 -8
- package/dist/{commands-Dk6_cHLS.mjs → commands-nN8WpvCe.mjs} +75 -58
- package/dist/{conformance-CDP8y2Zm.mjs → conformance-0xONNKcB.mjs} +102 -20
- package/dist/conformance.d.mts +1 -1
- package/dist/conformance.mjs +1 -1
- package/dist/{engine-BQ0hJw2w.mjs → engine-BH_nfJ3N.mjs} +6 -5
- package/dist/{errors-QhkdzI-o.mjs → errors-DwV7I0g4.mjs} +53 -174
- package/dist/{events-B-qyxNsJ.mjs → events-B76-4bNp.mjs} +2 -2
- package/dist/{exports-inhbjAWi.d.mts → exports-CKEK1fxf.d.mts} +4 -4
- package/dist/{exports-BzSG0_wL.mjs → exports-DLzyKwEp.mjs} +336 -82
- package/dist/{external-BuVTXT1R.mjs → external-BLI4W_-V.mjs} +1 -1
- package/dist/{index-B0Fnhvk7.d.mts → index-DxqSw8yd.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 +39 -39
- package/dist/inspect-B2ImmOvw.mjs +272 -0
- package/dist/inspect-DnRQUmP8.mjs +2 -0
- package/dist/inspect.d.mts +17 -0
- package/dist/inspect.mjs +196 -0
- package/dist/{local-BdKxz-Dn.mjs → local-DPpSEq2_.mjs} +132 -50
- package/dist/{main-BgiViRZ8.mjs → main-CqX7JqzE.mjs} +214 -46
- package/dist/{memory-CMm-Fk_A.mjs → memory-DzqkXIB-.mjs} +2 -2
- package/dist/{middleware-CT6vMNUc.mjs → middleware-ysFm2S4v.mjs} +2 -2
- package/dist/{page-D-OzDOAX.mjs → page-7KWSGxyC.mjs} +12 -2
- package/dist/page-CN77B014.mjs +2 -0
- package/dist/profile-header-Cj0Cb1cf.mjs +163 -0
- package/dist/{provider-DC3WHUXo.d.mts → provider-Dwb_9mnu.d.mts} +3 -529
- package/dist/{routes-CjA6Mp4D.d.mts → routes-B6SEA9yw.d.mts} +130 -4
- package/dist/{routes-ABXf3CKq.mjs → routes-qS92xz8U.mjs} +33 -7
- package/dist/{schemas-7VFdomFB.mjs → schemas-TeVe4Udh.mjs} +4 -2
- package/dist/{serve-C2Jbr7pR.mjs → serve-CYr4RwRM.mjs} +5 -5
- package/dist/{server-EOQC5Gtq.mjs → server-V7qyVUiJ.mjs} +11 -9
- package/dist/{serving-BNdiReQT.mjs → serving-BzNxlnDV.mjs} +1 -1
- package/dist/{inspect-n9brh-TT.mjs → shell-BLHZoP-x.mjs} +25 -274
- package/dist/{sql-IEbO5vXs.mjs → sql-DIKQd5Me.mjs} +2 -2
- package/dist/{sqlite-CVG8XKHm.mjs → sqlite-BF9kBDh1.mjs} +2 -2
- package/dist/{sqlite-db-BJjPQYx4.mjs → sqlite-db-CSYwiZ4E.mjs} +1 -1
- package/dist/sync-chat-sdk.d.mts +2 -2
- package/dist/sync-chat-sdk.mjs +2 -2
- package/dist/sync-github.d.mts +2 -2
- package/dist/sync-github.mjs +2 -2
- package/dist/sync-linear.d.mts +3 -3
- package/dist/sync-linear.mjs +2 -2
- package/dist/sync-slack.d.mts +3 -3
- package/dist/sync-slack.mjs +11 -7
- package/dist/sync.d.mts +2 -2
- package/dist/sync.mjs +2 -2
- package/dist/{task-batch-D4p9N8lN.mjs → task-batch-CujTLmTB.mjs} +10 -11
- package/dist/{tokens-BCp4EWTQ.mjs → tokens-BmCDAT4N.mjs} +2 -2
- package/dist/version-BCoWGLEW.mjs +5 -0
- package/docs/01-board/01-mounting.md +8 -0
- package/docs/01-board/04-messages.md +6 -2
- package/docs/01-board/05-reading.md +17 -2
- package/docs/01-board/09-access.md +40 -6
- package/docs/01-board/11-tasks.md +3 -3
- package/docs/03-guides/02-inspector.md +31 -0
- package/docs/04-reference/01-cli.md +81 -32
- package/docs/04-reference/02-board.md +14 -3
- package/docs/04-reference/03-http.md +5 -3
- package/docs/04-reference/05-conformance.md +7 -0
- package/package.json +5 -1
- package/dist/inspect-BB-XUPuz.mjs +0 -2
- package/dist/page-Dr9Aqlej.mjs +0 -2
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { i as ATTACHMENT_BYTES_MAX, r as parseProfileHeader, t as InvalidProfileHeader } from "./profile-header-Cj0Cb1cf.mjs";
|
|
2
|
+
import "./schemas-TeVe4Udh.mjs";
|
|
3
|
+
import { t as Failed, u as who } from "./main-CqX7JqzE.mjs";
|
|
4
|
+
import { n as relative, r as shell, t as CSP } from "./shell-BLHZoP-x.mjs";
|
|
5
|
+
import { t as page } from "./page-7KWSGxyC.mjs";
|
|
6
|
+
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
7
|
+
import { createServer } from "node:http";
|
|
8
|
+
/** What a proxy adds to a request it forwards: Tailscale's sets `X-Forwarded-Host`, most others `X-Forwarded-For`. */
|
|
9
|
+
const FORWARDED = [
|
|
10
|
+
"forwarded",
|
|
11
|
+
"x-forwarded-for",
|
|
12
|
+
"x-forwarded-host",
|
|
13
|
+
"x-forwarded-proto",
|
|
14
|
+
"x-real-ip"
|
|
15
|
+
];
|
|
16
|
+
/** The prebuilt UI where board is installed, read only when an inspector starts without one. */
|
|
17
|
+
const bundle = async () => (await import("./page-CN77B014.mjs")).page();
|
|
18
|
+
/**
|
|
19
|
+
* The UI, and a proxy for the board's API that adds your credentials, for a server
|
|
20
|
+
* on 127.0.0.1 to answer with. Because the proxy acts as you, it answers only its
|
|
21
|
+
* own page: every request must name the server's host (no DNS rebinding) and carry
|
|
22
|
+
* the per-run token that only the page has, and writes must also come from its
|
|
23
|
+
* origin. Your credential for the board never leaves this process. `hosts` adds
|
|
24
|
+
* names a proxy in front of it serves the page on: whoever reaches the page there
|
|
25
|
+
* acts as you, unless `visitor` says who a request is: a client that acts as them,
|
|
26
|
+
* or null for one it can't name, which the proxy then refuses. A request a proxy
|
|
27
|
+
* forwarded must name one of them: the server's own name is this machine's alone,
|
|
28
|
+
* and a caller behind a proxy can send it too.
|
|
29
|
+
*/
|
|
30
|
+
async function createInspector(options) {
|
|
31
|
+
const { client } = options;
|
|
32
|
+
const me = await client.me();
|
|
33
|
+
const started = me.profile === null ? null : me.avatar ? {
|
|
34
|
+
name: me.profile,
|
|
35
|
+
avatar: me.avatar
|
|
36
|
+
} : me.profile;
|
|
37
|
+
const board = (await client.discovery().catch(() => null))?.url ?? client.url;
|
|
38
|
+
const app = options.page ?? await bundle();
|
|
39
|
+
const token = randomBytes(24).toString("base64url");
|
|
40
|
+
const allowed = new Set((options.hosts ?? []).map((host) => host.toLowerCase()));
|
|
41
|
+
let hosts = /* @__PURE__ */ new Set();
|
|
42
|
+
let proxied = /* @__PURE__ */ new Set();
|
|
43
|
+
let origins = /* @__PURE__ */ new Set();
|
|
44
|
+
const streams = /* @__PURE__ */ new Set();
|
|
45
|
+
async function proxy(request, response, url) {
|
|
46
|
+
const reading = request.method === "GET" || request.method === "HEAD";
|
|
47
|
+
if (!sameSecret(String(request.headers["x-inspect-token"] ?? ""), token) || !reading && !origins.has(String(request.headers.origin))) return json(response, 403, { error: {
|
|
48
|
+
code: "forbidden",
|
|
49
|
+
message: "the board is reachable only from the inspector's own page"
|
|
50
|
+
} });
|
|
51
|
+
const path = url.pathname.slice(6) || "/";
|
|
52
|
+
if (relative(path)) return json(response, 400, { error: {
|
|
53
|
+
code: "bad_path",
|
|
54
|
+
message: "no relative paths"
|
|
55
|
+
} });
|
|
56
|
+
const visiting = options.visitor?.(request);
|
|
57
|
+
if (visiting === null) return json(response, 401, { error: {
|
|
58
|
+
code: "unauthorized",
|
|
59
|
+
message: "the proxy in front of this page names no one for this request, so there's no one to act as"
|
|
60
|
+
} });
|
|
61
|
+
const acting = visiting ?? client;
|
|
62
|
+
const controller = new AbortController();
|
|
63
|
+
streams.add(controller);
|
|
64
|
+
response.on("close", () => controller.abort());
|
|
65
|
+
try {
|
|
66
|
+
const body = reading ? void 0 : await read(request, path === "/attachments" ? ATTACHMENT_BYTES_MAX : void 0);
|
|
67
|
+
let as = null;
|
|
68
|
+
const named = request.headers["x-board-profile"];
|
|
69
|
+
if (typeof named === "string" && named.trim() !== "") try {
|
|
70
|
+
const { name, avatar } = parseProfileHeader(named);
|
|
71
|
+
as = avatar === null ? name : {
|
|
72
|
+
name,
|
|
73
|
+
avatar
|
|
74
|
+
};
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (!(error instanceof InvalidProfileHeader)) throw error;
|
|
77
|
+
return json(response, 400, { error: {
|
|
78
|
+
code: "invalid_profile",
|
|
79
|
+
message: error.message
|
|
80
|
+
} });
|
|
81
|
+
}
|
|
82
|
+
const upstream = await acting.request(`${path}${url.search}`, {
|
|
83
|
+
as,
|
|
84
|
+
method: request.method ?? "GET",
|
|
85
|
+
headers: {
|
|
86
|
+
...request.headers["content-type"] ? { "content-type": String(request.headers["content-type"]) } : {},
|
|
87
|
+
...request.headers.accept ? { accept: String(request.headers.accept) } : {},
|
|
88
|
+
...request.headers["x-board-filename"] ? { "x-board-filename": String(request.headers["x-board-filename"]) } : {}
|
|
89
|
+
},
|
|
90
|
+
...body ? { body } : {},
|
|
91
|
+
signal: controller.signal
|
|
92
|
+
});
|
|
93
|
+
response.writeHead(upstream.status, {
|
|
94
|
+
"content-type": upstream.headers.get("content-type") ?? "application/json",
|
|
95
|
+
"cache-control": "no-store",
|
|
96
|
+
"x-content-type-options": "nosniff"
|
|
97
|
+
});
|
|
98
|
+
if (upstream.body) for await (const chunk of upstream.body) response.write(chunk);
|
|
99
|
+
response.end();
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (controller.signal.aborted || response.headersSent) return void response.end();
|
|
102
|
+
json(response, 502, { error: {
|
|
103
|
+
code: "unreachable",
|
|
104
|
+
message: `the board didn't answer: ${error.message}`
|
|
105
|
+
} });
|
|
106
|
+
} finally {
|
|
107
|
+
streams.delete(controller);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function handle(request, response) {
|
|
111
|
+
let url;
|
|
112
|
+
try {
|
|
113
|
+
url = new URL(request.url ?? "/", "http://inspector");
|
|
114
|
+
} catch {
|
|
115
|
+
return send(response, 400, "text/plain; charset=utf-8", "bad request");
|
|
116
|
+
}
|
|
117
|
+
const named = String(request.headers.host).toLowerCase();
|
|
118
|
+
if (!(FORWARDED.some((header) => request.headers[header] !== void 0) ? proxied : hosts).has(named)) return send(response, 421, "text/plain; charset=utf-8", "wrong host");
|
|
119
|
+
if (url.pathname === "/board" || url.pathname.startsWith("/board/")) return void proxy(request, response, url).catch(() => response.headersSent ? response.end() : send(response, 500, "text/plain; charset=utf-8", "error"));
|
|
120
|
+
if (request.method !== "GET") return send(response, 405, "text/plain; charset=utf-8", "method not allowed");
|
|
121
|
+
if (url.pathname === "/app.js") return app ? send(response, 200, "text/javascript; charset=utf-8", app) : send(response, 503, "text/plain; charset=utf-8", "the inspector's UI isn't built: run `pnpm --filter @vercel/board build`");
|
|
122
|
+
send(response, 200, "text/html; charset=utf-8", shell({
|
|
123
|
+
token,
|
|
124
|
+
profile: started,
|
|
125
|
+
base: ""
|
|
126
|
+
}), { "content-security-policy": CSP });
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
board,
|
|
130
|
+
me,
|
|
131
|
+
at(port) {
|
|
132
|
+
const own = [`127.0.0.1:${port}`, `localhost:${port}`];
|
|
133
|
+
hosts = /* @__PURE__ */ new Set([...own, ...allowed]);
|
|
134
|
+
proxied = new Set([...allowed].filter((host) => !own.includes(host)));
|
|
135
|
+
origins = /* @__PURE__ */ new Set([
|
|
136
|
+
`http://127.0.0.1:${port}`,
|
|
137
|
+
`http://localhost:${port}`,
|
|
138
|
+
...[...allowed].flatMap((host) => [`http://${host}`, `https://${host}`])
|
|
139
|
+
]);
|
|
140
|
+
return {
|
|
141
|
+
handle,
|
|
142
|
+
close() {
|
|
143
|
+
for (const stream of streams) stream.abort();
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/** The inspector on a server of its own, on 127.0.0.1: `port`, or 4747, or any free one when that's taken. */
|
|
150
|
+
async function startInspector(options) {
|
|
151
|
+
const inspector = await createInspector(options);
|
|
152
|
+
let page = null;
|
|
153
|
+
const server = createServer((request, response) => page ? page.handle(request, response) : void response.writeHead(503).end());
|
|
154
|
+
const port = await listen(server, options.port ?? 4747, options.port === void 0);
|
|
155
|
+
const serving = page = inspector.at(port);
|
|
156
|
+
return {
|
|
157
|
+
url: `http://127.0.0.1:${port}`,
|
|
158
|
+
board: inspector.board,
|
|
159
|
+
me: inspector.me,
|
|
160
|
+
close: () => new Promise((resolve) => {
|
|
161
|
+
serving.close();
|
|
162
|
+
server.close(() => resolve());
|
|
163
|
+
server.closeAllConnections();
|
|
164
|
+
})
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function sameSecret(given, expected) {
|
|
168
|
+
const a = Buffer.from(given);
|
|
169
|
+
const b = Buffer.from(expected);
|
|
170
|
+
return a.length === b.length && timingSafeEqual(a, b);
|
|
171
|
+
}
|
|
172
|
+
function read(request, limit = 1048576) {
|
|
173
|
+
return new Promise((resolve, reject) => {
|
|
174
|
+
let size = 0;
|
|
175
|
+
const chunks = [];
|
|
176
|
+
request.on("data", (chunk) => {
|
|
177
|
+
size += chunk.length;
|
|
178
|
+
if (size > limit) {
|
|
179
|
+
reject(/* @__PURE__ */ new Error("request too large"));
|
|
180
|
+
request.destroy();
|
|
181
|
+
} else chunks.push(chunk);
|
|
182
|
+
});
|
|
183
|
+
request.on("end", () => resolve(new Uint8Array(Buffer.concat(chunks))));
|
|
184
|
+
request.on("error", reject);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
function send(response, status, type, body, headers = {}) {
|
|
188
|
+
response.writeHead(status, {
|
|
189
|
+
"content-type": type,
|
|
190
|
+
"cache-control": "no-store",
|
|
191
|
+
"x-content-type-options": "nosniff",
|
|
192
|
+
"referrer-policy": "no-referrer",
|
|
193
|
+
...headers
|
|
194
|
+
});
|
|
195
|
+
response.end(body);
|
|
196
|
+
}
|
|
197
|
+
const json = (response, status, body) => send(response, status, "application/json", JSON.stringify(body));
|
|
198
|
+
/** Listens on the port, or any free one when the default port is taken. */
|
|
199
|
+
async function listen(server, port, fallback) {
|
|
200
|
+
try {
|
|
201
|
+
return await listenOn(server, port);
|
|
202
|
+
} catch (error) {
|
|
203
|
+
if (fallback && error.code === "EADDRINUSE") return listenOn(server, 0);
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function listenOn(server, port) {
|
|
208
|
+
return new Promise((resolve, reject) => {
|
|
209
|
+
const onError = (error) => {
|
|
210
|
+
server.off("listening", onListening);
|
|
211
|
+
reject(error);
|
|
212
|
+
};
|
|
213
|
+
const onListening = () => {
|
|
214
|
+
server.off("error", onError);
|
|
215
|
+
resolve(server.address().port);
|
|
216
|
+
};
|
|
217
|
+
server.once("error", onError);
|
|
218
|
+
server.once("listening", onListening);
|
|
219
|
+
server.listen(port, "127.0.0.1");
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
//#endregion
|
|
223
|
+
//#region src/cli/inspect.ts
|
|
224
|
+
/** Until Ctrl-C or a SIGTERM, whichever comes first: then neither is listened for. */
|
|
225
|
+
const stopped = () => new Promise((resolve) => {
|
|
226
|
+
const done = () => {
|
|
227
|
+
process.off("SIGINT", done);
|
|
228
|
+
process.off("SIGTERM", done);
|
|
229
|
+
resolve();
|
|
230
|
+
};
|
|
231
|
+
process.once("SIGINT", done);
|
|
232
|
+
process.once("SIGTERM", done);
|
|
233
|
+
});
|
|
234
|
+
/** The page to serve, before anything starts: an install without one says so, and serves nothing. */
|
|
235
|
+
async function pageOf() {
|
|
236
|
+
const found = await page();
|
|
237
|
+
if (found === null) throw new Failed({
|
|
238
|
+
class: "unknown",
|
|
239
|
+
reason: "no_inspector_page",
|
|
240
|
+
message: "The inspector's page isn't installed with this CLI.",
|
|
241
|
+
next: [{ instruction: "Reinstall or update the CLI" }]
|
|
242
|
+
});
|
|
243
|
+
return found;
|
|
244
|
+
}
|
|
245
|
+
/** The hosts `--allow-host`, or `--tailscale`, names: each a host, with its port when that isn't its scheme's default. */
|
|
246
|
+
function allowedHosts({ flags, usage }, flag = "allow-host") {
|
|
247
|
+
const hosts = flags[flag] ?? [];
|
|
248
|
+
const bad = hosts.find((host) => !/^[a-z0-9.-]+(:\d+)?$/i.test(host));
|
|
249
|
+
if (bad !== void 0) throw usage(`--${flag} takes a host, with a port if it isn't the default, like box.tailnet.ts.net: not ${bad}.`);
|
|
250
|
+
return [...new Set(hosts.flatMap((host) => [host, host.replace(/:(443|80)$/, "")]))];
|
|
251
|
+
}
|
|
252
|
+
async function inspect({ flags, out, usage, portFlag, client, openBrowser }) {
|
|
253
|
+
const port = portFlag();
|
|
254
|
+
const hosts = allowedHosts({
|
|
255
|
+
flags,
|
|
256
|
+
usage
|
|
257
|
+
});
|
|
258
|
+
const page = await pageOf();
|
|
259
|
+
const inspector = await startInspector({
|
|
260
|
+
client: client(),
|
|
261
|
+
page,
|
|
262
|
+
hosts,
|
|
263
|
+
...port !== void 0 ? { port } : {}
|
|
264
|
+
});
|
|
265
|
+
out(`inspecting ${inspector.board} as ${who(inspector.me)} at ${inspector.url} (Ctrl-C to stop)`);
|
|
266
|
+
if (!flags["no-open"]) openBrowser(inspector.url);
|
|
267
|
+
await stopped();
|
|
268
|
+
await inspector.close();
|
|
269
|
+
return 0;
|
|
270
|
+
}
|
|
271
|
+
//#endregion
|
|
272
|
+
export { createInspector as i, inspect as n, stopped as r, allowedHosts as t };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { t as BoardClient } from "./client-C2T4eaXd.mjs";
|
|
2
|
+
//#region src/inspect/handler.d.ts
|
|
3
|
+
/** Answers the inspector's requests under `basePath`: `GET`, `POST`, `PATCH`, `PUT`, and `DELETE` alike. */
|
|
4
|
+
export type InspectorHandler = (request: Request) => Promise<Response>;
|
|
5
|
+
/**
|
|
6
|
+
* The inspector for `board`, served under `basePath` (`/board`: the page at `/board`, its bundle at
|
|
7
|
+
* `/board/app.js`, its proxy at `/board/board/*`). The proxy answers only the page: a request names
|
|
8
|
+
* its token, a browser's says it's from this origin (`Sec-Fetch-Site`), and a write carries this
|
|
9
|
+
* origin. Each request acts as `board`, under the profile the page picks, the way any handle names
|
|
10
|
+
* one (`x-board-profile`). A write that names none is refused: the handle is the deployment, not
|
|
11
|
+
* whoever's visiting, so the page asks who's posting before it loads.
|
|
12
|
+
*/
|
|
13
|
+
export declare function inspector(options: {
|
|
14
|
+
board: BoardClient;
|
|
15
|
+
basePath: string;
|
|
16
|
+
}): InspectorHandler;
|
|
17
|
+
//#endregion
|
package/dist/inspect.mjs
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { i as ATTACHMENT_BYTES_MAX, r as parseProfileHeader, t as InvalidProfileHeader } from "./profile-header-Cj0Cb1cf.mjs";
|
|
2
|
+
import "./schemas-TeVe4Udh.mjs";
|
|
3
|
+
import { n as relative, r as shell, t as CSP } from "./shell-BLHZoP-x.mjs";
|
|
4
|
+
import { t as page } from "./page-7KWSGxyC.mjs";
|
|
5
|
+
//#region src/inspect/handler.ts
|
|
6
|
+
/**
|
|
7
|
+
* The page's credential for its proxy. A server answers it on many instances, so it's the same for
|
|
8
|
+
* every page: it's no secret, it's a header no other site's page can send without asking first.
|
|
9
|
+
*/
|
|
10
|
+
const TOKEN = "page";
|
|
11
|
+
/** A JSON body the inspector reads whole: everything but an upload. */
|
|
12
|
+
const BODY_MAX = 1048576;
|
|
13
|
+
/**
|
|
14
|
+
* The inspector for `board`, served under `basePath` (`/board`: the page at `/board`, its bundle at
|
|
15
|
+
* `/board/app.js`, its proxy at `/board/board/*`). The proxy answers only the page: a request names
|
|
16
|
+
* its token, a browser's says it's from this origin (`Sec-Fetch-Site`), and a write carries this
|
|
17
|
+
* origin. Each request acts as `board`, under the profile the page picks, the way any handle names
|
|
18
|
+
* one (`x-board-profile`). A write that names none is refused: the handle is the deployment, not
|
|
19
|
+
* whoever's visiting, so the page asks who's posting before it loads.
|
|
20
|
+
*/
|
|
21
|
+
function inspector(options) {
|
|
22
|
+
const { board } = options;
|
|
23
|
+
const base = options.basePath.replace(/\/+$/, "");
|
|
24
|
+
const app = cached(page);
|
|
25
|
+
const started = cached(async () => profileOf(await board.me()));
|
|
26
|
+
async function proxy(request, path, search) {
|
|
27
|
+
const reading = request.method === "GET" || request.method === "HEAD";
|
|
28
|
+
const site = request.headers.get("sec-fetch-site");
|
|
29
|
+
if (request.headers.get("x-inspect-token") !== TOKEN || site !== null && site !== "same-origin" || !reading && !sameOrigin(request)) return json(403, { error: {
|
|
30
|
+
code: "forbidden",
|
|
31
|
+
message: "the board is reachable only from the inspector's own page"
|
|
32
|
+
} });
|
|
33
|
+
if (relative(path)) return json(400, { error: {
|
|
34
|
+
code: "bad_path",
|
|
35
|
+
message: "no relative paths"
|
|
36
|
+
} });
|
|
37
|
+
let as = null;
|
|
38
|
+
const named = request.headers.get("x-board-profile");
|
|
39
|
+
if (named !== null && named.trim() !== "") try {
|
|
40
|
+
const { name, avatar } = parseProfileHeader(named);
|
|
41
|
+
as = avatar === null ? name : {
|
|
42
|
+
name,
|
|
43
|
+
avatar
|
|
44
|
+
};
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (!(error instanceof InvalidProfileHeader)) throw error;
|
|
47
|
+
return json(400, { error: {
|
|
48
|
+
code: "invalid_profile",
|
|
49
|
+
message: error.message
|
|
50
|
+
} });
|
|
51
|
+
}
|
|
52
|
+
if (!reading && (as === null || nameOf(as) === nameOf(await started().catch(() => null)))) return json(401, { error: {
|
|
53
|
+
code: "profile_required",
|
|
54
|
+
message: "name a profile to write as, other than the app's own: this page doesn't know who you are"
|
|
55
|
+
} });
|
|
56
|
+
let body;
|
|
57
|
+
if (!reading) {
|
|
58
|
+
const limit = path === "/attachments" ? ATTACHMENT_BYTES_MAX : BODY_MAX;
|
|
59
|
+
const read = await readUpTo(request, limit);
|
|
60
|
+
if (read === null) return json(413, { error: {
|
|
61
|
+
code: "too_large",
|
|
62
|
+
message: `a request to the inspector is at most ${limit} bytes`
|
|
63
|
+
} });
|
|
64
|
+
body = read;
|
|
65
|
+
}
|
|
66
|
+
const forwarded = {};
|
|
67
|
+
for (const header of [
|
|
68
|
+
"content-type",
|
|
69
|
+
"accept",
|
|
70
|
+
"x-board-filename"
|
|
71
|
+
]) {
|
|
72
|
+
const value = request.headers.get(header);
|
|
73
|
+
if (value !== null) forwarded[header] = value;
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
const upstream = await board.request(`${path}${search}`, {
|
|
77
|
+
as,
|
|
78
|
+
method: request.method,
|
|
79
|
+
headers: forwarded,
|
|
80
|
+
...body ? { body } : {},
|
|
81
|
+
signal: request.signal
|
|
82
|
+
});
|
|
83
|
+
return new Response(upstream.body, {
|
|
84
|
+
status: upstream.status,
|
|
85
|
+
headers: {
|
|
86
|
+
"content-type": upstream.headers.get("content-type") ?? "application/json",
|
|
87
|
+
"cache-control": "no-store",
|
|
88
|
+
"x-content-type-options": "nosniff"
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
} catch (error) {
|
|
92
|
+
return json(502, { error: {
|
|
93
|
+
code: "unreachable",
|
|
94
|
+
message: `the board didn't answer: ${error.message}`
|
|
95
|
+
} });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return async (request) => {
|
|
99
|
+
const url = new URL(request.url);
|
|
100
|
+
const at = url.pathname === base || url.pathname.startsWith(`${base}/`) ? url.pathname.slice(base.length) || "/" : null;
|
|
101
|
+
if (at === null) return text(404, "not found");
|
|
102
|
+
if (at === "/board" || at.startsWith("/board/")) return proxy(request, at.slice(6) || "/", url.search);
|
|
103
|
+
if (request.method !== "GET" && request.method !== "HEAD") return text(405, "method not allowed");
|
|
104
|
+
if (at === "/app.js") {
|
|
105
|
+
const script = await app();
|
|
106
|
+
if (script === null) return text(503, "the inspector's UI isn't installed with @vercel/board");
|
|
107
|
+
return new Response(script, { headers: {
|
|
108
|
+
...HEADERS,
|
|
109
|
+
"content-type": "text/javascript; charset=utf-8",
|
|
110
|
+
"cache-control": "private, max-age=31536000, immutable"
|
|
111
|
+
} });
|
|
112
|
+
}
|
|
113
|
+
const profile = await started().catch(() => null);
|
|
114
|
+
return new Response(shell({
|
|
115
|
+
token: TOKEN,
|
|
116
|
+
profile,
|
|
117
|
+
base,
|
|
118
|
+
named: true
|
|
119
|
+
}), { headers: {
|
|
120
|
+
...HEADERS,
|
|
121
|
+
"content-type": "text/html; charset=utf-8",
|
|
122
|
+
"content-security-policy": CSP
|
|
123
|
+
} });
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* A request's body, or null once it's more than `limit` bytes: a `Content-Length` over it is refused
|
|
128
|
+
* before reading, and a body without one stops being read, and is canceled, as soon as it passes it.
|
|
129
|
+
*/
|
|
130
|
+
async function readUpTo(request, limit) {
|
|
131
|
+
if (Number(request.headers.get("content-length") ?? 0) > limit) return null;
|
|
132
|
+
if (request.body === null) return /* @__PURE__ */ new Uint8Array(0);
|
|
133
|
+
const reader = request.body.getReader();
|
|
134
|
+
const chunks = [];
|
|
135
|
+
let size = 0;
|
|
136
|
+
for (;;) {
|
|
137
|
+
const { done, value } = await reader.read();
|
|
138
|
+
if (done) break;
|
|
139
|
+
size += value.byteLength;
|
|
140
|
+
if (size > limit) {
|
|
141
|
+
await reader.cancel().catch(() => {});
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
chunks.push(value);
|
|
145
|
+
}
|
|
146
|
+
const body = new Uint8Array(size);
|
|
147
|
+
let at = 0;
|
|
148
|
+
for (const chunk of chunks) {
|
|
149
|
+
body.set(chunk, at);
|
|
150
|
+
at += chunk.byteLength;
|
|
151
|
+
}
|
|
152
|
+
return body;
|
|
153
|
+
}
|
|
154
|
+
/** `load`'s answer, asked for once: a failure is asked again next time. */
|
|
155
|
+
function cached(load) {
|
|
156
|
+
let promise = null;
|
|
157
|
+
return () => promise ??= load().catch((error) => {
|
|
158
|
+
promise = null;
|
|
159
|
+
throw error;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
const HEADERS = {
|
|
163
|
+
"cache-control": "no-store",
|
|
164
|
+
"x-content-type-options": "nosniff",
|
|
165
|
+
"referrer-policy": "no-referrer"
|
|
166
|
+
};
|
|
167
|
+
/** The profile a handle acts as, as the page is told it. */
|
|
168
|
+
const profileOf = (me) => me.profile === null ? null : me.avatar ? {
|
|
169
|
+
name: me.profile,
|
|
170
|
+
avatar: me.avatar
|
|
171
|
+
} : me.profile;
|
|
172
|
+
/** A profile's name as the board keys it: lowercased, so `Factory` is the same actor as `factory`. */
|
|
173
|
+
const nameOf = (profile) => {
|
|
174
|
+
if (profile === null) return null;
|
|
175
|
+
return (typeof profile === "string" ? profile : profile.name).toLowerCase();
|
|
176
|
+
};
|
|
177
|
+
/** A write's `Origin` is this server's, as the request reached it. */
|
|
178
|
+
function sameOrigin(request) {
|
|
179
|
+
const origin = request.headers.get("origin");
|
|
180
|
+
if (origin === null) return false;
|
|
181
|
+
const host = URL.parse(origin)?.host;
|
|
182
|
+
return host !== void 0 && (host === new URL(request.url).host || host === request.headers.get("host"));
|
|
183
|
+
}
|
|
184
|
+
const text = (status, body) => new Response(body, {
|
|
185
|
+
status,
|
|
186
|
+
headers: {
|
|
187
|
+
...HEADERS,
|
|
188
|
+
"content-type": "text/plain; charset=utf-8"
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
const json = (status, body) => Response.json(body, {
|
|
192
|
+
status,
|
|
193
|
+
headers: HEADERS
|
|
194
|
+
});
|
|
195
|
+
//#endregion
|
|
196
|
+
export { inspector };
|