Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@relaycorp/ws-mock

Package Overview
Dependencies
Maintainers
2
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@relaycorp/ws-mock - npm Package Compare versions

Comparing version 2.0.1 to 2.0.2

9

build/main/lib/MockPeer.d.ts

@@ -15,4 +15,11 @@ /// <reference types="node" />

get peerCloseFrame(): CloseFrame | null;
protected convertBinaryType(message: Buffer | ArrayBuffer | readonly Buffer[]): Buffer | ArrayBuffer | readonly Buffer[];
/**
* 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;
private requireConnectionStillOpen;
}

20

build/main/lib/MockPeer.js
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockPeer = void 0;
const buffer_to_arraybuffer_1 = __importDefault(require("buffer-to-arraybuffer"));
const MockWebSocket_1 = require("./MockWebSocket");

@@ -20,3 +24,5 @@ class MockPeer {

this.requireConnectionStillOpen();
const messageSerialized = typeof message === 'string' ? message : this.convertBinaryType(message);
const messageSerialized = typeof message === 'string' || Array.isArray(message)
? message
: this.convertBinaryType(message);
return new Promise((resolve) => {

@@ -44,8 +50,14 @@ this.peerWebSocket.once('message', resolve);

}
/**
* Mimic the conversion that `ws` would do on binary frames.
*
* @param message
*
* See https://github.com/websockets/ws/blob/master/doc/ws.md#websocketbinarytype
*/
convertBinaryType(message) {
const binaryType = this.peerWebSocket.binaryType;
if (binaryType === 'nodebuffer') {
if (this.peerWebSocket.binaryType === 'nodebuffer') {
return Buffer.isBuffer(message) ? message : Buffer.from(message);
}
throw new Error(`Unsupported WebSocket.binaryType (${binaryType}); feel free to open a PR`);
return Buffer.isBuffer(message) ? buffer_to_arraybuffer_1.default(message) : message;
}

@@ -52,0 +64,0 @@ requireConnectionStillOpen() {

@@ -5,2 +5,6 @@ import { IncomingMessage } from 'http';

export class MockClient extends MockPeer {
wsServer;
headers;
url;
socket;
constructor(wsServer, headers = {}, url = '/') {

@@ -7,0 +11,0 @@ super();

@@ -15,4 +15,11 @@ /// <reference types="node" />

get peerCloseFrame(): CloseFrame | null;
protected convertBinaryType(message: Buffer | ArrayBuffer | readonly Buffer[]): Buffer | ArrayBuffer | readonly Buffer[];
/**
* 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;
private requireConnectionStillOpen;
}

@@ -0,6 +1,5 @@

import bufferToArray from 'buffer-to-arraybuffer';
import { MockWebSocket } from './MockWebSocket';
export class MockPeer {
constructor() {
this.peerWebSocket = new MockWebSocket();
}
peerWebSocket = new MockWebSocket();
get wasConnectionClosed() {

@@ -17,3 +16,5 @@ return this.peerWebSocket.closeFrame !== null;

this.requireConnectionStillOpen();
const messageSerialized = typeof message === 'string' ? message : this.convertBinaryType(message);
const messageSerialized = typeof message === 'string' || Array.isArray(message)
? message
: this.convertBinaryType(message);
return new Promise((resolve) => {

@@ -41,8 +42,14 @@ this.peerWebSocket.once('message', resolve);

}
/**
* Mimic the conversion that `ws` would do on binary frames.
*
* @param message
*
* See https://github.com/websockets/ws/blob/master/doc/ws.md#websocketbinarytype
*/
convertBinaryType(message) {
const binaryType = this.peerWebSocket.binaryType;
if (binaryType === 'nodebuffer') {
if (this.peerWebSocket.binaryType === 'nodebuffer') {
return Buffer.isBuffer(message) ? message : Buffer.from(message);
}
throw new Error(`Unsupported WebSocket.binaryType (${binaryType}); feel free to open a PR`);
return Buffer.isBuffer(message) ? bufferToArray(message) : message;
}

@@ -49,0 +56,0 @@ requireConnectionStillOpen() {

// tslint:disable:max-classes-per-file
export class MockServerAction {
constructor() {
// tslint:disable-next-line:readonly-keyword
this._wasRun = false;
}
// tslint:disable-next-line:readonly-keyword
_wasRun = false;
get wasRun() {

@@ -24,2 +22,4 @@ return this._wasRun;

export class CloseConnectionAction extends MockServerAction {
code;
reason;
constructor(code = 1000, reason) {

@@ -36,2 +36,3 @@ super();

export class SendMessageAction extends MockServerAction {
message;
constructor(message) {

@@ -47,7 +48,4 @@ super();

export class ReceiveMessageAction extends MockServerAction {
constructor() {
super(...arguments);
// tslint:disable-next-line:readonly-keyword
this._message = null;
}
// tslint:disable-next-line:readonly-keyword
_message = null;
get message() {

@@ -63,2 +61,3 @@ return this._message;

export class EmitClientErrorAction extends MockServerAction {
error;
constructor(error) {

@@ -65,0 +64,0 @@ super();

import { EventEmitter } from 'events';
import { Duplex } from 'stream';
export class MockWebSocket extends EventEmitter {
constructor() {
super(...arguments);
// tslint:disable-next-line:readonly-keyword
this.binaryType = 'nodebuffer';
// tslint:disable-next-line:readonly-keyword
this.ownCloseFrame = null;
// tslint:disable-next-line:readonly-array
this.messagesSent = [];
this.ownEvents = new EventEmitter();
}
// tslint:disable-next-line:readonly-keyword
binaryType = 'nodebuffer';
// tslint:disable-next-line:readonly-keyword
ownCloseFrame = null;
// tslint:disable-next-line:readonly-array
messagesSent = [];
ownEvents = new EventEmitter();
send(data) {

@@ -15,0 +12,0 @@ this.messagesSent.push(data);

{
"name": "@relaycorp/ws-mock",
"version": "2.0.1",
"version": "2.0.2",
"author": {

@@ -30,6 +30,7 @@ "email": "no-reply@relaycorp.tech",

"engines": {
"node": ">=10"
"node": ">=12"
},
"dependencies": {
"ws": "^7.4.6"
"buffer-to-arraybuffer": "0.0.6",
"ws": "^7.5.0"
},

@@ -41,3 +42,3 @@ "peerDependencies": {

"@relaycorp/shared-config": "^1.4.15",
"@types/ws": "^7.4.2",
"@types/ws": "^7.4.5",
"del-cli": "^4.0.0",

@@ -48,4 +49,4 @@ "npm-run-all": "^4.1.5",

"tslint": "^5.20.1",
"typedoc": "^0.21.0",
"typescript": "^4.2.4"
"typedoc": "^0.21.1",
"typescript": "^4.3.4"
},

@@ -52,0 +53,0 @@ "prettier": "@relaycorp/shared-config/.prettierrc.json",

# @relaycorp/ws-mock
Mock client and server to unit test the NPM package [`ws`](https://www.npmjs.com/package/ws).
Mock client and server to unit test servers and clients powered by [`ws`](https://www.npmjs.com/package/ws).

@@ -13,3 +13,3 @@ ## Install

You should initialise `MockClient` by passing the `ws` server to be tested and then call `client.connect()` to initiate the connection. From that point you can interact with the server. For example:
You'd use a mock client when you need to test a server. You should initialise `MockClient` by passing the `ws` server to be tested and then call `client.connect()` to initiate the connection. From that point you can interact with the server. For example:

@@ -26,7 +26,7 @@ ```javascript

You'll find [real-world examples in relaycorp/relaynet-internet-gateway](https://github.com/relaycorp/relaynet-internet-gateway/search?l=TypeScript&q=%22%40relaycorp%2Fws-mock%22).
You'll find real-world examples in [relaycorp/relaynet-internet-gateway](https://github.com/relaycorp/relaynet-internet-gateway/search?l=TypeScript&q=%22%40relaycorp%2Fws-mock%22) and [relaycorp/awala-gateway-desktop](https://github.com/relaycorp/awala-gateway-desktop/search?l=TypeScript&q=%22%40relaycorp%2Fws-mock%22).
## Using the mock server
You basically need to initialise `MockServer` and replace the default export from `ws` with a mock WebSocket. Here's an example with Jest:
You'd use a mock server when you need to test a client. You basically need to initialise `MockServer` and replace the default export from `ws` with a mock WebSocket. Here's an example with Jest:

@@ -33,0 +33,0 @@ ```javascript

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc