about summary refs log tree commit diff
path: root/src/state/session/logging.ts
blob: bf847f08f5441e82dce7b9a32b326f2ff9cfb4b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {type AtpSessionData, type AtpSessionEvent} from '@atproto/api'
import {sha256} from 'js-sha256'
import {Statsig} from 'statsig-react-native-expo'

import {IS_INTERNAL} from '#/env'
import {type Schema} from '../persisted'
import {type Action, type State} from './reducer'
import {type SessionAccount} from './types'

type Reducer = (state: State, action: Action) => State

type Log =
  | {
      type: 'reducer:init'
      state: State
    }
  | {
      type: 'reducer:call'
      action: Action
      prevState: State
      nextState: State
    }
  | {
      type: 'method:start'
      method:
        | 'createAccount'
        | 'login'
        | 'logout'
        | 'resumeSession'
        | 'removeAccount'
      account?: SessionAccount
    }
  | {
      type: 'method:end'
      method:
        | 'createAccount'
        | 'login'
        | 'logout'
        | 'resumeSession'
        | 'removeAccount'
      account?: SessionAccount
    }
  | {
      type: 'persisted:broadcast'
      data: Schema['session']
    }
  | {
      type: 'persisted:receive'
      data: Schema['session']
    }
  | {
      type: 'agent:switch'
      prevAgent: object
      nextAgent: object
    }
  | {
      type: 'agent:patch'
      agent: object
      prevSession: AtpSessionData | undefined
      nextSession: AtpSessionData | undefined
    }

export function wrapSessionReducerForLogging(reducer: Reducer): Reducer {
  return function loggingWrapper(prevState: State, action: Action): State {
    const nextState = reducer(prevState, action)
    addSessionDebugLog({type: 'reducer:call', prevState, action, nextState})
    return nextState
  }
}

let nextMessageIndex = 0
const MAX_SLICE_LENGTH = 1000

// Not gated.
export function addSessionErrorLog(did: string, event: AtpSessionEvent) {
  try {
    if (!Statsig.initializeCalled() || !Statsig.getStableID()) {
      return
    }
    const stack = (new Error().stack ?? '').slice(0, MAX_SLICE_LENGTH)
    Statsig.logEvent('session:error', null, {
      did,
      event,
      stack,
    })
  } catch (e) {
    console.error(e)
  }
}

export function addSessionDebugLog(log: Log) {
  try {
    if (!Statsig.initializeCalled() || !Statsig.getStableID()) {
      // Drop these logs for now.
      return
    }
    // DISABLING THIS GATE DUE TO EME @TODO EME-GATE
    if (!IS_INTERNAL) {
      return
    }
    // if (!Statsig.checkGate('debug_session')) {
    //   return
    // }
    const messageIndex = nextMessageIndex++
    const {type, ...content} = log
    let payload = JSON.stringify(content, replacer)

    let nextSliceIndex = 0
    while (payload.length > 0) {
      const sliceIndex = nextSliceIndex++
      const slice = payload.slice(0, MAX_SLICE_LENGTH)
      payload = payload.slice(MAX_SLICE_LENGTH)
      Statsig.logEvent('session:debug', null, {
        realmId,
        messageIndex: String(messageIndex),
        messageType: type,
        sliceIndex: String(sliceIndex),
        slice,
      })
    }
  } catch (e) {
    console.error(e)
  }
}

let agentIds = new WeakMap<object, string>()
let realmId = Math.random().toString(36).slice(2)
let nextAgentId = 1

function getAgentId(agent: object) {
  let id = agentIds.get(agent)
  if (id === undefined) {
    id = realmId + '::' + nextAgentId++
    agentIds.set(agent, id)
  }
  return id
}

function replacer(key: string, value: unknown) {
  if (typeof value === 'object' && value != null && 'api' in value) {
    return getAgentId(value)
  }
  if (
    key === 'service' ||
    key === 'email' ||
    key === 'emailConfirmed' ||
    key === 'emailAuthFactor' ||
    key === 'pdsUrl'
  ) {
    return undefined
  }
  if (
    typeof value === 'string' &&
    (key === 'refreshJwt' || key === 'accessJwt')
  ) {
    return sha256(value)
  }
  return value
}