Skip to content

Commit

Permalink
feat!: Utilize process.allowedNodeEnvironmentFlags (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Nov 8, 2021
1 parent 530b798 commit 2240a0f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 38 deletions.
55 changes: 21 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,43 +85,30 @@ function normalizeFlagName(flag) {
// `--v8-options` and parses the result, returning an array of command
// line flags.
function getFlags(cb) {
var errored = false;
var pending = 0;
var flags = [];

runNode('--help');
runNode('--v8-options');

function runNode(option) {
pending++;
execFile(process.execPath, [option], function (execErr, result) {
if (execErr || errored) {
if (!errored) {
errored = true;
cb(execErr);
}
return;
}
var flags = Array.from(process.allowedNodeEnvironmentFlags);

var index = result.indexOf('\nOptions:');
if (index >= 0) {
var regexp = /^\s\s--[\w-]+/gm;
regexp.lastIndex = index;
var matchedFlags = result.match(regexp);
if (matchedFlags) {
flags = flags.concat(
matchedFlags.map(normalizeFlagName).filter(function (name) {
return exclusions.indexOf(name) === -1;
})
);
}
}
execFile(process.execPath, ['--v8-options'], function (execErr, result) {
if (execErr) {
cb(execErr);
return;
}

if (--pending === 0) {
cb(null, flags);
var index = result.indexOf('\nOptions:');
if (index >= 0) {
var regexp = /^\s\s--[\w-]+/gm;
regexp.lastIndex = index;
var matchedFlags = result.match(regexp);
if (matchedFlags) {
flags = flags.concat(
matchedFlags.map(normalizeFlagName).filter(function (name) {
return exclusions.indexOf(name) === -1;
})
);
}
});
}
}

cb(null, flags);
});
}

// write some json to a file descriptor. if this fails, call back
Expand Down
7 changes: 3 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('v8flags', function () {
});
});

it('will not throw on non-matching return from node --v8-options', function (done) {
it('always has node flags, even when non-matching return from node --v8-options', function (done) {
if (os.platform() === 'win32') {
this.skip();
}
Expand All @@ -131,7 +131,7 @@ describe('v8flags', function () {

v8flags(function (err, flags) {
expect(err).toBeNull();
expect(flags).toEqual([]);
expect(flags).toEqual(Array.from(process.allowedNodeEnvironmentFlags));
// Restore original execPath
process.execPath = execPath;
done();
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('v8flags', function () {
eraseHome();
var v8flags = require('../');
v8flags(function (err, flags) {
expect(flags).toContain('--no_deprecation');
expect(flags).toContain('--no-deprecation');
done();
});
});
Expand All @@ -196,7 +196,6 @@ describe('v8flags', function () {
expect(flags).not.toContain('--exec');
expect(flags).not.toContain('--print');
expect(flags).not.toContain('--interactive');
expect(flags).not.toContain('--require');
expect(flags).not.toContain('--version');
done();
});
Expand Down

0 comments on commit 2240a0f

Please sign in to comment.