util.js 7.9 KB

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