|
185 | 185 |
|
186 | 186 | perf.markMilestone(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
|
187 | 187 |
|
| 188 | + setupAllowedFlags(); |
| 189 | + |
188 | 190 | // There are various modes that Node can run in. The most common two
|
189 | 191 | // are running from a script and running the REPL - but there are a few
|
190 | 192 | // others like the debugger or running --eval arguments. Here we decide
|
|
631 | 633 | new vm.Script(source, { displayErrors: true, filename });
|
632 | 634 | }
|
633 | 635 |
|
| 636 | + function setupAllowedFlags() { |
| 637 | + // This builds process.allowedNodeEnvironmentFlags |
| 638 | + // from data in the config binding |
| 639 | + |
| 640 | + const replaceDashesRegex = /-/g; |
| 641 | + const leadingDashesRegex = /^--?/; |
| 642 | + const trailingValuesRegex = /=.*$/; |
| 643 | + |
| 644 | + // Save references so user code does not interfere |
| 645 | + const replace = Function.call.bind(String.prototype.replace); |
| 646 | + const has = Function.call.bind(Set.prototype.has); |
| 647 | + const test = Function.call.bind(RegExp.prototype.test); |
| 648 | + |
| 649 | + const { |
| 650 | + allowedV8EnvironmentFlags, |
| 651 | + allowedNodeEnvironmentFlags |
| 652 | + } = process.binding('config'); |
| 653 | + |
| 654 | + const trimLeadingDashes = (flag) => replace(flag, leadingDashesRegex, ''); |
| 655 | + |
| 656 | + // Save these for comparison against flags provided to |
| 657 | + // process.allowedNodeEnvironmentFlags.has() which lack leading dashes. |
| 658 | + // Avoid interference w/ user code by flattening `Set.prototype` into |
| 659 | + // each object. |
| 660 | + const [nodeFlags, v8Flags] = [ |
| 661 | + allowedNodeEnvironmentFlags, allowedV8EnvironmentFlags |
| 662 | + ].map((flags) => Object.defineProperties( |
| 663 | + new Set(flags.map(trimLeadingDashes)), |
| 664 | + Object.getOwnPropertyDescriptors(Set.prototype)) |
| 665 | + ); |
| 666 | + |
| 667 | + class NodeEnvironmentFlagsSet extends Set { |
| 668 | + constructor(...args) { |
| 669 | + super(...args); |
| 670 | + |
| 671 | + // the super constructor consumes `add`, but |
| 672 | + // disallow any future adds. |
| 673 | + this.add = () => this; |
| 674 | + } |
| 675 | + |
| 676 | + delete() { |
| 677 | + // noop, `Set` API compatible |
| 678 | + return false; |
| 679 | + } |
| 680 | + |
| 681 | + clear() { |
| 682 | + // noop |
| 683 | + } |
| 684 | + |
| 685 | + has(key) { |
| 686 | + // This will return `true` based on various possible |
| 687 | + // permutations of a flag, including present/missing leading |
| 688 | + // dash(es) and/or underscores-for-dashes in the case of V8-specific |
| 689 | + // flags. Strips any values after `=`, inclusive. |
| 690 | + if (typeof key === 'string') { |
| 691 | + key = replace(key, trailingValuesRegex, ''); |
| 692 | + if (test(leadingDashesRegex, key)) { |
| 693 | + return has(this, key) || |
| 694 | + has(v8Flags, |
| 695 | + replace( |
| 696 | + replace( |
| 697 | + key, |
| 698 | + leadingDashesRegex, |
| 699 | + '' |
| 700 | + ), |
| 701 | + replaceDashesRegex, |
| 702 | + '_' |
| 703 | + ) |
| 704 | + ); |
| 705 | + } |
| 706 | + return has(nodeFlags, key) || |
| 707 | + has(v8Flags, replace(key, replaceDashesRegex, '_')); |
| 708 | + } |
| 709 | + return false; |
| 710 | + } |
| 711 | + } |
| 712 | + |
| 713 | + Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor); |
| 714 | + Object.freeze(NodeEnvironmentFlagsSet.prototype); |
| 715 | + |
| 716 | + process.allowedNodeEnvironmentFlags = Object.freeze( |
| 717 | + new NodeEnvironmentFlagsSet( |
| 718 | + allowedNodeEnvironmentFlags.concat(allowedV8EnvironmentFlags) |
| 719 | + ) |
| 720 | + ); |
| 721 | + } |
| 722 | + |
634 | 723 | startup();
|
635 | 724 | });
|
0 commit comments