diff --git a/Cargo.lock b/Cargo.lock index 80bd63ca8a..9bfcb88ccd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5263,6 +5263,7 @@ dependencies = [ "thiserror 2.0.21", "tiberius", "tiff", + "time", "tokio", "tokio-postgres-rustls", "tokio-util", @@ -6468,6 +6469,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb87b95ec50ddfa440816d227a17b2ccbdda963a316a727fda0fc4334f7d134" dependencies = [ "deranged", + "js-sys", "num-conv", "powerfmt", "serde_core", diff --git a/crates/data/Cargo.toml b/crates/data/Cargo.toml index d5e3d7bac6..cb7789aeaf 100644 --- a/crates/data/Cargo.toml +++ b/crates/data/Cargo.toml @@ -50,6 +50,12 @@ ruzstd = { workspace = true } lerc-reader = { workspace = true } lerc-core = { workspace = true } +# A Shapefile's `.dbf` header holds the day it was written, which `dbase` reads from `time`'s +# clock. Without this feature that clock is std's, which panics in the browser, so a Shapefile +# export stopped the web app (opengeos/GeoLibre#3648); with it, the clock is JavaScript's. +[target.'cfg(target_arch = "wasm32")'.dependencies] +time = { version = "0.3", default-features = false, features = ["wasm-bindgen"] } + [target.'cfg(not(target_arch = "wasm32"))'.dependencies] rusqlite = { workspace = true } postgres = { workspace = true } diff --git a/crates/data/src/io.rs b/crates/data/src/io.rs index e5dd5d686d..adb0983d7a 100644 --- a/crates/data/src/io.rs +++ b/crates/data/src/io.rs @@ -6042,6 +6042,43 @@ mod tests { use super::*; use std::io::Write; + /// A Shapefile's `.dbf` header holds the day it was written, which `dbase` takes from `time`'s + /// clock: std's unless `time`'s `wasm-bindgen` feature is on, and std's clock panics on + /// wasm32-unknown-unknown, so every Shapefile export stopped the web app with no message + /// (opengeos/GeoLibre#3648). The browser build can't run here, so this checks both halves on + /// the host: that writing a `.dbf` stamps today's date (it reads the clock), and that this + /// crate turns the feature on for wasm32. + #[test] + fn a_shapefile_dbf_is_stamped_with_the_day_it_was_written() { + let today = || { + let secs = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs(); + let (y, m, d) = crate::date::civil_from_days(i64::try_from(secs / 86_400).unwrap()); + (y - 1900, m, d) + }; + let mut ds = Dataset::new("pts", GeometryType::Point, vec![Field::new("n", FieldType::Integer)]); + ds.push(Some(Geometry::Point(Coord::new(1.0, 2.0))), vec![Value::Int(1)]); + let before = today(); + let (parts, _) = shapefile_parts_in(&ds, Crs::LonLat).unwrap(); + let after = today(); + let dbf = &parts.iter().find(|(e, _)| *e == "dbf").unwrap().1; + // Bytes 1 to 3: the year since 1900, the month and the day it was written (UTC), either + // side of a midnight passed during the write. + let stamped = (i64::from(dbf[1]), i64::from(dbf[2]), i64::from(dbf[3])); + assert!(stamped == before || stamped == after, "{stamped:?}, written between {before:?} and {after:?}"); + + // A plain text match, so the test needs no TOML parser: it is loud, not silent, if the + // section is ever written another way (a `[target.….dependencies.time]` table). + let manifest = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml")).replace("\r\n", "\n"); + let wasm = manifest + .split("\n[") + .find(|section| section.starts_with("target.'cfg(target_arch = \"wasm32\")'.dependencies]")) + .expect("crates/data/Cargo.toml has a [target.'cfg(target_arch = \"wasm32\")'.dependencies] section"); + assert!( + wasm.lines().any(|l| l.starts_with("time ") && l.contains("\"wasm-bindgen\"")), + "time's wasm-bindgen feature is on for wasm32, or a Shapefile export panics in the browser:\n{wasm}" + ); + } + /// A write to a name nothing holds reserves it first: a second write to it is refused before /// anything is written, and dropping the write removes the reservation and the folder made for /// it; once installed, both stay (#2865).