|
194 | 194 |
|
195 | 195 | perf.markMilestone(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
|
196 | 196 |
|
| 197 | + setupAllowedFlags(); |
| 198 | + |
197 | 199 | // There are various modes that Node can run in. The most common two
|
198 | 200 | // are running from a script and running the REPL - but there are a few
|
199 | 201 | // others like the debugger or running --eval arguments. Here we decide
|
|
593 | 595 | new vm.Script(source, { displayErrors: true, filename });
|
594 | 596 | }
|
595 | 597 |
|
| 598 | + function setupAllowedFlags() { |
| 599 | + // This builds process.allowedNodeEnvironmentFlags |
| 600 | + // from data in the config binding |
| 601 | + |
| 602 | + const replaceDashesRegex = /-/g; |
| 603 | + const leadingDashesRegex = /^--?/; |
| 604 | + const trailingValuesRegex = /=.*$/; |
| 605 | + |
| 606 | + // Save references so user code does not interfere |
| 607 | + const replace = Function.call.bind(String.prototype.replace); |
| 608 | + const has = Function.call.bind(Set.prototype.has); |
| 609 | + const test = Function.call.bind(RegExp.prototype.test); |
| 610 | + |
| 611 | + const { |
| 612 | + allowedV8EnvironmentFlags, |
| 613 | + allowedNodeEnvironmentFlags |
| 614 | + } = process.binding('config'); |
| 615 | + |
| 616 | + const trimLeadingDashes = (flag) => replace(flag, leadingDashesRegex, ''); |
| 617 | + |
| 618 | + // Save these for comparison against flags provided to |
| 619 | + // process.allowedNodeEnvironmentFlags.has() which lack leading dashes. |
| 620 | + // Avoid interference w/ user code by flattening `Set.prototype` into |
| 621 | + // each object. |
| 622 | + const [nodeFlags, v8Flags] = [ |
| 623 | + allowedNodeEnvironmentFlags, allowedV8EnvironmentFlags |
| 624 | + ].map((flags) => Object.defineProperties( |
| 625 | + new Set(flags.map(trimLeadingDashes)), |
| 626 | + Object.getOwnPropertyDescriptors(Set.prototype)) |
| 627 | + ); |
| 628 | + |
| 629 | + class NodeEnvironmentFlagsSet extends Set { |
| 630 | + constructor(...args) { |
| 631 | + super(...args); |
| 632 | + |
| 633 | + // the super constructor consumes `add`, but |
| 634 | + // disallow any future adds. |
| 635 | + this.add = () => this; |
| 636 | + } |
| 637 | + |
| 638 | + delete() { |
| 639 | + // noop, `Set` API compatible |
| 640 | + return false; |
| 641 | + } |
| 642 | + |
| 643 | + clear() { |
| 644 | + // noop |
| 645 | + } |
| 646 | + |
| 647 | + has(key) { |
| 648 | + // This will return `true` based on various possible |
| 649 | + // permutations of a flag, including present/missing leading |
| 650 | + // dash(es) and/or underscores-for-dashes in the case of V8-specific |
| 651 | + // flags. Strips any values after `=`, inclusive. |
| 652 | + if (typeof key === 'string') { |
| 653 | + key = replace(key, trailingValuesRegex, ''); |
| 654 | + if (test(leadingDashesRegex, key)) { |
| 655 | + return has(this, key) || |
| 656 | + has(v8Flags, |
| 657 | + replace( |
| 658 | + replace( |
| 659 | + key, |
| 660 | + leadingDashesRegex, |
| 661 | + '' |
| 662 | + ), |
| 663 | + replaceDashesRegex, |
| 664 | + '_' |
| 665 | + ) |
| 666 | + ); |
| 667 | + } |
| 668 | + return has(nodeFlags, key) || |
| 669 | + has(v8Flags, replace(key, replaceDashesRegex, '_')); |
| 670 | + } |
| 671 | + return false; |
| 672 | + } |
| 673 | + } |
| 674 | + |
| 675 | + Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor); |
| 676 | + Object.freeze(NodeEnvironmentFlagsSet.prototype); |
| 677 | + |
| 678 | + process.allowedNodeEnvironmentFlags = Object.freeze( |
| 679 | + new NodeEnvironmentFlagsSet( |
| 680 | + allowedNodeEnvironmentFlags.concat(allowedV8EnvironmentFlags) |
| 681 | + ) |
| 682 | + ); |
| 683 | + } |
| 684 | + |
596 | 685 | startup();
|
597 | 686 | });
|
0 commit comments