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

opentok

Package Overview
Dependencies
Maintainers
4
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

opentok - npm Package Compare versions

Comparing version 2.17.0 to 2.17.1-0

589

lib/client.js

@@ -1,8 +0,8 @@

var request = require('needle');
var _ = require('lodash');
var generateJwt = require('./generateJwt');
var pkg = require('../package.json');
var Stream = require('./stream');
var Broadcast = require('./broadcast');
var defaultConfig = {
const _ = require('lodash');
const generateJwt = require('./generateJwt');
const pkg = require('../package.json');
const Stream = require('./stream');
const Broadcast = require('./broadcast');
const fetch = require('node-fetch');
const defaultConfig = {
apiKey: null,

@@ -42,3 +42,74 @@ apiSecret: null,

var Client = function (c) {
const api = ({
method,
url,
callback,
body,
form,
headers = {},
}) => {
let requestBody = null;
if (body && ['POST', 'PATCH', 'PUT'].includes(method)) {
headers['Content-Type'] = 'application/json';
requestBody = JSON.stringify(body);
}
if (form) {
requestBody = new URLSearchParams(form).toString();
headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
Promise.resolve(fetch(
url,
{
method: method,
body: requestBody,
headers: headers,
}
))
.then(async (response) => {
const bodyText = await response.text();
let body = bodyText;
switch (response.headers.get('content-type')) {
case 'application/x-www-form-urlencoded':
body = response.body
? new URLSearchParams(body)
: '' ;
break;
case 'application/json':
body = JSON.parse(bodyText);
}
if (response.status === 400) {
if (body?.code === 15204) {
return callback(new Error('SIP Interconnect for Video is not enabled in this project'));
}
return callback(new Error('Bad session ID, token, SIP credentials, or SIP URI (sip:user@domain.tld)'));
}
// handle client errors
if (response.status === 403) {
return callback(new Error('An authentication error occurred: ('
+ response.status
+ ') '
+ bodyText));
}
// handle server errors
if (response.status >= 500 && response.status <= 599) {
return callback(new Error('A server error occurred: ('
+ response.status
+ ') '
+ bodyText));
}
callback(null, body);
})
.catch(async (error) => {
callback(error);
});
};
const Client = function (c) {
this.c = {};

@@ -56,5 +127,2 @@ this.config(_.defaults(c, defaultConfig));

}
if ('request' in this.c) {
request.defaults(this.c.request);
}

@@ -65,39 +133,11 @@ return this.c;

Client.prototype.createSession = function (opts, cb) {
request.post(
this.c.apiUrl + this.c.endpoints.createSession,
opts,
{
// TODO: only works while apiUrl is always up to the domain, without the ending slash
headers: this.generateHeaders()
const url = new URL(this.c.apiUrl + this.c.endpoints.createSession);
},
function (err, resp, body) {
if (err) {
return cb(new Error('The request failed: ' + err));
}
// handle client errors
if (resp.statusCode === 403) {
return cb(new Error('An authentication error occurred: ('
+ resp.statusCode
+ ') '
+ JSON.stringify(body)));
}
// handle server errors
if (resp.statusCode >= 500 && resp.statusCode <= 599) {
return cb(new Error('A server error occurred: ('
+ resp.statusCode
+ ') '
+ JSON.stringify(body)));
}
// check if the returned object is valid JSON
if (typeof body !== 'object') {
return cb(new Error('Server returned invalid JSON'));
}
return cb(null, body);
}
);
api({
url: url.toString(),
method: 'POST',
form: opts,
headers: this.generateHeaders(),
callback: cb,
});
};

@@ -116,3 +156,3 @@

Client.prototype.playDTMF = function (opts, cb) {
var url;
let url;

@@ -133,25 +173,15 @@ if (opts.sessionId) {

request.post(
url,
{
api({
url:url,
method: 'POST',
body: {
digits: opts.digits
},
{
headers: this.generateHeaders()
},
function (err, resp, body) {
if (err) return cb(new Error('The request failed: ' + err));
if (resp.statusCode === 200) {
// Success
return cb(null);
}
return cb(new Error('(' + resp.statusCode + ') ' + JSON.stringify(body)));
}
);
headers: this.generateHeaders(),
callback: cb,
});
};
Client.prototype.forceMuteStream = function (opts, cb) {
var url = this.c.apiUrl
const url = this.c.apiUrl
+ this.c.endpoints.forceMuteStream

@@ -162,23 +192,13 @@ .replace(/<%apiKey%>/g, this.c.apiKey)

request.post(
url,
{},
{
headers: this.generateHeaders()
},
function (err, resp, body) {
if (err) return cb(new Error('The request failed: ' + err));
if (resp.statusCode === 200) {
// Success
return cb(null);
}
return cb(new Error('(' + resp.statusCode + ') ' + JSON.stringify(body)));
}
);
api({
url:url,
method: 'POST',
body: { },
headers: this.generateHeaders(),
callback: cb,
});
};
Client.prototype.forceMuteAll = function (opts, cb) {
var url = this.c.apiUrl
const url = this.c.apiUrl
+ this.c.endpoints.forceMute

@@ -190,23 +210,13 @@ .replace(/<%apiKey%>/g, this.c.apiKey)

request.post(
url,
opts.options,
{
headers: this.generateHeaders()
},
function (err, resp, body) {
if (err) return cb(new Error('The request failed: ' + err));
if (resp.statusCode === 200) {
// Success
return cb(null);
}
return cb(new Error('(' + resp.statusCode + ') ' + JSON.stringify(body)));
}
);
api({
url:url,
method: 'POST',
body: opts.options,
headers: this.generateHeaders(),
callback: cb,
});
};
Client.prototype.disableForceMute = function (opts, cb) {
var url = this.c.apiUrl
const url = this.c.apiUrl
+ this.c.endpoints.forceMute

@@ -216,33 +226,24 @@ .replace(/<%apiKey%>/g, this.c.apiKey)

var options = {
const options = {
active: false
};
request.post(
url,
options,
{
headers: this.generateHeaders()
},
function (err, resp, body) {
if (err) return cb(new Error('The request failed: ' + err));
if (resp.statusCode === 200) {
// Success
return cb(null);
}
return cb(new Error('(' + resp.statusCode + ') ' + JSON.stringify(body)));
}
);
api({
url:url,
method: 'POST',
body: options,
headers: this.generateHeaders(),
callback: cb,
});
};
Client.prototype.setArchiveLayout = function setArchiveLayout(opts, cb) {
var url = this.c.apiUrl
const url = this.c.apiUrl
+ this.c.endpoints.setArchiveLayout
.replace(/<%apiKey%>/g, this.c.apiKey)
.replace(/<%archiveId%>/g, opts.archiveId);
request.put(
url,
{
api({
url:url,
method: 'PUT',
body: {
type: opts.type,

@@ -252,144 +253,93 @@ stylesheet: opts.stylesheet || undefined,

},
{
headers: this.generateHeaders()
},
function requestCallback(err, resp, body) {
if (err) {
return cb(new Error('The request failed: ' + err));
}
if (resp.statusCode === 200) {
return cb(null, body);
}
return cb(new Error('(' + resp.statusCode + ') ' + body.message));
}
);
headers: this.generateHeaders(),
callback: cb,
});
};
Client.prototype.startBroadcast = function (opts, cb) {
var url = this.c.apiUrl
const url = this.c.apiUrl
+ this.c.endpoints.startBroadcast.replace(/<%apiKey%>/g, this.c.apiKey);
request.post(
url,
opts,
{
headers: this.generateHeaders()
api({
url:url,
method: 'POST',
body: opts,
headers: this.generateHeaders(),
callback: (err, json) => {
const responseText = typeof json === 'object' ? JSON.stringify(json) : json;
cb(err, responseText);
},
function (err, resp, body) {
if (err) return cb(new Error('The request failed: ' + err));
});
if (resp.statusCode !== 200) {
return cb(new Error('(' + resp.statusCode + ') ' + JSON.stringify(body)));
}
return cb(null, JSON.stringify(body));
}
);
};
Client.prototype.patchBroadcast = function patchBroadcast(broadcastId, opts, cb) {
var url = this.c.apiUrl + this.c.endpoints.patchBroadcast.replace(/<%apiKey%>/g, this.c.apiKey)
const url = this.c.apiUrl + this.c.endpoints.patchBroadcast.replace(/<%apiKey%>/g, this.c.apiKey)
.replace(/<%broadcastId%>/g, broadcastId);
api({
url:url,
method: 'PATCH',
body: opts,
headers: this.generateHeaders(),
callback: cb,
});
request.patch(
url,
opts,
{
headers: this.generateHeaders()
},
function (err, resp, body) {
if (err) return cb(new Error('The request failed'));
if (resp.statusCode === 204) {
return cb(null, JSON.stringify(body));
}
return cb(new Error('(' + resp.statusCode + ') ' + JSON.stringify(body)));
}
);
};
Client.prototype.stopBroadcast = function (broadcastId, cb) {
var url = this.c.apiUrl
const url = this.c.apiUrl
+ this.c.endpoints.stopBroadcast
.replace(/<%apiKey%>/g, this.c.apiKey)
.replace(/<%broadcastId%>/g, broadcastId);
request.post(
url,
{},
{
headers: this.generateHeaders()
},
function (err, resp, body) {
if (err) return cb(new Error('The request failed: ' + err));
if (resp.statusCode === 200) {
// Success
return cb(null, body);
}
return cb(new Error('(' + resp.statusCode + ') ' + JSON.parse(body).message));
}
);
api({
url:url,
method: 'POST',
body: { },
headers: this.generateHeaders(),
callback: cb,
});
};
Client.prototype.getBroadcast = function getBroadcast(broadcastId, cb) {
var url = this.c.apiUrl
const url = this.c.apiUrl
+ this.c.endpoints.getBroadcast
.replace(/<%apiKey%>/g, this.c.apiKey)
.replace(/<%broadcastId%>/g, broadcastId);
request.get(
url,
{
headers: this.generateHeaders()
},
function requestCallback(err, resp, body) {
if (err) {
return cb(new Error('The request failed: ' + err));
}
if (resp.statusCode === 200) {
return cb(null, body);
}
return cb(new Error('(' + resp.statusCode + ') ' + body ? body.message : ''));
}
);
api({
url:url,
method: 'GET',
headers: this.generateHeaders(),
callback: cb,
});
};
Client.prototype.listBroadcasts = function listBroadcasts(queryString, cb) {
var baseUrl = this.c.apiUrl
const baseUrl = this.c.apiUrl
+ this.c.endpoints.listBroadcasts.replace(/<%apiKey%>/g, this.c.apiKey);
var url = queryString.length > 0 ? baseUrl + '?' + queryString : baseUrl;
var parsedBody;
request.get(
url,
{
headers: this.generateHeaders()
const url = queryString.length > 0 ? baseUrl + '?' + queryString : baseUrl;
api({
url:url,
method: 'GET',
headers: this.generateHeaders(),
callback: (err, response ) => {
const items = response ? JSON.parse(response) : response;
cb(
err,
items?.items.map((item) => new Broadcast(Client, JSON.stringify(item))),
items?.count,
)
},
function requestCallback(err, resp, body) {
if (err) {
return cb(new Error('The request failed: ' + err));
}
if (resp.statusCode === 200) {
parsedBody = JSON.parse(body);
return cb(
null,
parsedBody.items.map(function itemIterator(item) {
return new Broadcast(Client, JSON.stringify(item));
}),
parsedBody.count
);
}
return cb(new Error('(' + resp.statusCode + ') ' + body ? body.message : ''));
}
);
});
};
Client.prototype.setBroadcastLayout = function setBroadcastLayout(opts, cb) {
var url = this.c.apiUrl
const url = this.c.apiUrl
+ this.c.endpoints.setBroadcastLayout
.replace(/<%apiKey%>/g, this.c.apiKey)
.replace(/<%broadcastId%>/g, opts.broadcastId);
request.put(
url,
{
api({
url:url,
method: 'PUT',
body: {
type: opts.type,

@@ -399,38 +349,19 @@ stylesheet: opts.stylesheet || undefined,

},
{
headers: this.generateHeaders()
},
function requestCallback(err, resp, body) {
if (err) {
return cb(new Error('The request failed: ' + err));
}
if (resp.statusCode === 200) {
return cb(null, body);
}
return cb(new Error('(' + resp.statusCode + ') ' + body.message));
}
);
headers: this.generateHeaders(),
callback: cb,
});
};
Client.prototype.websocketConnect = function websocketConnect(opts, cb) {
var url = this.c.apiUrl + this.c.endpoints.audioStreamer
const url = this.c.apiUrl + this.c.endpoints.audioStreamer
.replace(/<%apiKey%>/g, this.c.apiKey);
api({
url:url,
method: 'POST',
body: opts,
headers: this.generateHeaders(),
callback: cb,
});
request.post(
url,
opts,
{
headers: this.generateHeaders(),
json: true
},
function requestCallback(err, resp, body) {
if (err) {
return cb(new Error('The request failed: ' + err));
}
if (resp.statusCode === 200) {
return cb(null, body);
}
return cb(new Error('(' + resp.statusCode + ') ' + body.message));
}
);
};

@@ -443,80 +374,29 @@

) {
var url = this.c.apiUrl
const url = this.c.apiUrl
+ this.c.endpoints.setStreamClassLists
.replace(/<%apiKey%>/, this.c.apiKey)
.replace(/<%sessionId%>/g, sessionId);
request.put(
url,
{
api({
url:url,
method: 'PUT',
body: {
items: classListArray
},
{
headers: this.generateHeaders()
},
function requestCallback(err, resp, body) {
if (err) return cb(new Error('The request failed: ' + err));
// handle client errors
switch (resp.statusCode) {
case 200:
return cb(null, body);
case 400:
return cb(new Error('Invalid session ID or stream ID (400).'));
case 403:
return cb(new Error('Invalid API key or secret (403).'));
default:
if (resp.statusCode >= 500 && resp.statusCode <= 599) {
// handle server errors
return cb(new Error('A server error occurred: ('
+ resp.statusCode
+ ') '
+ JSON.stringify(body)));
}
}
return cb(new Error('An unexpected error occurred: ('
+ resp.statusCode
+ ') '
+ JSON.stringify(body)));
}
);
headers: this.generateHeaders(),
callback: cb,
});
};
Client.prototype.dial = function (opts, cb) {
request.post(
this.c.apiUrl + this.c.endpoints.dial,
opts,
{
// TODO: only works while apiUrl is always up to the domain, without the ending slash
headers: this.generateHeaders()
},
function (err, resp, body) {
if (err) return cb(new Error('The request failed: ' + err));
// handle client errors
if (resp.statusCode === 400) {
if (body.code === 15204) {
return cb(new Error('SIP Interconnect for Video is not enabled in this project'));
}
return cb(new Error('Bad session ID, token, SIP credentials, or SIP URI (sip:user@domain.tld)'));
}
if (resp.statusCode === 403) {
return cb(new Error('Invalid API key or secret'));
}
if (resp.statusCode === 409) {
return cb(new Error('Only Routed Sessions are allowed to initiate SIP Calls.'));
}
// handle server errors
if (resp.statusCode >= 500 && resp.statusCode <= 599) {
return cb(new Error('A server error occurred: (' + resp.statusCode + ') ' + body));
}
// Parse data from server
return cb(null, body);
}
);
api({
url: this.c.apiUrl + this.c.endpoints.dial,
method: 'POST',
body: opts,
headers: this.generateHeaders(),
callback: cb,
});
};
Client.prototype.getStream = function getStream(sessionId, streamId, cb) {
var url = this.c.apiUrl
const url = this.c.apiUrl
+ this.c.endpoints.getStream

@@ -526,44 +406,25 @@ .replace(/<%apiKey%>/g, this.c.apiKey)

.replace(/<%sessionId%>/g, sessionId);
request.get(
url,
{
headers: this.generateHeaders()
},
function requestCallback(err, resp, body) {
if (err) {
return cb(new Error('The request failed: ' + err));
}
if (resp.statusCode === 200) {
return cb(null, body);
}
return cb(new Error('(' + resp.statusCode + ') ' + body.message));
}
);
api({
url: url,
method: 'GET',
headers: this.generateHeaders(),
callback: cb,
});
};
Client.prototype.listStreams = function listStreams(sessionId, cb) {
var url = this.c.apiUrl
const url = this.c.apiUrl
+ this.c.endpoints.listStreams
.replace(/<%apiKey%>/g, this.c.apiKey)
.replace(/<%sessionId%>/g, sessionId);
request.get(
url,
{
headers: this.generateHeaders()
api({
url: url,
method: 'GET',
headers: this.generateHeaders(),
callback: (err, response) => {
const body = response ? JSON.parse(response) : response;
cb(err, body?.items.map((stream) => new Stream(JSON.stringify(stream))))
},
function requestCallback(err, resp, body) {
if (err) {
return cb(new Error('The request failed: ' + err));
}
if (resp.statusCode === 200) {
return cb(
null,
JSON.parse(body).items.map(function itemIterator(item) {
return new Stream(JSON.stringify(item));
})
);
}
return cb(new Error('(' + resp.statusCode + ') ' + body.message));
}
);
});
};

@@ -570,0 +431,0 @@

{
"$schema": "https://json.schemastore.org/package.json",
"name": "opentok",
"version": "2.17.1-0",
"description": "OpenTok server-side SDK",
"version": "2.17.0",
"homepage": "https://github.com/opentok/opentok-node",
"bugs": {
"url": "https://github.com/opentok/opentok-node/issues",
"email": "support@tokbox.com"
},
"repository": {

@@ -10,6 +15,2 @@ "type": "git",

},
"bugs": {
"url": "https://github.com/opentok/opentok-node/issues",
"email": "support@tokbox.com"
},
"license": "MIT",

@@ -59,2 +60,7 @@ "contributors": [

"url": "https://twitter.com/moficodes"
},
{
"name": "Chuck \"MANCHUCK\" Reeves",
"email": "chuck@manchuck.com",
"url": "https://github.com/manchuck"
}

@@ -67,15 +73,18 @@ ],

"scripts": {
"jasmine-coverage": "cross-env NODE_ENV=test nyc --reporter=text-lcov jasmine-node spec > jasmine.lcov",
"jasmine_node": "jasmine spec/opentok_spec.js",
"lint": "./node_modules/.bin/eslint ./lib/ ./test/ ./sample/",
"lint-fix": "eslint --fix lib test",
"mocha-coverage": "cross-env NODE_ENV=test nyc --reporter=text-lcov mocha > mocha.lcov",
"report-coverage": "npm run mocha-coverage && npm run jasmine-coverage",
"test": "npm run test-no-lint && npm run jasmine_node",
"test-coverage": "cross-env NODE_ENV=test nyc mocha",
"test-coverage-html": "cross-env NODE_ENV=test nyc --reporter html mocha",
"test-no-lint": "mocha ./test/*-test.js",
"lint": "./node_modules/.bin/eslint ./lib/ ./test/ ./sample/",
"lint-fix": "eslint --fix lib test",
"jasmine_node": "jasmine spec/opentok_spec.js",
"report-coverage": "npm run mocha-coverage && npm run jasmine-coverage",
"mocha-coverage": "cross-env NODE_ENV=test nyc --reporter=text-lcov mocha > mocha.lcov",
"jasmine-coverage": "cross-env NODE_ENV=test nyc --reporter=text-lcov jasmine-node spec > jasmine.lcov"
"test-no-lint": "mocha ./test/*-test.js"
},
"engines": {
"node": ">=4"
"dependencies": {
"jsonwebtoken": "9.0.2",
"lodash": "4.17.21",
"node-fetch": "2.7.0",
"opentok-token": "1.1.1"
},

@@ -94,9 +103,5 @@ "devDependencies": {

},
"dependencies": {
"jsonwebtoken": "9.0.2",
"lodash": "4.17.21",
"needle": "3.2.0",
"node-fetch": "2.7.0",
"opentok-token": "1.1.1"
"engines": {
"node": ">=4"
}
}
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