@n6k.io/serve 0.1.1 → 0.2.2
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 +21 -3
- package/package.json +7 -4
- package/src/assets.ts +0 -1
- package/src/bundle.ts +7 -2
- package/src/cli.ts +134 -35
- package/src/component-docs.ts +63 -0
- package/src/css.ts +3 -2
- package/src/data-args.test.ts +27 -5
- package/src/data-args.ts +29 -6
- package/src/mcp-instructions.ts +22 -0
- package/src/mcp.test.ts +227 -0
- package/src/mcp.ts +268 -0
- package/src/mdx-components.tsx +14 -5
- package/src/pages-site.ts +113 -0
- package/src/pages.test.ts +91 -0
- package/src/pages.ts +128 -0
- package/src/root.tsx +14 -16
- package/src/server.ts +96 -91
- package/src/sql/console.mdx +1 -0
- package/src/sql/editor.mdx +1 -0
- package/src/virtual-modules.d.ts +2 -2
- package/src/watch.ts +10 -2
|
@@ -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
|
+
export 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,17 @@ 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
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return new WebSocket(`${scheme}://${location.host}/_n6k/data/${alias}`);
|
|
13
|
-
},
|
|
14
|
-
token: "",
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function engine(aliases: string[]): N6kDuckDB {
|
|
8
|
+
function engine(
|
|
9
|
+
data: Record<string, string>,
|
|
10
|
+
tokens: Record<string, string>,
|
|
11
|
+
): N6kDuckDB {
|
|
19
12
|
const sockets: Record<string, SocketSpec> = {};
|
|
20
13
|
const databases: Record<string, DatabaseSpec> = {};
|
|
21
|
-
for (const alias of
|
|
22
|
-
sockets[alias] =
|
|
14
|
+
for (const [alias, url] of Object.entries(data)) {
|
|
15
|
+
sockets[alias] = {
|
|
16
|
+
connect: () => new WebSocket(url),
|
|
17
|
+
token: Object.hasOwn(tokens, alias) ? tokens[alias]! : "",
|
|
18
|
+
};
|
|
23
19
|
databases[alias] = { socket: alias };
|
|
24
20
|
}
|
|
25
21
|
return new N6kDuckDB({
|
|
@@ -33,12 +29,14 @@ function engine(aliases: string[]): N6kDuckDB {
|
|
|
33
29
|
|
|
34
30
|
export default function Root({
|
|
35
31
|
children,
|
|
36
|
-
|
|
32
|
+
data = {},
|
|
33
|
+
tokens = {},
|
|
37
34
|
}: {
|
|
38
35
|
children: ReactNode;
|
|
39
|
-
|
|
36
|
+
data?: Record<string, string>;
|
|
37
|
+
tokens?: Record<string, string>;
|
|
40
38
|
}) {
|
|
41
|
-
const [duckdb] = useState(() => engine(
|
|
39
|
+
const [duckdb] = useState(() => engine(data, tokens));
|
|
42
40
|
return (
|
|
43
41
|
<QueryProvider>
|
|
44
42
|
<DuckDBProvider connect={duckdb.connect} metadataFns={duckdb.metadataFns}>
|
package/src/server.ts
CHANGED
|
@@ -1,47 +1,62 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import type { ServerWebSocket } from "bun";
|
|
3
1
|
import type { Bundle } from "./bundle";
|
|
4
2
|
import { serveAsset } from "./assets";
|
|
5
3
|
|
|
6
|
-
export type
|
|
4
|
+
export type PageState = {
|
|
7
5
|
bundle: Bundle;
|
|
8
6
|
css: string;
|
|
9
|
-
data: Record<string, string>;
|
|
10
7
|
};
|
|
11
8
|
|
|
12
|
-
type
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
9
|
+
export type ServedPage = {
|
|
10
|
+
title: string;
|
|
11
|
+
key: string;
|
|
12
|
+
state: () => Promise<PageState>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type Resolved =
|
|
16
|
+
| { kind: "page"; page: ServedPage; rest: string }
|
|
17
|
+
| { kind: "redirect"; to: string }
|
|
18
|
+
| { kind: "html"; body: string };
|
|
19
|
+
|
|
20
|
+
export type Site = {
|
|
21
|
+
resolve: (pathname: string) => Resolved | undefined;
|
|
22
|
+
page: (key: string) => ServedPage | undefined;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type ServeOptions = {
|
|
26
|
+
hostname: string;
|
|
27
|
+
port: number;
|
|
28
|
+
origins: string[];
|
|
29
|
+
};
|
|
20
30
|
|
|
21
|
-
type
|
|
31
|
+
type SocketData = { key: string };
|
|
22
32
|
|
|
23
33
|
const ISOLATION = {
|
|
24
34
|
"Cross-Origin-Opener-Policy": "same-origin",
|
|
25
35
|
"Cross-Origin-Embedder-Policy": "require-corp",
|
|
26
36
|
};
|
|
27
37
|
|
|
28
|
-
const
|
|
38
|
+
const RELOAD_PREFIX = "/_n6k/reload/";
|
|
29
39
|
|
|
30
|
-
function
|
|
40
|
+
function topicFor(key: string): string {
|
|
41
|
+
return `reload:${key}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function shell(page: ServedPage): string {
|
|
45
|
+
const reload = JSON.stringify(RELOAD_PREFIX + page.key);
|
|
31
46
|
return `<!doctype html>
|
|
32
47
|
<html lang="en">
|
|
33
48
|
<head>
|
|
34
49
|
<meta charset="utf-8" />
|
|
35
50
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
36
|
-
<title>${Bun.escapeHTML(title)}</title>
|
|
37
|
-
<link rel="stylesheet" href="
|
|
51
|
+
<title>${Bun.escapeHTML(page.title)}</title>
|
|
52
|
+
<link rel="stylesheet" href="./styles.css" />
|
|
38
53
|
</head>
|
|
39
54
|
<body>
|
|
40
55
|
<div id="root"></div>
|
|
41
|
-
<script type="module" src="
|
|
56
|
+
<script type="module" src="./index.js"></script>
|
|
42
57
|
<script>
|
|
43
58
|
(function connect() {
|
|
44
|
-
const ws = new WebSocket((location.protocol === "https:" ? "wss://" : "ws://") + location.host +
|
|
59
|
+
const ws = new WebSocket((location.protocol === "https:" ? "wss://" : "ws://") + location.host + ${reload});
|
|
45
60
|
ws.onmessage = () => location.reload();
|
|
46
61
|
ws.onclose = () => setTimeout(connect, 1000);
|
|
47
62
|
})();
|
|
@@ -55,75 +70,77 @@ function withIsolation(res: Response): Response {
|
|
|
55
70
|
return res;
|
|
56
71
|
}
|
|
57
72
|
|
|
73
|
+
function html(body: string): Response {
|
|
74
|
+
return withIsolation(
|
|
75
|
+
new Response(body, {
|
|
76
|
+
headers: { "Content-Type": "text/html; charset=utf-8" },
|
|
77
|
+
}),
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
58
81
|
function contentType(file: string): string {
|
|
59
82
|
if (file.endsWith(".js")) return "text/javascript";
|
|
60
83
|
if (file.endsWith(".css")) return "text/css";
|
|
61
84
|
return "application/octet-stream";
|
|
62
85
|
}
|
|
63
86
|
|
|
64
|
-
function
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
87
|
+
async function servePage(
|
|
88
|
+
page: ServedPage,
|
|
89
|
+
rest: string,
|
|
90
|
+
): Promise<Response | undefined> {
|
|
91
|
+
if (rest === "/") return html(shell(page));
|
|
92
|
+
let state: PageState;
|
|
93
|
+
try {
|
|
94
|
+
state = await page.state();
|
|
95
|
+
} catch (err) {
|
|
96
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
97
|
+
return withIsolation(new Response(message, { status: 500 }));
|
|
98
|
+
}
|
|
99
|
+
if (rest === "/styles.css") {
|
|
100
|
+
return withIsolation(
|
|
101
|
+
new Response(state.css, { headers: { "Content-Type": "text/css" } }),
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
const built = state.bundle.get(rest);
|
|
105
|
+
if (!built) return undefined;
|
|
106
|
+
return withIsolation(
|
|
107
|
+
new Response(built, {
|
|
108
|
+
headers: {
|
|
109
|
+
"Content-Type": contentType(rest),
|
|
110
|
+
"Cache-Control": "no-store",
|
|
111
|
+
},
|
|
112
|
+
}),
|
|
113
|
+
);
|
|
82
114
|
}
|
|
83
115
|
|
|
84
|
-
export function startServer(
|
|
85
|
-
const
|
|
116
|
+
export function startServer(site: Site, options: ServeOptions) {
|
|
117
|
+
const originAllowed = (req: Request): boolean =>
|
|
118
|
+
options.origins.length === 0 ||
|
|
119
|
+
options.origins.includes(req.headers.get("origin") ?? "");
|
|
120
|
+
|
|
86
121
|
return Bun.serve<SocketData>({
|
|
87
|
-
|
|
122
|
+
hostname: options.hostname,
|
|
123
|
+
port: options.port,
|
|
88
124
|
async fetch(req, server) {
|
|
89
125
|
const { pathname } = new URL(req.url);
|
|
90
126
|
|
|
91
|
-
if (pathname
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (!url)
|
|
99
|
-
return new Response(`no data alias ${alias}`, { status: 404 });
|
|
100
|
-
if (server.upgrade(req, { data: { kind: "data", url, pending: [] } }))
|
|
101
|
-
return;
|
|
127
|
+
if (pathname.startsWith(RELOAD_PREFIX)) {
|
|
128
|
+
if (!originAllowed(req))
|
|
129
|
+
return new Response("origin not allowed", { status: 403 });
|
|
130
|
+
const key = pathname.slice(RELOAD_PREFIX.length);
|
|
131
|
+
if (!site.page(key))
|
|
132
|
+
return new Response(`no page ${key}`, { status: 404 });
|
|
133
|
+
if (server.upgrade(req, { data: { key } })) return;
|
|
102
134
|
return new Response("upgrade required", { status: 426 });
|
|
103
135
|
}
|
|
104
136
|
|
|
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
|
-
);
|
|
137
|
+
const resolved = site.resolve(pathname);
|
|
138
|
+
if (resolved?.kind === "redirect")
|
|
139
|
+
return Response.redirect(resolved.to, 308);
|
|
140
|
+
if (resolved?.kind === "html") return html(resolved.body);
|
|
141
|
+
if (resolved?.kind === "page") {
|
|
142
|
+
const res = await servePage(resolved.page, resolved.rest);
|
|
143
|
+
if (res) return res;
|
|
127
144
|
}
|
|
128
145
|
const asset = await serveAsset(pathname);
|
|
129
146
|
if (asset) return withIsolation(asset);
|
|
@@ -131,27 +148,15 @@ export function startServer(mdxPath: string, port: number, state: ServeState) {
|
|
|
131
148
|
},
|
|
132
149
|
websocket: {
|
|
133
150
|
open(ws) {
|
|
134
|
-
|
|
135
|
-
else openUpstream(ws);
|
|
136
|
-
},
|
|
137
|
-
message(ws, msg) {
|
|
138
|
-
const data = ws.data;
|
|
139
|
-
if (data.kind !== "data") return;
|
|
140
|
-
const frame: Frame =
|
|
141
|
-
typeof msg === "string" ? msg : new Uint8Array(msg);
|
|
142
|
-
if (data.upstream?.readyState === WebSocket.OPEN) {
|
|
143
|
-
data.upstream.send(frame);
|
|
144
|
-
} else {
|
|
145
|
-
data.pending.push(frame);
|
|
146
|
-
}
|
|
147
|
-
},
|
|
148
|
-
close(ws) {
|
|
149
|
-
if (ws.data.kind === "data") ws.data.upstream?.close();
|
|
151
|
+
ws.subscribe(topicFor(ws.data.key));
|
|
150
152
|
},
|
|
153
|
+
message() {},
|
|
151
154
|
},
|
|
152
155
|
});
|
|
153
156
|
}
|
|
154
157
|
|
|
155
|
-
export
|
|
156
|
-
|
|
158
|
+
export type Server = ReturnType<typeof startServer>;
|
|
159
|
+
|
|
160
|
+
export function reloadPage(server: Server, key: string): void {
|
|
161
|
+
server.publish(topicFor(key), "reload");
|
|
157
162
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<SqlConsole />
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<SqlEditor />
|
package/src/virtual-modules.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
// Provided by @n6k.io/build's
|
|
1
|
+
// Provided by @n6k.io/build's n6kWorkersPlugin.
|
|
2
2
|
declare module "virtual:n6k-workers" {
|
|
3
3
|
export const duckdbWorkerUrl: string;
|
|
4
4
|
export const wsWorkerUrl: string;
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
// Provided by @n6k.io/build's
|
|
7
|
+
// Provided by @n6k.io/build's n6kDuckDBPlugin.
|
|
8
8
|
declare module "virtual:n6k-duckdb-bundle" {
|
|
9
9
|
export const duckdbBundle: {
|
|
10
10
|
mainModule: string;
|
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
|
}
|