@livespace/plugin-studio 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +122 -2
- package/dist/auth.js +220 -0
- package/dist/cli.js +67 -0
- package/dist/keys.js +42 -0
- package/dist/manifest.js +16 -0
- package/dist/page/domain/address.js +79 -0
- package/dist/page/domain/connection.js +2 -0
- package/dist/page/domain/display.js +38 -0
- package/dist/page/domain/draft.js +80 -0
- package/dist/page/domain/failures-store.js +41 -0
- package/dist/page/domain/failures.js +75 -0
- package/dist/page/domain/layout-store.js +119 -0
- package/dist/page/domain/log-store.js +37 -0
- package/dist/page/domain/navigation-store.js +17 -0
- package/dist/page/domain/plugin-data.js +70 -0
- package/dist/page/domain/plugin-store.js +257 -0
- package/dist/page/domain/probe-store.js +100 -0
- package/dist/page/domain/profile-session.js +115 -0
- package/dist/page/domain/profile-store.js +51 -0
- package/dist/page/domain/samples.js +221 -0
- package/dist/page/domain/secrets-store.js +159 -0
- package/dist/page/domain/secrets.js +135 -0
- package/dist/page/domain/settings-data.js +64 -0
- package/dist/page/domain/settings-session.js +206 -0
- package/dist/page/domain/settings-store.js +62 -0
- package/dist/page/domain/slot-store.js +50 -0
- package/dist/page/domain/studio.js +162 -0
- package/dist/page/domain/surface-store.js +71 -0
- package/dist/page/domain/toasts-store.js +60 -0
- package/dist/page/domain/view-data.js +122 -0
- package/dist/page/domain/view-store.js +132 -0
- package/dist/page/host.css +907 -0
- package/dist/page/index.html +31 -0
- package/dist/page/main.js +43 -0
- package/dist/page/plugin-studio-logo.svg +4 -0
- package/dist/page/reload.svg +1 -0
- package/dist/page/transport/browser-storage.js +41 -0
- package/dist/page/transport/console-mirror.js +48 -0
- package/dist/page/transport/embed.js +142 -0
- package/dist/page/transport/plugin-keys.js +68 -0
- package/dist/page/transport/studio-api.js +74 -0
- package/dist/page/ui/app.js +61 -0
- package/dist/page/ui/boot.js +44 -0
- package/dist/page/ui/brand.js +3 -0
- package/dist/page/ui/context.js +11 -0
- package/dist/page/ui/json-field.js +19 -0
- package/dist/page/ui/json-tokens.js +40 -0
- package/dist/page/ui/json-view.js +11 -0
- package/dist/page/ui/memo.js +23 -0
- package/dist/page/ui/messages.js +34 -0
- package/dist/page/ui/page-switcher.js +11 -0
- package/dist/page/ui/plugin-frame.js +43 -0
- package/dist/page/ui/profile-page.js +13 -0
- package/dist/page/ui/resize-handle.js +63 -0
- package/dist/page/ui/root.js +9 -0
- package/dist/page/ui/section.js +19 -0
- package/dist/page/ui/settings/config-form.js +59 -0
- package/dist/page/ui/settings/secret-field.js +22 -0
- package/dist/page/ui/settings/settings-page.js +60 -0
- package/dist/page/ui/sidebar/custom-settings-section.js +10 -0
- package/dist/page/ui/sidebar/failures-section.js +27 -0
- package/dist/page/ui/sidebar/installation-section.js +30 -0
- package/dist/page/ui/sidebar/metadata-section.js +9 -0
- package/dist/page/ui/sidebar/plugin-section.js +18 -0
- package/dist/page/ui/sidebar/preferences-section.js +16 -0
- package/dist/page/ui/sidebar/record-section.js +17 -0
- package/dist/page/ui/sidebar/viewer-section.js +8 -0
- package/dist/page/ui/sidebar-group.js +4 -0
- package/dist/page/ui/splash.js +6 -0
- package/dist/page/ui/status.js +10 -0
- package/dist/page/ui/surface.js +23 -0
- package/dist/page/ui/theme.js +13 -0
- package/dist/page/ui/toasts.js +31 -0
- package/dist/page/ui/top-bar.js +37 -0
- package/dist/server.js +519 -0
- package/dist/start.js +108 -0
- package/package.json +55 -5
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "preact/jsx-runtime";
|
|
2
|
+
import { useLayoutEffect, useRef } from "preact/hooks";
|
|
3
|
+
import { useStudio } from "./context.js";
|
|
4
|
+
const CALLOUT = {
|
|
5
|
+
default: "neutral",
|
|
6
|
+
success: "success",
|
|
7
|
+
warning: "warning",
|
|
8
|
+
error: "danger",
|
|
9
|
+
};
|
|
10
|
+
export function Toasts() {
|
|
11
|
+
const { toasts, layout } = useStudio();
|
|
12
|
+
const list = toasts.toasts.value;
|
|
13
|
+
const raised = toasts.raised.value;
|
|
14
|
+
const area = useRef(null);
|
|
15
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: each raise must put the toasts on top again.
|
|
16
|
+
useLayoutEffect(() => {
|
|
17
|
+
const element = area.current;
|
|
18
|
+
if (element === null) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (element.matches(":popover-open")) {
|
|
22
|
+
element.hidePopover();
|
|
23
|
+
}
|
|
24
|
+
if (list.length > 0) {
|
|
25
|
+
element.showPopover();
|
|
26
|
+
}
|
|
27
|
+
}, [list, raised]);
|
|
28
|
+
return (_jsx("div", { ref: area, id: "toasts", class: "toasts wa-stack wa-gap-xs", popover: "manual",
|
|
29
|
+
// Left of the messages pane while it's shown (host.css).
|
|
30
|
+
style: { "--messages-offset": `${layout.messagesOffset.value}px` }, children: list.map((toast) => (_jsx("wa-callout", { class: "toast", role: "status", variant: CALLOUT[toast.variant], appearance: "filled-outlined", size: "s", children: _jsxs("div", { class: "wa-flank:end wa-align-items-start", children: [_jsxs("div", { class: "wa-stack wa-gap-3xs", children: [_jsx("strong", { children: toast.title }), toast.body ? _jsx("span", { children: toast.body }) : null] }), _jsx("wa-button", { size: "s", appearance: "plain", "aria-label": "Dismiss", onClick: () => toasts.dismiss(toast.id), children: "\u00D7" })] }) }, toast.id))) }));
|
|
31
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "preact/jsx-runtime";
|
|
2
|
+
/** The top bar: the studio's name, the plugin's address in the middle, and the messages pane's toggle. */
|
|
3
|
+
import { useEffect, useState } from "preact/hooks";
|
|
4
|
+
import { STUDIO_LOGO, STUDIO_NAME } from "./brand.js";
|
|
5
|
+
import { useStudio } from "./context.js";
|
|
6
|
+
import { fieldValue } from "./status.js";
|
|
7
|
+
/**
|
|
8
|
+
* The address: typed here, then embedded on Enter or the button. The address embedded now is
|
|
9
|
+
* reloaded instead, with the messages cleared.
|
|
10
|
+
*/
|
|
11
|
+
function AddressForm() {
|
|
12
|
+
const studio = useStudio();
|
|
13
|
+
const { plugin } = studio;
|
|
14
|
+
const embedded = plugin.pluginUrl.value;
|
|
15
|
+
const problem = plugin.addressProblem.value;
|
|
16
|
+
const [text, setText] = useState(embedded ?? "");
|
|
17
|
+
// A new address from elsewhere (the studio's start) replaces what's in the field.
|
|
18
|
+
useEffect(() => setText(embedded ?? ""), [embedded]);
|
|
19
|
+
const reloading = embedded !== null && plugin.isCurrent(text);
|
|
20
|
+
const action = reloading ? "Reload the plugin" : "Embed this address";
|
|
21
|
+
return (_jsxs("form", { class: "url-form wa-cluster wa-gap-2xs", onSubmit: (event) => {
|
|
22
|
+
event.preventDefault();
|
|
23
|
+
void studio.embed(text);
|
|
24
|
+
}, children: [_jsx("wa-input", { size: "s", class: "address", label: "Plugin address", value: text, type: "url", spellcheck: false, autocomplete: "off", placeholder: "Your plugin's address: http://localhost:5173", "aria-invalid": problem !== undefined, "aria-describedby": "url-problem", onInput: (event) => {
|
|
25
|
+
setText(fieldValue(event));
|
|
26
|
+
plugin.clearAddressProblem();
|
|
27
|
+
} }), _jsx("wa-button", { size: "s", type: "submit", variant: reloading ? "neutral" : "brand", title: action, children: _jsx("wa-icon", { src: "/reload.svg", label: action }) }), problem ? (_jsx("p", { id: "url-problem", class: "url-problem", role: "alert", children: problem })) : null] }));
|
|
28
|
+
}
|
|
29
|
+
/** The messages pane's toggle: a "right sidebar" icon, pressed while the pane is shown. */
|
|
30
|
+
function MessagesToggle() {
|
|
31
|
+
const { layout } = useStudio();
|
|
32
|
+
const shown = layout.messagesShown.value;
|
|
33
|
+
return (_jsxs("button", { class: "icon-button", type: "button", "aria-pressed": shown, "aria-controls": "messages", title: shown ? "Hide the messages" : "Show the messages", onClick: layout.toggleMessages, children: [_jsxs("svg", { width: "18", height: "18", viewBox: "0 0 18 18", "aria-hidden": "true", children: [_jsx("rect", { x: "1.5", y: "2.5", width: "15", height: "13", rx: "2", fill: "none", stroke: "currentColor", "stroke-width": "1.5" }), _jsx("rect", { x: "10.5", y: "3.25", width: "5.25", height: "11.5", fill: "currentColor", opacity: "0.35" }), _jsx("line", { x1: "10.5", y1: "2.5", x2: "10.5", y2: "15.5", stroke: "currentColor", "stroke-width": "1.5" })] }), _jsx("span", { class: "wa-visually-hidden", children: "Messages" })] }));
|
|
34
|
+
}
|
|
35
|
+
export function TopBar() {
|
|
36
|
+
return (_jsxs("header", { class: "topbar", children: [_jsxs("strong", { class: "brand", children: [_jsx("img", { class: "brand-logo", src: STUDIO_LOGO, alt: "" }), STUDIO_NAME] }), _jsx(AddressForm, {}), _jsx(MessagesToggle, {})] }));
|
|
37
|
+
}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The studio's server: the page from `dist/page/`, the SDK's host module and tokens from the
|
|
3
|
+
* installed SDK, and the page's settings. Only those paths: nothing else on disk is served.
|
|
4
|
+
*/
|
|
5
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
6
|
+
import { readFile } from "node:fs/promises";
|
|
7
|
+
import { createServer, } from "node:http";
|
|
8
|
+
import { createRequire } from "node:module";
|
|
9
|
+
import { dirname, join } from "node:path";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
import { DevAccount, EXCHANGE_REFUSALS, KEYS_PATH, SECRETS_PATH, TOKEN_PATH, } from "./auth.js";
|
|
12
|
+
import { readManifestText } from "./manifest.js";
|
|
13
|
+
/** The same from `src/` and `dist/`; the page is only ever built into `dist/page/`. */
|
|
14
|
+
const PAGE_DIR = fileURLToPath(new URL("../dist/page/", import.meta.url));
|
|
15
|
+
const require = createRequire(import.meta.url);
|
|
16
|
+
const SDK_DIST = join(dirname(require.resolve("@livespace/plugin-sdk/package.json")), "dist");
|
|
17
|
+
const TYPES = {
|
|
18
|
+
".html": "text/html; charset=utf-8",
|
|
19
|
+
".css": "text/css; charset=utf-8",
|
|
20
|
+
".js": "text/javascript; charset=utf-8",
|
|
21
|
+
".mjs": "text/javascript; charset=utf-8",
|
|
22
|
+
".json": "application/json; charset=utf-8",
|
|
23
|
+
".svg": "image/svg+xml",
|
|
24
|
+
};
|
|
25
|
+
/** An `exports` entry's file for an ES module import: `import`, else `browser`, else `default`. */
|
|
26
|
+
function importTarget(entry) {
|
|
27
|
+
if (typeof entry === "string") {
|
|
28
|
+
return entry;
|
|
29
|
+
}
|
|
30
|
+
if (entry === null || typeof entry !== "object") {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
const conditions = entry;
|
|
34
|
+
for (const condition of ["import", "browser", "default"]) {
|
|
35
|
+
const target = importTarget(conditions[condition]);
|
|
36
|
+
if (target !== undefined) {
|
|
37
|
+
return target;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* A module's ES module file, as the page's import map names it: `preact/hooks` is the `./hooks`
|
|
44
|
+
* export of `preact`. Read from its package's `exports`, as a browser would get it from a CDN.
|
|
45
|
+
*/
|
|
46
|
+
function moduleFile(specifier) {
|
|
47
|
+
const parts = specifier.split("/");
|
|
48
|
+
const nameLength = specifier.startsWith("@") ? 2 : 1;
|
|
49
|
+
const name = parts.slice(0, nameLength).join("/");
|
|
50
|
+
const subpath = [".", ...parts.slice(nameLength)].join("/");
|
|
51
|
+
// The package's directory, from whatever its main entry resolves to.
|
|
52
|
+
let dir = dirname(require.resolve(name));
|
|
53
|
+
while (!existsSync(join(dir, "package.json")) || !isPackage(dir, name)) {
|
|
54
|
+
const parent = dirname(dir);
|
|
55
|
+
if (parent === dir) {
|
|
56
|
+
throw new Error(`Can't find ${name}'s package.json`);
|
|
57
|
+
}
|
|
58
|
+
dir = parent;
|
|
59
|
+
}
|
|
60
|
+
const { exports } = JSON.parse(readFileSync(join(dir, "package.json"), "utf8"));
|
|
61
|
+
const target = importTarget(exports?.[subpath]);
|
|
62
|
+
if (target === undefined) {
|
|
63
|
+
throw new Error(`${name} exports no ES module at ${subpath}`);
|
|
64
|
+
}
|
|
65
|
+
return join(dir, target);
|
|
66
|
+
}
|
|
67
|
+
function isPackage(dir, name) {
|
|
68
|
+
const { name: found } = JSON.parse(readFileSync(join(dir, "package.json"), "utf8"));
|
|
69
|
+
return found === name;
|
|
70
|
+
}
|
|
71
|
+
/** Request path → file: the SDK's two, and Preact's and signals' modules, one file each. */
|
|
72
|
+
const VENDOR_FILES = {
|
|
73
|
+
"/sdk/index.js": join(SDK_DIST, "index.js"),
|
|
74
|
+
"/sdk/ls-tokens.css": join(SDK_DIST, "ls-tokens.css"),
|
|
75
|
+
"/vendor/preact.js": moduleFile("preact"),
|
|
76
|
+
"/vendor/preact-hooks.js": moduleFile("preact/hooks"),
|
|
77
|
+
"/vendor/preact-jsx-runtime.js": moduleFile("preact/jsx-runtime"),
|
|
78
|
+
"/vendor/signals.js": moduleFile("@preact/signals"),
|
|
79
|
+
"/vendor/signals-core.js": moduleFile("@preact/signals-core"),
|
|
80
|
+
};
|
|
81
|
+
/** Request path prefix → directory: Web Awesome's browser build, which loads its parts by path. */
|
|
82
|
+
const VENDOR_DIRS = {
|
|
83
|
+
"/vendor/webawesome/": join(dirname(require.resolve("@awesome.me/webawesome/package.json")), "dist-cdn"),
|
|
84
|
+
};
|
|
85
|
+
/** Path segments a served file may have: none starting with a dot, so never `..`. */
|
|
86
|
+
const SEGMENT = /^[A-Za-z0-9_][A-Za-z0-9._-]*$/;
|
|
87
|
+
/** The page's own files: its modules, stylesheet and icons, in its own folders. */
|
|
88
|
+
const PAGE_FILE = /^\/(?:[a-z][a-z0-9-]*\/)*[a-z][a-z0-9-]*\.(?:js|css|svg)$/;
|
|
89
|
+
/** `rest` under `dir`, when every segment is a plain name. */
|
|
90
|
+
function within(dir, rest) {
|
|
91
|
+
const segments = rest.split("/");
|
|
92
|
+
return segments.length > 0 && segments.every((part) => SEGMENT.test(part))
|
|
93
|
+
? join(dir, ...segments)
|
|
94
|
+
: undefined;
|
|
95
|
+
}
|
|
96
|
+
function fileFor(path) {
|
|
97
|
+
if (path === "/") {
|
|
98
|
+
return join(PAGE_DIR, "index.html");
|
|
99
|
+
}
|
|
100
|
+
const vendored = VENDOR_FILES[path];
|
|
101
|
+
if (vendored !== undefined) {
|
|
102
|
+
return vendored;
|
|
103
|
+
}
|
|
104
|
+
for (const [prefix, dir] of Object.entries(VENDOR_DIRS)) {
|
|
105
|
+
if (path.startsWith(prefix)) {
|
|
106
|
+
return within(dir, path.slice(prefix.length));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return PAGE_FILE.test(path) ? join(PAGE_DIR, path) : undefined;
|
|
110
|
+
}
|
|
111
|
+
/** The project's manifest, parsed, or `null` when there's none or it isn't JSON. */
|
|
112
|
+
async function projectManifest(dir) {
|
|
113
|
+
const text = await readManifestText(dir);
|
|
114
|
+
if (text === undefined) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
return JSON.parse(text);
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** Why a plugin address can't be embedded, or undefined when it can. */
|
|
125
|
+
export function pluginUrlProblem(value, devOrigin) {
|
|
126
|
+
if (typeof value !== "string") {
|
|
127
|
+
return "The address is text: http or https.";
|
|
128
|
+
}
|
|
129
|
+
let url;
|
|
130
|
+
try {
|
|
131
|
+
url = new URL(value);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return "That isn't an address.";
|
|
135
|
+
}
|
|
136
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
137
|
+
return "The address must be http or https.";
|
|
138
|
+
}
|
|
139
|
+
if (url.origin === devOrigin) {
|
|
140
|
+
return "That's the studio's own origin. Livespace refuses a plugin on its own origin.";
|
|
141
|
+
}
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* The `kid` of a compact JWE sealed as the settings screen seals one, or undefined when it isn't
|
|
146
|
+
* one. Only the header is read: the studio can't open it, and shouldn't.
|
|
147
|
+
*/
|
|
148
|
+
export function sealedKid(jwe) {
|
|
149
|
+
if (typeof jwe !== "string" || jwe.split(".").length !== 5) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
const header = JSON.parse(Buffer.from(jwe.split(".")[0] ?? "", "base64url").toString("utf8"));
|
|
154
|
+
return header.alg === "RSA-OAEP-256" &&
|
|
155
|
+
header.enc === "A256GCM" &&
|
|
156
|
+
typeof header.kid === "string" &&
|
|
157
|
+
header.kid !== ""
|
|
158
|
+
? header.kid
|
|
159
|
+
: undefined;
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/** Where the plugin's keys are: the catalog manifest's `publicKeys`, or its origin. */
|
|
166
|
+
function keysOf(state) {
|
|
167
|
+
if (state.distribution === "url") {
|
|
168
|
+
return { source: "url" };
|
|
169
|
+
}
|
|
170
|
+
const manifest = state.manifest;
|
|
171
|
+
return { source: "manifest", keySet: manifest?.publicKeys ?? null };
|
|
172
|
+
}
|
|
173
|
+
/** A key set's `sig` key by `kid`, read as an RSA public key and nothing more. */
|
|
174
|
+
function signingKeyIn(keySet, kid) {
|
|
175
|
+
const keys = keySet?.keys;
|
|
176
|
+
if (!Array.isArray(keys)) {
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
const key = keys.find((k) => typeof k === "object" &&
|
|
180
|
+
k !== null &&
|
|
181
|
+
k.kid === kid &&
|
|
182
|
+
k.use === "sig" &&
|
|
183
|
+
k.kty === "RSA");
|
|
184
|
+
return key && typeof key.n === "string" && typeof key.e === "string"
|
|
185
|
+
? { kty: "RSA", kid: key.kid, use: "sig", alg: "RS256", n: key.n, e: key.e }
|
|
186
|
+
: undefined;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* The plugin's signing keys, where Livespace finds them: the manifest's `publicKeys` for a catalog
|
|
190
|
+
* plugin, else the key set published on its origin, fetched at every exchange so rotating works.
|
|
191
|
+
*/
|
|
192
|
+
function signingKeysOf(state) {
|
|
193
|
+
return async (kid) => {
|
|
194
|
+
const keys = keysOf(state);
|
|
195
|
+
if (keys.source === "manifest") {
|
|
196
|
+
return signingKeyIn(keys.keySet, kid);
|
|
197
|
+
}
|
|
198
|
+
if (state.pluginUrl === null) {
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
const response = await fetch(`${new URL(state.pluginUrl).origin}/.well-known/livespace/jwks.json`, { redirect: "error", signal: AbortSignal.timeout(5000) });
|
|
203
|
+
return response.ok ? signingKeyIn(await response.json(), kid) : undefined;
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
/** Livespace caps a plugin's stored config at 64 KiB. */
|
|
211
|
+
export const MAX_SETTINGS_BYTES = 64 * 1024;
|
|
212
|
+
const isConfig = (value) => value === null ||
|
|
213
|
+
(typeof value === "object" &&
|
|
214
|
+
!Array.isArray(value) &&
|
|
215
|
+
Object.values(value).every((v) => ["string", "number", "boolean"].includes(typeof v)));
|
|
216
|
+
async function readBody(request, limit) {
|
|
217
|
+
let size = 0;
|
|
218
|
+
const chunks = [];
|
|
219
|
+
for await (const chunk of request) {
|
|
220
|
+
size += chunk.length;
|
|
221
|
+
if (size > limit) {
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
chunks.push(chunk);
|
|
225
|
+
}
|
|
226
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
227
|
+
}
|
|
228
|
+
const json = (response, status, body) => response
|
|
229
|
+
.writeHead(status, {
|
|
230
|
+
"Content-Type": TYPES[".json"],
|
|
231
|
+
"Cache-Control": "no-store",
|
|
232
|
+
})
|
|
233
|
+
.end(JSON.stringify(body));
|
|
234
|
+
export class PortInUse extends Error {
|
|
235
|
+
port;
|
|
236
|
+
constructor(port) {
|
|
237
|
+
super(`Port ${port} is in use`);
|
|
238
|
+
this.port = port;
|
|
239
|
+
this.name = "PortInUse";
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
export async function startStudio(options) {
|
|
243
|
+
const manifest = await projectManifest(options.dir);
|
|
244
|
+
const plugin = {
|
|
245
|
+
pluginUrl: options.pluginUrl,
|
|
246
|
+
clientId: options.clientId,
|
|
247
|
+
distribution: manifest === null ? "url" : "catalog",
|
|
248
|
+
manifest,
|
|
249
|
+
};
|
|
250
|
+
// The account's address needs the port, known only once listening; requests wait for it.
|
|
251
|
+
let made = () => { };
|
|
252
|
+
const accountMade = new Promise((resolve) => {
|
|
253
|
+
made = resolve;
|
|
254
|
+
});
|
|
255
|
+
let config = null;
|
|
256
|
+
const secrets = new Map();
|
|
257
|
+
const storedSecrets = () => Object.fromEntries([...secrets].map(([key, { kid }]) => [key, { kid }]));
|
|
258
|
+
const server = createServer(async (request, response) => {
|
|
259
|
+
const path = new URL(request.url ?? "/", "http://localhost").pathname;
|
|
260
|
+
// The Livespace services a backend calls, as the dev account.
|
|
261
|
+
const account = await accountMade;
|
|
262
|
+
if (path === KEYS_PATH && request.method === "GET") {
|
|
263
|
+
json(response, 200, account.keySet());
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (path === TOKEN_PATH && request.method === "POST") {
|
|
267
|
+
const text = (await readBody(request, MAX_SETTINGS_BYTES)) ?? "";
|
|
268
|
+
const answer = await account.exchange(new URLSearchParams(text), signingKeysOf(plugin));
|
|
269
|
+
json(response, answer.status, answer.body);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (path === SECRETS_PATH && request.method === "GET") {
|
|
273
|
+
const answer = account.secrets(request.headers.authorization, Object.fromEntries([...secrets].map(([key, { jwe }]) => [key, jwe])));
|
|
274
|
+
json(response, answer.status, answer.body);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
// The page asks for a plugin token for its viewer, as plugin-slot asks Livespace.
|
|
278
|
+
if (path === "/api/session-token" && request.method === "POST") {
|
|
279
|
+
const text = await readBody(request, MAX_SETTINGS_BYTES);
|
|
280
|
+
let sub;
|
|
281
|
+
let expired;
|
|
282
|
+
try {
|
|
283
|
+
;
|
|
284
|
+
({ sub, expired } = JSON.parse(text ?? ""));
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
// Answered below.
|
|
288
|
+
}
|
|
289
|
+
if (typeof sub !== "string" || sub === "") {
|
|
290
|
+
json(response, 401, {
|
|
291
|
+
error: "No viewer: Livespace issues no token without one",
|
|
292
|
+
});
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
json(response, 200, account.sessionToken(sub, { expired: expired === true }));
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
// The failure switch the dev account itself acts on: how the exchange refuses.
|
|
299
|
+
if (path === "/api/failures") {
|
|
300
|
+
if (request.method === "PUT") {
|
|
301
|
+
const text = await readBody(request, MAX_SETTINGS_BYTES);
|
|
302
|
+
let exchange;
|
|
303
|
+
try {
|
|
304
|
+
exchange = JSON.parse(text ?? "").exchange;
|
|
305
|
+
}
|
|
306
|
+
catch {
|
|
307
|
+
// Answered below.
|
|
308
|
+
}
|
|
309
|
+
if (exchange !== null &&
|
|
310
|
+
!EXCHANGE_REFUSALS.includes(exchange)) {
|
|
311
|
+
json(response, 400, {
|
|
312
|
+
error: `exchange is null or one of ${EXCHANGE_REFUSALS.join(", ")}`,
|
|
313
|
+
});
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
account.refusal = exchange;
|
|
317
|
+
}
|
|
318
|
+
if (request.method === "GET" || request.method === "PUT") {
|
|
319
|
+
json(response, 200, { exchange: account.refusal });
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
response.writeHead(405).end();
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
if (path === "/api/plugin") {
|
|
326
|
+
if (request.method === "PUT") {
|
|
327
|
+
const text = await readBody(request, MAX_SETTINGS_BYTES);
|
|
328
|
+
let changes;
|
|
329
|
+
try {
|
|
330
|
+
changes = JSON.parse(text ?? "");
|
|
331
|
+
}
|
|
332
|
+
catch {
|
|
333
|
+
json(response, 400, { error: "Not JSON" });
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const problem = "pluginUrl" in changes
|
|
337
|
+
? pluginUrlProblem(changes.pluginUrl, account.issuer)
|
|
338
|
+
: undefined;
|
|
339
|
+
if (problem !== undefined) {
|
|
340
|
+
json(response, 400, { error: problem });
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
if ("clientId" in changes &&
|
|
344
|
+
(typeof changes.clientId !== "string" ||
|
|
345
|
+
changes.clientId.trim() === "")) {
|
|
346
|
+
json(response, 400, { error: "The client id can't be empty." });
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
if ("distribution" in changes &&
|
|
350
|
+
changes.distribution !== "url" &&
|
|
351
|
+
changes.distribution !== "catalog") {
|
|
352
|
+
json(response, 400, { error: 'distribution is "url" or "catalog".' });
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if ("manifest" in changes &&
|
|
356
|
+
changes.manifest !== null &&
|
|
357
|
+
(typeof changes.manifest !== "object" ||
|
|
358
|
+
Array.isArray(changes.manifest))) {
|
|
359
|
+
json(response, 400, { error: "manifest is a JSON object, or null." });
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
// Field by field: nothing else the page sends lands in the state.
|
|
363
|
+
if ("pluginUrl" in changes) {
|
|
364
|
+
plugin.pluginUrl = changes.pluginUrl;
|
|
365
|
+
}
|
|
366
|
+
if ("clientId" in changes) {
|
|
367
|
+
plugin.clientId = changes.clientId.trim();
|
|
368
|
+
}
|
|
369
|
+
if ("distribution" in changes) {
|
|
370
|
+
plugin.distribution =
|
|
371
|
+
changes.distribution;
|
|
372
|
+
}
|
|
373
|
+
if ("manifest" in changes) {
|
|
374
|
+
plugin.manifest = changes.manifest;
|
|
375
|
+
}
|
|
376
|
+
account.clientId = plugin.clientId;
|
|
377
|
+
}
|
|
378
|
+
if (request.method === "GET" || request.method === "PUT") {
|
|
379
|
+
json(response, 200, plugin);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
response.writeHead(405).end();
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
if (path === "/api/manifest" && request.method === "GET") {
|
|
386
|
+
// Read on every request: the author may rotate keys while the studio runs.
|
|
387
|
+
json(response, 200, { manifest: await projectManifest(options.dir) });
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
if (path === "/api/keys" && request.method === "GET") {
|
|
391
|
+
json(response, 200, keysOf(plugin));
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
if (path === "/api/secrets") {
|
|
395
|
+
if (request.method === "GET") {
|
|
396
|
+
json(response, 200, { stored: storedSecrets() });
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
if (request.method === "PUT") {
|
|
400
|
+
const text = await readBody(request, MAX_SETTINGS_BYTES);
|
|
401
|
+
let changes;
|
|
402
|
+
try {
|
|
403
|
+
changes = JSON.parse(text ?? "").changes;
|
|
404
|
+
}
|
|
405
|
+
catch {
|
|
406
|
+
json(response, 400, { error: "Not JSON" });
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (typeof changes !== "object" || changes === null) {
|
|
410
|
+
json(response, 400, { error: "changes is { key: JWE or null }" });
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const entries = Object.entries(changes);
|
|
414
|
+
for (const [key, value] of entries) {
|
|
415
|
+
if (value !== null && sealedKid(value) === undefined) {
|
|
416
|
+
json(response, 400, {
|
|
417
|
+
error: `${key} isn't a compact JWE sealed with RSA-OAEP-256 and A256GCM`,
|
|
418
|
+
});
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
for (const [key, value] of entries) {
|
|
423
|
+
if (value === null) {
|
|
424
|
+
secrets.delete(key);
|
|
425
|
+
}
|
|
426
|
+
else {
|
|
427
|
+
secrets.set(key, {
|
|
428
|
+
jwe: value,
|
|
429
|
+
kid: sealedKid(value),
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
json(response, 200, { stored: storedSecrets() });
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
response.writeHead(405).end();
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
if (path === "/api/settings") {
|
|
440
|
+
if (request.method === "GET") {
|
|
441
|
+
json(response, 200, { config });
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
if (request.method === "PUT") {
|
|
445
|
+
const text = await readBody(request, MAX_SETTINGS_BYTES);
|
|
446
|
+
if (text === undefined) {
|
|
447
|
+
json(response, 413, {
|
|
448
|
+
error: "Over 64 KiB, which Livespace refuses",
|
|
449
|
+
});
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
let body;
|
|
453
|
+
try {
|
|
454
|
+
body = JSON.parse(text);
|
|
455
|
+
}
|
|
456
|
+
catch {
|
|
457
|
+
json(response, 400, { error: "Not JSON" });
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
const next = body?.config;
|
|
461
|
+
if (!isConfig(next)) {
|
|
462
|
+
json(response, 400, {
|
|
463
|
+
error: "config is an object of strings, numbers and booleans, or null",
|
|
464
|
+
});
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
config = next;
|
|
468
|
+
json(response, 200, { config });
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
response.writeHead(405).end();
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
if (request.method !== "GET" && request.method !== "HEAD") {
|
|
475
|
+
response.writeHead(405).end();
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
const file = fileFor(path);
|
|
479
|
+
if (file === undefined) {
|
|
480
|
+
response.writeHead(404).end();
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
try {
|
|
484
|
+
const body = await readFile(file);
|
|
485
|
+
const extension = file.slice(file.lastIndexOf("."));
|
|
486
|
+
response
|
|
487
|
+
.writeHead(200, {
|
|
488
|
+
"Content-Type": TYPES[extension] ?? "application/octet-stream",
|
|
489
|
+
"Cache-Control": "no-store",
|
|
490
|
+
})
|
|
491
|
+
.end(request.method === "HEAD" ? undefined : body);
|
|
492
|
+
}
|
|
493
|
+
catch {
|
|
494
|
+
response.writeHead(404).end();
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
await new Promise((resolve, reject) => {
|
|
498
|
+
server.once("error", (error) => {
|
|
499
|
+
reject(error.code === "EADDRINUSE" ? new PortInUse(options.port) : error);
|
|
500
|
+
});
|
|
501
|
+
server.listen(options.port, "localhost", () => resolve());
|
|
502
|
+
});
|
|
503
|
+
const address = server.address();
|
|
504
|
+
const port = typeof address === "object" && address !== null
|
|
505
|
+
? address.port
|
|
506
|
+
: options.port;
|
|
507
|
+
const url = `http://localhost:${port}`;
|
|
508
|
+
const account = await DevAccount.create(url, options.clientId);
|
|
509
|
+
made(account);
|
|
510
|
+
return {
|
|
511
|
+
url,
|
|
512
|
+
keysUrl: account.keysUrl,
|
|
513
|
+
authUrl: url,
|
|
514
|
+
close: () => new Promise((resolve) => {
|
|
515
|
+
server.closeAllConnections();
|
|
516
|
+
server.close(() => resolve());
|
|
517
|
+
}),
|
|
518
|
+
};
|
|
519
|
+
}
|