util.js 5.3 KB

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