createProcessObject.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = _default;
  6. var _deepCyclicCopy = _interopRequireDefault(require('./deepCyclicCopy'));
  7. function _interopRequireDefault(obj) {
  8. return obj && obj.__esModule ? obj : {default: obj};
  9. }
  10. /**
  11. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  12. *
  13. * This source code is licensed under the MIT license found in the
  14. * LICENSE file in the root directory of this source tree.
  15. */
  16. const BLACKLIST = new Set(['env', 'mainModule', '_events']);
  17. const isWin32 = process.platform === 'win32';
  18. const proto = Object.getPrototypeOf(process.env); // The "process.env" object has a bunch of particularities: first, it does not
  19. // directly extend from Object; second, it converts any assigned value to a
  20. // string; and third, it is case-insensitive in Windows. We use a proxy here to
  21. // mimic it (see https://nodejs.org/api/process.html#process_process_env).
  22. function createProcessEnv() {
  23. const real = Object.create(proto);
  24. const lookup = {};
  25. function deletePropertyWin32(_target, key) {
  26. for (const name in real) {
  27. if (real.hasOwnProperty(name)) {
  28. if (typeof key === 'string') {
  29. if (name.toLowerCase() === key.toLowerCase()) {
  30. delete real[name];
  31. delete lookup[name.toLowerCase()];
  32. }
  33. } else {
  34. if (key === name) {
  35. delete real[name];
  36. delete lookup[name];
  37. }
  38. }
  39. }
  40. }
  41. return true;
  42. }
  43. function deleteProperty(_target, key) {
  44. delete real[key];
  45. delete lookup[key];
  46. return true;
  47. }
  48. function getProperty(_target, key) {
  49. return real[key];
  50. }
  51. function getPropertyWin32(_target, key) {
  52. if (typeof key === 'string') {
  53. return lookup[key in proto ? key : key.toLowerCase()];
  54. } else {
  55. return real[key];
  56. }
  57. }
  58. const proxy = new Proxy(real, {
  59. deleteProperty: isWin32 ? deletePropertyWin32 : deleteProperty,
  60. get: isWin32 ? getPropertyWin32 : getProperty,
  61. set(_target, key, value) {
  62. const strValue = '' + value;
  63. if (typeof key === 'string') {
  64. lookup[key.toLowerCase()] = strValue;
  65. }
  66. real[key] = strValue;
  67. return true;
  68. }
  69. });
  70. return Object.assign(proxy, process.env);
  71. }
  72. function _default() {
  73. const process = require('process');
  74. const newProcess = (0, _deepCyclicCopy.default)(process, {
  75. blacklist: BLACKLIST,
  76. keepPrototype: true
  77. });
  78. try {
  79. // This fails on Node 12, but it's already set to 'process'
  80. newProcess[Symbol.toStringTag] = 'process';
  81. } catch (e) {
  82. // Make sure it's actually set instead of potentially ignoring errors
  83. if (newProcess[Symbol.toStringTag] !== 'process') {
  84. e.message =
  85. 'Unable to set toStringTag on process. Please open up an issue at https://github.com/facebook/jest\n\n' +
  86. e.message;
  87. throw e;
  88. }
  89. } // Sequentially execute all constructors over the object.
  90. let proto = process;
  91. while ((proto = Object.getPrototypeOf(proto))) {
  92. if (typeof proto.constructor === 'function') {
  93. proto.constructor.call(newProcess);
  94. }
  95. }
  96. newProcess.env = createProcessEnv();
  97. newProcess.send = () => {};
  98. const domainPropertyDescriptor = Object.getOwnPropertyDescriptor(
  99. newProcess,
  100. 'domain'
  101. );
  102. if (domainPropertyDescriptor && !domainPropertyDescriptor.enumerable) {
  103. Object.defineProperty(newProcess, 'domain', {
  104. get() {
  105. return process.domain;
  106. }
  107. });
  108. }
  109. return newProcess;
  110. }