diff --git a/openvidu-node-client/lib/OpenVidu.d.ts b/openvidu-node-client/lib/OpenVidu.d.ts index ba1a5c4c..00e23861 100644 --- a/openvidu-node-client/lib/OpenVidu.d.ts +++ b/openvidu-node-client/lib/OpenVidu.d.ts @@ -1,10 +1,21 @@ import { Session } from "./Session"; import { SessionProperties } from "./SessionProperties"; +import { Archive } from "./Archive"; export declare class OpenVidu { private urlOpenViduServer; - private secret; + private static readonly API_RECORDINGS; + private static readonly API_RECORDINGS_START; + private static readonly API_RECORDINGS_STOP; + private hostname; + private port; + private basicAuth; constructor(urlOpenViduServer: string, secret: string); createSession(properties?: SessionProperties): Session; - startArchive(sessionId: string): void; - stopArchive(sessionId: string): void; + startRecording(sessionId: string): Promise; + stopRecording(recordingId: string): Promise; + getRecording(recordingId: string): Promise; + listRecordings(): Promise; + deleteRecording(recordingId: string): Promise; + private getBasicAuth(secret); + private setHostnameAndPort(); } diff --git a/openvidu-node-client/lib/OpenVidu.js b/openvidu-node-client/lib/OpenVidu.js index 6fe6f0aa..6abb7b67 100644 --- a/openvidu-node-client/lib/OpenVidu.js +++ b/openvidu-node-client/lib/OpenVidu.js @@ -1,20 +1,231 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Session_1 = require("./Session"); +var Archive_1 = require("./Archive"); +var https = require('https'); var OpenVidu = /** @class */ (function () { function OpenVidu(urlOpenViduServer, secret) { this.urlOpenViduServer = urlOpenViduServer; - this.secret = secret; + this.setHostnameAndPort(); + this.basicAuth = this.getBasicAuth(secret); } OpenVidu.prototype.createSession = function (properties) { - return new Session_1.Session(this.urlOpenViduServer, this.secret, properties); + return new Session_1.Session(this.hostname, this.port, this.basicAuth, properties); }; - OpenVidu.prototype.startArchive = function (sessionId) { - // TODO: REST POST to start recording in OpenVidu Server + OpenVidu.prototype.startRecording = function (sessionId) { + var _this = this; + return new Promise(function (resolve, reject) { + var requestBody = JSON.stringify({ + 'session': sessionId + }); + var options = { + hostname: _this.hostname, + port: _this.port, + path: OpenVidu.API_RECORDINGS + OpenVidu.API_RECORDINGS_START, + method: 'POST', + headers: { + 'Authorization': _this.basicAuth, + '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 () { + if (res.statusCode === 200) { + // SUCCESS response from openvidu-server (Archive in JSON format). Resolve new Archive + resolve(new Archive_1.Archive(JSON.parse(body))); + } + else { + // ERROR response from openvidu-server. Resolve HTTP status + reject(new Error(res.statusCode)); + } + }); + }); + req.on('error', function (e) { + reject(new Error(e)); + }); + req.write(requestBody); + req.end(); + }); }; - OpenVidu.prototype.stopArchive = function (sessionId) { - // TODO: REST POST to end recording in OpenVidu Server + OpenVidu.prototype.stopRecording = function (recordingId) { + var _this = this; + return new Promise(function (resolve, reject) { + var options = { + hostname: _this.hostname, + port: _this.port, + path: OpenVidu.API_RECORDINGS + OpenVidu.API_RECORDINGS_STOP + '/' + recordingId, + method: 'POST', + headers: { + 'Authorization': _this.basicAuth, + 'Content-Type': 'application/x-www-form-urlencoded' + } + }; + 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 () { + if (res.statusCode === 200) { + // SUCCESS response from openvidu-server (Archive in JSON format). Resolve new Archive + resolve(new Archive_1.Archive(JSON.parse(body))); + } + else { + // ERROR response from openvidu-server. Resolve HTTP status + reject(new Error(res.statusCode)); + } + }); + }); + req.on('error', function (e) { + reject(new Error(e)); + }); + //req.write(); + req.end(); + }); }; + OpenVidu.prototype.getRecording = function (recordingId) { + var _this = this; + return new Promise(function (resolve, reject) { + var options = { + hostname: _this.hostname, + port: _this.port, + path: OpenVidu.API_RECORDINGS + '/' + recordingId, + method: 'GET', + headers: { + 'Authorization': _this.basicAuth, + 'Content-Type': 'application/x-www-form-urlencoded' + } + }; + 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 () { + if (res.statusCode === 200) { + // SUCCESS response from openvidu-server (Archive in JSON format). Resolve new Archive + resolve(new Archive_1.Archive(JSON.parse(body))); + } + else { + // ERROR response from openvidu-server. Resolve HTTP status + reject(new Error(res.statusCode)); + } + }); + }); + req.on('error', function (e) { + reject(new Error(e)); + }); + //req.write(); + req.end(); + }); + }; + OpenVidu.prototype.listRecordings = function () { + var _this = this; + return new Promise(function (resolve, reject) { + var options = { + hostname: _this.hostname, + port: _this.port, + path: OpenVidu.API_RECORDINGS, + method: 'GET', + headers: { + 'Authorization': _this.basicAuth, + 'Content-Type': 'application/x-www-form-urlencoded' + } + }; + 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 () { + if (res.statusCode === 200) { + // SUCCESS response from openvidu-server (JSON arrays of Archives in JSON format). Resolve list of new Archives + var archiveArray = []; + var responseItems = JSON.parse(body)['items']; + for (var i = 0; i < responseItems.length; i++) { + archiveArray.push(new Archive_1.Archive(responseItems[i])); + } + resolve(archiveArray); + } + else { + // ERROR response from openvidu-server. Resolve HTTP status + reject(new Error(res.statusCode)); + } + }); + }); + req.on('error', function (e) { + reject(new Error(e)); + }); + //req.write(); + req.end(); + }); + }; + OpenVidu.prototype.deleteRecording = function (recordingId) { + var _this = this; + return new Promise(function (resolve, reject) { + var options = { + hostname: _this.hostname, + port: _this.port, + path: OpenVidu.API_RECORDINGS + '/' + recordingId, + method: 'DELETE', + headers: { + 'Authorization': _this.basicAuth, + 'Content-Type': 'application/x-www-form-urlencoded' + } + }; + 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 () { + if (res.statusCode === 204) { + // SUCCESS response from openvidu-server. Resolve undefined + resolve(undefined); + } + else { + // ERROR response from openvidu-server. Resolve HTTP status + reject(new Error(res.statusCode)); + } + }); + }); + req.on('error', function (e) { + reject(new Error(e)); + }); + //req.write(); + req.end(); + }); + }; + OpenVidu.prototype.getBasicAuth = function (secret) { + return 'Basic ' + (new Buffer('OPENVIDUAPP:' + secret).toString('base64')); + }; + OpenVidu.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 + "')"); + } + }; + OpenVidu.API_RECORDINGS = '/api/recordings'; + OpenVidu.API_RECORDINGS_START = '/start'; + OpenVidu.API_RECORDINGS_STOP = '/stop'; return OpenVidu; }()); exports.OpenVidu = OpenVidu; diff --git a/openvidu-node-client/lib/OpenVidu.js.map b/openvidu-node-client/lib/OpenVidu.js.map index d1bd3c2c..b3440988 100644 --- a/openvidu-node-client/lib/OpenVidu.js.map +++ b/openvidu-node-client/lib/OpenVidu.js.map @@ -1 +1 @@ -{"version":3,"file":"OpenVidu.js","sourceRoot":"","sources":["../src/OpenVidu.ts"],"names":[],"mappings":";;AAAA,qCAAoC;AAGpC;IAEE,kBAAoB,iBAAyB,EAAU,MAAc;QAAjD,sBAAiB,GAAjB,iBAAiB,CAAQ;QAAU,WAAM,GAAN,MAAM,CAAQ;IAAI,CAAC;IAEnE,gCAAa,GAApB,UAAqB,UAA8B;QACjD,MAAM,CAAC,IAAI,iBAAO,CAAC,IAAI,CAAC,iBAAiB,EAAE,IAAI,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACtE,CAAC;IAEM,+BAAY,GAAnB,UAAoB,SAAiB;QACnC,wDAAwD;IAC1D,CAAC;IAEM,8BAAW,GAAlB,UAAmB,SAAiB;QAClC,sDAAsD;IACxD,CAAC;IAEH,eAAC;AAAD,C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Q,CAAC;IACxC,4BAAmB,GAAW,OAAO,CAAC;IA6OhE,eAAC;CAAA,AAjPD,IAiPC;AAjPY,4BAAQ"} \ No newline at end of file diff --git a/openvidu-node-client/lib/Session.d.ts b/openvidu-node-client/lib/Session.d.ts index 24330ac9..4494ea68 100644 --- a/openvidu-node-client/lib/Session.d.ts +++ b/openvidu-node-client/lib/Session.d.ts @@ -1,19 +1,16 @@ import { TokenOptions } from './TokenOptions'; import { SessionProperties } from './SessionProperties'; export declare class Session { - private urlOpenViduServer; - private secret; - private sessionIdURL; - private tokenURL; - private sessionId; - private properties; private hostname; private port; - constructor(urlOpenViduServer: string, secret: string, properties?: SessionProperties); + private basicAuth; + private static readonly API_SESSIONS; + private static readonly API_TOKENS; + private sessionId; + private properties; + constructor(hostname: string, port: number, basicAuth: string, properties?: SessionProperties); getSessionId(callback: Function): void; generateToken(callback: Function): any; generateToken(tokenOptions: TokenOptions, callback: Function): any; getProperties(): SessionProperties; - private getBasicAuth(); - private setHostnameAndPort(); } diff --git a/openvidu-node-client/lib/Session.js b/openvidu-node-client/lib/Session.js index 7146d2fc..7873eec9 100644 --- a/openvidu-node-client/lib/Session.js +++ b/openvidu-node-client/lib/Session.js @@ -4,11 +4,10 @@ var OpenViduRole_1 = require("./OpenViduRole"); var SessionProperties_1 = require("./SessionProperties"); var https = require('https'); var Session = /** @class */ (function () { - function Session(urlOpenViduServer, secret, properties) { - this.urlOpenViduServer = urlOpenViduServer; - this.secret = secret; - this.sessionIdURL = '/api/sessions'; - this.tokenURL = '/api/tokens'; + function Session(hostname, port, basicAuth, properties) { + this.hostname = hostname; + this.port = port; + this.basicAuth = basicAuth; this.sessionId = ""; if (properties == null) { this.properties = new SessionProperties_1.SessionProperties.Builder().build(); @@ -16,7 +15,6 @@ var Session = /** @class */ (function () { else { this.properties = properties; } - this.setHostnameAndPort(); } Session.prototype.getSessionId = function (callback) { var _this = this; @@ -32,10 +30,10 @@ var Session = /** @class */ (function () { var options = { hostname: this.hostname, port: this.port, - path: this.sessionIdURL, + path: Session.API_SESSIONS, method: 'POST', headers: { - 'Authorization': this.getBasicAuth(), + 'Authorization': this.basicAuth, 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(requestBody) } @@ -47,10 +45,16 @@ var Session = /** @class */ (function () { body += d; }); res.on('end', function () { - // Data reception is done - var parsed = JSON.parse(body); - _this.sessionId = parsed.id; - callback(parsed.id); + if (res.statusCode === 200) { + // SUCCESS response from openvidu-server. Resolve sessionId + var parsed = JSON.parse(body); + _this.sessionId = parsed.id; + callback(parsed.id); + } + else { + // ERROR response from openvidu-server. Resolve HTTP status + console.error(res.statusCode); + } }); }); req.on('error', function (e) { @@ -79,10 +83,10 @@ var Session = /** @class */ (function () { var options = { hostname: this.hostname, port: this.port, - path: this.tokenURL, + path: Session.API_TOKENS, method: 'POST', headers: { - 'Authorization': this.getBasicAuth(), + 'Authorization': this.basicAuth, 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(requestBody) } @@ -94,9 +98,15 @@ var Session = /** @class */ (function () { body += d; }); res.on('end', function () { - // Data reception is done - var parsed = JSON.parse(body); - callback(parsed.id); + if (res.statusCode === 200) { + // SUCCESS response from openvidu-server. Resolve token + var parsed = JSON.parse(body); + callback(parsed.id); + } + else { + // ERROR response from openvidu-server. Resolve HTTP status + console.error(res.statusCode); + } }); }); req.on('error', function (e) { @@ -108,23 +118,8 @@ var Session = /** @class */ (function () { Session.prototype.getProperties = function () { return this.properties; }; - 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 + "')"); - } - }; + Session.API_SESSIONS = '/api/sessions'; + Session.API_TOKENS = '/api/tokens'; return Session; }()); exports.Session = Session; diff --git a/openvidu-node-client/lib/Session.js.map b/openvidu-node-client/lib/Session.js.map index d693eb67..6cb8dc1d 100644 --- a/openvidu-node-client/lib/Session.js.map +++ b/openvidu-node-client/lib/Session.js.map @@ -1 +1 @@ -{"version":3,"file":"Session.js","sourceRoot":"","sources":["../src/Session.ts"],"names":[],"mappings":";;AACA,+CAA8C;AAC9C,yDAAwD;AAKxD,IAAI,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAE7B;IASI,iBAAoB,iBAAyB,EAAU,MAAc,EAAE,UAA8B;QAAjF,sBAAiB,GAAjB,iBAAiB,CAAQ;QAAU,WAAM,GAAN,MAAM,CAAQ;QAP7D,iBAAY,GAAW,eAAe,CAAC;QACvC,aAAQ,GAAW,aAAa,CAAC;QACjC,cAAS,GAAW,EAAE,CAAC;QAM3B,EAAE,CAAC,CAAC,UAAU,IAAI,IAAI,CAAC,CAAC,CAAC;YACrB,IAAI,CAAC,UAAU,GAAG,IAAI,qCAAiB,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC;QAC9D,CAAC;QAAC,IAAI,CAAC,CAAC;YACJ,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QACjC,CAAC;QACD,IAAI,CAAC,kBAAkB,EAAE,CAAC;IAC9B,CAAC;IAEM,8BAAY,GAAnB,UAAoB,QAAkB;QAAtC,iBA2CC;QAzCG,EAAE,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;YACjB,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YACzB,MAAM,CAAC;QACX,CAAC;QAED,IAAI,WAAW,GAAG,IAAI,CAAC,SAAS,CAAC;YAC7B,eAAe,EAAE,IAA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AI,CAAC,CAAC;YACd,CAAC,CAAC,CAAC;YACH,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE;gBACV,EAAE,CAAC,CAAC,GAAG,CAAC,UAAU,KAAK,GAAG,CAAC,CAAC,CAAC;oBACzB,uDAAuD;oBACvD,IAAI,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;oBAC9B,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;gBACxB,CAAC;gBAAC,IAAI,CAAC,CAAC;oBACJ,2DAA2D;oBAC3D,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;gBAClC,CAAC;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;QAEH,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,UAAC,CAAC;YACd,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACrB,CAAC,CAAC,CAAC;QACH,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QACvB,GAAG,CAAC,GAAG,EAAE,CAAC;IACd,CAAC;IAEM,+BAAa,GAApB;QACI,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC;IAC3B,CAAC;IA3HuB,oBAAY,GAAW,eAAe,CAAC;IACvC,kBAAU,GAAW,aAAa,CAAC;IA4H/D,cAAC;CAAA,AA/HD,IA+HC;AA/HY,0BAAO"} \ No newline at end of file diff --git a/openvidu-node-client/lib/TokenOptions.js b/openvidu-node-client/lib/TokenOptions.js index bdbb9d56..3ea8a635 100644 --- a/openvidu-node-client/lib/TokenOptions.js +++ b/openvidu-node-client/lib/TokenOptions.js @@ -1,5 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); +var OpenViduRole_1 = require("./OpenViduRole"); var TokenOptions = /** @class */ (function () { function TokenOptions(data, role) { this.data = data; @@ -17,6 +18,8 @@ exports.TokenOptions = TokenOptions; (function (TokenOptions) { var Builder = /** @class */ (function () { function Builder() { + this.dataProp = ''; + this.roleProp = OpenViduRole_1.OpenViduRole.PUBLISHER; } Builder.prototype.build = function () { return new TokenOptions(this.dataProp, this.roleProp); diff --git a/openvidu-node-client/lib/TokenOptions.js.map b/openvidu-node-client/lib/TokenOptions.js.map index f74a5c0f..6960a271 100644 --- a/openvidu-node-client/lib/TokenOptions.js.map +++ b/openvidu-node-client/lib/TokenOptions.js.map @@ -1 +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;IAAI,CAAC;IAEjE,8BAAO,GAAP;QACI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IAED,8BAAO,GAAP;QACI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IACL,mBAAC;AAAD,CAAC,AAXD,IAWC;AAXY,oCAAY;AAazB,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;AAlCY,oCAAY"} \ No newline at end of file +{"version":3,"file":"TokenOptions.js","sourceRoot":"","sources":["../src/TokenOptions.ts"],"names":[],"mappings":";;AAAA,+CAA8C;AAE9C;IAEI,sBAAoB,IAAY,EAAU,IAAkB;QAAxC,SAAI,GAAJ,IAAI,CAAQ;QAAU,SAAI,GAAJ,IAAI,CAAc;IAAI,CAAC;IAEjE,8BAAO,GAAP;QACI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IAED,8BAAO,GAAP;QACI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;IACrB,CAAC;IACL,mBAAC;AAAD,CAAC,AAXD,IAWC;AAXY,oCAAY;AAazB,WAAiB,YAAY;IACzB;QAAA;YAEY,aAAQ,GAAW,EAAE,CAAC;YACtB,aAAQ,GAAiB,2BAAY,CAAC,SAAS,CAAC;QAgB5D,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;AAlCY,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 index eb195f07..c00244e2 100644 --- a/openvidu-node-client/lib/index.d.ts +++ b/openvidu-node-client/lib/index.d.ts @@ -1,8 +1,9 @@ export * from './OpenVidu'; export * from './OpenViduRole'; export * from './Session'; +export * from './SessionProperties'; export * from './TokenOptions'; export * from './MediaMode'; export * from './ArchiveLayout'; export * from './ArchiveMode'; -export * from './SessionProperties'; +export * from './Archive'; diff --git a/openvidu-node-client/lib/index.js b/openvidu-node-client/lib/index.js index f98bdce0..754f12e6 100644 --- a/openvidu-node-client/lib/index.js +++ b/openvidu-node-client/lib/index.js @@ -6,9 +6,10 @@ Object.defineProperty(exports, "__esModule", { value: true }); __export(require("./OpenVidu")); __export(require("./OpenViduRole")); __export(require("./Session")); +__export(require("./SessionProperties")); __export(require("./TokenOptions")); __export(require("./MediaMode")); __export(require("./ArchiveLayout")); __export(require("./ArchiveMode")); -__export(require("./SessionProperties")); +__export(require("./Archive")); //# 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 index 39de48b5..596dd115 100644 --- a/openvidu-node-client/lib/index.js.map +++ b/openvidu-node-client/lib/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,gCAA2B;AAC3B,oCAA+B;AAC/B,+BAA0B;AAC1B,oCAA+B;AAC/B,iCAA4B;AAC5B,qCAAgC;AAChC,mCAA8B;AAC9B,yCAAoC"} \ No newline at end of file +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AAAA,gCAA2B;AAC3B,oCAA+B;AAC/B,+BAA0B;AAC1B,yCAAoC;AACpC,oCAA+B;AAC/B,iCAA4B;AAC5B,qCAAgC;AAChC,mCAA8B;AAC9B,+BAA0B"} \ No newline at end of file diff --git a/openvidu-node-client/package.json b/openvidu-node-client/package.json index 873c1edf..93d71f41 100644 --- a/openvidu-node-client/package.json +++ b/openvidu-node-client/package.json @@ -16,9 +16,8 @@ "dependencies": {}, "devDependencies": { "@types/node": "9.4.0", - "codelyzer": "4.1.0", "ts-node": "4.1.0", "tslint": "5.9.1", "typescript": "2.6.2" } -} +} \ No newline at end of file diff --git a/openvidu-node-client/src/Archive.ts b/openvidu-node-client/src/Archive.ts new file mode 100644 index 00000000..804c17ff --- /dev/null +++ b/openvidu-node-client/src/Archive.ts @@ -0,0 +1,77 @@ +export class Archive { + + private id: string; + private name: string; + private sessionId: string; + private createdAt: number; + private size: number = 0; + private duration: number = 0; + private url: string; + private hasaudio: boolean = true; + private hasvideo: boolean = true; + private status: Archive.Status; + + constructor(json: JSON) { + this.id = json['id']; + this.name = json['name']; + this.sessionId = json['sessionId']; + this.createdAt = json['createdAt']; + this.size = json['size']; + this.duration = json['duration']; + this.url = json['url']; + this.hasaudio = json['hasAudio']; + this.hasvideo = json['hasVideo']; + this.status = json['status']; + } + + public getStatus(): Archive.Status { + return this.status; + } + + public getId(): string { + return this.id; + } + + public getName(): string { + return this.name; + } + + public getSessionId(): string { + return this.sessionId; + } + + public getCreatedAt(): number { + return this.createdAt; + } + + public getSize(): number { + return this.size; + } + + public getDuration(): number { + return this.duration; + } + + public getUrl(): string { + return this.url; + } + + public hasAudio(): boolean { + return this.hasaudio; + } + + public hasVideo(): boolean { + return this.hasvideo; + } +} + +export namespace Archive { + export enum Status { + starting, // The recording is starting (cannot be stopped) + started, // The recording has started and is going on + stopped, // The recording has finished OK + available, // The recording is available for downloading. This status is reached for all + // stopped recordings if property 'openvidu.recording.free-access' is true + failed // The recording has failed + } +} \ No newline at end of file diff --git a/openvidu-node-client/src/OpenVidu.ts b/openvidu-node-client/src/OpenVidu.ts index 1134eeae..a450a046 100644 --- a/openvidu-node-client/src/OpenVidu.ts +++ b/openvidu-node-client/src/OpenVidu.ts @@ -1,20 +1,249 @@ import { Session } from "./Session"; import { SessionProperties } from "./SessionProperties"; +import { Archive } from "./Archive"; + +declare const Buffer; +let https = require('https'); export class OpenVidu { - constructor(private urlOpenViduServer: string, private secret: string) { } + private static readonly API_RECORDINGS: string = '/api/recordings'; + private static readonly API_RECORDINGS_START: string = '/start'; + private static readonly API_RECORDINGS_STOP: string = '/stop'; + + private hostname: string; + private port: number; + private basicAuth: string; + + constructor(private urlOpenViduServer: string, secret: string) { + this.setHostnameAndPort(); + this.basicAuth = this.getBasicAuth(secret); + } public createSession(properties?: SessionProperties): Session { - return new Session(this.urlOpenViduServer, this.secret, properties); + return new Session(this.hostname, this.port, this.basicAuth, properties); } - public startRecording(sessionId: string) { - // TODO: REST POST to start recording in OpenVidu Server + public startRecording(sessionId: string): Promise { + return new Promise((resolve, reject) => { + + let requestBody = JSON.stringify({ + 'session': sessionId + }); + + let options = { + hostname: this.hostname, + port: this.port, + path: OpenVidu.API_RECORDINGS + OpenVidu.API_RECORDINGS_START, + method: 'POST', + headers: { + 'Authorization': this.basicAuth, + 'Content-Type': 'application/json', + 'Content-Length': Buffer.byteLength(requestBody) + } + } + const req = https.request(options, (res) => { + let body = ''; + res.on('data', (d) => { + // Continuously update stream with data + body += d; + }); + res.on('end', () => { + if (res.statusCode === 200) { + // SUCCESS response from openvidu-server (Archive in JSON format). Resolve new Archive + resolve(new Archive(JSON.parse(body))); + } else { + // ERROR response from openvidu-server. Resolve HTTP status + reject(new Error(res.statusCode)); + } + }); + }); + + req.on('error', (e) => { + reject(new Error(e)); + }); + req.write(requestBody); + req.end(); + + }); } - public stopRecording(sessionId: string) { - // TODO: REST POST to end recording in OpenVidu Server + public stopRecording(recordingId: string): Promise { + return new Promise((resolve, reject) => { + + let options = { + hostname: this.hostname, + port: this.port, + path: OpenVidu.API_RECORDINGS + OpenVidu.API_RECORDINGS_STOP + '/' + recordingId, + method: 'POST', + headers: { + 'Authorization': this.basicAuth, + 'Content-Type': 'application/x-www-form-urlencoded' + } + } + const req = https.request(options, (res) => { + let body = ''; + res.on('data', (d) => { + // Continuously update stream with data + body += d; + }); + res.on('end', () => { + if (res.statusCode === 200) { + // SUCCESS response from openvidu-server (Archive in JSON format). Resolve new Archive + resolve(new Archive(JSON.parse(body))); + } else { + // ERROR response from openvidu-server. Resolve HTTP status + reject(new Error(res.statusCode)); + } + }); + }); + + req.on('error', (e) => { + reject(new Error(e)); + }); + //req.write(); + req.end(); + + }); + } + + public getRecording(recordingId: string): Promise { + return new Promise((resolve, reject) => { + + let options = { + hostname: this.hostname, + port: this.port, + path: OpenVidu.API_RECORDINGS + '/' + recordingId, + method: 'GET', + headers: { + 'Authorization': this.basicAuth, + 'Content-Type': 'application/x-www-form-urlencoded' + } + } + const req = https.request(options, (res) => { + let body = ''; + res.on('data', (d) => { + // Continuously update stream with data + body += d; + }); + res.on('end', () => { + if (res.statusCode === 200) { + // SUCCESS response from openvidu-server (Archive in JSON format). Resolve new Archive + resolve(new Archive(JSON.parse(body))); + } else { + // ERROR response from openvidu-server. Resolve HTTP status + reject(new Error(res.statusCode)); + } + }); + }); + + req.on('error', (e) => { + reject(new Error(e)); + }); + //req.write(); + req.end(); + + }); + } + + public listRecordings(): Promise { + return new Promise((resolve, reject) => { + + let options = { + hostname: this.hostname, + port: this.port, + path: OpenVidu.API_RECORDINGS, + method: 'GET', + headers: { + 'Authorization': this.basicAuth, + 'Content-Type': 'application/x-www-form-urlencoded' + } + } + const req = https.request(options, (res) => { + let body = ''; + res.on('data', (d) => { + // Continuously update stream with data + body += d; + }); + res.on('end', () => { + if (res.statusCode === 200) { + // SUCCESS response from openvidu-server (JSON arrays of Archives in JSON format). Resolve list of new Archives + let archiveArray: Archive[] = []; + let responseItems = JSON.parse(body)['items']; + for (let i = 0; i < responseItems.length; i++) { + archiveArray.push(new Archive(responseItems[i])); + } + resolve(archiveArray); + } else { + // ERROR response from openvidu-server. Resolve HTTP status + reject(new Error(res.statusCode)); + } + }); + }); + + req.on('error', (e) => { + reject(new Error(e)); + }); + //req.write(); + req.end(); + + }); + } + + public deleteRecording(recordingId: string): Promise { + return new Promise((resolve, reject) => { + + let options = { + hostname: this.hostname, + port: this.port, + path: OpenVidu.API_RECORDINGS + '/' + recordingId, + method: 'DELETE', + headers: { + 'Authorization': this.basicAuth, + 'Content-Type': 'application/x-www-form-urlencoded' + } + } + const req = https.request(options, (res) => { + let body = ''; + res.on('data', (d) => { + // Continuously update stream with data + body += d; + }); + res.on('end', () => { + if (res.statusCode === 204) { + // SUCCESS response from openvidu-server. Resolve undefined + resolve(undefined); + } else { + // ERROR response from openvidu-server. Resolve HTTP status + reject(new Error(res.statusCode)); + } + }); + }); + + req.on('error', (e) => { + reject(new Error(e)); + }); + //req.write(); + req.end(); + + }); + } + + private getBasicAuth(secret: string): string { + return 'Basic ' + (new Buffer('OPENVIDUAPP:' + secret).toString('base64')); + } + + private setHostnameAndPort(): void { + let 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/Session.ts b/openvidu-node-client/src/Session.ts index 495c9a98..5ddbe3fd 100644 --- a/openvidu-node-client/src/Session.ts +++ b/openvidu-node-client/src/Session.ts @@ -9,20 +9,18 @@ let https = require('https'); export class Session { - private sessionIdURL: string = '/api/sessions'; - private tokenURL: string = '/api/tokens'; + private static readonly API_SESSIONS: string = '/api/sessions'; + private static readonly API_TOKENS: string = '/api/tokens'; + private sessionId: string = ""; private properties: SessionProperties; - private hostname: string; - private port: number; - constructor(private urlOpenViduServer: string, private secret: string, properties?: SessionProperties) { + constructor(private hostname: string, private port: number, private basicAuth: string, properties?: SessionProperties) { if (properties == null) { this.properties = new SessionProperties.Builder().build(); } else { this.properties = properties; } - this.setHostnameAndPort(); } public getSessionId(callback: Function) { @@ -41,10 +39,10 @@ export class Session { let options = { hostname: this.hostname, port: this.port, - path: this.sessionIdURL, + path: Session.API_SESSIONS, method: 'POST', headers: { - 'Authorization': this.getBasicAuth(), + 'Authorization': this.basicAuth, 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(requestBody) } @@ -56,10 +54,15 @@ export class Session { body += d; }); res.on('end', () => { - // Data reception is done - let parsed = JSON.parse(body); - this.sessionId = parsed.id; - callback(parsed.id); + if (res.statusCode === 200) { + // SUCCESS response from openvidu-server. Resolve sessionId + let parsed = JSON.parse(body); + this.sessionId = parsed.id; + callback(parsed.id); + } else { + // ERROR response from openvidu-server. Resolve HTTP status + console.error(res.statusCode); + } }); }); @@ -94,10 +97,10 @@ export class Session { let options = { hostname: this.hostname, port: this.port, - path: this.tokenURL, + path: Session.API_TOKENS, method: 'POST', headers: { - 'Authorization': this.getBasicAuth(), + 'Authorization': this.basicAuth, 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(requestBody) } @@ -109,9 +112,14 @@ export class Session { body += d; }); res.on('end', () => { - // Data reception is done - let parsed = JSON.parse(body); - callback(parsed.id); + if (res.statusCode === 200) { + // SUCCESS response from openvidu-server. Resolve token + let parsed = JSON.parse(body); + callback(parsed.id); + } else { + // ERROR response from openvidu-server. Resolve HTTP status + console.error(res.statusCode); + } }); }); @@ -123,24 +131,7 @@ export class Session { } public getProperties(): SessionProperties { - return this.properties; - } - - private getBasicAuth() { - return 'Basic ' + (new Buffer('OPENVIDUAPP:' + this.secret).toString('base64')); - } - - private setHostnameAndPort() { - let 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 + "')"); - } + return this.properties; } } \ No newline at end of file diff --git a/openvidu-node-client/src/TokenOptions.ts b/openvidu-node-client/src/TokenOptions.ts index fc74a6de..274bff9b 100644 --- a/openvidu-node-client/src/TokenOptions.ts +++ b/openvidu-node-client/src/TokenOptions.ts @@ -16,8 +16,8 @@ export class TokenOptions { export namespace TokenOptions { export class Builder { - private dataProp: string; - private roleProp: OpenViduRole; + private dataProp: string = ''; + private roleProp: OpenViduRole = OpenViduRole.PUBLISHER; build(): TokenOptions { return new TokenOptions(this.dataProp, this.roleProp); diff --git a/openvidu-node-client/src/index.ts b/openvidu-node-client/src/index.ts index 61fe4c5b..da5f1d6d 100644 --- a/openvidu-node-client/src/index.ts +++ b/openvidu-node-client/src/index.ts @@ -1,8 +1,9 @@ export * from './OpenVidu'; export * from './OpenViduRole'; export * from './Session'; +export * from './SessionProperties'; export * from './TokenOptions'; export * from './MediaMode'; export * from './ArchiveLayout'; export * from './ArchiveMode'; -export * from './SessionProperties'; \ No newline at end of file +export * from './Archive'; \ No newline at end of file