@relaycorp/ws-mock
Advanced tools
Comparing version 4.1.2 to 4.2.0
"use strict"; | ||
// tslint:disable:readonly-keyword no-object-mutation | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -13,5 +14,5 @@ exports.MockClient = void 0; | ||
this.url = url; | ||
// tslint:disable-next-line:readonly-keyword | ||
this.connected = false; | ||
this.socket = new net_1.Socket(); | ||
// TODO: Propagate error somehow, instead of silencing it | ||
this.socket.on('error', (hadError) => { | ||
@@ -27,3 +28,2 @@ // tslint:disable-next-line:no-console | ||
const incomingMessage = new http_1.IncomingMessage(this.socket); | ||
// tslint:disable-next-line:no-object-mutation | ||
incomingMessage.headers = { | ||
@@ -33,5 +33,3 @@ ...incomingMessage.headers, | ||
}; | ||
// tslint:disable-next-line:no-object-mutation | ||
incomingMessage.url = this.url; | ||
// tslint:disable-next-line:no-object-mutation | ||
this.connected = true; | ||
@@ -38,0 +36,0 @@ // Only return once the server's own `connection` event handler has been been executed |
@@ -10,2 +10,3 @@ /// <reference types="node" /> | ||
protected wasAborted: boolean; | ||
constructor(); | ||
get didPeerCloseConnection(): boolean; | ||
@@ -12,0 +13,0 @@ protected get didICloseConnection(): boolean; |
"use strict"; | ||
// tslint:disable:readonly-keyword no-object-mutation | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -11,7 +12,10 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
constructor() { | ||
this.peerWebSocket = new MockWebSocket_1.MockWebSocket(); | ||
// tslint:disable-next-line:readonly-keyword | ||
this.ownCloseFrame = null; | ||
// tslint:disable-next-line:readonly-keyword | ||
this.wasAborted = false; | ||
this.peerWebSocket = new MockWebSocket_1.MockWebSocket(() => { | ||
if (!this.ownCloseFrame) { | ||
// The closing handshake was initiated by the other peer | ||
this.close(); | ||
} | ||
}); | ||
} | ||
@@ -25,3 +29,2 @@ get didPeerCloseConnection() { | ||
close(code = 1005, reason) { | ||
// tslint:disable-next-line:readonly-keyword no-object-mutation | ||
this.ownCloseFrame = { code, reason }; | ||
@@ -31,6 +34,5 @@ this.peerWebSocket.emit('close', code, reason); | ||
abort(error) { | ||
// tslint:disable-next-line:no-object-mutation | ||
this.wasAborted = true; | ||
this.peerWebSocket.emit('error', error); | ||
this.close(1006); | ||
this.peerWebSocket.emit('close', 1006, 'Aborted from test'); | ||
} | ||
@@ -37,0 +39,0 @@ async send(message) { |
@@ -5,2 +5,3 @@ "use strict"; | ||
const MockPeer_1 = require("./MockPeer"); | ||
const MockWebSocket_1 = require("./MockWebSocket"); | ||
class MockServer extends MockPeer_1.MockPeer { | ||
@@ -20,3 +21,3 @@ get client() { | ||
await new Promise(setImmediate); | ||
if (!this.didICloseConnection && !this.didPeerCloseConnection) { | ||
if (this.client.readyState === MockWebSocket_1.MockWebSocket.OPEN) { | ||
this.close(); | ||
@@ -23,0 +24,0 @@ } |
@@ -8,2 +8,3 @@ /// <reference types="node" /> | ||
export declare class MockWebSocket extends EventEmitter { | ||
protected readonly closingHandshakeCallback: () => void; | ||
static readonly CONNECTING: number; | ||
@@ -25,3 +26,3 @@ static readonly OPEN: number; | ||
protected readonly ownEvents: EventEmitter; | ||
constructor(); | ||
constructor(closingHandshakeCallback: () => void); | ||
get readyState(): number; | ||
@@ -28,0 +29,0 @@ send(data: WSData): void; |
"use strict"; | ||
// tslint:disable:readonly-keyword readonly-array no-object-mutation | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -8,4 +9,5 @@ exports.MockWebSocket = void 0; | ||
class MockWebSocket extends events_1.EventEmitter { | ||
constructor() { | ||
constructor(closingHandshakeCallback) { | ||
super(); | ||
this.closingHandshakeCallback = closingHandshakeCallback; | ||
this.CONNECTING = MockWebSocket.CONNECTING; | ||
@@ -15,21 +17,20 @@ this.OPEN = MockWebSocket.OPEN; | ||
this.CLOSED = MockWebSocket.CLOSED; | ||
// tslint:disable-next-line:readonly-keyword | ||
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 | ||
this._readyState = MockWebSocket.CONNECTING; | ||
// tslint:disable-next-line:readonly-keyword | ||
this.ownCloseFrame = null; | ||
// tslint:disable-next-line:readonly-keyword | ||
this._wasTerminated = false; | ||
// tslint:disable-next-line:readonly-array | ||
this.messagesSent = []; | ||
this.ownEvents = new events_1.EventEmitter(); | ||
this.once('open', () => { | ||
// tslint:disable-next-line:no-object-mutation | ||
this._readyState = MockWebSocket.OPEN; | ||
}); | ||
this.once('close', (code) => { | ||
if (this.readyState !== MockWebSocket.CLOSING && code !== 1006) { | ||
// The closing handshake was initiated by the other peer | ||
this.close(); | ||
} | ||
this._readyState = MockWebSocket.CLOSED; | ||
}); | ||
} | ||
@@ -73,7 +74,6 @@ get readyState() { | ||
} | ||
close(code, reason) { | ||
close(code = 1005, reason) { | ||
this.requireOpenConnection(); | ||
// tslint:disable-next-line:no-object-mutation | ||
this._readyState = MockWebSocket.CLOSED; | ||
// tslint:disable-next-line:no-object-mutation | ||
this._readyState = MockWebSocket.CLOSING; | ||
this.closingHandshakeCallback(); | ||
this.ownCloseFrame = { code, reason }; | ||
@@ -99,5 +99,3 @@ this.ownEvents.emit('close', this.ownCloseFrame); | ||
this.requireOpenConnection(); | ||
// tslint:disable-next-line:no-object-mutation | ||
this._readyState = MockWebSocket.CLOSED; | ||
// tslint:disable-next-line:no-object-mutation | ||
this._wasTerminated = true; | ||
@@ -115,8 +113,2 @@ this.ownEvents.emit('termination'); | ||
objectMode: true, | ||
destroy(error, callback) { | ||
if (!connection.wasTerminated) { | ||
connection.emit('close', error ? 1006 : 1005); | ||
} | ||
callback(error); | ||
}, | ||
read(_size) { | ||
@@ -123,0 +115,0 @@ // Do nothing. We're already recording incoming messages. |
@@ -0,1 +1,2 @@ | ||
// tslint:disable:readonly-keyword no-object-mutation | ||
import { IncomingMessage } from 'http'; | ||
@@ -9,3 +10,2 @@ import { Socket } from 'net'; | ||
socket; | ||
// tslint:disable-next-line:readonly-keyword | ||
connected = false; | ||
@@ -18,2 +18,3 @@ constructor(wsServer, headers = {}, url = '/') { | ||
this.socket = new Socket(); | ||
// TODO: Propagate error somehow, instead of silencing it | ||
this.socket.on('error', (hadError) => { | ||
@@ -29,3 +30,2 @@ // tslint:disable-next-line:no-console | ||
const incomingMessage = new IncomingMessage(this.socket); | ||
// tslint:disable-next-line:no-object-mutation | ||
incomingMessage.headers = { | ||
@@ -35,5 +35,3 @@ ...incomingMessage.headers, | ||
}; | ||
// tslint:disable-next-line:no-object-mutation | ||
incomingMessage.url = this.url; | ||
// tslint:disable-next-line:no-object-mutation | ||
this.connected = true; | ||
@@ -40,0 +38,0 @@ // Only return once the server's own `connection` event handler has been been executed |
@@ -10,2 +10,3 @@ /// <reference types="node" /> | ||
protected wasAborted: boolean; | ||
constructor(); | ||
get didPeerCloseConnection(): boolean; | ||
@@ -12,0 +13,0 @@ protected get didICloseConnection(): boolean; |
@@ -0,9 +1,16 @@ | ||
// tslint:disable:readonly-keyword no-object-mutation | ||
import bufferToArray from 'buffer-to-arraybuffer'; | ||
import { MockWebSocket } from './MockWebSocket'; | ||
export class MockPeer { | ||
peerWebSocket = new MockWebSocket(); | ||
// tslint:disable-next-line:readonly-keyword | ||
peerWebSocket; | ||
ownCloseFrame = null; | ||
// tslint:disable-next-line:readonly-keyword | ||
wasAborted = false; | ||
constructor() { | ||
this.peerWebSocket = new MockWebSocket(() => { | ||
if (!this.ownCloseFrame) { | ||
// The closing handshake was initiated by the other peer | ||
this.close(); | ||
} | ||
}); | ||
} | ||
get didPeerCloseConnection() { | ||
@@ -16,3 +23,2 @@ return this.peerWebSocket.closeFrame !== null || this.peerWebSocket.wasTerminated; | ||
close(code = 1005, reason) { | ||
// tslint:disable-next-line:readonly-keyword no-object-mutation | ||
this.ownCloseFrame = { code, reason }; | ||
@@ -22,6 +28,5 @@ this.peerWebSocket.emit('close', code, reason); | ||
abort(error) { | ||
// tslint:disable-next-line:no-object-mutation | ||
this.wasAborted = true; | ||
this.peerWebSocket.emit('error', error); | ||
this.close(1006); | ||
this.peerWebSocket.emit('close', 1006, 'Aborted from test'); | ||
} | ||
@@ -28,0 +33,0 @@ async send(message) { |
import { MockPeer } from './MockPeer'; | ||
import { MockWebSocket } from './MockWebSocket'; | ||
export class MockServer extends MockPeer { | ||
@@ -16,3 +17,3 @@ get client() { | ||
await new Promise(setImmediate); | ||
if (!this.didICloseConnection && !this.didPeerCloseConnection) { | ||
if (this.client.readyState === MockWebSocket.OPEN) { | ||
this.close(); | ||
@@ -19,0 +20,0 @@ } |
@@ -8,2 +8,3 @@ /// <reference types="node" /> | ||
export declare class MockWebSocket extends EventEmitter { | ||
protected readonly closingHandshakeCallback: () => void; | ||
static readonly CONNECTING: number; | ||
@@ -25,3 +26,3 @@ static readonly OPEN: number; | ||
protected readonly ownEvents: EventEmitter; | ||
constructor(); | ||
constructor(closingHandshakeCallback: () => void); | ||
get readyState(): number; | ||
@@ -28,0 +29,0 @@ send(data: WSData): void; |
@@ -0,1 +1,2 @@ | ||
// tslint:disable:readonly-keyword readonly-array no-object-mutation | ||
import { EventEmitter } from 'events'; | ||
@@ -5,2 +6,3 @@ import { Duplex } from 'stream'; | ||
export class MockWebSocket extends EventEmitter { | ||
closingHandshakeCallback; | ||
static CONNECTING = READY_STATES.indexOf('CONNECTING'); | ||
@@ -14,23 +16,23 @@ static OPEN = READY_STATES.indexOf('OPEN'); | ||
CLOSED = MockWebSocket.CLOSED; | ||
// tslint:disable-next-line:readonly-keyword | ||
binaryType = 'nodebuffer'; | ||
// tslint:disable-next-line:readonly-array | ||
outgoingPings = []; | ||
// tslint:disable-next-line:readonly-array | ||
outgoingPongs = []; | ||
// tslint:disable-next-line:readonly-keyword | ||
_readyState = MockWebSocket.CONNECTING; | ||
// tslint:disable-next-line:readonly-keyword | ||
ownCloseFrame = null; | ||
// tslint:disable-next-line:readonly-keyword | ||
_wasTerminated = false; | ||
// tslint:disable-next-line:readonly-array | ||
messagesSent = []; | ||
ownEvents = new EventEmitter(); | ||
constructor() { | ||
constructor(closingHandshakeCallback) { | ||
super(); | ||
this.closingHandshakeCallback = closingHandshakeCallback; | ||
this.once('open', () => { | ||
// tslint:disable-next-line:no-object-mutation | ||
this._readyState = MockWebSocket.OPEN; | ||
}); | ||
this.once('close', (code) => { | ||
if (this.readyState !== MockWebSocket.CLOSING && code !== 1006) { | ||
// The closing handshake was initiated by the other peer | ||
this.close(); | ||
} | ||
this._readyState = MockWebSocket.CLOSED; | ||
}); | ||
} | ||
@@ -74,7 +76,6 @@ get readyState() { | ||
} | ||
close(code, reason) { | ||
close(code = 1005, reason) { | ||
this.requireOpenConnection(); | ||
// tslint:disable-next-line:no-object-mutation | ||
this._readyState = MockWebSocket.CLOSED; | ||
// tslint:disable-next-line:no-object-mutation | ||
this._readyState = MockWebSocket.CLOSING; | ||
this.closingHandshakeCallback(); | ||
this.ownCloseFrame = { code, reason }; | ||
@@ -100,5 +101,3 @@ this.ownEvents.emit('close', this.ownCloseFrame); | ||
this.requireOpenConnection(); | ||
// tslint:disable-next-line:no-object-mutation | ||
this._readyState = MockWebSocket.CLOSED; | ||
// tslint:disable-next-line:no-object-mutation | ||
this._wasTerminated = true; | ||
@@ -116,8 +115,2 @@ this.ownEvents.emit('termination'); | ||
objectMode: true, | ||
destroy(error, callback) { | ||
if (!connection.wasTerminated) { | ||
connection.emit('close', error ? 1006 : 1005); | ||
} | ||
callback(error); | ||
}, | ||
read(_size) { | ||
@@ -124,0 +117,0 @@ // Do nothing. We're already recording incoming messages. |
{ | ||
"name": "@relaycorp/ws-mock", | ||
"version": "4.1.2", | ||
"version": "4.2.0", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "email": "no-reply@relaycorp.tech", |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
60087
978