gantry-web 0.3.5 → 0.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.
- package/package.json +43 -43
- package/src/Await.tsx +85 -0
- package/src/ErrorScreen.tsx +98 -0
- package/src/ResizeFrame.tsx +43 -40
- package/src/app.tsx +368 -201
- package/src/bridge.ts +95 -94
- package/src/env.ts +71 -0
- package/src/errors.ts +214 -0
- package/src/index.ts +32 -22
- package/src/resources.ts +17 -0
- package/src/router.tsx +180 -160
- package/src/service.ts +7 -2
- package/src/socket.ts +227 -187
- package/src/styles.css +247 -0
- package/src/vite/index.js +57 -0
package/src/vite/index.js
CHANGED
|
@@ -19,6 +19,36 @@ import path from "node:path";
|
|
|
19
19
|
const VIRTUAL_ID = "virtual:gantry-app";
|
|
20
20
|
const RESOLVED_ID = "\0" + VIRTUAL_ID;
|
|
21
21
|
|
|
22
|
+
// Content types for the dev /resources/ middleware. Keep in rough sync
|
|
23
|
+
// with the extensions developers drop in resources/ (images, fonts,
|
|
24
|
+
// data). Unknown types fall back to a generic binary stream.
|
|
25
|
+
const RESOURCE_MIME = {
|
|
26
|
+
".png": "image/png",
|
|
27
|
+
".jpg": "image/jpeg",
|
|
28
|
+
".jpeg": "image/jpeg",
|
|
29
|
+
".gif": "image/gif",
|
|
30
|
+
".webp": "image/webp",
|
|
31
|
+
".svg": "image/svg+xml",
|
|
32
|
+
".ico": "image/x-icon",
|
|
33
|
+
".avif": "image/avif",
|
|
34
|
+
".woff": "font/woff",
|
|
35
|
+
".woff2": "font/woff2",
|
|
36
|
+
".ttf": "font/ttf",
|
|
37
|
+
".otf": "font/otf",
|
|
38
|
+
".json": "application/json",
|
|
39
|
+
".txt": "text/plain; charset=utf-8",
|
|
40
|
+
".css": "text/css; charset=utf-8",
|
|
41
|
+
".wasm": "application/wasm",
|
|
42
|
+
".mp3": "audio/mpeg",
|
|
43
|
+
".mp4": "video/mp4",
|
|
44
|
+
".webm": "video/webm",
|
|
45
|
+
".pdf": "application/pdf",
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
function resourceMime(file) {
|
|
49
|
+
return RESOURCE_MIME[path.extname(file).toLowerCase()] || "application/octet-stream";
|
|
50
|
+
}
|
|
51
|
+
|
|
22
52
|
/**
|
|
23
53
|
* @param {{ appRoot?: string, goPort?: number }} [opts]
|
|
24
54
|
* @returns {import("vite").Plugin}
|
|
@@ -218,6 +248,33 @@ export function gantry(opts = {}) {
|
|
|
218
248
|
},
|
|
219
249
|
|
|
220
250
|
configureServer(server) {
|
|
251
|
+
// Serve /resources/<path> live off <appRoot>/resources during dev,
|
|
252
|
+
// the same tree the Go side embeds and serves in production. Kept
|
|
253
|
+
// off the Go server (no proxy) so edits appear without a rebuild.
|
|
254
|
+
const resourcesDir = path.join(appRoot, "resources");
|
|
255
|
+
server.middlewares.use((req, res, next) => {
|
|
256
|
+
const url = (req.url || "").split("?")[0];
|
|
257
|
+
if (!url.startsWith("/resources/")) return next();
|
|
258
|
+
const rel = decodeURIComponent(url.slice("/resources/".length));
|
|
259
|
+
const abs = path.join(resourcesDir, rel);
|
|
260
|
+
// Contain the request inside resources/ (reject ../ traversal).
|
|
261
|
+
const rootPrefix = resourcesDir + path.sep;
|
|
262
|
+
if (abs !== resourcesDir && !abs.startsWith(rootPrefix)) {
|
|
263
|
+
res.statusCode = 403;
|
|
264
|
+
return res.end("Forbidden");
|
|
265
|
+
}
|
|
266
|
+
let stat;
|
|
267
|
+
try {
|
|
268
|
+
stat = fs.statSync(abs);
|
|
269
|
+
} catch {
|
|
270
|
+
return next();
|
|
271
|
+
}
|
|
272
|
+
if (!stat.isFile()) return next();
|
|
273
|
+
res.setHeader("Content-Type", resourceMime(abs));
|
|
274
|
+
res.setHeader("Content-Length", stat.size);
|
|
275
|
+
fs.createReadStream(abs).pipe(res);
|
|
276
|
+
});
|
|
277
|
+
|
|
221
278
|
server.watcher.add(appRoot);
|
|
222
279
|
const refresh = (file) => {
|
|
223
280
|
const f = file.split(path.sep).join("/");
|