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 = {};
this.forEach = function(callback)
{
for(var key in sources)
{
this.forEach = function (callback) {
for (var key in sources) {
var source = sources[key];
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];
if (ids == undefined)
return undefined;
@ -23,8 +19,7 @@ function Mapper()
return ids[id];
};
this.remove = function(id, source)
{
this.remove = function (id, source) {
var ids = sources[source];
if (ids == undefined)
return;
@ -32,13 +27,14 @@ function Mapper()
delete ids[id];
// Check it's empty
for(var i in ids){return false}
for (var i in ids) {
return false
}
delete sources[source];
};
this.set = function(value, id, source)
{
this.set = function (value, id, source) {
if (value == undefined)
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);
if (value == undefined)
return undefined;

View File

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

View File

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

View File

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

View File

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