util.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. var util = {
  2. noop: function() {},
  3. // Logging logic
  4. logLevel: 0,
  5. setLogLevel: function(level) {
  6. if (level = parseInt(level, 10)) {
  7. util.logLevel = level;
  8. } else {
  9. // If they are using truthy/falsy values for debug
  10. util.logLevel = (!!level) ? 3 : 0;
  11. }
  12. util.log = util.warn = util.error = util.noop;
  13. if (util.logLevel > 0) {
  14. util.error = util._printWith('ERROR');
  15. }
  16. if (util.logLevel > 1) {
  17. util.warn = util._printWith('WARNING');
  18. }
  19. if (util.logLevel > 2) {
  20. util.log = util._print;
  21. }
  22. },
  23. setLogFunction: function(fn) {
  24. if (fn.constructor !== Function) {
  25. util.warn('The log function you passed in is not a function. Defaulting to regular logs.');
  26. } else {
  27. util._print = fn;
  28. }
  29. },
  30. _printWith: function(prefix) {
  31. return function() {}
  32. var copy = Array.prototype.slice.call(arguments);
  33. copy.unshift(prefix);
  34. util._print.apply(util, copy);
  35. };
  36. },
  37. _print: function () {
  38. var err = false;
  39. var copy = Array.prototype.slice.call(arguments);
  40. copy.unshift('PeerJS: ');
  41. for (var i = 0, l = copy.length; i < l; i++){
  42. if (copy[i] instanceof Error) {
  43. copy[i] = '(' + copy[i].name + ') ' + copy[i].message;
  44. err = true;
  45. }
  46. }
  47. err ? console.error.apply(console, copy) : console.log.apply(console, copy);
  48. },
  49. //
  50. // Lists which features are supported
  51. // Temporarily set everything to true
  52. supports: (function(){
  53. return {
  54. audioVideo: true,
  55. data: true,
  56. binaryData: true,
  57. reliableData: true
  58. };
  59. }()),
  60. //
  61. // Ensure alphanumeric ids
  62. validateId: function(id) {
  63. // Allow empty ids
  64. return !id || /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(id);
  65. },
  66. validateKey: function(key) {
  67. // Allow empty keys
  68. return !key || /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(key);
  69. },
  70. // OLD
  71. chromeCompatible: true,
  72. firefoxCompatible: true,
  73. chromeVersion: 26,
  74. firefoxVersion: 22,
  75. debug: false,
  76. browserisms: '',
  77. inherits: function(ctor, superCtor) {
  78. ctor.super_ = superCtor;
  79. ctor.prototype = Object.create(superCtor.prototype, {
  80. constructor: {
  81. value: ctor,
  82. enumerable: false,
  83. writable: true,
  84. configurable: true
  85. }
  86. });
  87. },
  88. extend: function(dest, source) {
  89. for(var key in source) {
  90. if(source.hasOwnProperty(key)) {
  91. dest[key] = source[key];
  92. }
  93. }
  94. return dest;
  95. },
  96. pack: BinaryPack.pack,
  97. unpack: BinaryPack.unpack,
  98. log: function () {
  99. if (util.debug) {
  100. var err = false;
  101. var copy = Array.prototype.slice.call(arguments);
  102. copy.unshift('PeerJS: ');
  103. for (var i = 0, l = copy.length; i < l; i++){
  104. if (copy[i] instanceof Error) {
  105. copy[i] = '(' + copy[i].name + ') ' + copy[i].message;
  106. err = true;
  107. }
  108. }
  109. err ? console.error.apply(console, copy) : console.log.apply(console, copy);
  110. }
  111. },
  112. setZeroTimeout: (function(global) {
  113. var timeouts = [];
  114. var messageName = 'zero-timeout-message';
  115. // Like setTimeout, but only takes a function argument. There's
  116. // no time argument (always zero) and no arguments (you have to
  117. // use a closure).
  118. function setZeroTimeoutPostMessage(fn) {
  119. timeouts.push(fn);
  120. global.postMessage(messageName, '*');
  121. }
  122. function handleMessage(event) {
  123. if (event.source == global && event.data == messageName) {
  124. if (event.stopPropagation) {
  125. event.stopPropagation();
  126. }
  127. if (timeouts.length) {
  128. timeouts.shift()();
  129. }
  130. }
  131. }
  132. if (global.addEventListener) {
  133. global.addEventListener('message', handleMessage, true);
  134. } else if (global.attachEvent) {
  135. global.attachEvent('onmessage', handleMessage);
  136. }
  137. return setZeroTimeoutPostMessage;
  138. }(this)),
  139. blobToArrayBuffer: function(blob, cb){
  140. var fr = new FileReader();
  141. fr.onload = function(evt) {
  142. cb(evt.target.result);
  143. };
  144. fr.readAsArrayBuffer(blob);
  145. },
  146. blobToBinaryString: function(blob, cb){
  147. var fr = new FileReader();
  148. fr.onload = function(evt) {
  149. cb(evt.target.result);
  150. };
  151. fr.readAsBinaryString(blob);
  152. },
  153. binaryStringToArrayBuffer: function(binary) {
  154. var byteArray = new Uint8Array(binary.length);
  155. for (var i = 0; i < binary.length; i++) {
  156. byteArray[i] = binary.charCodeAt(i) & 0xff;
  157. }
  158. return byteArray.buffer;
  159. },
  160. randomToken: function () {
  161. return Math.random().toString(36).substr(2);
  162. },
  163. // When we have proper version/feature mappings we can remove this
  164. isBrowserCompatible: function() {
  165. var c, f;
  166. if (this.chromeCompatible) {
  167. if ((c = navigator.userAgent.split('Chrome/')) && c.length > 1) {
  168. // Get version #.
  169. var v = c[1].split('.')[0];
  170. return parseInt(v) >= this.chromeVersion;
  171. }
  172. }
  173. if (this.firefoxCompatible) {
  174. if ((f = navigator.userAgent.split('Firefox/')) && f.length > 1) {
  175. // Get version #.
  176. var v = f[1].split('.')[0];
  177. return parseInt(v) >= this.firefoxVersion;
  178. }
  179. }
  180. return false;
  181. },
  182. isSecure: function() {
  183. return location.protocol === 'https:';
  184. }
  185. };