Skip to content

The headless browser for AI agents and web scraping

fix(storage): retry profile writes and preserve browser strings

SGavrl677f6d2fbb0b
diff --git a/Cargo.lock b/Cargo.lock
index 767d6170..07689264 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1992,6 +1992,7 @@ dependencies = [
"obscura-dom",
"obscura-js",
"obscura-net",
+ "serde",
"serde_json",
"thiserror 2.0.18",
"tokio",
diff --git a/crates/obscura-browser/src/context.rs b/crates/obscura-browser/src/context.rs
index 96cadf97..7e36e3cb 100644
--- a/crates/obscura-browser/src/context.rs
+++ b/crates/obscura-browser/src/context.rs
@@ -1,7 +1,7 @@
use std::path::PathBuf;
use std::sync::Arc;
-use obscura_js::ops::{IndexedDbStorage, OriginStorage};
+use obscura_js::ops::OriginStorage;
use obscura_net::{CookieJar, ObscuraHttpClient, RobotsCache};
pub struct BrowserContext {
@@ -11,9 +11,6 @@ pub struct BrowserContext {
/// rather than by a V8 realm, so an entry written in one document is still
/// there in the next (issue #678).
pub local_storage: Arc<OriginStorage>,
- /// IndexedDB records and schema, keyed by origin. Owned by the context, so
- /// a page reload or a second tab sees the same databases.
- pub indexed_db: Arc<IndexedDbStorage>,
pub http_client: Arc<ObscuraHttpClient>,
pub user_agent: String,
pub platform: String,
@@ -77,20 +74,32 @@ fn write_file_atomically(path: &std::path::Path, bytes: &[u8]) -> std::io::Resul
/// Restore `{storage_dir}/localStorage/*.json` into a fresh store.
fn load_web_storage(storage: &OriginStorage, dir: Option<&PathBuf>) {
+ use std::io::Read;
let Some(dir) = dir else { return };
let root = dir.join("localStorage");
let Ok(read_dir) = std::fs::read_dir(&root) else { return };
for entry in read_dir.flatten() {
let path = entry.path();
- if path.extension().and_then(|ext| ext.to_str()) != Some("json") {
+ if path.extension().and_then(|ext| ext.to_str()) != Some("json")
+ || !entry.file_type().is_ok_and(|kind| kind.is_file()) {
continue;
}
- let Ok(contents) = std::fs::read_to_string(&path) else {
+ // A 5 MiB store can expand sixfold when encoded as JSON escapes.
+ let mut contents = String::new();
+ let limit = 32 * 1024 * 1024;
+ let read = std::fs::File::open(&path).and_then(|file| file.take(limit + 1).read_to_string(&mut contents));
+ if read.is_err() || contents.len() as u64 > limit {
tracing::warn!("Failed to read {}", path.display());
continue;
- };
+ }
match serde_json::from_str::<StoredOriginStorage>(&contents) {
- Ok(stored) => storage.load(&stored.origin, stored.entries),
+ Ok(stored) => {
+ let valid_origin = stored.origin == "file://" || url::Url::parse(&stored.origin)
+ .is_ok_and(|url| url.origin().ascii_serialization() == stored.origin && stored.origin != "null");
+ if !valid_origin || !storage.load(&stored.origin, stored.entries) {
+ tracing::warn!("Ignoring invalid or over-budget {}", path.display());
+ }
+ }
Err(e) => tracing::warn!("Ignoring unreadable {}: {}", path.display(), e),
}
}
@@ -191,7 +200,6 @@ impl BrowserContext {
id,
cookie_jar,
local_storage,
- indexed_db: Arc::new(IndexedDbStorage::default()),
http_client,
user_agent: resolved_ua,
platform,
@@ -257,11 +265,6 @@ impl BrowserContext {
} else {
Arc::new(OriginStorage::default())
},
- indexed_db: if persistent {
- self.indexed_db.clone()
- } else {
- Arc::new(IndexedDbStorage::default())
- },
http_client: Arc::new(client),
user_agent: self.user_agent.clone(),
platform: self.platform.clone(),
@@ -289,34 +292,28 @@ impl BrowserContext {
if !self.local_storage.is_dirty() {
return;
}
- let changed = self.local_storage.changed_origins();
let root = dir.join("localStorage");
if let Err(e) = std::fs::create_dir_all(&root) {
tracing::warn!("Failed to create {}: {}", root.display(), e);
return;
}
- for origin in &changed {
+ self.local_storage.flush_changed(|origin, entries| {
let path = root.join(format!("{}.json", storage_file_stem(origin)));
- let entries = self.local_storage.snapshot(origin);
- if entries.is_empty() {
+ let result = if entries.is_empty() {
match std::fs::remove_file(&path) {
- Ok(()) => {}
- Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
- Err(e) => tracing::warn!("Failed to remove {}: {}", path.display(), e),
- }
- continue;
- }
- let stored = StoredOriginStorage { origin: origin.clone(), entries };
- match serde_json::to_string(&stored) {
- Ok(payload) => {
- if let Err(e) = write_file_atomically(&path, payload.as_bytes()) {
- tracing::warn!("Failed to save {}: {}", path.display(), e);
- }
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
+ result => result,
}
- Err(e) => tracing::warn!("Failed to encode localStorage: {}", e),
+ } else {
+ let stored = StoredOriginStorage { origin: origin.to_string(), entries: entries.to_vec() };
+ serde_json::to_vec(&stored).map_err(std::io::Error::other)
+ .and_then(|bytes| write_file_atomically(&path, &bytes))
+ };
+ match result {
+ Ok(()) => true,
+ Err(e) => { tracing::warn!("Failed to persist {}: {}", path.display(), e); false }
}
- }
- self.local_storage.take_changed();
+ });
}
/// Persist cookies and Web Storage if storage_dir is configured.
@@ -390,6 +387,123 @@ mod tests {
/// --storage-dir has to survive a restart, which is the whole point of a
/// profile: a second context over the same directory reads back what the
/// first one wrote.
+ #[test]
+ fn local_storage_retries_failed_writes_without_rewriting_successes() {
+ let dir = temp_storage_dir("write-retry");
+ let context = BrowserContext::with_storage("write-retry".into(), Some(dir.clone()));
+ let root = dir.join("localStorage");
+ std::fs::create_dir_all(&root).unwrap();
+ let failed = root.join(format!("{}.json", storage_file_stem("https://failed.test")));
+ let blocked = failed.with_extension("json.tmp");
+ std::fs::create_dir(&blocked).unwrap();
+ assert!(context.local_storage.set("https://failed.test", "token".into(), "kept".into()));
+ assert!(context.local_storage.set("https://saved.test", "token".into(), "saved".into()));
+ context.save_local_storage();
+ assert!(context.local_storage.is_dirty(), "failed writes must remain retryable");
+ assert_eq!(context.local_storage.changed_origins(), vec!["https://failed.test"]);
+ std::fs::remove_dir(&blocked).unwrap();
+ context.save_local_storage();
+ assert!(!context.local_storage.is_dirty());
+ let reloaded = BrowserContext::with_storage("reloaded".into(), Some(dir.clone()));
+ assert_eq!(reloaded.local_storage.get("https://failed.test", "token").as_deref(), Some("kept"));
+ assert_eq!(reloaded.local_storage.get("https://saved.test", "token").as_deref(), Some("saved"));
+ std::fs::remove_dir_all(dir).unwrap();
+ }
+
+ #[test]
+ #[cfg(target_os = "linux")]
+ fn local_storage_ignores_non_regular_profile_files() {
+ let dir = temp_storage_dir("non-regular");
+ let root = dir.join("localStorage");
+ std::fs::create_dir_all(&root).unwrap();
+ let pipe = root.join("blocked.json");
+ assert!(std::process::Command::new("mkfifo").arg(&pipe).status().unwrap().success());
+ let (tx, rx) = std::sync::mpsc::channel();
+ let profile = dir.clone();
+ let reader = std::thread::spawn(move || {
+ let context = BrowserContext::with_storage("reader".into(), Some(profile));
+ tx.send(context.local_storage.is_dirty()).unwrap();
+ });
+ let completed = rx.recv_timeout(std::time::Duration::from_secs(2));
+ if completed.is_err() {
+ // Release the broken reader before reporting the failure.
+ std::fs::write(&pipe, b"{}").unwrap();
+ }
+ reader.join().unwrap();
+ std::fs::remove_dir_all(dir).unwrap();
+ assert_eq!(completed.ok(), Some(false), "profile loading must not block on a FIFO");
+ }
+
+ #[test]
+ fn local_storage_retries_failed_deletions() {
+ let dir = temp_storage_dir("delete-retry");
+ let context = BrowserContext::with_storage("delete-retry".into(), Some(dir.clone()));
+ assert!(context.local_storage.set("https://app.test", "token".into(), "old".into()));
+ context.save_local_storage();
+ let path = dir.join("localStorage").join(format!("{}.json", storage_file_stem("https://app.test")));
+ std::fs::remove_file(&path).unwrap();
+ std::fs::create_dir(&path).unwrap();
+ context.local_storage.clear("https://app.test");
+ context.save_local_storage();
+ assert!(context.local_storage.is_dirty(), "a failed deletion must not be acknowledged");
+ std::fs::remove_dir(&path).unwrap();
+ context.save_local_storage();
+ assert!(!context.local_storage.is_dirty());
+ assert!(!path.exists());
+ std::fs::remove_dir_all(dir).unwrap();
+ }
+
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn local_storage_retries_mutations_made_during_a_flush() {
+ use std::io::Read;
+ use std::sync::mpsc;
+ let dir = temp_storage_dir("concurrent-flush");
+ let context = Arc::new(BrowserContext::with_storage("writer".into(), Some(dir.clone())));
+ let other = context.isolated_copy("mutator".into(), true);
+ let root = dir.join("localStorage");
+ std::fs::create_dir_all(&root).unwrap();
+ let path = root.join(format!("{}.json", storage_file_stem("https://app.test")));
+ let pipe = path.with_extension("json.tmp");
+ assert!(std::process::Command::new("mkfifo").arg(&pipe).status().unwrap().success());
+ let old = "x".repeat(64 * 1024);
+ assert!(context.local_storage.set("https://app.test", "token".into(), old.clone()));
+ let (opened_tx, opened_rx) = mpsc::channel();
+ let (release_tx, release_rx) = mpsc::channel();
+ let reader = std::thread::spawn(move || {
+ let mut file = std::fs::File::open(pipe).unwrap();
+ opened_tx.send(()).unwrap();
+ release_rx.recv().unwrap();
+ let mut bytes = Vec::new();
+ file.read_to_end(&mut bytes).unwrap();
+ bytes
+ });
+ let writer_context = context.clone();
+ let writer = std::thread::spawn(move || writer_context.save_local_storage());
+ opened_rx.recv_timeout(std::time::Duration::from_secs(2)).unwrap();
+ let (attempted_tx, attempted_rx) = mpsc::channel();
+ let (changed_tx, changed_rx) = mpsc::channel();
+ let mutator = std::thread::spawn(move || {
+ attempted_tx.send(()).unwrap();
+ assert!(other.local_storage.set("https://app.test", "token".into(), "latest".into()));
+ changed_tx.send(()).unwrap();
+ });
+ attempted_rx.recv_timeout(std::time::Duration::from_secs(2)).unwrap();
+ // Let the old unlocked writer lose a mutation while its FIFO is held.
+ // A serialized writer keeps the mutation blocked until the flush ends.
+ let _ = changed_rx.recv_timeout(std::time::Duration::from_millis(500));
+ release_tx.send(()).unwrap();
+ writer.join().unwrap();
+ mutator.join().unwrap();
+ let written: StoredOriginStorage = serde_json::from_slice(&reader.join().unwrap()).unwrap();
+ assert_eq!(written.entries, vec![("token".into(), old)]);
+ assert!(context.local_storage.is_dirty(), "a newer mutation must survive the older flush acknowledgement");
+ context.save_local_storage();
+ let reloaded = BrowserContext::with_storage("reloaded".into(), Some(dir.clone()));
+ assert_eq!(reloaded.local_storage.get("https://app.test", "token").as_deref(), Some("latest"));
+ std::fs::remove_dir_all(dir).unwrap();
+ }
+
#[test]
fn local_storage_round_trips_through_the_storage_dir() {
let dir = temp_storage_dir("roundtrip");
diff --git a/crates/obscura-browser/src/page.rs b/crates/obscura-browser/src/page.rs
index 9a39d560..47d97008 100644
--- a/crates/obscura-browser/src/page.rs
+++ b/crates/obscura-browser/src/page.rs
@@ -1842,7 +1842,6 @@ impl Page {
// Web Storage lives outside the realm so a navigation cannot drop it.
rt.set_local_storage(self.context.local_storage.clone());
rt.set_session_storage(self.session_storage.clone());
- rt.set_indexed_db(self.context.indexed_db.clone());
rt.set_http_client(self.http_client.clone());
rt.set_callbacks(self.callbacks.clone());
rt.set_blocked_urls(self.blocked_url_patterns.clone());
@@ -6774,6 +6773,7 @@ mod tests {
page.navigate(&origin_a).await.unwrap();
let back = page.evaluate("localStorage.getItem('l1')");
assert_eq!(back.as_str(), Some("local-value"));
+ assert_eq!(page.evaluate("sessionStorage.getItem('s1')").as_str(), Some("session-value"));
}
#[tokio::test]
@@ -6818,6 +6818,16 @@ mod tests {
view.get("session") == Some(&serde_json::Value::Null),
"sessionStorage must not cross tabs: {view:?}"
);
+ assert_eq!(second.evaluate("(() => { localStorage.setItem('shared', 'from-tab-two'); sessionStorage.setItem('tab', 'from-tab-two'); return localStorage.getItem('shared'); })()"),
+ serde_json::json!("from-tab-two"));
+ second.suspend_js();
+ first.suspend_js();
+ first.resume_js();
+ assert_eq!(first.evaluate("[localStorage.getItem('shared'), sessionStorage.getItem('tab')]"),
+ serde_json::json!(["from-tab-two", "from-tab-one"]));
+ first.suspend_js();
+ second.resume_js();
+ assert_eq!(second.evaluate("sessionStorage.getItem('tab')").as_str(), Some("from-tab-two"));
}
/// `/` puts its iframe inside a closed shadow root, `/plain.html` puts the
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index e7591917..877bdc83 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -12240,13 +12240,32 @@ const _storageSlot = (value) => {
return slot;
};
const _storageOp = (slot) => slot.local ? __obscuraCore.ops.op_local_storage : __obscuraCore.ops.op_session_storage;
+// Preserve WTF-16 through UTF-8 ops and profile JSON without changing normal strings.
+const _storageEncode = (value) => value.startsWith('\0') || !value.isWellFormed()
+ ? '\0' + JSON.stringify(value) : value;
+const _storageDecode = (value) => {
+ if (typeof value === 'string' && value.startsWith('\0')) {
+ try {
+ const decoded = JSON.parse(value.slice(1));
+ if (typeof decoded === 'string' && (decoded.startsWith('\0') || !decoded.isWellFormed())) return decoded;
+ } catch (_) {}
+ }
+ return value;
+};
const _storageCall = (slot, command, key, value) => {
// The WebIDL string conversions stay outside the try: a Symbol argument has
// to raise TypeError, not fall through to the null that means "no backend".
- const k = String(key);
- const v = value === undefined ? '' : String(value);
- try { return JSON.parse(_storageOp(slot)(command, k, v)); }
+ const k = command === 'key' ? `${key}` : _storageEncode(`${key}`);
+ const v = command === 'set' ? _storageEncode(`${value}`) : '';
+ let result;
+ try { result = JSON.parse(_storageOp(slot)(command, k, v)); }
catch (_) { return null; }
+ if (result && result.error === 'SecurityError')
+ throw new DOMException('Storage is unavailable for an opaque origin.', 'SecurityError');
+ if (command === 'get' || command === 'key') return _storageDecode(result);
+ if (command === 'snapshot' && Array.isArray(result))
+ return result.map(([key, value]) => [_storageDecode(key), _storageDecode(value)]);
+ return result;
};
// [key, value] pairs in insertion order, which is the order Storage.key() is
// defined over.
@@ -12265,12 +12284,11 @@ Storage.prototype.setItem = function(k, v) {
Storage.prototype.removeItem = function(k) { _storageCall(_storageSlot(this), 'remove', k, ''); };
Storage.prototype.clear = function() { _storageCall(_storageSlot(this), 'clear', '', ''); };
Storage.prototype.key = function(i) {
- const entries = _storageEntries(_storageSlot(this));
i = i >>> 0;
- return i < entries.length ? entries[i][0] : null;
+ return _storageCall(_storageSlot(this), 'key', i, '');
};
Object.defineProperty(Storage.prototype, 'length', {
- get: function() { return _storageEntries(_storageSlot(this)).length; },
+ get: function() { return _storageCall(_storageSlot(this), 'length', '', ''); },
configurable: true,
});
@@ -12299,8 +12317,13 @@ const _mkStore = (local) => {
_storageSlots.set(proxy, slot);
return proxy;
};
-globalThis.localStorage = _mkStore(true);
-globalThis.sessionStorage = _mkStore(false);
+for (const [name, local] of [['localStorage', true], ['sessionStorage', false]]) {
+ const store = _mkStore(local);
+ Object.defineProperty(globalThis, name, {configurable: true, enumerable: true, get() {
+ _storageCall(_storageSlot(store), 'access', '', '');
+ return store;
+ }});
+}
globalThis.btoa = globalThis.btoa || ((s) => { s = String(s); const b = new Uint8Array(s.length); for (let i = 0; i < s.length; i++) { const cp = s.charCodeAt(i); if (cp > 0xFF) throw new DOMException("The string to be encoded contains characters outside of the Latin1 range.", "InvalidCharacterError"); b[i] = cp; } const c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; let r=""; for(let i=0;i<b.length;i+=3){const a=b[i],bb=b[i+1]??0,cc=b[i+2]??0; r+=c[a>>2]+c[((a&3)<<4)|(bb>>4)]+(i+1<b.length?c[((bb&15)<<2)|(cc>>6)]:"=")+(i+2<b.length?c[cc&63]:"=");} return r; });
globalThis.atob = globalThis.atob || ((s) => {
@@ -15332,810 +15355,129 @@ globalThis.RTCPeerConnection = class RTCPeerConnection {
globalThis.RTCSessionDescription = class RTCSessionDescription { constructor(d){this.type=d?.type;this.sdp=d?.sdp;} };
globalThis.RTCIceCandidate = class RTCIceCandidate { constructor(d){this.candidate=d?.candidate||'';} };
-// IndexedDB. The object graph, the event ordering and the key handling live
-// here; the records and the schema live in Rust, keyed by origin, so they
-// survive the realm rebuild that every navigation performs and a profile
-// reload. The previous shim handed each object store its own Map and never
-// fired onupgradeneeded, so a schema-created database was unreachable and its
-// first read threw.
-(() => {
- const _idbBackend = (payload) => {
+// Minimal but spec-shape-correct IndexedDB shim. We don't persist anything,
+// but authentication libraries (Firebase, Supabase, dexie) hang forever on
+// the first `get` because their request's `onsuccess` is never called. Fire
+// `onsuccess` asynchronously with `null` so reads complete-but-empty, which
+// most libraries treat as a cache miss and fall back to the network.
+function _idbRequest(produceResult) {
+ const req = {
+ result: undefined,
+ error: null,
+ source: null,
+ transaction: null,
+ readyState: 'pending',
+ onsuccess: null,
+ onerror: null,
+ addEventListener(type, fn) { req['on' + type] = fn; },
+ removeEventListener(type, fn) { if (req['on' + type] === fn) req['on' + type] = null; },
+ };
+ Promise.resolve().then(() => {
try {
- const reply = JSON.parse(__obscuraCore.ops.op_indexed_db(JSON.stringify(payload)));
- if (reply && reply.ok) return reply;
- return { ok: false, error: (reply && reply.error) || 'IndexedDB request failed' };
+ req.result = produceResult();
+ req.readyState = 'done';
+ if (typeof req.onsuccess === 'function') {
+ try { req.onsuccess({ target: req, type: 'success' }); } catch (e) {}
+ }
} catch (e) {
- return { ok: false, error: 'IndexedDB backend is unavailable' };
- }
- };
- const _idbFail = (name, message) => new DOMException(message, name);
-
- // Key ordering follows the spec's type order, so a numeric key sorts before a
- // string one and an array key after both.
- const _keyRank = (value) => {
- if (typeof value === 'number') return 0;
- if (value instanceof Date) return 1;
- if (typeof value === 'string') return 2;
- if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) return 3;
- if (Array.isArray(value)) return 4;
- return 5;
- };
- const _idbCmp = (a, b) => {
- const ra = _keyRank(a);
- const rb = _keyRank(b);
- if (ra !== rb) return ra < rb ? -1 : 1;
- if (ra === 0) return a === b ? 0 : (a < b ? -1 : 1);
- if (ra === 1) {
- const ta = a.getTime();
- const tb = b.getTime();
- return ta === tb ? 0 : (ta < tb ? -1 : 1);
- }
- if (ra === 2) return a === b ? 0 : (a < b ? -1 : 1);
- if (ra === 4) {
- const length = Math.max(a.length, b.length);
- for (let i = 0; i < length; i++) {
- if (i >= a.length) return -1;
- if (i >= b.length) return 1;
- const inner = _idbCmp(a[i], b[i]);
- if (inner !== 0) return inner;
+ req.error = e; req.readyState = 'done';
+ if (typeof req.onerror === 'function') {
+ try { req.onerror({ target: req, type: 'error' }); } catch (e2) {}
}
- return 0;
- }
- return 0;
- };
- const _idbValidKey = (value) => {
- const rank = _keyRank(value);
- if (rank === 0) return Number.isFinite(value);
- if (rank === 2) return true;
- if (rank === 4) return value.every(_idbValidKey);
- return false;
- };
- const _idbInRange = (key, query) => {
- if (query === undefined || query === null) return true;
- if (typeof query === 'number' || typeof query === 'string' || query instanceof Date || Array.isArray(query)) {
- return _idbCmp(key, query) === 0;
- }
- if (query.lower !== undefined && query.lower !== null) {
- const lower = _idbCmp(key, query.lower);
- if (query.lowerOpen ? lower <= 0 : lower < 0) return false;
- }
- if (query.upper !== undefined && query.upper !== null) {
- const upper = _idbCmp(key, query.upper);
- if (query.upperOpen ? upper >= 0 : upper > 0) return false;
- }
- return true;
- };
- // Key path: dotted, with numeric segments indexing into arrays, matching the
- // spec's evaluation order for in-line keys.
- const _idbExtractKey = (keyPath, value) => {
- if (keyPath === undefined || keyPath === null || keyPath === '') return undefined;
- let current = value;
- for (const segment of String(keyPath).split('.')) {
- if (current === undefined || current === null) return undefined;
- current = current[segment];
- }
- return current;
- };
- const _idbSortedEntries = (records, query, direction, count) => {
- const parsed = records.map((record) => ({
- key: JSON.parse(record.key),
- value: JSON.parse(record.value),
- }));
- parsed.sort((a, b) => _idbCmp(a.key, b.key));
- if (direction === 'prev' || direction === 'previous') parsed.reverse();
- if (query !== undefined && query !== null) {
- return parsed.filter((entry) => _idbInRange(entry.key, query));
}
- return parsed;
- };
+ });
+ return req;
+}
- // ── IDBRequest ───────────────────────────────────────────────────────────
- function IDBRequest() {}
- IDBRequest.prototype = {
- addEventListener(type, fn) {
- if (typeof fn !== 'function') return;
- (this._listeners[type] || (this._listeners[type] = [])).push(fn);
- },
- removeEventListener(type, fn) {
- const list = this._listeners[type];
- if (!list) return;
- const at = list.indexOf(fn);
- if (at >= 0) list.splice(at, 1);
- },
- _fire(type, event) {
- const handler = this['on' + type];
- if (typeof handler === 'function') { try { handler.call(this, event); } catch (e) {} }
- for (const fn of (this._listeners[type] || []).slice()) {
- try { fn.call(this, event); } catch (e) {}
- }
- },
- };
- Object.defineProperty(IDBRequest.prototype, 'IDBRequest', { value: IDBRequest, configurable: true });
-
- const _newRequest = (transaction, source) => {
- const request = Object.create(IDBRequest.prototype);
- request._listeners = {};
- request.result = undefined;
- request.error = null;
- request.source = source || null;
- request.transaction = transaction || null;
- request.readyState = 'pending';
- request.onsuccess = null;
- request.onerror = null;
- return request;
- };
- const _settle = (request, work) => {
- Promise.resolve().then(() => {
- if (request.readyState !== 'pending') return;
- try {
- request.result = work();
- request.readyState = 'done';
- request._fire('success', { type: 'success', target: request });
- } catch (error) {
- request.error = error;
- request.readyState = 'done';
- request._fire('error', { type: 'error', target: request });
- }
- if (request.transaction) request.transaction._requestSettled();
- });
- };
- const _fail = (request, name, message) => {
- request.error = _idbFail(name, message);
- request.readyState = 'done';
- request._fire('error', { type: 'error', target: request });
- if (request.transaction) request.transaction._requestSettled();
+function _idbObjectStore(name) {
+ const data = new Map();
+ return {
+ name,
+ keyPath: null,
+ autoIncrement: false,
+ indexNames: { contains() { return false; }, length: 0, item() { return null; } },
+ transaction: null,
+ add(value, key) { const k = key ?? Date.now(); data.set(k, value); return _idbRequest(() => k); },
+ put(value, key) { const k = key ?? Date.now(); data.set(k, value); return _idbRequest(() => k); },
+ get(key) { return _idbRequest(() => data.get(key) ?? undefined); },
+ getAll() { return _idbRequest(() => Array.from(data.values())); },
+ getAllKeys() { return _idbRequest(() => Array.from(data.keys())); },
+ getKey(key) { return _idbRequest(() => (data.has(key) ? key : undefined)); },
+ delete(key) { return _idbRequest(() => { data.delete(key); return undefined; }); },
+ clear() { return _idbRequest(() => { data.clear(); return undefined; }); },
+ count() { return _idbRequest(() => data.size); },
+ openCursor() { return _idbRequest(() => null); },
+ openKeyCursor() { return _idbRequest(() => null); },
+ createIndex() { return { name: '', keyPath: '', unique: false, multiEntry: false, get() { return _idbRequest(() => undefined); } }; },
+ index() { return { get() { return _idbRequest(() => undefined); }, getAll() { return _idbRequest(() => []); }, count() { return _idbRequest(() => 0); }, openCursor() { return _idbRequest(() => null); } }; },
+ deleteIndex() {},
};
+}
- // ── IDBTransaction ───────────────────────────────────────────────────────
- function IDBTransaction() {}
- IDBTransaction.prototype = {
- addEventListener(type, fn) {
- if (typeof fn !== 'function') return;
- (this._listeners[type] || (this._listeners[type] = [])).push(fn);
- },
- removeEventListener(type, fn) {
- const list = this._listeners[type];
- if (!list) return;
- const at = list.indexOf(fn);
- if (at >= 0) list.splice(at, 1);
- },
- _fire(type, event) {
- const handler = this['on' + type];
- if (typeof handler === 'function') { try { handler.call(this, event); } catch (e) {} }
- for (const fn of (this._listeners[type] || []).slice()) {
- try { fn.call(this, event); } catch (e) {}
- }
- },
+function _idbTransaction(storeNames) {
+ const stores = new Map();
+ const names = Array.isArray(storeNames) ? storeNames : [storeNames];
+ for (const n of names) stores.set(String(n), _idbObjectStore(String(n)));
+ const tx = {
+ db: null,
+ mode: 'readonly',
+ objectStoreNames: { contains: (n) => stores.has(String(n)), length: stores.size },
+ onabort: null, oncomplete: null, onerror: null,
+ error: null,
objectStore(name) {
- const key = String(name);
- if (!this._storeNames.has(key)) {
- throw _idbFail('NotFoundError', "no object store named '" + key + "' in this transaction");
- }
- return this._store(key);
- },
- abort() {
- if (this._finished) throw _idbFail('InvalidStateError', 'the transaction is already finished');
- this._finished = true;
- this.error = this.error || _idbFail('AbortError', 'the transaction was aborted');
- this._fire('abort', { type: 'abort', target: this });
+ let s = stores.get(name);
+ if (!s) { s = _idbObjectStore(name); stores.set(name, s); }
+ s.transaction = tx;
+ return s;
},
- commit() { this._committed = true; },
- };
- Object.defineProperty(IDBTransaction.prototype, 'IDBTransaction', { value: IDBTransaction, configurable: true });
-
- const _newTransaction = (db, storeNames, mode) => {
- const transaction = Object.create(IDBTransaction.prototype);
- transaction._listeners = {};
- transaction.db = db;
- transaction.mode = mode || 'readonly';
- transaction.error = null;
- transaction.onabort = null;
- transaction.oncomplete = null;
- transaction.onerror = null;
- transaction._storeNames = new Set(storeNames);
- transaction._stores = new Map();
- transaction._pending = 0;
- transaction._finished = false;
- transaction._committed = false;
- transaction.objectStoreNames = {
- get length() { return transaction._storeNames.size; },
- item: (index) => [...transaction._storeNames][index] ?? null,
- contains: (name) => transaction._storeNames.has(String(name)),
- };
- return transaction;
+ abort() {},
+ commit() {},
+ addEventListener(type, fn) { tx['on' + type] = fn; },
+ removeEventListener(type, fn) { if (tx['on' + type] === fn) tx['on' + type] = null; },
};
- IDBTransaction.prototype._store = function(name) {
- let store = this._stores.get(name);
- if (!store) {
- store = _newObjectStore(this, name);
- this._stores.set(name, store);
+ Promise.resolve().then(() => {
+ if (typeof tx.oncomplete === 'function') {
+ try { tx.oncomplete({ target: tx, type: 'complete' }); } catch (e) {}
}
- return store;
- };
- // The spec completes a transaction once its last request has settled, not on a
- // fixed microtask, so a caller that chains requests in oncomplete still sees
- // a live transaction.
- IDBTransaction.prototype._requestStarted = function() {
- if (this._finished) throw _idbFail('TransactionInactiveError', 'the transaction has already finished');
- this._pending += 1;
- };
- IDBTransaction.prototype._requestSettled = function() {
- if (this._finished) return;
- this._pending -= 1;
- if (this._pending > 0) return;
- // Completion waits for a macrotask, not a microtask. The spec keeps a
- // transaction alive while requests are still being queued in the same task,
- // and the normal pattern is exactly that: chain a get after a getAll. With a
- // microtask the transaction was already finished before the second request
- // existed, so every chained call raised TransactionInactiveError.
- setTimeout(() => {
- if (this._finished || this._pending > 0) return;
- this._finished = true;
- this._fire('complete', { type: 'complete', target: this });
- }, 0);
- };
-
- // ── IDBCursor ────────────────────────────────────────────────────────────
- function IDBCursor() {}
- IDBCursor.prototype = {
- addEventListener(type, fn) {
- if (typeof fn !== 'function') return;
- (this._listeners[type] || (this._listeners[type] = [])).push(fn);
- },
- removeEventListener(type, fn) {
- const list = this._listeners[type];
- if (!list) return;
- const at = list.indexOf(fn);
- if (at >= 0) list.splice(at, 1);
- },
- continue(key) {
- if (key !== undefined) this._pendingKey = key;
- this._advance(1);
- },
- continuePrimaryKey(key, primaryKey) { this.continue(key); },
- advance(count) { this._advance(count); },
- update(value) {
- this._objectStore._write(this.primaryKey, value, false);
- },
- delete() {
- this._objectStore._request('delete', () => ({ key: this.primaryKey }), undefined);
- },
- _advance(step) {
- this._index += step;
- this._emit();
- },
- _emit() {
- const entry = this._entries[this._index];
- // Running past the last record has to publish a null result and fire
- // success again, otherwise a `while (cursor) cursor.continue()` loop never
- // ends: the request kept its old cursor and nothing was dispatched.
- if (!entry) {
- this.request.result = null;
- this._emitEvent();
- return;
- }
- // `key` is the index key when the cursor came from an index, and the
- // primary key otherwise; `primaryKey` is always the record's own key.
- this.primaryKey = entry.primaryKey === undefined ? entry.key : entry.primaryKey;
- this.key = this._keyOnly ? this.primaryKey : entry.key;
- this.value = this._keyOnly ? undefined : entry.value;
- this.request.result = this;
- this._emitEvent();
- },
- _emitEvent() {
- const handler = this.request.onsuccess;
- const event = { type: 'success', target: this.request };
- if (typeof handler === 'function') { try { handler.call(this.request, event); } catch (e) {} }
- for (const fn of (this.request._listeners.success || []).slice()) {
- try { fn.call(this.request, event); } catch (e) {}
- }
- },
- _indexKey() { return this.key; },
- // Unused now that _emit reads entry.key and entry.primaryKey directly, kept
- // so nothing external depends on the old helper.
- _indexKeyOf(entry) { return entry.key; },
- };
- Object.defineProperty(IDBCursor.prototype, 'IDBCursor', { value: IDBCursor, configurable: true });
-
- const _newCursor = (request, objectStore, entries, keyOnly) => {
- const cursor = Object.create(IDBCursor.prototype);
- cursor._listeners = {};
- cursor.request = request;
- cursor._objectStore = objectStore;
- cursor._entries = entries;
- cursor._keyOnly = keyOnly;
- cursor.direction = 'next';
- cursor._index = -1;
- cursor.primaryKey = undefined;
- cursor.key = undefined;
- cursor.value = undefined;
- return cursor;
- };
-
- // ── IDBObjectStore and IDBIndex ───────────────────────────────────────────
- function IDBObjectStore() {}
- function IDBIndex() {}
- IDBIndex.prototype = {
- addEventListener() {}, removeEventListener() {},
- get(key) { return this._objectStore._request('get', () => ({ key, index: this }), undefined); },
- getKey(key) { return this._objectStore._request('get', () => ({ key, index: this }), undefined); },
- getAll(query, count) {
- return this._objectStore._request('entries', () => ({ query, count, index: this }), undefined);
- },
- getAllKeys(query, count) {
- return this._objectStore._request('entries', () => ({ query, count, keysOnly: true, index: this }), undefined);
- },
- count(query) { return this._objectStore._request('entries', () => ({ query, index: this, countOnly: true }), undefined); },
- openCursor(query, direction) {
- return this._objectStore._request('entries', () => ({ query, direction, index: this, cursor: true }), undefined);
- },
- openKeyCursor(query, direction) {
- return this._objectStore._request('entries', () => ({ query, direction, keysOnly: true, index: this, cursor: true }), undefined);
- },
- };
- Object.defineProperty(IDBIndex.prototype, 'IDBIndex', { value: IDBIndex, configurable: true });
-
- IDBObjectStore.prototype = {
- addEventListener() {}, removeEventListener() {},
- get name() { return this._name; },
- get keyPath() { return this._keyPath ?? null; },
- get autoIncrement() { return !!this._autoIncrement; },
- get transaction() { return this._transaction; },
- get indexNames() {
- const names = Object.keys(this._indexes);
- return {
- get length() { return names.length; },
- item: (index) => names[index] ?? null,
- contains: (name) => Object.prototype.hasOwnProperty.call(this._indexes, String(name)),
- };
- },
- put(value, key) { return this._write(key, value, false); },
- add(value, key) { return this._write(key, value, true); },
- get(key) { return this._request('get', () => ({ key }), undefined); },
- getKey(key) { return this._request('get', () => ({ key }), undefined); },
- getAll(query, count) { return this._request('entries', () => ({ query, count }), undefined); },
- getAllKeys(query, count) { return this._request('entries', () => ({ query, count, keysOnly: true }), undefined); },
- count(query) { return this._request('entries', () => ({ query, countOnly: true }), undefined); },
- delete(key) { return this._request('delete', () => ({ key }), undefined); },
- clear() { return this._request('clear', () => ({}), undefined); },
- openCursor(query, direction) {
- return this._cursor(query, direction, false);
- },
- openKeyCursor(query, direction) {
- return this._cursor(query, direction, true);
- },
- createIndex(name, keyPath, options) {
- if (this._transaction.mode !== 'versionchange') {
- throw _idbFail('InvalidStateError', 'createIndex requires a versionchange transaction');
- }
- const index = Object.create(IDBIndex.prototype);
- index._objectStore = this;
- index.name = String(name);
- index.keyPath = String(keyPath);
- index.unique = !!(options && options.unique);
- index.multiEntry = !!(options && options.multiEntry);
- this._indexes[index.name] = index;
- this._transaction._schemaChanged();
- return index;
- },
- index(name) {
- const index = this._indexes[String(name)];
- if (!index) throw _idbFail('NotFoundError', "no index named '" + name + "'");
- return index;
- },
- deleteIndex(name) {
- delete this._indexes[String(name)];
- this._transaction._schemaChanged();
- },
- // Resolve the key the spec would use: explicit key first, then the key path,
- // then the generator when the store is auto-incrementing.
- _resolveKey(value, explicitKey) {
- if (explicitKey !== undefined && explicitKey !== null) {
- if (!_idbValidKey(explicitKey)) throw _idbFail('DataError', 'the key is not a valid IndexedDB key');
- return explicitKey;
- }
- const fromPath = this._keyPath ? _idbExtractKey(this._keyPath, value) : undefined;
- if (fromPath !== undefined) {
- if (!_idbValidKey(fromPath)) throw _idbFail('DataError', 'the key path did not yield a valid key');
- return fromPath;
- }
- if (this._autoIncrement) {
- this._nextKey += 1;
- return this._nextKey;
- }
- throw _idbFail('DataError', 'the store needs an explicit key or a key path');
- },
- _write(key, value, mustBeNew) {
- return this._request('put', () => ({ key: this._resolveKey(value, key), value, mustBeNew }), undefined);
- },
- _cursor(query, direction, keyOnly) {
- return this._request('entries', () => ({ query, direction, keysOnly: keyOnly, cursor: true }));
- },
- // Every operation is one request: the spec allows the caller to read
- // `transaction` and the store's metadata while a request is pending.
- _request(command, args, initial) {
- const request = _newRequest(this._transaction, this);
- if (command === 'entries' && args().cursor) {
- // A cursor request starts empty and is filled in by the body below, so
- // `request.result` can be replaced by the first `continue`.
- request.result = null;
- }
- if (initial !== null && initial !== undefined && command !== 'entries') {
- request.result = initial;
- return request;
- }
- this._transaction._requestStarted();
- Promise.resolve().then(() => {
- try {
- const payload = args();
- const base = { db: this._dbName, store: this._name, cmd: command };
- let reply;
- if (command === 'entries') {
- reply = _idbBackend(Object.assign({}, base, { cmd: 'entries' }));
- if (!reply.ok) return _fail(request, 'UnknownError', reply.error);
- const entries = reply.records || [];
- const parsed = entries.map((record) => ({ key: JSON.parse(record.key), value: JSON.parse(record.value) }));
- const index = payload.index;
- const scoped = index
- ? parsed
- .map((entry) => ({ key: _idbExtractKey(index.keyPath, entry.value), value: entry.value, primaryKey: entry.key }))
- .filter((entry) => entry.key !== undefined && _idbInRange(entry.key, payload.query))
- .sort((a, b) => _idbCmp(a.key, b.key))
- : parsed.sort((a, b) => _idbCmp(a.key, b.key));
- if (payload.direction === 'prev' || payload.direction === 'previous') scoped.reverse();
- const limited = typeof payload.count === 'number' ? scoped.slice(0, payload.count) : scoped;
- if (payload.cursor) {
- const cursor = _newCursor(request, this, limited, !!payload.keysOnly);
- cursor.direction = payload.direction || 'next';
- request.result = cursor;
- cursor._index = -1;
- cursor._advance(1);
- request.readyState = 'done';
- this._transaction._requestSettled();
- return;
- }
- if (payload.countOnly) { request.result = limited.length; request.readyState = 'done'; request._fire('success', { type: 'success', target: request }); this._transaction._requestSettled(); return; }
- if (payload.keysOnly) { request.result = limited.map((entry) => (index ? entry.key : entry.key)); }
- else if (index) { request.result = limited.map((entry) => entry.value); }
- else { request.result = limited.map((entry) => entry.value); }
- request.readyState = 'done';
- request._fire('success', { type: 'success', target: request });
- this._transaction._requestSettled();
- return;
- }
- if (command === 'get') {
- const keyJson = JSON.stringify(payload.key);
- reply = _idbBackend(Object.assign({}, base, { cmd: 'get', key: payload.key }));
- if (!reply.ok) return _fail(request, 'UnknownError', reply.error);
- if (!reply.found) {
- request.result = undefined;
- } else {
- const value = JSON.parse(reply.value);
- if (payload.index) {
- const indexKey = _idbExtractKey(payload.index.keyPath, value);
- request.result = indexKey !== undefined && _idbCmp(indexKey, payload.key) === 0 ? value : undefined;
- } else if (_idbCmp(JSON.parse(keyJson), payload.key) === 0) {
- request.result = value;
- } else {
- request.result = undefined;
- }
- }
- request.readyState = 'done';
- request._fire('success', { type: 'success', target: request });
- this._transaction._requestSettled();
- return;
- }
- if (command === 'put') {
- reply = _idbBackend(Object.assign({}, base, { cmd: 'put', key: payload.key, value: payload.value }));
- if (!reply.ok) return _fail(request, 'QuotaExceededError', reply.error);
- if (payload.mustBeNew && reply.replaced) {
- return _fail(request, 'ConstraintError', 'the key already exists');
- }
- if (payload.index) {
- this._keySeq += 1;
- const keyJson = JSON.stringify(payload.key);
- }
- if (this._autoIncrement && typeof payload.key === 'number' && payload.key >= this._nextKey) {
- this._nextKey = payload.key;
- }
- request.result = payload.key;
- request.readyState = 'done';
- request._fire('success', { type: 'success', target: request });
- this._transaction._requestSettled();
- return;
- }
- reply = _idbBackend(Object.assign({}, base, { cmd: command, key: payload.key }));
- if (!reply.ok) return _fail(request, 'UnknownError', reply.error);
- request.result = undefined;
- request.readyState = 'done';
- request._fire('success', { type: 'success', target: request });
- this._transaction._requestSettled();
- } catch (e) {
- // A throw inside the body used to leave the request pending and the
- // transaction never completing, which reads as a hung page.
- _fail(request, 'UnknownError', String(e));
- }
- });
- return request;
- },
- };
- Object.defineProperty(IDBObjectStore.prototype, 'IDBObjectStore', { value: IDBObjectStore, configurable: true });
-
- const _newObjectStore = (transaction, name) => {
- const store = Object.create(IDBObjectStore.prototype);
- const definition = transaction._schema[name] || {};
- store._name = name;
- store._dbName = transaction.db.name;
- store._transaction = transaction;
- store._keyPath = definition.keyPath ?? null;
- store._autoIncrement = !!definition.autoIncrement;
- store._indexes = {};
- for (const stored of definition.indexes || []) {
- const index = Object.create(IDBIndex.prototype);
- index._objectStore = store;
- index.name = stored.name;
- index.keyPath = stored.keyPath;
- index.unique = !!stored.unique;
- index.multiEntry = !!stored.multiEntry;
- store._indexes[index.name] = index;
- }
- store._nextKey = 0;
- store._keySeq = 0;
- return store;
- };
+ });
+ return tx;
+}
- // ── IDBDatabase ──────────────────────────────────────────────────────────
- function IDBDatabase() {}
- IDBDatabase.prototype = {
- addEventListener(type, fn) {
- if (typeof fn !== 'function') return;
- (this._listeners[type] || (this._listeners[type] = [])).push(fn);
- },
- removeEventListener(type, fn) {
- const list = this._listeners[type];
- if (!list) return;
- const at = list.indexOf(fn);
- if (at >= 0) list.splice(at, 1);
- },
- _fire(type, event) {
- const handler = this['on' + type];
- if (typeof handler === 'function') { try { handler.call(this, event); } catch (e) {} }
- for (const fn of (this._listeners[type] || []).slice()) {
- try { fn.call(this, event); } catch (e) {}
- }
- },
- get name() { return this._name; },
- get version() { return this._version; },
- get objectStoreNames() {
- const names = Object.keys(this._schema);
- return {
- get length() { return names.length; },
- item: (index) => names[index] ?? null,
- contains: (name) => Object.prototype.hasOwnProperty.call(this._schema, String(name)),
- };
- },
- createObjectStore(name, options) {
- if (this._closed) throw _idbFail('InvalidStateError', 'the database is closed');
- const key = String(name);
- if (this._schema[key]) throw _idbFail('ConstraintError', "an object store named '" + key + "' already exists");
- this._schema[key] = {
- keyPath: (options && options.keyPath) ?? null,
- autoIncrement: !!(options && options.autoIncrement),
- indexes: [],
- };
- this._upgradeTransaction._schemaChanged();
- return this._upgradeTransaction._store(key);
- },
- deleteObjectStore(name) {
- const key = String(name);
- if (!this._schema[key]) throw _idbFail('NotFoundError', "no object store named '" + key + "'");
- delete this._schema[key];
- this._upgradeTransaction._schemaChanged();
- },
+function _idbDatabase(name, version) {
+ return {
+ name,
+ version,
+ objectStoreNames: { contains() { return false; }, length: 0, item() { return null; } },
+ createObjectStore(n) { return _idbObjectStore(n); },
+ deleteObjectStore() {},
transaction(storeNames, mode) {
- if (this._closed) throw _idbFail('InvalidStateError', 'the database is closed');
- const names = (Array.isArray(storeNames) ? storeNames : [storeNames]).map(String);
- for (const name of names) {
- if (!this._schema[name]) {
- throw _idbFail('NotFoundError', "no object store named '" + name + "'");
- }
- }
- const transaction = _newTransaction(this, names, mode);
- transaction._schema = this._schema;
- return transaction;
+ const tx = _idbTransaction(storeNames);
+ tx.mode = mode || 'readonly';
+ return tx;
},
- close() { this._closed = true; },
- };
- Object.defineProperty(IDBDatabase.prototype, 'IDBDatabase', { value: IDBDatabase, configurable: true });
-
- const _newDatabase = (name, version, schema, upgradeTransaction) => {
- const db = Object.create(IDBDatabase.prototype);
- db._listeners = {};
- db._name = name;
- db._version = version;
- db._schema = schema;
- db._upgradeTransaction = upgradeTransaction || null;
- db._closed = false;
- db.onversionchange = null;
- db.onabort = null;
- db.onerror = null;
- db.onclose = null;
- return db;
- };
-
- const _serializeSchema = (db) => {
- const stores = {};
- for (const [name, definition] of Object.entries(db._schema)) {
- stores[name] = {
- keyPath: definition.keyPath ?? null,
- autoIncrement: !!definition.autoIncrement,
- indexes: Object.values(definition.indexes || {}).map((index) => ({
- name: index.name, keyPath: index.keyPath, unique: !!index.unique, multiEntry: !!index.multiEntry,
- })),
- };
- }
- return stores;
- };
- const _deserializeSchema = (raw) => {
- const schema = {};
- for (const [name, definition] of Object.entries(raw || {})) {
- const indexes = {};
- for (const index of definition.indexes || []) indexes[index.name] = index;
- schema[name] = {
- keyPath: definition.keyPath ?? null,
- autoIncrement: !!definition.autoIncrement,
- indexes,
- };
- }
- return schema;
- };
-
- // ── IDBKeyRange ──────────────────────────────────────────────────────────
- function IDBKeyRange(lower, upper, lowerOpen, upperOpen) {
- this.lower = lower === undefined ? null : lower;
- this.upper = upper === undefined ? null : upper;
- this.lowerOpen = !!lowerOpen;
- this.upperOpen = !!upperOpen;
- }
- IDBKeyRange.prototype.includes = function(key) { return _idbInRange(key, this); };
- Object.defineProperty(IDBKeyRange.prototype, 'IDBKeyRange', { value: IDBKeyRange, configurable: true });
-
- const _keyRangeCheck = (value) => {
- if (!_idbValidKey(value)) throw _idbFail('DataError', 'not a valid IndexedDB key');
- return value;
+ close() {},
+ onversionchange: null, onabort: null, onerror: null, onclose: null,
+ addEventListener() {}, removeEventListener() {},
};
+}
- // ── IDBFactory ───────────────────────────────────────────────────────────
- function IDBFactory() {}
- IDBFactory.prototype = {
- open(name, version) {
- const dbName = String(name);
- const requested = Math.max(1, Math.trunc(version === undefined ? 1 : version) || 1);
- const request = _newRequest(null, null);
- request.onupgradeneeded = null;
- request.onblocked = null;
- request.onupgradeneeded_elsewhere = null;
- Promise.resolve().then(() => {
- try {
- const reply = _idbBackend({ db: dbName, cmd: 'open', version: requested });
- if (!reply.ok) {
- request.error = _idbFail('UnknownError', reply.error);
- request.readyState = 'done';
- request._fire('error', { type: 'error', target: request });
- return;
- }
- const schema = _deserializeSchema(reply.stores);
- if (!reply.upgradeNeeded) {
- request.result = _newDatabase(dbName, reply.version || requested, schema, null);
- request.readyState = 'done';
- request._fire('success', { type: 'success', target: request });
- return;
- }
- // Upgrade path: hand the page a versionchange transaction whose
- // createObjectStore calls land in the schema, then commit it.
- const oldVersion = reply.version || 0;
- const database = _newDatabase(dbName, requested, schema, null);
- const transaction = _newTransaction(database, [], 'versionchange');
- transaction._schema = schema;
- transaction._upgrade = true;
- transaction._schemaChanged = function() { this._schemaDirty = true; };
- database._upgradeTransaction = transaction;
- request.transaction = transaction;
- request.result = database;
- request.readyState = 'done';
- request._fire('upgradeneeded', {
- type: 'upgradeneeded',
- target: request,
- oldVersion,
- newVersion: requested,
- transaction,
- });
- Promise.resolve().then(() => {
- if (!reply.exists && Object.keys(schema).length === 0 && !transaction._schemaDirty) {
- // No schema was declared, which is a legitimate empty database.
- }
- const commit = _idbBackend({
- db: dbName,
- cmd: 'schema',
- version: requested,
- stores: _serializeSchema(database),
- });
- if (!commit.ok) {
- request.error = _idbFail('UnknownError', commit.error);
- request._fire('error', { type: 'error', target: request });
- return;
- }
- transaction._fire('complete', { type: 'complete', target: transaction });
- request._fire('success', { type: 'success', target: request });
- });
- } catch (e) {
- request.error = _idbFail('UnknownError', String(e));
- request.readyState = 'done';
- request._fire('error', { type: 'error', target: request });
- }
- });
- return request;
- },
- deleteDatabase(name) {
- const dbName = String(name);
- const request = _newRequest(null, null);
- request.onblocked = null;
- Promise.resolve().then(() => {
- const reply = _idbBackend({ db: dbName, cmd: 'deleteDatabase' });
- request.result = undefined;
- request.readyState = 'done';
- if (!reply.ok) {
- request.error = _idbFail('UnknownError', reply.error);
- request._fire('error', { type: 'error', target: request });
- return;
- }
- request._fire('success', { type: 'success', target: request });
- });
- return request;
- },
- // The spec has IDBFactory.databases() return a Promise of
- // IDBDatabaseInfo records, not a request. Returning the request left
- // `await indexedDB.databases()` resolving to the request itself, so callers
- // that read `.result` off the awaited value saw undefined and a boot that
- // waits on that list never finished.
- databases() {
- const request = _newRequest(null, null);
- const promise = Promise.resolve().then(() => {
- const reply = _idbBackend({ cmd: 'list' });
- const databases = reply.ok ? (reply.databases || []) : [];
- request.result = databases;
- request.readyState = 'done';
- request._fire('success', { type: 'success', target: request });
- return databases;
- });
- promise.request = request;
- return promise;
- },
- cmp(a, b) { return _idbCmp(a, b); },
- };
- Object.defineProperty(IDBFactory.prototype, 'IDBFactory', { value: IDBFactory, configurable: true });
-
- const _idbOpenRequests = new WeakMap();
- const factory = new IDBFactory();
- _idbOpenRequests.set(factory, factory);
- globalThis.IDBFactory = IDBFactory;
- globalThis.IDBRequest = IDBRequest;
- globalThis.IDBTransaction = IDBTransaction;
- globalThis.IDBDatabase = IDBDatabase;
- globalThis.IDBObjectStore = IDBObjectStore;
- globalThis.IDBIndex = IDBIndex;
- globalThis.IDBCursor = IDBCursor;
- globalThis.IDBKeyRange = IDBKeyRange;
- globalThis.IDBKeyRange.only = (value) => new IDBKeyRange(_keyRangeCheck(value), _keyRangeCheck(value), false, false);
- globalThis.IDBKeyRange.lowerBound = (value, open) => new IDBKeyRange(_keyRangeCheck(value), null, !!open, false);
- globalThis.IDBKeyRange.upperBound = (value, open) => new IDBKeyRange(null, _keyRangeCheck(value), false, !!open);
- globalThis.IDBKeyRange.bound = (lower, upper, lowerOpen, upperOpen) =>
- new IDBKeyRange(_keyRangeCheck(lower), _keyRangeCheck(upper), !!lowerOpen, !!upperOpen);
- globalThis.indexedDB = factory;
-})();
+globalThis.indexedDB = {
+ open(name, version) {
+ return _idbRequest(() => _idbDatabase(name, version || 1));
+ },
+ deleteDatabase(_name) { return _idbRequest(() => undefined); },
+ databases() { return Promise.resolve([]); },
+ cmp(a, b) { return a < b ? -1 : a > b ? 1 : 0; },
+};
+globalThis.IDBKeyRange = {
+ only(v) { return { lower: v, upper: v, lowerOpen: false, upperOpen: false, includes(x) { return x === v; } }; },
+ lowerBound(v, open) { return { lower: v, upper: null, lowerOpen: !!open, upperOpen: false, includes(x) { return open ? x > v : x >= v; } }; },
+ upperBound(v, open) { return { lower: null, upper: v, lowerOpen: false, upperOpen: !!open, includes(x) { return open ? x < v : x <= v; } }; },
+ bound(l, u, lo, uo) { return { lower: l, upper: u, lowerOpen: !!lo, upperOpen: !!uo, includes(x) { return (lo ? x > l : x >= l) && (uo ? x < u : x <= u); } }; },
+};
// Do not advertise CacheStorage until it can retain responses. A successful
-// no-op cache selects broken persistence paths instead of normal fetch fallbacks.ch fallbacks.
+// no-op cache selects broken persistence paths instead of normal fetch fallbacks.
_markNative(AudioContext); _markNative(OfflineAudioContext);
_markNative(SpeechSynthesisUtterance);
diff --git a/crates/obscura-js/src/ops.rs b/crates/obscura-js/src/ops.rs
index 66f4f01b..464282ba 100644
--- a/crates/obscura-js/src/ops.rs
+++ b/crates/obscura-js/src/ops.rs
@@ -159,8 +159,7 @@ impl NavigationTiming {
/// BrowserContext so every page in a context shares it, `sessionStorage` on the
/// Page so it crosses same-origin navigation without ever leaving the tab.
///
-/// Insertion order is preserved because `Storage.key(i)` is defined in insertion
-/// order, not sorted order.
+/// This implementation retains insertion order for reproducible enumeration.
const LOCAL_STORAGE_ORIGIN_LIMIT: usize = 5 * 1024 * 1024;
const LOCAL_STORAGE_TOTAL_LIMIT: usize = 32 * 1024 * 1024;
const LOCAL_STORAGE_ORIGIN_COUNT_LIMIT: usize = 256;
@@ -169,499 +168,138 @@ const LOCAL_STORAGE_ORIGIN_COUNT_LIMIT: usize = 256;
struct OriginStorageInner {
origins: HashMap<String, Vec<(String, String)>>,
bytes: usize,
+ changed: HashSet<String>,
}
#[derive(Default)]
pub struct OriginStorage {
inner: std::sync::Mutex<OriginStorageInner>,
- /// Set by every mutation, cleared by the disk writer. Without it a
- /// navigation would rewrite every origin file even when nothing changed,
- /// and `--storage-dir` would turn each page load into a few dozen writes.
- dirty: std::sync::atomic::AtomicBool,
- /// Origins touched since the last successful write. The writer only has to
- /// look at these, and an origin that lost its last entry is in here too, so
- /// its file can be removed instead of resurrected on the next load.
- changed: std::sync::Mutex<HashSet<String>>,
}
impl OriginStorage {
+ fn lock(&self) -> std::sync::MutexGuard<'_, OriginStorageInner> {
+ self.inner.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
+ }
+
pub fn snapshot(&self, origin: &str) -> Vec<(String, String)> {
- self.inner
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner())
- .origins
- .get(origin)
- .cloned()
- .unwrap_or_default()
+ self.lock().origins.get(origin).cloned().unwrap_or_default()
}
pub fn get(&self, origin: &str, key: &str) -> Option<String> {
- self.inner
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner())
- .origins
- .get(origin)
+ self.lock().origins.get(origin)
.and_then(|items| items.iter().find(|(name, _)| name == key))
.map(|(_, value)| value.clone())
}
- /// Returns false when the write would exceed a quota, which the caller
- /// surfaces as a `QuotaExceededError` the way a real browser does.
+ pub fn len(&self, origin: &str) -> usize {
+ self.lock().origins.get(origin).map_or(0, Vec::len)
+ }
+
+ pub fn key(&self, origin: &str, index: usize) -> Option<String> {
+ self.lock().origins.get(origin).and_then(|items| items.get(index))
+ .map(|(key, _)| key.clone())
+ }
+
+ /// Refuse writes over budget without changing the previous value.
pub fn set(&self, origin: &str, key: String, value: String) -> bool {
- let mut inner = self
- .inner
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner());
+ let mut inner = self.lock();
if !inner.origins.contains_key(origin)
- && inner.origins.len() >= LOCAL_STORAGE_ORIGIN_COUNT_LIMIT
- {
- return false;
- }
-
+ && inner.origins.len() >= LOCAL_STORAGE_ORIGIN_COUNT_LIMIT { return false; }
let items = inner.origins.get(origin);
- let previous = items
- .and_then(|items| items.iter().find(|(name, _)| name == &key))
- .map(|(name, value)| name.len() + value.len())
- .unwrap_or(0);
- let origin_bytes = items
- .map(|items| {
- items
- .iter()
- .map(|(name, value)| name.len() + value.len())
- .sum::<usize>()
- })
- .unwrap_or(0);
+ let previous = items.and_then(|items| items.iter().find(|(name, _)| name == &key));
+ if previous.is_some_and(|(_, previous)| previous == &value) { return true; }
+ let previous_bytes = previous.map_or(0, |(name, value)| name.len() + value.len());
+ let origin_bytes = items.map_or(0, |items| {
+ items.iter().map(|(name, value)| name.len() + value.len()).sum::<usize>()
+ });
let new_bytes = key.len() + value.len();
- let next_origin_bytes = origin_bytes.saturating_sub(previous) + new_bytes;
- let next_total_bytes = inner.bytes.saturating_sub(previous) + new_bytes;
- if next_origin_bytes > LOCAL_STORAGE_ORIGIN_LIMIT
- || next_total_bytes > LOCAL_STORAGE_TOTAL_LIMIT
- {
+ let next_origin_bytes = origin_bytes.saturating_sub(previous_bytes) + new_bytes;
+ let next_total_bytes = inner.bytes.saturating_sub(previous_bytes) + new_bytes;
+ if next_origin_bytes > LOCAL_STORAGE_ORIGIN_LIMIT || next_total_bytes > LOCAL_STORAGE_TOTAL_LIMIT {
return false;
}
-
let items = inner.origins.entry(origin.to_string()).or_default();
if let Some((_, old_value)) = items.iter_mut().find(|(name, _)| name == &key) {
*old_value = value;
- } else {
- items.push((key, value));
- }
+ } else { items.push((key, value)); }
inner.bytes = next_total_bytes;
- drop(inner);
- self.mark_changed(origin);
+ inner.changed.insert(origin.to_string());
true
}
pub fn remove(&self, origin: &str, key: &str) {
- let mut inner = self
- .inner
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner());
+ let mut inner = self.lock();
let removed = inner.origins.get_mut(origin).and_then(|items| {
- items
- .iter()
- .position(|(name, _)| name == key)
- .map(|index| items.remove(index))
+ items.iter().position(|(name, _)| name == key).map(|index| items.remove(index))
});
if let Some((name, value)) = removed {
inner.bytes -= name.len() + value.len();
+ if inner.origins.get(origin).is_some_and(Vec::is_empty) { inner.origins.remove(origin); }
+ inner.changed.insert(origin.to_string());
}
- if inner.origins.get(origin).is_some_and(Vec::is_empty) {
- inner.origins.remove(origin);
- }
- drop(inner);
- self.mark_changed(origin);
}
- /// Every origin with entries, in a stable order so a disk write produces a
- /// reproducible file set.
pub fn entries(&self) -> Vec<(String, Vec<(String, String)>)> {
- let inner = self
- .inner
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner());
- let mut out: Vec<(String, Vec<(String, String)>)> = inner
- .origins
- .iter()
- .filter(|(_, items)| !items.is_empty())
- .map(|(origin, items)| (origin.clone(), items.clone()))
- .collect();
+ let inner = self.lock();
+ let mut out: Vec<_> = inner.origins.iter()
+ .map(|(origin, items)| (origin.clone(), items.clone())).collect();
out.sort_by(|a, b| a.0.cmp(&b.0));
out
}
- /// Replace an origin's store wholesale. Used when reloading a profile from
- /// `--storage-dir`, and by tests that assert a round trip.
- pub fn load(&self, origin: &str, items: Vec<(String, String)>) {
- let mut inner = self
- .inner
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner());
- let bytes: usize = items
- .iter()
- .map(|(name, value)| name.len() + value.len())
- .sum();
- inner.bytes = inner.bytes.saturating_sub(
- inner
- .origins
- .get(origin)
- .map(|old| old.iter().map(|(name, value)| name.len() + value.len()).sum::<usize>())
- .unwrap_or(0),
- ) + bytes;
- if items.is_empty() {
- inner.origins.remove(origin);
- } else {
- inner.origins.insert(origin.to_string(), items);
- }
- drop(inner);
- self.mark_changed(origin);
- }
-
- fn mark_changed(&self, origin: &str) {
- self.dirty.store(true, std::sync::atomic::Ordering::Release);
- self.changed
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner())
- .insert(origin.to_string());
+ /// Restore a validated profile without scheduling a disk rewrite.
+ pub fn load(&self, origin: &str, items: Vec<(String, String)>) -> bool {
+ let mut seen = HashSet::new();
+ let mut bytes = 0usize;
+ for (name, value) in &items {
+ if !seen.insert(name.as_str()) { return false; }
+ bytes = bytes.saturating_add(name.len()).saturating_add(value.len());
+ if bytes > LOCAL_STORAGE_ORIGIN_LIMIT { return false; }
+ }
+ let mut inner = self.lock();
+ if !items.is_empty() && !inner.origins.contains_key(origin)
+ && inner.origins.len() >= LOCAL_STORAGE_ORIGIN_COUNT_LIMIT { return false; }
+ let previous_bytes = inner.origins.get(origin).map_or(0, |old| {
+ old.iter().map(|(name, value)| name.len() + value.len()).sum::<usize>()
+ });
+ let next_bytes = inner.bytes.saturating_sub(previous_bytes).saturating_add(bytes);
+ if next_bytes > LOCAL_STORAGE_TOTAL_LIMIT { return false; }
+ if items.is_empty() { inner.origins.remove(origin); }
+ else { inner.origins.insert(origin.to_string(), items); }
+ inner.bytes = next_bytes;
+ inner.changed.remove(origin);
+ true
}
- /// Origins written to since the last successful flush, in a stable order.
pub fn changed_origins(&self) -> Vec<String> {
- let changed = self
- .changed
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner());
- let mut out: Vec<String> = changed.iter().cloned().collect();
- out.sort();
- out
- }
-
- /// Called by the disk writer once every touched origin reached the disk, so
- /// a failed write leaves the store dirty and is retried.
- pub fn take_changed(&self) {
- self.changed
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner())
- .clear();
- self.dirty.store(false, std::sync::atomic::Ordering::Release);
+ let mut origins: Vec<_> = self.lock().changed.iter().cloned().collect();
+ origins.sort();
+ origins
+ }
+
+ /// One lock fences mutations and competing writers through acknowledgement.
+ pub fn flush_changed(&self, mut write: impl FnMut(&str, &[(String, String)]) -> bool) {
+ let mut inner = self.lock();
+ let mut origins: Vec<_> = inner.changed.iter().cloned().collect();
+ origins.sort();
+ for origin in origins {
+ if write(&origin, inner.origins.get(&origin).map_or(&[], Vec::as_slice)) {
+ inner.changed.remove(&origin);
+ }
+ }
}
- pub fn is_dirty(&self) -> bool {
- self.dirty.load(std::sync::atomic::Ordering::Acquire)
- }
+ pub fn is_dirty(&self) -> bool { !self.lock().changed.is_empty() }
pub fn clear(&self, origin: &str) {
- let mut inner = self
- .inner
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner());
- let removed = inner.origins.remove(origin);
- if let Some(items) = removed {
- inner.bytes -= items
- .iter()
- .map(|(name, value)| name.len() + value.len())
- .sum::<usize>();
+ let mut inner = self.lock();
+ if let Some(items) = inner.origins.remove(origin) {
+ inner.bytes -= items.iter().map(|(name, value)| name.len() + value.len()).sum::<usize>();
+ inner.changed.insert(origin.to_string());
}
- drop(inner);
- // Marked as changed, not just dirty: the writer only inspects origins in
- // this set, so a clear that only set the flag left the file on disk and
- // the next load resurrected the entries the user had cleared.
- self.mark_changed(origin);
- }
-}
-
-/// One object store's durable state. Keys and values are JSON so they cross the
-/// op boundary without a native value type; a `Date`, `Map` or `Blob` therefore
-/// does not survive a reload, which is a documented limitation rather than a
-/// silent one.
-#[derive(Clone, Default, serde::Serialize, serde::Deserialize)]
-struct StoredIdbStore {
- #[serde(default)]
- key_path: Option<String>,
- #[serde(default)]
- auto_increment: bool,
- #[serde(default)]
- indexes: Vec<StoredIdbIndex>,
- /// (JSON key, JSON value) in insertion order.
- #[serde(default)]
- records: Vec<(String, String)>,
-}
-
-#[derive(Clone, Default, serde::Serialize, serde::Deserialize)]
-struct StoredIdbIndex {
- name: String,
- key_path: String,
- #[serde(default)]
- unique: bool,
-}
-
-#[derive(Clone, Default, serde::Serialize, serde::Deserialize)]
-struct StoredIdbDatabase {
- version: u64,
- #[serde(default)]
- stores: std::collections::BTreeMap<String, StoredIdbStore>,
-}
-
-#[derive(Default)]
-struct IndexedDbInner {
- /// origin -> database name -> database.
- origins: HashMap<String, HashMap<String, StoredIdbDatabase>>,
-}
-
-/// IndexedDB backing store, keyed by origin the same way `localStorage` is.
-///
-/// The shim in `bootstrap.js` used to hand every object store its own `Map`, so
-/// `createObjectStore` results were unreachable and a database's data died with
-/// the realm. Records live here instead, so they survive navigation and a
-/// profile reload.
-#[derive(Default)]
-pub struct IndexedDbStorage {
- inner: std::sync::Mutex<IndexedDbInner>,
-}
-
-impl IndexedDbStorage {
- fn lock(&self) -> std::sync::MutexGuard<'_, IndexedDbInner> {
- self.inner.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
- }
-
- fn databases(&self, origin: &str) -> HashMap<String, StoredIdbDatabase> {
- self.lock().origins.get(origin).cloned().unwrap_or_default()
}
}
-/// Total records one origin may hold, so a runaway page cannot grow without
-/// bound inside one session.
-const IDB_ORIGIN_RECORD_LIMIT: usize = 100_000;
-
-fn idb_fail(message: &str) -> String {
- let mut out = String::from("{\"ok\":false,\"error\":");
- out.push_str(&serde_json::to_string(message).unwrap_or_else(|_| "\"error\"".to_string()));
- out.push('}');
- out
-}
-
-/// Handle one IndexedDB command. `payload` is a JSON object with at least a
-/// `cmd`; the reply is always a JSON object with `ok`, so the shim can tell a
-/// real failure from a malformed request.
-fn indexed_db_command(
- storage: &Option<Arc<IndexedDbStorage>>,
- origin: &str,
- payload: &str,
-) -> String {
- let Some(storage) = storage else {
- return idb_fail("no IndexedDB storage is attached");
- };
- let Ok(request) = serde_json::from_str::<serde_json::Value>(payload) else {
- return idb_fail("malformed IndexedDB request");
- };
- let command = request.get("cmd").and_then(|v| v.as_str()).unwrap_or("");
- let db_name = request.get("db").and_then(|v| v.as_str()).unwrap_or("");
- let store_name = request.get("store").and_then(|v| v.as_str()).unwrap_or("");
-
- match command {
- // Read the schema, and report whether an upgrade transaction is needed.
- "open" => {
- let requested = request
- .get("version")
- .and_then(|v| v.as_u64())
- .unwrap_or(1)
- .max(1);
- let databases = storage.databases(origin);
- let existing = databases.get(db_name);
- let version = existing.map(|db| db.version).unwrap_or(0);
- let stores = existing
- .map(|db| {
- serde_json::to_value(&db.stores).unwrap_or_else(|_| serde_json::Value::Null)
- })
- .unwrap_or_else(|| serde_json::json!({}));
- return serde_json::json!({
- "ok": true,
- "exists": existing.is_some(),
- "version": version,
- "upgradeNeeded": existing.is_none() || requested > version,
- "stores": stores,
- })
- .to_string();
- }
- // Write the schema and the version once an upgrade transaction finished.
- "schema" => {
- let version = request.get("version").and_then(|v| v.as_u64()).unwrap_or(1);
- let stores: std::collections::BTreeMap<String, StoredIdbStore> = request
- .get("stores")
- .and_then(|v| serde_json::from_value(v.clone()).ok())
- .unwrap_or_default();
- let mut inner = storage.lock();
- let entry = inner
- .origins
- .entry(origin.to_string())
- .or_default()
- .entry(db_name.to_string())
- .or_default();
- entry.version = version;
- // Stores removed by the upgrade are dropped, and the rest keep their
- // records unless the key path or key generator changed.
- let dropped: Vec<String> = entry
- .stores
- .keys()
- .filter(|name| !stores.contains_key(*name))
- .cloned()
- .collect();
- for name in dropped {
- entry.stores.remove(&name);
- }
- for (name, store) in stores {
- match entry.stores.get_mut(&name) {
- Some(existing) => {
- if existing.key_path != store.key_path
- || existing.auto_increment != store.auto_increment
- {
- *existing = StoredIdbStore {
- key_path: store.key_path,
- auto_increment: store.auto_increment,
- indexes: store.indexes,
- records: existing.records.clone(),
- };
- } else {
- existing.indexes = store.indexes;
- }
- }
- None => {
- entry.stores.insert(name, store);
- }
- }
- }
- return serde_json::json!({ "ok": true }).to_string();
- }
- "put" => {
- let key = request.get("key").cloned().unwrap_or(serde_json::Value::Null);
- let value = request.get("value").cloned().unwrap_or(serde_json::Value::Null);
- let key_json = key.to_string();
- let value_json = value.to_string();
- let mut inner = storage.lock();
- let total: usize = inner
- .origins
- .get(origin)
- .map(|databases| {
- databases
- .values()
- .flat_map(|db| db.stores.values())
- .map(|store| store.records.len())
- .sum()
- })
- .unwrap_or(0);
- let Some(store) = inner
- .origins
- .get_mut(origin)
- .and_then(|databases| databases.get_mut(db_name))
- .and_then(|db| db.stores.get_mut(store_name))
- else {
- return idb_fail("object store not found");
- };
- if let Some(slot) = store
- .records
- .iter_mut()
- .find(|(existing, _)| *existing == key_json)
- {
- slot.1 = value_json;
- return serde_json::json!({ "ok": true, "replaced": true }).to_string();
- }
- if total >= IDB_ORIGIN_RECORD_LIMIT {
- return idb_fail("quota exceeded");
- }
- store.records.push((key_json, value_json));
- serde_json::json!({ "ok": true, "replaced": false }).to_string()
- }
- "get" => {
- let key_json = request
- .get("key")
- .map(|v| v.to_string())
- .unwrap_or_else(|| "null".to_string());
- let inner = storage.lock();
- let store = inner
- .origins
- .get(origin)
- .and_then(|databases| databases.get(db_name))
- .and_then(|db| db.stores.get(store_name));
- let found = store.and_then(|store| {
- store
- .records
- .iter()
- .find(|(key, _)| *key == key_json)
- .map(|(_, value)| value.clone())
- });
- match found {
- Some(value) => serde_json::json!({ "ok": true, "found": true, "value": value }).to_string(),
- None => serde_json::json!({ "ok": true, "found": false }).to_string(),
- }
- }
- "delete" => {
- let key_json = request
- .get("key")
- .map(|v| v.to_string())
- .unwrap_or_else(|| "null".to_string());
- let mut inner = storage.lock();
- let Some(store) = inner
- .origins
- .get_mut(origin)
- .and_then(|databases| databases.get_mut(db_name))
- .and_then(|db| db.stores.get_mut(store_name))
- else {
- return idb_fail("object store not found");
- };
- store.records.retain(|(key, _)| *key != key_json);
- serde_json::json!({ "ok": true }).to_string()
- }
- "clear" => {
- let mut inner = storage.lock();
- if let Some(store) = inner
- .origins
- .get_mut(origin)
- .and_then(|databases| databases.get_mut(db_name))
- .and_then(|db| db.stores.get_mut(store_name))
- {
- store.records.clear();
- }
- serde_json::json!({ "ok": true }).to_string()
- }
- // Every read returns the whole store in one op call: getAll, getAllKeys,
- // count and get are all projections of the same list, and a page that
- // asks twice should not pay two round trips through V8.
- "entries" => {
- let inner = storage.lock();
- let store = inner
- .origins
- .get(origin)
- .and_then(|databases| databases.get(db_name))
- .and_then(|db| db.stores.get(store_name));
- let records = store.map(|store| store.records.clone()).unwrap_or_default();
- serde_json::json!({
- "ok": true,
- "records": records
- .into_iter()
- .map(|(key, value)| serde_json::json!({ "key": key, "value": value }))
- .collect::<Vec<_>>(),
- "keyPath": store.and_then(|store| store.key_path.clone()),
- })
- .to_string()
- }
- "deleteDatabase" => {
- let mut inner = storage.lock();
- if let Some(databases) = inner.origins.get_mut(origin) {
- databases.remove(db_name);
- }
- serde_json::json!({ "ok": true }).to_string()
- }
- "list" => {
- let databases = storage.lock().origins.get(origin).cloned().unwrap_or_default();
- let names: Vec<serde_json::Value> = databases
- .into_iter()
- .map(|(name, db)| serde_json::json!({ "name": name, "version": db.version }))
- .collect();
- serde_json::json!({ "ok": true, "databases": names }).to_string()
- }
- _ => idb_fail("unknown IndexedDB command"),
- }
-}
pub struct ObscuraState {
pub dom: Option<DomTree>,
@@ -692,9 +330,6 @@ pub struct ObscuraState {
/// origin: an entry survives realm teardown on a same-origin navigation but
/// never leaves the tab (issue #678).
pub session_storage: Option<Arc<OriginStorage>>,
- /// BrowserContext-scoped IndexedDB records and schema. Shared by every page
- /// of one context and keyed by origin, so a realm rebuild cannot lose them.
- pub indexed_db: Option<Arc<IndexedDbStorage>>,
pub http_client: Option<Arc<ObscuraHttpClient>>,
/// The owning page's passive on_request/on_response callbacks (issue
/// #408). Page-scoped, so scripted fetch()/XHR observation stays local to
@@ -935,7 +570,6 @@ impl ObscuraState {
// own bucket instead of the top document's.
self.local_storage = parent.local_storage.clone();
self.session_storage = parent.session_storage.clone();
- self.indexed_db = parent.indexed_db.clone();
self.http_client = parent.http_client.clone();
self.callbacks = parent.callbacks.clone();
self.encoding = parent.encoding.clone();
@@ -969,7 +603,6 @@ impl ObscuraState {
cookie_jar: None,
local_storage: Some(Arc::new(OriginStorage::default())),
session_storage: Some(Arc::new(OriginStorage::default())),
- indexed_db: Some(Arc::new(IndexedDbStorage::default())),
http_client: None,
callbacks: None,
#[cfg(feature = "stealth")]
@@ -5101,6 +4734,42 @@ mod tests {
// #678: Web Storage lives in Rust, keyed by origin, so these pin the
// behaviour the realm-local object could not provide.
+ #[test]
+ fn origin_storage_rejects_duplicate_profile_keys_without_replacing_valid_data() {
+ let store = OriginStorage::default();
+ assert!(store.set("https://app.test", "token".into(), "original".into()));
+ store.load("https://app.test", vec![("key".into(), "one".into()), ("key".into(), "two".into())]);
+ assert_eq!(store.get("https://app.test", "token").as_deref(), Some("original"));
+ }
+
+ #[test]
+ fn origin_storage_rejects_over_budget_profiles_without_replacing_valid_data() {
+ let store = OriginStorage::default();
+ assert!(store.set("https://app.test", "token".into(), "original".into()));
+ store.load("https://app.test", vec![("key".into(), "x".repeat(LOCAL_STORAGE_ORIGIN_LIMIT))]);
+ assert_eq!(store.get("https://app.test", "token").as_deref(), Some("original"));
+ let count_limited = OriginStorage::default();
+ for index in 0..LOCAL_STORAGE_ORIGIN_COUNT_LIMIT {
+ assert!(count_limited.set(&format!("https://origin-{index}.test"), "key".into(), "value".into()));
+ }
+ count_limited.load("https://extra.test", vec![("key".into(), "value".into())]);
+ assert_eq!(count_limited.get("https://extra.test", "key"), None);
+ let total_limited = OriginStorage::default();
+ for index in 0..6 {
+ assert!(total_limited.set(&format!("https://large-{index}.test"), "key".into(), "x".repeat(LOCAL_STORAGE_ORIGIN_LIMIT - 3)));
+ }
+ total_limited.load("https://extra.test", vec![("key".into(), "x".repeat(LOCAL_STORAGE_ORIGIN_LIMIT - 3))]);
+ assert_eq!(total_limited.get("https://extra.test", "key"), None);
+ }
+
+ #[test]
+ fn origin_storage_restores_profiles_without_marking_them_dirty() {
+ let store = OriginStorage::default();
+ store.load("https://app.test", vec![("token".into(), "restored".into())]);
+ assert_eq!(store.get("https://app.test", "token").as_deref(), Some("restored"));
+ assert!(!store.is_dirty(), "reading a saved profile must not schedule a rewrite");
+ }
+
#[test]
fn origin_storage_keeps_origins_apart_and_preserves_insertion_order() {
let store = OriginStorage::default();
@@ -5193,7 +4862,7 @@ mod tests {
let store = OriginStorage::default();
let origin = "https://a.example";
assert!(store.set(origin, "k".into(), "v".into()));
- store.take_changed();
+ store.flush_changed(|_, _| true);
assert!(!store.is_dirty());
store.clear(origin);
assert!(store.is_dirty(), "a clear must mark the store dirty");
@@ -6398,7 +6067,8 @@ fn op_get_cookies(scope: &mut v8::PinScope, state: &OpState) -> String {
fn storage_origin(state: &ObscuraState) -> String {
state.inherited_origin.clone().unwrap_or_else(|| {
url::Url::parse(&state.url)
- .map(|url| url.origin().ascii_serialization())
+ .map(|url| if url.scheme() == "file" { "file://".to_string() }
+ else { url.origin().ascii_serialization() })
.unwrap_or_else(|_| "null".to_string())
})
}
@@ -6410,11 +6080,16 @@ fn origin_storage_command(
key: &str,
value: &str,
) -> String {
+ if origin == "null" { return "{\"error\":\"SecurityError\"}".to_string(); }
let Some(storage) = storage else {
return "null".to_string();
};
match command {
+ "access" => "true".to_string(),
+ "length" => storage.len(origin).to_string(),
+ "key" => serde_json::to_string(&key.parse::<usize>().ok().and_then(|index| storage.key(origin, index)))
+ .unwrap_or_else(|_| "null".to_string()),
"snapshot" => serde_json::to_string(&storage.snapshot(origin))
.unwrap_or_else(|_| "[]".to_string()),
"get" => serde_json::to_string(&storage.get(origin, key))
@@ -6455,20 +6130,6 @@ fn op_local_storage(
)
}
-/// IndexedDB backend. One command per call, JSON in and JSON out, so the shim
-/// keeps the spec's object graph and only the data lives in Rust.
-#[op2]
-#[string]
-fn op_indexed_db(
- scope: &mut v8::PinScope,
- state: &OpState,
- #[string] payload: &str,
-) -> String {
- let gs = realm_state(scope, state);
- let gs = gs.borrow();
- indexed_db_command(&gs.indexed_db, &storage_origin(&gs), payload)
-}
-
/// `sessionStorage` backend, page-scoped in exactly the same way.
#[op2]
#[string]
@@ -7736,7 +7397,6 @@ pub fn build_extension() -> Extension {
op_set_cookie(),
op_local_storage(),
op_session_storage(),
- op_indexed_db(),
op_navigate(),
op_session_history(),
op_history_traverse(),
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index e0f78a4f..89b1b3a5 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -20,7 +20,7 @@ use crate::module_loader::{ModuleLoadActivity, ObscuraModuleLoader};
#[cfg(all(test, feature = "render"))]
use crate::ops::ensure_prepared_render;
use crate::ops::{
- IndexedDbStorage, ObscuraState, OriginStorage, RuntimeEvent, StoredNetworkResponseBody,
+ ObscuraState, OriginStorage, RuntimeEvent, StoredNetworkResponseBody,
begin_animation_task, build_extension, node_is_script,
};
#[cfg(feature = "render")]
@@ -1181,12 +1181,6 @@ impl ObscuraJsRuntime {
self.state.borrow_mut().session_storage = Some(storage);
}
- /// Install the context-scoped IndexedDB backing store, so records and the
- /// schema outlive the realm that wrote them.
- pub fn set_indexed_db(&self, storage: std::sync::Arc<IndexedDbStorage>) {
- self.state.borrow_mut().indexed_db = Some(storage);
- }
-
pub fn set_http_client(&self, client: std::sync::Arc<obscura_net::ObscuraHttpClient>) {
let mut state = self.state.borrow_mut();
state.http_client = Some(client);
@@ -22436,6 +22430,68 @@ mod tests {
);
}
+ #[test]
+ fn web_storage_preserves_undefined_values_and_rejects_symbol_arguments() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ assert_eq!(rt.evaluate(r#"[localStorage, sessionStorage].map(store => {
+ store.setItem('undefined', undefined);
+ const errors = [() => store.setItem(Symbol('key'), 'value'),
+ () => store.setItem('symbol', Symbol('value')),
+ () => store.getItem(Symbol('key')), () => store.removeItem(Symbol('key'))]
+ .map(call => { try { call(); return null; } catch (error) { return error.name; } });
+ return [store.getItem('undefined'), ...errors];
+ })"#).unwrap(), serde_json::json!([
+ ["undefined", "TypeError", "TypeError", "TypeError", "TypeError"],
+ ["undefined", "TypeError", "TypeError", "TypeError", "TypeError"]
+ ]));
+ }
+
+ #[test]
+ fn web_storage_preserves_unpaired_utf16_and_transport_prefixes() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ assert_eq!(rt.evaluate(r#"[localStorage, sessionStorage].map(store => {
+ store.setItem('\ud800', '\udfff');
+ store.setItem('\0"ordinary"', '\0"literal"');
+ return [store.key(0).charCodeAt(0), store.getItem('\ud800').charCodeAt(0),
+ Object.keys(store).map(key => key.charCodeAt(0)), store.getItem('\0"ordinary"')];
+ })"#).unwrap(), serde_json::json!([
+ [55296, 57343, [55296, 0], "\0\"literal\""],
+ [55296, 57343, [55296, 0], "\0\"literal\""]
+ ]));
+ }
+
+ #[test]
+ fn web_storage_initial_frame_inherits_the_parent_origin() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ assert_eq!(rt.evaluate(r#"(() => {
+ localStorage.setItem('local', 'shared');
+ sessionStorage.setItem('session', 'same-tab');
+ const frame = document.createElement('iframe');
+ document.body.appendChild(frame);
+ return [frame.contentWindow.localStorage.getItem('local'),
+ frame.contentWindow.sessionStorage.getItem('session')];
+ })()"#).unwrap(), serde_json::json!(["shared", "same-tab"]));
+ }
+
+ #[test]
+ fn web_storage_file_documents_share_the_file_origin() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ rt.set_url("file:///tmp/first.html");
+ rt.evaluate("localStorage.setItem('token', 'file-value')").unwrap();
+ rt.set_url("file:///tmp/second.html");
+ assert_eq!(rt.evaluate("localStorage.getItem('token')").unwrap(), serde_json::json!("file-value"));
+ }
+
+ #[test]
+ fn web_storage_getters_reject_opaque_origins() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ rt.set_url("data:text/html,opaque");
+ assert_eq!(rt.evaluate(r#"['localStorage', 'sessionStorage'].map(name => {
+ try { return globalThis[name].getItem('token'); }
+ catch (error) { return error.name; }
+ })"#).unwrap(), serde_json::json!(["SecurityError", "SecurityError"]));
+ }
+
#[test]
fn test_storage_event_constructor_and_legacy_factory() {
let mut rt = setup_runtime("<html><body></body></html>");
diff --git a/crates/obscura-mcp/src/lib.rs b/crates/obscura-mcp/src/lib.rs
index b1c9de39..87916c3c 100644
--- a/crates/obscura-mcp/src/lib.rs
+++ b/crates/obscura-mcp/src/lib.rs
@@ -2221,39 +2221,19 @@ fn tool_storage_state(state: &mut BrowserState) -> Result<String, String> {
"same_site": c.same_site,
"expires": c.expires,
})).collect();
- // Web Storage now lives in the context, so every origin the browser has
- // seen is reported, not only the page's current one. That is the whole point
- // of an exported session state: an agent stashes it and restores it later.
+ // Pull localStorage + sessionStorage for the current page's origin.
let storage_js = r#"(function(){
var ls = [], ss = [];
try { for (var i = 0; i < localStorage.length; i++) { var k = localStorage.key(i); ls.push([k, localStorage.getItem(k)]); } } catch(e) {}
try { for (var j = 0; j < sessionStorage.length; j++) { var k2 = sessionStorage.key(j); ss.push([k2, sessionStorage.getItem(k2)]); } } catch(e) {}
return { origin: location.origin || '', localStorage: ls, sessionStorage: ss };
})()"#;
- let mut origins: Vec<Value> = Vec::new();
- if state.active_tab.is_some() {
- let storage = state.page_mut().evaluate(storage_js);
- if storage.is_object() {
- origins.push(storage);
- }
- }
- // Any origin written by a page that has since navigated away still counts.
- for (origin, entries) in state.context.local_storage.entries() {
- if entries.is_empty() {
- continue;
- }
- if origins
- .iter()
- .any(|entry| entry.get("origin").and_then(Value::as_str) == Some(origin.as_str()))
- {
- continue;
- }
- origins.push(json!({
- "origin": origin,
- "localStorage": entries,
- "sessionStorage": [],
- }));
- }
+ let storage = if state.active_tab.is_some() {
+ state.page_mut().evaluate(storage_js)
+ } else {
+ Value::Null
+ };
+ let origins = if storage.is_object() { vec![storage] } else { vec![] };
let out = json!({ "cookies": cookies, "origins": origins });
serde_json::to_string_pretty(&out).map_err(|e| e.to_string())
}
diff --git a/docs/Persist-cookies-and-storage.md b/docs/Persist-cookies-and-storage.md
index 153f31f0..f9f73855 100644
--- a/docs/Persist-cookies-and-storage.md
+++ b/docs/Persist-cookies-and-storage.md
@@ -17,9 +17,13 @@ obscura serve --storage-dir ./obscura-data
All CDP sessions read and write to the same directory. Run separate `obscura serve` processes with different `--storage-dir` paths for isolated profiles.
-`--storage-dir` is a global flag, so it applies to `obscura mcp` as well. The MCP
-server picks it up from `OBSCURA_STORAGE_DIR`, which `obscura mcp` sets from the
-flag, and it flushes the profile on exit.
+For MCP, put the profile flag before the subcommand:
+
+```bash
+obscura --storage-dir ./obscura-data mcp
+```
+
+The MCP server also accepts `OBSCURA_STORAGE_DIR` and flushes the profile on exit.
## Layout
@@ -28,9 +32,8 @@ Inside `./obscura-data`:
- `cookies.json`: cookie jar in a stable format with `same_site`, `expires`, `http_only`, `secure`.
- `localStorage/<origin>-<digest>.json`: one file per origin, holding
`{"origin": "...", "entries": [["key", "value"], ...]}`. The digest is an FNV-1a
- hash of the origin, so two origins that sanitize to the same filename cannot
- collide. Entries stay in insertion order because `Storage.key(i)` is defined
- in insertion order.
+ hash of the origin, avoiding collisions caused by filename sanitization.
+ This implementation retains entries in insertion order.
Inspect one with `jq`:
@@ -56,6 +59,18 @@ is dropped when the tab goes away, the same as a real browser, but it does
survive navigation and CDP target switching within the process
([#678](https://github.com/h4ckf0r0day/obscura/issues/678)).
+Web Storage is limited to 5 MiB per origin, 32 MiB per backing store and 256
+origins. Failed profile writes and removals stay pending for a later flush.
+The budgets count UTF-8 backing bytes. Strings containing unpaired UTF-16 units
+or starting with NUL use a NUL-prefixed JSON string escape internally, including
+in profile files; the JavaScript API decodes them losslessly. Normal strings are
+stored unchanged. Encoded strings consume more of the byte budget.
+Shared contexts serialize flushes with storage mutations. Use separate profile
+directories for independent browser processes; this is not a cross-process
+database. Opaque-origin documents cannot access either store.
+
+Automatic cross-document `storage` events are not implemented by this change.
+
## Login once, scrape many
```bash