builder.common.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. var vm = require('vm')
  7. , should = require('should');
  8. /**
  9. * Generates evn variables for the vm so we can `emulate` a browser.
  10. * @returns {Object} evn variables
  11. */
  12. exports.env = function env () {
  13. var details = {
  14. location: {
  15. port: 8080
  16. , host: 'www.example.org'
  17. , hostname: 'www.example.org'
  18. , href: 'http://www.example.org/example/'
  19. , pathname: '/example/'
  20. , protocol: 'http:'
  21. , search: ''
  22. , hash: ''
  23. }
  24. , console: {
  25. log: function(){},
  26. info: function(){},
  27. warn: function(){},
  28. error: function(){}
  29. }
  30. , navigator: {
  31. userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit'
  32. + '/534.27 (KHTML, like Gecko) Chrome/12.0.716.0 Safari/534.27'
  33. , appName: 'socket.io'
  34. , platform: process.platform
  35. , appVersion: process.version
  36. , }
  37. , name: 'socket.io'
  38. , innerWidth: 1024
  39. , innerHeight: 768
  40. , length: 1
  41. , outerWidth: 1024
  42. , outerHeight: 768
  43. , pageXOffset: 0
  44. , pageYOffset: 0
  45. , screenX: 0
  46. , screenY: 0
  47. , screenLeft: 0
  48. , screenTop: 0
  49. , scrollX: 0
  50. , scrollY: 0
  51. , scrollTop: 0
  52. , scrollLeft: 0
  53. , screen: {
  54. width: 0
  55. , height: 0
  56. }
  57. };
  58. // circular references
  59. details.window = details.self = details.contentWindow = details;
  60. // callable methods
  61. details.Image = details.scrollTo = details.scrollBy = details.scroll =
  62. details.resizeTo = details.resizeBy = details.prompt = details.print =
  63. details.open = details.moveTo = details.moveBy = details.focus =
  64. details.createPopup = details.confirm = details.close = details.blur =
  65. details.alert = details.clearTimeout = details.clearInterval =
  66. details.setInterval = details.setTimeout = details.XMLHttpRequest =
  67. details.getComputedStyle = details.trigger = details.dispatchEvent =
  68. details.removeEventListener = details.addEventListener = function(){};
  69. // frames
  70. details.frames = [details];
  71. // document
  72. details.document = details;
  73. details.document.domain = details.location.href;
  74. return details;
  75. };
  76. /**
  77. * Executes a script in a browser like env and returns
  78. * the result
  79. *
  80. * @param {String} contents The script content
  81. * @returns {Object} The evaluated script.
  82. */
  83. exports.execute = function execute (contents) {
  84. var env = exports.env()
  85. , script = vm.createScript(contents);
  86. // run the script with `browser like` globals
  87. script.runInNewContext(env);
  88. return env;
  89. };