Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
451 changes: 272 additions & 179 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [ "parcel_sourcemap", "parcel_sourcemap_node", "parcel_sourcemap_wasm" ]
resolver = "2"

[profile.release]
lto = true
Expand Down
6 changes: 3 additions & 3 deletions parcel_sourcemap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ repository = "https://github.com/parcel-bundler/source-map"

[dependencies]
"vlq" = "0.5.1"
rkyv = "0.7.38"
rkyv = "0.8.16"
serde = {version = "1", features = ["derive"], optional = true}
serde_json = { version = "1", optional = true }
base64-simd = { version = "0.7", optional = true }
data-url = { version = "0.1.1", optional = true }
base64-simd = { version = "0.8", optional = true }
data-url = { version = "0.3.2", optional = true }

[features]
json = ["serde", "serde_json", "base64-simd", "data-url"]
63 changes: 25 additions & 38 deletions parcel_sourcemap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ use std::borrow::Cow;
use std::io;

use rkyv::{
archived_root,
ser::{
serializers::{AlignedSerializer, AllocScratch, CompositeSerializer},
Serializer,
},
AlignedVec, Archive, Deserialize, Infallible, Serialize,
from_bytes_unchecked, rancor::Error as RkyvError, to_bytes, util::AlignedVec, Archive,
Deserialize, Serialize,
};

use vlq_utils::{is_mapping_separator, read_relative_vlq};
Expand Down Expand Up @@ -265,18 +261,18 @@ impl SourceMap {
}

pub fn add_name(&mut self, name: &str) -> u32 {
return match self.inner.names.iter().position(|s| name.eq(s)) {
match self.inner.names.iter().position(|s| name.eq(s)) {
Some(i) => i as u32,
None => {
self.inner.names.push(String::from(name));
(self.inner.names.len() - 1) as u32
}
};
}
}

pub fn add_names<I: AsRef<str>>(&mut self, names: Vec<I>) -> Vec<u32> {
self.inner.names.reserve(names.len());
return names.iter().map(|n| self.add_name(n.as_ref())).collect();
names.iter().map(|n| self.add_name(n.as_ref())).collect()
}

pub fn get_name_index(&self, name: &str) -> Option<u32> {
Expand Down Expand Up @@ -342,20 +338,15 @@ impl SourceMap {
// Write the sourcemap instance to a buffer
pub fn to_buffer(&self, output: &mut AlignedVec) -> Result<(), SourceMapError> {
output.clear();
let mut serializer = CompositeSerializer::new(
AlignedSerializer::new(output),
AllocScratch::default(),
Infallible,
);
serializer.serialize_value(&self.inner)?;
let bytes = to_bytes::<RkyvError>(&self.inner)?;
output.extend_from_slice(&bytes);
Ok(())
}

// Create a sourcemap instance from a buffer
pub fn from_buffer(project_root: &str, buf: &[u8]) -> Result<SourceMap, SourceMapError> {
let archived = unsafe { archived_root::<SourceMapInner>(buf) };
// TODO: see if we can use the archived data directly rather than deserializing at all...
let inner = archived.deserialize(&mut Infallible)?;
let inner = unsafe { from_bytes_unchecked::<SourceMapInner, RkyvError>(buf)? };
Ok(SourceMap {
project_root: String::from(project_root),
inner,
Expand Down Expand Up @@ -397,11 +388,9 @@ impl SourceMap {
if generated_line >= 0 {
let mut line = mapping_line;
for mapping in line.mappings.iter_mut() {
match &mut mapping.original {
Some(original_mapping_location) => {
original_mapping_location.source = match source_indexes
.get(original_mapping_location.source as usize)
{
if let Some(original_mapping_location) = &mut mapping.original {
original_mapping_location.source =
match source_indexes.get(original_mapping_location.source as usize) {
Some(new_source_index) => *new_source_index,
None => {
return Err(SourceMapError::new(
Expand All @@ -410,19 +399,17 @@ impl SourceMap {
}
};

original_mapping_location.name = match original_mapping_location.name {
Some(name_index) => match names_indexes.get(name_index as usize) {
Some(new_name_index) => Some(*new_name_index),
None => {
return Err(SourceMapError::new(
SourceMapErrorType::NameOutOfRange,
));
}
},
None => None,
};
}
None => {}
original_mapping_location.name = match original_mapping_location.name {
Some(name_index) => match names_indexes.get(name_index as usize) {
Some(new_name_index) => Some(*new_name_index),
None => {
return Err(SourceMapError::new(
SourceMapErrorType::NameOutOfRange,
));
}
},
None => None,
};
}
}

Expand Down Expand Up @@ -557,7 +544,7 @@ impl SourceMap {

// Read source, original line, and original column if the
// mapping has them.
let original = if input.peek().cloned().map_or(true, is_mapping_separator) {
let original = if input.peek().cloned().is_none_or(is_mapping_separator) {
None
} else {
read_relative_vlq(&mut source, &mut input)?;
Expand All @@ -574,7 +561,7 @@ impl SourceMap {
));
}
},
if input.peek().cloned().map_or(true, is_mapping_separator) {
if input.peek().cloned().is_none_or(is_mapping_separator) {
None
} else {
read_relative_vlq(&mut name, &mut input)?;
Expand Down Expand Up @@ -757,7 +744,7 @@ impl SourceMap {
#[cfg(feature = "json")]
pub fn to_data_url(&mut self, source_root: Option<&str>) -> Result<String, SourceMapError> {
let buf = self.to_json(source_root)?;
let b64 = base64_simd::Base64::STANDARD.encode_to_boxed_str(buf.as_bytes());
let b64 = base64_simd::STANDARD.encode_to_string(buf.as_bytes());
Ok(format!(
"data:application/json;charset=utf-8;base64,{}",
b64
Expand Down
19 changes: 5 additions & 14 deletions parcel_sourcemap/src/mapping_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ impl MappingLine {

pub fn ensure_sorted(&mut self) {
if !self.is_sorted {
self.mappings
.sort_by(|a, b| a.generated_column.cmp(&b.generated_column));
self.mappings.sort_by_key(|a| a.generated_column);
self.is_sorted = true
}
}
Expand Down Expand Up @@ -86,23 +85,15 @@ impl MappingLine {
}

self.ensure_sorted();
let mut index = match self
let mut index = self
.mappings
.binary_search_by(|m| m.generated_column.cmp(&generated_column))
{
Ok(index) => index,
Err(index) => index,
};
.binary_search_by(|m| m.generated_column.cmp(&generated_column)).unwrap_or_else(|index| index);

if generated_column_offset < 0 {
let u_start_column = start_column as u32;
let start_index = match self
let start_index = self
.mappings
.binary_search_by(|m| m.generated_column.cmp(&u_start_column))
{
Ok(index) => index,
Err(index) => index,
};
.binary_search_by(|m| m.generated_column.cmp(&u_start_column)).unwrap_or_else(|index| index);

self.mappings.drain(start_index..index);
index = start_index;
Expand Down
7 changes: 2 additions & 5 deletions parcel_sourcemap/src/sourcemap_error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use rkyv::ser::serializers::{AllocScratchError, CompositeSerializerError};
use std::{convert::Infallible, io};

// Errors that can occur during processing/modifying source map
Expand Down Expand Up @@ -161,11 +160,9 @@ impl From<Infallible> for SourceMapError {
}
}

impl From<CompositeSerializerError<Infallible, AllocScratchError, Infallible>> for SourceMapError {
impl From<rkyv::rancor::Error> for SourceMapError {
#[inline]
fn from(
_err: CompositeSerializerError<Infallible, AllocScratchError, Infallible>,
) -> SourceMapError {
fn from(_err: rkyv::rancor::Error) -> SourceMapError {
SourceMapError::new(SourceMapErrorType::BufferError)
}
}
Expand Down
11 changes: 5 additions & 6 deletions parcel_sourcemap/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Based on https://github.com/getsentry/rust-sourcemap/blob/master/src/utils.rs
use std::borrow::Cow;
use std::iter::repeat;

pub fn is_abs_path(s: &str) -> bool {
if s.starts_with('/') || s.starts_with('\\') {
Expand Down Expand Up @@ -47,10 +46,9 @@ fn get_common_prefix_len<'a>(items: &'a [Cow<'a, [&'a str]>]) -> usize {
}

fn chunk_path(p: &str) -> Vec<&str> {
return p
.split(&['/', '\\'][..])
p.split(&['/', '\\'][..])
.filter(|x| !x.is_empty() && *x != ".")
.collect();
.collect()
}

// Helper function to calculate the path from a base file to a target file.
Expand All @@ -67,7 +65,7 @@ pub fn make_relative_path(base: &str, target: &str) -> String {
if target_str.contains(':') {
String::from(target_str)
} else {
return chunk_path(target_str).join("/");
chunk_path(target_str).join("/")
}
} else {
let target_path: Vec<&str> = chunk_path(target_str);
Expand All @@ -77,7 +75,8 @@ pub fn make_relative_path(base: &str, target: &str) -> String {
Cow::Borrowed(target_path.as_slice()),
];
let prefix_len = get_common_prefix_len(&items);
let mut rel_list: Vec<&str> = repeat("..").take(base_dir.len() - prefix_len).collect();
let mut rel_list: Vec<&str> =
std::iter::repeat_n("..", base_dir.len() - prefix_len).collect();
rel_list.extend_from_slice(&target_path[prefix_len..]);
rel_list.join("/")
}
Expand Down
2 changes: 1 addition & 1 deletion parcel_sourcemap_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ napi-derive = "2.12.2"
parcel_sourcemap = {path = "../parcel_sourcemap"}
serde = "1"
serde_json = "1"
rkyv = "0.7.38"
rkyv = "0.8.16"

[target.'cfg(target_os = "macos")'.dependencies]
jemallocator = {version = "0.3.2", features = ["disable_initial_exec_tls"]}
Expand Down
2 changes: 1 addition & 1 deletion parcel_sourcemap_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use napi::{
};
use napi_derive::napi;
use parcel_sourcemap::{Mapping, OriginalLocation, SourceMap, SourceMapError};
use rkyv::AlignedVec;
use rkyv::util::AlignedVec;

#[cfg(target_os = "macos")]
#[global_allocator]
Expand Down
3 changes: 2 additions & 1 deletion parcel_sourcemap_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ parcel_sourcemap = { path = "../parcel_sourcemap" }
serde = { version = "1.0", features = ["derive"] }
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
js-sys = "0.3"
rkyv = "0.7.38"
rkyv = "0.8.16"
serde-wasm-bindgen = "0.6.5"
21 changes: 11 additions & 10 deletions parcel_sourcemap_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

use js_sys::Uint8Array;
use parcel_sourcemap::{Mapping, OriginalLocation, SourceMap as NativeSourceMap, SourceMapError};
use rkyv::AlignedVec;
use rkyv::util::AlignedVec;
use serde::Serialize;
use serde_wasm_bindgen::{from_value, to_value};
use std::convert::TryFrom;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -90,9 +91,9 @@ impl SourceMap {
line_offset: i32,
column_offset: i32,
) -> Result<JsValue, JsValue> {
let sources_string: Vec<String> = sources.into_serde().unwrap();
let sources_content_string: Vec<String> = sources_content.into_serde().unwrap();
let names_string: Vec<String> = names.into_serde().unwrap();
let sources_string: Vec<String> = from_value(sources).unwrap();
let sources_content_string: Vec<String> = from_value(sources_content).unwrap();
let names_string: Vec<String> = from_value(names).unwrap();
self.map
.add_vlq_map(
vlq_mappings.as_bytes(),
Expand All @@ -117,7 +118,7 @@ impl SourceMap {
sourcesContent: self.map.get_sources_content().clone(),
names: self.map.get_names().clone(),
};
Ok(JsValue::from_serde(&result).unwrap())
Ok(to_value(&result).unwrap())
}

pub fn getMappings(&self) -> Result<JsValue, JsValue> {
Expand All @@ -136,19 +137,19 @@ impl SourceMap {
source: mapping.original.map(|p| p.source),
});
}
Ok(JsValue::from_serde(&mappings).unwrap())
Ok(to_value(&mappings).unwrap())
}

pub fn getSources(&self) -> Result<JsValue, JsValue> {
Ok(JsValue::from_serde(&self.map.get_sources()).unwrap())
Ok(to_value(&self.map.get_sources()).unwrap())
}

pub fn getSourcesContent(&self) -> Result<JsValue, JsValue> {
Ok(JsValue::from_serde(&self.map.get_sources_content()).unwrap())
Ok(to_value(&self.map.get_sources_content()).unwrap())
}

pub fn getNames(&self) -> Result<JsValue, JsValue> {
Ok(JsValue::from_serde(&self.map.get_names()).unwrap())
Ok(to_value(&self.map.get_names()).unwrap())
}

pub fn addName(&mut self, name: &str) -> u32 {
Expand Down Expand Up @@ -299,7 +300,7 @@ impl SourceMap {
.map
.find_closest_mapping(generated_line, generated_column)
{
Some(mapping) => JsValue::from_serde(&MappingResult::from(&mapping)).unwrap(),
Some(mapping) => to_value(&MappingResult::from(&mapping)).unwrap(),
None => JsValue::NULL,
}
}
Expand Down