The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // A production Hermes map has over a million segments. General-purpose decoders hold |
| 2 | // each as its own array, past a Worker isolate's memory budget; typed arrays fit. |
| 3 | |
| 4 | const BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 5 | const BASE64_VALUES = new Int8Array(128).fill(-1); |
| 6 | for (let index = 0; index < BASE64.length; index += 1) { |
| 7 | BASE64_VALUES[BASE64.charCodeAt(index)] = index; |
| 8 | } |
| 9 | const COMMA = 44; |
| 10 | const SEMICOLON = 59; |
| 11 | /** The relay caches two decoded maps in a 128 MB isolate. */ |
| 12 | const MAX_DECODED_BYTES = 32 * 1024 * 1024; |
| 13 | |
| 14 | export interface CompactSourceMap { |
| 15 | readonly sources: ReadonlyArray<string>; |
| 16 | /** Segment index where each generated line starts, plus one past the last line. */ |
| 17 | readonly lineStarts: Int32Array; |
| 18 | readonly generatedColumns: Int32Array; |
| 19 | /** -1 for segments that map to no source. */ |
| 20 | readonly sourceIndexes: Int32Array; |
| 21 | readonly originalLines: Int32Array; |
| 22 | readonly originalColumns: Int32Array; |
| 23 | } |
| 24 | |
| 25 | interface OriginalPosition { |
| 26 | readonly source: string; |
| 27 | /** 1-based. */ |
| 28 | readonly line: number; |
| 29 | /** 0-based, as source maps store it. */ |
| 30 | readonly column: number; |
| 31 | } |
| 32 | |
| 33 | const isStringArray = (value: unknown): value is ReadonlyArray<string> => |
| 34 | Array.isArray(value) && value.every((item) => typeof item === "string"); |
| 35 | |
| 36 | /** |
| 37 | * Calls `visit` per segment in order; `sourceIndex` is -1 for a segment that maps to no source. |
| 38 | * False if a segment is malformed. |
| 39 | */ |
| 40 | function walkSegments( |
| 41 | mappings: string, |
| 42 | visit: ( |
| 43 | line: number, |
| 44 | generatedColumn: number, |
| 45 | sourceIndex: number, |
| 46 | originalLine: number, |
| 47 | originalColumn: number, |
| 48 | ) => void, |
| 49 | ): boolean { |
| 50 | // Names keep the running offsets right but are not stored: they name an identifier, not the function. |
| 51 | const fields = [0, 0, 0, 0, 0]; |
| 52 | let line = 0; |
| 53 | let generatedColumn = 0; |
| 54 | let sourceIndex = 0; |
| 55 | let originalLine = 0; |
| 56 | let originalColumn = 0; |
| 57 | let index = 0; |
| 58 | |
| 59 | while (index < mappings.length) { |
| 60 | const code = mappings.charCodeAt(index); |
| 61 | if (code === SEMICOLON) { |
| 62 | line += 1; |
| 63 | generatedColumn = 0; |
| 64 | index += 1; |
| 65 | continue; |
| 66 | } |
| 67 | if (code === COMMA) { |
| 68 | index += 1; |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | let fieldCount = 0; |
| 73 | while (index < mappings.length) { |
| 74 | const next = mappings.charCodeAt(index); |
| 75 | if (next === COMMA || next === SEMICOLON) break; |
| 76 | let value = 0; |
| 77 | let shift = 0; |
| 78 | let digit: number; |
| 79 | do { |
| 80 | const char = mappings.charCodeAt(index); |
| 81 | digit = char < 128 ? BASE64_VALUES[char]! : -1; |
| 82 | if (digit === -1) return false; |
| 83 | value += (digit & 31) * 2 ** shift; |
| 84 | shift += 5; |
| 85 | index += 1; |
| 86 | } while (digit & 32); |
| 87 | const negative = value % 2 === 1; |
| 88 | value = Math.floor(value / 2); |
| 89 | if (fieldCount < fields.length) fields[fieldCount] = negative ? -value : value; |
| 90 | fieldCount += 1; |
| 91 | } |
| 92 | if (fieldCount !== 1 && fieldCount !== 4 && fieldCount !== 5) return false; |
| 93 | |
| 94 | generatedColumn += fields[0]!; |
| 95 | if (fieldCount === 1) { |
| 96 | visit(line, generatedColumn, -1, 0, 0); |
| 97 | } else { |
| 98 | sourceIndex += fields[1]!; |
| 99 | originalLine += fields[2]!; |
| 100 | originalColumn += fields[3]!; |
| 101 | visit(line, generatedColumn, sourceIndex, originalLine, originalColumn); |
| 102 | } |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * `too_large` past the decode budget; `invalid` for anything but a version 3 map, including |
| 109 | * index maps, which React Native never emits. |
| 110 | */ |
| 111 | export function decodeSourceMap(raw: unknown): CompactSourceMap | "too_large" | "invalid" { |
| 112 | if (typeof raw !== "object" || raw === null) return "invalid"; |
| 113 | const version = Reflect.get(raw, "version"); |
| 114 | const mappings = Reflect.get(raw, "mappings"); |
| 115 | const sources = Reflect.get(raw, "sources"); |
| 116 | const sourceRoot = Reflect.get(raw, "sourceRoot"); |
| 117 | if (version !== 3 || typeof mappings !== "string" || !isStringArray(sources)) return "invalid"; |
| 118 | |
| 119 | // Hermes maps most bytecode instructions to the same position as the one before; a lookup |
| 120 | // lands on the earlier segment instead with the same answer. Dropping them shrinks our iOS |
| 121 | // map from 2.1M segments to 1.2M, so the first pass counts what the second keeps. |
| 122 | let lineCount = 1; |
| 123 | for (let index = 0; index < mappings.length; index += 1) { |
| 124 | if (mappings.charCodeAt(index) === SEMICOLON) lineCount += 1; |
| 125 | } |
| 126 | let kept = 0; |
| 127 | let lastLine = -1; |
| 128 | let lastSource = -2; |
| 129 | let lastOriginalLine = 0; |
| 130 | let lastOriginalColumn = 0; |
| 131 | const repeats = (line: number, source: number, originalLine: number, originalColumn: number) => { |
| 132 | const same = |
| 133 | line === lastLine && |
| 134 | source === lastSource && |
| 135 | (source === -1 || |
| 136 | (originalLine === lastOriginalLine && originalColumn === lastOriginalColumn)); |
| 137 | lastLine = line; |
| 138 | lastSource = source; |
| 139 | lastOriginalLine = originalLine; |
| 140 | lastOriginalColumn = originalColumn; |
| 141 | return same; |
| 142 | }; |
| 143 | const counted = walkSegments(mappings, (line, _column, source, originalLine, originalColumn) => { |
| 144 | if (!repeats(line, source, originalLine, originalColumn)) kept += 1; |
| 145 | }); |
| 146 | if (!counted) return "invalid"; |
| 147 | |
| 148 | const prefixLength = typeof sourceRoot === "string" ? sourceRoot.length + 1 : 0; |
| 149 | let sourceChars = 0; |
| 150 | for (const source of sources) sourceChars += source.length + prefixLength; |
| 151 | // A path takes up to two bytes a character, plus its string header and array slot. |
| 152 | const decodedBytes = kept * 16 + (lineCount + 1) * 4 + sourceChars * 2 + sources.length * 32; |
| 153 | if (decodedBytes > MAX_DECODED_BYTES) return "too_large"; |
| 154 | |
| 155 | const lineStarts = new Int32Array(lineCount + 1); |
| 156 | const generatedColumns = new Int32Array(kept); |
| 157 | const sourceIndexes = new Int32Array(kept); |
| 158 | const originalLines = new Int32Array(kept); |
| 159 | const originalColumns = new Int32Array(kept); |
| 160 | let segment = 0; |
| 161 | let currentLine = 0; |
| 162 | lastLine = -1; |
| 163 | lastSource = -2; |
| 164 | walkSegments(mappings, (line, generatedColumn, source, originalLine, originalColumn) => { |
| 165 | if (repeats(line, source, originalLine, originalColumn)) return; |
| 166 | while (currentLine < line) { |
| 167 | currentLine += 1; |
| 168 | lineStarts[currentLine] = segment; |
| 169 | } |
| 170 | generatedColumns[segment] = generatedColumn; |
| 171 | sourceIndexes[segment] = source; |
| 172 | originalLines[segment] = originalLine; |
| 173 | originalColumns[segment] = originalColumn; |
| 174 | segment += 1; |
| 175 | }); |
| 176 | while (currentLine < lineCount) { |
| 177 | currentLine += 1; |
| 178 | lineStarts[currentLine] = segment; |
| 179 | } |
| 180 | |
| 181 | const prefix = typeof sourceRoot === "string" && sourceRoot !== "" ? sourceRoot : undefined; |
| 182 | return { |
| 183 | sources: prefix ? sources.map((source) => `${prefix.replace(/\/?$/u, "/")}${source}`) : sources, |
| 184 | lineStarts, |
| 185 | generatedColumns, |
| 186 | sourceIndexes, |
| 187 | originalLines, |
| 188 | originalColumns, |
| 189 | }; |
| 190 | } |
| 191 | |
| 192 | /** `line` is 1-based and `column` 0-based, as in `@jridgewell/trace-mapping`. */ |
| 193 | function originalPositionFor( |
| 194 | map: CompactSourceMap, |
| 195 | line: number, |
| 196 | column: number, |
| 197 | ): OriginalPosition | undefined { |
| 198 | const lineIndex = line - 1; |
| 199 | if (lineIndex < 0 || lineIndex >= map.lineStarts.length - 1) return undefined; |
| 200 | const lineStart = map.lineStarts[lineIndex]!; |
| 201 | // An exact hit takes the first segment at `column`: Hermes maps a function's first |
| 202 | // instruction twice (declaration, then first statement), and other libraries take the first. |
| 203 | let low = lineStart; |
| 204 | let high = map.lineStarts[lineIndex + 1]!; |
| 205 | while (low < high) { |
| 206 | const middle = (low + high) >>> 1; |
| 207 | if (map.generatedColumns[middle]! < column) low = middle + 1; |
| 208 | else high = middle; |
| 209 | } |
| 210 | const exact = low < map.lineStarts[lineIndex + 1]! && map.generatedColumns[low] === column; |
| 211 | const found = exact ? low : low - 1; |
| 212 | if (found < lineStart) return undefined; |
| 213 | const sourceIndex = map.sourceIndexes[found]!; |
| 214 | const source = sourceIndex === -1 ? undefined : map.sources[sourceIndex]; |
| 215 | if (source === undefined) return undefined; |
| 216 | return { |
| 217 | source, |
| 218 | line: map.originalLines[found]! + 1, |
| 219 | column: map.originalColumns[found]!, |
| 220 | }; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * `at render (address at index.android.bundle:1:48213)`: Hermes prints the bytecode offset, |
| 225 | * the composed map's generated column. On iOS the path is absolute, and an OTA bundle's |
| 226 | * contains a space (`Application Support`). |
| 227 | */ |
| 228 | const FRAME_MARKER = "address at "; |
| 229 | const FRAME_POSITION = /:(\d+):(\d+)\)?$/u; |
| 230 | |
| 231 | /** |
| 232 | * One anchored match and a few scans instead of `/address at (.+?):(\d+):(\d+)(?=\)?$)/`, which |
| 233 | * retries from every marker and is quadratic on a line of them. Telemetry is unauthenticated. |
| 234 | */ |
| 235 | function bundleFrame(line: string) { |
| 236 | const position = FRAME_POSITION.exec(line); |
| 237 | if (!position) return null; |
| 238 | // `.` stops at these, so a path never spans one. |
| 239 | const lineBreak = Math.max( |
| 240 | line.lastIndexOf("\r", position.index), |
| 241 | line.lastIndexOf("\u2028", position.index), |
| 242 | line.lastIndexOf("\u2029", position.index), |
| 243 | ); |
| 244 | const index = line.indexOf(FRAME_MARKER, lineBreak + 1); |
| 245 | const pathStart = index + FRAME_MARKER.length; |
| 246 | if (index === -1 || pathStart >= position.index) return null; |
| 247 | return { |
| 248 | index, |
| 249 | path: line.slice(pathStart, position.index), |
| 250 | line: Number(position[1]), |
| 251 | column: Number(position[2]), |
| 252 | /** After the column, before a closing parenthesis. */ |
| 253 | end: line.length - (line.endsWith(")") ? 1 : 0), |
| 254 | }; |
| 255 | } |
| 256 | /** Hermes's own runtime, not in the map. */ |
| 257 | const HERMES_RUNTIME = "InternalBytecode.js"; |
| 258 | |
| 259 | const baseName = (path: string) => path.slice(path.lastIndexOf("/") + 1); |
| 260 | |
| 261 | /** iOS bundle paths carry per-install container UUIDs, which split one crash into many groups. */ |
| 262 | export function stripBundlePaths(stack: string): string { |
| 263 | return stack |
| 264 | .split("\n") |
| 265 | .map((line) => { |
| 266 | const frame = bundleFrame(line); |
| 267 | if (!frame || !frame.path.includes("/")) return line; |
| 268 | const start = frame.index + FRAME_MARKER.length; |
| 269 | return line.slice(0, start) + baseName(frame.path) + line.slice(start + frame.path.length); |
| 270 | }) |
| 271 | .join("\n"); |
| 272 | } |
| 273 | |
| 274 | interface SymbolicatedStack { |
| 275 | readonly stack: string; |
| 276 | /** Frames in the app bundle. */ |
| 277 | readonly bundleFrames: number; |
| 278 | /** Bundle frames the map resolved. */ |
| 279 | readonly mappedFrames: number; |
| 280 | /** How much longer the stack got. */ |
| 281 | readonly growth: number; |
| 282 | } |
| 283 | |
| 284 | /** Once the stack would grow by more than `maxGrowth`, the remaining frames keep their offsets. */ |
| 285 | export function symbolicateStack( |
| 286 | stack: string, |
| 287 | map: CompactSourceMap, |
| 288 | maxGrowth: number, |
| 289 | ): SymbolicatedStack { |
| 290 | let bundleFrames = 0; |
| 291 | let mappedFrames = 0; |
| 292 | let growth = 0; |
| 293 | let exhausted = false; |
| 294 | const lines = stack.split("\n").map((line) => { |
| 295 | const frame = bundleFrame(line); |
| 296 | if (!frame || baseName(frame.path) === HERMES_RUNTIME) return line; |
| 297 | bundleFrames += 1; |
| 298 | if (exhausted) return line; |
| 299 | const position = originalPositionFor(map, frame.line, frame.column); |
| 300 | if (!position) return line; |
| 301 | const lineAndColumn = `:${position.line}:${position.column + 1}`; |
| 302 | const added = position.source.length + lineAndColumn.length - (frame.end - frame.index); |
| 303 | if (growth + added > maxGrowth) { |
| 304 | exhausted = true; |
| 305 | return line; |
| 306 | } |
| 307 | growth += added; |
| 308 | mappedFrames += 1; |
| 309 | // Spliced, not `replace`d: a replacement string would expand `$&` and `$'` in the path. |
| 310 | return line.slice(0, frame.index) + position.source + lineAndColumn + line.slice(frame.end); |
| 311 | }); |
| 312 | return { stack: lines.join("\n"), bundleFrames, mappedFrames, growth }; |
| 313 | } |