123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- var util = {
- noop: function() {},
-
- // Logging logic
- logLevel: 0,
- setLogLevel: function(level) {
- if (level = parseInt(level, 10)) {
- util.logLevel = level;
- } else {
- // If they are using truthy/falsy values for debug
- util.logLevel = (!!level) ? 3 : 0;
- }
- util.log = util.warn = util.error = util.noop;
- if (util.logLevel > 0) {
- util.error = util._printWith('ERROR');
- }
- if (util.logLevel > 1) {
- util.warn = util._printWith('WARNING');
- }
- if (util.logLevel > 2) {
- util.log = util._print;
- }
- },
- setLogFunction: function(fn) {
- if (fn.constructor !== Function) {
- util.warn('The log function you passed in is not a function. Defaulting to regular logs.');
- } else {
- util._print = fn;
- }
- },
- _printWith: function(prefix) {
- return function() {}
- var copy = Array.prototype.slice.call(arguments);
- copy.unshift(prefix);
- util._print.apply(util, copy);
- };
- },
- _print: function () {
- var err = false;
- var copy = Array.prototype.slice.call(arguments);
- copy.unshift('PeerJS: ');
- for (var i = 0, l = copy.length; i < l; i++){
- if (copy[i] instanceof Error) {
- copy[i] = '(' + copy[i].name + ') ' + copy[i].message;
- err = true;
- }
- }
- err ? console.error.apply(console, copy) : console.log.apply(console, copy);
- },
- //
-
- // Lists which features are supported
- // Temporarily set everything to true
- supports: (function(){
- return {
- audioVideo: true,
- data: true,
- binaryData: true,
- reliableData: true
- };
- }()),
- //
-
- // Ensure alphanumeric ids
- validateId: function(id) {
- // Allow empty ids
- return !id || /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(id);
- },
-
- validateKey: function(key) {
- // Allow empty keys
- return !key || /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(key);
- },
-
-
- // OLD
- chromeCompatible: true,
- firefoxCompatible: true,
- chromeVersion: 26,
- firefoxVersion: 22,
- debug: false,
- browserisms: '',
- inherits: function(ctor, superCtor) {
- ctor.super_ = superCtor;
- ctor.prototype = Object.create(superCtor.prototype, {
- constructor: {
- value: ctor,
- enumerable: false,
- writable: true,
- configurable: true
- }
- });
- },
- extend: function(dest, source) {
- for(var key in source) {
- if(source.hasOwnProperty(key)) {
- dest[key] = source[key];
- }
- }
- return dest;
- },
- pack: BinaryPack.pack,
- unpack: BinaryPack.unpack,
- log: function () {
- if (util.debug) {
- var err = false;
- var copy = Array.prototype.slice.call(arguments);
- copy.unshift('PeerJS: ');
- for (var i = 0, l = copy.length; i < l; i++){
- if (copy[i] instanceof Error) {
- copy[i] = '(' + copy[i].name + ') ' + copy[i].message;
- err = true;
- }
- }
- err ? console.error.apply(console, copy) : console.log.apply(console, copy);
- }
- },
- setZeroTimeout: (function(global) {
- var timeouts = [];
- var messageName = 'zero-timeout-message';
- // Like setTimeout, but only takes a function argument. There's
- // no time argument (always zero) and no arguments (you have to
- // use a closure).
- function setZeroTimeoutPostMessage(fn) {
- timeouts.push(fn);
- global.postMessage(messageName, '*');
- }
- function handleMessage(event) {
- if (event.source == global && event.data == messageName) {
- if (event.stopPropagation) {
- event.stopPropagation();
- }
- if (timeouts.length) {
- timeouts.shift()();
- }
- }
- }
- if (global.addEventListener) {
- global.addEventListener('message', handleMessage, true);
- } else if (global.attachEvent) {
- global.attachEvent('onmessage', handleMessage);
- }
- return setZeroTimeoutPostMessage;
- }(this)),
- blobToArrayBuffer: function(blob, cb){
- var fr = new FileReader();
- fr.onload = function(evt) {
- cb(evt.target.result);
- };
- fr.readAsArrayBuffer(blob);
- },
- blobToBinaryString: function(blob, cb){
- var fr = new FileReader();
- fr.onload = function(evt) {
- cb(evt.target.result);
- };
- fr.readAsBinaryString(blob);
- },
- binaryStringToArrayBuffer: function(binary) {
- var byteArray = new Uint8Array(binary.length);
- for (var i = 0; i < binary.length; i++) {
- byteArray[i] = binary.charCodeAt(i) & 0xff;
- }
- return byteArray.buffer;
- },
- randomToken: function () {
- return Math.random().toString(36).substr(2);
- },
-
- // When we have proper version/feature mappings we can remove this
- isBrowserCompatible: function() {
- var c, f;
- if (this.chromeCompatible) {
- if ((c = navigator.userAgent.split('Chrome/')) && c.length > 1) {
- // Get version #.
- var v = c[1].split('.')[0];
- return parseInt(v) >= this.chromeVersion;
- }
- }
- if (this.firefoxCompatible) {
- if ((f = navigator.userAgent.split('Firefox/')) && f.length > 1) {
- // Get version #.
- var v = f[1].split('.')[0];
- return parseInt(v) >= this.firefoxVersion;
- }
- }
- return false;
- },
-
-
- isSecure: function() {
- return location.protocol === 'https:';
- }
- };
|