2015-05-22 09:52:05 -05:00
|
|
|
'use strict';
|
2020-03-29 00:01:29 -05:00
|
|
|
// Flags: --expose-internals
|
2015-05-22 09:52:05 -05:00
|
|
|
|
2017-02-10 12:08:49 -08:00
|
|
|
const common = require('../common');
|
2016-01-28 23:02:57 -08:00
|
|
|
const assert = require('assert');
|
2019-03-20 12:15:06 +01:00
|
|
|
const getValidStdio = require('internal/child_process').getValidStdio;
|
2015-05-22 09:52:05 -05:00
|
|
|
|
2020-08-08 19:01:59 +03:00
|
|
|
const expectedError = { code: 'ERR_INVALID_ARG_VALUE', name: 'TypeError' };
|
2017-02-10 12:08:49 -08:00
|
|
|
|
2018-12-10 13:27:32 +01:00
|
|
|
// Should throw if string and not ignore, pipe, or inherit
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => getValidStdio('foo'), expectedError);
|
2015-05-22 09:52:05 -05:00
|
|
|
|
2019-03-07 01:03:53 +01:00
|
|
|
// Should throw if not a string or array
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => getValidStdio(600), expectedError);
|
2015-05-22 09:52:05 -05:00
|
|
|
|
2019-01-21 01:22:27 +01:00
|
|
|
// Should populate stdio with undefined if len < 3
|
2016-01-28 23:02:57 -08:00
|
|
|
{
|
|
|
|
const stdio1 = [];
|
2019-03-20 12:15:06 +01:00
|
|
|
const result = getValidStdio(stdio1, false);
|
2016-12-01 09:56:23 -06:00
|
|
|
assert.strictEqual(stdio1.length, 3);
|
2022-01-24 23:03:17 -08:00
|
|
|
assert.strictEqual(Object.hasOwn(result, 'stdio'), true);
|
|
|
|
assert.strictEqual(Object.hasOwn(result, 'ipc'), true);
|
|
|
|
assert.strictEqual(Object.hasOwn(result, 'ipcFd'), true);
|
2016-01-28 23:02:57 -08:00
|
|
|
}
|
2015-05-22 09:52:05 -05:00
|
|
|
|
2019-01-21 01:22:27 +01:00
|
|
|
// Should throw if stdio has ipc and sync is true
|
2016-01-28 23:02:57 -08:00
|
|
|
const stdio2 = ['ipc', 'ipc', 'ipc'];
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => getValidStdio(stdio2, true),
|
|
|
|
{ code: 'ERR_IPC_SYNC_FORK', name: 'Error' }
|
2017-12-06 14:07:52 +05:30
|
|
|
);
|
2015-09-07 12:25:25 -05:00
|
|
|
|
2019-02-24 00:22:46 -05:00
|
|
|
// Should throw if stdio is not a valid input
|
|
|
|
{
|
|
|
|
const stdio = ['foo'];
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => getValidStdio(stdio, false),
|
|
|
|
{ code: 'ERR_INVALID_SYNC_FORK_INPUT', name: 'TypeError' }
|
2019-02-24 00:22:46 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should throw if stdio is not a valid option
|
|
|
|
{
|
|
|
|
const stdio = [{ foo: 'bar' }];
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => getValidStdio(stdio), expectedError);
|
2019-02-24 00:22:46 -05:00
|
|
|
}
|
2018-05-18 01:20:25 +02:00
|
|
|
|
2025-01-22 14:19:38 -08:00
|
|
|
const { isMainThread } = require('worker_threads');
|
|
|
|
|
|
|
|
if (isMainThread) {
|
2016-01-28 23:02:57 -08:00
|
|
|
const stdio3 = [process.stdin, process.stdout, process.stderr];
|
2019-03-20 12:15:06 +01:00
|
|
|
const result = getValidStdio(stdio3, false);
|
2016-01-28 23:02:57 -08:00
|
|
|
assert.deepStrictEqual(result, {
|
|
|
|
stdio: [
|
|
|
|
{ type: 'fd', fd: 0 },
|
|
|
|
{ type: 'fd', fd: 1 },
|
2021-03-26 08:51:08 -07:00
|
|
|
{ type: 'fd', fd: 2 },
|
2016-01-28 23:02:57 -08:00
|
|
|
],
|
|
|
|
ipc: undefined,
|
|
|
|
ipcFd: undefined
|
|
|
|
});
|
2018-05-18 01:20:25 +02:00
|
|
|
} else {
|
|
|
|
common.printSkipMessage(
|
|
|
|
'stdio is not associated with file descriptors in Workers');
|
2016-01-28 23:02:57 -08:00
|
|
|
}
|