-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTransportStream.ts
84 lines (70 loc) · 1.91 KB
/
TransportStream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { EventEmitter } from 'events';
import { TelnetStream } from '../types/TelnetStream';
import { TelnetSocket } from '../types/TelnetSocket';
import { RanvierWebSocket } from '../types/RanvierWebSocket';
import { WebsocketStream } from '../types/WebsocketStream';
export type StreamType = TelnetStream | WebsocketStream;
export type SocketType = TelnetSocket | RanvierWebSocket;
/**
* Base class for anything that should be sending or receiving data from the player
*/
export class TransportStream extends EventEmitter {
socket?: SocketType;
_prompted: boolean = false;
get readable() {
return true;
}
get writable() {
return true;
}
write(message: string, encoding?: string) {
/* noop */
}
/**
* A subtype-safe way to execute commands on a specific type of stream that invalid types will ignore. For given input
* for command (example, `"someCommand"` ill look for a method called `executeSomeCommand` on the `TransportStream`
* @param {string} command
* @param {...*} args
* @return {*}
*/
command<T, K extends keyof T>(this: T, command: string, ...args: any[]): any {
if (!command || !command.length) {
throw new RangeError('Must specify a command to the stream');
}
const methodName = 'execute' + command[0].toUpperCase() + command.substr(1);
if (typeof this[methodName as K] === 'function') {
const commandMethod = this[methodName as K] as unknown as (
...args: any[]
) => any;
return commandMethod.call(this, ...args);
}
}
address(...args: any[]) {
return null;
}
end(...args: any[]) {
/* noop */
}
setEncoding(...args: any[]) {
/* noop */
}
pause(...args: any[]) {
/* noop */
}
resume(...args: any[]) {
/* noop */
}
destroy(...args: any[]) {
/* noop */
}
/**
* Attach a socket to this stream
* @param {*} socket
*/
attach(socket: SocketType) {
this.socket = socket;
this.socket.on('close', (_?: any) => {
this.emit('close');
});
}
}