validate.js
This file
Path
lib/transform/validate.js
43 lines · 1.7 KiB · JavaScript
Last changed by
Allow passing `Buffer` to the `input` option (#1029)ehmickybefore adoption40fdc75
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–30why, one step back1import {Buffer} from 'node:buffer';2import {isUint8Array} from '../utils/uint-array.js';34// Validate the type of chunk argument passed to transform generators5export const getValidateTransformInput = (writableObjectMode, optionName) => writableObjectMode6 ? undefined7 : validateStringTransformInput.bind(undefined, optionName);89const validateStringTransformInput = function * (optionName, chunk) {10 if (typeof chunk !== 'string' && !isUint8Array(chunk) && !Buffer.isBuffer(chunk)) {11 throw new TypeError(`The \`${optionName}\` option's transform must use "objectMode: true" to receive as input: ${typeof chunk}.`);12 }1314 yield chunk;15};1617// Validate the type of the value returned by transform generators18export const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode19 ? validateObjectTransformReturn.bind(undefined, optionName)20 : validateStringTransformReturn.bind(undefined, optionName);2122const validateObjectTransformReturn = function * (optionName, chunk) {23 validateEmptyReturn(optionName, chunk);24 yield chunk;25};2627const validateStringTransformReturn = function * (optionName, chunk) {28 validateEmptyReturn(optionName, chunk);2930 if (typeof chunk !== 'string' && !isUint8Array(chunk)) {Allow passing `Buffer` to the `input` option (#1029)ehmickybefore adoption40fdc75
line 31why, one step back31 throw new TypeError(`The \`${optionName}\` option's function must yield a string or an Uint8Array, not ${typeof chunk}.`);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 32–43why, one step back32 }3334 yield chunk;35};3637const validateEmptyReturn = (optionName, chunk) => {38 if (chunk === null || chunk === undefined) {39 throw new TypeError(`The \`${optionName}\` option's function must not call \`yield ${chunk}\`.40Instead, \`yield\` should either be called with a value, or not be called at all. For example:41 if (condition) { yield value; }`);42 }43};