zelune 0.0.0-stage → 1.4.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.
@@ -0,0 +1,154 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import type { IncomingMessage, ServerResponse } from 'node:http';
4
+ import type { Plugin } from 'vite';
5
+ import {
6
+ discoverScreens,
7
+ inspectElement,
8
+ patchElement,
9
+ undoLastPatch,
10
+ redoLastPatch,
11
+ getHistoryStatus,
12
+ type PatchPayload,
13
+ } from './astEngine';
14
+
15
+ async function readJsonBody<T>(req: IncomingMessage): Promise<T> {
16
+ return new Promise((resolve, reject) => {
17
+ let raw = '';
18
+ req.on('data', (chunk) => {
19
+ raw += chunk.toString();
20
+ if (raw.length > 1_000_000) {
21
+ reject(new Error('Request payload too large'));
22
+ }
23
+ });
24
+ req.on('end', () => {
25
+ try {
26
+ resolve(raw ? (JSON.parse(raw) as T) : ({} as T));
27
+ } catch (err) {
28
+ reject(err);
29
+ }
30
+ });
31
+ req.on('error', reject);
32
+ });
33
+ }
34
+
35
+ function sendJson(res: ServerResponse, status: number, payload: unknown) {
36
+ res.statusCode = status;
37
+ res.setHeader('Content-Type', 'application/json; charset=utf-8');
38
+ res.end(JSON.stringify(payload));
39
+ }
40
+
41
+ export function viteCanvasApiMiddleware(workspaceRoot: string): Plugin {
42
+ const layoutFilePath = path.resolve(workspaceRoot, '.canvas-layout.json');
43
+
44
+ async function resolveConnectedProjectName(): Promise<string> {
45
+ const dirName = path.basename(workspaceRoot);
46
+ try {
47
+ const appJsonRaw = await fs.readFile(path.join(workspaceRoot, 'app.json'), 'utf-8');
48
+ const appJson = JSON.parse(appJsonRaw) as {
49
+ expo?: { name?: string };
50
+ name?: string;
51
+ };
52
+ if (appJson?.expo?.name) return appJson.expo.name;
53
+ if (appJson?.name) return appJson.name;
54
+ } catch {
55
+ // No app.json present
56
+ }
57
+ try {
58
+ const pkgRaw = await fs.readFile(path.join(workspaceRoot, 'package.json'), 'utf-8');
59
+ const pkg = JSON.parse(pkgRaw) as { name?: string };
60
+ if (pkg?.name && pkg.name.toLowerCase() !== 'zelune') {
61
+ return pkg.name;
62
+ }
63
+ } catch {
64
+ // Fallback to directory name
65
+ }
66
+ return dirName;
67
+ }
68
+
69
+ return {
70
+ name: 'vite-canvas-api-middleware',
71
+ configureServer(server) {
72
+ server.middlewares.use(async (req, res, next) => {
73
+ if (!req.url || !req.url.startsWith('/api/')) {
74
+ return next();
75
+ }
76
+
77
+ const urlObj = new URL(req.url, 'http://127.0.0.1');
78
+ const pathname = urlObj.pathname;
79
+
80
+ try {
81
+ // GET /api/screens
82
+ if (req.method === 'GET' && pathname === '/api/screens') {
83
+ const [screens, projectName] = await Promise.all([
84
+ discoverScreens(workspaceRoot),
85
+ resolveConnectedProjectName(),
86
+ ]);
87
+ const history = getHistoryStatus();
88
+ return sendJson(res, 200, { ok: true, projectName, screens, history });
89
+ }
90
+
91
+ // GET /api/inspect?file=...&astPath=...&line=...
92
+ if (req.method === 'GET' && pathname === '/api/inspect') {
93
+ const file = urlObj.searchParams.get('file') ?? '';
94
+ const astPath = urlObj.searchParams.get('astPath') ?? '';
95
+ const lineParam = urlObj.searchParams.get('line');
96
+ const line = lineParam ? Number.parseInt(lineParam, 10) : undefined;
97
+
98
+ const element = await inspectElement(workspaceRoot, { file, astPath, line });
99
+ const history = getHistoryStatus();
100
+ return sendJson(res, 200, { ok: true, element, history });
101
+ }
102
+
103
+ // POST /api/patch
104
+ if (req.method === 'POST' && pathname === '/api/patch') {
105
+ const body = await readJsonBody<PatchPayload>(req);
106
+ const result = await patchElement(workspaceRoot, body);
107
+ return sendJson(res, 200, {
108
+ ok: true,
109
+ element: result.element,
110
+ history: {
111
+ undoAvailable: result.undoAvailable,
112
+ redoAvailable: result.redoAvailable,
113
+ },
114
+ });
115
+ }
116
+
117
+ // POST /api/undo
118
+ if (req.method === 'POST' && pathname === '/api/undo') {
119
+ const result = await undoLastPatch(workspaceRoot);
120
+ return sendJson(res, 200, { ok: true, ...result });
121
+ }
122
+
123
+ // POST /api/redo
124
+ if (req.method === 'POST' && pathname === '/api/redo') {
125
+ const result = await redoLastPatch(workspaceRoot);
126
+ return sendJson(res, 200, { ok: true, ...result });
127
+ }
128
+
129
+ // GET /api/layout
130
+ if (req.method === 'GET' && pathname === '/api/layout') {
131
+ try {
132
+ const raw = await fs.readFile(layoutFilePath, 'utf-8');
133
+ return sendJson(res, 200, { ok: true, layout: JSON.parse(raw) });
134
+ } catch {
135
+ return sendJson(res, 200, { ok: true, layout: null });
136
+ }
137
+ }
138
+
139
+ // POST /api/layout
140
+ if (req.method === 'POST' && pathname === '/api/layout') {
141
+ const body = await readJsonBody<Record<string, unknown>>(req);
142
+ await fs.writeFile(layoutFilePath, JSON.stringify(body, null, 2), 'utf-8');
143
+ return sendJson(res, 200, { ok: true });
144
+ }
145
+
146
+ return sendJson(res, 404, { ok: false, error: `Unknown API route: ${pathname}` });
147
+ } catch (err) {
148
+ const message = err instanceof Error ? err.message : String(err);
149
+ return sendJson(res, 400, { ok: false, error: message });
150
+ }
151
+ });
152
+ },
153
+ };
154
+ }