Socket
Socket
Sign inDemoInstall

rpc-websockets

Package Overview
Dependencies
Maintainers
1
Versions
127
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rpc-websockets - npm Package Compare versions

Comparing version 7.6.2 to 7.7.0

2

API.md

@@ -77,2 +77,3 @@ # API Documentation

* `generate_request_id` {Function} Custom function to generate request id instead of simple increment by default. Passes `method` and `params` to parameters.
* `dataPack` {DataPack} data pack contains encoder and decoder.

@@ -184,2 +185,3 @@ ### ws.connect()

* `host` {String}: Address on which the server will listen for incoming requests.
* `dataPack` {DataPack} data pack contains encoder and decoder.

@@ -186,0 +188,0 @@ Once the Server class is instantiated, you can use a `ws` library's instance via server.wss object.

5

build-ts/lib/client.d.ts

@@ -8,2 +8,3 @@ /**

import { NodeWebSocketType, ICommonWebSocketFactory } from "./client/client.types";
import { DataPack } from "./utils";
interface IQueueElement {

@@ -39,2 +40,3 @@ promise: [

private webSocketFactory;
private dataPack;
/**

@@ -47,2 +49,3 @@ * Instantiate a Client class.

* @param {Function} generate_request_id - custom generation request Id
* @param {DataPack} dataPack - data pack contains encoder and decoder
* @return {CommonClient}

@@ -55,3 +58,3 @@ */

max_reconnects?: number;
}, generate_request_id?: (method: string, params: object | Array<any>) => number);
}, generate_request_id?: (method: string, params: object | Array<any>) => number, dataPack?: DataPack<object, string>);
/**

@@ -58,0 +61,0 @@ * Connects to a defined server if not connected already.

@@ -20,2 +20,3 @@ /**

import { EventEmitter } from "eventemitter3";
import { DefaultDataPack } from "./utils";
export default class CommonClient extends EventEmitter {

@@ -29,5 +30,6 @@ /**

* @param {Function} generate_request_id - custom generation request Id
* @param {DataPack} dataPack - data pack contains encoder and decoder
* @return {CommonClient}
*/
constructor(webSocketFactory, address = "ws://localhost:8080", _a = {}, generate_request_id) {
constructor(webSocketFactory, address = "ws://localhost:8080", _a = {}, generate_request_id, dataPack) {
var { autoconnect = true, reconnect = true, reconnect_interval = 1000, max_reconnects = 5 } = _a, rest_options = __rest(_a, ["autoconnect", "reconnect", "reconnect_interval", "max_reconnects"]);

@@ -48,2 +50,6 @@ super();

this.generate_request_id = generate_request_id || (() => ++this.rpc_id);
if (!dataPack)
this.dataPack = new DefaultDataPack();
else
this.dataPack = dataPack;
if (this.autoconnect)

@@ -86,3 +92,3 @@ this._connect(this.address, Object.assign({ autoconnect: this.autoconnect, reconnect: this.reconnect, reconnect_interval: this.reconnect_interval, max_reconnects: this.max_reconnects }, this.rest_options));

};
this.socket.send(JSON.stringify(message), ws_opts, (error) => {
this.socket.send(this.dataPack.encode(message), ws_opts, (error) => {
if (error)

@@ -136,3 +142,3 @@ return reject(error);

};
this.socket.send(JSON.stringify(message), (error) => {
this.socket.send(this.dataPack.encode(message), (error) => {
if (error)

@@ -204,3 +210,3 @@ return reject(error);

try {
message = JSON.parse(message);
message = this.dataPack.decode(message);
}

@@ -207,0 +213,0 @@ catch (error) {

@@ -7,2 +7,3 @@ /**

import NodeWebSocket, { Server as WebSocketServer } from "ws";
import * as utils from "./utils";
interface INamespaceEvent {

@@ -37,2 +38,3 @@ [x: string]: {

private namespaces;
private dataPack;
wss: InstanceType<typeof WebSocketServer>;

@@ -43,5 +45,6 @@ /**

* @param {Object} options - ws constructor's parameters with rpc
* @param {DataPack} dataPack - data pack contains encoder and decoder
* @return {Server} - returns a new Server instance
*/
constructor(options: NodeWebSocket.ServerOptions);
constructor(options: NodeWebSocket.ServerOptions, dataPack?: utils.DataPack<object, string>);
/**

@@ -48,0 +51,0 @@ * Registers an RPC method.

@@ -17,5 +17,6 @@ /**

* @param {Object} options - ws constructor's parameters with rpc
* @param {DataPack} dataPack - data pack contains encoder and decoder
* @return {Server} - returns a new Server instance
*/
constructor(options) {
constructor(options, dataPack) {
super();

@@ -34,2 +35,6 @@ /**

this.namespaces = {};
if (!dataPack)
this.dataPack = new utils.DefaultDataPack();
else
this.dataPack = dataPack;
this.wss = new WebSocketServer(options);

@@ -185,3 +190,3 @@ this.wss.on("listening", () => this.emit("listening"));

continue;
socket.send(JSON.stringify({
socket.send(this.dataPack.encode({
notification: name,

@@ -242,3 +247,3 @@ params: params || null

for (let i = 0, id; id = socket_ids[i]; ++i) {
self.namespaces[name].clients.get(id).send(JSON.stringify({
self.namespaces[name].clients.get(id).send(self.dataPack.encode({
notification: event,

@@ -342,6 +347,6 @@ params: params || []

try {
parsedData = JSON.parse(data);
parsedData = this.dataPack.decode(data);
}
catch (error) {
return socket.send(JSON.stringify({
return socket.send(this.dataPack.encode({
jsonrpc: "2.0",

@@ -354,3 +359,3 @@ error: utils.createError(-32700, error.toString()),

if (!parsedData.length)
return socket.send(JSON.stringify({
return socket.send(this.dataPack.encode({
jsonrpc: "2.0",

@@ -369,3 +374,3 @@ error: utils.createError(-32600, "Invalid array"),

return;
return socket.send(JSON.stringify(responses), msg_options);
return socket.send(this.dataPack.encode(responses), msg_options);
}

@@ -375,3 +380,3 @@ const response = await this._runMethod(parsedData, socket._id, ns);

return;
return socket.send(JSON.stringify(response), msg_options);
return socket.send(this.dataPack.encode(response), msg_options);
});

@@ -378,0 +383,0 @@ }

@@ -6,2 +6,10 @@ interface IRPCError {

}
export interface DataPack<T, R extends string | ArrayBufferLike | Blob | ArrayBufferView> {
encode(value: T): R;
decode(value: R): T;
}
export declare class DefaultDataPack implements DataPack<Object, string> {
encode(value: Object): string;
decode(value: string): Object;
}
/**

@@ -8,0 +16,0 @@ * Creates a JSON-RPC 2.0-compliant error.

@@ -13,2 +13,10 @@ "use strict";

]);
export class DefaultDataPack {
encode(value) {
return JSON.stringify(value);
}
decode(value) {
return JSON.parse(value);
}
}
/**

@@ -15,0 +23,0 @@ * Creates a JSON-RPC 2.0-compliant error.

@@ -8,2 +8,3 @@ /**

import { NodeWebSocketType, ICommonWebSocketFactory } from "./client/client.types";
import { DataPack } from "./utils";
interface IQueueElement {

@@ -39,2 +40,3 @@ promise: [

private webSocketFactory;
private dataPack;
/**

@@ -47,2 +49,3 @@ * Instantiate a Client class.

* @param {Function} generate_request_id - custom generation request Id
* @param {DataPack} dataPack - data pack contains encoder and decoder
* @return {CommonClient}

@@ -55,3 +58,3 @@ */

max_reconnects?: number;
}, generate_request_id?: (method: string, params: object | Array<any>) => number);
}, generate_request_id?: (method: string, params: object | Array<any>) => number, dataPack?: DataPack<object, string>);
/**

@@ -58,0 +61,0 @@ * Connects to a defined server if not connected already.

@@ -22,2 +22,3 @@ /**

var _eventemitter = require("eventemitter3");
var _utils = require("./utils");
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

@@ -46,2 +47,3 @@ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

* @param {Function} generate_request_id - custom generation request Id
* @param {DataPack} dataPack - data pack contains encoder and decoder
* @return {CommonClient}

@@ -54,2 +56,3 @@ */

var generate_request_id = arguments.length > 3 ? arguments[3] : undefined;
var dataPack = arguments.length > 4 ? arguments[4] : undefined;
(0, _classCallCheck2["default"])(this, CommonClient);

@@ -81,2 +84,3 @@ var _a$autoconnect = _a.autoconnect,

};
if (!dataPack) _this.dataPack = new _utils.DefaultDataPack();else _this.dataPack = dataPack;
if (_this.autoconnect) _this._connect(_this.address, Object.assign({

@@ -132,3 +136,3 @@ autoconnect: _this.autoconnect,

};
_this2.socket.send(JSON.stringify(message), ws_opts, function (error) {
_this2.socket.send(_this2.dataPack.encode(message), ws_opts, function (error) {
if (error) return reject(error);

@@ -232,3 +236,3 @@ _this2.queue[rpc_id] = {

};
_this3.socket.send(JSON.stringify(message), function (error) {
_this3.socket.send(_this3.dataPack.encode(message), function (error) {
if (error) return reject(error);

@@ -354,3 +358,3 @@ resolve();

try {
message = JSON.parse(message);
message = _this4.dataPack.decode(message);
} catch (error) {

@@ -357,0 +361,0 @@ return;

@@ -7,2 +7,3 @@ /**

import NodeWebSocket, { Server as WebSocketServer } from "ws";
import * as utils from "./utils";
interface INamespaceEvent {

@@ -37,2 +38,3 @@ [x: string]: {

private namespaces;
private dataPack;
wss: InstanceType<typeof WebSocketServer>;

@@ -43,5 +45,6 @@ /**

* @param {Object} options - ws constructor's parameters with rpc
* @param {DataPack} dataPack - data pack contains encoder and decoder
* @return {Server} - returns a new Server instance
*/
constructor(options: NodeWebSocket.ServerOptions);
constructor(options: NodeWebSocket.ServerOptions, dataPack?: utils.DataPack<object, string>);
/**

@@ -48,0 +51,0 @@ * Registers an RPC method.

@@ -43,5 +43,6 @@ /**

* @param {Object} options - ws constructor's parameters with rpc
* @param {DataPack} dataPack - data pack contains encoder and decoder
* @return {Server} - returns a new Server instance
*/
function Server(options) {
function Server(options, dataPack) {
var _this;

@@ -62,2 +63,3 @@ (0, _classCallCheck2["default"])(this, Server);

_this.namespaces = {};
if (!dataPack) _this.dataPack = new utils.DefaultDataPack();else _this.dataPack = dataPack;
_this.wss = new _ws.Server(options);

@@ -256,3 +258,3 @@ _this.wss.on("listening", function () {

if (!socket) continue;
socket.send(JSON.stringify({
socket.send(_this3.dataPack.encode({
notification: name,

@@ -321,3 +323,3 @@ params: params || null

for (var i = 0, id; id = socket_ids[i]; ++i) {
self.namespaces[name].clients.get(id).send(JSON.stringify({
self.namespaces[name].clients.get(id).send(self.dataPack.encode({
notification: event,

@@ -442,3 +444,3 @@ params: params || []

_context.prev = 4;
parsedData = JSON.parse(data);
parsedData = _this5.dataPack.decode(data);
_context.next = 11;

@@ -449,3 +451,3 @@ break;

_context.t0 = _context["catch"](4);
return _context.abrupt("return", socket.send(JSON.stringify({
return _context.abrupt("return", socket.send(_this5.dataPack.encode({
jsonrpc: "2.0",

@@ -464,3 +466,3 @@ error: utils.createError(-32700, _context.t0.toString()),

}
return _context.abrupt("return", socket.send(JSON.stringify({
return _context.abrupt("return", socket.send(_this5.dataPack.encode({
jsonrpc: "2.0",

@@ -513,3 +515,3 @@ error: utils.createError(-32600, "Invalid array"),

case 38:
return _context.abrupt("return", socket.send(JSON.stringify(responses), msg_options));
return _context.abrupt("return", socket.send(_this5.dataPack.encode(responses), msg_options));
case 39:

@@ -526,3 +528,3 @@ _context.next = 41;

case 44:
return _context.abrupt("return", socket.send(JSON.stringify(response), msg_options));
return _context.abrupt("return", socket.send(_this5.dataPack.encode(response), msg_options));
case 45:

@@ -529,0 +531,0 @@ case "end":

@@ -6,2 +6,10 @@ interface IRPCError {

}
export interface DataPack<T, R extends string | ArrayBufferLike | Blob | ArrayBufferView> {
encode(value: T): R;
decode(value: R): T;
}
export declare class DefaultDataPack implements DataPack<Object, string> {
encode(value: Object): string;
decode(value: string): Object;
}
/**

@@ -8,0 +16,0 @@ * Creates a JSON-RPC 2.0-compliant error.

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DefaultDataPack = void 0;
exports.createError = createError;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var errors = new Map([[-32000, "Event not provided"], [-32600, "Invalid Request"], [-32601, "Method not found"], [-32602, "Invalid params"], [-32603, "Internal error"], [-32604, "Params not found"], [-32605, "Method forbidden"], [-32606, "Event forbidden"], [-32700, "Parse error"]]);
var DefaultDataPack = /*#__PURE__*/function () {
function DefaultDataPack() {
(0, _classCallCheck2["default"])(this, DefaultDataPack);
}
(0, _createClass2["default"])(DefaultDataPack, [{
key: "encode",
value: function encode(value) {
return JSON.stringify(value);
}
}, {
key: "decode",
value: function decode(value) {
return JSON.parse(value);
}
}]);
return DefaultDataPack;
}();
/**

@@ -14,2 +35,3 @@ * Creates a JSON-RPC 2.0-compliant error.

*/
exports.DefaultDataPack = DefaultDataPack;
function createError(code, details) {

@@ -16,0 +38,0 @@ var error = {

{
"name": "rpc-websockets",
"version": "7.6.2",
"version": "7.7.0",
"description": "JSON-RPC 2.0 implementation over WebSockets for Node.js",

@@ -5,0 +5,0 @@ "main": "./dist/index.js",

Sorry, the diff of this file is too big to display

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