-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
115 lines (101 loc) · 3.48 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2024 the Marveluzz authors. All rights reserved. MIT license.
/**
* A basic server-side only reactive webframework that is simple and easy to use.
* @module
*
* Marveluzz is derived from the Python library [PyWebIO](https://www.pyweb.io/) and ported to server-side JS.
*
* Here is how simple an webapp with server-side interaction can look like:
*
* ```ts
* import { handleRequest, Page, Widgets } from "jsr:@marveluzz-ui/marveluzz-ui";
*
*const port: number = 8080;
*
*function callback()
*{
* console.log("Thankyou for clicking!");
*}
*
*async function pageApp(page: Page) {
* page.add(Widgets.put_text("Hello World!"));
* page.add(Widgets.put_button("Click me!", callback));
* await page.wait_for_close();
*}
*
*const handler = async (req: Request): Promise<Response> => {
* return handleRequest(req, "Simple Test App", pageApp);
*}
*
*Deno.serve({ port }, handler);
*```
*
*/
import { join, fromFileUrl } from "@std/path";
import { Page } from "./page.ts";
import { Widgets } from "./widgets.ts"
export { Page, Widgets }
const baseUrl = new URL(".", import.meta.url);
let indexHtmlTemplate: string;
if (baseUrl.protocol === "file:") {
const __dirname = fromFileUrl(baseUrl);
const filePath = join(__dirname, "assets", "index.html.template");
const decoder = new TextDecoder("utf-8");
indexHtmlTemplate = decoder.decode(await Deno.readFile(filePath));
} else {
const response = await fetch(new URL("assets/index.html.template", import.meta.url).href);
indexHtmlTemplate = await response.text();
}
/**
* Routine used to handle HTTP request interacting with the Marveluzz UI.
* @param request
* The data structure containing the HTTP request.
* @param pageTitle
* Title shown by the browser tab defined by the HTML tag <title>.
* @param pageCallback
* The callback function implementing the Marveluzz UI based app.
* @returns
* The data structure containing the HTTP response.
*/
export const handleRequest = async (request: Request, pageTitle: string, pageCallback: (page: Page) => Promise<void>): Promise<Response> => {
const url = new URL(request.url);
if (url.searchParams.get("app") === "index") {
return wsHandler(request, pageCallback);
} else {
const indexHtmlContent = indexHtmlTemplate.replace("__MARVELUZZ_UI__PAGE_TITLE__", pageTitle);
return new Promise((resolve, reject) => {
resolve(new Response(indexHtmlContent, {
status: 200,
headers: {
"content-type": "text/html; charset=utf-8",
},
}));
});
}
}
/**
* Internal handler for the Websocket connection initiated by the client side JS framework.
* @param request
* The HTTP request
* @param pageCallback
* Callback to the Marveluzz UI based app
* @returns
* The HTTP respones
*/
async function wsHandler(request: Request, pageCallback: (page: Page) => Promise<void>): Promise<Response> {
const { response, socket } = Deno.upgradeWebSocket(request);
const page = new Page(socket);
socket.onopen = async () => {
console.log("WebSocket connection opened");
await pageCallback(page); // This will access the correct socket // catch errors / needs to be tested
socket.close();
console.log("Page closed!");
};
socket.onclose = () => {
console.log("WebSocket connection closed");
};
socket.onerror = (err) => {
console.error("WebSocket error:", err);
};
return response;
}