@n6k.io/serve 0.0.0-stage → 0.1.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 +26 -2
- package/package.json +51 -4
- package/src/assets.ts +72 -0
- package/src/bundle.ts +65 -0
- package/src/cli.ts +80 -0
- package/src/css.ts +28 -0
- package/src/data-args.test.ts +39 -0
- package/src/data-args.ts +44 -0
- package/src/globals.css +126 -0
- package/src/mdx-components.tsx +21 -0
- package/src/paths.ts +18 -0
- package/src/root.tsx +50 -0
- package/src/server.ts +157 -0
- package/src/virtual-modules.d.ts +14 -0
- package/src/watch.ts +9 -0
package/README.md
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @n6k.io/serve
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Serve a single `.mdx` page in the browser with live data from
|
|
4
|
+
[n6kd](https://github.com/n6k-io/n6kd) or pykit `n6k database`.
|
|
5
|
+
|
|
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 --port 3000 # default 3000
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or in the page's frontmatter (`--data` wins for the same alias):
|
|
14
|
+
|
|
15
|
+
```yaml
|
|
16
|
+
---
|
|
17
|
+
attach:
|
|
18
|
+
nyc: ws://localhost:8080/ws
|
|
19
|
+
---
|
|
20
|
+
<Metric label="Trips" query="SELECT count(*) FROM nyc.trips" />
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The alias is the catalog name on the server: `nyc=ws://…` attaches the
|
|
24
|
+
server's `nyc` catalog as `nyc`. A bare URL attaches the server's `db` catalog.
|
|
25
|
+
|
|
26
|
+
Every `@n6k.io/ui` component is available in the page without an import. The
|
|
27
|
+
page reloads when the file is saved.
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n6k.io/serve",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Serve a single .mdx page with live data from n6kd or pykit",
|
|
6
|
+
"repository": "https://github.com/n6k-io/js-serve",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"files": [
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"bin": {
|
|
13
|
+
"n6k-serve": "src/cli.ts"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "bun test",
|
|
17
|
+
"typecheck": "tsc --noEmit",
|
|
18
|
+
"lint": "eslint .",
|
|
19
|
+
"fmt": "prettier --write --log-level=warn .",
|
|
20
|
+
"check": "bun run fmt && bun run lint --fix && bun run typecheck",
|
|
21
|
+
"fmt:check": "prettier --check .",
|
|
22
|
+
"ci": "bun run fmt:check && bun run lint && bun run typecheck && bun test"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@duckdb/duckdb-wasm": "1.33.1-dev114.0",
|
|
26
|
+
"@n6k.io/build": "^0.5.1",
|
|
27
|
+
"@n6k.io/db": "^0.13.1",
|
|
28
|
+
"@n6k.io/runtime": "^0.1.2",
|
|
29
|
+
"@n6k.io/ui": "^0.10.2",
|
|
30
|
+
"@tailwindcss/cli": "^4.3.3",
|
|
31
|
+
"@tailwindcss/typography": "^0.5.20",
|
|
32
|
+
"@tanstack/react-query": "^5.104.1",
|
|
33
|
+
"esbuild": "^0.28.2",
|
|
34
|
+
"radix-ui": "^1.7.0",
|
|
35
|
+
"react": "^19.3.0",
|
|
36
|
+
"react-dom": "^19.3.0",
|
|
37
|
+
"shadcn": "^4.21.4",
|
|
38
|
+
"tailwindcss": "^4.3.3",
|
|
39
|
+
"tw-animate-css": "^1.4.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@eslint/js": "^10.0.1",
|
|
43
|
+
"@types/bun": "latest",
|
|
44
|
+
"@types/mdx": "^2.0.14",
|
|
45
|
+
"@types/react": "^19.3.0",
|
|
46
|
+
"@types/react-dom": "^19.3.0",
|
|
47
|
+
"eslint": "^10.4.1",
|
|
48
|
+
"eslint-plugin-unicorn": "^65.0.1",
|
|
49
|
+
"prettier": "^3.8.1",
|
|
50
|
+
"typescript": "6.0.3",
|
|
51
|
+
"typescript-eslint": "^8.61.0"
|
|
52
|
+
}
|
|
53
|
+
}
|
package/src/assets.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { wasmDir } from "@n6k.io/db/wasm-dir";
|
|
3
|
+
import {
|
|
4
|
+
N6K_DUCKDB_FILES,
|
|
5
|
+
N6K_WASM_MIME,
|
|
6
|
+
N6K_WORKER_DEV_URLS,
|
|
7
|
+
bundleN6kWorker,
|
|
8
|
+
collectN6kWasmFiles,
|
|
9
|
+
findFontFile,
|
|
10
|
+
n6kDuckDBUrl,
|
|
11
|
+
resolveDuckDBDist,
|
|
12
|
+
resolveN6kWorkerSource,
|
|
13
|
+
} from "@n6k.io/build/pipeline";
|
|
14
|
+
import { SERVE_NODE_MODULES, SERVE_ROOT } from "./paths";
|
|
15
|
+
|
|
16
|
+
const SAME_ORIGIN = { "Cross-Origin-Resource-Policy": "same-origin" };
|
|
17
|
+
|
|
18
|
+
const duckdbFiles = new Map(
|
|
19
|
+
Object.values(N6K_DUCKDB_FILES).map((file) => [
|
|
20
|
+
n6kDuckDBUrl(file),
|
|
21
|
+
resolveDuckDBDist(file),
|
|
22
|
+
]),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const wasmFiles = new Map(
|
|
26
|
+
collectN6kWasmFiles(wasmDir).map((f) => [`/${f.rel}`, f.abs]),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
function mime(file: string): string {
|
|
30
|
+
return N6K_WASM_MIME[path.extname(file)] ?? "application/octet-stream";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// duckdb runtime, n6k driver workers, n6k duckdb extensions and fontsource fonts; undefined if `pathname` is none of them.
|
|
34
|
+
export async function serveAsset(
|
|
35
|
+
pathname: string,
|
|
36
|
+
): Promise<Response | undefined> {
|
|
37
|
+
const fixed = duckdbFiles.get(pathname) ?? wasmFiles.get(pathname);
|
|
38
|
+
if (fixed) {
|
|
39
|
+
return new Response(Bun.file(fixed), {
|
|
40
|
+
headers: { "Content-Type": mime(fixed), ...SAME_ORIGIN },
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (pathname.startsWith("/files/")) {
|
|
44
|
+
const font = findFontFile(
|
|
45
|
+
SERVE_NODE_MODULES,
|
|
46
|
+
pathname.slice("/files/".length),
|
|
47
|
+
);
|
|
48
|
+
if (font) {
|
|
49
|
+
return new Response(Bun.file(font), {
|
|
50
|
+
headers: { "Content-Type": "font/woff2", ...SAME_ORIGIN },
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const workerRel = N6K_WORKER_DEV_URLS[pathname];
|
|
55
|
+
if (workerRel) {
|
|
56
|
+
const code = await bundleN6kWorker(
|
|
57
|
+
resolveN6kWorkerSource(workerRel, SERVE_ROOT),
|
|
58
|
+
);
|
|
59
|
+
return new Response(code, {
|
|
60
|
+
headers: {
|
|
61
|
+
"Content-Type": "text/javascript",
|
|
62
|
+
"Cache-Control": "no-store",
|
|
63
|
+
...SAME_ORIGIN,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function hasWasmExtensions(): boolean {
|
|
71
|
+
return wasmFiles.size > 0;
|
|
72
|
+
}
|
package/src/bundle.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import * as esbuild from "esbuild";
|
|
3
|
+
import {
|
|
4
|
+
n6kDuckDBPlugin,
|
|
5
|
+
n6kWorkersPlugin,
|
|
6
|
+
preprocess,
|
|
7
|
+
virtualModulesPlugin,
|
|
8
|
+
} from "@n6k.io/build/pipeline";
|
|
9
|
+
import {
|
|
10
|
+
SERVE_NODE_MODULES,
|
|
11
|
+
SERVE_ROOT,
|
|
12
|
+
SRC_DIR,
|
|
13
|
+
nearestNodeModules,
|
|
14
|
+
} from "./paths";
|
|
15
|
+
|
|
16
|
+
export type Bundle = Map<string, string>;
|
|
17
|
+
|
|
18
|
+
export async function bundlePage(
|
|
19
|
+
mdxPath: string,
|
|
20
|
+
aliases: string[],
|
|
21
|
+
): Promise<Bundle> {
|
|
22
|
+
const mdxDir = path.dirname(mdxPath);
|
|
23
|
+
const { entries, modules } = await preprocess(
|
|
24
|
+
{ index: mdxPath },
|
|
25
|
+
{
|
|
26
|
+
rootComponent: path.join(SRC_DIR, "root.tsx"),
|
|
27
|
+
rootProps: { aliases },
|
|
28
|
+
mdxComponents: path.join(SRC_DIR, "mdx-components.tsx"),
|
|
29
|
+
},
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const pageNodeModules = nearestNodeModules(mdxDir);
|
|
33
|
+
const result = await esbuild.build({
|
|
34
|
+
entryPoints: entries,
|
|
35
|
+
bundle: true,
|
|
36
|
+
splitting: true,
|
|
37
|
+
format: "esm",
|
|
38
|
+
jsx: "automatic",
|
|
39
|
+
write: false,
|
|
40
|
+
outdir: "/",
|
|
41
|
+
absWorkingDir: SERVE_ROOT,
|
|
42
|
+
tsconfig: path.join(SERVE_ROOT, "tsconfig.json"),
|
|
43
|
+
nodePaths: [
|
|
44
|
+
SERVE_NODE_MODULES,
|
|
45
|
+
...(pageNodeModules && pageNodeModules !== SERVE_NODE_MODULES
|
|
46
|
+
? [pageNodeModules]
|
|
47
|
+
: []),
|
|
48
|
+
],
|
|
49
|
+
define: { "process.env.NODE_ENV": JSON.stringify("development") },
|
|
50
|
+
logLevel: "silent",
|
|
51
|
+
plugins: [
|
|
52
|
+
virtualModulesPlugin(modules, mdxDir),
|
|
53
|
+
n6kWorkersPlugin(undefined, { used: false }),
|
|
54
|
+
n6kDuckDBPlugin(undefined, { used: false }),
|
|
55
|
+
],
|
|
56
|
+
alias: {
|
|
57
|
+
react: path.join(SERVE_NODE_MODULES, "react"),
|
|
58
|
+
"react-dom": path.join(SERVE_NODE_MODULES, "react-dom"),
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const out: Bundle = new Map();
|
|
63
|
+
for (const file of result.outputFiles) out.set(file.path, file.text);
|
|
64
|
+
return out;
|
|
65
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import { parseAttachUrls } from "@n6k.io/build/pipeline";
|
|
6
|
+
import { DataArgError, resolveData } from "./data-args";
|
|
7
|
+
import { bundlePage } from "./bundle";
|
|
8
|
+
import { buildCss } from "./css";
|
|
9
|
+
import { hasWasmExtensions } from "./assets";
|
|
10
|
+
import { reloadClients, startServer, type ServeState } from "./server";
|
|
11
|
+
import { watchFile } from "./watch";
|
|
12
|
+
|
|
13
|
+
const USAGE =
|
|
14
|
+
"usage: n6k-serve <page.mdx> [--data [alias=]ws://…]... [--port 3000]";
|
|
15
|
+
|
|
16
|
+
function fail(message: string): never {
|
|
17
|
+
console.error(message);
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const { values, positionals } = parseArgs({
|
|
22
|
+
args: process.argv.slice(2),
|
|
23
|
+
options: {
|
|
24
|
+
data: { type: "string", multiple: true, default: [] },
|
|
25
|
+
port: { type: "string", default: "3000" },
|
|
26
|
+
},
|
|
27
|
+
allowPositionals: true,
|
|
28
|
+
strict: true,
|
|
29
|
+
});
|
|
30
|
+
|
|
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
|
+
const port = Number(values.port);
|
|
36
|
+
if (!Number.isInteger(port) || port < 0)
|
|
37
|
+
fail(`--port ${values.port}: not a port`);
|
|
38
|
+
if (!hasWasmExtensions())
|
|
39
|
+
fail("n6k duckdb wasm extensions missing from @n6k.io/db");
|
|
40
|
+
|
|
41
|
+
const dataFlags = values.data ?? [];
|
|
42
|
+
|
|
43
|
+
function resolveDataFor(file: string): Record<string, string> {
|
|
44
|
+
try {
|
|
45
|
+
return resolveData(parseAttachUrls(readFileSync(file, "utf8")), dataFlags);
|
|
46
|
+
} catch (err) {
|
|
47
|
+
if (err instanceof DataArgError) fail(err.message);
|
|
48
|
+
throw err;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function build(data: Record<string, string>): Promise<ServeState> {
|
|
53
|
+
const [bundle, css] = await Promise.all([
|
|
54
|
+
bundlePage(mdxPath, Object.keys(data)),
|
|
55
|
+
buildCss(mdxPath),
|
|
56
|
+
]);
|
|
57
|
+
return { bundle, css, data };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const state = await build(resolveDataFor(mdxPath));
|
|
61
|
+
const server = startServer(mdxPath, port, state);
|
|
62
|
+
|
|
63
|
+
console.log(`n6k-serve ${server.url}`);
|
|
64
|
+
for (const [alias, url] of Object.entries(state.data)) {
|
|
65
|
+
console.log(` ${alias} → ${url}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
watchFile(mdxPath, async () => {
|
|
69
|
+
try {
|
|
70
|
+
const data = resolveData(
|
|
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)}`);
|
|
77
|
+
} catch (err) {
|
|
78
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
79
|
+
}
|
|
80
|
+
});
|
package/src/css.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { mkdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { SERVE_NODE_MODULES, SRC_DIR } from "./paths";
|
|
4
|
+
|
|
5
|
+
// Under node_modules so the stylesheet's bare `@import`s resolve from serve's own deps.
|
|
6
|
+
const CACHE_DIR = path.join(SERVE_NODE_MODULES, ".cache", "n6k-serve");
|
|
7
|
+
const TAILWIND = path.join(SERVE_NODE_MODULES, ".bin", "tailwindcss");
|
|
8
|
+
|
|
9
|
+
export async function buildCss(mdxPath: string): Promise<string> {
|
|
10
|
+
mkdirSync(CACHE_DIR, { recursive: true });
|
|
11
|
+
const input = path.join(CACHE_DIR, `${process.pid}.in.css`);
|
|
12
|
+
const output = path.join(CACHE_DIR, `${process.pid}.out.css`);
|
|
13
|
+
const base = readFileSync(path.join(SRC_DIR, "globals.css"), "utf8");
|
|
14
|
+
await Bun.write(
|
|
15
|
+
input,
|
|
16
|
+
`${base}\n@source ${JSON.stringify(path.dirname(mdxPath))};\n`,
|
|
17
|
+
);
|
|
18
|
+
const proc = Bun.spawn([TAILWIND, "-i", input, "-o", output], {
|
|
19
|
+
stdout: "ignore",
|
|
20
|
+
stderr: "pipe",
|
|
21
|
+
});
|
|
22
|
+
if ((await proc.exited) !== 0) {
|
|
23
|
+
throw new Error(
|
|
24
|
+
`tailwindcss failed:\n${await new Response(proc.stderr).text()}`,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
return readFileSync(output, "utf8");
|
|
28
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { DataArgError, resolveData } from "./data-args";
|
|
3
|
+
|
|
4
|
+
describe("resolveData", () => {
|
|
5
|
+
test("bare URL attaches as db", () => {
|
|
6
|
+
expect(resolveData({}, ["ws://h:7823/ws"])).toEqual({
|
|
7
|
+
db: "ws://h:7823/ws",
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test("name=URL attaches as name", () => {
|
|
12
|
+
expect(resolveData({}, ["nyc=wss://h/ws", "sales=ws://h2/ws"])).toEqual({
|
|
13
|
+
nyc: "wss://h/ws",
|
|
14
|
+
sales: "ws://h2/ws",
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("a URL whose query contains = is still a bare URL", () => {
|
|
19
|
+
expect(resolveData({}, ["ws://h/ws?a=b"])).toEqual({
|
|
20
|
+
db: "ws://h/ws?a=b",
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("--data overrides frontmatter for the same alias", () => {
|
|
25
|
+
expect(
|
|
26
|
+
resolveData({ nyc: "ws://a/ws", other: "ws://o/ws" }, ["nyc=ws://b/ws"]),
|
|
27
|
+
).toEqual({ nyc: "ws://b/ws", other: "ws://o/ws" });
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("bad alias is rejected", () => {
|
|
31
|
+
expect(() => resolveData({}, ["1x=ws://h/ws"])).toThrow(DataArgError);
|
|
32
|
+
expect(() => resolveData({ "a-b": "ws://h/ws" }, [])).toThrow(DataArgError);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("non-ws URL is rejected", () => {
|
|
36
|
+
expect(() => resolveData({}, ["http://h/ws"])).toThrow(DataArgError);
|
|
37
|
+
expect(() => resolveData({}, ["db=localhost:7823"])).toThrow(DataArgError);
|
|
38
|
+
});
|
|
39
|
+
});
|
package/src/data-args.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const ALIAS = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
2
|
+
const DEFAULT_ALIAS = "db";
|
|
3
|
+
|
|
4
|
+
export class DataArgError extends Error {}
|
|
5
|
+
|
|
6
|
+
function checked(alias: string, url: string, source: string): [string, string] {
|
|
7
|
+
if (!ALIAS.test(alias)) {
|
|
8
|
+
throw new DataArgError(
|
|
9
|
+
`${source}: alias ${JSON.stringify(alias)} must match ${ALIAS.source}`,
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
if (!/^wss?:\/\//.test(url)) {
|
|
13
|
+
throw new DataArgError(
|
|
14
|
+
`${source}: ${JSON.stringify(url)} is not a ws:// or wss:// URL`,
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
return [alias, url];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function parseFlag(flag: string): [string, string] {
|
|
21
|
+
const eq = flag.indexOf("=");
|
|
22
|
+
const schemeAt = flag.indexOf("://");
|
|
23
|
+
if (eq === -1 || (schemeAt !== -1 && schemeAt < eq)) {
|
|
24
|
+
return checked(DEFAULT_ALIAS, flag, "--data");
|
|
25
|
+
}
|
|
26
|
+
return checked(flag.slice(0, eq), flag.slice(eq + 1), "--data");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Alias → URL. The alias is also the server catalog name the client attaches.
|
|
30
|
+
export function resolveData(
|
|
31
|
+
frontmatterUrls: Record<string, string>,
|
|
32
|
+
dataFlags: string[],
|
|
33
|
+
): Record<string, string> {
|
|
34
|
+
const out: Record<string, string> = {};
|
|
35
|
+
for (const [alias, url] of Object.entries(frontmatterUrls)) {
|
|
36
|
+
const [a, u] = checked(alias, url, "attach:");
|
|
37
|
+
out[a] = u;
|
|
38
|
+
}
|
|
39
|
+
for (const flag of dataFlags) {
|
|
40
|
+
const [a, u] = parseFlag(flag);
|
|
41
|
+
out[a] = u;
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
package/src/globals.css
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
@import "tailwindcss";
|
|
2
|
+
@import "@n6k.io/ui/preset.css";
|
|
3
|
+
@plugin "@tailwindcss/typography";
|
|
4
|
+
@import "tw-animate-css";
|
|
5
|
+
@import "shadcn/tailwind.css";
|
|
6
|
+
|
|
7
|
+
@custom-variant dark (&:is(.dark *));
|
|
8
|
+
|
|
9
|
+
:root {
|
|
10
|
+
--background: oklch(1 0 0);
|
|
11
|
+
--foreground: oklch(0.145 0 0);
|
|
12
|
+
--card: oklch(1 0 0);
|
|
13
|
+
--card-foreground: oklch(0.145 0 0);
|
|
14
|
+
--popover: oklch(1 0 0);
|
|
15
|
+
--popover-foreground: oklch(0.145 0 0);
|
|
16
|
+
--primary: oklch(0.205 0 0);
|
|
17
|
+
--primary-foreground: oklch(0.985 0 0);
|
|
18
|
+
--secondary: oklch(0.97 0 0);
|
|
19
|
+
--secondary-foreground: oklch(0.205 0 0);
|
|
20
|
+
--muted: oklch(0.97 0 0);
|
|
21
|
+
--muted-foreground: oklch(0.556 0 0);
|
|
22
|
+
--accent: oklch(0.97 0 0);
|
|
23
|
+
--accent-foreground: oklch(0.205 0 0);
|
|
24
|
+
--destructive: oklch(0.58 0.22 27);
|
|
25
|
+
--border: oklch(0.922 0 0);
|
|
26
|
+
--input: oklch(0.922 0 0);
|
|
27
|
+
--ring: oklch(0.708 0 0);
|
|
28
|
+
--chart-1: oklch(0.809 0.105 251.813);
|
|
29
|
+
--chart-2: oklch(0.623 0.214 259.815);
|
|
30
|
+
--chart-3: oklch(0.546 0.245 262.881);
|
|
31
|
+
--chart-4: oklch(0.488 0.243 264.376);
|
|
32
|
+
--chart-5: oklch(0.424 0.199 265.638);
|
|
33
|
+
--radius: 0.625rem;
|
|
34
|
+
--sidebar: oklch(0.985 0 0);
|
|
35
|
+
--sidebar-foreground: oklch(0.145 0 0);
|
|
36
|
+
--sidebar-primary: oklch(0.205 0 0);
|
|
37
|
+
--sidebar-primary-foreground: oklch(0.985 0 0);
|
|
38
|
+
--sidebar-accent: oklch(0.97 0 0);
|
|
39
|
+
--sidebar-accent-foreground: oklch(0.205 0 0);
|
|
40
|
+
--sidebar-border: oklch(0.922 0 0);
|
|
41
|
+
--sidebar-ring: oklch(0.708 0 0);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.dark {
|
|
45
|
+
--background: oklch(0.145 0 0);
|
|
46
|
+
--foreground: oklch(0.985 0 0);
|
|
47
|
+
--card: oklch(0.205 0 0);
|
|
48
|
+
--card-foreground: oklch(0.985 0 0);
|
|
49
|
+
--popover: oklch(0.205 0 0);
|
|
50
|
+
--popover-foreground: oklch(0.985 0 0);
|
|
51
|
+
--primary: oklch(0.87 0 0);
|
|
52
|
+
--primary-foreground: oklch(0.205 0 0);
|
|
53
|
+
--secondary: oklch(0.269 0 0);
|
|
54
|
+
--secondary-foreground: oklch(0.985 0 0);
|
|
55
|
+
--muted: oklch(0.269 0 0);
|
|
56
|
+
--muted-foreground: oklch(0.708 0 0);
|
|
57
|
+
--accent: oklch(0.371 0 0);
|
|
58
|
+
--accent-foreground: oklch(0.985 0 0);
|
|
59
|
+
--destructive: oklch(0.704 0.191 22.216);
|
|
60
|
+
--border: oklch(1 0 0 / 10%);
|
|
61
|
+
--input: oklch(1 0 0 / 15%);
|
|
62
|
+
--ring: oklch(0.556 0 0);
|
|
63
|
+
--chart-1: oklch(0.809 0.105 251.813);
|
|
64
|
+
--chart-2: oklch(0.623 0.214 259.815);
|
|
65
|
+
--chart-3: oklch(0.546 0.245 262.881);
|
|
66
|
+
--chart-4: oklch(0.488 0.243 264.376);
|
|
67
|
+
--chart-5: oklch(0.424 0.199 265.638);
|
|
68
|
+
--sidebar: oklch(0.205 0 0);
|
|
69
|
+
--sidebar-foreground: oklch(0.985 0 0);
|
|
70
|
+
--sidebar-primary: oklch(0.488 0.243 264.376);
|
|
71
|
+
--sidebar-primary-foreground: oklch(0.985 0 0);
|
|
72
|
+
--sidebar-accent: oklch(0.269 0 0);
|
|
73
|
+
--sidebar-accent-foreground: oklch(0.985 0 0);
|
|
74
|
+
--sidebar-border: oklch(1 0 0 / 10%);
|
|
75
|
+
--sidebar-ring: oklch(0.556 0 0);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@theme inline {
|
|
79
|
+
--color-sidebar-ring: var(--sidebar-ring);
|
|
80
|
+
--color-sidebar-border: var(--sidebar-border);
|
|
81
|
+
--color-sidebar-accent-foreground: var(--sidebar-accent-foreground);
|
|
82
|
+
--color-sidebar-accent: var(--sidebar-accent);
|
|
83
|
+
--color-sidebar-primary-foreground: var(--sidebar-primary-foreground);
|
|
84
|
+
--color-sidebar-primary: var(--sidebar-primary);
|
|
85
|
+
--color-sidebar-foreground: var(--sidebar-foreground);
|
|
86
|
+
--color-sidebar: var(--sidebar);
|
|
87
|
+
--color-chart-5: var(--chart-5);
|
|
88
|
+
--color-chart-4: var(--chart-4);
|
|
89
|
+
--color-chart-3: var(--chart-3);
|
|
90
|
+
--color-chart-2: var(--chart-2);
|
|
91
|
+
--color-chart-1: var(--chart-1);
|
|
92
|
+
--color-ring: var(--ring);
|
|
93
|
+
--color-input: var(--input);
|
|
94
|
+
--color-border: var(--border);
|
|
95
|
+
--color-destructive: var(--destructive);
|
|
96
|
+
--color-accent-foreground: var(--accent-foreground);
|
|
97
|
+
--color-accent: var(--accent);
|
|
98
|
+
--color-muted-foreground: var(--muted-foreground);
|
|
99
|
+
--color-muted: var(--muted);
|
|
100
|
+
--color-secondary-foreground: var(--secondary-foreground);
|
|
101
|
+
--color-secondary: var(--secondary);
|
|
102
|
+
--color-primary-foreground: var(--primary-foreground);
|
|
103
|
+
--color-primary: var(--primary);
|
|
104
|
+
--color-popover-foreground: var(--popover-foreground);
|
|
105
|
+
--color-popover: var(--popover);
|
|
106
|
+
--color-card-foreground: var(--card-foreground);
|
|
107
|
+
--color-card: var(--card);
|
|
108
|
+
--color-foreground: var(--foreground);
|
|
109
|
+
--color-background: var(--background);
|
|
110
|
+
--radius-sm: calc(var(--radius) * 0.6);
|
|
111
|
+
--radius-md: calc(var(--radius) * 0.8);
|
|
112
|
+
--radius-lg: var(--radius);
|
|
113
|
+
--radius-xl: calc(var(--radius) * 1.4);
|
|
114
|
+
--radius-2xl: calc(var(--radius) * 1.8);
|
|
115
|
+
--radius-3xl: calc(var(--radius) * 2.2);
|
|
116
|
+
--radius-4xl: calc(var(--radius) * 2.6);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
@layer base {
|
|
120
|
+
* {
|
|
121
|
+
@apply border-border outline-ring/50;
|
|
122
|
+
}
|
|
123
|
+
body {
|
|
124
|
+
@apply bg-background text-foreground;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ComponentProps } from "react";
|
|
2
|
+
import type { MDXComponents } from "mdx/types";
|
|
3
|
+
import * as n6k from "@n6k.io/ui/components";
|
|
4
|
+
|
|
5
|
+
// MDX component scope. The build auto-discovers this file, so every name
|
|
6
|
+
// returned here is usable as a bare tag in any `.mdx` page — no import needed.
|
|
7
|
+
// Spread the n6k component set, then add your own components or restyle the
|
|
8
|
+
// markdown elements. A tag that isn't registered here (or imported in the page)
|
|
9
|
+
// fails when the page is rendered.
|
|
10
|
+
export function useMDXComponents(components: MDXComponents): MDXComponents {
|
|
11
|
+
return {
|
|
12
|
+
...n6k,
|
|
13
|
+
h1: (props: ComponentProps<"h1">) => (
|
|
14
|
+
<h1 className="pb-4 text-4xl font-black tracking-tight" {...props} />
|
|
15
|
+
),
|
|
16
|
+
h2: (props: ComponentProps<"h2">) => (
|
|
17
|
+
<h2 className="pb-3 text-2xl font-bold tracking-tight" {...props} />
|
|
18
|
+
),
|
|
19
|
+
...components,
|
|
20
|
+
} as unknown as MDXComponents;
|
|
21
|
+
}
|
package/src/paths.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
export const SRC_DIR = path.dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
export const SERVE_ROOT = path.dirname(SRC_DIR);
|
|
7
|
+
export const SERVE_NODE_MODULES = path.join(SERVE_ROOT, "node_modules");
|
|
8
|
+
|
|
9
|
+
export function nearestNodeModules(fromDir: string): string | undefined {
|
|
10
|
+
let dir = fromDir;
|
|
11
|
+
for (;;) {
|
|
12
|
+
const candidate = path.join(dir, "node_modules");
|
|
13
|
+
if (existsSync(candidate)) return candidate;
|
|
14
|
+
const parent = path.dirname(dir);
|
|
15
|
+
if (parent === dir) return undefined;
|
|
16
|
+
dir = parent;
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/root.tsx
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { useState, type ReactNode } from "react";
|
|
2
|
+
import { duckdbWorkerUrl, wsWorkerUrl } from "virtual:n6k-workers";
|
|
3
|
+
import { duckdbBundle } from "virtual:n6k-duckdb-bundle";
|
|
4
|
+
import { N6kDuckDB, type DatabaseSpec, type SocketSpec } from "@n6k.io/db";
|
|
5
|
+
import { DuckDBProvider, QueryProvider } from "@n6k.io/ui/duckdb";
|
|
6
|
+
import { ConnectionLost, DuckDBConnectionGuard } from "@n6k.io/runtime";
|
|
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 {
|
|
19
|
+
const sockets: Record<string, SocketSpec> = {};
|
|
20
|
+
const databases: Record<string, DatabaseSpec> = {};
|
|
21
|
+
for (const alias of aliases) {
|
|
22
|
+
sockets[alias] = dataSocket(alias);
|
|
23
|
+
databases[alias] = { socket: alias };
|
|
24
|
+
}
|
|
25
|
+
return new N6kDuckDB({
|
|
26
|
+
bundle: duckdbBundle,
|
|
27
|
+
duckdbWorkerUrl,
|
|
28
|
+
wsWorkerUrl,
|
|
29
|
+
sockets,
|
|
30
|
+
databases,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default function Root({
|
|
35
|
+
children,
|
|
36
|
+
aliases = [],
|
|
37
|
+
}: {
|
|
38
|
+
children: ReactNode;
|
|
39
|
+
aliases?: string[];
|
|
40
|
+
}) {
|
|
41
|
+
const [duckdb] = useState(() => engine(aliases));
|
|
42
|
+
return (
|
|
43
|
+
<QueryProvider>
|
|
44
|
+
<DuckDBProvider connect={duckdb.connect} metadataFns={duckdb.metadataFns}>
|
|
45
|
+
<DuckDBConnectionGuard>{children}</DuckDBConnectionGuard>
|
|
46
|
+
<ConnectionLost duckdb={duckdb} />
|
|
47
|
+
</DuckDBProvider>
|
|
48
|
+
</QueryProvider>
|
|
49
|
+
);
|
|
50
|
+
}
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { ServerWebSocket } from "bun";
|
|
3
|
+
import type { Bundle } from "./bundle";
|
|
4
|
+
import { serveAsset } from "./assets";
|
|
5
|
+
|
|
6
|
+
export type ServeState = {
|
|
7
|
+
bundle: Bundle;
|
|
8
|
+
css: string;
|
|
9
|
+
data: Record<string, string>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type SocketData =
|
|
13
|
+
| { kind: "reload" }
|
|
14
|
+
| {
|
|
15
|
+
kind: "data";
|
|
16
|
+
url: string;
|
|
17
|
+
upstream?: WebSocket;
|
|
18
|
+
pending: Frame[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type Frame = string | Uint8Array<ArrayBuffer>;
|
|
22
|
+
|
|
23
|
+
const ISOLATION = {
|
|
24
|
+
"Cross-Origin-Opener-Policy": "same-origin",
|
|
25
|
+
"Cross-Origin-Embedder-Policy": "require-corp",
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const RELOAD_TOPIC = "reload";
|
|
29
|
+
|
|
30
|
+
function shell(title: string): string {
|
|
31
|
+
return `<!doctype html>
|
|
32
|
+
<html lang="en">
|
|
33
|
+
<head>
|
|
34
|
+
<meta charset="utf-8" />
|
|
35
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
36
|
+
<title>${Bun.escapeHTML(title)}</title>
|
|
37
|
+
<link rel="stylesheet" href="/styles.css" />
|
|
38
|
+
</head>
|
|
39
|
+
<body>
|
|
40
|
+
<div id="root"></div>
|
|
41
|
+
<script type="module" src="/index.js"></script>
|
|
42
|
+
<script>
|
|
43
|
+
(function connect() {
|
|
44
|
+
const ws = new WebSocket((location.protocol === "https:" ? "wss://" : "ws://") + location.host + "/_n6k/reload");
|
|
45
|
+
ws.onmessage = () => location.reload();
|
|
46
|
+
ws.onclose = () => setTimeout(connect, 1000);
|
|
47
|
+
})();
|
|
48
|
+
</script>
|
|
49
|
+
</body>
|
|
50
|
+
</html>`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function withIsolation(res: Response): Response {
|
|
54
|
+
for (const [k, v] of Object.entries(ISOLATION)) res.headers.set(k, v);
|
|
55
|
+
return res;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function contentType(file: string): string {
|
|
59
|
+
if (file.endsWith(".js")) return "text/javascript";
|
|
60
|
+
if (file.endsWith(".css")) return "text/css";
|
|
61
|
+
return "application/octet-stream";
|
|
62
|
+
}
|
|
63
|
+
|
|
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
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function startServer(mdxPath: string, port: number, state: ServeState) {
|
|
85
|
+
const title = path.basename(mdxPath);
|
|
86
|
+
return Bun.serve<SocketData>({
|
|
87
|
+
port,
|
|
88
|
+
async fetch(req, server) {
|
|
89
|
+
const { pathname } = new URL(req.url);
|
|
90
|
+
|
|
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;
|
|
102
|
+
return new Response("upgrade required", { status: 426 });
|
|
103
|
+
}
|
|
104
|
+
|
|
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
|
+
);
|
|
127
|
+
}
|
|
128
|
+
const asset = await serveAsset(pathname);
|
|
129
|
+
if (asset) return withIsolation(asset);
|
|
130
|
+
return withIsolation(new Response("not found", { status: 404 }));
|
|
131
|
+
},
|
|
132
|
+
websocket: {
|
|
133
|
+
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();
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function reloadClients(server: ReturnType<typeof startServer>): void {
|
|
156
|
+
server.publish(RELOAD_TOPIC, "reload");
|
|
157
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Provided by @n6k.io/build's bundleN6kWorkers / n6kWorkersPlugin.
|
|
2
|
+
declare module "virtual:n6k-workers" {
|
|
3
|
+
export const duckdbWorkerUrl: string;
|
|
4
|
+
export const wsWorkerUrl: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// Provided by @n6k.io/build's bundleN6kDuckDB / n6kDuckDBPlugin.
|
|
8
|
+
declare module "virtual:n6k-duckdb-bundle" {
|
|
9
|
+
export const duckdbBundle: {
|
|
10
|
+
mainModule: string;
|
|
11
|
+
mainWorker: string | null;
|
|
12
|
+
pthreadWorker: string | null;
|
|
13
|
+
};
|
|
14
|
+
}
|
package/src/watch.ts
ADDED