From bc11f3def5c147c5d7ce0bce4a4e7b0c4c9d33fe Mon Sep 17 00:00:00 2001 From: Mohd Faiz Hasim Date: Tue, 12 Oct 2021 19:58:42 +0800 Subject: [PATCH] fix: fix restify instrumentation utils that might accept undefined types (#690) --- .../src/utils.ts | 20 +++---- .../test/utils.test.ts | 60 +++++++++++++++++++ 2 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 plugins/node/opentelemetry-instrumentation-restify/test/utils.test.ts diff --git a/plugins/node/opentelemetry-instrumentation-restify/src/utils.ts b/plugins/node/opentelemetry-instrumentation-restify/src/utils.ts index d749799e1d..036b71e0e9 100644 --- a/plugins/node/opentelemetry-instrumentation-restify/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-restify/src/utils.ts @@ -15,24 +15,20 @@ */ // util.types.isPromise is supported from 10.0.0 -export const isPromise = (value: any): value is Promise => { - if ( +export const isPromise = (value?: any): value is Promise => { + return !!( + value && typeof value.then === 'function' && typeof value.catch === 'function' && value.toString() === '[object Promise]' - ) { - return true; - } - return false; + ); }; // util.types.isAsyncFunction is supported from 10.0.0 -export const isAsyncFunction = (value: unknown) => { - if ( +export const isAsyncFunction = (value?: unknown) => { + return !!( + value && typeof value === 'function' && value.constructor?.name === 'AsyncFunction' - ) { - return true; - } - return false; + ); }; diff --git a/plugins/node/opentelemetry-instrumentation-restify/test/utils.test.ts b/plugins/node/opentelemetry-instrumentation-restify/test/utils.test.ts new file mode 100644 index 0000000000..1b2421e01c --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-restify/test/utils.test.ts @@ -0,0 +1,60 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { isAsyncFunction, isPromise } from '../src/utils'; +import * as assert from 'assert'; + +describe('utils', () => { + describe('isPromise', () => { + it('should be able to validate a promise to be true', () => { + assert.strictEqual(isPromise(Promise.resolve()), true); + }); + + it('should be able to validate non-promise to be false', () => { + assert.strictEqual(isPromise(), false); + assert.strictEqual(isPromise(null), false); + assert.strictEqual(isPromise({}), false); + assert.strictEqual(isPromise('string'), false); + assert.strictEqual(isPromise(123), false); + assert.strictEqual( + isPromise(() => {}), + false + ); + assert.strictEqual(isPromise((async () => {}) as any), false); + }); + }); + + describe('isAsyncFunction', () => { + it('should be able to validate an async function to be true', () => { + assert.strictEqual( + isAsyncFunction(async () => {}), + true + ); + }); + + it('should be able to validate non async function to be false', () => { + assert.strictEqual(isAsyncFunction(), false); + assert.strictEqual(isAsyncFunction(null), false); + assert.strictEqual(isAsyncFunction({}), false); + assert.strictEqual(isAsyncFunction('string'), false); + assert.strictEqual(isAsyncFunction(123), false); + assert.strictEqual( + isAsyncFunction(() => {}), + false + ); + assert.strictEqual(isAsyncFunction(Promise.resolve()), false); + }); + }); +});