Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add sourcemaps inject command #1469

Merged
merged 20 commits into from
Feb 23, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions src/commands/sourcemaps/inject.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use std::fs::File;
use std::io::{BufRead, BufReader, Seek, Write};
use std::fs::{self, File};
use std::io::{Seek, Write};
use std::path::{Path, PathBuf};

use anyhow::{bail, Context, Result};
use clap::{Arg, ArgMatches, Command};
use glob::glob;
use log::{debug, warn};
use serde_json::Value;
use symbolic::debuginfo::js;
use uuid::Uuid;

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";
const SOURCEMAP_URL_COMMENT_PREFIX: &str = "//# sourceMappingURL=";

pub fn make_command(command: Command) -> Command {
command
Expand Down Expand Up @@ -56,24 +56,15 @@ fn fixup_files(paths: &[PathBuf]) -> Result<()> {

debug!("Processing js file {}", js_path.display());

let f = File::open(js_path).context(format!("Failed to open {}", js_path.display()))?;
let f = BufReader::new(f);
let file =
fs::read_to_string(js_path).context(format!("Failed to open {}", js_path.display()))?;

let mut sourcemap_url = None;
for line in f.lines() {
let line = line.context(format!("Failed to process {}", js_path.display()))?;

if line.starts_with(DEBUGID_COMMENT_PREFIX) {
debug!("File {} was previously processed", js_path.display());
continue 'paths;
}

if let Some(url) = line.strip_prefix(SOURCEMAP_URL_COMMENT_PREFIX) {
sourcemap_url = Some(PathBuf::from(url));
}
if js::discover_debug_id(&file).is_some() {
debug!("File {} was previously processed", js_path.display());
continue 'paths;
loewenheim marked this conversation as resolved.
Show resolved Hide resolved
}

let Some(sourcemap_url) = sourcemap_url else {
let Some(sourcemap_url) = js::discover_sourcemaps_location(&file) else {
debug!("File {} does not contain a sourcemap url", js_path.display());
continue;
};
Expand Down