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

feat: enable manual identify #1784

Merged
merged 12 commits into from
Jul 20, 2023
19 changes: 12 additions & 7 deletions src/identify/identify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const defaultValues = {
maxPushIncomingStreams: 1,
maxPushOutgoingStreams: 1,
maxObservedAddresses: 10,
maxIdentifyMessageSize: 8192
maxIdentifyMessageSize: 8192,
autodialOnConnectionOpen: true
maschad marked this conversation as resolved.
Show resolved Hide resolved
}

export class DefaultIdentifyService implements Startable {
Expand Down Expand Up @@ -101,11 +102,13 @@ export class DefaultIdentifyService implements Startable {
agentVersion: init.agentVersion ?? defaultValues.agentVersion
}

// When a new connection happens, trigger identify
components.events.addEventListener('connection:open', (evt) => {
const connection = evt.detail
this.identify(connection).catch(err => { log.error('error during identify trigged by connection:open', err) })
})
if (init.autodialOnConnectionOpen ?? defaultValues.autodialOnConnectionOpen) {
// When a new connection happens, trigger identify
components.events.addEventListener('connection:open', (evt) => {
const connection = evt.detail
this.identify(connection).catch(err => { log.error('error during identify trigged by connection:open', err) })
})
}

// When self peer record changes, trigger identify-push
components.events.addEventListener('self:peer:update', (evt) => {
Expand Down Expand Up @@ -297,7 +300,7 @@ export class DefaultIdentifyService implements Startable {
}
}

async identify (connection: Connection, options: AbortOptions = {}): Promise<void> {
async identify (connection: Connection, options: AbortOptions = {}): Promise<IdentifyResult> {
maschad marked this conversation as resolved.
Show resolved Hide resolved
const message = await this._identify(connection, options)
const {
publicKey,
Expand Down Expand Up @@ -345,6 +348,8 @@ export class DefaultIdentifyService implements Startable {
}

this.events.safeDispatchEvent('peer:identify', { detail: result })

return result
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/identify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export interface IdentifyServiceInit {
maxPushIncomingStreams?: number
maxPushOutgoingStreams?: number
maxObservedAddresses?: number

/**
* Whether to automatically dial identify on newly opened connections (default: true)
*/
autodialOnConnectionOpen?: boolean
}

export interface IdentifyServiceComponents {
Expand Down