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

@anephenix/sarus

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@anephenix/sarus - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

12

__tests__/index/connectionOptions.test.ts

@@ -5,5 +5,5 @@ // File Dependencies

const url = "ws://localhost:1234";
const stringProtocol = "hybi-00";
const arrayOfProtocols = ["hybi-07", "hybi-00"];
const url: string = "ws://localhost:1234";
const stringProtocol: string = "hybi-00";
const arrayOfProtocols: Array<string> = ["hybi-07", "hybi-00"];

@@ -22,3 +22,3 @@ describe("connection options", () => {

it("should set the WebSocket protocols value to an empty string if nothing is passed", async () => {
const sarus = new Sarus({ url });
const sarus: Sarus = new Sarus({ url });
await server.connected;

@@ -29,3 +29,3 @@ expect(sarus.ws?.protocol).toBe("");

it("should set the WebSocket protocols value to a string if a string is passed", async () => {
const sarus = new Sarus({ url, protocols: stringProtocol });
const sarus: Sarus = new Sarus({ url, protocols: stringProtocol });
await server.connected;

@@ -36,3 +36,3 @@ expect(sarus.ws?.protocol).toBe(stringProtocol);

it("should set the WebSocket protocols value to the first value in an array if an array is passed", async () => {
const sarus = new Sarus({ url, protocols: arrayOfProtocols });
const sarus: Sarus = new Sarus({ url, protocols: arrayOfProtocols });
await server.connected;

@@ -39,0 +39,0 @@ expect(sarus.ws?.protocol).toBe(arrayOfProtocols[0]);

@@ -7,4 +7,4 @@ // File Dependencies

it("should serialize the javascript variable into a JSON string", () => {
const payload = { name: "Paul Jensen" };
const serializedPayload = serialize(payload);
const payload: object = { name: "Paul Jensen" };
const serializedPayload: string = serialize(payload);
expect(serializedPayload).toEqual(JSON.stringify(payload));

@@ -18,4 +18,4 @@ });

it("should return the deserialized data", () => {
const payload = { name: "Paul Jensen" };
const serialisedPayload = serialize(payload);
const payload: object = { name: "Paul Jensen" };
const serialisedPayload: string = serialize(payload);
expect(deserialize(serialisedPayload)).toEqual(payload);

@@ -22,0 +22,0 @@ });

@@ -9,4 +9,4 @@ // File Dependencies

it("should disconnect from the WebSocket server, and disable automatic reconnections", async () => {
const server = new WS(url);
const sarus = new Sarus({ url, reconnectAutomatically: true });
const server: WS = new WS(url);
const sarus: Sarus = new Sarus({ url, reconnectAutomatically: true });
const mockReconnect = jest.fn();

@@ -24,4 +24,4 @@ sarus.reconnect = mockReconnect;

it("should allow the developer to override disabling automatica reconnections", async () => {
const server = new WS(url);
const sarus = new Sarus({ url, reconnectAutomatically: true });
const server: WS = new WS(url);
const sarus: Sarus = new Sarus({ url, reconnectAutomatically: true });
const mockReconnect = jest.fn();

@@ -28,0 +28,0 @@ sarus.reconnect = mockReconnect;

@@ -9,3 +9,3 @@ // File Dependencies

it("should bind eventListeners that are passed during initialization", async () => {
const server = new WS("ws://localhost:1234");
const server: WS = new WS("ws://localhost:1234");
const mockOpen = jest.fn();

@@ -48,4 +48,4 @@ const mockParseMessage = jest.fn();

it("should prefill any missing eventListener events during initialization", () => {
const myFunc = () => {};
const sarus = new Sarus({
const myFunc: Function = () => {};
const sarus: Sarus = new Sarus({
url,

@@ -84,5 +84,5 @@ eventListeners: {

it("should allow an event listener to be added after initialization", () => {
const myFunc = () => {};
const anotherFunc = () => {};
const sarus = new Sarus({
const myFunc: Function = () => {};
const anotherFunc: Function = () => {};
const sarus: Sarus = new Sarus({
url,

@@ -102,6 +102,6 @@ eventListeners: {

it("should bind any added event listeners after initialization to the WebSocket", async () => {
const server = new WS(url);
const server: WS = new WS(url);
const myFunc = jest.fn();
const anotherFunc = jest.fn();
const sarus = new Sarus({
const sarus: Sarus = new Sarus({
url,

@@ -127,4 +127,4 @@ eventListeners: {

it("should allow an event listener to be removed by passing the function name", () => {
const myFunc = () => {};
const sarus = new Sarus({
const myFunc: Function = () => {};
const sarus: Sarus = new Sarus({
url,

@@ -144,4 +144,4 @@ eventListeners: {

it("should allow an event listener to be removed by passing the function", () => {
const myFunc = () => {};
const sarus = new Sarus({
const myFunc: Function = () => {};
const sarus: Sarus = new Sarus({
url,

@@ -161,5 +161,5 @@ eventListeners: {

it("should throw an error if a function cannot be found when trying to remove it from an event listener", () => {
const myFunc = () => {};
const anotherFunc = () => {};
const sarus = new Sarus({
const myFunc: Function = () => {};
const anotherFunc: Function = () => {};
const sarus: Sarus = new Sarus({
url,

@@ -181,5 +181,5 @@ eventListeners: {

it("should throw an error if a function name cannot be found when trying to remove it from an event listener", () => {
const myFunc = () => {};
const anotherFunc = () => {};
const sarus = new Sarus({
const myFunc: Function = () => {};
const anotherFunc: Function = () => {};
const sarus: Sarus = new Sarus({
url,

@@ -201,5 +201,5 @@ eventListeners: {

it("should not throw an error, if a function cannot be found when trying to remove it from an event listener, and additional doNotThrowError is passed", () => {
const myFunc = () => {};
const anotherFunc = () => {};
const sarus = new Sarus({
const myFunc: Function = () => {};
const anotherFunc: Function = () => {};
const sarus: Sarus = new Sarus({
url,

@@ -206,0 +206,0 @@ eventListeners: {

@@ -5,3 +5,3 @@ // File Dependencies

const url = "ws://localhost:1234";
const url: string = "ws://localhost:1234";

@@ -11,4 +11,4 @@ describe("message queue", () => {

it("should queue messages for delivery", async () => {
const server = new WS(url);
const sarus = new Sarus({ url });
const server: WS = new WS(url);
const sarus: Sarus = new Sarus({ url });
await server.connected;

@@ -25,4 +25,4 @@ sarus.send("Hello server");

it("should queue messages for delivery when server is offline for a bit", async () => {
const server = new WS(url);
const sarus = new Sarus({ url });
const server: WS = new WS(url);
const sarus: Sarus = new Sarus({ url });
await server.connected;

@@ -47,3 +47,3 @@ sarus.send("Hello server");

it("should allow the developer to provide a custom retryProcessTimePeriod", () => {
const sarus = new Sarus({ url, retryProcessTimePeriod: 25 });
const sarus: Sarus = new Sarus({ url, retryProcessTimePeriod: 25 });
expect(sarus.retryProcessTimePeriod).toBe(25);

@@ -57,4 +57,4 @@ });

storageType.clear();
const server = new WS(url);
const sarus = new Sarus(sarusConfig);
const server: WS = new WS(url);
const sarus: Sarus = new Sarus(sarusConfig);
expect(sarus.storageType).toBe(sarusConfig.storageType);

@@ -87,3 +87,3 @@ await server.connected;

it("should allow the developer to use a custom storageKey", () => {
const sarus = new Sarus({
const sarus: Sarus = new Sarus({
url,

@@ -97,3 +97,3 @@ storageType: "local",

const retrieveMessagesFromStorage = (sarusConfig: SarusClassParams) => {
let sarusOne = new Sarus(sarusConfig);
let sarusOne: Sarus = new Sarus(sarusConfig);
expect(sarusOne.messages).toEqual([]);

@@ -103,3 +103,3 @@ sarusOne.send("Hello world");

sarusOne.disconnect();
const sarusTwo = new Sarus(sarusConfig);
const sarusTwo: Sarus = new Sarus(sarusConfig);
expect(sarusTwo.messages).toEqual(["Hello world", "Hello again"]);

@@ -113,3 +113,3 @@ return sarusTwo;

const sarusTwo = retrieveMessagesFromStorage(sarusConfig);
const server = new WS(url);
const server: WS = new WS(url);
const messageOne = await server.nextMessage;

@@ -116,0 +116,0 @@ const messageTwo = await server.nextMessage;

@@ -5,9 +5,9 @@ // File Dependencies

const url = "ws://localhost:1234";
const url: string = "ws://localhost:1234";
describe("automatic reconnectivity", () => {
it("should reconnect the WebSocket connection when it is severed", async () => {
const server = new WS(url);
const server: WS = new WS(url);
const mockConnect = jest.fn();
const sarus = new Sarus({ url });
const sarus: Sarus = new Sarus({ url });
await server.connected;

@@ -20,5 +20,5 @@ sarus.connect = mockConnect;

it("should not reconnect if automatic reconnection is disabled", async () => {
const server = new WS(url);
const server: WS = new WS(url);
const mockConnect = jest.fn();
const sarus = new Sarus({
const sarus: Sarus = new Sarus({
url,

@@ -25,0 +25,0 @@ reconnectAutomatically: false

@@ -7,4 +7,2 @@ // File Dependencies

const delay = (duration: number) =>
new Promise(resolve => setTimeout(resolve, duration));
const condition = (func: Function) => {

@@ -11,0 +9,0 @@ return new Promise(resolve => {

@@ -5,8 +5,8 @@ // File Dependencies

const url = "ws://localhost:1234";
const url: string = "ws://localhost:1234";
describe("sending websocket messages", () => {
it("should send a message to the WebSocket server", async () => {
const server = new WS(url);
const sarus = new Sarus({ url });
const server: WS = new WS(url);
const sarus: Sarus = new Sarus({ url });
await server.connected;

@@ -13,0 +13,0 @@ sarus.send("Hello server");

# CHANGELOG
### 0.3.1 - Wednesday 25th March, 2020
- Fixed GitHub issue #14 (Cannot use import statement outside a module)
- Shipped module is now compiled as a CommonJS module
### 0.3.0 - Friday 6th March, 2020

@@ -4,0 +9,0 @@

@@ -0,4 +1,6 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// File Dependencies
import { WS_EVENT_NAMES, DATA_STORAGE_TYPES } from "./lib/constants";
import { serialize, deserialize } from "./lib/dataTransformer";
const constants_1 = require("./lib/constants");
const dataTransformer_1 = require("./lib/dataTransformer");
/**

@@ -26,6 +28,6 @@ * Retrieves the storage API for the browser

const getMessagesFromStore = ({ storageType, storageKey }) => {
if (DATA_STORAGE_TYPES.indexOf(storageType) !== -1) {
if (constants_1.DATA_STORAGE_TYPES.indexOf(storageType) !== -1) {
const storage = getStorage(storageType);
const rawData = (storage && storage.getItem(storageKey)) || null;
return deserialize(rawData) || [];
return dataTransformer_1.deserialize(rawData) || [];
}

@@ -47,3 +49,3 @@ };

*/
export default class Sarus {
class Sarus {
constructor(props) {

@@ -125,6 +127,6 @@ // Extract the properties that are passed to the class

const { storageType, storageKey } = this;
if (DATA_STORAGE_TYPES.indexOf(storageType) !== -1) {
if (constants_1.DATA_STORAGE_TYPES.indexOf(storageType) !== -1) {
const storage = getStorage(storageType);
if (storage)
storage.setItem(storageKey, serialize(data));
storage.setItem(storageKey, dataTransformer_1.serialize(data));
}

@@ -142,3 +144,3 @@ if (storageType === "memory") {

const { messages, storageType } = this;
if (DATA_STORAGE_TYPES.indexOf(storageType) === -1)
if (constants_1.DATA_STORAGE_TYPES.indexOf(storageType) === -1)
return null;

@@ -169,3 +171,3 @@ return (this.messages = [...messages, data]);

const { messages, storageType } = this;
if (DATA_STORAGE_TYPES.indexOf(storageType) === -1) {
if (constants_1.DATA_STORAGE_TYPES.indexOf(storageType) === -1) {
return this.messages.shift();

@@ -340,3 +342,3 @@ }

const self = this;
WS_EVENT_NAMES.forEach(eventName => {
constants_1.WS_EVENT_NAMES.forEach(eventName => {
self.ws[`on${eventName}`] = (e) => {

@@ -351,1 +353,2 @@ self.eventListeners[eventName].forEach((f) => f(e));

}
exports.default = Sarus;

@@ -0,1 +1,3 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**

@@ -6,3 +8,3 @@ * A definitive list of events for a WebSocket client to listen on

*/
export const WS_EVENT_NAMES = [
exports.WS_EVENT_NAMES = [
"open",

@@ -18,2 +20,2 @@ "close",

*/
export const DATA_STORAGE_TYPES = ["session", "local"];
exports.DATA_STORAGE_TYPES = ["session", "local"];

@@ -0,1 +1,3 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**

@@ -6,3 +8,3 @@ * Serializes the data for storing in sessionStorage/localStorage

*/
export const serialize = (data) => JSON.stringify(data);
exports.serialize = (data) => JSON.stringify(data);
/**

@@ -13,3 +15,3 @@ * Deserializes the data stored in sessionStorage/localStorage

*/
export const deserialize = (data) => {
exports.deserialize = (data) => {
if (!data)

@@ -16,0 +18,0 @@ return null;

@@ -5,3 +5,6 @@ module.exports = {

clearMocks: true,
coverageDirectory: "coverage"
coverageDirectory: "coverage",
transform: {
"//.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
}
};
{
"name": "@anephenix/sarus",
"version": "0.3.0",
"version": "0.3.1",
"description": "A WebSocket JavaScript library",
"main": "dist/index.js",
"devDependencies": {
"@babel/parser": "^7.8.3",
"@babel/types": "^7.8.3",
"@types/jest": "^24.9.0",
"@babel/parser": "^7.9.0",
"@babel/types": "^7.9.0",
"@types/jest": "^24.9.1",
"@types/websocket": "^1.0.0",
"coveralls": "^3.0.3",
"coveralls": "^3.0.11",
"dom-storage": "^2.1.0",
"jest": "^24.9.0",
"jest-websocket-mock": "^2.0.0",
"jest": "^25.1.0",
"jest-websocket-mock": "^2.0.2",
"jsdoc": "^3.6.3",
"jsdom": "^16.0.0",
"mock-socket": "^9.0.0",
"ts-jest": "^24.3.0",
"typescript": "^3.7.5"
"jsdom": "^16.2.1",
"mock-socket": "^9.0.3",
"ts-jest": "^25.2.1",
"typescript": "^3.8.3"
},
"scripts": {
"build": "tsc --project tsconfig.json",
"watch": "tsc --project tsconfig.json --watch",
"cover": "jest --coverage --coverageReporters=text-lcov | coveralls",
"test": "jest --coverage"
"build": "npx tsc --project tsconfig.json",
"watch": "npx tsc --project tsconfig.json --watch",
"cover": "npx jest --coverage --coverageReporters=text-lcov | coveralls",
"test": "npx jest --coverage"
},

@@ -27,0 +27,0 @@ "repository": {

@@ -5,6 +5,3 @@ # Sarus

[![npm version](https://badge.fury.io/js/%40anephenix%2Fsarus.svg)](https://badge.fury.io/js/%40anephenix%2Fsarus)
[![CircleCI](https://circleci.com/gh/anephenix/sarus.svg?style=shield)](https://circleci.com/gh/anephenix/sarus)
[![Coverage Status](https://coveralls.io/repos/github/anephenix/sarus/badge.svg?branch=master)](https://coveralls.io/github/anephenix/sarus?branch=master)
[![npm version](https://badge.fury.io/js/%40anephenix%2Fsarus.svg)](https://badge.fury.io/js/%40anephenix%2Fsarus) [![CircleCI](https://circleci.com/gh/anephenix/sarus.svg?style=shield)](https://circleci.com/gh/anephenix/sarus) [![Coverage Status](https://coveralls.io/repos/github/anephenix/sarus/badge.svg?branch=master)](https://coveralls.io/github/anephenix/sarus?branch=master)
[![Maintainability](https://api.codeclimate.com/v1/badges/0671cfc9630a97854b30/maintainability)](https://codeclimate.com/github/anephenix/sarus/maintainability)

@@ -33,6 +30,6 @@

```javascript
import Sarus from '@anephenix/sarus';
import Sarus from "@anephenix/sarus";
const sarus = new Sarus({
url: 'wss://ws.anephenix.com'
url: "wss://ws.anephenix.com"
});

@@ -53,3 +50,3 @@ ```

// Log a message that the connection is open
const noteOpened = () => console.log('Connection opened');
const noteOpened = () => console.log("Connection opened");

@@ -64,3 +61,3 @@ // Assuming that the WebSocket server is sending JSON data,

// Log a message that the connection has closed
const noteClosed = () => console.log('Connection closed');
const noteClosed = () => console.log("Connection closed");

@@ -72,3 +69,3 @@ // If an error occurs, throw the error

const sarus = new Sarus({
url: 'wss://ws.anephenix.com',
url: "wss://ws.anephenix.com",
eventListeners: {

@@ -92,3 +89,3 @@ open: [noteOpened],

const store = window.localStorage;
let record = store.getItem('messages');
let record = store.getItem("messages");
if (!record) {

@@ -100,3 +97,3 @@ record = [];

record.push(event.data);
store.setItem('messages', JSON.stringify(record));
store.setItem("messages", JSON.stringify(record));
};

@@ -106,3 +103,3 @@

// the WebSocket server
sarus.on('message', storeMessage);
sarus.on("message", storeMessage);
```

@@ -113,3 +110,3 @@

```javascript
sarus.send('Hello world');
sarus.send("Hello world");
```

@@ -143,3 +140,3 @@

const sarus = new Sarus({
url: 'wss://ws.anephenix.com',
url: "wss://ws.anephenix.com",
reconnectAutomatically: false

@@ -184,3 +181,3 @@ });

const sarus = new Sarus({
url: 'wss://ws.anephenix.com',
url: "wss://ws.anephenix.com",
retryConnectionDelay: true

@@ -195,3 +192,3 @@ });

const sarus = new Sarus({
url: 'wss://ws.anephenix.com',
url: "wss://ws.anephenix.com",
retryConnectionDelay: 500 // equivalent to 500ms or 1/2 second

@@ -225,3 +222,3 @@ });

// Log a message that the connection is open
const noteOpened = () => console.log('Connection opened');
const noteOpened = () => console.log("Connection opened");

@@ -236,3 +233,3 @@ // Assuming that the WebSocket server is sending JSON data,

// Log a message that the connection has closed
const noteClosed = () => console.log('Connection closed');
const noteClosed = () => console.log("Connection closed");

@@ -244,3 +241,3 @@ // If an error occurs, throw the error

const sarus = new Sarus({
url: 'wss://ws.anephenix.com',
url: "wss://ws.anephenix.com",
eventListeners: {

@@ -271,3 +268,3 @@ open: [noteOpened],

const store = window.localStorage;
let record = store.getItem('messages');
let record = store.getItem("messages");
if (!record) {

@@ -279,3 +276,3 @@ record = [];

record.push(event.data);
store.setItem('messages', JSON.stringify(record));
store.setItem("messages", JSON.stringify(record));
};

@@ -285,3 +282,3 @@

// the WebSocket server
sarus.on('message', storeMessage);
sarus.on("message", storeMessage);
```

@@ -294,6 +291,6 @@

// Pass the function variable
sarus.off('message', storeMessage);
sarus.off("message", storeMessage);
// You can also pass the name of the function as well
sarus.off('message', 'storeMessage');
sarus.off("message", "storeMessage");
```

@@ -312,3 +309,3 @@

```javascript
sarus.off('message', 'myNonExistentFunction', { doNotThrowError: true });
sarus.off("message", "myNonExistentFunction", { doNotThrowError: true });
```

@@ -334,4 +331,4 @@

const sarus = new Sarus({
url: 'wss://ws.anephenix.com',
storageType: 'session'
url: "wss://ws.anephenix.com",
storageType: "session"
});

@@ -350,4 +347,4 @@ ```

const sarus = new Sarus({
url: 'wss://ws.anephenix.com',
storageType: 'local'
url: "wss://ws.anephenix.com",
storageType: "local"
});

@@ -381,6 +378,6 @@ ```

const sarus = new Sarus({
url: 'wss.anephenix.com',
protocols: 'hybi-00',
url: "wss.anephenix.com",
protocols: "hybi-00",
retryProcessTimePeriod: 25,
storageKey: 'messageQueue'
storageKey: "messageQueue"
});

@@ -387,0 +384,0 @@ ```

{
"compilerOptions": {
"target": "es2020" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "ESNext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"target": "ES2015" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"allowJs": false /* Allow javascript files to be compiled. */,

@@ -6,0 +6,0 @@ "checkJs": false /* Report errors in .js files. */,

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