createProcessObject.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = createProcessObject;
  6. var _deepCyclicCopy = _interopRequireDefault(require('./deepCyclicCopy'));
  7. function _interopRequireDefault(obj) {
  8. return obj && obj.__esModule ? obj : {default: obj};
  9. }
  10. /**
  11. * Copyright (c) Meta Platforms, Inc. and affiliates.
  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);
  19. // The "process.env" object has a bunch of particularities: first, it does not
  20. // directly extend from Object; second, it converts any assigned value to a
  21. // string; and third, it is case-insensitive in Windows. We use a proxy here to
  22. // mimic it (see https://nodejs.org/api/process.html#process_process_env).
  23. function createProcessEnv() {
  24. const real = Object.create(proto);
  25. const lookup = {};
  26. function deletePropertyWin32(_target, key) {
  27. for (const name in real) {
  28. if (Object.prototype.hasOwnProperty.call(real, name)) {
  29. if (typeof key === 'string') {
  30. if (name.toLowerCase() === key.toLowerCase()) {
  31. delete real[name];
  32. delete lookup[name.toLowerCase()];
  33. }
  34. } else {
  35. if (key === name) {
  36. delete real[name];
  37. delete lookup[name];
  38. }
  39. }
  40. }
  41. }
  42. return true;
  43. }
  44. function deleteProperty(_target, key) {
  45. delete real[key];
  46. delete lookup[key];
  47. return true;
  48. }
  49. function getProperty(_target, key) {
  50. return real[key];
  51. }
  52. function getPropertyWin32(_target, key) {
  53. if (typeof key === 'string') {
  54. return lookup[key in proto ? key : key.toLowerCase()];
  55. } else {
  56. return real[key];
  57. }
  58. }
  59. const proxy = new Proxy(real, {
  60. deleteProperty: isWin32 ? deletePropertyWin32 : deleteProperty,
  61. get: isWin32 ? getPropertyWin32 : getProperty,
  62. set(_target, key, value) {
  63. const strValue = `${value}`;
  64. if (typeof key === 'string') {
  65. lookup[key.toLowerCase()] = strValue;
  66. }
  67. real[key] = strValue;
  68. return true;
  69. }
  70. });
  71. return Object.assign(proxy, process.env);
  72. }
  73. function createProcessObject() {
  74. const process = require('process');
  75. const newProcess = (0, _deepCyclicCopy.default)(process, {
  76. blacklist: BLACKLIST,
  77. keepPrototype: true
  78. });
  79. try {
  80. // This fails on Node 12, but it's already set to 'process'
  81. newProcess[Symbol.toStringTag] = 'process';
  82. } catch (e) {
  83. // Make sure it's actually set instead of potentially ignoring errors
  84. if (newProcess[Symbol.toStringTag] !== 'process') {
  85. e.message = `Unable to set toStringTag on process. Please open up an issue at https://github.com/jestjs/jest\n\n${e.message}`;
  86. throw e;
  87. }
  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 = () => true;
  98. Object.defineProperty(newProcess, 'domain', {
  99. get() {
  100. return process.domain;
  101. }
  102. });
  103. return newProcess;
  104. }