@pritzlaff/nano-app 0.0.0-stage → 0.5.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.
Files changed (85) hide show
  1. package/AGENTS.md +66 -0
  2. package/LICENSE +21 -0
  3. package/README.md +6 -2
  4. package/app/app.css +160 -0
  5. package/app/bundle/nano-ui.js +2554 -0
  6. package/app/examples/auth/example.auth.js +6 -0
  7. package/app/examples/auth/nano.json +46 -0
  8. package/app/examples/chat/example.chat.js +6 -0
  9. package/app/examples/chat/example.messaging.js +3 -0
  10. package/app/examples/chat/nano.json +115 -0
  11. package/app/examples/drift/example.drift.js +3 -0
  12. package/app/examples/drift/nano.json +31 -0
  13. package/app/examples/kv/example.kv.js +3 -0
  14. package/app/examples/nano.json +356 -0
  15. package/app/examples/originkey/_nano/bundle/nano-ui.js +212 -0
  16. package/app/examples/originkey/_nano/sandbox/boot.js +1 -0
  17. package/app/examples/originkey/_nano/shared/chunks/nano-core-2gg2vg5g.js +2 -0
  18. package/app/examples/originkey/_nano/shared/chunks/nano-core-5h5dssvv.js +3 -0
  19. package/app/examples/originkey/_nano/shared/chunks/nano-core-72b5e6vd.js +17 -0
  20. package/app/examples/originkey/_nano/shared/chunks/nano-core-87v2yejk.js +2 -0
  21. package/app/examples/originkey/_nano/shared/chunks/nano-core-hm29e227.js +7 -0
  22. package/app/examples/originkey/_nano/shared/chunks/nano-core-jtgk4ch8.js +2 -0
  23. package/app/examples/originkey/_nano/shared/chunks/nano-core-mqaje7vc.js +2 -0
  24. package/app/examples/originkey/_nano/shared/chunks/nano-core-mxgap7rs.js +2 -0
  25. package/app/examples/originkey/_nano/shared/chunks/nano-core-qy0mwf7y.js +1 -0
  26. package/app/examples/originkey/_nano/shared/chunks/nano-core-wanb7xw1.js +1 -0
  27. package/app/examples/originkey/_nano/shared/lit-class-map.js +1 -0
  28. package/app/examples/originkey/_nano/shared/lit-decorators.js +1 -0
  29. package/app/examples/originkey/_nano/shared/lit-directive.js +1 -0
  30. package/app/examples/originkey/_nano/shared/lit-if-defined.js +1 -0
  31. package/app/examples/originkey/_nano/shared/lit-ref.js +1 -0
  32. package/app/examples/originkey/_nano/shared/lit-repeat.js +1 -0
  33. package/app/examples/originkey/_nano/shared/lit-style-map.js +1 -0
  34. package/app/examples/originkey/_nano/shared/lit.js +1 -0
  35. package/app/examples/originkey/_nano/shared/nano-core.js +1 -0
  36. package/app/examples/originkey/_nano/shared/nano-elements.js +280 -0
  37. package/app/examples/originkey/_nano/shared/typebox-value.js +1 -0
  38. package/app/examples/originkey/_nano/shared/typebox.js +1 -0
  39. package/app/examples/originkey/example.originkey.js +6 -0
  40. package/app/examples/originkey/frame.html +1 -0
  41. package/app/examples/originkey/nano.json +50 -0
  42. package/app/examples/raft/example.raft.js +30 -0
  43. package/app/examples/raft/nano.json +74 -0
  44. package/app/examples/slice/nano.json +63 -0
  45. package/app/examples/slice/shout.js +14 -0
  46. package/app/examples/template/acme.template.js +20 -0
  47. package/app/examples/template/index.js +241 -0
  48. package/app/examples/template/nano.json +35 -0
  49. package/app/examples/todo/example.todo.js +8 -0
  50. package/app/examples/todo/nano.json +77 -0
  51. package/app/examples/typing/nano.json +33 -0
  52. package/app/favicon.svg +1 -0
  53. package/app/index.html +152 -0
  54. package/app/main.js +15908 -0
  55. package/app/react/index.html +50 -0
  56. package/app/react/main.js +9 -0
  57. package/app/sandbox/boot.js +980 -0
  58. package/app/shared/chunks/nano-core-296y9rgd.js +4321 -0
  59. package/app/shared/chunks/nano-core-2zz7d74m.js +16 -0
  60. package/app/shared/chunks/nano-core-35ryhkm1.js +33 -0
  61. package/app/shared/chunks/nano-core-5a3r312d.js +866 -0
  62. package/app/shared/chunks/nano-core-89mtxedm.js +186 -0
  63. package/app/shared/chunks/nano-core-bf13r20v.js +77 -0
  64. package/app/shared/chunks/nano-core-g3414rnc.js +248 -0
  65. package/app/shared/chunks/nano-core-hyg0qdwg.js +547 -0
  66. package/app/shared/chunks/nano-core-sxgrxy03.js +53 -0
  67. package/app/shared/chunks/nano-core-wg5c961d.js +4564 -0
  68. package/app/shared/lit-class-map.js +60 -0
  69. package/app/shared/lit-decorators.js +228 -0
  70. package/app/shared/lit-directive.js +11 -0
  71. package/app/shared/lit-if-defined.js +10 -0
  72. package/app/shared/lit-ref.js +12 -0
  73. package/app/shared/lit-repeat.js +141 -0
  74. package/app/shared/lit-style-map.js +67 -0
  75. package/app/shared/lit.js +47 -0
  76. package/app/shared/nano-core.js +2516 -0
  77. package/app/shared/nano-elements.js +1178 -0
  78. package/app/shared/typebox-value.js +78 -0
  79. package/app/shared/typebox.js +580 -0
  80. package/app/shared/versions.json +6 -0
  81. package/bin/nano-app.ts +34 -0
  82. package/package.json +22 -4
  83. package/src/events.ts +11 -0
  84. package/src/serve.ts +68 -0
  85. package/src/watch.ts +27 -0
package/src/events.ts ADDED
@@ -0,0 +1,11 @@
1
+ /** The set of open server-sent-event streams: `subscribe` for the server, `tell` for the watcher. A closed stream drops out. */
2
+ export function events() {
3
+ const open = new Set<ReadableStreamDefaultController<Uint8Array>>();
4
+ return {
5
+ subscribe(c: ReadableStreamDefaultController<Uint8Array>) { open.add(c); },
6
+ tell(event: string) {
7
+ const bytes = new TextEncoder().encode(`data: ${event.replace(/\n/g, ' ')}\n\n`);
8
+ for (const c of open) try { c.enqueue(bytes); } catch { open.delete(c); }
9
+ },
10
+ };
11
+ }
package/src/serve.ts ADDED
@@ -0,0 +1,68 @@
1
+ /**
2
+ * The nano-app CLI's server: the app, the project folder and the build folder on loopback, one writable file, rebuild events.
3
+ * GET precedence: the app first (index.html at `/`, and only files the app has, so a project file can never shadow `/main.js`),
4
+ * then the project folder, then `out`. Every candidate is resolved and must stay inside its root.
5
+ */
6
+ import { realpath, rename, stat, unlink, writeFile } from 'node:fs/promises';
7
+ import { basename, dirname, extname, join, resolve, sep } from 'node:path';
8
+
9
+ interface Options {
10
+ app: string; project: string; out: string;
11
+ /** The project-relative URL path of the opened file, e.g. `/nano.json`: the page's meta names it and it is the only PUT target. */
12
+ file: string;
13
+ port: () => number;
14
+ events: { subscribe(controller: ReadableStreamDefaultController<Uint8Array>): void };
15
+ }
16
+
17
+ const isFile = (p: string) => stat(p).then(s => s.isFile(), () => false);
18
+ /** `root`/`path` if it stays inside `root`, else undefined. */
19
+ const inside = (root: string, path: string) => { const f = resolve(root, '.' + path); return f.startsWith(resolve(root) + sep) ? f : undefined; };
20
+ /** `inside`, then the same check on the real path, so a symlink cannot lead out of `root`. */
21
+ const safe = async (root: string, path: string) => {
22
+ const f = inside(root, path);
23
+ if (!f) return undefined;
24
+ try { const real = await realpath(f); return real.startsWith(await realpath(root) + sep) ? real : undefined; } catch { return undefined; }
25
+ };
26
+ const text = (s: string, status: number, type = 'text/plain') => new Response(s, { status, headers: { 'content-type': type } });
27
+
28
+ export function handler(o: Options): (req: Request) => Promise<Response> {
29
+ return async req => {
30
+ const url = new URL(req.url);
31
+ const p0 = req.headers.get('host'), port = o.port();
32
+ if (!p0 || ![`127.0.0.1:${port}`, `localhost:${port}`, `[::1]:${port}`].includes(p0)) return text('Forbidden host', 403);
33
+ let path: string;
34
+ try { path = decodeURIComponent(url.pathname); } catch { return text('Not found', 404); }
35
+ if (path.includes('\0') || path.split('/').includes('..')) return text('Not found', 404);
36
+
37
+ if (req.method === 'PUT') {
38
+ // No preflight is ever answered, so a cross-site page cannot get here.
39
+ const target = path === o.file ? await safe(o.project, path) : undefined;
40
+ if (!target || !(await isFile(target))) return text('Not writable', 403);
41
+ const tmp = join(dirname(target), `.${basename(target)}.${process.pid}.${Date.now()}.tmp`);
42
+ try {
43
+ await writeFile(tmp, new Uint8Array(await req.arrayBuffer()));
44
+ await rename(tmp, target); // whole file or nothing
45
+ return new Response(null, { status: 204 });
46
+ } catch (e) { await unlink(tmp).catch(() => {}); return text(e instanceof Error ? e.message : String(e), 500); }
47
+ }
48
+ if (req.method !== 'GET' && req.method !== 'HEAD') return text('Method not allowed', 405);
49
+
50
+ if (path === '/__nano-app/events') {
51
+ const body = new ReadableStream<Uint8Array>({ start: c => o.events.subscribe(c) });
52
+ return new Response(body, { headers: { 'content-type': 'text/event-stream', 'cache-control': 'no-store' } });
53
+ }
54
+ if (path === '/' || path === '/index.html') {
55
+ const html = await Bun.file(join(o.app, 'index.html')).text();
56
+ const meta = `<meta name="nano-app-file" content="${o.file.replace(/&/g, '&amp;').replace(/"/g, '&quot;')}">`;
57
+ return text(/<head[^>]*>/i.test(html) ? html.replace(/<head[^>]*>/i, m => m + meta) : meta + html, 200, 'text/html; charset=utf-8');
58
+ }
59
+ for (const root of [o.app, o.project, o.out]) {
60
+ const f = await safe(root, path);
61
+ if (f && await isFile(f)) {
62
+ const type = extname(f) === '.json' ? 'application/json' : Bun.file(f).type;
63
+ return new Response(Bun.file(f), { headers: { 'content-type': type, 'cache-control': 'no-store' } });
64
+ }
65
+ }
66
+ return text('Not found', 404);
67
+ };
68
+ }
package/src/watch.ts ADDED
@@ -0,0 +1,27 @@
1
+ import { existsSync, watch as fsWatch } from 'node:fs';
2
+ import { join } from 'node:path';
3
+
4
+ /**
5
+ * Run `build` now, then again after each change under `<project>/src` (recursive, 200 ms of quiet; one build at a time, one more if a
6
+ * change landed meanwhile; `first` settles when the first build has), telling `rebuilt` or `build-failed <first stderr line>` (the latter also logged). Without a `src/` it only builds once.
7
+ */
8
+ export function watch(project: string, build: string[], notify: (event: string) => void): { stop: () => void; first: Promise<void> } {
9
+ const tell = (e: string) => { if (e.startsWith('build-failed')) console.error(`nano-app: ${e}`); notify(e) }
10
+ let timer: ReturnType<typeof setTimeout> | undefined, running = false, again = false;
11
+ const run = async (): Promise<void> => {
12
+ if (running) { again = true; return; }
13
+ running = true;
14
+ try {
15
+ const p = Bun.spawn(build, { cwd: project, stdout: 'ignore', stderr: 'pipe' });
16
+ const [err, code] = await Promise.all([new Response(p.stderr).text(), p.exited]);
17
+ tell(code === 0 ? 'rebuilt' : `build-failed ${err.trim().split('\n')[0] ?? ''}`.trimEnd());
18
+ } catch (e) { tell(`build-failed ${e instanceof Error ? e.message : String(e)}`); }
19
+ running = false;
20
+ if (again) { again = false; void run(); }
21
+ };
22
+ const first = run();
23
+ const src = join(project, 'src');
24
+ if (!existsSync(src)) { console.log(`nano-app: no ${src}, not watching for changes`); return { stop: () => {}, first }; }
25
+ const w = fsWatch(src, { recursive: true }, () => { clearTimeout(timer); timer = setTimeout(run, 200); });
26
+ return { stop: () => { clearTimeout(timer); w.close(); }, first };
27
+ }