object-mode.js
This file
Path
lib/transform/object-mode.js
41 lines · 1.8 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–41why, one step back1import {TRANSFORM_TYPES} from '../stdio/type.js';23/*4Retrieve the `objectMode`s of a single transform.5`objectMode` determines the return value's type, i.e. the `readableObjectMode`.6The chunk argument's type is based on the previous generator's return value, i.e. the `writableObjectMode` is based on the previous `readableObjectMode`.7The last input's generator is read by `subprocess.stdin` which:8- should not be in `objectMode` for performance reasons.9- can only be strings, Buffers and Uint8Arrays.10Therefore its `readableObjectMode` must be `false`.11The same applies to the first output's generator's `writableObjectMode`.12*/13export const getTransformObjectModes = (objectMode, index, newTransforms, direction) => direction === 'output'14 ? getOutputObjectModes(objectMode, index, newTransforms)15 : getInputObjectModes(objectMode, index, newTransforms);1617const getOutputObjectModes = (objectMode, index, newTransforms) => {18 const writableObjectMode = index !== 0 && newTransforms[index - 1].value.readableObjectMode;19 const readableObjectMode = objectMode ?? writableObjectMode;20 return {writableObjectMode, readableObjectMode};21};2223const getInputObjectModes = (objectMode, index, newTransforms) => {24 const writableObjectMode = index === 025 ? objectMode === true26 : newTransforms[index - 1].value.readableObjectMode;27 const readableObjectMode = index !== newTransforms.length - 1 && (objectMode ?? writableObjectMode);28 return {writableObjectMode, readableObjectMode};29};3031// Retrieve the `objectMode` of a file descriptor, e.g. `stdout` or `stderr`32export const getFdObjectMode = (stdioItems, direction) => {33 const lastTransform = stdioItems.findLast(({type}) => TRANSFORM_TYPES.has(type));34 if (lastTransform === undefined) {35 return false;36 }3738 return direction === 'input'39 ? lastTransform.value.writableObjectMode40 : lastTransform.value.readableObjectMode;41};