From 98c162c3fcf5124078a3ebd4a58f2bc6d4646e94 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Wed, 19 Jun 2019 10:00:00 +0200 Subject: [PATCH 1/4] Do not allow `stdin: 0` combined with `stdio` --- lib/stdio.js | 14 +++++++------- test/stdio.js | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/stdio.js b/lib/stdio.js index d9f5458b4d..127a85d9c7 100644 --- a/lib/stdio.js +++ b/lib/stdio.js @@ -1,15 +1,15 @@ 'use strict'; -const alias = ['stdin', 'stdout', 'stderr']; +const aliases = ['stdin', 'stdout', 'stderr']; -const hasAlias = opts => alias.some(x => Boolean(opts[x])); +const hasAlias = opts => aliases.some(alias => opts[alias] !== undefined); const stdio = opts => { if (!opts) { return; } - if (opts.stdio && hasAlias(opts)) { - throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${alias.map(x => `\`${x}\``).join(', ')}`); + if (opts.stdio !== undefined && hasAlias(opts)) { + throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${aliases.map(alias => `\`${alias}\``).join(', ')}`); } if (typeof opts.stdio === 'string') { @@ -23,15 +23,15 @@ const stdio = opts => { } const result = []; - const len = Math.max(stdio.length, alias.length); + const len = Math.max(stdio.length, aliases.length); for (let i = 0; i < len; i++) { let value; if (stdio[i] !== undefined) { value = stdio[i]; - } else if (opts[alias[i]] !== undefined) { - value = opts[alias[i]]; + } else if (opts[aliases[i]] !== undefined) { + value = opts[aliases[i]]; } result[i] = value; diff --git a/test/stdio.js b/test/stdio.js index a9ccb03256..9c3610fd62 100644 --- a/test/stdio.js +++ b/test/stdio.js @@ -56,6 +56,7 @@ test(stdioMacro, {stdio: {foo: 'bar'}}, new TypeError('Expected `stdio` to be of test(stdioMacro, {stdin: 'inherit', stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`')); test(stdioMacro, {stdin: 'inherit', stdio: ['pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`')); test(stdioMacro, {stdin: 'inherit', stdio: [undefined, 'pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`')); +test(stdioMacro, {stdin: 0, stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`')); const forkMacro = createMacro(stdio.node); From d51ca80e4fd3fd06b3fbfa4f3c953f8c9fccf773 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Wed, 19 Jun 2019 10:00:00 +0200 Subject: [PATCH 2/4] Fix test --- lib/stdio.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/stdio.js b/lib/stdio.js index 127a85d9c7..d42c633e2d 100644 --- a/lib/stdio.js +++ b/lib/stdio.js @@ -8,7 +8,7 @@ const stdio = opts => { return; } - if (opts.stdio !== undefined && hasAlias(opts)) { + if (opts.stdio && hasAlias(opts)) { throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${aliases.map(alias => `\`${alias}\``).join(', ')}`); } From e19cdaf8e7cb1b5e70531d8fc228493add533baf Mon Sep 17 00:00:00 2001 From: ehmicky Date: Wed, 19 Jun 2019 10:00:00 +0200 Subject: [PATCH 3/4] Refactor `stdio` option --- lib/stdio.js | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/lib/stdio.js b/lib/stdio.js index d42c633e2d..fcc3a15128 100644 --- a/lib/stdio.js +++ b/lib/stdio.js @@ -8,36 +8,26 @@ const stdio = opts => { return; } - if (opts.stdio && hasAlias(opts)) { - throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${aliases.map(alias => `\`${alias}\``).join(', ')}`); + const {stdio} = opts; + + if (stdio === undefined) { + return aliases.map(alias => opts[alias]); } - if (typeof opts.stdio === 'string') { - return opts.stdio; + if (hasAlias(opts)) { + throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${aliases.map(alias => `\`${alias}\``).join(', ')}`); } - const stdio = opts.stdio || []; + if (typeof stdio === 'string') { + return stdio; + } if (!Array.isArray(stdio)) { throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``); } - const result = []; - const len = Math.max(stdio.length, aliases.length); - - for (let i = 0; i < len; i++) { - let value; - - if (stdio[i] !== undefined) { - value = stdio[i]; - } else if (opts[aliases[i]] !== undefined) { - value = opts[aliases[i]]; - } - - result[i] = value; - } - - return result; + const length = Math.max(stdio.length, aliases.length); + return Array.from({length}, (value, index) => stdio[index]); }; module.exports = stdio; From c71d00f36fd111c73927a147eca0ff373fc77551 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Wed, 19 Jun 2019 10:00:00 +0200 Subject: [PATCH 4/4] Separate from other PR --- lib/stdio.js | 2 +- test/stdio.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/stdio.js b/lib/stdio.js index fcc3a15128..bef74aeead 100644 --- a/lib/stdio.js +++ b/lib/stdio.js @@ -1,7 +1,7 @@ 'use strict'; const aliases = ['stdin', 'stdout', 'stderr']; -const hasAlias = opts => aliases.some(alias => opts[alias] !== undefined); +const hasAlias = opts => aliases.some(alias => Boolean(opts[alias])); const stdio = opts => { if (!opts) { diff --git a/test/stdio.js b/test/stdio.js index 9c3610fd62..a9ccb03256 100644 --- a/test/stdio.js +++ b/test/stdio.js @@ -56,7 +56,6 @@ test(stdioMacro, {stdio: {foo: 'bar'}}, new TypeError('Expected `stdio` to be of test(stdioMacro, {stdin: 'inherit', stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`')); test(stdioMacro, {stdin: 'inherit', stdio: ['pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`')); test(stdioMacro, {stdin: 'inherit', stdio: [undefined, 'pipe']}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`')); -test(stdioMacro, {stdin: 0, stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`')); const forkMacro = createMacro(stdio.node);