Skip to content

Commit a32fbeb

Browse files
fix: use isPromise/etc function from utils (#2925)
Includes improvements to detection from #2918 --------- Signed-off-by: Sacha Froment <[email protected]> Co-authored-by: Sacha Froment <[email protected]>
1 parent 15a70af commit a32fbeb

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

packages/metrics-opentelemetry/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"dependencies": {
5050
"@libp2p/interface": "^2.4.0",
51+
"@libp2p/utils": "^6.3.1",
5152
"@opentelemetry/api": "^1.9.0",
5253
"it-foreach": "^2.1.1",
5354
"it-stream-types": "^2.0.2"

packages/metrics-opentelemetry/src/index.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
*/
3535

3636
import { InvalidParametersError, serviceCapabilities } from '@libp2p/interface'
37+
import { isAsyncGenerator } from '@libp2p/utils/is-async-generator'
38+
import { isGenerator } from '@libp2p/utils/is-generator'
39+
import { isPromise } from '@libp2p/utils/is-promise'
3740
import { trace, metrics, context, SpanStatusCode } from '@opentelemetry/api'
3841
import each from 'it-foreach'
3942
import { OpenTelemetryCounterGroup } from './counter-group.js'
@@ -438,10 +441,6 @@ export function openTelemetryMetrics (init: OpenTelemetryMetricsInit = {}): (com
438441
return (components: OpenTelemetryComponents) => new OpenTelemetryMetrics(components, init)
439442
}
440443

441-
function isPromise <T = any> (obj?: any): obj is Promise<T> {
442-
return typeof obj?.then === 'function'
443-
}
444-
445444
async function wrapPromise (promise: Promise<any>, span: Span, attributes: TraceAttributes, options?: TraceFunctionOptions<any, any>): Promise<any> {
446445
return promise
447446
.then(res => {
@@ -458,10 +457,6 @@ async function wrapPromise (promise: Promise<any>, span: Span, attributes: Trace
458457
})
459458
}
460459

461-
function isGenerator (obj?: any): obj is Generator {
462-
return obj?.[Symbol.iterator] != null
463-
}
464-
465460
function wrapGenerator (gen: Generator, span: Span, attributes: TraceAttributes, options?: TraceGeneratorFunctionOptions<any, any, any>): Generator {
466461
const iter = gen[Symbol.iterator]()
467462
let index = 0
@@ -502,10 +497,6 @@ function wrapGenerator (gen: Generator, span: Span, attributes: TraceAttributes,
502497
return wrapped
503498
}
504499

505-
function isAsyncGenerator (obj?: any): obj is AsyncGenerator {
506-
return obj?.[Symbol.asyncIterator] != null
507-
}
508-
509500
function wrapAsyncGenerator (gen: AsyncGenerator, span: Span, attributes: TraceAttributes, options?: TraceGeneratorFunctionOptions<any, any, any>): AsyncGenerator {
510501
const iter = gen[Symbol.asyncIterator]()
511502
let index = 0

packages/utils/src/is-async-generator.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
export function isAsyncGenerator (obj: unknown): obj is AsyncGenerator {
2-
if (obj == null) return false
2+
if (obj == null) {
3+
return false
4+
}
5+
36
const asyncIterator = (obj as { [Symbol.asyncIterator]?: unknown })?.[
47
Symbol.asyncIterator
58
]
6-
if (typeof asyncIterator !== 'function') return false
9+
10+
if (typeof asyncIterator !== 'function') {
11+
return false
12+
}
713

814
const instance = obj as { next?: unknown }
915
return typeof instance.next === 'function'

packages/utils/src/is-generator.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
export function isGenerator (obj: unknown): obj is Generator {
2-
if (obj == null) return false
2+
if (obj == null) {
3+
return false
4+
}
5+
36
const iterator = (obj as { [Symbol.iterator]?: unknown })?.[Symbol.iterator]
4-
if (typeof iterator !== 'function') return false
7+
8+
if (typeof iterator !== 'function') {
9+
return false
10+
}
511

612
const instance = obj as { next?: unknown }
13+
714
return typeof instance.next === 'function'
815
}

0 commit comments

Comments
 (0)