From 441d9de33e02a62b416527de039e8f4f59d22f34 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Sun, 1 Jan 2023 15:33:41 +0900 Subject: [PATCH] stream: refactor to use `validateFunction` PR-URL: https://github.com/nodejs/node/pull/46007 Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Antoine du Hamel Reviewed-By: Yagiz Nizipli --- lib/internal/webstreams/adapters.js | 4 ++-- lib/internal/webstreams/util.js | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 02411a07a436a5..2ebc18d7645cc9 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -64,6 +64,7 @@ const { const { validateBoolean, + validateFunction, validateObject, } = require('internal/validators'); @@ -924,8 +925,7 @@ function newReadableStreamFromStreamBase(streamBase, strategy, options = kEmptyO if (typeof streamBase.onread === 'function') throw new ERR_INVALID_STATE('StreamBase already has a consumer'); - if (typeof ondone !== 'function') - throw new ERR_INVALID_ARG_TYPE('options.ondone', 'Function', ondone); + validateFunction(ondone, 'options.ondone'); let controller; diff --git a/lib/internal/webstreams/util.js b/lib/internal/webstreams/util.js index 9f978ef3ec50ca..fe1202ec00bce3 100644 --- a/lib/internal/webstreams/util.js +++ b/lib/internal/webstreams/util.js @@ -19,7 +19,6 @@ const { const { codes: { - ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_OPERATION_FAILED, }, @@ -48,6 +47,10 @@ const { const assert = require('internal/assert'); const { isArrayBufferDetached } = require('internal/util'); +const { + validateFunction, +} = require('internal/validators'); + const kState = Symbol('kState'); const kType = Symbol('kType'); @@ -78,8 +81,7 @@ function extractHighWaterMark(value, defaultHWM) { function extractSizeAlgorithm(size) { if (size === undefined) return () => 1; - if (typeof size !== 'function') - throw new ERR_INVALID_ARG_TYPE('strategy.size', 'Function', size); + validateFunction(size, 'strategy.size'); return size; }