From c0d1876548fbfce339579a77662a12b232003e87 Mon Sep 17 00:00:00 2001
From: Dinhero21 <dinhero21@dinhero21.dev>
Date: Fri, 30 Aug 2024 21:13:01 -0300
Subject: [PATCH] More appropiate terminology

client-side, server-side (highly ambiguous and context-dependant) to upstream and downstream (industry-standard)
---
 src/module/proxy/global.ts | 46 ++++++++++++----------------
 src/module/proxy/local.ts  | 62 +++++++++++++++-----------------------
 src/util/packet.ts         | 13 +++-----
 3 files changed, 48 insertions(+), 73 deletions(-)

diff --git a/src/module/proxy/global.ts b/src/module/proxy/global.ts
index 4d39afd..3d0b771 100644
--- a/src/module/proxy/global.ts
+++ b/src/module/proxy/global.ts
@@ -5,8 +5,8 @@ import { type RawPacket } from '../../util/packet.js';
 import { type Message } from './shared.js';
 
 export default async function (instance: Instance): Promise<void> {
-  const clientQueue: RawPacket[] = [];
-  const serverQueue: RawPacket[] = [];
+  const downstreamQueue: RawPacket[] = [];
+  const upstreamQueue: RawPacket[] = [];
 
   const channel = instance.createChannel<Message>('proxy');
 
@@ -39,10 +39,8 @@ export default async function (instance: Instance): Promise<void> {
   });
 
   client.on('packet', (data, meta) => {
-    // if (meta.state !== states.PLAY) return;
-
     channel.write({
-      direction: 'downstream',
+      direction: 'upstream',
       packet: {
         ...meta,
         data,
@@ -51,10 +49,8 @@ export default async function (instance: Instance): Promise<void> {
   });
 
   server.on('packet', (data, meta) => {
-    // if (meta.state !== states.PLAY) return;
-
     channel.write({
-      direction: 'upstream',
+      direction: 'downstream',
       packet: {
         ...meta,
         data,
@@ -67,40 +63,36 @@ export default async function (instance: Instance): Promise<void> {
   client.on('state', (state) => {
     if (state !== states.PLAY) return;
 
-    const queue = clientQueue;
+    for (const packet of downstreamQueue) writeDownstreamPacket(packet);
 
-    for (const packet of queue) client.write(packet.name, packet.data);
-
-    queue.length = 0;
+    downstreamQueue.length = 0;
   });
 
   server.on('state', (state) => {
     if (state !== states.PLAY) return;
 
-    const queue = serverQueue;
+    for (const packet of upstreamQueue) writeUpstreamPacket(packet);
 
-    for (const packet of queue) server.write(packet.name, packet.data);
-
-    queue.length = 0;
+    upstreamQueue.length = 0;
   });
 
-  channel.subscribe(({ direction: side, packet }) => {
-    switch (side) {
+  channel.subscribe(({ direction: direction, packet }) => {
+    switch (direction) {
       case 'downstream':
-        writeClientPacket(packet);
+        writeDownstreamPacket(packet);
         break;
       case 'upstream':
-        writeServerPacket(packet);
+        writeUpstreamPacket(packet);
         break;
       default:
-        throw new Error(`Invalid side: ${side as any}`);
+        throw new Error(`Unknown direction: ${direction as any}`);
     }
   });
 
-  function writeClientPacket(packet: RawPacket): void {
-    // wait until play state
+  function writeDownstreamPacket(packet: RawPacket): void {
+    // queue until play state
     if (client.state !== states.PLAY) {
-      clientQueue.push(packet);
+      downstreamQueue.push(packet);
 
       return;
     }
@@ -110,10 +102,10 @@ export default async function (instance: Instance): Promise<void> {
     client.write(packet.name, packet.data);
   }
 
-  function writeServerPacket(packet: RawPacket): void {
-    // wait until play state
+  function writeUpstreamPacket(packet: RawPacket): void {
+    // queue until play state
     if (server.state !== states.PLAY) {
-      serverQueue.push(packet);
+      upstreamQueue.push(packet);
 
       return;
     }
diff --git a/src/module/proxy/local.ts b/src/module/proxy/local.ts
index c03a96f..f0b2b86 100644
--- a/src/module/proxy/local.ts
+++ b/src/module/proxy/local.ts
@@ -1,6 +1,4 @@
-import type { States } from 'minecraft-protocol';
-import { states } from 'minecraft-protocol';
-
+import type { EventMap } from '../../util/events.js';
 import { PublicEventHandler } from '../../util/events.js';
 import { Packet, type RawPacket } from '../../util/packet.js';
 import { type AsyncVoid } from '../../util/types.js';
@@ -19,51 +17,39 @@ function write(direction: Direction, packet: RawPacket): void {
   });
 }
 
-export const proxy = {
-  client: new PublicEventHandler<PacketEventMap>(),
-  server: new PublicEventHandler<PacketEventMap>(),
+class ConnectionSide<
+  TEventMap extends EventMap<TEventMap>,
+> extends PublicEventHandler<TEventMap> {
+  constructor(protected readonly direction: Direction) {
+    super();
+  }
 
-  writeDownstream(
-    name: string,
-    data: unknown,
-    state: States = states.PLAY,
-  ): void {
-    write('downstream', { name, data, state });
-  },
-  writeUpstream(
-    name: string,
-    data: unknown,
-    state: States = states.PLAY,
-  ): void {
-    write('upstream', { name, data, state });
-  },
+  public write(packet: Packet): void {
+    write(this.direction, packet);
+  }
+}
+
+// ngl I feel like I need rx soo bad
+// then I could just do
+// channel.filter(({ direction }) => direction === 'downstream')
+// and it would just work
+
+export const proxy = {
+  upstream: new ConnectionSide<PacketEventMap>('upstream'),
+  downstream: new ConnectionSide<PacketEventMap>('downstream'),
 } as const;
 
 channel.subscribe(({ direction, packet: raw }: Message) => {
   void (async () => {
-    const sourceHandler = {
-      downstream: proxy.server,
-      upstream: proxy.client,
-    }[direction];
+    const connection = proxy[direction];
 
-    const packet = new Packet(raw.name, raw.data);
+    const packet = new Packet(raw.name, raw.data, raw.state);
 
-    await sourceHandler.emit('packet', packet);
-    await sourceHandler.emit(packet.name, packet);
+    await connection.emit('packet', packet);
+    await connection.emit(packet.name, packet);
 
     if (packet.canceled) return;
 
-    switch (direction) {
-      case 'downstream':
-        direction = 'upstream';
-        break;
-      case 'upstream':
-        direction = 'downstream';
-        break;
-      default:
-        throw new Error(`Invalid direction: ${direction as any}`);
-    }
-
     // Forward packet
     channel.write({
       direction,
diff --git a/src/util/packet.ts b/src/util/packet.ts
index 6103834..a3e3773 100644
--- a/src/util/packet.ts
+++ b/src/util/packet.ts
@@ -6,14 +6,11 @@ export interface RawPacket extends PacketMeta {
 }
 
 export class Packet<Data = unknown> {
-  public name;
-  public state: States = states.PLAY;
-  public data;
-
   public canceled: boolean = false;
 
-  constructor(name: string, data: Data) {
-    this.name = name;
-    this.data = data;
-  }
+  constructor(
+    public name: string,
+    public data: Data,
+    public state: States = states.PLAY,
+  ) {}
 }