Socket
Socket
Sign inDemoInstall

rpcapi

Package Overview
Dependencies
157
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.2.0 to 1.3.0

2

index.d.ts

@@ -5,2 +5,2 @@ export { API } from './src/API';

export { WebSocketAccessMethod } from './src/accessMethods/WebSocketAccessMethod';
export { AccessDeniedError } from './src/errorTypes';
export { AccessDeniedError, ActionError } from './src/errorTypes';

@@ -16,2 +16,3 @@ "use strict";

exports.AccessDeniedError = errorTypes_1.AccessDeniedError;
exports.ActionError = errorTypes_1.ActionError;
//# sourceMappingURL=index.js.map
{
"name": "rpcapi",
"version": "1.2.0",
"version": "1.3.0",
"description": "Provides a struture for hosting RPC style APIs, supports both http and websocket access out of the box",

@@ -34,2 +34,3 @@ "repository": {

"dependencies": {
"@types/debug": "^0.0.30",
"@types/express": "^4.0.39",

@@ -39,2 +40,3 @@ "@types/node-fetch": "^1.6.7",

"coveralls": "^3.0.0",
"debug": "^3.1.0",
"express": "^4.16.2",

@@ -41,0 +43,0 @@ "socket.io": "^2.0.4"

@@ -41,2 +41,4 @@ "use strict";

var errorTypes_1 = require("../../errorTypes");
var debugFactory = require("debug");
var debug = debugFactory('rpcapi:WebAPIAccessMethod');
var defaultConfig = {

@@ -59,5 +61,7 @@ prefix: '/api'

.then(function (result) {
debug("Request: " + endpointName + "/" + actionName + " %o: %o", req.query, result);
res.end(_this.formatResult(null, result));
}).catch(function (e) {
if (e instanceof customErrors_1.NotFoundError) {
debug("Request: " + endpointName + "/" + actionName + " %o: NotFound - " + e.message, req.query);
res

@@ -69,2 +73,3 @@ .status(404)

if (e instanceof customErrors_1.InvalidTypeError) {
debug("Request: " + endpointName + "/" + actionName + " %o: InvalidType - " + e.message, req.query);
res

@@ -76,2 +81,3 @@ .status(400)

if (e instanceof errorTypes_1.AccessDeniedError) {
debug("Request: " + endpointName + "/" + actionName + " %o: AccessDenied - " + e.message, req.query);
res

@@ -82,2 +88,9 @@ .status(401)

}
if (e instanceof errorTypes_1.ActionError) {
debug("Request: " + endpointName + "/" + actionName + " %o: ActionError - " + e.message, req.query);
res
.status(500)
.end(_this.formatResult(e.message));
return;
}
res

@@ -89,2 +102,3 @@ .status(500)

}
debug("Request: " + endpointName + "/" + actionName + " %o: Unknown error - " + e.message, req.query);
});

@@ -91,0 +105,0 @@ });

@@ -273,3 +273,3 @@ "use strict";

}); });
ava_1.default('Calling access denied in connect causes processRequest to throw', function (t) { return __awaiter(_this, void 0, void 0, function () {
ava_1.default('Calling ActionError in endpoint passes out error message', function (t) { return __awaiter(_this, void 0, void 0, function () {
var TestEndpoint, testApi, accessMethod, e_1;

@@ -284,7 +284,4 @@ return __generator(this, function (_a) {

}
TestEndpoint.prototype.connect = function () {
throw new errorTypes_1.AccessDeniedError('Test access denied');
};
TestEndpoint.prototype.$testFunc = function () {
return { done: true };
throw new errorTypes_1.ActionError('Hello! I am an error');
};

@@ -306,3 +303,3 @@ return TestEndpoint;

e_1 = _a.sent();
t.is(e_1.message, 'Test access denied');
t.is(e_1.message, 'Hello! I am an error');
t.pass();

@@ -309,0 +306,0 @@ return [3 /*break*/, 4];

@@ -53,2 +53,4 @@ "use strict";

var errorTypes_1 = require("../../errorTypes");
var debugFactory = require("debug");
var debug = debugFactory('rpcapi:WebSocketAccessMethod');
var WebSocketAccessMethod = /** @class */ (function (_super) {

@@ -67,2 +69,3 @@ __extends(WebSocketAccessMethod, _super);

}
debug('bind()');
this.io = io;

@@ -73,5 +76,8 @@ this.io.on('connection', function (socket) { return _this.handleNewConnection(socket); });

var _this = this;
debug("New connection - id: '" + socket.id + "', waiting on connectToEndpoint event...");
socket.on('connectToEndpoint', function (endpointName, accessKey, cb) {
debug("socket id: '" + socket.id + "', connecting to endpoint '" + endpointName + "'");
_this.createNewSocketEndpoint(socket.id, endpointName, accessKey).then(function (endpointConnection) {
if (endpointConnection === null) {
debug("socket id: '" + socket.id + "', connecting to endpoint '" + endpointName + "' - failed, endpoint does not exist");
cb("Unable to create endpoint connection, '" + endpointName + "' does not exist", null);

@@ -84,9 +90,11 @@ return;

});
debug("socket id: '" + socket.id + "', connecting to endpoint '" + endpointName + "' - connectionId: '" + endpointConnection.endpointConnectionId + "'");
cb(null, endpointConnection.endpointConnectionId);
}).catch(function (e) {
if (e instanceof errorTypes_1.AccessDeniedError) {
debug("socket id: '" + socket.id + "', connecting to endpoint '" + endpointName + "' - failed, access denied");
cb("Access denied: " + e.message, null);
return;
}
console.error(e);
debug("socket id: '" + socket.id + "', connecting to endpoint '" + endpointName + "' - failed, %o", e);
cb("Unable to create endpoint connection, '" + endpointName + "' threw an error while setting up", null);

@@ -97,28 +105,44 @@ });

_this.callEndpointAction(socket.id, endpointConnectionId, actionName, params)
.then(function (retVal) { return cb(null, retVal); })
.then(function (retVal) {
debug("epcid: '" + endpointConnectionId + "' called " + actionName + " %o: %o", params, retVal);
cb(null, retVal);
})
.catch(function (e) {
if (e instanceof customErrors_1.NotFoundError) {
debug("epcid: '" + endpointConnectionId + "' called " + actionName + "(%o): failed: NotFound - " + e.message);
cb(e.message, null);
return;
}
else if (e instanceof customErrors_1.InvalidTypeError) {
if (e instanceof customErrors_1.InvalidTypeError) {
debug("epcid: '" + endpointConnectionId + "' called " + actionName + "(%o): failed: InvalidType - " + e.message);
cb(e.message, null);
return;
}
else if (e instanceof errorTypes_1.AccessDeniedError) {
if (e instanceof errorTypes_1.AccessDeniedError) {
debug("epcid: '" + endpointConnectionId + "' called " + actionName + "(%o): failed: AccessDenied - " + e.message);
cb("Access denied: " + e.message, null);
return;
}
else {
if (_this.outputActionErrors) {
console.error(e);
}
cb('Internal server error', null);
if (e instanceof errorTypes_1.ActionError) {
debug("epcid: '" + endpointConnectionId + "' called " + actionName + "(%o): failed: ActionError - " + e.message);
cb(e.message, null);
return;
}
if (_this.outputActionErrors) {
console.error(e);
}
debug("epcid: '" + endpointConnectionId + "' called " + actionName + "(%o): failed: Unknown error - " + e.message);
cb('Internal server error', null);
});
});
socket.on('disconnect', function () {
debug("Socket '" + socket.id + "' disconnected, disconnecting all endpoints");
_this.disconnectAllSocketEndpoints(socket.id);
});
socket.on('disconnectEndpointConnection', function (endpointConnectionId) {
debug("disconnectEndpointConnection('" + socket.id + "', '" + endpointConnectionId + "')");
_this.disconnectEndpointConnection(socket.id, endpointConnectionId);
});
//Tell the client we are ready to receive messages
debug(socket.id + " - Server ready");
socket.emit('serverReady');

@@ -125,0 +149,0 @@ };

@@ -721,2 +721,40 @@ "use strict";

}); });
ava_1.default('Throwing AccessDeniedError returns error when calling action', function (t) { return __awaiter(_this, void 0, void 0, function () {
var TestEndpoint, testApi, accessMethod, ep, e_8;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
TestEndpoint = /** @class */ (function (_super) {
__extends(TestEndpoint, _super);
function TestEndpoint() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestEndpoint.prototype.$test = function () {
throw new errorTypes_1.ActionError('Hello test error');
};
return TestEndpoint;
}(APIEndpoint_1.APIEndpoint));
testApi = new API_1.API();
testApi.registerEndpoint('test', TestEndpoint);
accessMethod = new index_1.WebSocketAccessMethod(testApi);
return [4 /*yield*/, accessMethod.createNewSocketEndpoint('mySocketID', 'test')];
case 1:
ep = _a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, accessMethod.callEndpointAction('mySocketID', ep.endpointConnectionId, 'test')];
case 3:
_a.sent();
t.fail();
return [3 /*break*/, 5];
case 4:
e_8 = _a.sent();
t.is(e_8.message, 'Hello test error');
t.pass();
return [3 /*break*/, 5];
case 5: return [2 /*return*/];
}
});
}); });
//# sourceMappingURL=WebSocketAccessMethod.test.js.map
export declare class AccessDeniedError extends Error {
constructor(m?: string);
}
export declare class ActionError extends Error {
constructor(m: string);
}

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

exports.AccessDeniedError = AccessDeniedError;
var ActionError = /** @class */ (function (_super) {
__extends(ActionError, _super);
function ActionError(m) {
var _this = _super.call(this, m) || this;
// Set the prototype explicitly.
Object.setPrototypeOf(_this, ActionError.prototype);
return _this;
}
return ActionError;
}(Error));
exports.ActionError = ActionError;
//# sourceMappingURL=errorTypes.js.map

@@ -49,2 +49,3 @@ "use strict";

var APIEndpoint_1 = require("../APIEndpoint");
var errorTypes_1 = require("../errorTypes");
var CalculatorEndpoint = /** @class */ (function (_super) {

@@ -72,2 +73,5 @@ __extends(CalculatorEndpoint, _super);

var value = a + b;
if (value > 10000) {
throw new errorTypes_1.ActionError('Add cannot perform operations greater than 10000');
}
if (this.sendAdds) {

@@ -74,0 +78,0 @@ this.emit('addCalculationPerformed', value);

@@ -59,4 +59,4 @@ "use strict";

}); });
ava_1.default('Adds larger numbers', function (t) { return __awaiter(_this, void 0, void 0, function () {
var calc, value;
ava_1.default('Throws adding larger numbers (ActionError)', function (t) { return __awaiter(_this, void 0, void 0, function () {
var calc, e_1;
return __generator(this, function (_a) {

@@ -69,7 +69,16 @@ switch (_a.label) {

_a.sent();
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, calc.callAction('add', { a: 1000, b: 87652 })];
case 2:
value = _a.sent();
t.deepEqual(value, { value: 88652 });
return [2 /*return*/];
case 3:
_a.sent();
t.fail();
return [3 /*break*/, 5];
case 4:
e_1 = _a.sent();
t.is(e_1.message, 'Add cannot perform operations greater than 10000');
t.pass();
return [3 /*break*/, 5];
case 5: return [2 /*return*/];
}

@@ -232,3 +241,3 @@ });

ava_1.default('Fails to watch adds if the connection doenst allow emitting', function (t) { return __awaiter(_this, void 0, void 0, function () {
var calc, e_1;
var calc, e_2;
return __generator(this, function (_a) {

@@ -250,4 +259,4 @@ switch (_a.label) {

case 4:
e_1 = _a.sent();
t.is(e_1.message, 'Cannot watch adds from and endpoint that cannot receive events');
e_2 = _a.sent();
t.is(e_2.message, 'Cannot watch adds from and endpoint that cannot receive events');
t.pass();

@@ -254,0 +263,0 @@ return [3 /*break*/, 5];

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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc