-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: add option to disable loading native addons
Backport-PR-URL: #40094 PR-URL: #39977 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
- Loading branch information
1 parent
2755d39
commit b050c65
Showing
23 changed files
with
274 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
'targets': [ | ||
{ | ||
'target_name': 'binding', | ||
'sources': [ '../hello-world/binding.cc' ], | ||
'includes': ['../common.gypi'], | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Flags: --no-addons | ||
|
||
'use strict'; | ||
|
||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
const { Worker } = require('worker_threads'); | ||
|
||
const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`); | ||
|
||
const assertError = (error) => { | ||
assert.strictEqual(error.code, 'ERR_DLOPEN_DISABLED'); | ||
assert.strictEqual( | ||
error.message, | ||
'Cannot load native addon because loading addons is disabled.' | ||
); | ||
}; | ||
|
||
{ | ||
// Flags should be inherited | ||
const worker = new Worker(`require(${JSON.stringify(binding)})`, { | ||
eval: true, | ||
}); | ||
|
||
worker.on('error', common.mustCall(assertError)); | ||
} | ||
|
||
{ | ||
// Should throw when using `process.dlopen` directly | ||
const worker = new Worker( | ||
`process.dlopen({ exports: {} }, ${JSON.stringify(binding)});`, | ||
{ | ||
eval: true, | ||
} | ||
); | ||
|
||
worker.on('error', common.mustCall(assertError)); | ||
} | ||
|
||
{ | ||
// Explicitly pass `--no-addons` | ||
const worker = new Worker(`require(${JSON.stringify(binding)})`, { | ||
eval: true, | ||
execArgv: ['--no-addons'], | ||
}); | ||
|
||
worker.on('error', common.mustCall(assertError)); | ||
} | ||
|
||
{ | ||
// If `execArgv` is overwritten it should still fail to load addons | ||
const worker = new Worker(`require(${JSON.stringify(binding)})`, { | ||
eval: true, | ||
execArgv: [], | ||
}); | ||
|
||
worker.on('error', common.mustCall(assertError)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Flags: --no-addons | ||
|
||
'use strict'; | ||
|
||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
|
||
const bindingPath = require.resolve(`./build/${common.buildType}/binding`); | ||
|
||
const assertError = (error) => { | ||
assert(error instanceof Error); | ||
assert.strictEqual(error.code, 'ERR_DLOPEN_DISABLED'); | ||
assert.strictEqual( | ||
error.message, | ||
'Cannot load native addon because loading addons is disabled.' | ||
); | ||
}; | ||
|
||
{ | ||
let threw = false; | ||
|
||
try { | ||
require(bindingPath); | ||
} catch (error) { | ||
assertError(error); | ||
threw = true; | ||
} | ||
|
||
assert(threw); | ||
} | ||
|
||
{ | ||
let threw = false; | ||
|
||
try { | ||
process.dlopen({ exports: {} }, bindingPath); | ||
} catch (error) { | ||
assertError(error); | ||
threw = true; | ||
} | ||
|
||
assert(threw); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { mustCall } from '../common/index.mjs'; | ||
import { Worker, isMainThread } from 'worker_threads'; | ||
import assert from 'assert'; | ||
import { fileURLToPath } from 'url'; | ||
import { requireFixture, importFixture } from '../fixtures/pkgexports.mjs'; | ||
|
||
if (isMainThread) { | ||
const tests = [[], ['--no-addons']]; | ||
|
||
for (const execArgv of tests) { | ||
new Worker(fileURLToPath(import.meta.url), { execArgv }); | ||
} | ||
} else { | ||
[requireFixture, importFixture].forEach((loadFixture) => { | ||
loadFixture('pkgexports/no-addons').then( | ||
mustCall((module) => { | ||
const message = module.default; | ||
|
||
if (process.execArgv.length === 0) { | ||
assert.strictEqual(message, 'using native addons'); | ||
} else { | ||
assert.strictEqual(message, 'not using native addons'); | ||
} | ||
}) | ||
); | ||
}); | ||
} |
10 changes: 8 additions & 2 deletions
10
test/fixtures/es-module-loaders/loader-with-custom-condition.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.