Skip to content

Commit 94336f0

Browse files
authored
Merge pull request #24 from faststats-dev/feat/proguard
feat: add proguard support in backend
2 parents a94363a + 827763b commit 94336f0

File tree

8 files changed

+717
-146
lines changed

8 files changed

+717
-146
lines changed

.changeset/brown-suits-lie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@faststats/sourcemap-uploader-plugin": patch
3+
---
4+
5+
fix: add javascript as mapping type

.github/workflows/backend.yml

Lines changed: 0 additions & 34 deletions
This file was deleted.

apps/backend/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod auth;
22
mod config;
33
mod crypto;
44
mod error;
5+
mod mappings;
56
mod routes;
67
mod storage;
78

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use uuid::Uuid;
2+
3+
use crate::error::AppError;
4+
use crate::storage::Storage;
5+
6+
use super::{OriginalPosition, s3_key};
7+
8+
pub async fn ingest(
9+
storage: &Storage,
10+
project_id: Uuid,
11+
build_id: &str,
12+
sourcemaps: &[(String, String)], // (file_name, sourcemap_content)
13+
) -> Result<(), AppError> {
14+
for (file_name, content) in sourcemaps {
15+
let key = s3_key(project_id, build_id, file_name);
16+
storage.put(&key, content.as_bytes()).await?;
17+
}
18+
Ok(())
19+
}
20+
21+
pub fn apply(
22+
data: &[u8],
23+
_file_name: &str,
24+
line: u32,
25+
column: u32,
26+
) -> Result<OriginalPosition, AppError> {
27+
let source_map = sourcemap::SourceMap::from_slice(data)
28+
.map_err(|e| AppError::BadRequest(format!("invalid sourcemap: {e}")))?;
29+
let token = source_map
30+
.lookup_token(line.saturating_sub(1), column.saturating_sub(1))
31+
.ok_or(AppError::NotFound)?;
32+
let source = token.get_source().ok_or(AppError::NotFound)?;
33+
let src_line = token.get_src_line();
34+
let src_col = token.get_src_col();
35+
36+
if src_line == u32::MAX || src_col == u32::MAX {
37+
return Err(AppError::NotFound);
38+
}
39+
40+
Ok(OriginalPosition {
41+
source: source.to_string(),
42+
line: src_line.saturating_add(1),
43+
column: src_col.saturating_add(1),
44+
name: token.get_name().map(ToString::to_string),
45+
})
46+
}
47+
48+
pub fn map_file_name(file_name: &str) -> String {
49+
if file_name.ends_with(".map") {
50+
file_name.to_string()
51+
} else {
52+
format!("{file_name}.map")
53+
}
54+
}

apps/backend/src/mappings/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
pub mod javascript;
2+
pub mod proguard;
3+
4+
use serde::Serialize;
5+
use uuid::Uuid;
6+
7+
use crate::error::AppError;
8+
9+
#[derive(Serialize)]
10+
#[serde(rename_all = "camelCase")]
11+
pub struct OriginalPosition {
12+
pub source: String,
13+
pub line: u32,
14+
pub column: u32,
15+
pub name: Option<String>,
16+
}
17+
18+
pub(crate) fn require_non_empty(field: &str, value: &str) -> Result<(), AppError> {
19+
if value.trim().is_empty() {
20+
return Err(AppError::BadRequest(format!("{field} is required")));
21+
}
22+
Ok(())
23+
}
24+
25+
pub(crate) fn s3_key(project_id: Uuid, build_id: &str, file_name: &str) -> String {
26+
format!("{project_id}/{build_id}/{file_name}")
27+
}

0 commit comments

Comments
 (0)