Skip to content
Myr

validate.js

This file

Path

lib/stdio/validate.js

42 lines · 1.6 KiB · JavaScript

Last changed by

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

History of this file

Take it

Use `Uint8Array` instead of `Buffer` (#915)ehmickybefore adoption410bac3

line 1why, one step back
1import {Buffer} from 'node:buffer';

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 2–14why, one step back
2import {isUint8Array} from './uint-array.js';
34export const getValidateTransformInput = (writableObjectMode, optionName) => writableObjectMode5 ? undefined6 : validateStringTransformInput.bind(undefined, optionName);78const validateStringTransformInput = function * (optionName, chunk) {9 if (typeof chunk !== 'string' && !isUint8Array(chunk) && !Buffer.isBuffer(chunk)) {10 throw new TypeError(`The \`${optionName}\` option's transform must use "objectMode: true" to receive as input: ${typeof chunk}.`);11 }1213 yield chunk;14};

Split files (#837)ehmickybefore adoption672c2d7

lines 15–27why, one step back
15
16export const getValidateTransformReturn = (readableObjectMode, optionName) => readableObjectMode17 ? validateObjectTransformReturn.bind(undefined, optionName)18 : validateStringTransformReturn.bind(undefined, optionName);1920const validateObjectTransformReturn = function * (optionName, chunk) {21 validateEmptyReturn(optionName, chunk);22 yield chunk;23};2425const validateStringTransformReturn = function * (optionName, chunk) {26 validateEmptyReturn(optionName, chunk);27

Use `Uint8Array` instead of `Buffer` (#915)ehmickybefore adoption410bac3

lines 28–30why, one step back
28 if (typeof chunk !== 'string' && !isUint8Array(chunk)) {
29 const typeName = Buffer.isBuffer(chunk) ? 'a buffer' : typeof chunk;30 throw new TypeError(`The \`${optionName}\` option's function must yield a string or an Uint8Array, not ${typeName}.`);

Split files (#837)ehmickybefore adoption672c2d7

lines 31–42why, one step back
31 }
3233 yield chunk;34};3536const validateEmptyReturn = (optionName, chunk) => {37 if (chunk === null || chunk === undefined) {38 throw new TypeError(`The \`${optionName}\` option's function must not call \`yield ${chunk}\`.39Instead, \`yield\` should either be called with a value, or not be called at all. For example:40 if (condition) { yield value; }`);41 }42};