Skip to content

Commit

Permalink
fix: use isPromise/etc function from utils (#2925)
Browse files Browse the repository at this point in the history
Includes improvements to detection from #2918

---------

Signed-off-by: Sacha Froment <[email protected]>
Co-authored-by: Sacha Froment <[email protected]>
  • Loading branch information
achingbrain and sfroment authored Feb 3, 2025
1 parent 15a70af commit a32fbeb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/metrics-opentelemetry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
},
"dependencies": {
"@libp2p/interface": "^2.4.0",
"@libp2p/utils": "^6.3.1",
"@opentelemetry/api": "^1.9.0",
"it-foreach": "^2.1.1",
"it-stream-types": "^2.0.2"
Expand Down
15 changes: 3 additions & 12 deletions packages/metrics-opentelemetry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
*/

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

function isPromise <T = any> (obj?: any): obj is Promise<T> {
return typeof obj?.then === 'function'
}

async function wrapPromise (promise: Promise<any>, span: Span, attributes: TraceAttributes, options?: TraceFunctionOptions<any, any>): Promise<any> {
return promise
.then(res => {
Expand All @@ -458,10 +457,6 @@ async function wrapPromise (promise: Promise<any>, span: Span, attributes: Trace
})
}

function isGenerator (obj?: any): obj is Generator {
return obj?.[Symbol.iterator] != null
}

function wrapGenerator (gen: Generator, span: Span, attributes: TraceAttributes, options?: TraceGeneratorFunctionOptions<any, any, any>): Generator {
const iter = gen[Symbol.iterator]()
let index = 0
Expand Down Expand Up @@ -502,10 +497,6 @@ function wrapGenerator (gen: Generator, span: Span, attributes: TraceAttributes,
return wrapped
}

function isAsyncGenerator (obj?: any): obj is AsyncGenerator {
return obj?.[Symbol.asyncIterator] != null
}

function wrapAsyncGenerator (gen: AsyncGenerator, span: Span, attributes: TraceAttributes, options?: TraceGeneratorFunctionOptions<any, any, any>): AsyncGenerator {
const iter = gen[Symbol.asyncIterator]()
let index = 0
Expand Down
10 changes: 8 additions & 2 deletions packages/utils/src/is-async-generator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
export function isAsyncGenerator (obj: unknown): obj is AsyncGenerator {
if (obj == null) return false
if (obj == null) {
return false
}

const asyncIterator = (obj as { [Symbol.asyncIterator]?: unknown })?.[
Symbol.asyncIterator
]
if (typeof asyncIterator !== 'function') return false

if (typeof asyncIterator !== 'function') {
return false
}

const instance = obj as { next?: unknown }
return typeof instance.next === 'function'
Expand Down
11 changes: 9 additions & 2 deletions packages/utils/src/is-generator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
export function isGenerator (obj: unknown): obj is Generator {
if (obj == null) return false
if (obj == null) {
return false
}

const iterator = (obj as { [Symbol.iterator]?: unknown })?.[Symbol.iterator]
if (typeof iterator !== 'function') return false

if (typeof iterator !== 'function') {
return false
}

const instance = obj as { next?: unknown }

return typeof instance.next === 'function'
}

0 comments on commit a32fbeb

Please sign in to comment.