util.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. var defaultConfig = {'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }]};
  2. var util = {
  3. noop: function() {},
  4. CLOUD_HOST: '0.peerjs.com',
  5. CLOUD_PORT: 9000,
  6. // Logging logic
  7. logLevel: 0,
  8. setLogLevel: function(level) {
  9. var debugLevel = parseInt(level, 10);
  10. if (!isNaN(parseInt(level, 10))) {
  11. util.logLevel = debugLevel;
  12. } else {
  13. // If they are using truthy/falsy values for debug
  14. util.logLevel = level ? 3 : 0;
  15. }
  16. util.log = util.warn = util.error = util.noop;
  17. if (util.logLevel > 0) {
  18. util.error = util._printWith('ERROR');
  19. }
  20. if (util.logLevel > 1) {
  21. util.warn = util._printWith('WARNING');
  22. }
  23. if (util.logLevel > 2) {
  24. util.log = util._print;
  25. }
  26. },
  27. setLogFunction: function(fn) {
  28. if (fn.constructor !== Function) {
  29. util.warn('The log function you passed in is not a function. Defaulting to regular logs.');
  30. } else {
  31. util._print = fn;
  32. }
  33. },
  34. _printWith: function(prefix) {
  35. return function() {
  36. var copy = Array.prototype.slice.call(arguments);
  37. copy.unshift(prefix);
  38. util._print.apply(util, copy);
  39. };
  40. },
  41. _print: function () {
  42. var err = false;
  43. var copy = Array.prototype.slice.call(arguments);
  44. copy.unshift('PeerJS: ');
  45. for (var i = 0, l = copy.length; i < l; i++){
  46. if (copy[i] instanceof Error) {
  47. copy[i] = '(' + copy[i].name + ') ' + copy[i].message;
  48. err = true;
  49. }
  50. }
  51. err ? console.error.apply(console, copy) : console.log.apply(console, copy);
  52. },
  53. //
  54. // Returns browser-agnostic default config
  55. defaultConfig: defaultConfig,
  56. //
  57. // Returns the current browser.
  58. browser: (function() {
  59. if (window.mozRTCPeerConnection) {
  60. return 'Firefox';
  61. } else if (window.webkitRTCPeerConnection) {
  62. return 'Chrome';
  63. } else if (window.RTCPeerConnection) {
  64. return 'Supported';
  65. } else {
  66. return 'Unsupported';
  67. }
  68. })(),
  69. //
  70. // Lists which features are supported
  71. supports: (function() {
  72. if (typeof RTCPeerConnection === 'undefined') {
  73. return {};
  74. }
  75. var data = true;
  76. var audioVideo = true;
  77. var binary = false;
  78. var reliable = false;
  79. var onnegotiationneeded = !!window.webkitRTCPeerConnection;
  80. var pc, dc;
  81. try {
  82. pc = new RTCPeerConnection(defaultConfig, {optional: [{RtpDataChannels: true}]});
  83. } catch (e) {
  84. data = false;
  85. audioVideo = false;
  86. }
  87. if (data) {
  88. try {
  89. dc = pc.createDataChannel('_PEERJSTEST');
  90. } catch (e) {
  91. data = false;
  92. }
  93. }
  94. if (data) {
  95. try {
  96. dc.binaryType = 'blob';
  97. binary = true;
  98. } catch (e) {
  99. }
  100. var reliablePC = new RTCPeerConnection(defaultConfig, {});
  101. try {
  102. var reliableDC = reliablePC.createDataChannel('_PEERJSRELIABLETEST', {maxRetransmits: 0});
  103. reliable = true;
  104. } catch (e) {
  105. }
  106. reliablePC.close();
  107. }
  108. // FIXME: not really the best check...
  109. if (audioVideo) {
  110. audioVideo = !!pc.addStream;
  111. }
  112. // FIXME: this is not great because in theory it doesn't work for
  113. // av-only browsers (?).
  114. if (!onnegotiationneeded && data) {
  115. // sync default check.
  116. var negotiationPC = new RTCPeerConnection(defaultConfig, {optional: [{RtpDataChannels: true}]});
  117. negotiationPC.onnegotiationneeded = function() {
  118. onnegotiationneeded = true;
  119. // async check.
  120. if (util && util.supports) {
  121. util.supports.onnegotiationneeded = true;
  122. }
  123. };
  124. var negotiationDC = negotiationPC.createDataChannel('_PEERJSNEGOTIATIONTEST');
  125. setTimeout(function() {
  126. negotiationPC.close();
  127. }, 1000);
  128. }
  129. if (pc) {
  130. pc.close();
  131. }
  132. return {
  133. audioVideo: audioVideo,
  134. data: data,
  135. binary: binary,
  136. reliable: reliable,
  137. onnegotiationneeded: onnegotiationneeded
  138. };
  139. }()),
  140. //
  141. // Ensure alphanumeric ids
  142. validateId: function(id) {
  143. // Allow empty ids
  144. return !id || /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(id);
  145. },
  146. validateKey: function(key) {
  147. // Allow empty keys
  148. return !key || /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(key);
  149. },
  150. debug: false,
  151. inherits: function(ctor, superCtor) {
  152. ctor.super_ = superCtor;
  153. ctor.prototype = Object.create(superCtor.prototype, {
  154. constructor: {
  155. value: ctor,
  156. enumerable: false,
  157. writable: true,
  158. configurable: true
  159. }
  160. });
  161. },
  162. extend: function(dest, source) {
  163. for(var key in source) {
  164. if(source.hasOwnProperty(key)) {
  165. dest[key] = source[key];
  166. }
  167. }
  168. return dest;
  169. },
  170. pack: BinaryPack.pack,
  171. unpack: BinaryPack.unpack,
  172. log: function () {
  173. if (util.debug) {
  174. var err = false;
  175. var copy = Array.prototype.slice.call(arguments);
  176. copy.unshift('PeerJS: ');
  177. for (var i = 0, l = copy.length; i < l; i++){
  178. if (copy[i] instanceof Error) {
  179. copy[i] = '(' + copy[i].name + ') ' + copy[i].message;
  180. err = true;
  181. }
  182. }
  183. err ? console.error.apply(console, copy) : console.log.apply(console, copy);
  184. }
  185. },
  186. setZeroTimeout: (function(global) {
  187. var timeouts = [];
  188. var messageName = 'zero-timeout-message';
  189. // Like setTimeout, but only takes a function argument. There's
  190. // no time argument (always zero) and no arguments (you have to
  191. // use a closure).
  192. function setZeroTimeoutPostMessage(fn) {
  193. timeouts.push(fn);
  194. global.postMessage(messageName, '*');
  195. }
  196. function handleMessage(event) {
  197. if (event.source == global && event.data == messageName) {
  198. if (event.stopPropagation) {
  199. event.stopPropagation();
  200. }
  201. if (timeouts.length) {
  202. timeouts.shift()();
  203. }
  204. }
  205. }
  206. if (global.addEventListener) {
  207. global.addEventListener('message', handleMessage, true);
  208. } else if (global.attachEvent) {
  209. global.attachEvent('onmessage', handleMessage);
  210. }
  211. return setZeroTimeoutPostMessage;
  212. }(this)),
  213. // Binary stuff
  214. blobToArrayBuffer: function(blob, cb){
  215. var fr = new FileReader();
  216. fr.onload = function(evt) {
  217. cb(evt.target.result);
  218. };
  219. fr.readAsArrayBuffer(blob);
  220. },
  221. blobToBinaryString: function(blob, cb){
  222. var fr = new FileReader();
  223. fr.onload = function(evt) {
  224. cb(evt.target.result);
  225. };
  226. fr.readAsBinaryString(blob);
  227. },
  228. binaryStringToArrayBuffer: function(binary) {
  229. var byteArray = new Uint8Array(binary.length);
  230. for (var i = 0; i < binary.length; i++) {
  231. byteArray[i] = binary.charCodeAt(i) & 0xff;
  232. }
  233. return byteArray.buffer;
  234. },
  235. randomToken: function () {
  236. return Math.random().toString(36).substr(2);
  237. },
  238. //
  239. isSecure: function() {
  240. return location.protocol === 'https:';
  241. }
  242. };
  243. exports.util = util;