util.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. binary: false,
  57. reliable: false,
  58. onnegotiationneeded: true
  59. };
  60. }()),
  61. //
  62. // Ensure alphanumeric ids
  63. validateId: function(id) {
  64. // Allow empty ids
  65. return !id || /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(id);
  66. },
  67. validateKey: function(key) {
  68. // Allow empty keys
  69. return !key || /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(key);
  70. },
  71. // OLD
  72. chromeCompatible: true,
  73. firefoxCompatible: true,
  74. chromeVersion: 26,
  75. firefoxVersion: 22,
  76. debug: false,
  77. browserisms: '',
  78. inherits: function(ctor, superCtor) {
  79. ctor.super_ = superCtor;
  80. ctor.prototype = Object.create(superCtor.prototype, {
  81. constructor: {
  82. value: ctor,
  83. enumerable: false,
  84. writable: true,
  85. configurable: true
  86. }
  87. });
  88. },
  89. extend: function(dest, source) {
  90. for(var key in source) {
  91. if(source.hasOwnProperty(key)) {
  92. dest[key] = source[key];
  93. }
  94. }
  95. return dest;
  96. },
  97. pack: BinaryPack.pack,
  98. unpack: BinaryPack.unpack,
  99. log: function () {
  100. if (util.debug) {
  101. var err = false;
  102. var copy = Array.prototype.slice.call(arguments);
  103. copy.unshift('PeerJS: ');
  104. for (var i = 0, l = copy.length; i < l; i++){
  105. if (copy[i] instanceof Error) {
  106. copy[i] = '(' + copy[i].name + ') ' + copy[i].message;
  107. err = true;
  108. }
  109. }
  110. err ? console.error.apply(console, copy) : console.log.apply(console, copy);
  111. }
  112. },
  113. setZeroTimeout: (function(global) {
  114. var timeouts = [];
  115. var messageName = 'zero-timeout-message';
  116. // Like setTimeout, but only takes a function argument. There's
  117. // no time argument (always zero) and no arguments (you have to
  118. // use a closure).
  119. function setZeroTimeoutPostMessage(fn) {
  120. timeouts.push(fn);
  121. global.postMessage(messageName, '*');
  122. }
  123. function handleMessage(event) {
  124. if (event.source == global && event.data == messageName) {
  125. if (event.stopPropagation) {
  126. event.stopPropagation();
  127. }
  128. if (timeouts.length) {
  129. timeouts.shift()();
  130. }
  131. }
  132. }
  133. if (global.addEventListener) {
  134. global.addEventListener('message', handleMessage, true);
  135. } else if (global.attachEvent) {
  136. global.attachEvent('onmessage', handleMessage);
  137. }
  138. return setZeroTimeoutPostMessage;
  139. }(this)),
  140. // Binary stuff
  141. blobToArrayBuffer: function(blob, cb){
  142. var fr = new FileReader();
  143. fr.onload = function(evt) {
  144. cb(evt.target.result);
  145. };
  146. fr.readAsArrayBuffer(blob);
  147. },
  148. blobToBinaryString: function(blob, cb){
  149. var fr = new FileReader();
  150. fr.onload = function(evt) {
  151. cb(evt.target.result);
  152. };
  153. fr.readAsBinaryString(blob);
  154. },
  155. binaryStringToArrayBuffer: function(binary) {
  156. var byteArray = new Uint8Array(binary.length);
  157. for (var i = 0; i < binary.length; i++) {
  158. byteArray[i] = binary.charCodeAt(i) & 0xff;
  159. }
  160. return byteArray.buffer;
  161. },
  162. randomToken: function () {
  163. return Math.random().toString(36).substr(2);
  164. },
  165. //
  166. // When we have proper version/feature mappings we can remove this
  167. isBrowserCompatible: function() {
  168. var c, f;
  169. if (this.chromeCompatible) {
  170. if ((c = navigator.userAgent.split('Chrome/')) && c.length > 1) {
  171. // Get version #.
  172. var v = c[1].split('.')[0];
  173. return parseInt(v) >= this.chromeVersion;
  174. }
  175. }
  176. if (this.firefoxCompatible) {
  177. if ((f = navigator.userAgent.split('Firefox/')) && f.length > 1) {
  178. // Get version #.
  179. var v = f[1].split('.')[0];
  180. return parseInt(v) >= this.firefoxVersion;
  181. }
  182. }
  183. return false;
  184. },
  185. isSecure: function() {
  186. return location.protocol === 'https:';
  187. }
  188. };