@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.
@@ -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 dataSocket(alias: string): SocketSpec {
9
- return {
10
- connect: () => {
11
- const scheme = location.protocol === "https:" ? "wss" : "ws";
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 aliases) {
22
- sockets[alias] = dataSocket(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
- aliases = [],
32
+ data = {},
33
+ tokens = {},
37
34
  }: {
38
35
  children: ReactNode;
39
- aliases?: string[];
36
+ data?: Record<string, string>;
37
+ tokens?: Record<string, string>;
40
38
  }) {
41
- const [duckdb] = useState(() => engine(aliases));
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 ServeState = {
4
+ export type PageState = {
7
5
  bundle: Bundle;
8
6
  css: string;
9
- data: Record<string, string>;
10
7
  };
11
8
 
12
- type SocketData =
13
- | { kind: "reload" }
14
- | {
15
- kind: "data";
16
- url: string;
17
- upstream?: WebSocket;
18
- pending: Frame[];
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 Frame = string | Uint8Array<ArrayBuffer>;
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 RELOAD_TOPIC = "reload";
38
+ const RELOAD_PREFIX = "/_n6k/reload/";
29
39
 
30
- function shell(title: string): string {
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="/styles.css" />
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="/index.js"></script>
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 + "/_n6k/reload");
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 openUpstream(ws: ServerWebSocket<SocketData>): void {
65
- const data = ws.data;
66
- if (data.kind !== "data") return;
67
- const upstream = new WebSocket(data.url);
68
- upstream.binaryType = "arraybuffer";
69
- data.upstream = upstream;
70
- upstream.addEventListener("open", () => {
71
- for (const msg of data.pending) upstream.send(msg);
72
- data.pending = [];
73
- });
74
- upstream.addEventListener("message", (ev) => {
75
- ws.send(typeof ev.data === "string" ? ev.data : new Uint8Array(ev.data));
76
- });
77
- upstream.addEventListener("close", (ev) => ws.close(1000, ev.reason));
78
- upstream.addEventListener("error", () => {
79
- console.error(`[n6k-serve] upstream ${data.url} failed`);
80
- ws.close(1011, `upstream ${data.url} failed`);
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(mdxPath: string, port: number, state: ServeState) {
85
- const title = path.basename(mdxPath);
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
- port,
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 === "/_n6k/reload") {
92
- if (server.upgrade(req, { data: { kind: "reload" } })) return;
93
- return new Response("upgrade required", { status: 426 });
94
- }
95
- if (pathname.startsWith("/_n6k/data/")) {
96
- const alias = pathname.slice("/_n6k/data/".length);
97
- const url = state.data[alias];
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
- if (pathname === "/") {
106
- return withIsolation(
107
- new Response(shell(title), {
108
- headers: { "Content-Type": "text/html; charset=utf-8" },
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
- if (ws.data.kind === "reload") ws.subscribe(RELOAD_TOPIC);
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 function reloadClients(server: ReturnType<typeof startServer>): void {
156
- server.publish(RELOAD_TOPIC, "reload");
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 />
@@ -1,10 +1,10 @@
1
- // Provided by @n6k.io/build's bundleN6kWorkers / n6kWorkersPlugin.
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 bundleN6kDuckDB / n6kDuckDBPlugin.
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
- export function watchFile(file: string, onChange: () => void): void {
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
  }