From 309284ab68a0dc74d6a3eecea23db8091d517fdb Mon Sep 17 00:00:00 2001 From: PhilKunz Date: Sun, 7 Aug 2016 14:58:20 +0200 Subject: [PATCH] update structure --- .gitignore | 6 +-- README.md | 37 ++++++++++++++ dist/index.d.ts | 1 + dist/index.js | 6 +++ dist/smartsocket.classes.smartsocket.d.ts | 19 +++++++ dist/smartsocket.classes.smartsocket.js | 39 +++++++++++++++ .../smartsocket.helpers.d.ts | 0 dist/smartsocket.helpers.js | 2 + dist/smartsocket.plugins.d.ts | 6 +++ dist/smartsocket.plugins.js | 8 +++ package.json | 9 ++++ test/test.d.ts | 2 + test/test.js | 24 +++++++++ test/test.ts | 24 +++++++++ ts/index.ts | 4 ++ ts/smartsock.classes.smartsock.ts | 6 --- ts/smartsocket.classes.smartsocket.ts | 50 +++++++++++++++++++ ts/smartsocket.classes.smartsocketclient.ts | 15 ++++++ ts/smartsocket.classes.socketrole.ts | 10 ++++ ts/smartsocket.helpers.ts | 20 ++++++++ ts/smartsocket.plugins.ts | 7 +++ 21 files changed, 286 insertions(+), 9 deletions(-) create mode 100644 dist/index.d.ts create mode 100644 dist/index.js create mode 100644 dist/smartsocket.classes.smartsocket.d.ts create mode 100644 dist/smartsocket.classes.smartsocket.js rename ts/smartsock.plugins.ts => dist/smartsocket.helpers.d.ts (100%) create mode 100644 dist/smartsocket.helpers.js create mode 100644 dist/smartsocket.plugins.d.ts create mode 100644 dist/smartsocket.plugins.js create mode 100644 test/test.d.ts create mode 100644 test/test.js create mode 100644 test/test.ts delete mode 100644 ts/smartsock.classes.smartsock.ts create mode 100644 ts/smartsocket.classes.smartsocket.ts create mode 100644 ts/smartsocket.classes.smartsocketclient.ts create mode 100644 ts/smartsocket.classes.socketrole.ts create mode 100644 ts/smartsocket.helpers.ts create mode 100644 ts/smartsocket.plugins.ts diff --git a/.gitignore b/.gitignore index 645c923..2f0dc55 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -node_modules -coverage -docs/ \ No newline at end of file +node_modules/ +coverage/ +docs/ diff --git a/README.md b/README.md index fde8773..145d02c 100644 --- a/README.md +++ b/README.md @@ -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); + + +``` \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..6da3fef --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1 @@ +export * from "./smartsocket.classes.smartsocket"; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..e3e1dab --- /dev/null +++ b/dist/index.js @@ -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 \ No newline at end of file diff --git a/dist/smartsocket.classes.smartsocket.d.ts b/dist/smartsocket.classes.smartsocket.d.ts new file mode 100644 index 0000000..d8f0be1 --- /dev/null +++ b/dist/smartsocket.classes.smartsocket.d.ts @@ -0,0 +1,19 @@ +/// +/// +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; +} diff --git a/dist/smartsocket.classes.smartsocket.js b/dist/smartsocket.classes.smartsocket.js new file mode 100644 index 0000000..b1e53cf --- /dev/null +++ b/dist/smartsocket.classes.smartsocket.js @@ -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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuY2xhc3Nlcy5zbWFydHNvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmNsYXNzZXMuc21hcnRzb2NrZXQudHMiXSwibmFtZXMiO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QUM7UUFDN0IsZUFBZSxDQUFDLE1BQU0sQ0FBQyxFQUFFLENBQUMsVUFBVSxFQUFFLElBQUk7WUFDdEMsZUFBZSxDQUFDLE1BQU0sQ0FBQyxjQUFjLENBQUMsVUFBVSxFQUFFLFFBQVEsQ0FBQyxDQUFDLENBQUM7WUFDN0QsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFDO1FBQ25CLENBQUMsQ0FBQyxDQUFDO1FBQ0gsZUFBZSxDQUFDLE1BQU0sQ0FBQyxJQUFJLENBQUMsYUFBYSxDQUFDLENBQUM7UUFDM0MsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUM7SUFDeEIsQ0FBQzs7QUFRTCxDQUFDO0FBL0JZLG1CQUFXLGNBK0J2QixDQUFBIn0= \ No newline at end of file diff --git a/ts/smartsock.plugins.ts b/dist/smartsocket.helpers.d.ts similarity index 100% rename from ts/smartsock.plugins.ts rename to dist/smartsocket.helpers.d.ts diff --git a/dist/smartsocket.helpers.js b/dist/smartsocket.helpers.js new file mode 100644 index 0000000..1834b0a --- /dev/null +++ b/dist/smartsocket.helpers.js @@ -0,0 +1,2 @@ +"use strict"; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzb2NrZXQuaGVscGVycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c29ja2V0LmhlbHBlcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiJ9 \ No newline at end of file diff --git a/dist/smartsocket.plugins.d.ts b/dist/smartsocket.plugins.d.ts new file mode 100644 index 0000000..5de4cd4 --- /dev/null +++ b/dist/smartsocket.plugins.d.ts @@ -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"); diff --git a/dist/smartsocket.plugins.js b/dist/smartsocket.plugins.js new file mode 100644 index 0000000..5a8521c --- /dev/null +++ b/dist/smartsocket.plugins.js @@ -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== \ No newline at end of file diff --git a/package.json b/package.json index d47a27e..9fb2acb 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/test.d.ts b/test/test.d.ts new file mode 100644 index 0000000..b973566 --- /dev/null +++ b/test/test.d.ts @@ -0,0 +1,2 @@ +import "typings-test"; +import "should"; diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..d6f5add --- /dev/null +++ b/test/test.js @@ -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 \ No newline at end of file diff --git a/test/test.ts b/test/test.ts new file mode 100644 index 0000000..5517327 --- /dev/null +++ b/test/test.ts @@ -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(); + }); + }) +}); \ No newline at end of file diff --git a/ts/index.ts b/ts/index.ts index e69de29..e95ea2d 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -0,0 +1,4 @@ +import * as plugins from "./smartsocket.plugins"; + +export * from "./smartsocket.classes.smartsocket"; +export * from "./smartsocket.classes.smartsocketclient"; diff --git a/ts/smartsock.classes.smartsock.ts b/ts/smartsock.classes.smartsock.ts deleted file mode 100644 index 3fd4372..0000000 --- a/ts/smartsock.classes.smartsock.ts +++ /dev/null @@ -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"); \ No newline at end of file diff --git a/ts/smartsocket.classes.smartsocket.ts b/ts/smartsocket.classes.smartsocket.ts new file mode 100644 index 0000000..04adf75 --- /dev/null +++ b/ts/smartsocket.classes.smartsocket.ts @@ -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(); + } +} \ No newline at end of file diff --git a/ts/smartsocket.classes.smartsocketclient.ts b/ts/smartsocket.classes.smartsocketclient.ts new file mode 100644 index 0000000..2dd2433 --- /dev/null +++ b/ts/smartsocket.classes.smartsocketclient.ts @@ -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(){ + + } +} \ No newline at end of file diff --git a/ts/smartsocket.classes.socketrole.ts b/ts/smartsocket.classes.socketrole.ts new file mode 100644 index 0000000..d52c83a --- /dev/null +++ b/ts/smartsocket.classes.socketrole.ts @@ -0,0 +1,10 @@ +import * as plugins from "./smartsocket.plugins"; + +/** + * A socketrole defines access to certain routines. + */ +class Role { + constructor(){ + + } +} \ No newline at end of file diff --git a/ts/smartsocket.helpers.ts b/ts/smartsocket.helpers.ts new file mode 100644 index 0000000..dc9a197 --- /dev/null +++ b/ts/smartsocket.helpers.ts @@ -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; +}; \ No newline at end of file diff --git a/ts/smartsocket.plugins.ts b/ts/smartsocket.plugins.ts new file mode 100644 index 0000000..9a564b4 --- /dev/null +++ b/ts/smartsocket.plugins.ts @@ -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"); \ No newline at end of file