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

node-multi-socketservers

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-multi-socketservers - npm Package Compare versions

Comparing version 0.2.7 to 0.2.8

83

lib/main.js

@@ -16,6 +16,17 @@ "use strict";

this._tmpOnConnection = [];
this._sockets = [];
this._sockets = {};
}
addServer (options) {
return Promise.resolve().then(() => {
return super.addServer(options);
}).then(() => {
this._sockets[options.name] = [];
return Promise.resolve();
});
}
listen (requestListener) {

@@ -112,8 +123,8 @@

for (let i = 0; i < this._sockets.length; ++i) {
for (let i = 0; i < this._sockets[server.options.name].length; ++i) {
if (socket.id === this._sockets[i].id && server.options.port === this._sockets[i].server) {
if (socket.id === this._sockets[server.options.name][i].id) {
try {
this._sockets[i].close();
socket.removeAllListeners(Object.keys(socket._events));
}

@@ -124,3 +135,3 @@ catch (e) {

this._sockets.splice(i, 1);
this._sockets[server.options.name].splice(i, 1);

@@ -135,3 +146,3 @@ break;

this._sockets.push({
this._sockets[server.options.name].push({
"server": server.options.port,

@@ -141,6 +152,2 @@ "id": socket.id,

socket.removeAllListeners(eventNames);
},
"close": () => {
socket.removeAllListeners();
socket.disconnect(true);
}

@@ -290,6 +297,10 @@ });

for (let i = 0; i < this._sockets.length; ++i) {
this._sockets[i].removeAllListeners(eventName);
}
Object.keys(this._sockets).forEach((serverName) => {
for (let i = 0; i < this._sockets[serverName].length; ++i) {
this._sockets[serverName][i].removeAllListeners(eventName);
}
});
});

@@ -322,34 +333,32 @@

return Promise.resolve().then(() => {
return super.close();
}).then(() => {
return 0 >= this.servers.length ?
Promise.resolve() :
Promise.resolve().then(() => {
this.servers.forEach((server) => {
this.servers.forEach((server) => {
try {
if (server.sockets) {
server.sockets.close();
if (server.sockets) {
if (server.sockets.sockets) {
server.sockets.sockets.removeAllListeners(Object.keys(server.sockets.sockets._events));
}
});
if (server.sockets.eio) {
server.sockets.eio.removeAllListeners(Object.keys(server.sockets.eio._events));
}
for (let i = 0; i < this._sockets.length; ++i) {
server.sockets.close();
try {
this._sockets[i].close();
}
catch (e) {
// nothing to do here
}
}
this._sockets = [];
}
catch (e) {
// nothing to do here
}
return Promise.resolve();
});
});
return Promise.resolve();
}).then(() => {
return super.close();
});

@@ -363,5 +372,11 @@

this._tmpOnConnection = [];
return super.release();
}).then(() => {
this._tmpOnConnection = [];
this._sockets = {};
return Promise.resolve();
});

@@ -368,0 +383,0 @@

{
"name": "node-multi-socketservers",
"version": "0.2.7",
"version": "0.2.8",
"description": "A multi http & socket servers manager, based on node-multi-webserver",

@@ -34,3 +34,3 @@ "main": "lib/main.js",

"dependencies": {
"node-multi-webserver": "^1.2.5",
"node-multi-webserver": "^1.2.6",
"socket.io": "^2.0.4"

@@ -37,0 +37,0 @@ },

/*
eslint max-nested-callbacks: [ "error", 7 ]
*/
/*
eslint no-implicit-globals: 0
*/

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

// private
// methods
/**
* Create socketio client
* @param {int} port server port
* @returns {socket.io-client} The socketio client
*/
function _createClient (port) {
return ioClient("http://localhost:" + port, {
"reconnection": false
});
}
// tests

@@ -21,3 +41,3 @@

const servers = new MultiServers();
const multiServers = new MultiServers();

@@ -30,3 +50,3 @@ describe("connection", () => {

servers.connection().then(() => {
multiServers.connection().then(() => {
reject(new Error("run with missing eventListener"));

@@ -43,3 +63,3 @@ }).catch(resolve);

servers.connection("test").then(() => {
multiServers.connection("test").then(() => {
reject(new Error("run with wrong eventListener"));

@@ -60,3 +80,3 @@ }).catch(resolve);

servers.listen().then(() => {
multiServers.listen().then(() => {
reject(new Error("run with missing requestListener"));

@@ -73,3 +93,3 @@ }).catch(resolve);

servers.listen(false).then(() => {
multiServers.listen(false).then(() => {
reject(new Error("run with wrong requestListener"));

@@ -90,3 +110,3 @@ }).catch(resolve);

servers.emit().then(() => {
multiServers.emit().then(() => {
reject(new Error("run with missing eventName"));

@@ -103,3 +123,3 @@ }).catch(resolve);

servers.emit(false).then(() => {
multiServers.emit(false).then(() => {
reject(new Error("run with wrong eventName"));

@@ -116,3 +136,3 @@ }).catch(resolve);

servers.emit("").then(() => {
multiServers.emit("").then(() => {
reject(new Error("run with empty eventName"));

@@ -133,3 +153,3 @@ }).catch(resolve);

servers.broadcast().then(() => {
multiServers.broadcast().then(() => {
reject(new Error("run with missing socket"));

@@ -146,3 +166,3 @@ }).catch(resolve);

servers.broadcast(false).then(() => {
multiServers.broadcast(false).then(() => {
reject(new Error("run with wrong socket"));

@@ -159,3 +179,3 @@ }).catch(resolve);

servers.broadcast({}).then(() => {
multiServers.broadcast({}).then(() => {
reject(new Error("run with missing server"));

@@ -172,3 +192,3 @@ }).catch(resolve);

servers.broadcast({}, false).then(() => {
multiServers.broadcast({}, false).then(() => {
reject(new Error("run with wrong server"));

@@ -185,3 +205,3 @@ }).catch(resolve);

servers.broadcast({}, {}).then(() => {
multiServers.broadcast({}, {}).then(() => {
reject(new Error("run with missing eventName"));

@@ -198,3 +218,3 @@ }).catch(resolve);

servers.broadcast({}, {}, false).then(() => {
multiServers.broadcast({}, {}, false).then(() => {
reject(new Error("run with wrong eventName"));

@@ -211,3 +231,3 @@ }).catch(resolve);

servers.broadcast({}, {}, "").then(() => {
multiServers.broadcast({}, {}, "").then(() => {
reject(new Error("run with empty eventName"));

@@ -228,3 +248,3 @@ }).catch(resolve);

servers.removeAllListeners().then(() => {
multiServers.removeAllListeners().then(() => {
reject(new Error("run with missing eventName"));

@@ -241,3 +261,3 @@ }).catch(resolve);

servers.removeAllListeners(false).then(() => {
multiServers.removeAllListeners(false).then(() => {
reject(new Error("run with wrong eventName"));

@@ -256,6 +276,6 @@ }).catch(resolve);

const servers = new MultiServers();
const multiServers = new MultiServers();
it("should release servers", () => {
return servers.release();
return multiServers.release();
});

@@ -267,6 +287,6 @@

const servers = new MultiServers();
const multiServers = new MultiServers();
after(() => {
return servers.release();
return multiServers.release();
});

@@ -276,3 +296,3 @@

return servers.addServer({
return multiServers.addServer({
"port": 1337,

@@ -282,3 +302,3 @@ "name": "basic http server"

return servers.addServer({
return multiServers.addServer({
"port": 1338,

@@ -290,3 +310,3 @@ "name": "basic http server 2"

return servers.listen(() => {
return multiServers.listen(() => {
// nothing to do here

@@ -297,12 +317,19 @@ });

assert.strictEqual(2, servers.servers.length, "servers number is incorrect");
// test already listening
return multiServers.listen(() => {
// nothing to do here
});
assert.strictEqual(1337, servers.servers[0].options.port, "first server name is incorrect");
assert.strictEqual("basic http server", servers.servers[0].options.name, "first server name is incorrect");
assert.strictEqual(false, servers.servers[0].options.ssl, "first server ssl is incorrect");
}).then(() => {
assert.strictEqual(1338, servers.servers[1].options.port, "last server name is incorrect");
assert.strictEqual("basic http server 2", servers.servers[1].options.name, "last server name is incorrect");
assert.strictEqual(false, servers.servers[1].options.ssl, "last server ssl is incorrect");
assert.strictEqual(2, multiServers.servers.length, "servers number is incorrect");
assert.strictEqual(1337, multiServers.servers[0].options.port, "first server name is incorrect");
assert.strictEqual("basic http server", multiServers.servers[0].options.name, "first server name is incorrect");
assert.strictEqual(false, multiServers.servers[0].options.ssl, "first server ssl is incorrect");
assert.strictEqual(1338, multiServers.servers[1].options.port, "last server name is incorrect");
assert.strictEqual("basic http server 2", multiServers.servers[1].options.name, "last server name is incorrect");
assert.strictEqual(false, multiServers.servers[1].options.ssl, "last server ssl is incorrect");
return Promise.resolve();

@@ -318,13 +345,13 @@

describe("reverted", () => {
const multiServers = new MultiServers();
const servers = new MultiServers();
afterEach(() => {
return multiServers.release();
});
after(() => {
return servers.release();
});
describe("server side", () => {
it("should connect & disconnect on created servers", (done) => {
it("should connect & disconnect on created servers", () => {
servers.addServer({
return multiServers.addServer({
"port": 1337,

@@ -334,3 +361,3 @@ "name": "basic http server"

return servers.addServer({
return multiServers.addServer({
"port": 1338,

@@ -342,16 +369,16 @@ "name": "basic http server 2"

const socket = ioClient("http://localhost:1337");
return multiServers.listen(() => {
// nothing to do here
});
socket.on("disconnect", () => {
done();
}).on("connect", () => {
}).then(() => {
return multiServers.connection((socket) => {
socket.disconnect();
});
return Promise.resolve();
}).then(() => {
return servers.listen(() => {
// nothing to do here
return new Promise((resolve) => {
_createClient(1337).on("disconnect", resolve);
});

@@ -361,17 +388,11 @@

}).timeout(1000);
});
});
describe("classical", () => {
describe("client side", () => {
const servers = new MultiServers();
after(() => {
return servers.release();
});
it("should connect & disconnect on created servers", (done) => {
servers.addServer({
multiServers.addServer({
"port": 1337,

@@ -381,3 +402,3 @@ "name": "basic http server"

return servers.addServer({
return multiServers.addServer({
"port": 1338,

@@ -389,3 +410,3 @@ "name": "basic http server 2"

return servers.listen(() => {
return multiServers.listen(() => {
// nothing to do here

@@ -396,16 +417,22 @@ });

const socket = ioClient("http://localhost:1337");
return multiServers.connection((socket) => {
socket.on("disconnect", () => {
done();
}).on("connect", () => {
socket.on("disconnect", () => {
done();
});
});
}).then(() => {
const socket = _createClient(1337);
socket.on("connect", () => {
socket.disconnect();
});
return Promise.resolve();
}).catch(done);
});
});
}).timeout(1000);
});

@@ -419,11 +446,19 @@

const servers = new MultiServers();
const multiServers = new MultiServers();
after(() => {
return servers.release();
return multiServers.release();
});
it("should remove all inexistant listener", () => {
return multiServers.removeAllListeners("test");
});
it("should remove all inexistant listeners", () => {
return multiServers.removeAllListeners([ "test" ]);
});
it("should remove all listeners from these servers", () => {
return servers.addServer({
return multiServers.addServer({
"port": 1337,

@@ -433,3 +468,3 @@ "name": "basic http server"

return servers.addServer({
return multiServers.addServer({
"port": 1338,

@@ -441,3 +476,3 @@ "name": "basic http server 2"

return servers.listen(() => {
return multiServers.listen(() => {
// nothing to do here

@@ -448,3 +483,3 @@ });

return servers.connection(() => {
return multiServers.connection(() => {
// nothing to do here

@@ -455,20 +490,30 @@ });

assert.strictEqual(2, servers.servers.length, "servers number is incorrect");
assert.strictEqual("object", typeof servers.servers[0].sockets, "server sockets object is incorrect");
assert.strictEqual("object", typeof servers.servers[0].sockets.sockets, "sockets manager object is incorrect");
assert.strictEqual("object", typeof servers.servers[0].sockets.sockets._events, "event manager object is incorrect");
assert.strictEqual("function", typeof servers.servers[0].sockets.sockets._events.connection, "connection event is incorrect");
assert.strictEqual(2, multiServers.servers.length, "servers number is incorrect");
assert.strictEqual("object", typeof multiServers.servers[0].sockets, "server sockets object is incorrect");
assert.strictEqual("object", typeof multiServers.servers[0].sockets.sockets, "sockets manager object is incorrect");
assert.strictEqual("object", typeof multiServers.servers[0].sockets.sockets._events, "event manager object is incorrect");
assert.strictEqual(
"function",
typeof multiServers.servers[0].sockets.sockets._events.connection,
"connection event is incorrect"
);
return Promise.resolve();
}).then(() => {
return servers.removeAllListeners("connection");
return multiServers.removeAllListeners("connection");
}).then(() => {
assert.strictEqual(2, servers.servers.length, "servers number is incorrect");
assert.strictEqual("object", typeof servers.servers[0].sockets, "server sockets object is incorrect");
assert.strictEqual("object", typeof servers.servers[0].sockets.sockets, "sockets manager object is incorrect");
assert.strictEqual("object", typeof servers.servers[0].sockets.sockets._events, "event manager object is incorrect");
assert.strictEqual("undefined", typeof servers.servers[0].sockets.sockets._events.connection, "connection event is incorrect");
assert.strictEqual(2, multiServers.servers.length, "servers number is incorrect");
assert.strictEqual("object", typeof multiServers.servers[0].sockets, "server sockets object is incorrect");
assert.strictEqual("object", typeof multiServers.servers[0].sockets.sockets, "sockets manager object is incorrect");
assert.strictEqual("object", typeof multiServers.servers[0].sockets.sockets._events, "event manager object is incorrect");
assert.strictEqual(
"undefined",
typeof multiServers.servers[0].sockets.sockets._events.connection,
"connection event is incorrect"
);
return Promise.resolve();

@@ -484,6 +529,6 @@

const servers = new MultiServers();
const multiServers = new MultiServers();
after(() => {
return servers.release();
return multiServers.release();
});

@@ -493,3 +538,3 @@

servers.addServer({
multiServers.addServer({
"port": 1337,

@@ -499,3 +544,3 @@ "name": "basic http server"

return servers.addServer({
return multiServers.addServer({
"port": 1338,

@@ -507,3 +552,3 @@ "name": "basic http server 2"

return servers.listen(() => {
return multiServers.listen(() => {
// nothing to do here

@@ -514,3 +559,3 @@ });

return servers.connection((socket) => {
return multiServers.connection((socket) => {

@@ -527,3 +572,3 @@ socket.on("disconnect", () => {

servers.removeAllListeners("test2").then(() => {
multiServers.removeAllListeners("test2").then(() => {
socket.emit("answer");

@@ -538,19 +583,22 @@ });

const socketClient = ioClient("http://localhost:1337");
return new Promise((resolve) => {
socketClient.on("connect", () => {
socketClient.emit("query");
}).on("answer", () => {
const socketClient = _createClient(1337);
socketClient.emit("test1");
socketClient.emit("test2");
socketClient.emit("test3");
socketClient.on("connect", () => {
socketClient.emit("query");
resolve();
}).on("answer", () => {
socketClient.emit("test1");
socketClient.emit("test2");
socketClient.emit("test3");
});
});
return Promise.resolve();
});
});
}).timeout(1000);

@@ -563,6 +611,6 @@ });

const servers = new MultiServers();
const multiServers = new MultiServers();
after(() => {
return servers.release();
return multiServers.release();
});

@@ -572,3 +620,3 @@

servers.addServer({
multiServers.addServer({
"port": 1337,

@@ -578,3 +626,3 @@ "name": "basic http server"

return servers.addServer({
return multiServers.addServer({
"port": 1338,

@@ -586,3 +634,3 @@ "name": "basic http server 2"

return servers.listen(() => {
return multiServers.listen(() => {
// nothing to do here

@@ -593,3 +641,3 @@ });

return servers.connection((socket) => {
return multiServers.connection((socket) => {

@@ -600,4 +648,4 @@ socket.on("disconnect", () => {

servers.emit("answertest", "test").then(() => {
return servers.emit("answer");
multiServers.emit("answertest", "test").then(() => {
return multiServers.emit("answer");
});

@@ -611,10 +659,13 @@

const socketClient = ioClient("http://localhost:1337");
return new Promise((resolve) => {
socketClient.on("answer", socketClient.disconnect).on("connect", () => {
socketClient.emit("query");
const socketClient = _createClient(1337);
socketClient.on("answer", socketClient.disconnect).on("connect", () => {
socketClient.emit("query");
resolve();
});
});
return Promise.resolve();
});

@@ -630,6 +681,6 @@

const servers = new MultiServers();
const multiServers = new MultiServers();
after(() => {
return servers.release();
return multiServers.release();
});

@@ -639,3 +690,3 @@

servers.addServer({
multiServers.addServer({
"port": 1337,

@@ -645,3 +696,3 @@ "name": "basic http server"

return servers.addServer({
return multiServers.addServer({
"port": 1338,

@@ -653,3 +704,3 @@ "name": "basic http server 2"

return servers.listen(() => {
return multiServers.listen(() => {
// nothing to do here

@@ -660,3 +711,3 @@ });

return servers.connection((socket) => {
return multiServers.connection((socket) => {

@@ -671,10 +722,13 @@ socket.on("disconnect", () => {

const socketClient = ioClient("http://localhost:1337");
return new Promise((resolve) => {
socketClient.on("connect", () => {
socketClient.emit("query");
const socketClient = _createClient(1337);
socketClient.on("connect", () => {
socketClient.emit("query");
resolve();
});
});
return Promise.resolve();
});

@@ -688,6 +742,6 @@

const servers = new MultiServers();
const multiServers = new MultiServers();
after(() => {
return servers.release();
return multiServers.release();
});

@@ -697,3 +751,3 @@

servers.addServer({
multiServers.addServer({
"port": 1337,

@@ -703,3 +757,3 @@ "name": "basic http server"

return servers.addServer({
return multiServers.addServer({
"port": 1338,

@@ -711,3 +765,3 @@ "name": "basic http server 2"

return servers.connection((socket) => {
return multiServers.connection((socket) => {

@@ -722,3 +776,3 @@ socket.on("disconnect", () => {

return servers.listen(() => {
return multiServers.listen(() => {
// nothing to do here

@@ -729,10 +783,13 @@ });

const socketClient = ioClient("http://localhost:1337");
return new Promise((resolve) => {
socketClient.on("connect", () => {
socketClient.emit("query");
const socketClient = _createClient(1337);
socketClient.on("connect", () => {
socketClient.emit("query");
resolve();
});
});
return Promise.resolve();
});

@@ -748,6 +805,6 @@

const servers = new MultiServers();
const multiServers = new MultiServers();
after(() => {
return servers.release();
return multiServers.release();
});

@@ -757,3 +814,3 @@

servers.addServer({
multiServers.addServer({
"port": 1337,

@@ -763,3 +820,3 @@ "name": "basic http server"

return servers.addServer({
return multiServers.addServer({
"port": 1338,

@@ -771,3 +828,3 @@ "name": "basic http server 2"

return servers.listen(() => {
return multiServers.listen(() => {
// nothing to do here

@@ -778,8 +835,12 @@ });

return servers.connection((socket, server) => {
return multiServers.connection((socket, server) => {
if (1338 === server.options.port) {
servers.broadcast(socket, server, "answer").then(() => {
socket.disconnect();
multiServers.broadcast(socket, server, "testwithdata", false).then(() => {
return multiServers.broadcast(socket, server, "answer").then(() => {
socket.disconnect();
});
});

@@ -800,12 +861,51 @@

const socketClient = ioClient("http://localhost:1337");
return new Promise((resolve) => {
socketClient.on("connect", () => {
ioClient("http://localhost:1338");
}).on("answer", () => {
socketClient.disconnect();
const socketClient = _createClient(1337);
socketClient.on("connect", () => {
_createClient(1338);
resolve();
}).on("answer", () => {
socketClient.disconnect();
});
});
return Promise.resolve();
});
}).timeout(1000);
});
describe("close", () => {
const multiServers = new MultiServers();
afterEach(() => {
return multiServers.release();
});
it("should close server without connection", () => {
return multiServers.addServer({
"port": 1337,
"name": "basic http server"
}).then(() => {
return multiServers.listen(() => {
// nothing to do here
});
}).then(() => {
return multiServers.connection(() => {
// nothing to do here
});
}).then(() => {
return multiServers.release();
});

@@ -815,2 +915,31 @@

it("should close server with connection", () => {
return multiServers.addServer({
"port": 1337,
"name": "basic http server"
}).then(() => {
return multiServers.listen(() => {
// nothing to do here
});
}).then(() => {
return multiServers.connection(() => {
multiServers.release();
});
}).then(() => {
return new Promise((resolve) => {
_createClient(1337).on("disconnect", resolve);
});
});
}).timeout(2000);
});
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