From 8b7336b0728d1dacbe616db8d6007f5bdebc1f0f Mon Sep 17 00:00:00 2001 From: ZiJian Liu Date: Tue, 22 Dec 2020 23:23:23 +0800 Subject: [PATCH] quic,timers: refactor to use validateAbortSignal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/36604 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Rich Trott Reviewed-By: Juan José Arboleda Reviewed-By: Richard Lau --- lib/internal/quic/core.js | 7 +++---- lib/timers/promises.js | 28 ++++++++++------------------ 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/lib/internal/quic/core.js b/lib/internal/quic/core.js index d6f94ed831a982..0c326872a7a2ea 100644 --- a/lib/internal/quic/core.js +++ b/lib/internal/quic/core.js @@ -204,6 +204,7 @@ const { } = require('internal/histogram'); const { + validateAbortSignal, validateBoolean, validateInteger, validateObject, @@ -680,8 +681,7 @@ class QuicEndpoint { return this.address; const { signal } = { ...options }; - if (signal != null && !('aborted' in signal)) - throw new ERR_INVALID_ARG_TYPE('options.signal', 'AbortSignal', signal); + validateAbortSignal(signal, 'options.signal'); // If an AbortSignal was passed in, check to make sure it is not already // aborted before we continue on to do any work. @@ -1083,8 +1083,7 @@ class QuicSocket extends EventEmitter { return; const { signal } = { ...options }; - if (signal != null && !('aborted' in signal)) - throw new ERR_INVALID_ARG_TYPE('options.signal', 'AbortSignal', signal); + validateAbortSignal(signal, 'options.signal'); // If an AbotSignal was passed in, check to make sure it is not already // aborted before we continue on to do any work. diff --git a/lib/timers/promises.js b/lib/timers/promises.js index 76713bcf603342..55d554bb838e95 100644 --- a/lib/timers/promises.js +++ b/lib/timers/promises.js @@ -18,6 +18,8 @@ const { codes: { ERR_INVALID_ARG_TYPE } } = require('internal/errors'); +const { validateAbortSignal } = require('internal/validators'); + function cancelListenerHandler(clear, reject) { if (!this._destroyed) { clear(this); @@ -35,15 +37,10 @@ function setTimeout(after, value, options = {}) { options)); } const { signal, ref = true } = options; - if (signal !== undefined && - (signal === null || - typeof signal !== 'object' || - !('aborted' in signal))) { - return PromiseReject( - new ERR_INVALID_ARG_TYPE( - 'options.signal', - 'AbortSignal', - signal)); + try { + validateAbortSignal(signal, 'options.signal'); + } catch (err) { + return PromiseReject(err); } if (typeof ref !== 'boolean') { return PromiseReject( @@ -85,15 +82,10 @@ function setImmediate(value, options = {}) { options)); } const { signal, ref = true } = options; - if (signal !== undefined && - (signal === null || - typeof signal !== 'object' || - !('aborted' in signal))) { - return PromiseReject( - new ERR_INVALID_ARG_TYPE( - 'options.signal', - 'AbortSignal', - signal)); + try { + validateAbortSignal(signal, 'options.signal'); + } catch (err) { + return PromiseReject(err); } if (typeof ref !== 'boolean') { return PromiseReject(