|
619 | 619 | const has = Function.call.bind(Set.prototype.has); |
620 | 620 | const test = Function.call.bind(RegExp.prototype.test); |
621 | 621 |
|
622 | | - const { |
623 | | - getOptions, |
624 | | - types: { kV8Option }, |
625 | | - envSettings: { kAllowedInEnvironment } |
626 | | - } = internalBinding('options'); |
627 | | - const { options, aliases } = getOptions(); |
628 | | - |
629 | | - const allowedV8EnvironmentFlags = []; |
630 | | - const allowedNodeEnvironmentFlags = []; |
631 | | - for (const [name, info] of options) { |
632 | | - if (info.envVarSettings === kAllowedInEnvironment) { |
633 | | - if (info.type === kV8Option) { |
634 | | - allowedV8EnvironmentFlags.push(name); |
635 | | - } else { |
636 | | - allowedNodeEnvironmentFlags.push(name); |
| 622 | + const get = () => { |
| 623 | + const { |
| 624 | + getOptions, |
| 625 | + types: { kV8Option }, |
| 626 | + envSettings: { kAllowedInEnvironment } |
| 627 | + } = internalBinding('options'); |
| 628 | + const { options, aliases } = getOptions(); |
| 629 | + |
| 630 | + const allowedV8EnvironmentFlags = []; |
| 631 | + const allowedNodeEnvironmentFlags = []; |
| 632 | + for (const [name, info] of options) { |
| 633 | + if (info.envVarSettings === kAllowedInEnvironment) { |
| 634 | + if (info.type === kV8Option) { |
| 635 | + allowedV8EnvironmentFlags.push(name); |
| 636 | + } else { |
| 637 | + allowedNodeEnvironmentFlags.push(name); |
| 638 | + } |
637 | 639 | } |
638 | 640 | } |
639 | | - } |
640 | 641 |
|
641 | | - for (const [ from, expansion ] of aliases) { |
642 | | - let isAccepted = true; |
643 | | - for (const to of expansion) { |
644 | | - if (!to.startsWith('-')) continue; |
645 | | - const recursiveExpansion = aliases.get(to); |
646 | | - if (recursiveExpansion) { |
647 | | - expansion.push(...recursiveExpansion); |
648 | | - continue; |
| 642 | + for (const [ from, expansion ] of aliases) { |
| 643 | + let isAccepted = true; |
| 644 | + for (const to of expansion) { |
| 645 | + if (!to.startsWith('-')) continue; |
| 646 | + const recursiveExpansion = aliases.get(to); |
| 647 | + if (recursiveExpansion) { |
| 648 | + expansion.push(...recursiveExpansion); |
| 649 | + continue; |
| 650 | + } |
| 651 | + isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment; |
| 652 | + if (!isAccepted) break; |
| 653 | + } |
| 654 | + if (isAccepted) { |
| 655 | + let canonical = from; |
| 656 | + if (canonical.endsWith('=')) |
| 657 | + canonical = canonical.substr(0, canonical.length - 1); |
| 658 | + if (canonical.endsWith(' <arg>')) |
| 659 | + canonical = canonical.substr(0, canonical.length - 4); |
| 660 | + allowedNodeEnvironmentFlags.push(canonical); |
649 | 661 | } |
650 | | - isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment; |
651 | | - if (!isAccepted) break; |
652 | | - } |
653 | | - if (isAccepted) { |
654 | | - let canonical = from; |
655 | | - if (canonical.endsWith('=')) |
656 | | - canonical = canonical.substr(0, canonical.length - 1); |
657 | | - if (canonical.endsWith(' <arg>')) |
658 | | - canonical = canonical.substr(0, canonical.length - 4); |
659 | | - allowedNodeEnvironmentFlags.push(canonical); |
660 | 662 | } |
661 | | - } |
662 | 663 |
|
663 | | - const trimLeadingDashes = (flag) => replace(flag, leadingDashesRegex, ''); |
664 | | - |
665 | | - // Save these for comparison against flags provided to |
666 | | - // process.allowedNodeEnvironmentFlags.has() which lack leading dashes. |
667 | | - // Avoid interference w/ user code by flattening `Set.prototype` into |
668 | | - // each object. |
669 | | - const [nodeFlags, v8Flags] = [ |
670 | | - allowedNodeEnvironmentFlags, allowedV8EnvironmentFlags |
671 | | - ].map((flags) => Object.defineProperties( |
672 | | - new Set(flags.map(trimLeadingDashes)), |
673 | | - Object.getOwnPropertyDescriptors(Set.prototype)) |
674 | | - ); |
675 | | - |
676 | | - class NodeEnvironmentFlagsSet extends Set { |
677 | | - constructor(...args) { |
678 | | - super(...args); |
679 | | - |
680 | | - // the super constructor consumes `add`, but |
681 | | - // disallow any future adds. |
682 | | - this.add = () => this; |
683 | | - } |
| 664 | + const trimLeadingDashes = (flag) => replace(flag, leadingDashesRegex, ''); |
| 665 | + |
| 666 | + // Save these for comparison against flags provided to |
| 667 | + // process.allowedNodeEnvironmentFlags.has() which lack leading dashes. |
| 668 | + // Avoid interference w/ user code by flattening `Set.prototype` into |
| 669 | + // each object. |
| 670 | + const [nodeFlags, v8Flags] = [ |
| 671 | + allowedNodeEnvironmentFlags, allowedV8EnvironmentFlags |
| 672 | + ].map((flags) => Object.defineProperties( |
| 673 | + new Set(flags.map(trimLeadingDashes)), |
| 674 | + Object.getOwnPropertyDescriptors(Set.prototype)) |
| 675 | + ); |
684 | 676 |
|
685 | | - delete() { |
686 | | - // noop, `Set` API compatible |
687 | | - return false; |
688 | | - } |
| 677 | + class NodeEnvironmentFlagsSet extends Set { |
| 678 | + constructor(...args) { |
| 679 | + super(...args); |
689 | 680 |
|
690 | | - clear() { |
691 | | - // noop |
692 | | - } |
| 681 | + // the super constructor consumes `add`, but |
| 682 | + // disallow any future adds. |
| 683 | + this.add = () => this; |
| 684 | + } |
693 | 685 |
|
694 | | - has(key) { |
695 | | - // This will return `true` based on various possible |
696 | | - // permutations of a flag, including present/missing leading |
697 | | - // dash(es) and/or underscores-for-dashes in the case of V8-specific |
698 | | - // flags. Strips any values after `=`, inclusive. |
699 | | - // TODO(addaleax): It might be more flexible to run the option parser |
700 | | - // on a dummy option set and see whether it rejects the argument or |
701 | | - // not. |
702 | | - if (typeof key === 'string') { |
703 | | - key = replace(key, trailingValuesRegex, ''); |
704 | | - if (test(leadingDashesRegex, key)) { |
705 | | - return has(this, key) || |
706 | | - has(v8Flags, |
707 | | - replace( |
| 686 | + delete() { |
| 687 | + // noop, `Set` API compatible |
| 688 | + return false; |
| 689 | + } |
| 690 | + |
| 691 | + clear() { |
| 692 | + // noop |
| 693 | + } |
| 694 | + |
| 695 | + has(key) { |
| 696 | + // This will return `true` based on various possible |
| 697 | + // permutations of a flag, including present/missing leading |
| 698 | + // dash(es) and/or underscores-for-dashes in the case of V8-specific |
| 699 | + // flags. Strips any values after `=`, inclusive. |
| 700 | + // TODO(addaleax): It might be more flexible to run the option parser |
| 701 | + // on a dummy option set and see whether it rejects the argument or |
| 702 | + // not. |
| 703 | + if (typeof key === 'string') { |
| 704 | + key = replace(key, trailingValuesRegex, ''); |
| 705 | + if (test(leadingDashesRegex, key)) { |
| 706 | + return has(this, key) || |
| 707 | + has(v8Flags, |
708 | 708 | replace( |
709 | | - key, |
710 | | - leadingDashesRegex, |
711 | | - '' |
712 | | - ), |
713 | | - replaceDashesRegex, |
714 | | - '_' |
715 | | - ) |
716 | | - ); |
| 709 | + replace( |
| 710 | + key, |
| 711 | + leadingDashesRegex, |
| 712 | + '' |
| 713 | + ), |
| 714 | + replaceDashesRegex, |
| 715 | + '_' |
| 716 | + ) |
| 717 | + ); |
| 718 | + } |
| 719 | + return has(nodeFlags, key) || |
| 720 | + has(v8Flags, replace(key, replaceDashesRegex, '_')); |
717 | 721 | } |
718 | | - return has(nodeFlags, key) || |
719 | | - has(v8Flags, replace(key, replaceDashesRegex, '_')); |
| 722 | + return false; |
720 | 723 | } |
721 | | - return false; |
722 | 724 | } |
723 | | - } |
724 | 725 |
|
725 | | - Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor); |
726 | | - Object.freeze(NodeEnvironmentFlagsSet.prototype); |
| 726 | + Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor); |
| 727 | + Object.freeze(NodeEnvironmentFlagsSet.prototype); |
| 728 | + |
| 729 | + return process.allowedNodeEnvironmentFlags = Object.freeze( |
| 730 | + new NodeEnvironmentFlagsSet( |
| 731 | + allowedNodeEnvironmentFlags.concat(allowedV8EnvironmentFlags) |
| 732 | + )); |
| 733 | + }; |
727 | 734 |
|
728 | | - process.allowedNodeEnvironmentFlags = Object.freeze( |
729 | | - new NodeEnvironmentFlagsSet( |
730 | | - allowedNodeEnvironmentFlags.concat(allowedV8EnvironmentFlags) |
731 | | - ) |
732 | | - ); |
| 735 | + Object.defineProperty(process, 'allowedNodeEnvironmentFlags', { |
| 736 | + get, |
| 737 | + set(value) { |
| 738 | + Object.defineProperty(this, 'allowedNodeEnvironmentFlags', { |
| 739 | + value, |
| 740 | + configurable: true, |
| 741 | + enumerable: true, |
| 742 | + writable: true |
| 743 | + }); |
| 744 | + }, |
| 745 | + enumerable: true, |
| 746 | + configurable: true |
| 747 | + }); |
733 | 748 | } |
734 | 749 |
|
735 | 750 | startup(); |
|
0 commit comments