Skip to content

Commit

Permalink
fix: fix restify instrumentation utils that might accept undefined ty…
Browse files Browse the repository at this point in the history
…pes (#690)
  • Loading branch information
faizhasim authored Oct 12, 2021
1 parent aeadca8 commit bc11f3d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
20 changes: 8 additions & 12 deletions plugins/node/opentelemetry-instrumentation-restify/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,20 @@
*/

// util.types.isPromise is supported from 10.0.0
export const isPromise = (value: any): value is Promise<unknown> => {
if (
export const isPromise = (value?: any): value is Promise<unknown> => {
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;
);
};
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit bc11f3d

Please sign in to comment.