diff --git a/README.md b/README.md index fa37fd3..5e2b1b0 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ The UDN project aims to provide decentralized and localhost-able infrastructure - [Getting Started](#getting-started) - [Configuration](#configuration) - [TLS](#tls) - - [Connecting multiple servers](#connecting-multiple-servers) + - [Connecting Multiple Servers](#connecting-multiple-servers) - [App Developer's Guide](#app-developers-guide) - [Guidelines](#guidelines) - [The Recommended Way](#the-recommended-way) @@ -204,7 +204,7 @@ In local networks, you will likely need co create a self-signed certificate. Thi 2. Create the certificate with `openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 365`. This command will ask you a few questions and create the files required by your server. 3. Provide the passphrase via the `TLS_PASSPHRASE` environment variable (i.e. `export TLS_PASSPHRASE="my-securepass-phrase"`); -## Connecting multiple servers +## Connecting Multiple Servers 1. Add the WebSocket addresses of servers you want to connect to the `connectedServers` array in the configuration file. 2. Add secondary channels to the `subscribedChannels` array in the configuration file. diff --git a/static/bun.lockb b/static/bun.lockb index 584bcec..1d2a1fd 100755 Binary files a/static/bun.lockb and b/static/bun.lockb differ diff --git a/static/dist/index.js b/static/dist/index.js index d79a859..5d89646 100644 --- a/static/dist/index.js +++ b/static/dist/index.js @@ -231,16 +231,18 @@ // node_modules/udn-frontend/index.ts var UDNFrontend = class { ws; - // handlers + // HANDLERS connectionHandler = () => { }; disconnectionHandler = () => { }; messageHandler = (data) => { }; - mailboxHandler = (mailboxId2) => { + mailboxHandler = (mailboxId) => { }; - // init + mailboxConnectionHandler = (mailboxId) => { + }; + // INIT set onconnect(handler) { this.connectionHandler = handler; } @@ -253,14 +255,18 @@ set onmailbox(handler) { this.mailboxHandler = handler; } - // utility methods + set onmailboxconnect(handler) { + this.mailboxConnectionHandler = handler; + } + // UTILITY METHODS send(messageObject) { if (this.ws == void 0) return false; const messageString = JSON.stringify(messageObject); this.ws.send(messageString); return true; } - // public methods + // PUBLIC METHODS + // connection connect(address) { this.disconnect(); this.ws = new WebSocket(address); @@ -271,6 +277,8 @@ const data = JSON.parse(dataString); if (data.assignedMailboxId) { return this.mailboxHandler(data.assignedMailboxId); + } else if (data.connectedMailboxId) { + return this.mailboxConnectionHandler(data.assignedMailboxId); } else { this.messageHandler(data); } @@ -279,6 +287,7 @@ disconnect() { this.ws?.close(); } + // message sendMessage(channel, body) { const messageObject = { messageChannel: channel, @@ -286,6 +295,7 @@ }; return this.send(messageObject); } + // subscription subscribe(channel) { const messageObject = { subscribeChannel: channel @@ -298,15 +308,22 @@ }; return this.send(messageObject); } + // mailbox requestMailbox() { const messageObject = { requestingMailboxSetup: true }; return this.send(messageObject); } - accessMailbox(mailboxId2) { + connectMailbox(mailboxId) { const messageObject = { - requestedMailbox: mailboxId2 + requestedMailbox: mailboxId + }; + return this.send(messageObject); + } + deleteMailbox(mailboxId) { + const messageObject = { + deletingMailbox: mailboxId }; return this.send(messageObject); } @@ -315,7 +332,9 @@ // src/translations.tsx var staticTextEnglish = { mailbox: "Mailbox", - requestMailbox: "Request Mailbox", + requestMailbox: "Request", + deleteMailbox: "Delete", + mailboxConnected: "Mailbox connected", channel: "Channel", channel_placeholder: "my-channel", disconnected: "Server disconnected", @@ -339,7 +358,9 @@ en: staticTextEnglish, es: { mailbox: "Buz\xF3n", - requestMailbox: "Solicitar buz\xF3n", + requestMailbox: "Solicitar", + deleteMailbox: "Eliminar", + mailboxConnected: "Buz\xF3n conectado", channel: "Canal", channel_placeholder: "mi-canal", disconnected: "Sin coneci\xF3n", @@ -361,7 +382,9 @@ }, de: { mailbox: "Briefkasten", - requestMailbox: "Briefkasten beantragen", + requestMailbox: "Beantragen", + deleteMailbox: "L\xF6schen", + mailboxConnected: "Briefkasten verbunden", channel: "Kanal", channel_placeholder: "mein-kanal", disconnected: "Verbindung getrennt", @@ -398,35 +421,9 @@ } id = UUID(); }; - var UDN = new UDNFrontend(); - UDN.onconnect = () => { - isDisconnected.value = false; - updateStats(); - if (mailboxId.value != "") { - UDN.accessMailbox(mailboxId.value); - } - }; - UDN.onmessage = (data) => { - lastReceivedMessage.value = JSON.stringify(data, null, 4); - const { messageChannel, messageBody } = data; - if (messageChannel && messageBody) { - const messageObject = new Message(messageChannel, messageBody); - if (messageBody) messages.add(messageObject); - } - }; - UDN.onmailbox = (id) => { - mailboxId.value = id; - }; - UDN.ondisconnect = () => { - isDisconnected.value = true; - setTimeout(connect, 2e3); - }; - function connect() { - UDN.connect(`wss://${window.location.host}`); - } - connect(); var isDisconnected = new State(true); - var mailboxId = restoreState("mailbox-id", ""); + var storedMailboxId = restoreState("mailbox-id", ""); + var isMailboxDisonnected = new State(true); var messages = new ListState(); var lastReceivedMessage = new State( getText("noMessagesReceived") @@ -451,6 +448,42 @@ [newMessageChannel, newMessageBody], () => newMessageChannel.value == "" || newMessageBody.value == "" ); + var UDN = new UDNFrontend(); + UDN.onconnect = () => { + isDisconnected.value = false; + updateStats(); + if (storedMailboxId.value != "") { + UDN.connectMailbox(storedMailboxId.value); + } + }; + UDN.onmessage = (data) => { + lastReceivedMessage.value = JSON.stringify(data, null, 4); + const { messageChannel, messageBody } = data; + if (messageChannel && messageBody) { + const messageObject = new Message(messageChannel, messageBody); + if (messageBody) messages.add(messageObject); + } + }; + UDN.onmailbox = (id) => { + console.log(id); + storedMailboxId.value = id; + UDN.connectMailbox(id); + }; + UDN.onmailboxconnect = (id) => { + console.log(id); + isMailboxDisonnected.value = false; + }; + UDN.ondisconnect = () => { + isDisconnected.value = true; + isMailboxDisonnected.value = true; + setTimeout(connect, 1e3); + }; + function connect() { + UDN.connect( + `${window.location.protocol.replace("http", "ws")}//${window.location.host}` + ); + } + connect(); function subscribe() { UDN.subscribe(subscriptionChannel.value); } @@ -463,8 +496,13 @@ newMessageBody.value = ""; } function requestMailbox() { + deleteMailbox(); UDN.requestMailbox(); } + function deleteMailbox() { + console.log("test"); + UDN.deleteMailbox(storedMailboxId.value); + } // src/infoScreen.tsx function InfoScreen() { @@ -473,7 +511,24 @@ // src/mainScreen.tsx function MainScreen() { - return /* @__PURE__ */ createElement("article", { id: "main-screen" }, /* @__PURE__ */ createElement("header", null, getText("settings")), /* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("div", { class: "tile width-input" }, /* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("b", null, getText("messageLastReceived")), /* @__PURE__ */ createElement("p", { class: "secondary", "subscribe:innerText": lastReceivedMessage }))), /* @__PURE__ */ createElement("hr", null), /* @__PURE__ */ createElement("div", { class: "tile width-input" }, /* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("b", null, getText("mailbox")), /* @__PURE__ */ createElement("p", { class: "secondary", "subscribe:innerText": mailboxId }))), /* @__PURE__ */ createElement("div", { class: "flex-row width-input justify-end" }, /* @__PURE__ */ createElement("button", { class: "primary width-100", "on:click": requestMailbox }, getText("requestMailbox"), /* @__PURE__ */ createElement("span", { class: "icon" }, "inbox"))), /* @__PURE__ */ createElement("hr", null), /* @__PURE__ */ createElement("label", { class: "tile" }, /* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("span", null, getText("subscribeToChannel")), /* @__PURE__ */ createElement( + return /* @__PURE__ */ createElement("article", { id: "main-screen" }, /* @__PURE__ */ createElement("header", null, getText("settings")), /* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("div", { class: "tile width-input" }, /* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("b", null, getText("messageLastReceived")), /* @__PURE__ */ createElement("p", { class: "secondary", "subscribe:innerText": lastReceivedMessage }))), /* @__PURE__ */ createElement("hr", null), /* @__PURE__ */ createElement("div", { class: "tile width-input" }, /* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("b", null, getText("mailbox")), /* @__PURE__ */ createElement("p", { class: "secondary", "toggle:hidden": isMailboxDisonnected }, getText("mailboxConnected")))), /* @__PURE__ */ createElement("div", { class: "flex-row width-input justify-end" }, /* @__PURE__ */ createElement( + "button", + { + class: "danger width-50", + "on:click": deleteMailbox, + "toggle:disabled": isMailboxDisonnected + }, + getText("deleteMailbox") + ), /* @__PURE__ */ createElement( + "button", + { + class: "primary width-50", + "on:click": requestMailbox, + "toggle:disabled": isDisconnected + }, + getText("requestMailbox"), + /* @__PURE__ */ createElement("span", { class: "icon" }, "inbox") + )), /* @__PURE__ */ createElement("hr", null), /* @__PURE__ */ createElement("label", { class: "tile" }, /* @__PURE__ */ createElement("div", null, /* @__PURE__ */ createElement("span", null, getText("subscribeToChannel")), /* @__PURE__ */ createElement( "input", { placeholder: getText("channel_placeholder"), diff --git a/static/node_modules/udn-frontend/README.md b/static/node_modules/udn-frontend/README.md index 25eeb78..9562c99 100644 --- a/static/node_modules/udn-frontend/README.md +++ b/static/node_modules/udn-frontend/README.md @@ -13,82 +13,36 @@ import UDNFrontend from "udn-frontend"; // setup const UDN = new UDNFrontend(); + +// connect UDN.onconnect = () => { console.log("connected!"); }; UDN.ondisconnect = () => { console.log("disconnected!"); }; + +UDN.connect("ws://192.168.0.69:1234"); +UDN.disconnect(); + +// messages UDN.onmessage = (data: Message) => { console.log(data); }; -// do stuff -UDN.connect("ws://192.168.0.69:1234"); - -UDN.subscribe("my-channel"); UDN.sendMessage("my-channel", "Hello, world!"); + +// subscribe +UDN.subscribe("my-channel"); UDN.unsubscribe("my-channel"); -``` -# Type Reference - -```TypeScript -export interface Message { - // connecting to server - requestingServerConnection?: boolean; - - // mailbox - requestingMailboxSetup?: boolean; - assignedMailboxId?: string; // sent by server - requestedMailbox?: string; - - // subscribing to channel - subscribeChannel?: string; - unsubscribeChannel?: string; - subscribed?: boolean; // sent by server to confirm subscription - - //sending message - messageChannel?: string; - messageBody?: string; +// mailbox +UDN.onmailbox = (mailboxId: string) => { + UDN.connectMailbox(mailboxId); +} +UDN.onmailboxconnect = (mailboxId: string) => { + UDN.deleteMailbox(mailboxId); } -export default class UDNFrontend { - ws: WebSocket | undefined; - - // handlers - private connectionHandler = () => {}; - private disconnectionHandler = () => {}; - private messageHandler = (data: Message) => {}; - - // init - set onconnect(handler: () => void); - set ondisconnect(handler: () => void); - set onmessage(handler: (data: Message) => void); - - // methods - connect(address: string): void; - disconnect(address: string): void; - sendMessage(channel: string, body: string): boolean; // true if message is sent - subscribe(channel: string): boolean; // true if request is sent, NOT inherently when subscribed - unsubscribe(channel: string): boolean; // true if request is sent, NOT inherently when unsubscribed -} +UDN.requestMailbox(); ``` - -# Changelog - -## 1.0.1 - -- minor fixes - -## 1.0.2 - -- minor fixes - -## 1.0.3 - -- add `disconnect()` method - -## 1.0.4 - -- make ws public \ No newline at end of file diff --git a/static/node_modules/udn-frontend/index.ts b/static/node_modules/udn-frontend/index.ts index fbe2cdb..3b729f6 100644 --- a/static/node_modules/udn-frontend/index.ts +++ b/static/node_modules/udn-frontend/index.ts @@ -4,8 +4,10 @@ export interface Message { // mailbox requestingMailboxSetup?: boolean; - assignedMailboxId?: string; // sent by server requestedMailbox?: string; + deletingMailbox?: string; + assignedMailboxId?: string; // sent by server + connectedMailboxId?: string; // sent by server // subscribing to channel subscribeChannel?: string; @@ -20,13 +22,14 @@ export interface Message { export default class UDNFrontend { ws: WebSocket | undefined; - // handlers + // HANDLERS private connectionHandler = () => {}; private disconnectionHandler = () => {}; private messageHandler = (data: Message) => {}; private mailboxHandler = (mailboxId: string) => {}; + private mailboxConnectionHandler = (mailboxId: string) => {}; - // init + // INIT set onconnect(handler: () => void) { this.connectionHandler = handler; } @@ -39,8 +42,11 @@ export default class UDNFrontend { set onmailbox(handler: (mailboxId: string) => void) { this.mailboxHandler = handler; } + set onmailboxconnect(handler: (mailboxId: string) => void) { + this.mailboxConnectionHandler = handler; + } - // utility methods + // UTILITY METHODS private send(messageObject: Message): boolean { if (this.ws == undefined) return false; @@ -49,7 +55,8 @@ export default class UDNFrontend { return true; } - // public methods + // PUBLIC METHODS + // connection connect(address: string): void { this.disconnect(); @@ -62,6 +69,8 @@ export default class UDNFrontend { if (data.assignedMailboxId) { return this.mailboxHandler(data.assignedMailboxId); + } else if (data.connectedMailboxId) { + return this.mailboxConnectionHandler(data.assignedMailboxId); } else { this.messageHandler(data); } @@ -72,6 +81,7 @@ export default class UDNFrontend { this.ws?.close(); } + // message sendMessage(channel: string, body: string): boolean { const messageObject = { messageChannel: channel, @@ -80,6 +90,7 @@ export default class UDNFrontend { return this.send(messageObject); } + // subscription subscribe(channel: string): boolean { const messageObject = { subscribeChannel: channel, @@ -94,6 +105,7 @@ export default class UDNFrontend { return this.send(messageObject); } + // mailbox requestMailbox(): boolean { const messageObject = { requestingMailboxSetup: true, @@ -101,10 +113,17 @@ export default class UDNFrontend { return this.send(messageObject); } - accessMailbox(mailboxId: string): boolean { + connectMailbox(mailboxId: string): boolean { const messageObject = { requestedMailbox: mailboxId, }; return this.send(messageObject); } + + deleteMailbox(mailboxId: string): boolean { + const messageObject = { + deletingMailbox: mailboxId, + }; + return this.send(messageObject); + } } diff --git a/static/node_modules/udn-frontend/package.json b/static/node_modules/udn-frontend/package.json index 9dccf63..ff55b03 100644 --- a/static/node_modules/udn-frontend/package.json +++ b/static/node_modules/udn-frontend/package.json @@ -1,6 +1,6 @@ { "name": "udn-frontend", - "version": "1.0.4-beta2", + "version": "1.0.4-beta4", "description": "TypeScript library for UDN apps", "main": "index.ts", "scripts": { diff --git a/static/package.json b/static/package.json index 7d04992..3dba0c5 100644 --- a/static/package.json +++ b/static/package.json @@ -2,7 +2,7 @@ "dependencies": { "bloatless-react": "1.2.5-beta1", "esbuild": "latest", - "udn-frontend": "1.0.4-beta2" + "udn-frontend": "1.0.4-beta4" }, "scripts": { "build": "esbuild src/index.tsx --bundle --outdir=dist" diff --git a/static/src/mainScreen.tsx b/static/src/mainScreen.tsx index 69686a5..12d6832 100644 --- a/static/src/mainScreen.tsx +++ b/static/src/mainScreen.tsx @@ -1,9 +1,11 @@ import * as React from "bloatless-react"; import { + deleteMailbox, + isDisconnected, + isMailboxDisonnected, isMessageEmpty, lastReceivedMessage, - mailboxId, newMessageBody, newMessageChannel, requestMailbox, @@ -33,12 +35,25 @@ export function MainScreen() {
+ {getText("mailboxConnected")} +