-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsourcemap.rs
More file actions
400 lines (341 loc) · 12.7 KB
/
sourcemap.rs
File metadata and controls
400 lines (341 loc) · 12.7 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//! Sourcemap storage and resolution. Handles artifact bundle parsing
//! (ZIP files from `sentry-cli sourcemaps upload`) and on-the-fly
//! stack frame resolution using debug IDs.
use anyhow::{Context, Result};
use std::io::Read;
// ── Types ───────────────────────────────────────────────────────────
pub struct SourcemapEntry {
pub debug_id: String,
pub source_url: Option<String>,
pub data: Vec<u8>,
}
pub struct ResolvedFrame {
pub filename: String,
pub function: Option<String>,
pub lineno: u32,
pub colno: u32,
pub context_line: Option<String>,
pub pre_context: Vec<String>,
pub post_context: Vec<String>,
}
// ── Artifact bundle parsing ─────────────────────────────────────────
/// Extract sourcemaps from an artifact bundle ZIP.
///
/// The bundle is produced by `sentry-cli sourcemaps upload` and contains
/// a manifest plus the actual `.map` files. The manifest maps debug IDs
/// to source file paths.
pub fn parse_artifact_bundle(zip_data: &[u8]) -> Result<Vec<SourcemapEntry>> {
let cursor = std::io::Cursor::new(zip_data);
let mut archive = zip::ZipArchive::new(cursor).context("invalid ZIP archive")?;
// Try to find the manifest — could be at the root or under artifact-bundle/
let manifest: serde_json::Value = try_read_manifest(&mut archive)?;
let mut entries = Vec::new();
// The manifest has a "files" object mapping file paths to metadata
if let Some(files) = manifest.get("files").and_then(|f| f.as_object()) {
for (zip_path, meta) in files {
// Only care about sourcemap entries
let file_type = meta.get("type").and_then(|t| t.as_str()).unwrap_or("");
if file_type != "source_map" && file_type != "sourcemap" {
continue;
}
// The debug_id can be in headers or at the top level
let debug_id = extract_debug_id(meta);
let debug_id = match debug_id {
Some(id) => id,
None => continue,
};
let source_url = meta
.get("url")
.or_else(|| meta.get("abs_path"))
.and_then(|v| v.as_str())
.map(|s| s.to_string());
// Read the actual .map file from the ZIP
let data = match read_zip_entry(&mut archive, zip_path) {
Ok(d) => d,
Err(e) => {
tracing::warn!("skipping {zip_path}: {e}");
continue;
}
};
entries.push(SourcemapEntry {
debug_id,
source_url,
data,
});
}
}
// Fallback: if no manifest or no entries found, scan for .map files
// that have debug_id embedded in the JSON
if entries.is_empty() {
entries = scan_for_sourcemaps(&mut archive)?;
}
Ok(entries)
}
fn try_read_manifest(
archive: &mut zip::ZipArchive<std::io::Cursor<&[u8]>>,
) -> Result<serde_json::Value> {
// Try both possible manifest locations
for name in &["manifest.json", "artifact-bundle/manifest.json"] {
if let Ok(data) = read_zip_entry(archive, name) {
if let Ok(val) = serde_json::from_slice(&data) {
return Ok(val);
}
}
}
Ok(serde_json::Value::Object(serde_json::Map::new()))
}
fn extract_debug_id(meta: &serde_json::Value) -> Option<String> {
// Check headers.debug-id first
if let Some(id) = meta
.get("headers")
.and_then(|h| h.get("debug-id"))
.and_then(|v| v.as_str())
{
return Some(normalize_debug_id(id));
}
// Then top-level debug_id / debugId
if let Some(id) = meta
.get("debug_id")
.or_else(|| meta.get("debugId"))
.and_then(|v| v.as_str())
{
return Some(normalize_debug_id(id));
}
None
}
/// Normalize debug IDs — strip the optional `-sourcemap` suffix
fn normalize_debug_id(id: &str) -> String {
id.split_once("-sourcemap")
.map(|(base, _)| base.to_string())
.unwrap_or_else(|| id.to_string())
.to_lowercase()
}
fn read_zip_entry(
archive: &mut zip::ZipArchive<std::io::Cursor<&[u8]>>,
name: &str,
) -> Result<Vec<u8>> {
let mut file = archive.by_name(name)?;
let mut buf = Vec::with_capacity(file.size() as usize);
file.read_to_end(&mut buf)?;
Ok(buf)
}
/// Fallback: scan all .map files in the archive for embedded debug_id
fn scan_for_sourcemaps(
archive: &mut zip::ZipArchive<std::io::Cursor<&[u8]>>,
) -> Result<Vec<SourcemapEntry>> {
let mut entries = Vec::new();
let names: Vec<String> = (0..archive.len())
.filter_map(|i| archive.by_index(i).ok().map(|f| f.name().to_string()))
.collect();
for name in &names {
if !name.ends_with(".map") {
continue;
}
let data = match read_zip_entry(archive, name) {
Ok(d) => d,
Err(_) => continue,
};
// Try to extract debug_id from the sourcemap JSON
if let Ok(val) = serde_json::from_slice::<serde_json::Value>(&data) {
let debug_id = val
.get("debug_id")
.or_else(|| val.get("debugId"))
.and_then(|v| v.as_str())
.map(normalize_debug_id);
if let Some(id) = debug_id {
entries.push(SourcemapEntry {
debug_id: id,
source_url: Some(name.clone()),
data,
});
}
}
}
Ok(entries)
}
// ── Frame resolution ────────────────────────────────────────────────
/// Context lines to show above/below the error line
const CONTEXT_LINES: usize = 5;
/// Resolve a minified stack frame using a parsed sourcemap.
pub fn resolve_frame(sm: &sourcemap::SourceMap, line: u32, col: u32) -> Option<ResolvedFrame> {
// sourcemap crate uses 0-indexed line/col
let token = sm.lookup_token(line.saturating_sub(1), col.saturating_sub(1))?;
let src_id = token.get_src_id();
let orig_line = token.get_src_line(); // 0-indexed
let orig_col = token.get_src_col();
let filename = token.get_source().unwrap_or("<unknown>").to_string();
let function = token.get_name().map(|s| s.to_string());
// Try to get source content for context lines
let (context_line, pre_context, post_context) =
if let Some(source) = sm.get_source_contents(src_id) {
extract_context(source, orig_line as usize)
} else {
(None, Vec::new(), Vec::new())
};
Some(ResolvedFrame {
filename,
function,
lineno: orig_line + 1, // back to 1-indexed
colno: orig_col + 1,
context_line,
pre_context,
post_context,
})
}
fn extract_context(source: &str, line_idx: usize) -> (Option<String>, Vec<String>, Vec<String>) {
let lines: Vec<&str> = source.lines().collect();
if line_idx >= lines.len() {
return (None, Vec::new(), Vec::new());
}
let context_line = Some(lines[line_idx].to_string());
let pre_start = line_idx.saturating_sub(CONTEXT_LINES);
let pre_context: Vec<String> = lines[pre_start..line_idx]
.iter()
.map(|s| s.to_string())
.collect();
let post_end = (line_idx + 1 + CONTEXT_LINES).min(lines.len());
let post_context: Vec<String> = lines[line_idx + 1..post_end]
.iter()
.map(|s| s.to_string())
.collect();
(context_line, pre_context, post_context)
}
// ── DB helpers ──────────────────────────────────────────────────────
use crate::db::{sql, translate_sql, DbPool};
use sqlx::Row;
/// Store a sourcemap entry (zstd-compressed) in the database.
pub async fn store_sourcemap(pool: &DbPool, entry: &SourcemapEntry, project_id: u64) -> Result<()> {
let compressed =
zstd::encode_all(entry.data.as_slice(), 3).context("zstd compress sourcemap")?;
sqlx::query(sql!(
"INSERT INTO sourcemaps (debug_id, source_url, data, project_id)
VALUES (?1, ?2, ?3, ?4)
ON CONFLICT (debug_id) DO UPDATE SET data = ?3, source_url = ?2"
))
.bind(&entry.debug_id)
.bind(entry.source_url.as_deref())
.bind(&compressed)
.bind(project_id as i64)
.execute(pool)
.await?;
Ok(())
}
/// Store a chunk for later assembly.
pub async fn store_chunk(
pool: &DbPool,
checksum: &str,
data: &[u8],
project_id: u64,
) -> Result<()> {
sqlx::query(sql!(
"INSERT INTO upload_chunks (checksum, project_id, data) VALUES (?1, ?2, ?3)
ON CONFLICT (checksum, project_id) DO NOTHING"
))
.bind(checksum)
.bind(project_id as i64)
.bind(data)
.execute(pool)
.await?;
Ok(())
}
/// Return the subset of `checksums` that are not yet stored.
pub async fn find_missing_chunks(
pool: &DbPool,
checksums: &[String],
project_id: u64,
) -> Result<Vec<String>> {
if checksums.is_empty() {
return Ok(Vec::new());
}
let placeholders: Vec<String> = (1..=checksums.len()).map(|i| format!("?{i}")).collect();
let pid_idx = checksums.len() + 1;
let query = format!(
"SELECT checksum FROM upload_chunks WHERE project_id = ?{pid_idx} AND checksum IN ({})",
placeholders.join(", ")
);
let query = translate_sql(&query);
let mut q = sqlx::query_scalar::<_, String>(&query);
for cs in checksums {
q = q.bind(cs.clone());
}
q = q.bind(project_id as i64);
let found: Vec<String> = q.fetch_all(pool).await?;
let found_set: std::collections::HashSet<&str> = found.iter().map(|s| s.as_str()).collect();
Ok(checksums
.iter()
.filter(|cs| !found_set.contains(cs.as_str()))
.cloned()
.collect())
}
/// Read chunks in order and concatenate them into a single buffer.
pub async fn assemble_chunks(
pool: &DbPool,
checksums: &[String],
project_id: u64,
) -> Result<Vec<u8>> {
let mut result = Vec::new();
for checksum in checksums {
let row = sqlx::query(sql!(
"SELECT data FROM upload_chunks WHERE checksum = ?1 AND project_id = ?2"
))
.bind(checksum)
.bind(project_id as i64)
.fetch_optional(pool)
.await?;
match row {
Some(row) => {
let data: Vec<u8> = row.get("data");
result.extend_from_slice(&data);
}
None => anyhow::bail!("missing chunk: {checksum}"),
}
}
Ok(result)
}
/// Delete chunks after successful assembly.
pub async fn delete_chunks(pool: &DbPool, checksums: &[String], project_id: u64) -> Result<()> {
for checksum in checksums {
sqlx::query(sql!(
"DELETE FROM upload_chunks WHERE checksum = ?1 AND project_id = ?2"
))
.bind(checksum)
.bind(project_id as i64)
.execute(pool)
.await?;
}
Ok(())
}
/// Load and decompress a sourcemap by debug_id, then parse it.
pub async fn load_sourcemap(pool: &DbPool, debug_id: &str) -> Result<Option<sourcemap::SourceMap>> {
let row = sqlx::query(sql!("SELECT data FROM sourcemaps WHERE debug_id = ?1"))
.bind(debug_id)
.fetch_optional(pool)
.await?;
let row = match row {
Some(r) => r,
None => return Ok(None),
};
let compressed: Vec<u8> = row.get("data");
let raw = zstd::decode_all(compressed.as_slice()).context("zstd decompress sourcemap")?;
let sm = sourcemap::SourceMap::from_slice(&raw).context("parse sourcemap")?;
Ok(Some(sm))
}
/// Delete sourcemaps older than `max_age_secs`. Tied to the same retention
/// window as events so old debug artifacts don't accumulate forever.
pub async fn cleanup_old_sourcemaps(pool: &DbPool, max_age_secs: i64) -> Result<u64> {
let cutoff = chrono::Utc::now().timestamp() - max_age_secs;
let result = sqlx::query(sql!("DELETE FROM sourcemaps WHERE created_at < ?1"))
.bind(cutoff)
.execute(pool)
.await?;
Ok(result.rows_affected())
}
/// Delete old upload chunks (stale uploads that were never assembled).
pub async fn cleanup_stale_chunks(pool: &DbPool, max_age_secs: i64) -> Result<u64> {
let cutoff = chrono::Utc::now().timestamp() - max_age_secs;
let result = sqlx::query(sql!("DELETE FROM upload_chunks WHERE created_at < ?1"))
.bind(cutoff)
.execute(pool)
.await?;
Ok(result.rows_affected())
}