util.js 5.2 KB

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