Skip to content
Myr

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 file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–3why, one step back
1import {Buffer} from 'node:buffer';
2import {isUint8Array} from '../utils/uint-array.js';3

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 4why, one step back
4// Validate the type of chunk argument passed to transform generators

Rename files (#977)ehmickybefore adoption4b2e316

lines 5–16why, one step back
5export const getValidateTransformInput = (writableObjectMode, optionName) => writableObjectMode
6 ? 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};16

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 17why, one step back
17// Validate the type of the value returned by transform generators

Rename files (#977)ehmickybefore adoption4b2e316

lines 18–30why, one step back
18export const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode
19 ? 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 back
31 throw new TypeError(`The \`${optionName}\` option's function must yield a string or an Uint8Array, not ${typeof chunk}.`);

Rename files (#977)ehmickybefore adoption4b2e316

lines 32–43why, one step back
32 }
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};