Skip to content

Commit

Permalink
ref: Use sourcemap debugid features (#1686)
Browse files Browse the repository at this point in the history
  • Loading branch information
loewenheim authored Jul 19, 2023
1 parent e297bb9 commit c62f045
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ sentry = { version = "0.31.2", default-features = false, features = [
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.93"
sha1_smol = { version = "1.0.0", features = ["serde"] }
sourcemap = { version = "6.2.3", features = ["ram_bundle"] }
sourcemap = { version = "6.3.0", features = ["ram_bundle"] }
symbolic = { version = "12.1.5", features = ["debuginfo-serde", "il2cpp"] }
thiserror = "1.0.38"
url = "2.3.1"
Expand Down
2 changes: 1 addition & 1 deletion src/utils/sourcemaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ impl SourceMapProcessor {
.filter_map(|artifact| Digest::from_str(&artifact.sha1).ok())
.collect();

for mut source in self.sources.values_mut() {
for source in self.sources.values_mut() {
if let Ok(checksum) = source.checksum() {
if already_uploaded_checksums.contains(&checksum) {
source.already_uploaded = true;
Expand Down
51 changes: 29 additions & 22 deletions src/utils/sourcemaps/inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ use std::fmt;
use std::io::{BufRead, Write};
use std::path::PathBuf;

use anyhow::{bail, Result};
use anyhow::{bail, Context, Result};
use lazy_static::lazy_static;
use log::debug;
use sentry::types::DebugId;
use serde_json::Value;

const CODE_SNIPPET_TEMPLATE: &str = r#"!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="__SENTRY_DEBUG_ID__")}catch(e){}}();"#;
const DEBUGID_PLACEHOLDER: &str = "__SENTRY_DEBUG_ID__";
const SOURCEMAP_DEBUGID_KEY: &str = "debug_id";
const DEBUGID_COMMENT_PREFIX: &str = "//# debugId";

lazy_static! {
Expand Down Expand Up @@ -303,27 +302,35 @@ pub fn debug_id_from_bytes_hashed(bytes: &[u8]) -> DebugId {
///
/// In either case, the value of the `debug_id` key is returned.
pub fn fixup_sourcemap(sourcemap_contents: &mut Vec<u8>) -> Result<(DebugId, bool)> {
let mut sourcemap: Value = serde_json::from_slice(sourcemap_contents)?;

let Some(map) = sourcemap.as_object_mut() else {
bail!("Invalid sourcemap");
};

match map.get(SOURCEMAP_DEBUGID_KEY) {
Some(id) => {
let debug_id = serde_json::from_value(id.clone())?;
debug!("Sourcemap already has a debug id");
Ok((debug_id, false))
match sourcemap::decode_slice(sourcemap_contents).context("Invalid sourcemap")? {
sourcemap::DecodedMap::Regular(mut sm) => {
if let Some(debug_id) = sm.get_debug_id() {
debug!("Sourcemap already has a debug id");
Ok((debug_id, false))
} else {
let debug_id = debug_id_from_bytes_hashed(sourcemap_contents);
sm.set_debug_id(Some(debug_id));

sourcemap_contents.clear();
sm.to_writer(sourcemap_contents)?;
Ok((debug_id, true))
}
}

None => {
let debug_id = debug_id_from_bytes_hashed(sourcemap_contents);
let id = serde_json::to_value(debug_id)?;
map.insert(SOURCEMAP_DEBUGID_KEY.to_string(), id);

sourcemap_contents.clear();
serde_json::to_writer(sourcemap_contents, &sourcemap)?;
Ok((debug_id, true))
sourcemap::DecodedMap::Hermes(mut smh) => {
if let Some(debug_id) = smh.get_debug_id() {
debug!("Sourcemap already has a debug id");
Ok((debug_id, false))
} else {
let debug_id = debug_id_from_bytes_hashed(sourcemap_contents);
smh.set_debug_id(Some(debug_id));

sourcemap_contents.clear();
smh.to_writer(sourcemap_contents)?;
Ok((debug_id, true))
}
}
sourcemap::DecodedMap::Index(_) => {
bail!("DebugId injection is not supported for sourcemap indexes")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/sourcemaps/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn command_sourcemaps_upload_some_debugids_v2() {
let upload_endpoints = mock_common_upload_endpoints(
ServerBehavior::ModernV2,
ChunkOptions {
missing_chunks: vec!["5e102ab3da27af9d1095a9c847d4e92a57fe01af".to_string()],
missing_chunks: vec!["ff16e0ac593a74b454cc34814f6249f45a1a2dfe".to_string()],
chunk_size: 524288,
},
);
Expand Down

0 comments on commit c62f045

Please sign in to comment.