Skip to content
Open
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
43 changes: 26 additions & 17 deletions packages/auth/src/connection/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class Connection extends EventEmitter<ConnectionEvents> {
#started = false
#log = debug.extend('auth:connection')
#sharedLogger: any | undefined
private LOG = (level: 'info' | 'warn' | 'error', message: any, ...params: any[]) => {
private LOG = (level: 'info' | 'warn' | 'error' | 'debug', message: any, ...params: any[]) => {
if (this.#sharedLogger == null) {
this.#log(message, params)
return
Expand All @@ -114,6 +114,9 @@ export class Connection extends EventEmitter<ConnectionEvents> {
case 'error':
this.#sharedLogger.error(message, ...params)
break
case 'debug':
this.#sharedLogger.debug(message, ...params)
break
default:
throw new Error(`Unknown log level ${level}`)
}
Expand Down Expand Up @@ -213,7 +216,11 @@ export class Connection extends EventEmitter<ConnectionEvents> {
const { userName, userKeys } = theirIdentityClaim
team.admitMember(proofOfInvitation, userKeys, userName)
const userId = userKeys.name
if (context.server == null && !team.hasServer(userId) && !team.hasServer(context.user?.userId!)) {
if (
context.server == null &&
!team.hasServer(userId) &&
!team.hasServer(context.user?.userId!)
) {
this.#log(userId, context.user?.userId, context.userName)
team.addMemberRole(userId, 'member')
}
Expand Down Expand Up @@ -274,7 +281,12 @@ export class Connection extends EventEmitter<ConnectionEvents> {
const { deviceId } = theirIdentityClaim
const theirDevice = team.device(deviceId, { includeRemoved: true })
const peer = team.memberByDeviceId(deviceId, { includeRemoved: true })
this.LOG('info', 'Found the following device and member information', theirDevice.deviceId, peer.userId)
this.LOG(
'info',
'Found the following device and member information',
theirDevice.deviceId,
peer.userId
)

// we now have a user name so add that to the debug logger
this.#log = this.#log.extend(peer.userName)
Expand Down Expand Up @@ -302,7 +314,7 @@ export class Connection extends EventEmitter<ConnectionEvents> {
assertEvent(event, 'PROVE_IDENTITY')
const { roles, userId } = context.peer ?? { roles: undefined, userId: undefined }
const { team } = context

assert(team)
assert(roles)
assert(userId)
Expand Down Expand Up @@ -333,10 +345,10 @@ export class Connection extends EventEmitter<ConnectionEvents> {

// Undefined message means we're already synced
if (syncMessage) {
this.LOG('info', 'sending sync message', syncMessage.head)
this.LOG('debug', 'sending sync message', syncMessage.head)
this.#queueMessage('SYNC', syncMessage)
} else {
this.LOG('info', 'no sync message to send')
this.LOG('debug', 'no sync message to send')
}

return { syncState }
Expand Down Expand Up @@ -382,7 +394,7 @@ export class Connection extends EventEmitter<ConnectionEvents> {
const recipientPublicKey = peer!.keys.encryption
const senderSecretKey = user!.keys.encryption.secretKey

this.LOG('info', `encrypting seed with key ${recipientPublicKey}`)
this.LOG('debug', `encrypting seed with key ${recipientPublicKey}`)
const encryptedSeed = asymmetric.encryptBytes({
secret: seed,
recipientPublicKey,
Expand Down Expand Up @@ -509,10 +521,7 @@ export class Connection extends EventEmitter<ConnectionEvents> {
includeRemoved: true,
})
if (deviceMissing) {
this.LOG(
'error',
`Device ${theirIdentityClaim.deviceId} was unknown`,
)
this.LOG('error', `Device ${theirIdentityClaim.deviceId} was unknown`)
}
return deviceMissing
},
Expand Down Expand Up @@ -763,7 +772,7 @@ export class Connection extends EventEmitter<ConnectionEvents> {
next: state => {
const summary = stateSummary(state.value as string)
this.emit('change', summary)
this.LOG('info', `⏩ ${JSON.stringify(state.value, null, 2)} `)
this.LOG('debug', `⏩ ${JSON.stringify(state.value, null, 2)} `)
},
error: error => {
this.LOG('error', 'Connection encountered an unhandled error', error)
Expand All @@ -773,7 +782,7 @@ export class Connection extends EventEmitter<ConnectionEvents> {

// add automatic logging to all events
this.emit = (event, ...args) => {
this.LOG('info', `emit ${event}`)
this.LOG('debug', `emit ${event}`)
return super.emit(event, ...args)
}
}
Expand All @@ -782,7 +791,7 @@ export class Connection extends EventEmitter<ConnectionEvents> {

/** Starts the state machine. Returns this Connection object. */
public start = (storedMessages: Uint8Array[] = []) => {
this.LOG('info', 'starting')
this.LOG('debug', 'starting')
this.#machine.start()
this.#messageQueue.start()
this.#started = true
Expand Down Expand Up @@ -825,7 +834,7 @@ export class Connection extends EventEmitter<ConnectionEvents> {
public send = (message: any) => {
assert(this._sessionKey, "Can't send encrypted messages until we've finished connecting")
const encryptedMessage = symmetric.encryptBytes(message, this._sessionKey)
this.LOG('info', `encrypted message with session key ${base58.encode(this._sessionKey)}`)
this.LOG('debug', `encrypted message with session key ${base58.encode(this._sessionKey)}`)
this.#queueMessage('ENCRYPTED_MESSAGE', encryptedMessage)
}

Expand Down Expand Up @@ -916,8 +925,8 @@ export class Connection extends EventEmitter<ConnectionEvents> {

#logMessage(direction: 'in' | 'out', message: NumberedMessage<ConnectionMessage>) {
const arrow = direction === 'in' ? '<-' : '->'
const peerUserName = this.#started ? this._context.peer?.userName ?? '?' : '?'
this.LOG('info', `${arrow}${peerUserName} #${message.index} ${message.type}`)
const peerUserName = this.#started ? (this._context.peer?.userName ?? '?') : '?'
this.LOG('debug', `${arrow}${peerUserName} #${message.index} ${message.type}`)
}
}

Expand Down
9 changes: 6 additions & 3 deletions packages/auth/src/connection/MessageQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class MessageQueue<T> extends EventEmitter<MessageQueueEvents<T>> {

#processOutbound() {
// send outbound messages in order
this.#LOG('info', 'processOutbound')
this.#LOG('debug', 'processOutbound')
while (this.#outbound[this.#nextOutbound]) {
const message = this.#outbound[this.#nextOutbound]
this.#sendMessage(message)
Expand All @@ -99,7 +99,7 @@ export class MessageQueue<T> extends EventEmitter<MessageQueueEvents<T>> {
* Receives any messages that are pending in the inbound queue, and requests any missing messages.
*/
#processInbound() {
this.#LOG('info', 'processInbound')
this.#LOG('debug', 'processInbound')
// emit received messages in order
while (this.#inbound[this.#nextInbound]) {
const message = this.#inbound[this.#nextInbound]
Expand All @@ -122,7 +122,7 @@ export class MessageQueue<T> extends EventEmitter<MessageQueueEvents<T>> {
}
}

#LOG(level: 'info' | 'warn' | 'error', message: string, ...params: any[]) {
#LOG(level: 'info' | 'warn' | 'error' | 'debug', message: string, ...params: any[]) {
if (this.#sharedLogger == null) {
log(message, params)
return
Expand All @@ -138,6 +138,9 @@ export class MessageQueue<T> extends EventEmitter<MessageQueueEvents<T>> {
case 'error':
this.#sharedLogger.error(message, ...params)
break
case 'debug':
this.#sharedLogger.debug(message, ...params)
break
default:
throw new Error(`Unknown log level ${level}`)
}
Expand Down