@n6k.io/serve 0.2.5 → 0.3.1
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 +29 -9
- package/package.json +6 -5
- package/src/assets.ts +1 -17
- package/src/bundle.ts +1 -2
- package/src/cli.ts +139 -68
- package/src/logout.test.ts +65 -0
- package/src/logout.ts +35 -0
- package/src/mcp-instructions.ts +1 -1
- package/src/mcp.test.ts +1 -1
- package/src/oidc-grants.ts +220 -0
- package/src/oidc.test.ts +303 -0
- package/src/oidc.ts +120 -0
- package/src/root.tsx +26 -5
- package/src/saved-logins.ts +114 -0
- package/src/server.ts +66 -1
- package/src/token-route.test.ts +87 -0
- package/src/virtual-modules.d.ts +0 -6
package/README.md
CHANGED
|
@@ -4,16 +4,36 @@ Serve `.mdx` pages in the browser with live data from
|
|
|
4
4
|
[n6kd](https://github.com/n6k-io/n6kd) or pykit `n6k database`.
|
|
5
5
|
|
|
6
6
|
```sh
|
|
7
|
-
n6k-serve page.mdx # no data
|
|
8
|
-
n6k-serve page.mdx --data ws://localhost:8080/ws # attached as `db`
|
|
9
|
-
n6k-serve page.mdx --data nyc=ws://localhost:8080/ws --data sales=ws://localhost:7823/ws
|
|
10
|
-
n6k-serve page.mdx --data nyc=ws://<token>@localhost:8080/ws # token sent to the server
|
|
11
|
-
n6k-serve page.mdx --port 3000 # default 3000
|
|
7
|
+
n6k-serve page page.mdx # no data
|
|
8
|
+
n6k-serve page page.mdx --data ws://localhost:8080/ws # attached as `db`
|
|
9
|
+
n6k-serve page page.mdx --data nyc=ws://localhost:8080/ws --data sales=ws://localhost:7823/ws
|
|
10
|
+
n6k-serve page page.mdx --data nyc=ws://<token>@localhost:8080/ws # token sent to the server
|
|
11
|
+
n6k-serve page page.mdx --port 3000 # default 3000
|
|
12
12
|
```
|
|
13
13
|
|
|
14
14
|
A token in `--data` is removed from the URL and sent as the connection token.
|
|
15
15
|
Frontmatter `attach:` URLs may not carry one.
|
|
16
16
|
|
|
17
|
+
```sh
|
|
18
|
+
n6k-serve page page.mdx --data nyc=ws://localhost:8080/ws --oidc-login
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`--oidc-login` signs in to every source without a token, using the server's
|
|
22
|
+
`/.well-known/oauth-protected-resource` and the OAuth device flow (as
|
|
23
|
+
`n6kd auth login`). The refresh token stays in n6k-serve; the page fetches a
|
|
24
|
+
fresh access token on each connect. A source whose server does not offer
|
|
25
|
+
sign-in is an error. The sign-in is saved in the OS credential store and reused
|
|
26
|
+
on the next start.
|
|
27
|
+
|
|
28
|
+
With `--oidc-login`, n6k-serve prints a URL with a per-launch `?secret=`. Open
|
|
29
|
+
that URL: it sets a cookie, and the token route refuses requests without it.
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
n6k-serve logout # list saved sign-ins
|
|
33
|
+
n6k-serve logout ws://localhost:8080 # sign out of the sign-in that server uses
|
|
34
|
+
n6k-serve logout --all # sign out of all
|
|
35
|
+
```
|
|
36
|
+
|
|
17
37
|
Or in the page's frontmatter (`--data` wins for the same alias):
|
|
18
38
|
|
|
19
39
|
```yaml
|
|
@@ -35,11 +55,11 @@ reloads when the file is saved.
|
|
|
35
55
|
## MCP
|
|
36
56
|
|
|
37
57
|
```sh
|
|
38
|
-
claude mcp add n6k-serve -- bunx @n6k.io/serve
|
|
39
|
-
claude mcp add n6k-serve -- bunx @n6k.io/serve
|
|
58
|
+
claude mcp add n6k-serve -- bunx @n6k.io/serve mcp
|
|
59
|
+
claude mcp add n6k-serve -- bunx @n6k.io/serve mcp --port 4000
|
|
40
60
|
```
|
|
41
61
|
|
|
42
|
-
|
|
43
|
-
It takes `--port`, `--host` and `--origin
|
|
62
|
+
`mcp` serves pages at `/<page>/` and is driven over stdio by one MCP client.
|
|
63
|
+
It takes only `--port`, `--host` and `--origin`. Data
|
|
44
64
|
sources and pages are set with MCP tools (see `tools/list`); frontmatter
|
|
45
65
|
`attach:` is ignored. The process exits when stdin closes.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n6k.io/serve",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Serve .mdx pages with live data from n6kd or pykit",
|
|
6
6
|
"repository": "https://github.com/n6k-io/js-serve",
|
|
@@ -26,14 +26,15 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@duckdb/duckdb-wasm": "1.33.1-dev114.0",
|
|
28
28
|
"@modelcontextprotocol/server": "^2.4.0",
|
|
29
|
-
"@n6k.io/build": "^0.
|
|
30
|
-
"@n6k.io/db": "^0.13.
|
|
31
|
-
"@n6k.io/runtime": "^0.1.
|
|
32
|
-
"@n6k.io/ui": "^0.
|
|
29
|
+
"@n6k.io/build": "^0.6.0",
|
|
30
|
+
"@n6k.io/db": "^0.13.3",
|
|
31
|
+
"@n6k.io/runtime": "^0.1.3",
|
|
32
|
+
"@n6k.io/ui": "^0.11.0",
|
|
33
33
|
"@tailwindcss/node": "^4.3.5",
|
|
34
34
|
"@tailwindcss/oxide": "^4.3.5",
|
|
35
35
|
"@tailwindcss/typography": "^0.5.20",
|
|
36
36
|
"@tanstack/react-query": "^5.104.1",
|
|
37
|
+
"commander": "^15.1.0",
|
|
37
38
|
"esbuild": "^0.28.2",
|
|
38
39
|
"radix-ui": "^1.7.0",
|
|
39
40
|
"react": "^19.3.0",
|
package/src/assets.ts
CHANGED
|
@@ -3,15 +3,12 @@ import { wasmDir } from "@n6k.io/db/wasm-dir";
|
|
|
3
3
|
import {
|
|
4
4
|
N6K_DUCKDB_FILES,
|
|
5
5
|
N6K_WASM_MIME,
|
|
6
|
-
N6K_WORKER_DEV_URLS,
|
|
7
|
-
bundleN6kWorker,
|
|
8
6
|
collectN6kWasmFiles,
|
|
9
7
|
findFontFile,
|
|
10
8
|
n6kDuckDBUrl,
|
|
11
9
|
resolveDuckDBDist,
|
|
12
|
-
resolveN6kWorkerSource,
|
|
13
10
|
} from "@n6k.io/build/pipeline";
|
|
14
|
-
import { SERVE_NODE_MODULES
|
|
11
|
+
import { SERVE_NODE_MODULES } from "./paths";
|
|
15
12
|
|
|
16
13
|
const SAME_ORIGIN = { "Cross-Origin-Resource-Policy": "same-origin" };
|
|
17
14
|
|
|
@@ -50,19 +47,6 @@ export async function serveAsset(
|
|
|
50
47
|
});
|
|
51
48
|
}
|
|
52
49
|
}
|
|
53
|
-
const workerRel = N6K_WORKER_DEV_URLS[pathname];
|
|
54
|
-
if (workerRel) {
|
|
55
|
-
const code = await bundleN6kWorker(
|
|
56
|
-
resolveN6kWorkerSource(workerRel, SERVE_ROOT),
|
|
57
|
-
);
|
|
58
|
-
return new Response(code, {
|
|
59
|
-
headers: {
|
|
60
|
-
"Content-Type": "text/javascript",
|
|
61
|
-
"Cache-Control": "no-store",
|
|
62
|
-
...SAME_ORIGIN,
|
|
63
|
-
},
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
50
|
return undefined;
|
|
67
51
|
}
|
|
68
52
|
|
package/src/bundle.ts
CHANGED
|
@@ -2,7 +2,6 @@ import path from "node:path";
|
|
|
2
2
|
import * as esbuild from "esbuild";
|
|
3
3
|
import {
|
|
4
4
|
n6kDuckDBPlugin,
|
|
5
|
-
n6kWorkersPlugin,
|
|
6
5
|
preprocess,
|
|
7
6
|
virtualModulesPlugin,
|
|
8
7
|
} from "@n6k.io/build/pipeline";
|
|
@@ -19,6 +18,7 @@ export type Bundle = Map<string, string>;
|
|
|
19
18
|
export type RootProps = {
|
|
20
19
|
data: Record<string, string>;
|
|
21
20
|
tokens: Record<string, string>;
|
|
21
|
+
oidc?: string[];
|
|
22
22
|
bare?: boolean;
|
|
23
23
|
};
|
|
24
24
|
|
|
@@ -57,7 +57,6 @@ export async function bundlePage(
|
|
|
57
57
|
logLevel: "silent",
|
|
58
58
|
plugins: [
|
|
59
59
|
virtualModulesPlugin(modules, mdxDir),
|
|
60
|
-
n6kWorkersPlugin(undefined, { used: false }),
|
|
61
60
|
n6kDuckDBPlugin(undefined, { used: false }),
|
|
62
61
|
],
|
|
63
62
|
alias: {
|
package/src/cli.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
-
import {
|
|
4
|
+
import { Command } from "commander";
|
|
5
5
|
import { parseAttachUrls } from "@n6k.io/build/pipeline";
|
|
6
6
|
import { DataArgError, resolveData, type DataSource } from "./data-args";
|
|
7
7
|
import { bundlePage } from "./bundle";
|
|
@@ -10,6 +10,7 @@ import { hasWasmExtensions } from "./assets";
|
|
|
10
10
|
import { parsePages, type PagesConfig } from "./pages";
|
|
11
11
|
import { PagesSite } from "./pages-site";
|
|
12
12
|
import {
|
|
13
|
+
SECRET_PARAM,
|
|
13
14
|
reloadPage,
|
|
14
15
|
startServer,
|
|
15
16
|
type PageState,
|
|
@@ -19,89 +20,134 @@ import {
|
|
|
19
20
|
} from "./server";
|
|
20
21
|
import { watchFile } from "./watch";
|
|
21
22
|
import { McpSite, startMcp } from "./mcp";
|
|
23
|
+
import { OidcError, OidcLogins, type OidcSession } from "./oidc";
|
|
24
|
+
import { LogoutError, logout } from "./logout";
|
|
22
25
|
import { SRC_DIR } from "./paths";
|
|
23
26
|
import pkg from "../package.json";
|
|
24
27
|
|
|
25
|
-
const USAGE = `usage: n6k-serve <page.mdx> [--data [alias=]ws://…]... [options]
|
|
26
|
-
n6k-serve --pages <pages.yml> [options]
|
|
27
|
-
n6k-serve sql [--console] [--data [alias=]ws://…]... [options]
|
|
28
|
-
n6k-serve --mcp [options]
|
|
29
|
-
options: [--port 3000] [--host 127.0.0.1] [--origin <origin>]...`;
|
|
30
|
-
|
|
31
28
|
function fail(message: string): never {
|
|
32
29
|
console.error(message);
|
|
33
30
|
process.exit(1);
|
|
34
31
|
}
|
|
35
32
|
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
port
|
|
44
|
-
host
|
|
45
|
-
origin
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
origins:
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
if (values.console && positionals[0] !== "sql") fail(USAGE);
|
|
64
|
-
|
|
65
|
-
if (positionals[0] === "sql") {
|
|
66
|
-
if (positionals.length !== 1 || values.mcp || values.pages !== undefined)
|
|
67
|
-
fail(USAGE);
|
|
68
|
-
await servePage(
|
|
69
|
-
path.join(SRC_DIR, "sql", values.console ? "console.mdx" : "editor.mdx"),
|
|
70
|
-
true,
|
|
71
|
-
);
|
|
72
|
-
} else if (values.mcp) {
|
|
73
|
-
if (
|
|
74
|
-
positionals.length > 0 ||
|
|
75
|
-
values.pages !== undefined ||
|
|
76
|
-
values.data.length > 0
|
|
77
|
-
)
|
|
78
|
-
fail(USAGE);
|
|
79
|
-
serveMcp();
|
|
80
|
-
} else if (values.pages === undefined) {
|
|
81
|
-
if (positionals.length !== 1) fail(USAGE);
|
|
82
|
-
await servePage(positionals[0]);
|
|
83
|
-
} else {
|
|
84
|
-
if (positionals.length > 0 || values.data.length > 0) fail(USAGE);
|
|
85
|
-
servePages(values.pages);
|
|
33
|
+
const collect = (value: string, acc: string[]): string[] => [...acc, value];
|
|
34
|
+
|
|
35
|
+
type ServerFlags = { port: string; host: string; origin: string[] };
|
|
36
|
+
type DataFlags = { data: string[]; oidcLogin: boolean };
|
|
37
|
+
|
|
38
|
+
function withServerFlags(cmd: Command): Command {
|
|
39
|
+
return cmd
|
|
40
|
+
.option("--port <port>", "port to listen on", "3000")
|
|
41
|
+
.option("--host <host>", "host to bind", "127.0.0.1")
|
|
42
|
+
.option("--origin <origin>", "allowed origin (repeatable)", collect, []);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function withDataFlags(cmd: Command): Command {
|
|
46
|
+
return cmd
|
|
47
|
+
.option("--data <[alias=]ws://…>", "data source (repeatable)", collect, [])
|
|
48
|
+
.option("--oidc-login", "sign in to data sources with OIDC", false);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function serveOptions(flags: ServerFlags): ServeOptions {
|
|
52
|
+
const port = Number(flags.port);
|
|
53
|
+
if (!Number.isInteger(port) || port < 0)
|
|
54
|
+
fail(`--port ${flags.port}: not a port`);
|
|
55
|
+
if (!hasWasmExtensions())
|
|
56
|
+
fail("n6k duckdb wasm extensions missing from @n6k.io/db");
|
|
57
|
+
return { hostname: flags.host, port, origins: flags.origin };
|
|
86
58
|
}
|
|
87
59
|
|
|
88
|
-
|
|
60
|
+
const program = new Command("n6k-serve")
|
|
61
|
+
.description(pkg.description)
|
|
62
|
+
.version(pkg.version);
|
|
63
|
+
|
|
64
|
+
withDataFlags(withServerFlags(program.command("page")))
|
|
65
|
+
.description("serve one .mdx page")
|
|
66
|
+
.argument("<page.mdx>")
|
|
67
|
+
.action(async (arg: string, flags: ServerFlags & DataFlags) => {
|
|
68
|
+
await servePage(arg, flags, serveOptions(flags));
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
withServerFlags(program.command("site"))
|
|
72
|
+
.description("serve the pages listed in a pages.yml")
|
|
73
|
+
.argument("<pages.yml>")
|
|
74
|
+
.action((arg: string, flags: ServerFlags) => {
|
|
75
|
+
servePages(arg, serveOptions(flags));
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
withDataFlags(withServerFlags(program.command("sql")))
|
|
79
|
+
.description("serve the SQL editor")
|
|
80
|
+
.option("--console", "serve the SQL console instead of the editor", false)
|
|
81
|
+
.action(async (flags: ServerFlags & DataFlags & { console: boolean }) => {
|
|
82
|
+
await servePage(
|
|
83
|
+
path.join(SRC_DIR, "sql", flags.console ? "console.mdx" : "editor.mdx"),
|
|
84
|
+
flags,
|
|
85
|
+
serveOptions(flags),
|
|
86
|
+
true,
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
withServerFlags(program.command("mcp"))
|
|
91
|
+
.description("serve pages driven by an MCP client over stdio")
|
|
92
|
+
.action((flags: ServerFlags) => {
|
|
93
|
+
serveMcp(serveOptions(flags));
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
program
|
|
97
|
+
.command("logout")
|
|
98
|
+
.description("list saved sign-ins, or sign out")
|
|
99
|
+
.argument("[url]", "ws://host[:port]")
|
|
100
|
+
.option("--all", "sign out of all", false)
|
|
101
|
+
.action(async (url: string | undefined, flags: { all: boolean }) => {
|
|
102
|
+
if (url !== undefined && flags.all)
|
|
103
|
+
fail("logout: give a URL or --all, not both");
|
|
104
|
+
try {
|
|
105
|
+
for (const out of await logout(url, flags.all)) console.error(out);
|
|
106
|
+
} catch (err) {
|
|
107
|
+
if (err instanceof LogoutError) fail(err.message);
|
|
108
|
+
throw err;
|
|
109
|
+
}
|
|
110
|
+
process.exit(0);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
await program.parseAsync();
|
|
114
|
+
|
|
115
|
+
async function servePage(
|
|
116
|
+
arg: string,
|
|
117
|
+
flags: DataFlags,
|
|
118
|
+
options: ServeOptions,
|
|
119
|
+
bare = false,
|
|
120
|
+
): Promise<void> {
|
|
89
121
|
const mdxPath = path.resolve(arg);
|
|
90
122
|
if (!mdxPath.endsWith(".mdx")) fail(`${arg}: not an .mdx file`);
|
|
91
123
|
if (!existsSync(mdxPath)) fail(`${arg}: no such file`);
|
|
92
|
-
const dataFlags =
|
|
124
|
+
const dataFlags = flags.data;
|
|
125
|
+
const logins = flags.oidcLogin ? new OidcLogins() : undefined;
|
|
126
|
+
|
|
127
|
+
const login = async (
|
|
128
|
+
sources: Record<string, DataSource>,
|
|
129
|
+
): Promise<Map<string, OidcSession>> => {
|
|
130
|
+
const out = new Map<string, OidcSession>();
|
|
131
|
+
if (!logins) return out;
|
|
132
|
+
for (const [alias, { url, token }] of Object.entries(sources)) {
|
|
133
|
+
if (token === "") out.set(alias, await logins.sessionFor(url));
|
|
134
|
+
}
|
|
135
|
+
return out;
|
|
136
|
+
};
|
|
93
137
|
|
|
94
138
|
const resolveDataFor = (): Record<string, DataSource> =>
|
|
95
139
|
resolveData(parseAttachUrls(readFileSync(mdxPath, "utf8")), dataFlags);
|
|
96
140
|
|
|
97
141
|
const build = async (
|
|
98
142
|
sources: Record<string, DataSource>,
|
|
143
|
+
oidc: string[],
|
|
99
144
|
): Promise<PageState> => {
|
|
100
145
|
const entries = Object.entries(sources);
|
|
101
146
|
const [bundle, css] = await Promise.all([
|
|
102
147
|
bundlePage(mdxPath, {
|
|
103
148
|
data: Object.fromEntries(entries.map(([a, s]) => [a, s.url])),
|
|
104
149
|
tokens: Object.fromEntries(entries.map(([a, s]) => [a, s.token])),
|
|
150
|
+
oidc,
|
|
105
151
|
bare,
|
|
106
152
|
}),
|
|
107
153
|
buildCss(mdxPath),
|
|
@@ -110,13 +156,16 @@ async function servePage(arg: string, bare = false): Promise<void> {
|
|
|
110
156
|
};
|
|
111
157
|
|
|
112
158
|
let data: Record<string, DataSource>;
|
|
159
|
+
let sessions: Map<string, OidcSession>;
|
|
113
160
|
try {
|
|
114
161
|
data = resolveDataFor();
|
|
162
|
+
sessions = await login(data);
|
|
115
163
|
} catch (err) {
|
|
116
|
-
if (err instanceof DataArgError
|
|
164
|
+
if (err instanceof DataArgError || err instanceof OidcError)
|
|
165
|
+
fail(err.message);
|
|
117
166
|
throw err;
|
|
118
167
|
}
|
|
119
|
-
let state = await build(data);
|
|
168
|
+
let state = await build(data, [...sessions.keys()]);
|
|
120
169
|
const page: ServedPage = {
|
|
121
170
|
title: path.basename(mdxPath),
|
|
122
171
|
key: "",
|
|
@@ -126,18 +175,40 @@ async function servePage(arg: string, bare = false): Promise<void> {
|
|
|
126
175
|
resolve: (pathname) => ({ kind: "page", page, rest: pathname }),
|
|
127
176
|
page: (key) => (key === "" ? page : undefined),
|
|
128
177
|
};
|
|
129
|
-
const
|
|
178
|
+
const secret = logins
|
|
179
|
+
? Buffer.from(crypto.getRandomValues(new Uint8Array(32))).toString(
|
|
180
|
+
"base64url",
|
|
181
|
+
)
|
|
182
|
+
: undefined;
|
|
183
|
+
const server = startServer(site, {
|
|
184
|
+
...options,
|
|
185
|
+
token: (alias) => sessions.get(alias)?.token(),
|
|
186
|
+
secret,
|
|
187
|
+
});
|
|
130
188
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
189
|
+
const url = new URL(server.url);
|
|
190
|
+
if (secret) url.searchParams.set(SECRET_PARAM, secret);
|
|
191
|
+
console.error(`n6k-serve ${url}`);
|
|
192
|
+
const flagged = new Set(Object.keys(resolveData({}, dataFlags)));
|
|
193
|
+
const rows = Object.entries(data).map(([alias, { url }]) => {
|
|
194
|
+
const tags = [flagged.has(alias) ? "--data" : "attach:"];
|
|
195
|
+
if (sessions.has(alias)) tags.push("oidc");
|
|
196
|
+
return [`${alias}=${url}`, `(${tags.join(", ")})`];
|
|
197
|
+
});
|
|
198
|
+
if (rows.length > 0) {
|
|
199
|
+
const width = Math.max(...rows.map(([entry]) => entry!.length));
|
|
200
|
+
console.error("\ndata:");
|
|
201
|
+
for (const [entry, tags] of rows)
|
|
202
|
+
console.error(` ${entry!.padEnd(width)} ${tags}`);
|
|
134
203
|
}
|
|
135
204
|
|
|
136
205
|
watchFile(mdxPath, async () => {
|
|
137
206
|
try {
|
|
138
207
|
const nextData = resolveDataFor();
|
|
139
|
-
|
|
208
|
+
const nextSessions = await login(nextData);
|
|
209
|
+
state = await build(nextData, [...nextSessions.keys()]);
|
|
140
210
|
data = nextData;
|
|
211
|
+
sessions = nextSessions;
|
|
141
212
|
reloadPage(server, "");
|
|
142
213
|
console.error(`rebuilt ${path.basename(mdxPath)}`);
|
|
143
214
|
} catch (err) {
|
|
@@ -146,7 +217,7 @@ async function servePage(arg: string, bare = false): Promise<void> {
|
|
|
146
217
|
});
|
|
147
218
|
}
|
|
148
219
|
|
|
149
|
-
function servePages(arg: string): void {
|
|
220
|
+
function servePages(arg: string, options: ServeOptions): void {
|
|
150
221
|
const file = path.resolve(arg);
|
|
151
222
|
const load = (): PagesConfig => parsePages(readFileSync(file, "utf8"), file);
|
|
152
223
|
|
|
@@ -173,7 +244,7 @@ function servePages(arg: string): void {
|
|
|
173
244
|
});
|
|
174
245
|
}
|
|
175
246
|
|
|
176
|
-
function serveMcp(): void {
|
|
247
|
+
function serveMcp(options: ServeOptions): void {
|
|
177
248
|
const site = new McpSite((key) => reloadPage(server, key));
|
|
178
249
|
const server = startServer(site, options);
|
|
179
250
|
console.error(`n6k-serve ${server.url}`);
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { LogoutError, logout } from "./logout";
|
|
3
|
+
import { SavedLogins, type RefreshStore } from "./saved-logins";
|
|
4
|
+
|
|
5
|
+
async function seeded() {
|
|
6
|
+
const saved = new Map<string, string>();
|
|
7
|
+
const store: RefreshStore = {
|
|
8
|
+
get: async (name) => saved.get(name) ?? null,
|
|
9
|
+
set: async (name, value) => {
|
|
10
|
+
saved.set(name, value);
|
|
11
|
+
},
|
|
12
|
+
delete: async (name) => saved.delete(name),
|
|
13
|
+
};
|
|
14
|
+
const logins = new SavedLogins(store);
|
|
15
|
+
await logins.setRefreshToken("http://kc/realms/a cli", "ra");
|
|
16
|
+
await logins.addServer("http://kc/realms/a cli", "ws://h1:8080");
|
|
17
|
+
await logins.addServer("http://kc/realms/a cli", "ws://h2:8080");
|
|
18
|
+
await logins.setRefreshToken("http://kc/realms/b cli", "rb");
|
|
19
|
+
await logins.addServer("http://kc/realms/b cli", "wss://h3");
|
|
20
|
+
return { saved, logins };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
test("no server lists sign-ins and deletes nothing", async () => {
|
|
24
|
+
const { saved, logins } = await seeded();
|
|
25
|
+
expect(await logout(undefined, false, logins)).toEqual([
|
|
26
|
+
"saved sign-ins:",
|
|
27
|
+
" http://kc/realms/a (client cli): ws://h1:8080, ws://h2:8080",
|
|
28
|
+
" http://kc/realms/b (client cli): wss://h3",
|
|
29
|
+
"",
|
|
30
|
+
"sign out with: n6k-serve logout ws://host[:port] | --all",
|
|
31
|
+
]);
|
|
32
|
+
expect(saved.get("http://kc/realms/a cli")).toBe("ra");
|
|
33
|
+
expect(saved.get("http://kc/realms/b cli")).toBe("rb");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("a server removes only the sign-in it uses", async () => {
|
|
37
|
+
const { saved, logins } = await seeded();
|
|
38
|
+
expect(await logout("ws://h1:8080/ws", false, logins)).toEqual([
|
|
39
|
+
"signed out of:",
|
|
40
|
+
" http://kc/realms/a (client cli): ws://h1:8080, ws://h2:8080",
|
|
41
|
+
]);
|
|
42
|
+
expect(saved.has("http://kc/realms/a cli")).toBe(false);
|
|
43
|
+
expect(saved.get("http://kc/realms/b cli")).toBe("rb");
|
|
44
|
+
const left = await logins.list();
|
|
45
|
+
expect(left.map((l) => l.issuer)).toEqual(["http://kc/realms/b"]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("an unknown server or bad URL is an error", async () => {
|
|
49
|
+
const { logins } = await seeded();
|
|
50
|
+
await expect(logout("ws://other/ws", false, logins)).rejects.toThrow(
|
|
51
|
+
new LogoutError("no saved sign-in for ws://other"),
|
|
52
|
+
);
|
|
53
|
+
await expect(logout("http://h1:8080", false, logins)).rejects.toThrow(
|
|
54
|
+
LogoutError,
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("--all removes every sign-in and the index", async () => {
|
|
59
|
+
const { saved, logins } = await seeded();
|
|
60
|
+
const out = await logout(undefined, true, logins);
|
|
61
|
+
expect(out[0]).toBe("signed out of:");
|
|
62
|
+
expect(out).toHaveLength(3);
|
|
63
|
+
expect(saved.size).toBe(0);
|
|
64
|
+
expect(await logout(undefined, false, logins)).toEqual(["no saved sign-ins"]);
|
|
65
|
+
});
|
package/src/logout.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { SavedLogins, serverOf, type SavedLogin } from "./saved-logins";
|
|
2
|
+
|
|
3
|
+
export class LogoutError extends Error {}
|
|
4
|
+
|
|
5
|
+
function line({ issuer, clientId, servers }: SavedLogin): string {
|
|
6
|
+
return ` ${issuer} (client ${clientId}): ${servers.join(", ")}`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export async function logout(
|
|
10
|
+
server: string | undefined,
|
|
11
|
+
all: boolean,
|
|
12
|
+
saved: SavedLogins = new SavedLogins(),
|
|
13
|
+
): Promise<string[]> {
|
|
14
|
+
if (all) {
|
|
15
|
+
const removed = await saved.removeAll();
|
|
16
|
+
if (removed.length === 0) return ["no saved sign-ins"];
|
|
17
|
+
return ["signed out of:", ...removed.map((l) => line(l))];
|
|
18
|
+
}
|
|
19
|
+
if (server === undefined) {
|
|
20
|
+
const logins = await saved.list();
|
|
21
|
+
if (logins.length === 0) return ["no saved sign-ins"];
|
|
22
|
+
return [
|
|
23
|
+
"saved sign-ins:",
|
|
24
|
+
...logins.map((l) => line(l)),
|
|
25
|
+
"",
|
|
26
|
+
"sign out with: n6k-serve logout ws://host[:port] | --all",
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
if (!/^wss?:\/\//.test(server) || !URL.canParse(server))
|
|
30
|
+
throw new LogoutError(`${server}: not a ws:// or wss:// URL`);
|
|
31
|
+
const removed = await saved.removeForServer(serverOf(server));
|
|
32
|
+
if (!removed)
|
|
33
|
+
throw new LogoutError(`no saved sign-in for ${serverOf(server)}`);
|
|
34
|
+
return ["signed out of:", line(removed)];
|
|
35
|
+
}
|
package/src/mcp-instructions.ts
CHANGED
|
@@ -19,4 +19,4 @@ Controls
|
|
|
19
19
|
<SelectContent><SelectItem value="2020-09">Sep 2020</SelectItem></SelectContent>
|
|
20
20
|
:::
|
|
21
21
|
- The bracketed name is a variable usable in expressions, e.g. filters={{ category }} or query={\`SELECT … LIMIT \${sims}\`}.
|
|
22
|
-
- Attributes are strings coerced to numbers/booleans; \`options\` and \`default\`
|
|
22
|
+
- Attributes are strings coerced to numbers/booleans; \`options\` and \`default\` containing a comma become lists; an option "value:label" sets a separate label.`;
|
package/src/mcp.test.ts
CHANGED
|
@@ -16,7 +16,7 @@ type Reply = {
|
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
function spawnMcp(args: string[] = []) {
|
|
19
|
-
const proc = Bun.spawn(["bun", CLI, "
|
|
19
|
+
const proc = Bun.spawn(["bun", CLI, "mcp", "--port", "0", ...args], {
|
|
20
20
|
stdin: "pipe",
|
|
21
21
|
stdout: "pipe",
|
|
22
22
|
stderr: "pipe",
|