Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use isPromise/etc function from utils #2925

Merged
merged 5 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
}
Loading