Do not allow returning `null` from transforms (#737)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 85a5882 · Browse files · 2 files·+31 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+31 added−15 removed
- tests:+18 added−12 removed
- other:+13 added−3 removed
Provenance
- Commit
a5761a8- Tree
9e6d6bb- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
85a5882
2 files changed+31 added−15 removed
modified:lib/stdio/generator.js+13 added-3 removed
80 unmodified lines8182838485868586878888899091929394959697989910010110210321 unmodified lines80 unmodified lines return {value: duplexStream};};
const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode ? undefined : validateTransformReturn.bind(undefined, optionName); ? validateObjectTransformReturn.bind(undefined, optionName) : validateStringTransformReturn.bind(undefined, optionName);
const validateTransformReturn = async function * (optionName, chunks) {const validateObjectTransformReturn = async function * (optionName, chunks) { for await (const chunk of chunks) { if (chunk === null) { throw new Error(`The \`${optionName}\` option's function must not return null.`); }
yield chunk; }};
const validateStringTransformReturn = async function * (optionName, chunks) { for await (const chunk of chunks) { if (typeof chunk !== 'string' && !isBinary(chunk)) { throw new Error(`The \`${optionName}\` option's function must return a string or an Uint8Array, not ${typeof chunk}.`); }
21 unmodified linesmodified:test/stdio/generator.js+18 added-12 removed
77 unmodified lines78 79 test('Can use generators with childProcess.stdio[*] as input', testGeneratorStdioInputPipe, false);80 test('Can use generators with childProcess.stdio[*] as input, objectMode', testGeneratorStdioInputPipe, true);81 82-const testGeneratorReturn = async (t, index, generators, fixtureName) => {83- await t.throwsAsync(84- execa(fixtureName, [`${index}`], getStdio(index, generators)),85- {message: /a string or an Uint8Array/},86- );82+// eslint-disable-next-line max-params83+const testGeneratorReturn = async (t, index, generators, fixtureName, isNull) => {84+ const childProcess = execa(fixtureName, [`${index}`], getStdio(index, generators));85+ const message = isNull ? /not return null/ : /a string or an Uint8Array/;86+ await t.throwsAsync(childProcess, {message});87 };88 89 const inputObjectGenerators = [foobarUint8Array, getOutputGenerator(foobarObject, false), serializeGenerator];90 const lastInputObjectGenerators = [foobarUint8Array, getOutputGenerator(foobarObject, true)];91 const invalidOutputObjectGenerator = getOutputGenerator(foobarObject, false);92+const inputNullGenerator = objectMode => [foobarUint8Array, getOutputGenerator(null, objectMode), serializeGenerator];93+const outputNullGenerator = objectMode => getOutputGenerator(null, objectMode);94 93-test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputObjectGenerators, 'stdin-fd.js');94-test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputObjectGenerators, 'stdin-fd.js');95-test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputObjectGenerators, 'stdin-fd.js');96-test('The last generator with result.stdio[*] as input cannot return an object even in objectMode', testGeneratorReturn, 3, lastInputObjectGenerators, 'stdin-fd.js');97-test('Generators with result.stdout cannot return an object if not in objectMode', testGeneratorReturn, 1, invalidOutputObjectGenerator, 'noop-fd.js');98-test('Generators with result.stderr cannot return an object if not in objectMode', testGeneratorReturn, 2, invalidOutputObjectGenerator, 'noop-fd.js');99-test('Generators with result.stdio[*] as output cannot return an object if not in objectMode', testGeneratorReturn, 3, invalidOutputObjectGenerator, 'noop-fd.js');95+test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputObjectGenerators, 'stdin-fd.js', false);96+test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputObjectGenerators, 'stdin-fd.js', false);97+test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputObjectGenerators, 'stdin-fd.js', false);98+test('The last generator with result.stdio[*] as input cannot return an object even in objectMode', testGeneratorReturn, 3, lastInputObjectGenerators, 'stdin-fd.js', false);99+test('Generators with result.stdout cannot return an object if not in objectMode', testGeneratorReturn, 1, invalidOutputObjectGenerator, 'noop-fd.js', false);100+test('Generators with result.stderr cannot return an object if not in objectMode', testGeneratorReturn, 2, invalidOutputObjectGenerator, 'noop-fd.js', false);101+test('Generators with result.stdio[*] as output cannot return an object if not in objectMode', testGeneratorReturn, 3, invalidOutputObjectGenerator, 'noop-fd.js', false);102+test('Generators with result.stdin cannot return null if not in objectMode', testGeneratorReturn, 0, inputNullGenerator(false), 'stdin-fd.js', false);103+test('Generators with result.stdin cannot return null if in objectMode', testGeneratorReturn, 0, inputNullGenerator(true), 'stdin-fd.js', true);104+test('Generators with result.stdout cannot return null if not in objectMode', testGeneratorReturn, 1, outputNullGenerator(false), 'noop-fd.js', false);105+test('Generators with result.stdout cannot return null if in objectMode', testGeneratorReturn, 1, outputNullGenerator(true), 'noop-fd.js', true);106 107 // eslint-disable-next-line max-params108 const testGeneratorOutput = async (t, index, reject, useShortcutProperty, objectMode) => {109 const {generator, output} = getOutputObjectMode(objectMode);550 unmodified lines