Skip to content

Commit

Permalink
Merge pull request #91 from unyt-org/refactor-network
Browse files Browse the repository at this point in the history
Refactor network
  • Loading branch information
benStre authored Feb 10, 2024
2 parents dffafbe + 6602584 commit 633abe3
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 722 deletions.
2 changes: 1 addition & 1 deletion docs/manual/07 Rendering Methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class ButtonComponent extends Component {

The frontend rendering mode can be used to render parts of the UI only on the frontend.

> [!NODE]
> [!NOTE]
> If you want to render a page completely on the frontend, we recommend to just put the page in the frontend entrypoint.
> The frontend rendering mode only makes sense when you want to render parts of the UI in the backend, and hydrate other sections with > frontend-rendered content.
Expand Down
1 change: 1 addition & 0 deletions docs/manual/08 Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Experimental features are subject to change and might be enabled by default in f

* *embedded-reactivity*: Enables compile-time reactivity features for JSX templates and the `$()` syntax
* *protect-pointers*: Sets the `PROTECT_POINTERS` DATEX Runtime flag, which disables pointer read/write access for remote endpoints per default. Backend exports and pointers returned from backend functions are still publicly accessible by all endpoints.
* *indirect-references*: Sets the `INDIRECT_REFERENCES` DATEX Runtime, which enables for indirect references to pointers from other pointers.


### Paths
Expand Down
2 changes: 1 addition & 1 deletion src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class App {
this.frontends = frontends;

// log enabled experimental features
const allowedFeatures = ['embedded-reactivity', 'protect-pointers'];
const allowedFeatures = ['embedded-reactivity', 'protect-pointers', 'indirect-references'];
for (const feature of this.options.experimentalFeatures) {
if (allowedFeatures.includes(feature)) logger.info(`experimental feature "${feature}" enabled`)
else logger.error(`unknown experimental feature "${feature}"`)
Expand Down
2 changes: 2 additions & 0 deletions src/app/default-domain.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Datex } from "datex-core-legacy/mod.ts";

export function getDefaultDomainPrefix() {
return Datex.Unyt.endpointDomains()[0] ?? '';
}
20 changes: 15 additions & 5 deletions src/app/start-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ import { FrontendManager } from "./frontend-manager.ts";
import { Path } from "../utils/path.ts";
import { convertToWebPath } from "./convert-to-web-path.ts";
import { getDirType } from "./utils.ts";
import { WebSocketServerInterface } from "datex-core-legacy/network/communication-interfaces/websocket-server-interface.ts"
import { HTTPServerInterface } from "datex-core-legacy/network/communication-interfaces/http-server-interface.ts"
import { communicationHub } from "datex-core-legacy/network/communication-hub.ts";

const logger = new Datex.Logger("UIX App");

/**
* Start the UIX app on the backendase_url
*/
export async function startApp(app: {domains:string[], hostDomains: string[], options?:normalizedAppOptions, base_url?: URL}, options:appOptions = {}, original_base_url?:string|URL) {

// enable network interface
await import("../server/network-interface.ts");

const frontends = new Map<string, FrontendManager>()

// prevent circular dependency problems
Expand All @@ -27,7 +36,7 @@ export async function startApp(app: {domains:string[], hostDomains: string[], op

// enable experimental features
if (app.options?.experimentalFeatures.includes("protect-pointers")) Datex.Runtime.OPTIONS.PROTECT_POINTERS = true;

if (app.options?.experimentalFeatures.includes("indirect-references")) Datex.Runtime.OPTIONS.INDIRECT_REFERENCES = true;

// for unyt log
Datex.Unyt.setAppInfo({name:nOptions.name, version:nOptions.version, stage:stage, host:Deno.env.has("UIX_HOST_ENDPOINT") && Deno.env.get("UIX_HOST_ENDPOINT")!.startsWith("@") ? f(Deno.env.get("UIX_HOST_ENDPOINT") as any) : undefined, dynamicData: app})
Expand Down Expand Up @@ -79,12 +88,13 @@ export async function startApp(app: {domains:string[], hostDomains: string[], op
frontends.set(dir.toString(), frontend_manager);
}


// expose DATEX ws server node, shown in /.dx
// TODO: option to disable direct backend ws connection
if (server && endpoint_config.ws_relay !== false) {
const DatexServer = (await import("../server/datex_server.ts")).DatexServer
DatexServer.addInterfaces(["websocket", "webpush"], server);

// add WebSocket and HTTP server interfaces
await communicationHub.addInterface(new WebSocketServerInterface(server));
await communicationHub.addInterface(new HTTPServerInterface(server));

// also add custom .dx file
const nodes = new Map<Datex.Endpoint, {channels:Record<string,string>,keys:[ArrayBuffer, ArrayBuffer]}>();
nodes.set(Datex.Runtime.endpoint, {
Expand Down
1 change: 1 addition & 0 deletions src/html/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ export async function generateHTMLPage({
// files += `\nDatex.MessageLogger.enable();`

if (app.options?.experimentalFeatures.includes("protect-pointers")) files += indent(4) `\nDatex.Runtime.OPTIONS.PROTECT_POINTERS = true;`
if (app.options?.experimentalFeatures.includes("indirect-references")) files += indent(4) `\nDatex.Runtime.OPTIONS.INDIRECT_REFERENCES = true;`

// set app info

Expand Down
2 changes: 2 additions & 0 deletions src/html/unsafe-html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Datex } from "datex-core-legacy/mod.ts";
import { domUtils } from "../app/dom-context.ts";
import { HTMLElement } from "../uix-dom/dom/deno-dom/src/api.ts";


export function unsafeHTML(html:string, content?: Datex.RefOrValue<HTMLElement>|(Datex.RefOrValue<HTMLElement>)[]) {
return domUtils.createHTMLElement(html, content)
Expand Down
6 changes: 4 additions & 2 deletions src/hydration/hydrate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { app } from "../app/app.ts";
import { querySelector, querySelectorAll } from "../uix-dom/dom/shadow_dom_selector.ts";
import { domUtils } from "../app/dom-context.ts";
import { logger } from "../utils/global-values.ts";

export async function hydrate() {
const nodes = querySelectorAll("[uix-dry]");
Expand All @@ -14,14 +15,15 @@ export async function hydrate() {
else return true;
})
.map(([id]) => "$" + id);

const resolvedPtrs = await datex<any[]>(`${app.backend} :: [${ptrs.join(",")}]`);

for (const [index, resolved] of Object.entries(resolvedPtrs)) {
// replace non-node pointer - this currently only happens with uix-placeholder elements
if (!(resolved instanceof Node)) {
const node = querySelector(`[uix-ptr="${ptrs[Number(index)].substring(1)}"]`)
if (!node) console.error("hydrate: node not found for uix-ptr " + ptrs[Number(index)])
else domUtils.replaceWith(node, resolved)
if (!node) logger.debug("hydrate: node for uix-ptr " + ptrs[Number(index)] + " no longer exists")
else domUtils.replaceWith(node, resolved, true)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/routing/frontend-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export namespace Routing {

// no content for path found after initial loading
if (!frontend_available && !backend_available) {
displayError("No content", `Route resolved to null on the ${backend_entrypoint?'backend':''}${(backend_entrypoint&&frontend_entrypoint?' and ': '')}${frontend_entrypoint?'frontend':''}`)
if (!backend_entrypoint && !frontend_entrypoint) displayError("No entrypoint", `No backend or frontend entrypoint (default export) available.`)
else displayError("No content", `Route resolved to null on the ${backend_entrypoint?'backend':''}${(backend_entrypoint&&frontend_entrypoint?' and ': '')}${frontend_entrypoint?'frontend':''}`)
}
}

Expand Down
Loading

0 comments on commit 633abe3

Please sign in to comment.