(stdio, sync)
| 1006 | const nop = FunctionPrototype; |
| 1007 | |
| 1008 | function getValidStdio(stdio, sync) { |
| 1009 | let ipc; |
| 1010 | let ipcFd; |
| 1011 | |
| 1012 | // Replace shortcut with an array |
| 1013 | if (typeof stdio === 'string') { |
| 1014 | stdio = stdioStringToArray(stdio); |
| 1015 | } else if (!ArrayIsArray(stdio)) { |
| 1016 | throw new ERR_INVALID_ARG_VALUE('stdio', stdio); |
| 1017 | } |
| 1018 | |
| 1019 | // At least 3 stdio will be created |
| 1020 | // Don't concat() a new Array() because it would be sparse, and |
| 1021 | // stdio.reduce() would skip the sparse elements of stdio. |
| 1022 | // See https://stackoverflow.com/a/5501711/3561 |
| 1023 | while (stdio.length < 3) ArrayPrototypePush(stdio, undefined); |
| 1024 | |
| 1025 | // Translate stdio into C++-readable form |
| 1026 | // (i.e. PipeWraps or fds) |
| 1027 | stdio = ArrayPrototypeReduce(stdio, (acc, stdio, i) => { |
| 1028 | function cleanup() { |
| 1029 | for (let i = 0; i < acc.length; i++) { |
| 1030 | if ((acc[i].type === 'pipe' || acc[i].type === 'ipc') && acc[i].handle) |
| 1031 | acc[i].handle.close(); |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | // Defaults |
| 1036 | stdio ??= i < 3 ? 'pipe' : 'ignore'; |
| 1037 | |
| 1038 | if (stdio === 'ignore') { |
| 1039 | ArrayPrototypePush(acc, { type: 'ignore' }); |
| 1040 | } else if (stdio === 'pipe' || stdio === 'overlapped' || |
| 1041 | (typeof stdio === 'number' && stdio < 0)) { |
| 1042 | const a = { |
| 1043 | type: stdio === 'overlapped' ? 'overlapped' : 'pipe', |
| 1044 | readable: i === 0, |
| 1045 | writable: i !== 0, |
| 1046 | }; |
| 1047 | |
| 1048 | if (!sync) |
| 1049 | a.handle = new Pipe(PipeConstants.SOCKET); |
| 1050 | |
| 1051 | ArrayPrototypePush(acc, a); |
| 1052 | } else if (stdio === 'ipc') { |
| 1053 | if (sync || ipc !== undefined) { |
| 1054 | // Cleanup previously created pipes |
| 1055 | cleanup(); |
| 1056 | if (!sync) |
| 1057 | throw new ERR_IPC_ONE_PIPE(); |
| 1058 | else |
| 1059 | throw new ERR_IPC_SYNC_FORK(); |
| 1060 | } |
| 1061 | |
| 1062 | ipc = new Pipe(PipeConstants.IPC); |
| 1063 | ipcFd = i; |
| 1064 | |
| 1065 | ArrayPrototypePush(acc, { |
no test coverage detected
searching dependent graphs…