@replayd/capacitor-android 0.1.10 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@ package com.replayd.capacitor
3
3
  import android.app.ActivityManager
4
4
  import android.app.ApplicationExitInfo
5
5
  import android.content.Context
6
+ import android.content.SharedPreferences
6
7
  import android.os.Build
7
8
  import com.getcapacitor.JSArray
8
9
  import com.getcapacitor.JSObject
@@ -10,14 +11,18 @@ import com.getcapacitor.Plugin
10
11
  import com.getcapacitor.PluginCall
11
12
  import com.getcapacitor.PluginMethod
12
13
  import com.getcapacitor.annotation.CapacitorPlugin
14
+ import org.json.JSONArray
13
15
  import org.json.JSONObject
16
+ import java.io.ByteArrayOutputStream
14
17
  import java.io.File
18
+ import java.io.InputStream
15
19
 
16
20
  /**
17
21
  * T-154: native crashes and ANRs for replayd. An uncaught Java/Kotlin exception is written to app storage by a handler
18
22
  * chained before the previous one (≤ 64 KB each, ≤ 5 kept); on API 30+ the system's ApplicationExitInfo adds ANRs and
19
23
  * native crashes. The SDK's Capacitor chunk calls `pending()` on the next launch and sends them as error events,
20
- * linked to the session id it last reported with `setSession()`. Nothing leaves the device from here.
24
+ * linked to the session that was current when they happened (a bounded timeline of the ids reported with
25
+ * `setSession()`; an exit older than the timeline has no sid). Nothing leaves the device from here.
21
26
  */
22
27
  @CapacitorPlugin(name = "ReplaydNative")
23
28
  class ReplaydNativePlugin : Plugin() {
@@ -28,7 +33,7 @@ class ReplaydNativePlugin : Plugin() {
28
33
  @PluginMethod
29
34
  fun setSession(call: PluginCall) {
30
35
  val sid = call.getString("sid")
31
- if (sid != null && sid.length <= 64) CrashStore.prefs(context).edit().putString(CrashStore.SID, sid).apply()
36
+ if (sid != null && sid.length <= 64) CrashStore.recordSession(context, sid, System.currentTimeMillis())
32
37
  call.resolve()
33
38
  }
34
39
 
@@ -54,11 +59,15 @@ class ReplaydNativePlugin : Plugin() {
54
59
  .filter { it.timestamp > seen && (it.reason == ApplicationExitInfo.REASON_ANR || it.reason == ApplicationExitInfo.REASON_CRASH_NATIVE) }
55
60
  .take(5)
56
61
  if (list.isNotEmpty()) prefs.edit().putLong(CrashStore.EXIT_SEEN, list.maxOf { it.timestamp }).apply()
57
- val sid = prefs.getString(CrashStore.SID, null)
62
+ // the SDK reports the new launch's session before asking for these, so each exit is linked by its own
63
+ // timestamp to the session interval it happened in (unknown → null), never to the current session
64
+ val timeline = CrashStore.sessions(prefs)
58
65
  return list.map { info ->
66
+ val sid = CrashStore.sidAt(timeline, info.timestamp)
59
67
  val anr = info.reason == ApplicationExitInfo.REASON_ANR
60
- val trace = try {
61
- info.traceInputStream?.use { s -> String(s.readBytes().take(CrashStore.MAX_BYTES).toByteArray()) }
68
+ // only an ANR's stack is reported, so native crashes never open their (possibly huge) trace stream
69
+ val trace = if (!anr) null else try {
70
+ info.traceInputStream?.use { s -> CrashStore.readCapped(s, CrashStore.MAX_BYTES) }
62
71
  } catch (_: Throwable) {
63
72
  null
64
73
  }
@@ -88,12 +97,61 @@ class ReplaydNativePlugin : Plugin() {
88
97
  internal object CrashStore {
89
98
  const val SID = "sid"
90
99
  const val EXIT_SEEN = "exit_seen"
100
+ const val SESSIONS = "sessions"
91
101
  const val MAX_BYTES = 64 * 1024
92
102
  private const val MAX_FILES = 5
103
+ private const val MAX_SESSIONS = 32
93
104
  @Volatile private var installed = false
94
105
 
95
106
  fun prefs(ctx: Context) = ctx.getSharedPreferences("replayd", Context.MODE_PRIVATE)
96
107
 
108
+ /** Session intervals, oldest first: (sid, first time it was reported). Bounded to [MAX_SESSIONS]. */
109
+ fun sessions(p: SharedPreferences): List<Pair<String, Long>> {
110
+ return try {
111
+ val a = JSONArray(p.getString(SESSIONS, null) ?: return emptyList())
112
+ (0 until a.length()).mapNotNull { i ->
113
+ val e = a.optJSONArray(i)
114
+ val s = e?.optString(0, "")
115
+ if (e == null || s.isNullOrEmpty() || s.length > 64) null else s to e.optLong(1, 0L)
116
+ }
117
+ } catch (_: Throwable) {
118
+ emptyList()
119
+ }
120
+ }
121
+
122
+ /** The session that was current at [at], or null when the timeline does not reach back that far. */
123
+ fun sidAt(timeline: List<Pair<String, Long>>, at: Long): String? = timeline.lastOrNull { it.second <= at }?.first
124
+
125
+ /** Remembers the current session: [SID] for crash files, and a new interval when the id changed. */
126
+ @Synchronized
127
+ fun recordSession(ctx: Context, sid: String, now: Long) {
128
+ val p = prefs(ctx)
129
+ val list = sessions(p).toMutableList()
130
+ val e = p.edit().putString(SID, sid)
131
+ if (list.lastOrNull()?.first != sid) {
132
+ list.add(sid to now)
133
+ while (list.size > MAX_SESSIONS) list.removeAt(0)
134
+ val a = JSONArray()
135
+ for ((s, t) in list) a.put(JSONArray().put(s).put(t))
136
+ e.putString(SESSIONS, a.toString())
137
+ }
138
+ e.apply()
139
+ }
140
+
141
+ /** At most [max] bytes of [s] as text, read in small chunks: an oversized stream is never held in memory. */
142
+ fun readCapped(s: InputStream, max: Int): String {
143
+ val out = ByteArrayOutputStream(minOf(max, 8192))
144
+ val buf = ByteArray(8192)
145
+ var left = max
146
+ while (left > 0) {
147
+ val n = s.read(buf, 0, minOf(buf.size, left))
148
+ if (n < 0) break
149
+ out.write(buf, 0, n)
150
+ left -= n
151
+ }
152
+ return String(out.toByteArray(), Charsets.UTF_8)
153
+ }
154
+
97
155
  private fun dir(ctx: Context) = File(ctx.filesDir, "replayd-crashes").apply { mkdirs() }
98
156
 
99
157
  fun install(ctx: Context) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@replayd/capacitor-android",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "description": "Capacitor Android plugin for replayd: native (Java/Kotlin) crashes and ANRs, reported on the next launch",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -25,12 +25,12 @@
25
25
  }
26
26
  },
27
27
  "peerDependencies": {
28
- "@replayd/core": "^0.1.10"
28
+ "@replayd/core": "^0.1.12"
29
29
  },
30
30
  "devDependencies": {
31
- "@replayd/core": "0.1.10"
31
+ "@replayd/core": "0.1.12"
32
32
  },
33
- "gitHead": "5016777ab966b8b263944744b00b83fd7612aad9",
33
+ "gitHead": "8cf4906f5f7d70177a5ae3b6a6b2ba7560679356",
34
34
  "scripts": {
35
35
  "build": "node ../../scripts/build.mjs build.json && tsc -p tsconfig.build.json"
36
36
  }