Skip to content

Commit

Permalink
Basic PacketCloud
Browse files Browse the repository at this point in the history
  • Loading branch information
webdev03 committed Oct 14, 2023
1 parent 77f0e38 commit 742f461
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// a lot of this has been taken from https://github.com/meow-js/meow-js/blob/master/src/classes/CloudConnection.js thank you so much!

import { WebSocket } from "ws";
import { Session } from "../Consts";
import { Session } from "../../Consts";
import events from "node:events";

/**
Expand Down
34 changes: 34 additions & 0 deletions src/classes/cloud/PacketCloud.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type CloudConnection from "./CloudConnection";
import events from "events";
import { encode, decode } from "./utils";

type OnRequestFn = (name: string, value: string) => void;

class PacketCloud extends events.EventEmitter {
connection: CloudConnection;
onRequestListeners: OnRequestFn[] = [];
constructor(connection: CloudConnection) {
super();
this.connection = connection;
this.connection.on("set", (data) => {
if (String(data.name).includes("FROM_USER_")) {
const [name, value] = [...decode(data.value)];
for(const fn of this.onRequestListeners) fn(name, value);
this.emit(name, value);
}
});
}

onRequest(fn: OnRequestFn) {
this.onRequestListeners.push(fn);
return this;
}

send(name: string, value: string) {
const val = encode(name) + encode(value);
if (val.length > 250) console.warn("Packet length is greater than 250!");
this.connection.setVariable(`FROM_SERVER_SEND`, val);
}
}

export default PacketCloud;
18 changes: 18 additions & 0 deletions src/classes/cloud/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const CHARSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~1234567890! @#$%^&*()_-.\";:'?><,/".split("");
export function encode(string: string) {
let result = "";
for(let i = 0; i < string.length; i++) {
result += CHARSET.findIndex(x => x === string[i]) + 10;
};
return result + "00";
};
export function* 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 += CHARSET[idx];
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export { default as ScratchSession } from "./ScratchSession";
export { default as Profile } from "./classes/Profile";
export { default as Studio } from "./classes/Studio";
export { default as Project } from "./classes/Project";
export { default as CloudConnection } from "./classes/CloudConnection";
export { default as CloudConnection } from "./classes/cloud/CloudConnection";
export { default as PacketCloud } from "./classes/cloud/PacketCloud";
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 742f461

Please sign in to comment.