create_process_object.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = function() {
  6. const process = require('process');
  7. const newProcess = (0,
  8. (_deep_cyclic_copy || _load_deep_cyclic_copy()).default)(process, {
  9. blacklist: BLACKLIST,
  10. keepPrototype: true
  11. });
  12. newProcess[Symbol.toStringTag] = 'process';
  13. // Sequentially execute all constructors over the object.
  14. let proto = process;
  15. while ((proto = Object.getPrototypeOf(proto))) {
  16. if (typeof proto.constructor === 'function') {
  17. proto.constructor.call(newProcess);
  18. }
  19. }
  20. newProcess.env = createProcessEnv();
  21. newProcess.send = () => {};
  22. return newProcess;
  23. };
  24. var _deep_cyclic_copy;
  25. function _load_deep_cyclic_copy() {
  26. return (_deep_cyclic_copy = _interopRequireDefault(
  27. require('./deep_cyclic_copy')
  28. ));
  29. }
  30. function _interopRequireDefault(obj) {
  31. return obj && obj.__esModule ? obj : {default: obj};
  32. }
  33. const BLACKLIST = new Set(['env', 'mainModule', '_events']);
  34. // The "process.env" object has a bunch of particularities: first, it does not
  35. // directly extend from Object; second, it converts any assigned value to a
  36. // string; and third, it is case-insensitive in Windows. We use a proxy here to
  37. // mimic it (see https://nodejs.org/api/process.html#process_process_env).
  38. /**
  39. * Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
  40. *
  41. * This source code is licensed under the MIT license found in the
  42. * LICENSE file in the root directory of this source tree.
  43. *
  44. *
  45. */
  46. function createProcessEnv() {
  47. if (typeof Proxy === 'undefined') {
  48. return (0, (_deep_cyclic_copy || _load_deep_cyclic_copy()).default)(
  49. process.env
  50. );
  51. }
  52. // $FlowFixMe: Apparently Flow does not understand that this is a prototype.
  53. const proto = Object.getPrototypeOf(process.env);
  54. const real = Object.create(proto);
  55. const lookup = {};
  56. const proxy = new Proxy(real, {
  57. deleteProperty: function(target, key) {
  58. for (const name in real) {
  59. if (real.hasOwnProperty(name)) {
  60. if (typeof key === 'string' && process.platform === 'win32') {
  61. if (name.toLowerCase() === key.toLowerCase()) {
  62. delete real[name];
  63. delete lookup[name.toLowerCase()];
  64. }
  65. } else {
  66. if (key === name) {
  67. delete real[name];
  68. delete lookup[name];
  69. }
  70. }
  71. }
  72. }
  73. return true;
  74. },
  75. get: function(target, key) {
  76. if (typeof key === 'string' && process.platform === 'win32') {
  77. return lookup[key in proto ? key : key.toLowerCase()];
  78. } else {
  79. return real[key];
  80. }
  81. },
  82. set: function(target, key, value) {
  83. const strValue = '' + value;
  84. if (typeof key === 'string') {
  85. lookup[key.toLowerCase()] = strValue;
  86. }
  87. real[key] = strValue;
  88. return true;
  89. }
  90. });
  91. return Object.assign(proxy, process.env);
  92. }