@relaycorp/ws-mock
Advanced tools
Comparing version 2.0.4 to 2.1.0
export { MockClient } from './lib/MockClient'; | ||
export { MockPeer } from './lib/MockPeer'; | ||
export { MockServer } from './lib/MockServer'; | ||
export { PingOrPong } from './lib/PingOrPong'; | ||
export * from './lib/MockServerAction'; | ||
export { CloseFrame } from './lib/CloseFrame'; | ||
export { createMockWebSocketStream } from './lib/stream'; |
@@ -5,2 +5,3 @@ /// <reference types="node" /> | ||
import { MockWebSocket } from './MockWebSocket'; | ||
import { PingOrPong } from './PingOrPong'; | ||
export declare abstract class MockPeer { | ||
@@ -17,2 +18,22 @@ protected readonly peerWebSocket: MockWebSocket; | ||
/** | ||
* Send ping to peer and return ping data. | ||
* | ||
* @param data | ||
*/ | ||
ping(data?: Buffer): Buffer; | ||
/** | ||
* Send pong to peer. | ||
* | ||
* @param data | ||
*/ | ||
pong(data: Buffer): void; | ||
/** | ||
* Return pings sent by peer. | ||
*/ | ||
get incomingPings(): readonly PingOrPong[]; | ||
/** | ||
* Return pongs sent by peer. | ||
*/ | ||
get incomingPongs(): readonly PingOrPong[]; | ||
/** | ||
* Mimic the conversion that `ws` would do on binary frames. | ||
@@ -19,0 +40,0 @@ * |
@@ -50,2 +50,34 @@ "use strict"; | ||
/** | ||
* Send ping to peer and return ping data. | ||
* | ||
* @param data | ||
*/ | ||
ping(data) { | ||
const finalData = data !== null && data !== void 0 ? data : Buffer.from(Math.random().toString()); | ||
this.peerWebSocket.emit('ping', finalData); | ||
return finalData; | ||
} | ||
/** | ||
* Send pong to peer. | ||
* | ||
* @param data | ||
*/ | ||
pong(data) { | ||
this.peerWebSocket.emit('pong', data); | ||
} | ||
/** | ||
* Return pings sent by peer. | ||
*/ | ||
get incomingPings() { | ||
// Return a shallow copy to avoid race conditions if more pings are received | ||
return [...this.peerWebSocket.outgoingPings]; | ||
} | ||
/** | ||
* Return pongs sent by peer. | ||
*/ | ||
get incomingPongs() { | ||
// 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. | ||
@@ -52,0 +84,0 @@ * |
@@ -6,4 +6,7 @@ /// <reference types="node" /> | ||
import { CloseFrame } from './CloseFrame'; | ||
import { PingOrPong } from './PingOrPong'; | ||
export declare class MockWebSocket extends EventEmitter { | ||
binaryType: 'nodebuffer' | 'arraybuffer'; | ||
readonly outgoingPings: PingOrPong[]; | ||
readonly outgoingPongs: PingOrPong[]; | ||
protected ownCloseFrame: CloseFrame | null; | ||
@@ -13,4 +16,7 @@ protected readonly messagesSent: WSData[]; | ||
send(data: WSData): void; | ||
ping(data?: WSData, mask?: boolean, cb?: (err?: Error) => void): void; | ||
pong(data?: WSData, mask?: boolean, cb?: (err?: Error) => void): void; | ||
close(code?: number, reason?: string): void; | ||
makeDuplex(): Duplex; | ||
private requireOpenConnection; | ||
} |
@@ -11,2 +11,6 @@ "use strict"; | ||
this.binaryType = 'nodebuffer'; | ||
// tslint:disable-next-line:readonly-array | ||
this.outgoingPings = []; | ||
// tslint:disable-next-line:readonly-array | ||
this.outgoingPongs = []; | ||
// tslint:disable-next-line:readonly-keyword | ||
@@ -19,5 +23,20 @@ this.ownCloseFrame = null; | ||
send(data) { | ||
this.requireOpenConnection(); | ||
this.messagesSent.push(data); | ||
this.ownEvents.emit('messageSent', data); | ||
} | ||
ping(data, mask, cb) { | ||
this.requireOpenConnection(); | ||
this.outgoingPings.push({ data, cb, mask, date: new Date() }); | ||
if (cb) { | ||
cb(); | ||
} | ||
} | ||
pong(data, mask, cb) { | ||
this.requireOpenConnection(); | ||
this.outgoingPongs.push({ data, cb, mask, date: new Date() }); | ||
if (cb) { | ||
cb(); | ||
} | ||
} | ||
/** | ||
@@ -39,5 +58,3 @@ * @internal | ||
close(code, reason) { | ||
if (this.ownCloseFrame) { | ||
throw new Error('Connection closure was already initiated'); | ||
} | ||
this.requireOpenConnection(); | ||
// tslint:disable-next-line:no-object-mutation | ||
@@ -84,2 +101,7 @@ this.ownCloseFrame = { code, reason }; | ||
} | ||
requireOpenConnection() { | ||
if (this.ownCloseFrame) { | ||
throw new Error('Connection is not open (anymore)'); | ||
} | ||
} | ||
} | ||
@@ -86,0 +108,0 @@ exports.MockWebSocket = MockWebSocket; |
export { MockClient } from './lib/MockClient'; | ||
export { MockPeer } from './lib/MockPeer'; | ||
export { MockServer } from './lib/MockServer'; | ||
export { PingOrPong } from './lib/PingOrPong'; | ||
export * from './lib/MockServerAction'; | ||
export { CloseFrame } from './lib/CloseFrame'; | ||
export { createMockWebSocketStream } from './lib/stream'; |
@@ -5,2 +5,3 @@ /// <reference types="node" /> | ||
import { MockWebSocket } from './MockWebSocket'; | ||
import { PingOrPong } from './PingOrPong'; | ||
export declare abstract class MockPeer { | ||
@@ -17,2 +18,22 @@ protected readonly peerWebSocket: MockWebSocket; | ||
/** | ||
* Send ping to peer and return ping data. | ||
* | ||
* @param data | ||
*/ | ||
ping(data?: Buffer): Buffer; | ||
/** | ||
* Send pong to peer. | ||
* | ||
* @param data | ||
*/ | ||
pong(data: Buffer): void; | ||
/** | ||
* Return pings sent by peer. | ||
*/ | ||
get incomingPings(): readonly PingOrPong[]; | ||
/** | ||
* Return pongs sent by peer. | ||
*/ | ||
get incomingPongs(): readonly PingOrPong[]; | ||
/** | ||
* Mimic the conversion that `ws` would do on binary frames. | ||
@@ -19,0 +40,0 @@ * |
@@ -42,2 +42,34 @@ import bufferToArray from 'buffer-to-arraybuffer'; | ||
/** | ||
* Send ping to peer and return ping data. | ||
* | ||
* @param data | ||
*/ | ||
ping(data) { | ||
const finalData = data ?? Buffer.from(Math.random().toString()); | ||
this.peerWebSocket.emit('ping', finalData); | ||
return finalData; | ||
} | ||
/** | ||
* Send pong to peer. | ||
* | ||
* @param data | ||
*/ | ||
pong(data) { | ||
this.peerWebSocket.emit('pong', data); | ||
} | ||
/** | ||
* Return pings sent by peer. | ||
*/ | ||
get incomingPings() { | ||
// Return a shallow copy to avoid race conditions if more pings are received | ||
return [...this.peerWebSocket.outgoingPings]; | ||
} | ||
/** | ||
* Return pongs sent by peer. | ||
*/ | ||
get incomingPongs() { | ||
// 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. | ||
@@ -44,0 +76,0 @@ * |
@@ -6,4 +6,7 @@ /// <reference types="node" /> | ||
import { CloseFrame } from './CloseFrame'; | ||
import { PingOrPong } from './PingOrPong'; | ||
export declare class MockWebSocket extends EventEmitter { | ||
binaryType: 'nodebuffer' | 'arraybuffer'; | ||
readonly outgoingPings: PingOrPong[]; | ||
readonly outgoingPongs: PingOrPong[]; | ||
protected ownCloseFrame: CloseFrame | null; | ||
@@ -13,4 +16,7 @@ protected readonly messagesSent: WSData[]; | ||
send(data: WSData): void; | ||
ping(data?: WSData, mask?: boolean, cb?: (err?: Error) => void): void; | ||
pong(data?: WSData, mask?: boolean, cb?: (err?: Error) => void): void; | ||
close(code?: number, reason?: string): void; | ||
makeDuplex(): Duplex; | ||
private requireOpenConnection; | ||
} |
@@ -6,2 +6,6 @@ import { EventEmitter } from 'events'; | ||
binaryType = 'nodebuffer'; | ||
// tslint:disable-next-line:readonly-array | ||
outgoingPings = []; | ||
// tslint:disable-next-line:readonly-array | ||
outgoingPongs = []; | ||
// tslint:disable-next-line:readonly-keyword | ||
@@ -13,5 +17,20 @@ ownCloseFrame = null; | ||
send(data) { | ||
this.requireOpenConnection(); | ||
this.messagesSent.push(data); | ||
this.ownEvents.emit('messageSent', data); | ||
} | ||
ping(data, mask, cb) { | ||
this.requireOpenConnection(); | ||
this.outgoingPings.push({ data, cb, mask, date: new Date() }); | ||
if (cb) { | ||
cb(); | ||
} | ||
} | ||
pong(data, mask, cb) { | ||
this.requireOpenConnection(); | ||
this.outgoingPongs.push({ data, cb, mask, date: new Date() }); | ||
if (cb) { | ||
cb(); | ||
} | ||
} | ||
/** | ||
@@ -33,5 +52,3 @@ * @internal | ||
close(code, reason) { | ||
if (this.ownCloseFrame) { | ||
throw new Error('Connection closure was already initiated'); | ||
} | ||
this.requireOpenConnection(); | ||
// tslint:disable-next-line:no-object-mutation | ||
@@ -78,2 +95,7 @@ this.ownCloseFrame = { code, reason }; | ||
} | ||
requireOpenConnection() { | ||
if (this.ownCloseFrame) { | ||
throw new Error('Connection is not open (anymore)'); | ||
} | ||
} | ||
} | ||
@@ -80,0 +102,0 @@ async function waitForEvent(eventName, eventEmitter) { |
{ | ||
"name": "@relaycorp/ws-mock", | ||
"version": "2.0.4", | ||
"version": "2.1.0", | ||
"author": { | ||
@@ -41,4 +41,4 @@ "email": "no-reply@relaycorp.tech", | ||
"@relaycorp/shared-config": "^1.5.3", | ||
"@types/ws": "^7.4.6", | ||
"del-cli": "^4.0.0", | ||
"@types/ws": "^7.4.7", | ||
"del-cli": "^4.0.1", | ||
"npm-run-all": "^4.1.5", | ||
@@ -48,3 +48,3 @@ "prettier": "^2.3.2", | ||
"tslint": "^5.20.1", | ||
"typedoc": "^0.21.2", | ||
"typedoc": "^0.21.4", | ||
"typescript": "^4.3.5" | ||
@@ -51,0 +51,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
58232
57
991