/server/node_modules/rewire/test/testModules/moduleB.js

https://github.com/debdayal/TestApp · JavaScript · 137 lines · 121 code · 14 blank · 2 comment · 25 complexity · 62c2ada520094821deea6fac68bfd8e1 MD5 · raw file

  1. "use strict"; // run code in ES5 strict mode
  2. var someOtherModule = require("./someOtherModule.js"),
  3. myNumber = 0, // copy by value
  4. myObj = {}, // copy by reference
  5. env = "bla",
  6. fs;
  7. // We need getters and setters for private vars to check if our injected setters and getters actual work
  8. function setMyNumber(newNumber) {
  9. myNumber = newNumber;
  10. }
  11. function getMyNumber() {
  12. return myNumber;
  13. }
  14. function setMyObj(newObj) {
  15. myObj = newObj;
  16. }
  17. function getMyObj() {
  18. return myObj;
  19. }
  20. function readFileSync() {
  21. fs.readFileSync("bla.txt", "utf8");
  22. }
  23. function checkSomeGlobals() {
  24. var isLowerIE,
  25. typeOfGlobalFunc;
  26. if (typeof navigator !== "undefined") {
  27. isLowerIE = /MSIE [6-8]\.[0-9]/g.test(navigator.userAgent);
  28. }
  29. if (isLowerIE) {
  30. typeOfGlobalFunc = "object";
  31. } else {
  32. typeOfGlobalFunc = "function";
  33. }
  34. if (typeof global !== "object") {
  35. throw new ReferenceError("global is not an object");
  36. }
  37. if (typeof console !== "object") {
  38. throw new ReferenceError("console is not an object");
  39. }
  40. if (typeof require !== "function") {
  41. throw new ReferenceError("require is not a function");
  42. }
  43. if (typeof module !== "object") {
  44. throw new ReferenceError("module is not an object");
  45. }
  46. if (typeof exports !== "object") {
  47. throw new ReferenceError("exports is not an object");
  48. }
  49. if (module.exports === exports) {
  50. throw new Error("module.exports === exports returns true");
  51. }
  52. if (typeof __dirname !== "string") {
  53. throw new ReferenceError("__dirname is not a string");
  54. }
  55. if (typeof __filename !== "string") {
  56. throw new ReferenceError("__filename is not a string");
  57. }
  58. if (typeof setTimeout !== typeOfGlobalFunc) {
  59. throw new ReferenceError("setTimeout is not a function");
  60. }
  61. if (typeof clearTimeout !== typeOfGlobalFunc) {
  62. throw new ReferenceError("clearTimeout is not a function");
  63. }
  64. if (typeof setInterval !== typeOfGlobalFunc) {
  65. throw new ReferenceError("setInterval is not a function");
  66. }
  67. if (typeof clearInterval !== typeOfGlobalFunc) {
  68. throw new ReferenceError("clearInterval is not a function");
  69. }
  70. if (typeof Error !== "function") {
  71. throw new ReferenceError("Error is not a function");
  72. }
  73. if (typeof parseFloat !== "function") {
  74. throw new ReferenceError("parseFloat is not a function");
  75. }
  76. if (typeof parseInt !== "function") {
  77. throw new ReferenceError("parseInt is not a function");
  78. }
  79. if (typeof window === "undefined") {
  80. if (typeof process !== "object") {
  81. throw new ReferenceError("process is not an object");
  82. }
  83. if (typeof Buffer !== "function") {
  84. throw new ReferenceError("Buffer is not a function");
  85. }
  86. } else {
  87. if (typeof encodeURIComponent !== "function") {
  88. throw new ReferenceError("encodeURIComponent is not a function");
  89. }
  90. if (typeof decodeURIComponent !== "function") {
  91. throw new ReferenceError("decodeURIComponent is not a function");
  92. }
  93. if (typeof document !== "object") {
  94. throw new ReferenceError("document is not an object");
  95. }
  96. }
  97. }
  98. function getConsole() {
  99. return console;
  100. }
  101. function getFilename() {
  102. return __filename;
  103. }
  104. function getBuffer() {
  105. return Buffer;
  106. }
  107. function getDocument() {
  108. return document;
  109. }
  110. // different styles of exports in moduleA.js and moduleB.js
  111. module.exports = {
  112. setMyNumber: setMyNumber,
  113. getMyNumber: getMyNumber,
  114. setMyObj: setMyObj,
  115. getMyObj: getMyObj,
  116. readFileSync: readFileSync,
  117. checkSomeGlobals: checkSomeGlobals,
  118. getConsole: getConsole,
  119. getFilename: getFilename,
  120. getBuffer: getBuffer,
  121. getDocument: getDocument,
  122. someOtherModule: someOtherModule
  123. };