Surf Studio commited on
Commit
24f04ca
·
1 Parent(s): fd704a9

tg: wire-level diagnostics

Browse files
Files changed (1) hide show
  1. lib/tg/index.js +31 -0
lib/tg/index.js CHANGED
@@ -322,9 +322,39 @@ async function startClient() {
322
  }
323
  }
324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325
  /** Make `c` the live client: identity, dialog cache, event handlers. */
326
  async function attach(c) {
327
  client = c
 
328
  const me = await c.getMe()
329
  state.me = { name: [me.firstName, me.lastName].filter(Boolean).join(' ') || me.username || '', username: me.username || null }
330
  await loadDialogs()
@@ -766,6 +796,7 @@ function status() {
766
  realtimeResets: state.realtimeResets,
767
  raw: state.raw,
768
  rawTypes: state.rawTypes,
 
769
  globalHits: state.globalHits,
770
  floodUntil: state.floodUntil > Date.now() ? Math.floor(state.floodUntil / 1000) : null,
771
  connectedAt: state.connectedAt,
 
322
  }
323
  }
324
 
325
+ /** Count what actually arrives on the wire, so "no realtime updates" can be told apart from "updates we can't decode". */
326
+ function instrument(c) {
327
+ state.wire = { messages: 0, types: {}, notes: [] }
328
+ const sender = c._sender
329
+ if (sender && typeof sender._processMessage === 'function') {
330
+ const orig = sender._processMessage.bind(sender)
331
+ sender._processMessage = async (message) => {
332
+ try {
333
+ const obj = await message.obj
334
+ const name = obj?.className || obj?.constructor?.name || 'unknown'
335
+ state.wire.messages += 1
336
+ state.wire.types[name] = (state.wire.types[name] || 0) + 1
337
+ } catch {}
338
+ return orig(message)
339
+ }
340
+ }
341
+ for (const level of ['info', 'warn']) {
342
+ const orig = c._log[level]?.bind(c._log)
343
+ c._log[level] = (msg, ...rest) => {
344
+ const text = String(msg)
345
+ if (/not found|Security error|Invalid buffer|Broken auth/i.test(text)) {
346
+ state.wire.notes.unshift(`${new Date().toISOString().slice(11, 19)} ${text.slice(0, 160)}`)
347
+ state.wire.notes.length = Math.min(state.wire.notes.length, 15)
348
+ }
349
+ return orig?.(msg, ...rest)
350
+ }
351
+ }
352
+ }
353
+
354
  /** Make `c` the live client: identity, dialog cache, event handlers. */
355
  async function attach(c) {
356
  client = c
357
+ instrument(c)
358
  const me = await c.getMe()
359
  state.me = { name: [me.firstName, me.lastName].filter(Boolean).join(' ') || me.username || '', username: me.username || null }
360
  await loadDialogs()
 
796
  realtimeResets: state.realtimeResets,
797
  raw: state.raw,
798
  rawTypes: state.rawTypes,
799
+ wire: state.wire || null,
800
  globalHits: state.globalHits,
801
  floodUntil: state.floodUntil > Date.now() ? Math.floor(state.floodUntil / 1000) : null,
802
  connectedAt: state.connectedAt,