update structure

This commit is contained in:
Philipp Kunz 2016-08-07 14:58:20 +02:00
parent 8b419711fc
commit 309284ab68
21 changed files with 286 additions and 9 deletions

6
.gitignore vendored
View File

@ -1,3 +1,3 @@
node_modules
coverage
docs/
node_modules/
coverage/
docs/

View File

@ -7,3 +7,40 @@ easy and secure websocket communication
## Usage
We recommend the use of typescript.
```typescript
// serverside
import * as smartsocket from "smartsocket"
let mySmartsocket = new smartsocket.Smartsocket({
port: 3000 // the port smartsocket will listen on
});
let mySocketRole = new smartsocket.SocketRole({
name: "someRoleName",
passwordHash: "someHasedString"
});
let mySocketFunction = new smartsocket.SocketFunction({
func:() => {}, the function to execute
roles:[mySocketRole] // all roles that have access to a specific function
});
mySmartsocket.registerRole(mySocketRole);
// Client side
let mySmartsocketClient = new smartsocket.SmartsocketClient({
url: "somedomain.com", // url, note: will only work over https, no http supported.
port: 3000
role:"dockerhost", // some role, in this example a dockerhost vm,
password:"somePassword"
});
let mySocketFunction2 = new smartsocket.SocketFunction({
func:() => {}, the function to execute
roles: [mySocketRole] // all roles that have access to a specific function
});
mySmartsocketClient.registerFunction(mySocketFunction2);
```

1
dist/index.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export * from "./smartsocket.classes.smartsocket";

6
dist/index.js vendored Normal file
View File

@ -0,0 +1,6 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(require("./smartsocket.classes.smartsocket"));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O0FBRUEsaUJBQWMsbUNBQW1DLENBQUMsRUFBQSJ9

View File

@ -0,0 +1,19 @@
/// <reference types="socket.io" />
/// <reference types="q" />
import * as plugins from "./smartsocket.plugins";
import { Objectmap } from "lik";
export interface ISocketObject {
socket: SocketIO.Socket;
authenticated: boolean;
}
export interface ISmartsocketConstructorOptions {
port: number;
}
export declare class Smartsocket {
io: SocketIO.Server;
openSockets: Objectmap;
constructor(options: ISmartsocketConstructorOptions);
startListening: any;
authenticateSocket(socketObjectArg: ISocketObject): plugins.q.Promise<{}>;
closeServer: () => void;
}

39
dist/smartsocket.classes.smartsocket.js vendored Normal file
View File

@ -0,0 +1,39 @@
"use strict";
const plugins = require("./smartsocket.plugins");
// classes
const lik_1 = require("lik");
;
class Smartsocket {
constructor(options) {
this.openSockets = new lik_1.Objectmap();
this.closeServer = () => {
this.io.close();
this.openSockets.forEach((socketObjectArg) => {
socketObjectArg.socket.disconnect();
});
this.openSockets.wipe();
};
this.io = plugins.socketIo(options.port);
this.io.on('connection', (socket) => {
let socketObject = {
socket: socket,
authenticated: false
};
this.openSockets.add(socketObject);
this.authenticateSocket(socketObject);
});
}
;
authenticateSocket(socketObjectArg) {
let done = plugins.q.defer();
socketObjectArg.socket.on("dataAuth", data => {
socketObjectArg.socket.removeListener("dataAuth", () => { });
done.resolve();
});
socketObjectArg.socket.emit("requestAuth");
return done.promise;
}
;
}
exports.Smartsocket = Smartsocket;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHVCQUF1QixDQUFDLENBQUE7QUFHakQsVUFBVTtBQUNWLHNCQUEwQixLQUFLLENBQUMsQ0FBQTtBQUsvQixDQUFDO0FBT0Y7SUFHSSxZQUFZLE9BQXVDO1FBRG5ELGdCQUFXLEdBQUcsSUFBSSxlQUFTLEVBQUUsQ0FBQztRQXNCOUIsZ0JBQVcsR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O1FBQ0gsZUFBZSxDQUFDLE1BQU0sQ0FBQyxJQUFJLENBQUMsYUFBYSxDQUFDLENBQUM7UUFDM0MsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUM7SUFDeEIsQ0FBQzs7QUFRTCxDQUFDO0FBL0JZLG1CQUFXLGNBK0J2QixDQUFBIn0=

2
dist/smartsocket.helpers.js vendored Normal file
View File

@ -0,0 +1,2 @@
"use strict";
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuaGVscGVycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmhlbHBlcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiJ9

6
dist/smartsocket.plugins.d.ts vendored Normal file
View File

@ -0,0 +1,6 @@
import "typings-global";
export import beautylog = require("beautylog");
export import lik = require("lik");
export import q = require("q");
export import socketIo = require("socket.io");
export import taskbuffer = require("taskbuffer");

8
dist/smartsocket.plugins.js vendored Normal file
View File

@ -0,0 +1,8 @@
"use strict";
require("typings-global");
exports.beautylog = require("beautylog");
exports.lik = require("lik");
exports.q = require("q");
exports.socketIo = require("socket.io");
exports.taskbuffer = require("taskbuffer");
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sZ0JBQWdCLENBQUMsQ0FBQTtBQUNWLGlCQUFTLFdBQVcsV0FBVyxDQUFDLENBQUM7QUFDakMsV0FBRyxXQUFZLEtBQUssQ0FBQyxDQUFDO0FBQ3RCLFNBQUMsV0FBVyxHQUFHLENBQUMsQ0FBQztBQUNqQixnQkFBUSxXQUFXLFdBQVcsQ0FBQyxDQUFDO0FBQ2hDLGtCQUFVLFdBQVcsWUFBWSxDQUFDLENBQUMifQ==

View File

@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "easy and secure websocket communication",
"main": "dist/index.js",
"typings":"dist/index.d.ts",
"scripts": {
"test": "(npmts)"
},
@ -17,10 +18,18 @@
},
"homepage": "https://gitlab.com/pushrocks/smartsocket#README",
"dependencies": {
"@types/q": "0.x.x",
"@types/socket.io": "^1.4.26",
"@types/socket.io-client": "^1.4.26",
"beautylog": "^5.0.20",
"lik": "^1.0.9",
"q": "^1.4.1",
"socket.io": "^1.4.8",
"socket.io-client": "^1.4.8",
"taskbuffer": "^1.0.7"
},
"devDependencies": {
"should": "^10.0.0",
"typings-test": "^1.0.1"
}
}

2
test/test.d.ts vendored Normal file
View File

@ -0,0 +1,2 @@
import "typings-test";
import "should";

24
test/test.js Normal file
View File

@ -0,0 +1,24 @@
"use strict";
require("typings-test");
require("should");
const socketIoClient = require("socket.io-client");
const smartsocket = require("../dist/index");
let testSmartsocket;
describe("smartsocket", function () {
it("should create a new smartsocket", function () {
testSmartsocket = new smartsocket.Smartsocket({ port: 3000 });
testSmartsocket.should.be.instanceOf(smartsocket.Smartsocket);
});
it("should react to a new websocket connection", function (done) {
this.timeout(10000);
let socket = socketIoClient("http://localhost:3000", {});
socket.on("requestAuth", function () {
console.log("server requested authentication");
socket.emit("dataAuth", {
secret: "hello"
});
done();
});
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsUUFBTyxRQUFRLENBQUMsQ0FBQTtBQUNoQixNQUFPLGNBQWMsV0FBVyxrQkFBa0IsQ0FBQyxDQUFDO0FBQ3BELE1BQU8sV0FBVyxXQUFXLGVBQWUsQ0FBQyxDQUFDO0FBRTlDLElBQUksZUFBd0MsQ0FBQztBQUU3QyxRQUFRLENBQUMsYUFBYSxFQUFFO0lBQ3BCLEVBQUUsQ0FBQyxpQ0FBaUMsRUFBRTtRQUNsQyxlQUFlLEdBQUcsSUFBSSxXQUFXLENBQUMsV0FBVyxDQUFDLEVBQUUsSUFBSSxFQUFFLElBQUksRUFBRSxDQUFDLENBQUM7UUFDOUQsZUFBZSxDQUFDLE1BQU0sQ0FBQyxFQUFFLENBQUMsVUFBVSxDQUFDLFdBQVcsQ0FBQyxXQUFXLENBQUMsQ0FBQztJQUNsRSxDQUFDLENBQUMsQ0FBQztJQUNILEVBQUUsQ0FBQyw0Q0FBNEMsRUFBRSxVQUFVLElBQUk7UUFDM0QsSUFBSSxDQUFDLE9BQU8sQ0FBQyxLQUFLLENBQUMsQ0FBQztRQUNwQixJQUFJLE1BQU0sR0FBRyxjQUFjLENBQUMsdUJBQXVCLEVBQUUsRUFBRSxDQUFDLENBQUM7UUFDekQsTUFBTSxDQUFDLEVBQUUsQ0FBQyxhQUFhLEVBQUU7WUFDckIsT0FBTyxDQUFDLEdBQUcsQ0FBQyxpQ0FBaUMsQ0FBQyxDQUFDO1lBQy9DLE1BQU0sQ0FBQyxJQUFJLENBQUMsVUFBVSxFQUFFO2dCQUNwQixNQUFNLEVBQUUsT0FBTzthQUNsQixDQUFDLENBQUM7WUFDSCxJQUFJLEVBQUUsQ0FBQztRQUNYLENBQUMsQ0FBQyxDQUFDO0lBQ1AsQ0FBQyxDQUFDLENBQUE7QUFDTixDQUFDLENBQUMsQ0FBQyJ9

24
test/test.ts Normal file
View File

@ -0,0 +1,24 @@
import "typings-test";
import "should";
import socketIoClient = require("socket.io-client");
import smartsocket = require("../dist/index");
let testSmartsocket: smartsocket.Smartsocket;
describe("smartsocket", function () {
it("should create a new smartsocket", function () {
testSmartsocket = new smartsocket.Smartsocket({ port: 3000 });
testSmartsocket.should.be.instanceOf(smartsocket.Smartsocket);
});
it("should react to a new websocket connection", function (done) {
this.timeout(10000);
let socket = socketIoClient("http://localhost:3000", {});
socket.on("requestAuth", function () {
console.log("server requested authentication");
socket.emit("dataAuth", {
secret: "hello"
});
done();
});
})
});

View File

@ -0,0 +1,4 @@
import * as plugins from "./smartsocket.plugins";
export * from "./smartsocket.classes.smartsocket";
export * from "./smartsocket.classes.smartsocketclient";

View File

@ -1,6 +0,0 @@
import "typings-global";
export import beautylog = require("beautylog");
export import lik = require("lik");
export import q = require("q");
export import socketIo = require("socket.io");
export import taskbuffer = require("taskbuffer");

View File

@ -0,0 +1,50 @@
import * as plugins from "./smartsocket.plugins";
import * as helpers from "./smartsocket.helpers";
// classes
import { Objectmap } from "lik";
export interface ISocketObject {
group?:string,
socket: SocketIO.Socket,
authenticated: boolean
};
export interface ISmartsocketConstructorOptions {
port: number;
}
export class Smartsocket {
io: SocketIO.Server;
openSockets = new Objectmap();
constructor(options: ISmartsocketConstructorOptions) {
this.io = plugins.socketIo(options.port);
this.io.on('connection', this._handleSocket);
};
/**
* the standard handler for new socket connections
*/
private _handleSocket(socket) {
let socketObject: ISocketObject = {
socket: socket,
authenticated: false
};
this.openSockets.add(socketObject);
helpers.authenticateSocket(socketObject)
.then();
}
registerGroup(string){
}
closeServer = () => {
this.io.close();
this.openSockets.forEach((socketObjectArg: ISocketObject) => {
socketObjectArg.socket.disconnect();
});
this.openSockets.wipe();
}
}

View File

@ -0,0 +1,15 @@
import * as plugins from "./smartsocket.plugins"
/**
* interface for class SmartsocketClient
*/
export interface ISmartsocketClientOptions {
port:number;
url:string;
}
export class SmartsocketClient {
constructor(){
}
}

View File

@ -0,0 +1,10 @@
import * as plugins from "./smartsocket.plugins";
/**
* A socketrole defines access to certain routines.
*/
class Role {
constructor(){
}
}

20
ts/smartsocket.helpers.ts Normal file
View File

@ -0,0 +1,20 @@
import * as plugins from "./smartsocket.plugins";
// interfaces
import {ISocketObject} from "./smartsocket.classes.smartsocket";
/**
* authenticate a socket
*/
export let authenticateSocket = (socketObjectArg: ISocketObject) => {
let done = plugins.q.defer();
socketObjectArg.socket.on("dataAuth", data => {
socketObjectArg.socket.removeListener("dataAuth", () => { });
done.resolve();
});
socketObjectArg.socket.emit("requestAuth");
return done.promise;
};

View File

@ -0,0 +1,7 @@
import "typings-global";
export import beautylog = require("beautylog");
export import lik = require("lik");
export import q = require("q");
export import socketIo = require("socket.io");
export import socketIoClient = require("socket.io-client");
export import taskbuffer = require("taskbuffer");