create_process_object.js 2.8 KB

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