From 12fdd213157f05eead001a1b2e9a2289a5c17b54 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Sat, 10 Jun 2017 01:44:31 +0200 Subject: [PATCH] openvidu-node-client initial version --- openvidu-node-client/.gitignore | 43 ++++++++ openvidu-node-client/README.md | 28 +++++ openvidu-node-client/lib/OpenVidu.d.ts | 7 ++ openvidu-node-client/lib/OpenVidu.js | 15 +++ openvidu-node-client/lib/OpenVidu.js.map | 1 + openvidu-node-client/lib/OpenViduRole.d.ts | 5 + openvidu-node-client/lib/OpenViduRole.js | 9 ++ openvidu-node-client/lib/OpenViduRole.js.map | 1 + openvidu-node-client/lib/Session.d.ts | 15 +++ openvidu-node-client/lib/Session.js | 100 +++++++++++++++++ openvidu-node-client/lib/Session.js.map | 1 + openvidu-node-client/lib/TokenOptions.d.ts | 17 +++ openvidu-node-client/lib/TokenOptions.js | 40 +++++++ openvidu-node-client/lib/TokenOptions.js.map | 1 + openvidu-node-client/lib/index.d.ts | 4 + openvidu-node-client/lib/index.js | 10 ++ openvidu-node-client/lib/index.js.map | 1 + openvidu-node-client/package.json | 19 ++++ openvidu-node-client/src/OpenVidu.ts | 11 ++ openvidu-node-client/src/OpenViduRole.ts | 5 + openvidu-node-client/src/Session.ts | 109 +++++++++++++++++++ openvidu-node-client/src/TokenOptions.ts | 40 +++++++ openvidu-node-client/src/index.ts | 4 + openvidu-node-client/tsconfig.json | 20 ++++ 24 files changed, 506 insertions(+) create mode 100644 openvidu-node-client/.gitignore create mode 100644 openvidu-node-client/README.md create mode 100644 openvidu-node-client/lib/OpenVidu.d.ts create mode 100644 openvidu-node-client/lib/OpenVidu.js create mode 100644 openvidu-node-client/lib/OpenVidu.js.map create mode 100644 openvidu-node-client/lib/OpenViduRole.d.ts create mode 100644 openvidu-node-client/lib/OpenViduRole.js create mode 100644 openvidu-node-client/lib/OpenViduRole.js.map create mode 100644 openvidu-node-client/lib/Session.d.ts create mode 100644 openvidu-node-client/lib/Session.js create mode 100644 openvidu-node-client/lib/Session.js.map create mode 100644 openvidu-node-client/lib/TokenOptions.d.ts create mode 100644 openvidu-node-client/lib/TokenOptions.js create mode 100644 openvidu-node-client/lib/TokenOptions.js.map create mode 100644 openvidu-node-client/lib/index.d.ts create mode 100644 openvidu-node-client/lib/index.js create mode 100644 openvidu-node-client/lib/index.js.map create mode 100644 openvidu-node-client/package.json create mode 100644 openvidu-node-client/src/OpenVidu.ts create mode 100644 openvidu-node-client/src/OpenViduRole.ts create mode 100644 openvidu-node-client/src/Session.ts create mode 100644 openvidu-node-client/src/TokenOptions.ts create mode 100644 openvidu-node-client/src/index.ts create mode 100644 openvidu-node-client/tsconfig.json diff --git a/openvidu-node-client/.gitignore b/openvidu-node-client/.gitignore new file mode 100644 index 00000000..f845322e --- /dev/null +++ b/openvidu-node-client/.gitignore @@ -0,0 +1,43 @@ +# See http://help.github.com/ignore-files/ for more about ignoring files. + +# compiled output +/dist +/tmp +/out-tsc + +# dependencies +/node_modules + +# IDEs and editors +/.idea +.project +.classpath +.c9/ +*.launch +.settings/ +*.sublime-workspace + +# IDE - VSCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json + +# misc +/.sass-cache +/connect.lock +/coverage +/libpeerconnection.log +npm-debug.log +testem.log +/typings + +# e2e +/e2e/*.js +/e2e/*.map + +# System Files +.DS_Store +Thumbs.db +*.editorconfig diff --git a/openvidu-node-client/README.md b/openvidu-node-client/README.md new file mode 100644 index 00000000..6b09e2c1 --- /dev/null +++ b/openvidu-node-client/README.md @@ -0,0 +1,28 @@ +# OpenviduNodeClient + +This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 1.0.2. + +## Development server + +Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. + +## Code scaffolding + +Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|module`. + +## Build + +Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `-prod` flag for a production build. + +## Running unit tests + +Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io). + +## Running end-to-end tests + +Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/). +Before running the tests make sure you are serving the app via `ng serve`. + +## Further help + +To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md). diff --git a/openvidu-node-client/lib/OpenVidu.d.ts b/openvidu-node-client/lib/OpenVidu.d.ts new file mode 100644 index 00000000..7d72cb75 --- /dev/null +++ b/openvidu-node-client/lib/OpenVidu.d.ts @@ -0,0 +1,7 @@ +import { Session } from "./Session"; +export declare class OpenVidu { + private urlOpenViduServer; + private secret; + constructor(urlOpenViduServer: string, secret: string); + createSession(): Session; +} diff --git a/openvidu-node-client/lib/OpenVidu.js b/openvidu-node-client/lib/OpenVidu.js new file mode 100644 index 00000000..8a61fb6f --- /dev/null +++ b/openvidu-node-client/lib/OpenVidu.js @@ -0,0 +1,15 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var Session_1 = require("./Session"); +var OpenVidu = (function () { + function OpenVidu(urlOpenViduServer, secret) { + this.urlOpenViduServer = urlOpenViduServer; + this.secret = secret; + } + OpenVidu.prototype.createSession = function () { + return new Session_1.Session(this.urlOpenViduServer, this.secret); + }; + return OpenVidu; +}()); +exports.OpenVidu = OpenVidu; +//# sourceMappingURL=OpenVidu.js.map \ No newline at end of file diff --git a/openvidu-node-client/lib/OpenVidu.js.map b/openvidu-node-client/lib/OpenVidu.js.map new file mode 100644 index 00000000..d8c31617 --- /dev/null +++ b/openvidu-node-client/lib/OpenVidu.js.map @@ -0,0 +1 @@ +{"version":3,"file":"OpenVidu.js","sourceRoot":"","sources":["../src/OpenVidu.ts"],"names":[],"mappings":";;AAAA,qCAAoC;AAEpC;IAEE,kBAAoB,iBAAyB,EAAU,MAAc;QAAjD,sBAAiB,GAAjB,iBAAiB,CAAQ;QAAU,WAAM,GAAN,MAAM,CAAQ;IAAG,CAAC;IAElE,gCAAa,GAApB;QACI,MAAM,CAAC,IAAI,iBAAO,CAAC,IAAI,CAAC,iBAAiB,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5D,CAAC;IAEH,eAAC;AAAD,CAAC,AARD,IAQC;AARY,4BAAQ"} \ No newline at end of file diff --git a/openvidu-node-client/lib/OpenViduRole.d.ts b/openvidu-node-client/lib/OpenViduRole.d.ts new file mode 100644 index 00000000..d3e92d58 --- /dev/null +++ b/openvidu-node-client/lib/OpenViduRole.d.ts @@ -0,0 +1,5 @@ +export declare enum OpenViduRole { + SUBSCRIBER, + PUBLISHER, + MODERATOR, +} diff --git a/openvidu-node-client/lib/OpenViduRole.js b/openvidu-node-client/lib/OpenViduRole.js new file mode 100644 index 00000000..709ac303 --- /dev/null +++ b/openvidu-node-client/lib/OpenViduRole.js @@ -0,0 +1,9 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var OpenViduRole; +(function (OpenViduRole) { + OpenViduRole[OpenViduRole["SUBSCRIBER"] = 'SUBSCRIBER'] = "SUBSCRIBER"; + OpenViduRole[OpenViduRole["PUBLISHER"] = 'PUBLISHER'] = "PUBLISHER"; + OpenViduRole[OpenViduRole["MODERATOR"] = 'MODERATOR'] = "MODERATOR"; +})(OpenViduRole = exports.OpenViduRole || (exports.OpenViduRole = {})); +//# sourceMappingURL=OpenViduRole.js.map \ No newline at end of file diff --git a/openvidu-node-client/lib/OpenViduRole.js.map b/openvidu-node-client/lib/OpenViduRole.js.map new file mode 100644 index 00000000..0515959c --- /dev/null +++ b/openvidu-node-client/lib/OpenViduRole.js.map @@ -0,0 +1 @@ +{"version":3,"file":"OpenViduRole.js","sourceRoot":"","sources":["../src/OpenViduRole.ts"],"names":[],"mappings":";;AAAA,IAAY,YAIX;AAJD,WAAY,YAAY;IACvB,0CAAkB,YAAY,gBAAA,CAAA;IAC9B,yCAAiB,WAAW,eAAA,CAAA;IAC5B,yCAAiB,WAAW,eAAA,CAAA;AAC7B,CAAC,EAJW,YAAY,GAAZ,oBAAY,KAAZ,oBAAY,QAIvB"} \ No newline at end of file diff --git a/openvidu-node-client/lib/Session.d.ts b/openvidu-node-client/lib/Session.d.ts new file mode 100644 index 00000000..4498ee81 --- /dev/null +++ b/openvidu-node-client/lib/Session.d.ts @@ -0,0 +1,15 @@ +import { TokenOptions } from './TokenOptions'; +export declare class Session { + private urlOpenViduServer; + private secret; + private sessionIdURL; + private tokenURL; + private sessionId; + private hostname; + private port; + constructor(urlOpenViduServer: string, secret: string); + getSessionId(callback: Function): string; + generateToken(tokenOptions: TokenOptions, callback: Function): void; + private getBasicAuth(); + private setHostnameAndPort(); +} diff --git a/openvidu-node-client/lib/Session.js b/openvidu-node-client/lib/Session.js new file mode 100644 index 00000000..e23d7ed3 --- /dev/null +++ b/openvidu-node-client/lib/Session.js @@ -0,0 +1,100 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var https = require('https'); +var Session = (function () { + function Session(urlOpenViduServer, secret) { + this.urlOpenViduServer = urlOpenViduServer; + this.secret = secret; + this.sessionIdURL = '/api/sessions'; + this.tokenURL = '/api/tokens'; + this.sessionId = ""; + this.setHostnameAndPort(); + } + Session.prototype.getSessionId = function (callback) { + var _this = this; + if (this.sessionId) { + return this.sessionId; + } + var options = { + hostname: this.hostname, + port: this.port, + path: this.sessionIdURL, + method: 'POST', + headers: { + 'Authorization': this.getBasicAuth() + } + }; + var req = https.request(options, function (res) { + var body = ''; + res.on('data', function (d) { + // Continuously update stream with data + body += d; + }); + res.on('end', function () { + // Data reception is done + var parsed = JSON.parse(body); + _this.sessionId = parsed.id; + callback(parsed.id); + }); + }); + req.on('error', function (e) { + console.error(e); + }); + req.end(); + }; + Session.prototype.generateToken = function (tokenOptions, callback) { + var requestBody = JSON.stringify({ + 'session': this.sessionId, + 'role': tokenOptions.getRole(), + 'data': tokenOptions.getData() + }); + var options = { + hostname: this.hostname, + port: this.port, + path: this.tokenURL, + method: 'POST', + headers: { + 'Authorization': this.getBasicAuth(), + 'Content-Type': 'application/json', + 'Content-Length': Buffer.byteLength(requestBody) + } + }; + var req = https.request(options, function (res) { + var body = ''; + res.on('data', function (d) { + // Continuously update stream with data + body += d; + }); + res.on('end', function () { + // Data reception is done + var parsed = JSON.parse(body); + callback(parsed.id); + }); + }); + req.on('error', function (e) { + console.error(e); + }); + req.write(requestBody); + req.end(); + }; + Session.prototype.getBasicAuth = function () { + return 'Basic ' + (new Buffer('OPENVIDUAPP:' + this.secret).toString('base64')); + }; + Session.prototype.setHostnameAndPort = function () { + var urlSplitted = this.urlOpenViduServer.split(':'); + if (urlSplitted.length === 3) { + this.hostname = this.urlOpenViduServer.split(':')[1].replace(/\//g, ''); + this.port = parseInt(this.urlOpenViduServer.split(':')[2].replace(/\//g, '')); + } + else if (urlSplitted.length == 2) { + this.hostname = this.urlOpenViduServer.split(':')[0].replace(/\//g, ''); + this.port = parseInt(this.urlOpenViduServer.split(':')[1].replace(/\//g, '')); + } + else { + console.error("URL format incorrect: it must contain hostname and port (current value: '" + this.urlOpenViduServer + "')"); + } + }; + return Session; +}()); +exports.Session = Session; +//# sourceMappingURL=Session.js.map \ No newline at end of file diff --git a/openvidu-node-client/lib/Session.js.map b/openvidu-node-client/lib/Session.js.map new file mode 100644 index 00000000..d538a6c4 --- /dev/null +++ b/openvidu-node-client/lib/Session.js.map @@ -0,0 +1 @@ +{"version":3,"file":"Session.js","sourceRoot":"","sources":["../src/Session.ts"],"names":[],"mappings":";;AAKA,IAAI,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAE7B;IAQI,iBAAoB,iBAAyB,EAAU,MAAc;QAAjD,sBAAiB,GAAjB,iBAAiB,CAAQ;QAAU,WAAM,GAAN,MAAM,CAAQ;QAN7D,iBAAY,GAAW,eAAe,CAAC;QACvC,aAAQ,GAAW,aAAa,CAAC;QACjC,cAAS,GAAW,EAAE,CAAC;QAK3B,IAAI,CAAC,kBAAkB,EAAE,CAAC;IAC9B,CAAC;IAEM,8BA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oCAAkB,GAA1B;QACI,IAAI,WAAW,GAAG,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACpD,EAAE,CAAC,CAAC,WAAW,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC;YAC3B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;YACxE,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC;QAClF,CAAC;QAAC,IAAI,CAAC,EAAE,CAAC,CAAC,WAAW,CAAC,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC;YACjC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;YACxE,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC;QAClF,CAAC;QAAC,IAAI,CAAC,CAAC;YACJ,OAAO,CAAC,KAAK,CAAC,2EAA2E,GAAG,IAAI,CAAC,iBAAiB,GAAG,IAAI,CAAC,CAAC;QAC/H,CAAC;IACL,CAAC;IAEL,cAAC;AAAD,CAAC,AArGD,IAqGC;AArGY,0BAAO"} \ No newline at end of file diff --git a/openvidu-node-client/lib/TokenOptions.d.ts b/openvidu-node-client/lib/TokenOptions.d.ts new file mode 100644 index 00000000..e3a4b907 --- /dev/null +++ b/openvidu-node-client/lib/TokenOptions.d.ts @@ -0,0 +1,17 @@ +import { OpenViduRole } from "./OpenViduRole"; +export declare class TokenOptions { + private data; + private role; + constructor(data: string, role: OpenViduRole); + getData(): string; + getRole(): OpenViduRole; +} +export declare namespace TokenOptions { + class Builder { + private dataProp; + private roleProp; + build(): TokenOptions; + data(data: string): Builder; + role(role: OpenViduRole): Builder; + } +} diff --git a/openvidu-node-client/lib/TokenOptions.js b/openvidu-node-client/lib/TokenOptions.js new file mode 100644 index 00000000..0c5bc9d1 --- /dev/null +++ b/openvidu-node-client/lib/TokenOptions.js @@ -0,0 +1,40 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var TokenOptions = (function () { + function TokenOptions(data, role) { + this.data = data; + this.role = role; + this.data = data; + this.role = role; + } + TokenOptions.prototype.getData = function () { + return this.data; + }; + TokenOptions.prototype.getRole = function () { + return this.role; + }; + return TokenOptions; +}()); +exports.TokenOptions = TokenOptions; +(function (TokenOptions) { + var Builder = (function () { + function Builder() { + } + Builder.prototype.build = function () { + return new TokenOptions(this.dataProp, this.roleProp); + }; + Builder.prototype.data = function (data) { + this.dataProp = data; + return this; + }; + Builder.prototype.role = function (role) { + this.roleProp = role; + return this; + }; + return Builder; + }()); + TokenOptions.Builder = Builder; + ; +})(TokenOptions = exports.TokenOptions || (exports.TokenOptions = {})); +exports.TokenOptions = TokenOptions; +//# sourceMappingURL=TokenOptions.js.map \ No newline at end of file diff --git a/openvidu-node-client/lib/TokenOptions.js.map b/openvidu-node-client/lib/TokenOptions.js.map new file mode 100644 index 00000000..8ad14946 --- /dev/null +++ b/openvidu-node-client/lib/TokenOptions.js.map @@ -0,0 +1 @@ +{"version":3,"file":"TokenOptions.js","sourceRoot":"","sources":["../src/TokenOptions.ts"],"names":[],"mappings":";;AAEA;IAEI,sBAAoB,IAAY,EAAU,IAAkB;QAAxC,SAAI,GAAJ,IAAI,CAAQ;QAAU,SAAI,GAAJ,IAAI,CAAc;QACxD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACrB,CAAC;IAEM,8BAAO,GAAd;QACI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IAEM,8BAAO,GAAd;QACI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IACL,mBAAC;AAAD,CAAC,AAdD,IAcC;AAdY,oCAAY;AAgBzB,WAAiB,YAAY;IACzB;QAAA;QAmBA,CAAC;QAdG,uBAAK,GAAL;YACI,MAAM,CAAC,IAAI,YAAY,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC1D,CAAC;QAED,sBAAI,GAAJ,UAAK,IAAY;YACb,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;YACrB,MAAM,CAAC,IAAI,CAAC;QAChB,CAAC;QAED,sBAAI,GAAJ,UAAK,IAAkB;YACnB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;YACrB,MAAM,CAAC,IAAI,CAAC;QAChB,CAAC;QAEL,cAAC;IAAD,CAAC,AAnBD,IAmBC;IAnBY,oBAAO,UAmBnB,CAAA;IAAA,CAAC;AACN,CAAC,EArBgB,YAAY,GAAZ,oBAAY,KAAZ,oBAAY,QAqB5B;AArCY,oCAAY"} \ No newline at end of file diff --git a/openvidu-node-client/lib/index.d.ts b/openvidu-node-client/lib/index.d.ts new file mode 100644 index 00000000..62891d7f --- /dev/null +++ b/openvidu-node-client/lib/index.d.ts @@ -0,0 +1,4 @@ +export * from './OpenVidu'; +export * from './OpenViduRole'; +export * from './Session'; +export * from './TokenOptions'; diff --git a/openvidu-node-client/lib/index.js b/openvidu-node-client/lib/index.js new file mode 100644 index 00000000..19c30def --- /dev/null +++ b/openvidu-node-client/lib/index.js @@ -0,0 +1,10 @@ +"use strict"; +function __export(m) { + for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; +} +Object.defineProperty(exports, "__esModule", { value: true }); +__export(require("./OpenVidu")); +__export(require("./OpenViduRole")); +__export(require("./Session")); +__export(require("./TokenOptions")); +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/openvidu-node-client/lib/index.js.map b/openvidu-node-client/lib/index.js.map new file mode 100644 index 00000000..6ad1647a --- /dev/null +++ b/openvidu-node-client/lib/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,gCAA2B;AAC3B,oCAA+B;AAC/B,+BAA0B;AAC1B,oCAA+B"} \ No newline at end of file diff --git a/openvidu-node-client/package.json b/openvidu-node-client/package.json new file mode 100644 index 00000000..394d5be4 --- /dev/null +++ b/openvidu-node-client/package.json @@ -0,0 +1,19 @@ +{ + "name": "openvidu-node-client", + "version": "1.0.0", + "license": "MIT", + "main": "lib/index.js", + "typings": "lib/index.d.ts", + "scripts": { + "build": "tsc" + }, + "private": true, + "dependencies": {}, + "devDependencies": { + "@types/node": "^6.0.78", + "codelyzer": "~2.0.0", + "ts-node": "~2.0.0", + "tslint": "~4.5.0", + "typescript": "2.3.4" + } +} diff --git a/openvidu-node-client/src/OpenVidu.ts b/openvidu-node-client/src/OpenVidu.ts new file mode 100644 index 00000000..c9093161 --- /dev/null +++ b/openvidu-node-client/src/OpenVidu.ts @@ -0,0 +1,11 @@ +import { Session } from "./Session"; + +export class OpenVidu { + + constructor(private urlOpenViduServer: string, private secret: string){ } + + public createSession(): Session { + return new Session(this.urlOpenViduServer, this.secret); + } + +} \ No newline at end of file diff --git a/openvidu-node-client/src/OpenViduRole.ts b/openvidu-node-client/src/OpenViduRole.ts new file mode 100644 index 00000000..06e188a1 --- /dev/null +++ b/openvidu-node-client/src/OpenViduRole.ts @@ -0,0 +1,5 @@ +export enum OpenViduRole { + SUBSCRIBER = 'SUBSCRIBER', + PUBLISHER = 'PUBLISHER', + MODERATOR = 'MODERATOR' +} \ No newline at end of file diff --git a/openvidu-node-client/src/Session.ts b/openvidu-node-client/src/Session.ts new file mode 100644 index 00000000..1202f24c --- /dev/null +++ b/openvidu-node-client/src/Session.ts @@ -0,0 +1,109 @@ +import { TokenOptions } from './TokenOptions'; + +declare const Buffer; +declare const require; + +var https = require('https'); + +export class Session { + + private sessionIdURL: string = '/api/sessions'; + private tokenURL: string = '/api/tokens'; + private sessionId: string = ""; + private hostname: string; + private port: number; + + constructor(private urlOpenViduServer: string, private secret: string) { + this.setHostnameAndPort(); + } + + public getSessionId(callback: Function) { + + if (this.sessionId) { + return this.sessionId; + } + + let options = { + hostname: this.hostname, + port: this.port, + path: this.sessionIdURL, + method: 'POST', + headers: { + 'Authorization': this.getBasicAuth() + } + } + const req = https.request(options, (res) => { + var body = ''; + res.on('data', (d) => { + // Continuously update stream with data + body += d; + }); + res.on('end', () => { + // Data reception is done + var parsed = JSON.parse(body); + this.sessionId = parsed.id; + callback(parsed.id); + }); + }); + + req.on('error', (e) => { + console.error(e); + }); + req.end(); + } + + public generateToken(tokenOptions: TokenOptions, callback: Function) { + var requestBody = JSON.stringify({ + 'session': this.sessionId, + 'role': tokenOptions.getRole(), + 'data': tokenOptions.getData() + }); + var options = { + hostname: this.hostname, + port: this.port, + path: this.tokenURL, + method: 'POST', + headers: { + 'Authorization': this.getBasicAuth(), + 'Content-Type': 'application/json', + 'Content-Length': Buffer.byteLength(requestBody) + } + }; + const req = https.request(options, (res) => { + var body = ''; + res.on('data', (d) => { + // Continuously update stream with data + body += d; + }); + res.on('end', () => { + // Data reception is done + var parsed = JSON.parse(body); + callback(parsed.id); + }); + }); + + req.on('error', (e) => { + console.error(e); + }); + req.write(requestBody); + req.end(); + } + + private getBasicAuth() { + return 'Basic ' + (new Buffer('OPENVIDUAPP:' + this.secret).toString('base64')); + } + + private setHostnameAndPort() { + var urlSplitted = this.urlOpenViduServer.split(':'); + if (urlSplitted.length === 3) { // URL has format: http:// + hostname + :port + this.hostname = this.urlOpenViduServer.split(':')[1].replace(/\//g, ''); + this.port = parseInt(this.urlOpenViduServer.split(':')[2].replace(/\//g, '')); + } else if (urlSplitted.length == 2) { // URL has format: hostname + :port + this.hostname = this.urlOpenViduServer.split(':')[0].replace(/\//g, ''); + this.port = parseInt(this.urlOpenViduServer.split(':')[1].replace(/\//g, '')); + } else { + console.error("URL format incorrect: it must contain hostname and port (current value: '" + this.urlOpenViduServer + "')"); + } + } + +} \ No newline at end of file diff --git a/openvidu-node-client/src/TokenOptions.ts b/openvidu-node-client/src/TokenOptions.ts new file mode 100644 index 00000000..e481c770 --- /dev/null +++ b/openvidu-node-client/src/TokenOptions.ts @@ -0,0 +1,40 @@ +import { OpenViduRole } from "./OpenViduRole"; + +export class TokenOptions { + + constructor(private data: string, private role: OpenViduRole) { + this.data = data; + this.role = role; + } + + public getData(): string { + return this.data; + } + + public getRole(): OpenViduRole { + return this.role; + } +} + +export namespace TokenOptions { + export class Builder { + + private dataProp: string; + private roleProp: OpenViduRole; + + build(): TokenOptions { + return new TokenOptions(this.dataProp, this.roleProp); + } + + data(data: string): Builder { + this.dataProp = data; + return this; + } + + role(role: OpenViduRole): Builder { + this.roleProp = role; + return this; + } + + }; +} diff --git a/openvidu-node-client/src/index.ts b/openvidu-node-client/src/index.ts new file mode 100644 index 00000000..df975f07 --- /dev/null +++ b/openvidu-node-client/src/index.ts @@ -0,0 +1,4 @@ +export * from './OpenVidu'; +export * from './OpenViduRole'; +export * from './Session'; +export * from './TokenOptions'; \ No newline at end of file diff --git a/openvidu-node-client/tsconfig.json b/openvidu-node-client/tsconfig.json new file mode 100644 index 00000000..86c44bc2 --- /dev/null +++ b/openvidu-node-client/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compileOnSave": false, + "compilerOptions": { + "outDir": "lib", + "baseUrl": "src", + "sourceMap": true, + "declaration": true, + "moduleResolution": "node", + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "target": "es5", + "typeRoots": [ + "node_modules/@types" + ], + "lib": [ + "es2016", + "dom" + ] + } +}