From 6cba62e78c84ba505b5c5f52eb2cc26111b180cf Mon Sep 17 00:00:00 2001 From: Dinhero21 <78858626+Dinhero21@users.noreply.github.com> Date: Mon, 8 Jan 2024 04:25:38 -0300 Subject: [PATCH] Different Send/Receive Message Channel Types and some other minor stuff --- src/instance/index.ts | 8 +-- src/module/.gitignore | 5 ++ src/module/proxy/local.ts | 19 +++---- src/util/channel.ts | 20 ++++---- src/util/events.ts | 8 ++- src/util/packet.ts | 4 +- src/util/types.ts | 1 + src/worker/module/chat.ts | 104 -------------------------------------- src/worker/parent.ts | 6 +-- 9 files changed, 41 insertions(+), 134 deletions(-) create mode 100644 src/module/.gitignore create mode 100644 src/util/types.ts delete mode 100644 src/worker/module/chat.ts diff --git a/src/instance/index.ts b/src/instance/index.ts index 618c253..a8bb898 100644 --- a/src/instance/index.ts +++ b/src/instance/index.ts @@ -17,7 +17,7 @@ const MODULE_DIR_PATH = resolve(__dirname, '../module') const VERSION = '1.19.4' export const TARGET_OPTIONS = { - host: 'grandma-does.tech', + host: 'kaboom.pw', port: 25565, keepAlive: false, username: 'Player137', @@ -71,14 +71,14 @@ export class Instance { } satisfies Message) } - public createChannel (id: string): Channel { - const channel = new Channel(id) + public createChannel (id: string): Channel { + const channel = new Channel(id) channel._subscribe(data => { this.postMessage(id, data) }) - this.worker.on('message', (message: Message) => { + this.worker.on('message', (message: Message) => { if (message.channel !== id) return channel._write(message.data) diff --git a/src/module/.gitignore b/src/module/.gitignore new file mode 100644 index 0000000..9cb2ccc --- /dev/null +++ b/src/module/.gitignore @@ -0,0 +1,5 @@ +* +!.gitignore + +# internal modules +!proxy diff --git a/src/module/proxy/local.ts b/src/module/proxy/local.ts index 2595571..6219340 100644 --- a/src/module/proxy/local.ts +++ b/src/module/proxy/local.ts @@ -1,22 +1,17 @@ import { type Side, type Message } from './shared.js' -import { type AsyncVoid, EventHandler } from '../../util/events.js' +import { PublicEventHandler } from '../../util/events.js' import { createChannel } from '../../worker/parent.js' import { Packet, type RawPacket } from '../../util/packet.js' +import { type AsyncVoid } from '../../util/types.js' -export type EventMap = Record AsyncVoid> - -export class EventEmitter extends EventHandler { - public async emit (name: string, packet: Packet): Promise { - await this._emit(name, packet) - } -} +export type PacketEventMap = Record AsyncVoid> // ? Should I export the channel export const channel = createChannel('proxy') export class Proxy { - public readonly client = new EventEmitter() - public readonly server = new EventEmitter() + public readonly client = new PublicEventHandler() + public readonly server = new PublicEventHandler() constructor () { channel.subscribe(({ side, packet: raw }: Message) => { @@ -65,4 +60,6 @@ export class Proxy { } } -export default new Proxy() +export const proxy = new Proxy() + +export default proxy diff --git a/src/util/channel.ts b/src/util/channel.ts index 54bf3c7..f3bfdb2 100644 --- a/src/util/channel.ts +++ b/src/util/channel.ts @@ -1,39 +1,41 @@ +export type Listener = (data: T) => void + // _x -> x // x -> _x -export class Channel { +export class Channel { public readonly id constructor (id: string) { this.id = id } - private readonly listeners = new Set<(data: T) => void>() + private readonly listeners = new Set>() - public subscribe (listener: (data: T) => void): void { + public subscribe (listener: Listener): void { this.listeners.add(listener) } - public unsubscribe (listener: (data: T) => void): void { + public unsubscribe (listener: Listener): void { this.listeners.delete(listener) } - public write (data: T): void { + public write (data: TSend): void { for (const listener of this._listeners) { listener(data) } } - private readonly _listeners = new Set<(data: T) => void>() + private readonly _listeners = new Set>() - public _subscribe (listener: (data: T) => void): void { + public _subscribe (listener: Listener): void { this._listeners.add(listener) } - public _unsubscribe (listener: (data: T) => void): void { + public _unsubscribe (listener: Listener): void { this._listeners.delete(listener) } - public _write (data: T): void { + public _write (data: TReceive): void { for (const listener of this.listeners) { listener(data) } diff --git a/src/util/events.ts b/src/util/events.ts index e93b2e6..5819958 100644 --- a/src/util/events.ts +++ b/src/util/events.ts @@ -1,4 +1,4 @@ -export type AsyncVoid = void | Promise +import { type AsyncVoid } from './types.js' export type EventMap = { [K in keyof T]: (...args: any[]) => AsyncVoid } @@ -40,3 +40,9 @@ export class EventHandler> { map.clear() } } + +export class PublicEventHandler> extends EventHandler { + public async emit (name: E, ...data: Parameters): Promise { + await this._emit(name, ...data) + } +} diff --git a/src/util/packet.ts b/src/util/packet.ts index 00e2c3a..5be8dab 100644 --- a/src/util/packet.ts +++ b/src/util/packet.ts @@ -3,13 +3,13 @@ export interface RawPacket { data: unknown } -export class Packet { +export class Packet { public name public data public canceled: boolean = false - constructor (name: string, data: T) { + constructor (name: string, data: Data) { this.name = name this.data = data } diff --git a/src/util/types.ts b/src/util/types.ts new file mode 100644 index 0000000..19b6bee --- /dev/null +++ b/src/util/types.ts @@ -0,0 +1 @@ +export type AsyncVoid = void | Promise diff --git a/src/worker/module/chat.ts b/src/worker/module/chat.ts deleted file mode 100644 index 1be3e0e..0000000 --- a/src/worker/module/chat.ts +++ /dev/null @@ -1,104 +0,0 @@ -// import { type UUID } from 'crypto' -// import { type AsyncVoid, EventHandler } from '../../util/events.js' -// import { type Packet } from '../util/packet.js' -// import proxy from './proxy.js' - -// export type i64 = [number, number] - -// export interface ClientChatPacketData { -// timestamp: i64 -// salt: i64 -// acknowledged: Uint8Array -// } - -// export type Signature = Uint8Array - -// export interface ClientChatMessagePacketData extends ClientChatPacketData { -// message: string -// signature: Signature | undefined -// offset: number -// } - -// export interface ClientChatCommandPacketData extends ClientChatPacketData { -// command: string -// argumentSignatures: Signature[] -// messageCount: number -// } - -// export interface ProfileLessChatPacketData { -// message: string -// type: number -// name: string -// // TODO: find out what target is supposed to be -// target: undefined -// } - -// export interface PlayerChatPacketData { -// senderUuid: UUID -// index: number -// signature: Signature | undefined -// plainMessage: string -// timestamp: i64 -// salt: i64 -// // TODO: find out what previousMessages is supposed to be -// previousMessages: never[] -// unsignedChatContent: string -// filterType: number -// // TODO: Find out what filterTypeMask is supposed to be -// filterTypeMask: undefined -// type: number -// networkName: string -// // TODO: find out what networkTargetName is supposed to be -// networkTargetName: undefined -// } - -// export interface SystemChatPacketData { -// content: string -// isActionBar: boolean -// } - -// export interface ChatEventMap { -// 'client.command': (packet: Packet) => AsyncVoid -// 'client.message': (packet: Packet) => AsyncVoid - -// 'server.profiless': (packet: Packet) => AsyncVoid -// 'server.player': (packet: Packet) => AsyncVoid -// 'server.system': (packet: Packet) => AsyncVoid -// } - -// export class Chat extends EventHandler { -// constructor () { -// super() - -// proxy.client.on('chat_command', async packet => { -// await this._emit('client.command', packet as Packet) -// }) - -// proxy.client.on('chat_message', async packet => { -// await this._emit('client.message', packet as Packet) -// }) - -// proxy.server.on('profileless_chat', async packet => { -// await this._emit('server.profiless', packet as Packet) -// }) - -// proxy.server.on('player_chat', async packet => { -// await this._emit('server.player', packet as Packet) -// }) - -// proxy.server.on('system_chat', async packet => { -// await this._emit('server.system', packet as Packet) -// }) -// } - -// // // TODO: Fix -// // public writeClientCommand (command: string): void { -// // proxy.writeClient('chat_command', { command }) -// // } - -// // public writeClientMessage (message: string): void { -// // proxy.writeClient('chat_message', { message }) -// // } -// } - -// export default new Chat() diff --git a/src/worker/parent.ts b/src/worker/parent.ts index 535d27f..53d160b 100644 --- a/src/worker/parent.ts +++ b/src/worker/parent.ts @@ -17,14 +17,14 @@ function postMessage (channel: string, data: any): void { }) } -export function createChannel (id: string): Channel { - const channel = new Channel(id) +export function createChannel (id: string): Channel { + const channel = new Channel(id) channel._subscribe(message => { postMessage(id, message) }) - port.on('message', (message: Message) => { + port.on('message', (message: Message) => { if (message.channel !== id) return channel._write(message.data)