Skip to content

Commit

Permalink
Switch to CloudSerialiser
Browse files Browse the repository at this point in the history
  • Loading branch information
webdev03 committed Nov 4, 2023
1 parent b9beb10 commit 545e9dd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
23 changes: 23 additions & 0 deletions src/classes/cloud/CloudSerialiser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export default class CloudEncoder {
charset =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~1234567890! @#$%^&*()_-.\";:'?><,/".split(
""
);
encode(string: string) {
let result = "";
for (let i = 0; i < string.length; i++) {
result += this.charset.findIndex((x) => x === string[i]) + 10;
}
return result + "00";
}
*decode(string: string) {
let value = "";
for (let i = 1; i < string.length; i += 2) {
const idx = Number(string[i - 1] + string[i]) - 10;
if (idx < 0) {
yield value;
value = "";
} else value += this.charset[idx];
}
}
}
8 changes: 5 additions & 3 deletions src/classes/cloud/PacketCloud.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type CloudConnection from "./CloudConnection";
import events from "events";
import { encode, decode } from "./utils";
import CloudSerialiser from "./CloudSerialiser";

const serialiser = new CloudSerialiser();

class PacketCloud extends events.EventEmitter {
connection: CloudConnection;
Expand All @@ -10,7 +12,7 @@ class PacketCloud extends events.EventEmitter {
this.connection = connection;
this.connection.on("set", (data) => {
if (String(data.name).includes("FROM_USER")) {
const decoder = decode(data.value);
const decoder = serialiser.decode(data.value);
const name = decoder.next().value;
const value = decoder.next().value;
if (!name || !value) return;
Expand All @@ -21,7 +23,7 @@ class PacketCloud extends events.EventEmitter {
}

send(name: string, value: string) {
const val = encode(name) + encode(value);
const val = serialiser.encode(name) + serialiser.encode(value);
this.connection.setVariable(`FROM_SERVER_SEND`, val);
}
}
Expand Down
21 changes: 0 additions & 21 deletions src/classes/cloud/utils.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { default as Studio } from "./classes/Studio";
export { default as Project } from "./classes/Project";
export { default as CloudConnection } from "./classes/cloud/CloudConnection";
export { default as PacketCloud } from "./classes/cloud/PacketCloud";
export { default as CloudSerialiser } from "./classes/cloud/CloudSerialiser";
export { default as Forum } from "./classes/forums/Forum";
export { default as Topic } from "./classes/forums/Topic";
export { default as Post } from "./classes/forums/Post";

0 comments on commit 545e9dd

Please sign in to comment.