util.js 5.0 KB

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