util.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var util = {
  2. chromeCompatible: true,
  3. firefoxCompatible: false,
  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. randomPort: function() {
  30. return Math.round(Math.random() * 60535) + 5000;
  31. },
  32. log: function () {
  33. if (util.debug) {
  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. setZeroTimeout: (function(global) {
  47. var timeouts = [];
  48. var messageName = 'zero-timeout-message';
  49. // Like setTimeout, but only takes a function argument. There's
  50. // no time argument (always zero) and no arguments (you have to
  51. // use a closure).
  52. function setZeroTimeoutPostMessage(fn) {
  53. timeouts.push(fn);
  54. global.postMessage(messageName, '*');
  55. }
  56. function handleMessage(event) {
  57. if (event.source == global && event.data == messageName) {
  58. if (event.stopPropagation) {
  59. event.stopPropagation();
  60. }
  61. if (timeouts.length) {
  62. timeouts.shift()();
  63. }
  64. }
  65. }
  66. if (global.addEventListener) {
  67. global.addEventListener('message', handleMessage, true);
  68. } else if (global.attachEvent) {
  69. global.attachEvent('onmessage', handleMessage);
  70. }
  71. return setZeroTimeoutPostMessage;
  72. }(this)),
  73. blobToArrayBuffer: function(blob, cb){
  74. var fr = new FileReader();
  75. fr.onload = function(evt) {
  76. cb(evt.target.result);
  77. };
  78. fr.readAsArrayBuffer(blob);
  79. },
  80. blobToBinaryString: function(blob, cb){
  81. var fr = new FileReader();
  82. fr.onload = function(evt) {
  83. cb(evt.target.result);
  84. };
  85. fr.readAsBinaryString(blob);
  86. },
  87. binaryStringToArrayBuffer: function(binary) {
  88. var byteArray = new Uint8Array(binary.length);
  89. for (var i = 0; i < binary.length; i++) {
  90. byteArray[i] = binary.charCodeAt(i) & 0xff;
  91. }
  92. return byteArray.buffer;
  93. },
  94. randomToken: function () {
  95. return Math.random().toString(36).substr(2);
  96. },
  97. isBrowserCompatible: function() {
  98. var c, f;
  99. if (this.chromeCompatible) {
  100. if ((c = navigator.userAgent.split('Chrome/')) && c.length > 1) {
  101. // Get version #.
  102. var v = c[1].split('.')[0];
  103. return parseInt(v) >= this.chromeVersion;
  104. }
  105. }
  106. if (this.firefoxCompatible) {
  107. if ((f = navigator.userAgent.split('Firefox/')) && f.length > 1) {
  108. // Get version #.
  109. var v = f[1].split('.')[0];
  110. return parseInt(v) >= this.firefoxVersion;
  111. }
  112. }
  113. return false;
  114. }
  115. };