util.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var util = {
  2. chromeCompatible: true,
  3. firefoxCompatible: true,
  4. chromeVersion: 26,
  5. firefoxVersion: 22,
  6. debug: false,
  7. browserisms: '',
  8. inherits: function(ctor, superCtor) {
  9. ctor.super_ = superCtor;
  10. ctor.prototype = Object.create(superCtor.prototype, {
  11. constructor: {
  12. value: ctor,
  13. enumerable: false,
  14. writable: true,
  15. configurable: true
  16. }
  17. });
  18. },
  19. extend: function(dest, source) {
  20. for(var key in source) {
  21. if(source.hasOwnProperty(key)) {
  22. dest[key] = source[key];
  23. }
  24. }
  25. return dest;
  26. },
  27. pack: BinaryPack.pack,
  28. unpack: BinaryPack.unpack,
  29. log: function () {
  30. if (util.debug) {
  31. var err = false;
  32. var copy = Array.prototype.slice.call(arguments);
  33. copy.unshift('PeerJS: ');
  34. for (var i = 0, l = copy.length; i < l; i++){
  35. if (copy[i] instanceof Error) {
  36. copy[i] = '(' + copy[i].name + ') ' + copy[i].message;
  37. err = true;
  38. }
  39. }
  40. err ? console.error.apply(console, copy) : console.log.apply(console, copy);
  41. }
  42. },
  43. setZeroTimeout: (function(global) {
  44. var timeouts = [];
  45. var messageName = 'zero-timeout-message';
  46. // Like setTimeout, but only takes a function argument. There's
  47. // no time argument (always zero) and no arguments (you have to
  48. // use a closure).
  49. function setZeroTimeoutPostMessage(fn) {
  50. timeouts.push(fn);
  51. global.postMessage(messageName, '*');
  52. }
  53. function handleMessage(event) {
  54. if (event.source == global && event.data == messageName) {
  55. if (event.stopPropagation) {
  56. event.stopPropagation();
  57. }
  58. if (timeouts.length) {
  59. timeouts.shift()();
  60. }
  61. }
  62. }
  63. if (global.addEventListener) {
  64. global.addEventListener('message', handleMessage, true);
  65. } else if (global.attachEvent) {
  66. global.attachEvent('onmessage', handleMessage);
  67. }
  68. return setZeroTimeoutPostMessage;
  69. }(this)),
  70. blobToArrayBuffer: function(blob, cb){
  71. var fr = new FileReader();
  72. fr.onload = function(evt) {
  73. cb(evt.target.result);
  74. };
  75. fr.readAsArrayBuffer(blob);
  76. },
  77. blobToBinaryString: function(blob, cb){
  78. var fr = new FileReader();
  79. fr.onload = function(evt) {
  80. cb(evt.target.result);
  81. };
  82. fr.readAsBinaryString(blob);
  83. },
  84. binaryStringToArrayBuffer: function(binary) {
  85. var byteArray = new Uint8Array(binary.length);
  86. for (var i = 0; i < binary.length; i++) {
  87. byteArray[i] = binary.charCodeAt(i) & 0xff;
  88. }
  89. return byteArray.buffer;
  90. },
  91. randomToken: function () {
  92. return Math.random().toString(36).substr(2);
  93. },
  94. isBrowserCompatible: function() {
  95. var c, f;
  96. if (this.chromeCompatible) {
  97. if ((c = navigator.userAgent.split('Chrome/')) && c.length > 1) {
  98. // Get version #.
  99. var v = c[1].split('.')[0];
  100. return parseInt(v) >= this.chromeVersion;
  101. }
  102. }
  103. if (this.firefoxCompatible) {
  104. if ((f = navigator.userAgent.split('Firefox/')) && f.length > 1) {
  105. // Get version #.
  106. var v = f[1].split('.')[0];
  107. return parseInt(v) >= this.firefoxVersion;
  108. }
  109. }
  110. return false;
  111. },
  112. isSecure: function() {
  113. return location.protocol === 'https:';
  114. }
  115. };