Skip to content

Commit

Permalink
feat: add connector base
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite committed Jun 30, 2024
1 parent cda058a commit 4d2a7c0
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 3 deletions.
44 changes: 41 additions & 3 deletions packages/node/src/PlayerNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { randomUUID } from 'node:crypto';
import type { Connector } from './connector/Connector';
import { IPC } from './connector/IPC';
import { TCP } from './connector/TCP';

/**
* Represents a player node object.
Expand All @@ -24,6 +27,10 @@ export interface PlayerNodeOptions {
* The password of this node.
*/
password: string;
/**
* Whether this node is an IPC node.
*/
ipc: boolean;
}

/**
Expand All @@ -50,6 +57,11 @@ export interface PlayerNodeInit {
*/
export const DISCORD_PLAYER_PROTOCOL = 'discord-player:';

/**
* The hostname for discord player IPC nodes.
*/
export const DISCORD_PLAYER_IPC_HOSTNAME = 'ipc.discord-player.js.org';

function validateNodeOptions(node: PlayerNodeOptions): PlayerNodeOptions {
const required = ['host', 'clientId', 'password'] as const;

Expand Down Expand Up @@ -84,6 +96,16 @@ export class PlayerNode {
*/
private readonly node: PlayerNodeOptions;

/**
* Whether this player node uses IPC.
*/
#isIPC = false;

/**
* The connector for this player node.
*/
private readonly connector: Connector;

/**
* Creates a new player node.
* @param node - The node config or node string to use
Expand All @@ -95,17 +117,31 @@ export class PlayerNode {
} else {
this.node = validateNodeOptions(node);
}

this.#isIPC = this.node.ipc;
this.connector = this.#isIPC ? new IPC(this) : new TCP(this);
}

/**
* Whether this player node is an IPC node.
*/
public get isIPCNode() {
return this.#isIPC;
}

/**
* Connects to this player node.
*/
public async connect() {}
public async connect() {
await this.connector.connect();
}

/**
* Destroy this player node.
*/
public async delete() {}
public async delete() {
await this.connector.disconnect();
}

/**
* Sends a packet to the player node.
Expand Down Expand Up @@ -166,13 +202,15 @@ export class PlayerNode {
}

const isSecure = searchParams.get('secure') === 'true';
const isIpc = hostname === DISCORD_PLAYER_IPC_HOSTNAME;

return {
host: hostname,
port: port ? Number(port) : null,
secure: isSecure,
clientId: username,
password
password,
ipc: isIpc
};
}
}
37 changes: 37 additions & 0 deletions packages/node/src/connector/Connector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { EventEmitter } from '@discord-player/utils';
import type { PlayerNode } from '../PlayerNode';

export interface ConnectorEvents {
ready: () => Awaited<unknown>;
close: () => Awaited<unknown>;
error: (error: Error) => Awaited<unknown>;
}

export abstract class Connector extends EventEmitter<ConnectorEvents> {
/**
* Creates a new connector.
* @param node - The player node instance
*/
public constructor(public readonly node: PlayerNode) {
super();
}

/**
* Connects to the node.
*/
public abstract connect(): Promise<void>;
/**
* Disconnects from the node.
*/
public abstract disconnect(): Promise<void>;
/**
* Whether the node is connected.
*/
public abstract isConnected(): boolean;
/**
* Send a packet to the node.
* @param data - The data to send
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public abstract send(data: any): Promise<void>;
}
19 changes: 19 additions & 0 deletions packages/node/src/connector/IPC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Connector } from './Connector';

export class IPC extends Connector {
public connect(): Promise<void> {
throw new Error('Method not implemented.');
}

public disconnect(): Promise<void> {
throw new Error('Method not implemented.');
}

public isConnected(): boolean {
throw new Error('Method not implemented.');
}

public send(): Promise<void> {
throw new Error('Method not implemented.');
}
}
19 changes: 19 additions & 0 deletions packages/node/src/connector/TCP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Connector } from './Connector';

export class TCP extends Connector {
public connect(): Promise<void> {
throw new Error('Method not implemented.');
}

public disconnect(): Promise<void> {
throw new Error('Method not implemented.');
}

public isConnected(): boolean {
throw new Error('Method not implemented.');
}

public send(): Promise<void> {
throw new Error('Method not implemented.');
}
}

0 comments on commit 4d2a7c0

Please sign in to comment.