2020-03-12 13:11:48 +01:00
|
|
|
|
'use strict';
|
|
|
|
|
const common = require('../common');
|
|
|
|
|
const fixtures = require('../common/fixtures');
|
2022-12-12 18:18:37 +01:00
|
|
|
|
const tmpdir = require('../common/tmpdir');
|
2020-03-12 13:11:48 +01:00
|
|
|
|
const assert = require('assert');
|
2023-09-06 01:40:59 +02:00
|
|
|
|
const {
|
2024-03-20 19:44:14 +01:00
|
|
|
|
spawnSyncAndAssert,
|
2023-09-06 01:40:59 +02:00
|
|
|
|
spawnSyncAndExit,
|
|
|
|
|
spawnSyncAndExitWithoutError,
|
|
|
|
|
} = require('../common/child_process');
|
2020-03-12 13:11:48 +01:00
|
|
|
|
const path = require('path');
|
2022-12-12 18:18:37 +01:00
|
|
|
|
const fs = require('fs');
|
2024-08-27 23:30:50 +02:00
|
|
|
|
const os = require('os');
|
2020-03-12 13:11:48 +01:00
|
|
|
|
|
2022-12-12 18:18:37 +01:00
|
|
|
|
tmpdir.refresh();
|
2020-03-12 13:11:48 +01:00
|
|
|
|
common.allowGlobals(global.require);
|
2020-06-19 17:20:53 +02:00
|
|
|
|
common.allowGlobals(global.embedVars);
|
2022-12-12 18:18:37 +01:00
|
|
|
|
|
2023-12-05 19:28:17 +01:00
|
|
|
|
function resolveBuiltBinary(binary) {
|
2022-12-12 18:18:37 +01:00
|
|
|
|
if (common.isWindows) {
|
|
|
|
|
binary += '.exe';
|
|
|
|
|
}
|
2023-12-05 19:28:17 +01:00
|
|
|
|
return path.join(path.dirname(process.execPath), binary);
|
2020-03-12 13:11:48 +01:00
|
|
|
|
}
|
2022-12-12 18:18:37 +01:00
|
|
|
|
|
|
|
|
|
const binary = resolveBuiltBinary('embedtest');
|
2020-03-12 13:11:48 +01:00
|
|
|
|
|
2024-03-20 19:44:14 +01:00
|
|
|
|
spawnSyncAndAssert(
|
2023-09-06 01:40:59 +02:00
|
|
|
|
binary,
|
|
|
|
|
['console.log(42)'],
|
|
|
|
|
{
|
|
|
|
|
trim: true,
|
|
|
|
|
stdout: '42',
|
|
|
|
|
});
|
2020-03-12 13:11:48 +01:00
|
|
|
|
|
2024-03-20 19:44:14 +01:00
|
|
|
|
spawnSyncAndAssert(
|
2023-09-06 01:40:59 +02:00
|
|
|
|
binary,
|
|
|
|
|
['console.log(embedVars.nön_ascıı)'],
|
|
|
|
|
{
|
|
|
|
|
trim: true,
|
|
|
|
|
stdout: '🏳️🌈',
|
|
|
|
|
});
|
2020-03-12 13:11:48 +01:00
|
|
|
|
|
2023-09-06 01:40:59 +02:00
|
|
|
|
spawnSyncAndExit(
|
|
|
|
|
binary,
|
|
|
|
|
['throw new Error()'],
|
|
|
|
|
{
|
|
|
|
|
status: 1,
|
|
|
|
|
signal: null,
|
|
|
|
|
});
|
2023-03-02 15:31:08 +01:00
|
|
|
|
|
2023-09-06 01:40:59 +02:00
|
|
|
|
spawnSyncAndExit(
|
|
|
|
|
binary,
|
|
|
|
|
['require("lib/internal/test/binding")'],
|
|
|
|
|
{
|
|
|
|
|
status: 1,
|
|
|
|
|
signal: null,
|
|
|
|
|
});
|
2020-03-12 13:11:48 +01:00
|
|
|
|
|
2023-09-06 01:40:59 +02:00
|
|
|
|
spawnSyncAndExit(
|
|
|
|
|
binary,
|
|
|
|
|
['process.exitCode = 8'],
|
|
|
|
|
{
|
|
|
|
|
status: 8,
|
|
|
|
|
signal: null,
|
|
|
|
|
});
|
2020-03-12 13:11:48 +01:00
|
|
|
|
|
|
|
|
|
const fixturePath = JSON.stringify(fixtures.path('exit.js'));
|
2023-09-06 01:40:59 +02:00
|
|
|
|
spawnSyncAndExit(
|
|
|
|
|
binary,
|
|
|
|
|
[`require(${fixturePath})`, 92],
|
|
|
|
|
{
|
|
|
|
|
status: 92,
|
|
|
|
|
signal: null,
|
|
|
|
|
});
|
2022-12-12 18:18:37 +01:00
|
|
|
|
|
2023-01-24 21:35:52 +01:00
|
|
|
|
function getReadFileCodeForPath(path) {
|
|
|
|
|
return `(require("fs").readFileSync(${JSON.stringify(path)}, "utf8"))`;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 18:18:37 +01:00
|
|
|
|
// Basic snapshot support
|
2023-10-28 19:05:59 +02:00
|
|
|
|
for (const extraSnapshotArgs of [
|
|
|
|
|
[], ['--embedder-snapshot-as-file'], ['--without-code-cache'],
|
|
|
|
|
]) {
|
2023-01-24 21:35:52 +01:00
|
|
|
|
// readSync + eval since snapshots don't support userland require() (yet)
|
2022-12-12 18:18:37 +01:00
|
|
|
|
const snapshotFixture = fixtures.path('snapshot', 'echo-args.js');
|
2023-08-21 18:41:53 +02:00
|
|
|
|
const blobPath = tmpdir.resolve('embedder-snapshot.blob');
|
2023-09-06 01:40:59 +02:00
|
|
|
|
const buildSnapshotExecArgs = [
|
2023-01-24 21:35:52 +01:00
|
|
|
|
`eval(${getReadFileCodeForPath(snapshotFixture)})`, 'arg1', 'arg2',
|
2023-09-06 01:40:59 +02:00
|
|
|
|
];
|
|
|
|
|
const embedTestBuildArgs = [
|
2023-01-24 21:35:52 +01:00
|
|
|
|
'--embedder-snapshot-blob', blobPath, '--embedder-snapshot-create',
|
2023-02-06 20:57:19 +01:00
|
|
|
|
...extraSnapshotArgs,
|
|
|
|
|
];
|
2023-09-06 01:40:59 +02:00
|
|
|
|
const buildSnapshotArgs = [
|
|
|
|
|
...buildSnapshotExecArgs,
|
|
|
|
|
...embedTestBuildArgs,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const runSnapshotExecArgs = [
|
|
|
|
|
'arg3', 'arg4',
|
|
|
|
|
];
|
|
|
|
|
const embedTestRunArgs = [
|
|
|
|
|
'--embedder-snapshot-blob', blobPath,
|
|
|
|
|
...extraSnapshotArgs,
|
|
|
|
|
];
|
|
|
|
|
const runSnapshotArgs = [
|
|
|
|
|
...runSnapshotExecArgs,
|
|
|
|
|
...embedTestRunArgs,
|
2023-01-24 21:35:52 +01:00
|
|
|
|
];
|
2022-12-12 18:18:37 +01:00
|
|
|
|
|
|
|
|
|
fs.rmSync(blobPath, { force: true });
|
2023-09-06 01:40:59 +02:00
|
|
|
|
spawnSyncAndExitWithoutError(
|
|
|
|
|
binary,
|
|
|
|
|
[ '--', ...buildSnapshotArgs ],
|
2024-03-20 19:44:14 +01:00
|
|
|
|
{ cwd: tmpdir.path });
|
|
|
|
|
spawnSyncAndAssert(
|
2023-09-06 01:40:59 +02:00
|
|
|
|
binary,
|
|
|
|
|
[ '--', ...runSnapshotArgs ],
|
|
|
|
|
{ cwd: tmpdir.path },
|
|
|
|
|
{
|
|
|
|
|
stdout(output) {
|
|
|
|
|
assert.deepStrictEqual(JSON.parse(output), {
|
|
|
|
|
originalArgv: [binary, '__node_anonymous_main', ...buildSnapshotExecArgs],
|
|
|
|
|
currentArgv: [binary, ...runSnapshotExecArgs],
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-12-12 18:18:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create workers and vm contexts after deserialization
|
|
|
|
|
{
|
|
|
|
|
const snapshotFixture = fixtures.path('snapshot', 'create-worker-and-vm.js');
|
2023-08-21 18:41:53 +02:00
|
|
|
|
const blobPath = tmpdir.resolve('embedder-snapshot.blob');
|
2023-01-24 21:35:52 +01:00
|
|
|
|
const buildSnapshotArgs = [
|
|
|
|
|
`eval(${getReadFileCodeForPath(snapshotFixture)})`,
|
|
|
|
|
'--embedder-snapshot-blob', blobPath, '--embedder-snapshot-create',
|
|
|
|
|
];
|
2023-09-06 01:40:59 +02:00
|
|
|
|
const runEmbeddedArgs = [
|
|
|
|
|
'--embedder-snapshot-blob', blobPath,
|
|
|
|
|
];
|
2022-12-12 18:18:37 +01:00
|
|
|
|
|
|
|
|
|
fs.rmSync(blobPath, { force: true });
|
2023-09-06 01:40:59 +02:00
|
|
|
|
|
|
|
|
|
spawnSyncAndExitWithoutError(
|
|
|
|
|
binary,
|
|
|
|
|
[ '--', ...buildSnapshotArgs ],
|
2024-03-20 19:44:14 +01:00
|
|
|
|
{ cwd: tmpdir.path });
|
2023-09-06 01:40:59 +02:00
|
|
|
|
spawnSyncAndExitWithoutError(
|
|
|
|
|
binary,
|
|
|
|
|
[ '--', ...runEmbeddedArgs ],
|
2024-03-20 19:44:14 +01:00
|
|
|
|
{ cwd: tmpdir.path });
|
2022-12-12 18:18:37 +01:00
|
|
|
|
}
|
2024-05-23 20:41:55 +02:00
|
|
|
|
|
|
|
|
|
// Guarantee NODE_REPL_EXTERNAL_MODULE won't bypass kDisableNodeOptionsEnv
|
|
|
|
|
{
|
|
|
|
|
spawnSyncAndExit(
|
|
|
|
|
binary,
|
|
|
|
|
['require("os")'],
|
|
|
|
|
{
|
|
|
|
|
env: {
|
|
|
|
|
...process.env,
|
|
|
|
|
'NODE_REPL_EXTERNAL_MODULE': 'fs',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
status: 9,
|
|
|
|
|
signal: null,
|
2024-08-27 23:30:50 +02:00
|
|
|
|
stderr: `${binary}: NODE_REPL_EXTERNAL_MODULE can't be used with kDisableNodeOptionsEnv${os.EOL}`,
|
2024-05-23 20:41:55 +02:00
|
|
|
|
});
|
|
|
|
|
}
|