Validate against `yield undefined` in transforms (#756)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 14fca86 · Browse files · 3 files·+33 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+33 added−25 removed
- tests:+20 added−19 removed
- other:+13 added−6 removed
Provenance
- Commit
192d818- Tree
5377239- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
14fca86
3 files changed+33 added−25 removed
modified:docs/transform.md+1 added-1 removed
54 unmodified lines5556575859596061626365 unmodified lines54 unmodified linesPlease note the [`lines`](../readme.md#lines) option is unrelated: it has no impact on transforms.
## Object mode
By default, `stdout` and `stderr`'s transforms must return a string or an `Uint8Array`. However, if a `{transform, objectMode: true}` plain object is passed, any type can be returned instead, except `null`. The process' [`stdout`](../readme.md#stdout)/[`stderr`](../readme.md#stderr) will be an array of values.By default, `stdout` and `stderr`'s transforms must return a string or an `Uint8Array`. However, if a `{transform, objectMode: true}` plain object is passed, any type can be returned instead, except `null` or `undefined`. The process' [`stdout`](../readme.md#stdout)/[`stderr`](../readme.md#stderr) will be an array of values.
```jsconst transform = function * (line) { yield JSON.parse(line);65 unmodified linesmodified:lib/stdio/generator.js+12 added-5 removed
84 unmodified lines85 ? validateObjectTransformReturn.bind(undefined, optionName)86 : validateStringTransformReturn.bind(undefined, optionName);87 88 const validateObjectTransformReturn = function * (optionName, chunk) {89- if (chunk === null) {90- throw new TypeError(`The \`${optionName}\` option's function must not return null.`);91- }92-89+ validateEmptyReturn(optionName, chunk);90 yield chunk;91 };92 93 const validateStringTransformReturn = function * (optionName, chunk) {94+ validateEmptyReturn(optionName, chunk);95+96 if (typeof chunk !== 'string' && !isBinary(chunk)) {98- throw new TypeError(`The \`${optionName}\` option's function must return a string or an Uint8Array, not ${typeof chunk}.`);97+ throw new TypeError(`The \`${optionName}\` option's function must yield a string or an Uint8Array, not ${typeof chunk}.`);98 }99 100 yield chunk;101 };102 103+const validateEmptyReturn = (optionName, chunk) => {104+ if (chunk === null || chunk === undefined) {105+ throw new TypeError(`The \`${optionName}\` option's function must not call \`yield ${chunk}\`.106+Instead, \`yield\` should either be called with a value, or not be called at all. For example:107+ if (condition) { yield value; }`);108+ }109+};110+111 // `childProcess.stdin|stdout|stderr|stdio` is directly mutated.112 export const pipeGenerator = (spawned, {value, direction, index}) => {113 if (direction === 'output') {114 pipeStreams(spawned.stdio[index], value);13 unmodified linesmodified:test/stdio/generator.js+20 added-19 removed
84 unmodified lines85 86 // eslint-disable-next-line max-params87 const testGeneratorReturn = async (t, index, generators, fixtureName, isNull) => {88 const childProcess = execa(fixtureName, [`${index}`], getStdio(index, generators));89- const message = isNull ? /not return null/ : /a string or an Uint8Array/;89+ const message = isNull ? /not be called at all/ : /a string or an Uint8Array/;90 await t.throwsAsync(childProcess, {message});91 };92 93-const inputObjectGenerators = [foobarUint8Array, getOutputGenerator(foobarObject, false), serializeGenerator];94-const lastInputObjectGenerators = [foobarUint8Array, getOutputGenerator(foobarObject, true)];95-const invalidOutputObjectGenerator = getOutputGenerator(foobarObject, false);96-const inputNullGenerator = objectMode => [foobarUint8Array, getOutputGenerator(null, objectMode), serializeGenerator];97-const outputNullGenerator = objectMode => getOutputGenerator(null, objectMode);93+const lastInputGenerator = (input, objectMode) => [foobarUint8Array, getOutputGenerator(input, objectMode)];94+const inputGenerator = (input, objectMode) => [...lastInputGenerator(input, objectMode), serializeGenerator];95 99-test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputObjectGenerators, 'stdin-fd.js', false);100-test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputObjectGenerators, 'stdin-fd.js', false);101-test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputObjectGenerators, 'stdin-fd.js', false);102-test('The last generator with result.stdio[*] as input cannot return an object even in objectMode', testGeneratorReturn, 3, lastInputObjectGenerators, 'stdin-fd.js', false);103-test('Generators with result.stdout cannot return an object if not in objectMode', testGeneratorReturn, 1, invalidOutputObjectGenerator, 'noop-fd.js', false);104-test('Generators with result.stderr cannot return an object if not in objectMode', testGeneratorReturn, 2, invalidOutputObjectGenerator, 'noop-fd.js', false);105-test('Generators with result.stdio[*] as output cannot return an object if not in objectMode', testGeneratorReturn, 3, invalidOutputObjectGenerator, 'noop-fd.js', false);106-test('Generators with result.stdin cannot return null if not in objectMode', testGeneratorReturn, 0, inputNullGenerator(false), 'stdin-fd.js', false);107-test('Generators with result.stdin cannot return null if in objectMode', testGeneratorReturn, 0, inputNullGenerator(true), 'stdin-fd.js', true);108-test('Generators with result.stdout cannot return null if not in objectMode', testGeneratorReturn, 1, outputNullGenerator(false), 'noop-fd.js', false);109-test('Generators with result.stdout cannot return null if in objectMode', testGeneratorReturn, 1, outputNullGenerator(true), 'noop-fd.js', true);96+test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputGenerator(foobarObject, false), 'stdin-fd.js', false);97+test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputGenerator(foobarObject, false), 'stdin-fd.js', false);98+test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputGenerator(foobarObject, true), 'stdin-fd.js', false);99+test('The last generator with result.stdio[*] as input cannot return an object even in objectMode', testGeneratorReturn, 3, lastInputGenerator(foobarObject, true), 'stdin-fd.js', false);100+test('Generators with result.stdout cannot return an object if not in objectMode', testGeneratorReturn, 1, getOutputGenerator(foobarObject, false), 'noop-fd.js', false);101+test('Generators with result.stderr cannot return an object if not in objectMode', testGeneratorReturn, 2, getOutputGenerator(foobarObject, false), 'noop-fd.js', false);102+test('Generators with result.stdio[*] as output cannot return an object if not in objectMode', testGeneratorReturn, 3, getOutputGenerator(foobarObject, false), 'noop-fd.js', false);103+test('Generators with result.stdin cannot return null if not in objectMode', testGeneratorReturn, 0, inputGenerator(null, false), 'stdin-fd.js', true);104+test('Generators with result.stdin cannot return null if in objectMode', testGeneratorReturn, 0, inputGenerator(null, true), 'stdin-fd.js', true);105+test('Generators with result.stdout cannot return null if not in objectMode', testGeneratorReturn, 1, getOutputGenerator(null, false), 'noop-fd.js', true);106+test('Generators with result.stdout cannot return null if in objectMode', testGeneratorReturn, 1, getOutputGenerator(null, true), 'noop-fd.js', true);107+test('Generators with result.stdin cannot return undefined if not in objectMode', testGeneratorReturn, 0, inputGenerator(undefined, false), 'stdin-fd.js', true);108+test('Generators with result.stdin cannot return undefined if in objectMode', testGeneratorReturn, 0, inputGenerator(undefined, true), 'stdin-fd.js', true);109+test('Generators with result.stdout cannot return undefined if not in objectMode', testGeneratorReturn, 1, getOutputGenerator(undefined, false), 'noop-fd.js', true);110+test('Generators with result.stdout cannot return undefined if in objectMode', testGeneratorReturn, 1, getOutputGenerator(undefined, true), 'noop-fd.js', true);111 112 const testGeneratorFinal = async (t, fixtureName) => {113 const {stdout} = await execa(fixtureName, {stdout: convertTransformToFinal(getOutputGenerator(foobarString), true)});114 t.is(stdout, foobarString);2 unmodified lines117 test('Generators "final" can be used', testGeneratorFinal, 'noop.js');118 test('Generators "final" is used even on empty streams', testGeneratorFinal, 'empty.js');119 120 test('Generators "final" return value is validated', async t => {120- const childProcess = execa('noop.js', {stdout: convertTransformToFinal(outputNullGenerator(true), true)});121- await t.throwsAsync(childProcess, {message: /not return null/});121+ const childProcess = execa('noop.js', {stdout: convertTransformToFinal(getOutputGenerator(null, true), true)});122+ await t.throwsAsync(childProcess, {message: /not be called at all/});123 });124 125 // eslint-disable-next-line max-params126 const testGeneratorOutput = async (t, index, reject, useShortcutProperty, objectMode) => {578 unmodified lines