The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { mobileSourceMapKey, type MobilePlatform } from "@t3tools/shared/mobileSourceMaps"; |
| 2 | |
| 3 | import { stripBundlePaths, symbolicateStack, type CompactSourceMap } from "./sourceMap.ts"; |
| 4 | |
| 5 | /** The JS bundle a record came from, read from its OTLP resource. */ |
| 6 | interface BundleIdentity { |
| 7 | readonly platform: MobilePlatform; |
| 8 | /** Observe reports one only for an OTA launch. */ |
| 9 | readonly updateId: string | undefined; |
| 10 | readonly easBuildId: string | undefined; |
| 11 | } |
| 12 | |
| 13 | const BUNDLE_ID_PATTERN = /^[A-Za-z0-9-]{1,64}$/u; |
| 14 | |
| 15 | /** |
| 16 | * The map for the bundle that threw. A build map never stands in for a |
| 17 | * missing OTA map: its offsets belong to a different bundle. |
| 18 | */ |
| 19 | export function bundleSourceMapKey(identity: BundleIdentity): string | undefined { |
| 20 | if (identity.updateId !== undefined) { |
| 21 | return BUNDLE_ID_PATTERN.test(identity.updateId) |
| 22 | ? mobileSourceMapKey(identity.platform, "ota", identity.updateId) |
| 23 | : undefined; |
| 24 | } |
| 25 | if (identity.easBuildId !== undefined && BUNDLE_ID_PATTERN.test(identity.easBuildId)) { |
| 26 | return mobileSourceMapKey(identity.platform, "build", identity.easBuildId); |
| 27 | } |
| 28 | return undefined; |
| 29 | } |
| 30 | |
| 31 | interface JsException { |
| 32 | readonly identity: BundleIdentity; |
| 33 | readonly stack: string; |
| 34 | /** The record's attribute list, updated in place by `applySymbolication`. */ |
| 35 | readonly attributes: Array<unknown>; |
| 36 | } |
| 37 | |
| 38 | const isRecord = (value: unknown): value is Record<string, unknown> => |
| 39 | typeof value === "object" && value !== null && !Array.isArray(value); |
| 40 | |
| 41 | const arrayAt = (value: unknown, key: string): Array<unknown> | undefined => { |
| 42 | const child = isRecord(value) ? value[key] : undefined; |
| 43 | return Array.isArray(child) ? child : undefined; |
| 44 | }; |
| 45 | |
| 46 | const stringAttribute = (attributes: ReadonlyArray<unknown>, key: string): string | undefined => { |
| 47 | for (const attribute of attributes) { |
| 48 | if (!isRecord(attribute) || attribute.key !== key) continue; |
| 49 | const value = isRecord(attribute.value) ? attribute.value.stringValue : undefined; |
| 50 | return typeof value === "string" ? value : undefined; |
| 51 | } |
| 52 | return undefined; |
| 53 | }; |
| 54 | |
| 55 | const platformOf = (attributes: ReadonlyArray<unknown>): MobilePlatform | undefined => { |
| 56 | const osType = stringAttribute(attributes, "os.type"); |
| 57 | if (osType === "darwin") return "ios"; |
| 58 | if (osType === "linux") return "android"; |
| 59 | return undefined; |
| 60 | }; |
| 61 | |
| 62 | /** JavaScript exception records with a stack; native crash frames are not in the bundle. */ |
| 63 | export function collectJsExceptions(payload: unknown): ReadonlyArray<JsException> { |
| 64 | const exceptions: Array<JsException> = []; |
| 65 | for (const resourceLogs of arrayAt(payload, "resourceLogs") ?? []) { |
| 66 | const resourceAttributes = |
| 67 | arrayAt(isRecord(resourceLogs) ? resourceLogs.resource : undefined, "attributes") ?? []; |
| 68 | const platform = platformOf(resourceAttributes); |
| 69 | if (platform === undefined) continue; |
| 70 | const identity: BundleIdentity = { |
| 71 | platform, |
| 72 | updateId: |
| 73 | stringAttribute(resourceAttributes, "expo.app.updates.id") ?? |
| 74 | stringAttribute(resourceAttributes, "expo.app.update_id"), |
| 75 | easBuildId: stringAttribute(resourceAttributes, "expo.eas_build.id"), |
| 76 | }; |
| 77 | for (const scopeLogs of arrayAt(resourceLogs, "scopeLogs") ?? []) { |
| 78 | for (const logRecord of arrayAt(scopeLogs, "logRecords") ?? []) { |
| 79 | const attributes = arrayAt(logRecord, "attributes"); |
| 80 | if (!attributes) continue; |
| 81 | const stack = stringAttribute(attributes, "exception.stacktrace"); |
| 82 | if (stack === undefined) continue; |
| 83 | if (stringAttribute(attributes, "expo.error.source") === "nativeCrash") continue; |
| 84 | exceptions.push({ identity, stack, attributes }); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | return exceptions; |
| 89 | } |
| 90 | |
| 91 | const stringValue = (key: string, value: string) => ({ key, value: { stringValue: value } }); |
| 92 | |
| 93 | const setAttribute = (attributes: Array<unknown>, key: string, value: string) => { |
| 94 | const index = attributes.findIndex((attribute) => isRecord(attribute) && attribute.key === key); |
| 95 | if (index === -1) attributes.push(stringValue(key, value)); |
| 96 | else attributes[index] = stringValue(key, value); |
| 97 | }; |
| 98 | |
| 99 | /** Why a stack stayed minified, recorded as `relay.symbolication.status`. */ |
| 100 | export type SourceMapMiss = |
| 101 | /** A development or local build: no update or EAS build to name a map. */ |
| 102 | | "no_bundle_id" |
| 103 | | "source_map_missing" |
| 104 | | "source_map_too_large" |
| 105 | | "source_map_invalid" |
| 106 | /** The bucket could not be read; retried shortly. */ |
| 107 | | "source_map_unavailable" |
| 108 | /** The request named more bundles than the relay decodes for one request. */ |
| 109 | | "source_map_limit"; |
| 110 | |
| 111 | /** |
| 112 | * Rewrites one exception's stack, growing it by at most `maxGrowth`, and |
| 113 | * records the outcome on the record so a stack still minified in Axiom |
| 114 | * explains itself. |
| 115 | */ |
| 116 | export function applySymbolication( |
| 117 | exception: JsException, |
| 118 | found: { readonly key: string; readonly map: CompactSourceMap } | SourceMapMiss, |
| 119 | maxGrowth: number, |
| 120 | ): { readonly symbolicated: boolean; readonly growth: number } { |
| 121 | const stack = stripBundlePaths(exception.stack); |
| 122 | if (stack !== exception.stack) setAttribute(exception.attributes, "exception.stacktrace", stack); |
| 123 | if (typeof found === "string") { |
| 124 | setAttribute(exception.attributes, "relay.symbolication.status", found); |
| 125 | return { symbolicated: false, growth: 0 }; |
| 126 | } |
| 127 | const result = symbolicateStack(stack, found.map, maxGrowth); |
| 128 | setAttribute(exception.attributes, "relay.symbolication.source_map", found.key); |
| 129 | if (result.mappedFrames === 0) { |
| 130 | setAttribute(exception.attributes, "relay.symbolication.status", "no_bundle_frames"); |
| 131 | return { symbolicated: false, growth: 0 }; |
| 132 | } |
| 133 | setAttribute(exception.attributes, "relay.symbolication.status", "symbolicated"); |
| 134 | setAttribute(exception.attributes, "relay.symbolication.raw_stacktrace", stack); |
| 135 | setAttribute(exception.attributes, "exception.stacktrace", result.stack); |
| 136 | return { symbolicated: true, growth: result.growth }; |
| 137 | } |
| 138 | |
| 139 | const isFatal = (attributes: ReadonlyArray<unknown>) => |
| 140 | attributes.some( |
| 141 | (attribute) => |
| 142 | isRecord(attribute) && |
| 143 | attribute.key === "expo.error.is_fatal" && |
| 144 | isRecord(attribute.value) && |
| 145 | attribute.value.boolValue === true, |
| 146 | ); |
| 147 | |
| 148 | /** |
| 149 | * A fatal JS error ends in a native abort, which Observe reports as a second crash with the |
| 150 | * same generic stack in the same session. Marks those `relay.crash.js_fatal` so they don't |
| 151 | * bury real native crashes. Both arrive together on the next launch. |
| 152 | */ |
| 153 | export function markJsFatalCrashes(payload: unknown): number { |
| 154 | const records: Array<Array<unknown>> = []; |
| 155 | const fatalSessions = new Set<string>(); |
| 156 | for (const resourceLogs of arrayAt(payload, "resourceLogs") ?? []) { |
| 157 | for (const scopeLogs of arrayAt(resourceLogs, "scopeLogs") ?? []) { |
| 158 | for (const logRecord of arrayAt(scopeLogs, "logRecords") ?? []) { |
| 159 | const attributes = arrayAt(logRecord, "attributes"); |
| 160 | if (!attributes || !isFatal(attributes)) continue; |
| 161 | const session = stringAttribute(attributes, "session.id"); |
| 162 | if (session === undefined) continue; |
| 163 | if (stringAttribute(attributes, "expo.error.source") === "nativeCrash") { |
| 164 | records.push(attributes); |
| 165 | } else { |
| 166 | fatalSessions.add(session); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | let marked = 0; |
| 172 | for (const attributes of records) { |
| 173 | if (!fatalSessions.has(stringAttribute(attributes, "session.id")!)) continue; |
| 174 | attributes.push({ key: "relay.crash.js_fatal", value: { boolValue: true } }); |
| 175 | marked += 1; |
| 176 | } |
| 177 | return marked; |
| 178 | } |