Skip to content

Commit

Permalink
fix compiling errors; ditch webpack for vite
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak committed Aug 17, 2024
1 parent 63632e5 commit 5845ab5
Show file tree
Hide file tree
Showing 12 changed files with 723 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ <h1>Topology Protocol - Chat</h1>
});
</script>

<script src="./static/bundle/script.js"></script>
<script type="module" src="/src/index.ts"></script>
</body>
</html>
7 changes: 5 additions & 2 deletions examples/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@topology-foundation/network": "0.0.23-5",
"@topology-foundation/node": "0.0.23-5",
"@topology-foundation/object": "0.0.23-5",
"browserify-fs": "^1.0.0",
"crypto-browserify": "^3.12.0",
"process": "^0.11.10",
"stream-browserify": "^3.0.0",
Expand All @@ -20,14 +21,16 @@
"@types/node": "^20.11.16",
"ts-loader": "^9.3.1",
"typescript": "^4.7.4",
"vite": "^5.4.1",
"vite-plugin-node-polyfills": "^0.22.0",
"webpack": "^5.74.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
},
"scripts": {
"build": "webpack",
"build": "vite build",
"clean": "rm -rf dist/ node_modules/",
"dev": "webpack serve",
"dev": "vite serve",
"start": "ts-node ./src/index.ts"
}
}
2 changes: 1 addition & 1 deletion examples/chat/src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { toString as uint8ArrayToString } from "uint8arrays/to-string";
import { addMessage, Chat } from "./objects/chat";

export const handleChatMessages = (chat: ChatI, e: any) => {
export const handleChatMessages = (chat: Chat, e: any) => {
if (e.detail.msg.topic === "topology::discovery") return;
const input = uint8ArrayToString(e.detail.msg.data);
const message = JSON.parse(input);
Expand Down
21 changes: 12 additions & 9 deletions examples/chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import * as topology from "@topology-foundation/node";
import { Chat, addMessage, getMessages } from "./objects/chat";
import { handleChatMessages } from "./handlers";
import { GSet } from "@topology-foundation/crdt";
import { newTopologyObject } from "@topology-foundation/object";
import { newTopologyObject, TopologyObject } from "@topology-foundation/object";

const node = new TopologyNode();
// CRO = Conflict-free Replicated Object
let topologyObject: TopologyObject;
let chatCRO: Chat;
let peers: string[] = [];
let discoveryPeers: string[] = [];
Expand Down Expand Up @@ -65,20 +66,20 @@ async function main() {
await node.start();
render();

node.addCustomGroupMessageHandler(chatCRO.cro.id, (e) => {
node.addCustomGroupMessageHandler(topologyObject.id, (e) => {
handleChatMessages(chatCRO, e);
peers = node.networkNode.getAllPeers();
discoveryPeers = node.networkNode.getGroupPeers("topology::discovery");
if (chatCRO) objectPeers = node.networkNode.getGroupPeers(chatCRO.cro.id);
if (chatCRO) objectPeers = node.networkNode.getGroupPeers(topologyObject.id);
render();
});

let button_create = <HTMLButtonElement>document.getElementById("createRoom");
button_create.addEventListener("click", async () => {
chatCRO = await newTopologyObject(node.networkNode.peerId, "./objects/chat.ts")
topologyObject = await newTopologyObject(node.networkNode.peerId, "./objects/chat.ts");

topology.createObject(node, chatCRO);
(<HTMLButtonElement>document.getElementById("chatId")).innerHTML = chatCRO.cro.id;
// topology.createObject(node, chatCRO);
(<HTMLButtonElement>document.getElementById("chatId")).innerHTML = topologyObject.id;
render();
});

Expand All @@ -90,7 +91,9 @@ async function main() {
alert("Please enter a room id");
return;
}
await topology.subscribeObject(node, objectId, true);

//objectId
await topology.subscribeObject(node, new Uint8Array(), true);
});

let button_fetch = <HTMLButtonElement>document.getElementById("fetchMessages");
Expand All @@ -105,9 +108,9 @@ async function main() {
let arr: string[] = Array.from(object["chat"]["_set"]);
object["chat"]["_set"] = new Set<string>(arr);
object["chat"] = Object.assign(new GSet<string>(new Set<string>()), object["chat"]);
chatCRO = Object.assign(new Chat(node.networkNode.peerId), object);
chatCRO = Object.assign(new Chat(), object);

(<HTMLButtonElement>document.getElementById("chatId")).innerHTML = chatCRO.cro.id;
(<HTMLButtonElement>document.getElementById("chatId")).innerHTML = topologyObject.id;
render();
} catch (e) {
console.error("Error while connecting to the CRO ", objectId, e);
Expand Down
19 changes: 17 additions & 2 deletions examples/chat/src/objects/chat.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
// @ts-ignore
import { GSet, gset_create, gset_add, gset_merge } from "@topology-foundation/crdt/src/index.asc";
// if it can't compile, append src/index.asc to the import path on runtime
import { GSet, gset_create, gset_add, gset_merge } from "@topology-foundation/crdt";

export class Chat {
// store messages as strings in the format (timestamp, message, peerId)
messages: GSet<string>;
constructor() {
this.messages = gset_create<string>();
}

addMessage(timestamp: string, message: string, node_id: string): void {
this.messages.add(`(${timestamp}, ${message}, ${node_id})`)
}

getMessages(): GSet<string> {
return this.messages;
}

merge(other: Chat): void {
this.messages.merge(other.messages);
}
}

export function createChat(): Chat {
return new Chat();
}

// @ts-ignore
export function addMessage(chat: Chat, timestamp: string, message: string, node_id: string): void {
gset_add(chat.messages, `(${timestamp}, ${message}, ${node_id})`)
}

// @ts-ignore
export function getMessages(chat: Chat): GSet<string> {
return chat.messages;
}

// @ts-ignore
export function merge(chat: Chat, other: Chat): void {
gset_merge(chat.messages, other.messages);
}
24 changes: 12 additions & 12 deletions examples/chat/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"compilerOptions": {
"target": "es6",
"module": "ESNEXT",
"rootDir": ".",
"strict": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"allowJs": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"rootDir": ".",
"strict": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"allowJs": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
16 changes: 16 additions & 0 deletions examples/chat/vite.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from 'vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills'

export default defineConfig({
build: {
target: 'esnext',
},
plugins: [
nodePolyfills(),
],
optimizeDeps: {
esbuildOptions: {
target: 'esnext'
}
},
})
63 changes: 0 additions & 63 deletions examples/chat/webpack.config.js

This file was deleted.

4 changes: 4 additions & 0 deletions packages/crdt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
},
"./wasm": {
"types": "./dist/src/index.d.ts",
"import": "./src/index.asc.ts"
}
},
"scripts": {
Expand Down
1 change: 0 additions & 1 deletion packages/crdt/src/builtins/GSet/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* GSet with support for state and op changes */

export class GSet<T> {
set: Set<T>;

Expand Down
14 changes: 0 additions & 14 deletions packages/object/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import { compileWasm } from "./wasm/compiler.js";

export * from "./proto/object_pb.js";

async function compileCRO(path: string): Promise<Uint8Array> {
// get the bytecode from the wasm compiler and abi
// TODO: abi not extracted yet
return compileWasm(path);
}

/* Creates a new TopologyObject */
export async function newTopologyObject(peerId: string, path: string, id?: string, abi?: string): Promise<TopologyObject> {
const bytecode = await compileWasm(path);
Expand All @@ -32,13 +26,5 @@ export async function callFn(obj: TopologyObject, fn: string, args: string[]): P
fn: fn,
args: args
});

return obj;
}

async function run() {
// TODO: just for testing wasm compilation with tsx, should be deleted
let obj = await newTopologyObject("peerId", "/Users/droak/code/topology/ts-topology/examples/chat/src/objects/chat.ts", "", "");
console.log(obj);
}
// run();
Loading

0 comments on commit 5845ab5

Please sign in to comment.