openvidu-browser: beautify .js files

pull/621/head
pabloFuente 2021-03-22 12:14:38 +01:00
parent ae686ffbb6
commit 317b388088
7 changed files with 240 additions and 325 deletions

View File

@ -1,12 +1,9 @@
function Mapper() function Mapper() {
{
var sources = {}; var sources = {};
this.forEach = function(callback) this.forEach = function (callback) {
{ for (var key in sources) {
for(var key in sources)
{
var source = sources[key]; var source = sources[key];
for (var key2 in source) for (var key2 in source)
@ -14,8 +11,7 @@ function Mapper()
}; };
}; };
this.get = function(id, source) this.get = function (id, source) {
{
var ids = sources[source]; var ids = sources[source];
if (ids == undefined) if (ids == undefined)
return undefined; return undefined;
@ -23,8 +19,7 @@ function Mapper()
return ids[id]; return ids[id];
}; };
this.remove = function(id, source) this.remove = function (id, source) {
{
var ids = sources[source]; var ids = sources[source];
if (ids == undefined) if (ids == undefined)
return; return;
@ -32,13 +27,14 @@ function Mapper()
delete ids[id]; delete ids[id];
// Check it's empty // Check it's empty
for(var i in ids){return false} for (var i in ids) {
return false
}
delete sources[source]; delete sources[source];
}; };
this.set = function(value, id, source) this.set = function (value, id, source) {
{
if (value == undefined) if (value == undefined)
return this.remove(id, source); return this.remove(id, source);
@ -51,8 +47,7 @@ function Mapper()
}; };
Mapper.prototype.pop = function(id, source) Mapper.prototype.pop = function (id, source) {
{
var value = this.get(id, source); var value = this.get(id, source);
if (value == undefined) if (value == undefined)
return undefined; return undefined;

View File

@ -17,5 +17,4 @@
var WebSocketWithReconnection = require('./webSocketWithReconnection'); var WebSocketWithReconnection = require('./webSocketWithReconnection');
exports.WebSocketWithReconnection = WebSocketWithReconnection; exports.WebSocketWithReconnection = WebSocketWithReconnection;

View File

@ -17,14 +17,10 @@
var defineProperty_IE8 = false var defineProperty_IE8 = false
if(Object.defineProperty) if (Object.defineProperty) {
{ try {
try
{
Object.defineProperty({}, "x", {}); Object.defineProperty({}, "x", {});
} } catch (e) {
catch(e)
{
defineProperty_IE8 = true defineProperty_IE8 = true
} }
} }
@ -42,9 +38,9 @@ if (!Function.prototype.bind) {
fToBind = this, fToBind = this,
fNOP = function () {}, fNOP = function () {},
fBound = function () { fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis return fToBind.apply(this instanceof fNOP && oThis ?
? this this :
: oThis, oThis,
aArgs.concat(Array.prototype.slice.call(arguments))); aArgs.concat(Array.prototype.slice.call(arguments)));
}; };
@ -67,17 +63,14 @@ var Mapper = require('./Mapper');
var BASE_TIMEOUT = 5000; var BASE_TIMEOUT = 5000;
function unifyResponseMethods(responseMethods) function unifyResponseMethods(responseMethods) {
{
if (!responseMethods) return {}; if (!responseMethods) return {};
for(var key in responseMethods) for (var key in responseMethods) {
{
var value = responseMethods[key]; var value = responseMethods[key];
if (typeof value == 'string') if (typeof value == 'string')
responseMethods[key] = responseMethods[key] = {
{
response: value response: value
} }
}; };
@ -85,28 +78,27 @@ function unifyResponseMethods(responseMethods)
return responseMethods; return responseMethods;
}; };
function unifyTransport(transport) function unifyTransport(transport) {
{
if (!transport) return; if (!transport) return;
// Transport as a function // Transport as a function
if (transport instanceof Function) if (transport instanceof Function)
return {send: transport}; return {
send: transport
};
// WebSocket & DataChannel // WebSocket & DataChannel
if (transport.send instanceof Function) if (transport.send instanceof Function)
return transport; return transport;
// Message API (Inter-window & WebWorker) // Message API (Inter-window & WebWorker)
if(transport.postMessage instanceof Function) if (transport.postMessage instanceof Function) {
{
transport.send = transport.postMessage; transport.send = transport.postMessage;
return transport; return transport;
} }
// Stream API // Stream API
if(transport.write instanceof Function) if (transport.write instanceof Function) {
{
transport.send = transport.write; transport.send = transport.write;
return transport; return transport;
} }
@ -129,17 +121,19 @@ function unifyTransport(transport)
* @param {String} method -method of the notification * @param {String} method -method of the notification
* @param params - parameters of the notification * @param params - parameters of the notification
*/ */
function RpcNotification(method, params) function RpcNotification(method, params) {
{ if (defineProperty_IE8) {
if(defineProperty_IE8)
{
this.method = method this.method = method
this.params = params this.params = params
} } else {
else Object.defineProperty(this, 'method', {
{ value: method,
Object.defineProperty(this, 'method', {value: method, enumerable: true}); enumerable: true
Object.defineProperty(this, 'params', {value: params, enumerable: true}); });
Object.defineProperty(this, 'params', {
value: params,
enumerable: true
});
} }
}; };
@ -157,8 +151,7 @@ function RpcNotification(method, params)
* *
* @param {Function} [onRequest] * @param {Function} [onRequest]
*/ */
function RpcBuilder(packer, options, transport, onRequest) function RpcBuilder(packer, options, transport, onRequest) {
{
var self = this; var self = this;
if (!packer) if (!packer)
@ -170,8 +163,7 @@ function RpcBuilder(packer, options, transport, onRequest)
var responseMethods = unifyResponseMethods(packer.responseMethods); var responseMethods = unifyResponseMethods(packer.responseMethods);
if(options instanceof Function) if (options instanceof Function) {
{
if (transport != undefined) if (transport != undefined)
throw new SyntaxError("There can't be parameters after onRequest"); throw new SyntaxError("There can't be parameters after onRequest");
@ -180,8 +172,7 @@ function RpcBuilder(packer, options, transport, onRequest)
options = undefined; options = undefined;
}; };
if(options && options.send instanceof Function) if (options && options.send instanceof Function) {
{
if (transport && !(transport instanceof Function)) if (transport && !(transport instanceof Function))
throw new SyntaxError("Only a function can be after transport"); throw new SyntaxError("Only a function can be after transport");
@ -190,8 +181,7 @@ function RpcBuilder(packer, options, transport, onRequest)
options = undefined; options = undefined;
}; };
if(transport instanceof Function) if (transport instanceof Function) {
{
if (onRequest != undefined) if (onRequest != undefined)
throw new SyntaxError("There can't be parameters after onRequest"); throw new SyntaxError("There can't be parameters after onRequest");
@ -215,25 +205,23 @@ function RpcBuilder(packer, options, transport, onRequest)
if (defineProperty_IE8) if (defineProperty_IE8)
this.peerID = options.peerID this.peerID = options.peerID
else else
Object.defineProperty(this, 'peerID', {value: options.peerID}); Object.defineProperty(this, 'peerID', {
value: options.peerID
});
var max_retries = options.max_retries || 0; var max_retries = options.max_retries || 0;
function transportMessage(event) function transportMessage(event) {
{
self.decode(event.data || event); self.decode(event.data || event);
}; };
this.getTransport = function() this.getTransport = function () {
{
return transport; return transport;
} }
this.setTransport = function(value) this.setTransport = function (value) {
{
// Remove listener from old transport // Remove listener from old transport
if(transport) if (transport) {
{
// W3C transports // W3C transports
if (transport.removeEventListener) if (transport.removeEventListener)
transport.removeEventListener('message', transportMessage); transport.removeEventListener('message', transportMessage);
@ -244,8 +232,7 @@ function RpcBuilder(packer, options, transport, onRequest)
}; };
// Set listener on new transport // Set listener on new transport
if(value) if (value) {
{
// W3C transports // W3C transports
if (value.addEventListener) if (value.addEventListener)
value.addEventListener('message', transportMessage); value.addEventListener('message', transportMessage);
@ -259,8 +246,7 @@ function RpcBuilder(packer, options, transport, onRequest)
} }
if (!defineProperty_IE8) if (!defineProperty_IE8)
Object.defineProperty(this, 'transport', Object.defineProperty(this, 'transport', {
{
get: this.getTransport.bind(this), get: this.getTransport.bind(this),
set: this.setTransport.bind(this) set: this.setTransport.bind(this)
}) })
@ -286,14 +272,11 @@ function RpcBuilder(packer, options, transport, onRequest)
/** /**
* Store the response to prevent to process duplicate request later * Store the response to prevent to process duplicate request later
*/ */
function storeResponse(message, id, dest) function storeResponse(message, id, dest) {
{ var response = {
var response =
{
message: message, message: message,
/** Timeout to auto-clean old responses */ /** Timeout to auto-clean old responses */
timeout: setTimeout(function() timeout: setTimeout(function () {
{
responses.remove(id, dest); responses.remove(id, dest);
}, },
response_timeout) response_timeout)
@ -305,10 +288,8 @@ function RpcBuilder(packer, options, transport, onRequest)
/** /**
* Store the response to ignore duplicated messages later * Store the response to ignore duplicated messages later
*/ */
function storeProcessedResponse(ack, from) function storeProcessedResponse(ack, from) {
{ var timeout = setTimeout(function () {
var timeout = setTimeout(function()
{
processedResponses.remove(ack, from); processedResponses.remove(ack, from);
}, },
duplicates_timeout); duplicates_timeout);
@ -330,22 +311,18 @@ function RpcBuilder(packer, options, transport, onRequest)
* @param {Integer} id - identifier of the request * @param {Integer} id - identifier of the request
* @param [from] - source of the notification * @param [from] - source of the notification
*/ */
function RpcRequest(method, params, id, from, transport) function RpcRequest(method, params, id, from, transport) {
{
RpcNotification.call(this, method, params); RpcNotification.call(this, method, params);
this.getTransport = function() this.getTransport = function () {
{
return transport; return transport;
} }
this.setTransport = function(value) this.setTransport = function (value) {
{
transport = unifyTransport(value); transport = unifyTransport(value);
} }
if (!defineProperty_IE8) if (!defineProperty_IE8)
Object.defineProperty(this, 'transport', Object.defineProperty(this, 'transport', {
{
get: this.getTransport.bind(this), get: this.getTransport.bind(this),
set: this.setTransport.bind(this) set: this.setTransport.bind(this)
}) })
@ -355,13 +332,11 @@ function RpcBuilder(packer, options, transport, onRequest)
/** /**
* @constant {Boolean} duplicated * @constant {Boolean} duplicated
*/ */
if(!(transport || self.getTransport())) if (!(transport || self.getTransport())) {
{
if (defineProperty_IE8) if (defineProperty_IE8)
this.duplicated = Boolean(response) this.duplicated = Boolean(response)
else else
Object.defineProperty(this, 'duplicated', Object.defineProperty(this, 'duplicated', {
{
value: Boolean(response) value: Boolean(response)
}); });
} }
@ -378,22 +353,17 @@ function RpcBuilder(packer, options, transport, onRequest)
* *
* @returns {string} * @returns {string}
*/ */
this.reply = function(error, result, transport) this.reply = function (error, result, transport) {
{
// Fix optional parameters // Fix optional parameters
if(error instanceof Function || error && error.send instanceof Function) if (error instanceof Function || error && error.send instanceof Function) {
{
if (result != undefined) if (result != undefined)
throw new SyntaxError("There can't be parameters after callback"); throw new SyntaxError("There can't be parameters after callback");
transport = error; transport = error;
result = null; result = null;
error = undefined; error = undefined;
} } else if (result instanceof Function ||
result && result.send instanceof Function) {
else if(result instanceof Function
|| result && result.send instanceof Function)
{
if (transport != undefined) if (transport != undefined)
throw new SyntaxError("There can't be parameters after callback"); throw new SyntaxError("There can't be parameters after callback");
@ -407,8 +377,7 @@ function RpcBuilder(packer, options, transport, onRequest)
if (response) if (response)
clearTimeout(response.timeout); clearTimeout(response.timeout);
if(from != undefined) if (from != undefined) {
{
if (error) if (error)
error.dest = from; error.dest = from;
@ -419,10 +388,8 @@ function RpcBuilder(packer, options, transport, onRequest)
var message; var message;
// New request or overriden one, create new response with provided data // New request or overriden one, create new response with provided data
if(error || result != undefined) if (error || result != undefined) {
{ if (self.peerID != undefined) {
if(self.peerID != undefined)
{
if (error) if (error)
error.from = self.peerID; error.from = self.peerID;
else else
@ -430,30 +397,24 @@ function RpcBuilder(packer, options, transport, onRequest)
} }
// Protocol indicates that responses has own request methods // Protocol indicates that responses has own request methods
if(responseMethod) if (responseMethod) {
{
if (responseMethod.error == undefined && error) if (responseMethod.error == undefined && error)
message = message = {
{
error: error error: error
}; };
else else {
{ var method = error ?
var method = error responseMethod.error :
? responseMethod.error responseMethod.response;
: responseMethod.response;
message = message = {
{
method: method, method: method,
params: error || result params: error || result
}; };
} }
} } else
else message = {
message =
{
error: error, error: error,
result: result result: result
}; };
@ -467,7 +428,9 @@ function RpcBuilder(packer, options, transport, onRequest)
// New empty reply, response null value // New empty reply, response null value
else else
message = packer.pack({result: null}, id); message = packer.pack({
result: null
}, id);
// Store the response to prevent to process a duplicated request later // Store the response to prevent to process a duplicated request later
storeResponse(message, id, from); storeResponse(message, id, from);
@ -484,8 +447,7 @@ function RpcBuilder(packer, options, transport, onRequest)
inherits(RpcRequest, RpcNotification); inherits(RpcRequest, RpcNotification);
function cancel(message) function cancel(message) {
{
var key = message2Key[message]; var key = message2Key[message];
if (!key) return; if (!key) return;
@ -505,8 +467,7 @@ function RpcBuilder(packer, options, transport, onRequest)
* *
* If `message` is not given, cancel all the request * If `message` is not given, cancel all the request
*/ */
this.cancel = function(message) this.cancel = function (message) {
{
if (message) return cancel(message); if (message) return cancel(message);
for (var message in message2Key) for (var message in message2Key)
@ -514,8 +475,7 @@ function RpcBuilder(packer, options, transport, onRequest)
}; };
this.close = function() this.close = function () {
{
// Prevent to receive new messages // Prevent to receive new messages
var transport = this.getTransport(); var transport = this.getTransport();
if (transport && transport.close) if (transport && transport.close)
@ -527,8 +487,7 @@ function RpcBuilder(packer, options, transport, onRequest)
processedResponses.forEach(clearTimeout); processedResponses.forEach(clearTimeout);
// Responses // Responses
responses.forEach(function(response) responses.forEach(function (response) {
{
clearTimeout(response.timeout); clearTimeout(response.timeout);
}); });
}; };
@ -546,11 +505,9 @@ function RpcBuilder(packer, options, transport, onRequest)
* *
* @returns {string} A raw JsonRPC 2.0 request or notification string * @returns {string} A raw JsonRPC 2.0 request or notification string
*/ */
this.encode = function(method, params, dest, transport, callback) this.encode = function (method, params, dest, transport, callback) {
{
// Fix optional parameters // Fix optional parameters
if(params instanceof Function) if (params instanceof Function) {
{
if (dest != undefined) if (dest != undefined)
throw new SyntaxError("There can't be parameters after callback"); throw new SyntaxError("There can't be parameters after callback");
@ -558,20 +515,14 @@ function RpcBuilder(packer, options, transport, onRequest)
transport = undefined; transport = undefined;
dest = undefined; dest = undefined;
params = undefined; params = undefined;
} } else if (dest instanceof Function) {
else if(dest instanceof Function)
{
if (transport != undefined) if (transport != undefined)
throw new SyntaxError("There can't be parameters after callback"); throw new SyntaxError("There can't be parameters after callback");
callback = dest; callback = dest;
transport = undefined; transport = undefined;
dest = undefined; dest = undefined;
} } else if (transport instanceof Function) {
else if(transport instanceof Function)
{
if (callback != undefined) if (callback != undefined)
throw new SyntaxError("There can't be parameters after callback"); throw new SyntaxError("There can't be parameters after callback");
@ -579,43 +530,37 @@ function RpcBuilder(packer, options, transport, onRequest)
transport = undefined; transport = undefined;
}; };
if(self.peerID != undefined) if (self.peerID != undefined) {
{
params = params || {}; params = params || {};
params.from = self.peerID; params.from = self.peerID;
}; };
if(dest != undefined) if (dest != undefined) {
{
params = params || {}; params = params || {};
params.dest = dest; params.dest = dest;
}; };
// Encode message // Encode message
var message = var message = {
{
method: method, method: method,
params: params params: params
}; };
if(callback) if (callback) {
{
var id = requestID++; var id = requestID++;
var retried = 0; var retried = 0;
message = packer.pack(message, id); message = packer.pack(message, id);
function dispatchCallback(error, result) function dispatchCallback(error, result) {
{
self.cancel(message); self.cancel(message);
callback(error, result); callback(error, result);
}; };
var request = var request = {
{
message: message, message: message,
callback: dispatchCallback, callback: dispatchCallback,
responseMethods: responseMethods[method] || {} responseMethods: responseMethods[method] || {}
@ -623,11 +568,13 @@ function RpcBuilder(packer, options, transport, onRequest)
var encode_transport = unifyTransport(transport); var encode_transport = unifyTransport(transport);
function sendRequest(transport) function sendRequest(transport) {
{
var rt = (method === 'ping' ? ping_request_timeout : request_timeout); var rt = (method === 'ping' ? ping_request_timeout : request_timeout);
request.timeout = setTimeout(timeout, rt * Math.pow(2, retried++)); request.timeout = setTimeout(timeout, rt * Math.pow(2, retried++));
message2Key[message] = {id: id, dest: dest}; message2Key[message] = {
id: id,
dest: dest
};
requests.set(request, id, dest); requests.set(request, id, dest);
transport = transport || encode_transport || self.getTransport(); transport = transport || encode_transport || self.getTransport();
@ -637,8 +584,7 @@ function RpcBuilder(packer, options, transport, onRequest)
return message; return message;
}; };
function retry(transport) function retry(transport) {
{
transport = unifyTransport(transport); transport = unifyTransport(transport);
console.warn(retried + ' retry for request message:', message); console.warn(retried + ' retry for request message:', message);
@ -649,8 +595,7 @@ function RpcBuilder(packer, options, transport, onRequest)
return sendRequest(transport); return sendRequest(transport);
}; };
function timeout() function timeout() {
{
if (retried < max_retries) if (retried < max_retries)
return retry(transport); return retry(transport);
@ -686,17 +631,13 @@ function RpcBuilder(packer, options, transport, onRequest)
* *
* @throws {TypeError} - Message is not defined * @throws {TypeError} - Message is not defined
*/ */
this.decode = function(message, transport) this.decode = function (message, transport) {
{
if (!message) if (!message)
throw new TypeError("Message is not defined"); throw new TypeError("Message is not defined");
try try {
{
message = packer.unpack(message); message = packer.unpack(message);
} } catch (e) {
catch(e)
{
// Ignore invalid messages // Ignore invalid messages
return console.debug(e, message); return console.debug(e, message);
}; };
@ -713,8 +654,7 @@ function RpcBuilder(packer, options, transport, onRequest)
if (self.peerID != undefined && from == self.peerID) return; if (self.peerID != undefined && from == self.peerID) return;
// Notification // Notification
if(id == undefined && ack == undefined) if (id == undefined && ack == undefined) {
{
var notification = new RpcNotification(method, params); var notification = new RpcNotification(method, params);
if (self.emit('request', notification)) return; if (self.emit('request', notification)) return;
@ -722,12 +662,10 @@ function RpcBuilder(packer, options, transport, onRequest)
}; };
function processRequest() function processRequest() {
{
// If we have a transport and it's a duplicated request, reply inmediatly // If we have a transport and it's a duplicated request, reply inmediatly
transport = unifyTransport(transport) || self.getTransport(); transport = unifyTransport(transport) || self.getTransport();
if(transport) if (transport) {
{
var response = responses.get(id, from); var response = responses.get(id, from);
if (response) if (response)
return transport.send(response.message); return transport.send(response.message);
@ -740,13 +678,11 @@ function RpcBuilder(packer, options, transport, onRequest)
return request; return request;
}; };
function processResponse(request, error, result) function processResponse(request, error, result) {
{
request.callback(error, result); request.callback(error, result);
}; };
function duplicatedResponse(timeout) function duplicatedResponse(timeout) {
{
console.warn("Response already processed", message); console.warn("Response already processed", message);
// Update duplicated responses timeout // Update duplicated responses timeout
@ -756,14 +692,11 @@ function RpcBuilder(packer, options, transport, onRequest)
// Request, or response with own method // Request, or response with own method
if(method) if (method) {
{
// Check if it's a response with own method // Check if it's a response with own method
if(dest == undefined || dest == self.peerID) if (dest == undefined || dest == self.peerID) {
{
var request = requests.get(ack, from); var request = requests.get(ack, from);
if(request) if (request) {
{
var responseMethods = request.responseMethods; var responseMethods = request.responseMethods;
if (method == responseMethods.error) if (method == responseMethods.error)
@ -793,8 +726,7 @@ function RpcBuilder(packer, options, transport, onRequest)
// Response // Response
var request = requests.get(ack, from); var request = requests.get(ack, from);
if(!request) if (!request) {
{
var processed = processedResponses.get(ack, from); var processed = processedResponses.get(ack, from);
if (processed) if (processed)
return duplicatedResponse(processed); return duplicatedResponse(processed);

View File

@ -10,16 +10,13 @@
* *
* @return {String} - the stringified JsonRPC 2.0 message * @return {String} - the stringified JsonRPC 2.0 message
*/ */
function pack(message, id) function pack(message, id) {
{ var result = {
var result =
{
jsonrpc: "2.0" jsonrpc: "2.0"
}; };
// Request // Request
if(message.method) if (message.method) {
{
result.method = message.method; result.method = message.method;
if (message.params) if (message.params)
@ -31,16 +28,13 @@ function pack(message, id)
} }
// Response // Response
else if(id != undefined) else if (id != undefined) {
{ if (message.error) {
if(message.error)
{
if (message.result !== undefined) if (message.result !== undefined)
throw new TypeError("Both result and error are defined"); throw new TypeError("Both result and error are defined");
result.error = message.error; result.error = message.error;
} } else if (message.result !== undefined)
else if(message.result !== undefined)
result.result = message.result; result.result = message.result;
else else
throw new TypeError("No result or error is defined"); throw new TypeError("No result or error is defined");
@ -60,8 +54,7 @@ function pack(message, id)
* *
* @return {Object} - object filled with the JsonRPC 2.0 message content * @return {Object} - object filled with the JsonRPC 2.0 message content
*/ */
function unpack(message) function unpack(message) {
{
var result = message; var result = message;
if (typeof message === 'string' || message instanceof String) { if (typeof message === 'string' || message instanceof String) {
@ -75,8 +68,7 @@ function unpack(message)
throw new TypeError("Invalid JsonRPC version '" + version + "': " + message); throw new TypeError("Invalid JsonRPC version '" + version + "': " + message);
// Response // Response
if(result.method == undefined) if (result.method == undefined) {
{
if (result.id == undefined) if (result.id == undefined)
throw new TypeError("Invalid message: " + message); throw new TypeError("Invalid message: " + message);

View File

@ -1,13 +1,10 @@
function pack(message) function pack(message) {
{
throw new TypeError("Not yet implemented"); throw new TypeError("Not yet implemented");
}; };
function unpack(message) function unpack(message) {
{
throw new TypeError("Not yet implemented"); throw new TypeError("Not yet implemented");
}; };
exports.pack = pack; exports.pack = pack;
exports.unpack = unpack; exports.unpack = unpack;