@n6k.io/serve 0.1.0 → 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/package.json +2 -1
- package/src/bundle.ts +8 -2
- package/src/cli.ts +98 -34
- package/src/css.ts +3 -2
- package/src/data-args.ts +1 -1
- package/src/pages-site.ts +121 -0
- package/src/pages.test.ts +91 -0
- package/src/pages.ts +128 -0
- package/src/root.tsx +10 -6
- package/src/server.ts +109 -45
- package/src/watch.ts +10 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n6k.io/serve",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Serve a single .mdx page with live data from n6kd or pykit",
|
|
6
6
|
"repository": "https://github.com/n6k-io/js-serve",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
16
|
"test": "bun test",
|
|
17
|
+
"tags": "bun scripts/tag-release.ts",
|
|
17
18
|
"typecheck": "tsc --noEmit",
|
|
18
19
|
"lint": "eslint .",
|
|
19
20
|
"fmt": "prettier --write --log-level=warn .",
|
package/src/bundle.ts
CHANGED
|
@@ -15,16 +15,22 @@ import {
|
|
|
15
15
|
|
|
16
16
|
export type Bundle = Map<string, string>;
|
|
17
17
|
|
|
18
|
+
export type RootProps = {
|
|
19
|
+
aliases: string[];
|
|
20
|
+
dataPath: string;
|
|
21
|
+
token: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
18
24
|
export async function bundlePage(
|
|
19
25
|
mdxPath: string,
|
|
20
|
-
|
|
26
|
+
rootProps: RootProps,
|
|
21
27
|
): Promise<Bundle> {
|
|
22
28
|
const mdxDir = path.dirname(mdxPath);
|
|
23
29
|
const { entries, modules } = await preprocess(
|
|
24
30
|
{ index: mdxPath },
|
|
25
31
|
{
|
|
26
32
|
rootComponent: path.join(SRC_DIR, "root.tsx"),
|
|
27
|
-
rootProps
|
|
33
|
+
rootProps,
|
|
28
34
|
mdxComponents: path.join(SRC_DIR, "mdx-components.tsx"),
|
|
29
35
|
},
|
|
30
36
|
);
|
package/src/cli.ts
CHANGED
|
@@ -7,11 +7,21 @@ import { DataArgError, resolveData } from "./data-args";
|
|
|
7
7
|
import { bundlePage } from "./bundle";
|
|
8
8
|
import { buildCss } from "./css";
|
|
9
9
|
import { hasWasmExtensions } from "./assets";
|
|
10
|
-
import {
|
|
10
|
+
import { parsePages, type PagesConfig } from "./pages";
|
|
11
|
+
import { PagesSite } from "./pages-site";
|
|
12
|
+
import {
|
|
13
|
+
reloadPage,
|
|
14
|
+
startServer,
|
|
15
|
+
type PageState,
|
|
16
|
+
type ServeOptions,
|
|
17
|
+
type ServedPage,
|
|
18
|
+
type Site,
|
|
19
|
+
} from "./server";
|
|
11
20
|
import { watchFile } from "./watch";
|
|
12
21
|
|
|
13
|
-
const USAGE =
|
|
14
|
-
|
|
22
|
+
const USAGE = `usage: n6k-serve <page.mdx> [--data [alias=]ws://…]... [options]
|
|
23
|
+
n6k-serve --pages <pages.yml> [options]
|
|
24
|
+
options: [--port 3000] [--host 127.0.0.1] [--origin <origin>]...`;
|
|
15
25
|
|
|
16
26
|
function fail(message: string): never {
|
|
17
27
|
console.error(message);
|
|
@@ -22,59 +32,113 @@ const { values, positionals } = parseArgs({
|
|
|
22
32
|
args: process.argv.slice(2),
|
|
23
33
|
options: {
|
|
24
34
|
data: { type: "string", multiple: true, default: [] },
|
|
35
|
+
pages: { type: "string" },
|
|
25
36
|
port: { type: "string", default: "3000" },
|
|
37
|
+
host: { type: "string", default: "127.0.0.1" },
|
|
38
|
+
origin: { type: "string", multiple: true, default: [] },
|
|
26
39
|
},
|
|
27
40
|
allowPositionals: true,
|
|
28
41
|
strict: true,
|
|
29
42
|
});
|
|
30
43
|
|
|
31
|
-
if (positionals.length !== 1) fail(USAGE);
|
|
32
|
-
const mdxPath = path.resolve(positionals[0]);
|
|
33
|
-
if (!mdxPath.endsWith(".mdx")) fail(`${positionals[0]}: not an .mdx file`);
|
|
34
|
-
if (!existsSync(mdxPath)) fail(`${positionals[0]}: no such file`);
|
|
35
44
|
const port = Number(values.port);
|
|
36
45
|
if (!Number.isInteger(port) || port < 0)
|
|
37
46
|
fail(`--port ${values.port}: not a port`);
|
|
38
47
|
if (!hasWasmExtensions())
|
|
39
48
|
fail("n6k duckdb wasm extensions missing from @n6k.io/db");
|
|
40
49
|
|
|
41
|
-
const
|
|
50
|
+
const options: ServeOptions = {
|
|
51
|
+
hostname: values.host,
|
|
52
|
+
port,
|
|
53
|
+
origins: values.origin,
|
|
54
|
+
};
|
|
42
55
|
|
|
43
|
-
|
|
56
|
+
if (values.pages === undefined) {
|
|
57
|
+
if (positionals.length !== 1) fail(USAGE);
|
|
58
|
+
await servePage(positionals[0]);
|
|
59
|
+
} else {
|
|
60
|
+
if (positionals.length > 0 || values.data.length > 0) fail(USAGE);
|
|
61
|
+
servePages(values.pages);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function servePage(arg: string): Promise<void> {
|
|
65
|
+
const mdxPath = path.resolve(arg);
|
|
66
|
+
if (!mdxPath.endsWith(".mdx")) fail(`${arg}: not an .mdx file`);
|
|
67
|
+
if (!existsSync(mdxPath)) fail(`${arg}: no such file`);
|
|
68
|
+
const dataFlags = values.data;
|
|
69
|
+
|
|
70
|
+
const resolveDataFor = (): Record<string, string> =>
|
|
71
|
+
resolveData(parseAttachUrls(readFileSync(mdxPath, "utf8")), dataFlags);
|
|
72
|
+
|
|
73
|
+
const build = async (aliases: string[]): Promise<PageState> => {
|
|
74
|
+
const [bundle, css] = await Promise.all([
|
|
75
|
+
bundlePage(mdxPath, { aliases, dataPath: "/_n6k/data/", token: "" }),
|
|
76
|
+
buildCss(mdxPath),
|
|
77
|
+
]);
|
|
78
|
+
return { bundle, css };
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
let data: Record<string, string>;
|
|
44
82
|
try {
|
|
45
|
-
|
|
83
|
+
data = resolveDataFor();
|
|
46
84
|
} catch (err) {
|
|
47
85
|
if (err instanceof DataArgError) fail(err.message);
|
|
48
86
|
throw err;
|
|
49
87
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
88
|
+
let state = await build(Object.keys(data));
|
|
89
|
+
const page: ServedPage = {
|
|
90
|
+
title: path.basename(mdxPath),
|
|
91
|
+
key: "",
|
|
92
|
+
state: () => Promise.resolve(state),
|
|
93
|
+
};
|
|
94
|
+
const site: Site = {
|
|
95
|
+
resolve: (pathname) => ({ kind: "page", page, rest: pathname }),
|
|
96
|
+
page: (key) => (key === "" ? page : undefined),
|
|
97
|
+
upstream: (alias) => data[alias],
|
|
98
|
+
};
|
|
99
|
+
const server = startServer(site, options);
|
|
59
100
|
|
|
60
|
-
|
|
61
|
-
const
|
|
101
|
+
console.log(`n6k-serve ${server.url}`);
|
|
102
|
+
for (const [alias, url] of Object.entries(data)) {
|
|
103
|
+
console.log(` ${alias} → ${url}`);
|
|
104
|
+
}
|
|
62
105
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
106
|
+
watchFile(mdxPath, async () => {
|
|
107
|
+
try {
|
|
108
|
+
const nextData = resolveDataFor();
|
|
109
|
+
state = await build(Object.keys(nextData));
|
|
110
|
+
data = nextData;
|
|
111
|
+
reloadPage(server, "");
|
|
112
|
+
console.log(`rebuilt ${path.basename(mdxPath)}`);
|
|
113
|
+
} catch (err) {
|
|
114
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
115
|
+
}
|
|
116
|
+
});
|
|
66
117
|
}
|
|
67
118
|
|
|
68
|
-
|
|
119
|
+
function servePages(arg: string): void {
|
|
120
|
+
const file = path.resolve(arg);
|
|
121
|
+
const load = (): PagesConfig => parsePages(readFileSync(file, "utf8"), file);
|
|
122
|
+
|
|
123
|
+
let config: PagesConfig;
|
|
69
124
|
try {
|
|
70
|
-
|
|
71
|
-
parseAttachUrls(readFileSync(mdxPath, "utf8")),
|
|
72
|
-
dataFlags,
|
|
73
|
-
);
|
|
74
|
-
Object.assign(state, await build(data));
|
|
75
|
-
reloadClients(server);
|
|
76
|
-
console.log(`rebuilt ${path.basename(mdxPath)}`);
|
|
125
|
+
config = load();
|
|
77
126
|
} catch (err) {
|
|
78
|
-
|
|
127
|
+
if (err instanceof Error) fail(err.message);
|
|
128
|
+
throw err;
|
|
79
129
|
}
|
|
80
|
-
|
|
130
|
+
|
|
131
|
+
const site = new PagesSite((key) => reloadPage(server, key));
|
|
132
|
+
site.update(config);
|
|
133
|
+
const server = startServer(site, options);
|
|
134
|
+
console.log(`n6k-serve ${server.url}`);
|
|
135
|
+
|
|
136
|
+
watchFile(file, () => {
|
|
137
|
+
try {
|
|
138
|
+
site.update(load());
|
|
139
|
+
console.log(`reloaded ${path.basename(file)}`);
|
|
140
|
+
} catch (err) {
|
|
141
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
package/src/css.ts
CHANGED
|
@@ -8,8 +8,9 @@ const TAILWIND = path.join(SERVE_NODE_MODULES, ".bin", "tailwindcss");
|
|
|
8
8
|
|
|
9
9
|
export async function buildCss(mdxPath: string): Promise<string> {
|
|
10
10
|
mkdirSync(CACHE_DIR, { recursive: true });
|
|
11
|
-
const
|
|
12
|
-
const
|
|
11
|
+
const stem = `${process.pid}.${Bun.hash(mdxPath).toString(36)}`;
|
|
12
|
+
const input = path.join(CACHE_DIR, `${stem}.in.css`);
|
|
13
|
+
const output = path.join(CACHE_DIR, `${stem}.out.css`);
|
|
13
14
|
const base = readFileSync(path.join(SRC_DIR, "globals.css"), "utf8");
|
|
14
15
|
await Bun.write(
|
|
15
16
|
input,
|
package/src/data-args.ts
CHANGED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { bundlePage } from "./bundle";
|
|
3
|
+
import { buildCss } from "./css";
|
|
4
|
+
import { diffPages, type PagesConfig, type PageRef } from "./pages";
|
|
5
|
+
import type { PageState, ServedPage, Site } from "./server";
|
|
6
|
+
import { watchFile } from "./watch";
|
|
7
|
+
|
|
8
|
+
type Entry = {
|
|
9
|
+
served: ServedPage;
|
|
10
|
+
unwatch: () => void;
|
|
11
|
+
cached?: Promise<PageState>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
function keyOf({ app, page }: PageRef): string {
|
|
15
|
+
return `${app}/${page}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function listing(config: PagesConfig): string {
|
|
19
|
+
const items = Object.entries(config).flatMap(([app, { pages }]) =>
|
|
20
|
+
Object.keys(pages).map((page) => {
|
|
21
|
+
const href = `/${app}/${page}/`;
|
|
22
|
+
return `<li><a href="${href}">${Bun.escapeHTML(`${app}/${page}`)}</a></li>`;
|
|
23
|
+
}),
|
|
24
|
+
);
|
|
25
|
+
return `<!doctype html>
|
|
26
|
+
<html lang="en">
|
|
27
|
+
<head><meta charset="utf-8" /><title>n6k-serve</title></head>
|
|
28
|
+
<body><ul>${items.join("")}</ul></body>
|
|
29
|
+
</html>`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class PagesSite implements Site {
|
|
33
|
+
private config: PagesConfig = {};
|
|
34
|
+
private readonly entries = new Map<string, Entry>();
|
|
35
|
+
|
|
36
|
+
constructor(private readonly onChange: (key: string) => void) {}
|
|
37
|
+
|
|
38
|
+
update(next: PagesConfig): void {
|
|
39
|
+
const prev = this.config;
|
|
40
|
+
this.config = next;
|
|
41
|
+
const diff = diffPages(prev, next);
|
|
42
|
+
for (const ref of [...diff.removed, ...diff.changed]) this.drop(ref);
|
|
43
|
+
for (const ref of [...diff.added, ...diff.changed]) this.add(ref);
|
|
44
|
+
for (const ref of diff.changed) this.onChange(keyOf(ref));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
resolve: Site["resolve"] = (pathname) => {
|
|
48
|
+
if (pathname === "/") return { kind: "html", body: listing(this.config) };
|
|
49
|
+
const [, app, page, ...rest] = pathname.split("/");
|
|
50
|
+
const entry = this.entries.get(`${app}/${page}`);
|
|
51
|
+
if (!entry) return;
|
|
52
|
+
if (rest.length === 0) return { kind: "redirect", to: `${pathname}/` };
|
|
53
|
+
return { kind: "page", page: entry.served, rest: `/${rest.join("/")}` };
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
page: Site["page"] = (key) => this.entries.get(key)?.served;
|
|
57
|
+
|
|
58
|
+
upstream: Site["upstream"] = (rest) => {
|
|
59
|
+
const [app, catalog] = rest.split("/");
|
|
60
|
+
const gateway = this.config[app]?.gateway;
|
|
61
|
+
if (!gateway?.catalogs.includes(catalog)) return;
|
|
62
|
+
return gateway.url;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
private add(ref: PageRef): void {
|
|
66
|
+
const key = keyOf(ref);
|
|
67
|
+
const file = this.config[ref.app].pages[ref.page];
|
|
68
|
+
const { token, catalogs } = this.config[ref.app].gateway;
|
|
69
|
+
const entry: Entry = {
|
|
70
|
+
served: {
|
|
71
|
+
title: path.basename(file),
|
|
72
|
+
key,
|
|
73
|
+
state: () => {
|
|
74
|
+
if (entry.cached) return entry.cached;
|
|
75
|
+
const built = this.build(file, ref.app, catalogs, token);
|
|
76
|
+
entry.cached = built;
|
|
77
|
+
built.catch(() => {
|
|
78
|
+
if (entry.cached === built) entry.cached = undefined;
|
|
79
|
+
});
|
|
80
|
+
return built;
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
unwatch: () => {},
|
|
84
|
+
};
|
|
85
|
+
try {
|
|
86
|
+
entry.unwatch = watchFile(file, () => {
|
|
87
|
+
entry.cached = undefined;
|
|
88
|
+
this.onChange(key);
|
|
89
|
+
console.log(`changed ${key}`);
|
|
90
|
+
});
|
|
91
|
+
} catch (err) {
|
|
92
|
+
console.error(
|
|
93
|
+
`${key}: cannot watch ${file}: ${err instanceof Error ? err.message : String(err)}`,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
this.entries.set(key, entry);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private drop(ref: PageRef): void {
|
|
100
|
+
const key = keyOf(ref);
|
|
101
|
+
this.entries.get(key)?.unwatch();
|
|
102
|
+
this.entries.delete(key);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private async build(
|
|
106
|
+
file: string,
|
|
107
|
+
app: string,
|
|
108
|
+
catalogs: string[],
|
|
109
|
+
token: string,
|
|
110
|
+
): Promise<PageState> {
|
|
111
|
+
const [bundle, css] = await Promise.all([
|
|
112
|
+
bundlePage(file, {
|
|
113
|
+
aliases: catalogs,
|
|
114
|
+
dataPath: `/_n6k/data/${app}/`,
|
|
115
|
+
token,
|
|
116
|
+
}),
|
|
117
|
+
buildCss(file),
|
|
118
|
+
]);
|
|
119
|
+
return { bundle, css };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { PagesError, diffPages, parsePages, type PagesConfig } from "./pages";
|
|
3
|
+
|
|
4
|
+
const YML = `
|
|
5
|
+
apps:
|
|
6
|
+
nyc-taxi:
|
|
7
|
+
gateway: { url: ws://127.0.0.1:54321/ws, token: tok, catalogs: [remote, memory] }
|
|
8
|
+
pages:
|
|
9
|
+
queens-pickups: /abs/nyc/queens.mdx
|
|
10
|
+
rel: pages/rel.mdx
|
|
11
|
+
`;
|
|
12
|
+
|
|
13
|
+
describe("parsePages", () => {
|
|
14
|
+
test("parses apps, gateway and pages", () => {
|
|
15
|
+
expect(parsePages(YML, "/home/u/.n6k/pages.yml")).toEqual({
|
|
16
|
+
"nyc-taxi": {
|
|
17
|
+
gateway: {
|
|
18
|
+
url: "ws://127.0.0.1:54321/ws",
|
|
19
|
+
token: "tok",
|
|
20
|
+
catalogs: ["remote", "memory"],
|
|
21
|
+
},
|
|
22
|
+
pages: {
|
|
23
|
+
"queens-pickups": "/abs/nyc/queens.mdx",
|
|
24
|
+
rel: "/home/u/.n6k/pages/rel.mdx",
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("empty file is no apps", () => {
|
|
31
|
+
expect(parsePages("", "/p.yml")).toEqual({});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test.each([
|
|
35
|
+
["bad yaml", "apps: [", /p\.yml/],
|
|
36
|
+
["missing gateway", "apps:\n a:\n pages: {}", /apps\.a\.gateway/],
|
|
37
|
+
[
|
|
38
|
+
"non-ws url",
|
|
39
|
+
"apps:\n a:\n gateway: { url: http://x, token: t, catalogs: [] }",
|
|
40
|
+
/not ws/,
|
|
41
|
+
],
|
|
42
|
+
[
|
|
43
|
+
"bad catalog",
|
|
44
|
+
"apps:\n a:\n gateway: { url: ws://x, token: t, catalogs: [1x] }",
|
|
45
|
+
/catalogs\[0\]/,
|
|
46
|
+
],
|
|
47
|
+
[
|
|
48
|
+
"bad page name",
|
|
49
|
+
"apps:\n a:\n gateway: { url: ws://x, token: t, catalogs: [] }\n pages: { 'a/b': /x.mdx }",
|
|
50
|
+
/must match/,
|
|
51
|
+
],
|
|
52
|
+
[
|
|
53
|
+
"non-mdx page",
|
|
54
|
+
"apps:\n a:\n gateway: { url: ws://x, token: t, catalogs: [] }\n pages: { p: /x.tsx }",
|
|
55
|
+
/not an \.mdx/,
|
|
56
|
+
],
|
|
57
|
+
])("%s fails", (_label, text, message) => {
|
|
58
|
+
expect(() => parsePages(text, "/p.yml")).toThrow(PagesError);
|
|
59
|
+
expect(() => parsePages(text, "/p.yml")).toThrow(message);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
function config(pages: Record<string, string>, token = "t"): PagesConfig {
|
|
64
|
+
return { a: { gateway: { url: "ws://x", token, catalogs: ["db"] }, pages } };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
describe("diffPages", () => {
|
|
68
|
+
test("added, removed and changed file", () => {
|
|
69
|
+
const prev = config({ keep: "/k.mdx", gone: "/g.mdx", moved: "/m.mdx" });
|
|
70
|
+
const next = config({ keep: "/k.mdx", moved: "/m2.mdx", fresh: "/f.mdx" });
|
|
71
|
+
expect(diffPages(prev, next)).toEqual({
|
|
72
|
+
added: [{ app: "a", page: "fresh" }],
|
|
73
|
+
removed: [{ app: "a", page: "gone" }],
|
|
74
|
+
changed: [{ app: "a", page: "moved" }],
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("gateway change marks every page of the app changed", () => {
|
|
79
|
+
expect(
|
|
80
|
+
diffPages(config({ p: "/p.mdx" }), config({ p: "/p.mdx" }, "t2")),
|
|
81
|
+
).toEqual({ added: [], removed: [], changed: [{ app: "a", page: "p" }] });
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("removed app removes its pages", () => {
|
|
85
|
+
expect(diffPages(config({ p: "/p.mdx" }), {})).toEqual({
|
|
86
|
+
added: [],
|
|
87
|
+
removed: [{ app: "a", page: "p" }],
|
|
88
|
+
changed: [],
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
});
|
package/src/pages.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { ALIAS } from "./data-args";
|
|
3
|
+
|
|
4
|
+
export type Gateway = { url: string; token: string; catalogs: string[] };
|
|
5
|
+
export type App = { gateway: Gateway; pages: Record<string, string> };
|
|
6
|
+
export type PagesConfig = Record<string, App>;
|
|
7
|
+
|
|
8
|
+
export type PageRef = { app: string; page: string };
|
|
9
|
+
export type PagesDiff = {
|
|
10
|
+
added: PageRef[];
|
|
11
|
+
removed: PageRef[];
|
|
12
|
+
changed: PageRef[];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export class PagesError extends Error {}
|
|
16
|
+
|
|
17
|
+
const NAME = /^[A-Za-z0-9_-]+$/;
|
|
18
|
+
|
|
19
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
20
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function record(value: unknown, where: string): Record<string, unknown> {
|
|
24
|
+
if (!isRecord(value)) throw new PagesError(`${where}: expected a mapping`);
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function string(value: unknown, where: string): string {
|
|
29
|
+
if (typeof value !== "string" || value === "")
|
|
30
|
+
throw new PagesError(`${where}: expected a non-empty string`);
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function name(value: string, where: string): string {
|
|
35
|
+
if (!NAME.test(value))
|
|
36
|
+
throw new PagesError(
|
|
37
|
+
`${where}: ${JSON.stringify(value)} must match ${NAME.source}`,
|
|
38
|
+
);
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function parseGateway(value: unknown, where: string): Gateway {
|
|
43
|
+
const raw = record(value, where);
|
|
44
|
+
const url = string(raw.url, `${where}.url`);
|
|
45
|
+
if (!/^wss?:\/\//.test(url))
|
|
46
|
+
throw new PagesError(
|
|
47
|
+
`${where}.url: ${JSON.stringify(url)} is not ws(s)://`,
|
|
48
|
+
);
|
|
49
|
+
if (!Array.isArray(raw.catalogs))
|
|
50
|
+
throw new PagesError(`${where}.catalogs: expected a list`);
|
|
51
|
+
const catalogs = raw.catalogs.map((c, i) => {
|
|
52
|
+
const catalog = string(c, `${where}.catalogs[${i}]`);
|
|
53
|
+
if (!ALIAS.test(catalog))
|
|
54
|
+
throw new PagesError(
|
|
55
|
+
`${where}.catalogs[${i}]: ${JSON.stringify(catalog)} must match ${ALIAS.source}`,
|
|
56
|
+
);
|
|
57
|
+
return catalog;
|
|
58
|
+
});
|
|
59
|
+
return { url, token: string(raw.token, `${where}.token`), catalogs };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function parsePageFiles(
|
|
63
|
+
value: unknown,
|
|
64
|
+
where: string,
|
|
65
|
+
baseDir: string,
|
|
66
|
+
): Record<string, string> {
|
|
67
|
+
const pages: Record<string, string> = {};
|
|
68
|
+
for (const [page, file] of Object.entries(record(value, where))) {
|
|
69
|
+
const at = `${where}.${name(page, where)}`;
|
|
70
|
+
const abs = path.resolve(baseDir, string(file, at));
|
|
71
|
+
if (!abs.endsWith(".mdx")) throw new PagesError(`${at}: not an .mdx file`);
|
|
72
|
+
pages[page] = abs;
|
|
73
|
+
}
|
|
74
|
+
return pages;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function parsePages(text: string, file: string): PagesConfig {
|
|
78
|
+
let doc: unknown;
|
|
79
|
+
try {
|
|
80
|
+
doc = Bun.YAML.parse(text);
|
|
81
|
+
} catch (err) {
|
|
82
|
+
throw new PagesError(
|
|
83
|
+
`${file}: ${err instanceof Error ? err.message : String(err)}`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
const root = record(doc ?? {}, file);
|
|
87
|
+
const config: PagesConfig = {};
|
|
88
|
+
const baseDir = path.dirname(file);
|
|
89
|
+
for (const [app, value] of Object.entries(
|
|
90
|
+
record(root.apps ?? {}, `${file}: apps`),
|
|
91
|
+
)) {
|
|
92
|
+
const where = `${file}: apps.${name(app, `${file}: apps`)}`;
|
|
93
|
+
const raw = record(value, where);
|
|
94
|
+
config[app] = {
|
|
95
|
+
gateway: parseGateway(raw.gateway, `${where}.gateway`),
|
|
96
|
+
pages: parsePageFiles(raw.pages ?? {}, `${where}.pages`, baseDir),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return config;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function sameGateway(a: Gateway, b: Gateway): boolean {
|
|
103
|
+
return (
|
|
104
|
+
a.url === b.url &&
|
|
105
|
+
a.token === b.token &&
|
|
106
|
+
a.catalogs.join("\0") === b.catalogs.join("\0")
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function diffPages(prev: PagesConfig, next: PagesConfig): PagesDiff {
|
|
111
|
+
const diff: PagesDiff = { added: [], removed: [], changed: [] };
|
|
112
|
+
for (const [app, { gateway, pages }] of Object.entries(next)) {
|
|
113
|
+
const old = prev[app];
|
|
114
|
+
for (const [page, file] of Object.entries(pages)) {
|
|
115
|
+
const oldFile = old?.pages[page];
|
|
116
|
+
if (oldFile === undefined) diff.added.push({ app, page });
|
|
117
|
+
else if (oldFile !== file || !sameGateway(old.gateway, gateway))
|
|
118
|
+
diff.changed.push({ app, page });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
for (const [app, { pages }] of Object.entries(prev)) {
|
|
122
|
+
for (const page of Object.keys(pages)) {
|
|
123
|
+
if (next[app]?.pages[page] === undefined)
|
|
124
|
+
diff.removed.push({ app, page });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return diff;
|
|
128
|
+
}
|
package/src/root.tsx
CHANGED
|
@@ -5,21 +5,21 @@ import { N6kDuckDB, type DatabaseSpec, type SocketSpec } from "@n6k.io/db";
|
|
|
5
5
|
import { DuckDBProvider, QueryProvider } from "@n6k.io/ui/duckdb";
|
|
6
6
|
import { ConnectionLost, DuckDBConnectionGuard } from "@n6k.io/runtime";
|
|
7
7
|
|
|
8
|
-
function dataSocket(
|
|
8
|
+
function dataSocket(url: string, token: string): SocketSpec {
|
|
9
9
|
return {
|
|
10
10
|
connect: () => {
|
|
11
11
|
const scheme = location.protocol === "https:" ? "wss" : "ws";
|
|
12
|
-
return new WebSocket(`${scheme}://${location.host}
|
|
12
|
+
return new WebSocket(`${scheme}://${location.host}${url}`);
|
|
13
13
|
},
|
|
14
|
-
token
|
|
14
|
+
token,
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
function engine(aliases: string[]): N6kDuckDB {
|
|
18
|
+
function engine(aliases: string[], dataPath: string, token: string): N6kDuckDB {
|
|
19
19
|
const sockets: Record<string, SocketSpec> = {};
|
|
20
20
|
const databases: Record<string, DatabaseSpec> = {};
|
|
21
21
|
for (const alias of aliases) {
|
|
22
|
-
sockets[alias] = dataSocket(alias);
|
|
22
|
+
sockets[alias] = dataSocket(`${dataPath}${alias}`, token);
|
|
23
23
|
databases[alias] = { socket: alias };
|
|
24
24
|
}
|
|
25
25
|
return new N6kDuckDB({
|
|
@@ -34,11 +34,15 @@ function engine(aliases: string[]): N6kDuckDB {
|
|
|
34
34
|
export default function Root({
|
|
35
35
|
children,
|
|
36
36
|
aliases = [],
|
|
37
|
+
dataPath = "/_n6k/data/",
|
|
38
|
+
token = "",
|
|
37
39
|
}: {
|
|
38
40
|
children: ReactNode;
|
|
39
41
|
aliases?: string[];
|
|
42
|
+
dataPath?: string;
|
|
43
|
+
token?: string;
|
|
40
44
|
}) {
|
|
41
|
-
const [duckdb] = useState(() => engine(aliases));
|
|
45
|
+
const [duckdb] = useState(() => engine(aliases, dataPath, token));
|
|
42
46
|
return (
|
|
43
47
|
<QueryProvider>
|
|
44
48
|
<DuckDBProvider connect={duckdb.connect} metadataFns={duckdb.metadataFns}>
|
package/src/server.ts
CHANGED
|
@@ -1,16 +1,37 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
1
|
import type { ServerWebSocket } from "bun";
|
|
3
2
|
import type { Bundle } from "./bundle";
|
|
4
3
|
import { serveAsset } from "./assets";
|
|
5
4
|
|
|
6
|
-
export type
|
|
5
|
+
export type PageState = {
|
|
7
6
|
bundle: Bundle;
|
|
8
7
|
css: string;
|
|
9
|
-
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type ServedPage = {
|
|
11
|
+
title: string;
|
|
12
|
+
key: string;
|
|
13
|
+
state: () => Promise<PageState>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type Resolved =
|
|
17
|
+
| { kind: "page"; page: ServedPage; rest: string }
|
|
18
|
+
| { kind: "redirect"; to: string }
|
|
19
|
+
| { kind: "html"; body: string };
|
|
20
|
+
|
|
21
|
+
export type Site = {
|
|
22
|
+
resolve: (pathname: string) => Resolved | undefined;
|
|
23
|
+
page: (key: string) => ServedPage | undefined;
|
|
24
|
+
upstream: (path: string) => string | undefined;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type ServeOptions = {
|
|
28
|
+
hostname: string;
|
|
29
|
+
port: number;
|
|
30
|
+
origins: string[];
|
|
10
31
|
};
|
|
11
32
|
|
|
12
33
|
type SocketData =
|
|
13
|
-
| { kind: "reload" }
|
|
34
|
+
| { kind: "reload"; key: string }
|
|
14
35
|
| {
|
|
15
36
|
kind: "data";
|
|
16
37
|
url: string;
|
|
@@ -25,23 +46,29 @@ const ISOLATION = {
|
|
|
25
46
|
"Cross-Origin-Embedder-Policy": "require-corp",
|
|
26
47
|
};
|
|
27
48
|
|
|
28
|
-
const
|
|
49
|
+
const RELOAD_PREFIX = "/_n6k/reload/";
|
|
50
|
+
const DATA_PREFIX = "/_n6k/data/";
|
|
29
51
|
|
|
30
|
-
function
|
|
52
|
+
function topicFor(key: string): string {
|
|
53
|
+
return `reload:${key}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function shell(page: ServedPage): string {
|
|
57
|
+
const reload = JSON.stringify(RELOAD_PREFIX + page.key);
|
|
31
58
|
return `<!doctype html>
|
|
32
59
|
<html lang="en">
|
|
33
60
|
<head>
|
|
34
61
|
<meta charset="utf-8" />
|
|
35
62
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
36
|
-
<title>${Bun.escapeHTML(title)}</title>
|
|
37
|
-
<link rel="stylesheet" href="
|
|
63
|
+
<title>${Bun.escapeHTML(page.title)}</title>
|
|
64
|
+
<link rel="stylesheet" href="./styles.css" />
|
|
38
65
|
</head>
|
|
39
66
|
<body>
|
|
40
67
|
<div id="root"></div>
|
|
41
|
-
<script type="module" src="
|
|
68
|
+
<script type="module" src="./index.js"></script>
|
|
42
69
|
<script>
|
|
43
70
|
(function connect() {
|
|
44
|
-
const ws = new WebSocket((location.protocol === "https:" ? "wss://" : "ws://") + location.host +
|
|
71
|
+
const ws = new WebSocket((location.protocol === "https:" ? "wss://" : "ws://") + location.host + ${reload});
|
|
45
72
|
ws.onmessage = () => location.reload();
|
|
46
73
|
ws.onclose = () => setTimeout(connect, 1000);
|
|
47
74
|
})();
|
|
@@ -55,6 +82,14 @@ function withIsolation(res: Response): Response {
|
|
|
55
82
|
return res;
|
|
56
83
|
}
|
|
57
84
|
|
|
85
|
+
function html(body: string): Response {
|
|
86
|
+
return withIsolation(
|
|
87
|
+
new Response(body, {
|
|
88
|
+
headers: { "Content-Type": "text/html; charset=utf-8" },
|
|
89
|
+
}),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
58
93
|
function contentType(file: string): string {
|
|
59
94
|
if (file.endsWith(".js")) return "text/javascript";
|
|
60
95
|
if (file.endsWith(".css")) return "text/css";
|
|
@@ -81,49 +116,76 @@ function openUpstream(ws: ServerWebSocket<SocketData>): void {
|
|
|
81
116
|
});
|
|
82
117
|
}
|
|
83
118
|
|
|
84
|
-
|
|
85
|
-
|
|
119
|
+
async function servePage(
|
|
120
|
+
page: ServedPage,
|
|
121
|
+
rest: string,
|
|
122
|
+
): Promise<Response | undefined> {
|
|
123
|
+
if (rest === "/") return html(shell(page));
|
|
124
|
+
let state: PageState;
|
|
125
|
+
try {
|
|
126
|
+
state = await page.state();
|
|
127
|
+
} catch (err) {
|
|
128
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
129
|
+
return withIsolation(new Response(message, { status: 500 }));
|
|
130
|
+
}
|
|
131
|
+
if (rest === "/styles.css") {
|
|
132
|
+
return withIsolation(
|
|
133
|
+
new Response(state.css, { headers: { "Content-Type": "text/css" } }),
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
const built = state.bundle.get(rest);
|
|
137
|
+
if (!built) return undefined;
|
|
138
|
+
return withIsolation(
|
|
139
|
+
new Response(built, {
|
|
140
|
+
headers: {
|
|
141
|
+
"Content-Type": contentType(rest),
|
|
142
|
+
"Cache-Control": "no-store",
|
|
143
|
+
},
|
|
144
|
+
}),
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function startServer(site: Site, options: ServeOptions) {
|
|
149
|
+
const originAllowed = (req: Request): boolean =>
|
|
150
|
+
options.origins.length === 0 ||
|
|
151
|
+
options.origins.includes(req.headers.get("origin") ?? "");
|
|
152
|
+
|
|
86
153
|
return Bun.serve<SocketData>({
|
|
87
|
-
|
|
154
|
+
hostname: options.hostname,
|
|
155
|
+
port: options.port,
|
|
88
156
|
async fetch(req, server) {
|
|
89
157
|
const { pathname } = new URL(req.url);
|
|
90
158
|
|
|
91
|
-
if (
|
|
92
|
-
|
|
159
|
+
if (
|
|
160
|
+
(pathname.startsWith(RELOAD_PREFIX) ||
|
|
161
|
+
pathname.startsWith(DATA_PREFIX)) &&
|
|
162
|
+
!originAllowed(req)
|
|
163
|
+
) {
|
|
164
|
+
return new Response("origin not allowed", { status: 403 });
|
|
165
|
+
}
|
|
166
|
+
if (pathname.startsWith(RELOAD_PREFIX)) {
|
|
167
|
+
const key = pathname.slice(RELOAD_PREFIX.length);
|
|
168
|
+
if (!site.page(key))
|
|
169
|
+
return new Response(`no page ${key}`, { status: 404 });
|
|
170
|
+
if (server.upgrade(req, { data: { kind: "reload", key } })) return;
|
|
93
171
|
return new Response("upgrade required", { status: 426 });
|
|
94
172
|
}
|
|
95
|
-
if (pathname.startsWith(
|
|
96
|
-
const
|
|
97
|
-
const url =
|
|
98
|
-
if (!url)
|
|
99
|
-
return new Response(`no data alias ${alias}`, { status: 404 });
|
|
173
|
+
if (pathname.startsWith(DATA_PREFIX)) {
|
|
174
|
+
const rest = pathname.slice(DATA_PREFIX.length);
|
|
175
|
+
const url = site.upstream(rest);
|
|
176
|
+
if (!url) return new Response(`no data ${rest}`, { status: 404 });
|
|
100
177
|
if (server.upgrade(req, { data: { kind: "data", url, pending: [] } }))
|
|
101
178
|
return;
|
|
102
179
|
return new Response("upgrade required", { status: 426 });
|
|
103
180
|
}
|
|
104
181
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
if (pathname === "/styles.css") {
|
|
113
|
-
return withIsolation(
|
|
114
|
-
new Response(state.css, { headers: { "Content-Type": "text/css" } }),
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
const built = state.bundle.get(pathname);
|
|
118
|
-
if (built) {
|
|
119
|
-
return withIsolation(
|
|
120
|
-
new Response(built, {
|
|
121
|
-
headers: {
|
|
122
|
-
"Content-Type": contentType(pathname),
|
|
123
|
-
"Cache-Control": "no-store",
|
|
124
|
-
},
|
|
125
|
-
}),
|
|
126
|
-
);
|
|
182
|
+
const resolved = site.resolve(pathname);
|
|
183
|
+
if (resolved?.kind === "redirect")
|
|
184
|
+
return Response.redirect(resolved.to, 308);
|
|
185
|
+
if (resolved?.kind === "html") return html(resolved.body);
|
|
186
|
+
if (resolved?.kind === "page") {
|
|
187
|
+
const res = await servePage(resolved.page, resolved.rest);
|
|
188
|
+
if (res) return res;
|
|
127
189
|
}
|
|
128
190
|
const asset = await serveAsset(pathname);
|
|
129
191
|
if (asset) return withIsolation(asset);
|
|
@@ -131,7 +193,7 @@ export function startServer(mdxPath: string, port: number, state: ServeState) {
|
|
|
131
193
|
},
|
|
132
194
|
websocket: {
|
|
133
195
|
open(ws) {
|
|
134
|
-
if (ws.data.kind === "reload") ws.subscribe(
|
|
196
|
+
if (ws.data.kind === "reload") ws.subscribe(topicFor(ws.data.key));
|
|
135
197
|
else openUpstream(ws);
|
|
136
198
|
},
|
|
137
199
|
message(ws, msg) {
|
|
@@ -152,6 +214,8 @@ export function startServer(mdxPath: string, port: number, state: ServeState) {
|
|
|
152
214
|
});
|
|
153
215
|
}
|
|
154
216
|
|
|
155
|
-
export
|
|
156
|
-
|
|
217
|
+
export type Server = ReturnType<typeof startServer>;
|
|
218
|
+
|
|
219
|
+
export function reloadPage(server: Server, key: string): void {
|
|
220
|
+
server.publish(topicFor(key), "reload");
|
|
157
221
|
}
|
package/src/watch.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
+
import path from "node:path";
|
|
1
2
|
import { watch } from "node:fs";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
// Watches the parent dir: editors that save via rename replace the inode, which ends a file watch.
|
|
5
|
+
export function watchFile(file: string, onChange: () => void): () => void {
|
|
6
|
+
const name = path.basename(file);
|
|
4
7
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
5
|
-
watch(file, () => {
|
|
8
|
+
const watcher = watch(path.dirname(file), (_event, changed) => {
|
|
9
|
+
if (changed !== name) return;
|
|
6
10
|
clearTimeout(timer);
|
|
7
11
|
timer = setTimeout(onChange, 100);
|
|
8
12
|
});
|
|
13
|
+
return () => {
|
|
14
|
+
clearTimeout(timer);
|
|
15
|
+
watcher.close();
|
|
16
|
+
};
|
|
9
17
|
}
|