-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockPeer.ts
139 lines (112 loc) · 3.67 KB
/
MockPeer.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// tslint:disable:readonly-keyword no-object-mutation
import bufferToArray from 'buffer-to-arraybuffer';
import { Data as WSData } from 'ws';
import { CloseFrame } from './CloseFrame';
import { MockWebSocket } from './MockWebSocket';
import { PingOrPong } from './PingOrPong';
export abstract class MockPeer {
protected readonly peerWebSocket: MockWebSocket;
protected ownCloseFrame: CloseFrame | null = null;
protected wasAborted: boolean = false;
constructor() {
this.peerWebSocket = new MockWebSocket(() => {
if (!this.ownCloseFrame) {
// The closing handshake was initiated by the other peer
this.close();
}
});
}
get didPeerCloseConnection(): boolean {
return this.peerWebSocket.closeFrame !== null || this.peerWebSocket.wasTerminated;
}
protected get didICloseConnection(): boolean {
return this.ownCloseFrame !== null || this.wasAborted;
}
public close(code: number = 1005, reason?: Buffer | string): void {
this.ownCloseFrame = { code, reason };
this.peerWebSocket.emit('close', code, reason);
}
public abort(error: Error): void {
this.wasAborted = true;
this.peerWebSocket.emit('error', error);
this.peerWebSocket.emit('close', 1006, 'Aborted from test');
}
public async send(message: WSData): Promise<void> {
this.requireConnectionStillOpen();
const messageSerialized =
typeof message === 'string' || Array.isArray(message)
? message
: this.convertBinaryType(message);
return new Promise((resolve) => {
this.peerWebSocket.once('message', resolve);
this.peerWebSocket.emit('message', messageSerialized);
});
}
public async receive(): Promise<WSData> {
this.requireConnectionStillOpen();
const oldestMessage = this.popOldestPeerMessage();
if (oldestMessage) {
return oldestMessage;
}
return this.peerWebSocket.getLastMessageSent();
}
public popOldestPeerMessage(): WSData | undefined {
return this.peerWebSocket.popOldestMessage();
}
public async waitForPeerClosure(): Promise<CloseFrame> {
return this.peerWebSocket.getCloseFrameWhenAvailable();
}
get peerCloseFrame(): CloseFrame | null {
return this.peerWebSocket.closeFrame;
}
/**
* Send ping to peer and return ping data.
*
* @param data
*/
public ping(data?: Buffer): Buffer {
const finalData = data ?? Buffer.from(Math.random().toString());
this.peerWebSocket.emit('ping', finalData);
return finalData;
}
/**
* Send pong to peer.
*
* @param data
*/
public pong(data: Buffer): void {
this.peerWebSocket.emit('pong', data);
}
/**
* Return pings sent by peer.
*/
get incomingPings(): readonly PingOrPong[] {
// Return a shallow copy to avoid race conditions if more pings are received
return [...this.peerWebSocket.outgoingPings];
}
/**
* Return pongs sent by peer.
*/
get incomingPongs(): readonly PingOrPong[] {
// Return a shallow copy to avoid race conditions if more pings are received
return [...this.peerWebSocket.outgoingPongs];
}
/**
* Mimic the conversion that `ws` would do on binary frames.
*
* @param message
*
* See https://github.com/websockets/ws/blob/master/doc/ws.md#websocketbinarytype
*/
protected convertBinaryType(message: Buffer | ArrayBuffer): Buffer | ArrayBuffer {
if (this.peerWebSocket.binaryType === 'nodebuffer') {
return Buffer.isBuffer(message) ? message : Buffer.from(message);
}
return Buffer.isBuffer(message) ? bufferToArray(message) : message;
}
private requireConnectionStillOpen(): void {
if (this.didPeerCloseConnection) {
throw new Error('Connection was already closed');
}
}
}