1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//! Authenticated inspector reads go directly to the live IDB server. This
//! module runs off the VM thread and never opens/upgrades a JS database.
use crate::json::Json;
#[cfg(feature = "indexeddb")]
mod enabled {
use super::*;
use sk_runtime::redjs::webapi::indexeddb::{
inspection::{self, Preview, Query, Response},
keypath::KeyPath,
};
fn text(request: &Json, key: &str, required: bool) -> Result<String, String> {
let value = request.get(key).and_then(Json::as_str).unwrap_or("");
if required && value.is_empty() {
return Err(format!("indexeddb requires {key}"));
}
if value.len() > 4096 {
return Err(format!("indexeddb {key} exceeds 4096 bytes"));
}
Ok(value.to_owned())
}
fn path(path: &KeyPath) -> Json {
match path {
KeyPath::String(path) => Json::s(String::from_utf16_lossy(path)),
KeyPath::List(paths) => Json::arr(
paths
.iter()
.take(128)
.map(|path| Json::s(String::from_utf16_lossy(path)))
.collect(),
),
}
}
fn preview(value: Preview) -> Json {
let mut fields = vec![
("type", Json::s(value.kind)),
("preview", Json::s(value.preview)),
("truncated", Json::Bool(value.truncated)),
];
if !value.children.is_empty() {
fields.push((
"children",
Json::arr(
value
.children
.into_iter()
.map(|child| {
Json::obj(vec![
("name", Json::s(child.name)),
("value", preview(child.value)),
])
})
.collect(),
),
));
}
if let Some(size) = value.size {
fields.push(("size", Json::n(size as f64)));
}
if let Some(mime) = value.mime {
fields.push(("mime", Json::s(mime)));
}
if let Some(name) = value.name {
fields.push(("name", Json::s(name)));
}
if let Some(reference) = value.reference {
fields.push(("ref", Json::n(reference as f64)));
}
if let Some(id) = value.object_id {
fields.push(("objectId", Json::n(id as f64)));
}
Json::obj(fields)
}
pub(super) fn command(request: &Json) -> Result<Json, String> {
let operation = request.get("op").and_then(Json::as_str).unwrap_or("list");
if operation == "list" && !sk_runtime::redjs::webapi::indexeddb::server::started() {
return Ok(Json::obj(vec![
("available", Json::Bool(false)),
(
"backend",
Json::s("IndexedDB has not been initialized by the application"),
),
("databases", Json::arr(Vec::new())),
("truncated", Json::Bool(false)),
]));
}
let query = match operation {
"list" => Query::List,
"schema" => Query::Schema {
database: text(request, "database", true)?.encode_utf16().collect(),
},
"records" => {
let limit = request.get("limit").and_then(Json::as_f64).unwrap_or(50.0);
if !limit.is_finite() || limit.fract() != 0.0 || !(1.0..=100.0).contains(&limit) {
return Err("indexeddb limit must be between 1 and 100".into());
}
let search = text(request, "search", false)?;
if search.chars().count() > 256 {
return Err("indexeddb search exceeds 256 characters".into());
}
let after = request
.get("after")
.and_then(Json::as_str)
.filter(|value| !value.is_empty())
.map(inspection::unhex)
.transpose()?;
Query::Records {
database: text(request, "database", true)?.encode_utf16().collect(),
store: text(request, "store", true)?,
index: Some(text(request, "index", false)?).filter(|value| !value.is_empty()),
after,
limit: limit as usize,
search,
}
}
_ => return Err("indexeddb is read-only: op must be list, schema, or records".into()),
};
let response = inspection::inspect(query)?
.recv_timeout(std::time::Duration::from_secs(2))
.map_err(|_| {
"IndexedDB inspection timed out; the application transaction is left untouched"
.to_owned()
})??;
Ok(match response {
Response::List {
available,
backend,
databases,
truncated,
} => Json::obj(vec![
("available", Json::Bool(available)),
("backend", Json::s(backend)),
(
"databases",
Json::arr(
databases
.into_iter()
.map(|database| {
Json::obj(vec![
("name", Json::s(database.name)),
("version", Json::n(database.version as f64)),
])
})
.collect(),
),
),
("truncated", Json::Bool(truncated)),
]),
Response::Schema {
database,
version,
stores,
truncated,
} => Json::obj(vec![
("available", Json::Bool(true)),
("database", Json::s(database)),
("version", Json::n(version as f64)),
(
"stores",
Json::arr(
stores
.into_iter()
.map(|store| {
Json::obj(vec![
("name", Json::s(store.name)),
(
"keyPath",
store.key_path.as_ref().map(path).unwrap_or(Json::Null),
),
("autoIncrement", Json::Bool(store.auto_increment)),
("count", Json::n(store.count as f64)),
(
"indexes",
Json::arr(
store
.indexes
.into_iter()
.map(|index| {
Json::obj(vec![
("name", Json::s(index.name)),
("keyPath", path(&index.key_path)),
("unique", Json::Bool(index.unique)),
(
"multiEntry",
Json::Bool(index.multi_entry),
),
("count", Json::n(index.count as f64)),
])
})
.collect(),
),
),
])
})
.collect(),
),
),
("truncated", Json::Bool(truncated)),
]),
Response::Records {
records,
next_cursor,
has_more,
scanned,
truncated,
} => Json::obj(vec![
("available", Json::Bool(true)),
(
"records",
Json::arr(
records
.into_iter()
.map(|record| {
Json::obj(vec![
("key", preview(record.key)),
("primaryKey", preview(record.primary_key)),
("value", preview(record.value)),
("encodedBytes", Json::n(record.encoded_bytes as f64)),
])
})
.collect(),
),
),
(
"nextCursor",
next_cursor
.map(|cursor| Json::s(inspection::hex(&cursor)))
.unwrap_or(Json::Null),
),
("hasMore", Json::Bool(has_more)),
("scanned", Json::n(scanned as f64)),
("truncated", Json::Bool(truncated)),
(
"searchScope",
Json::s(
"Key and bounded structured-clone preview; Blob bodies are not searched",
),
),
]),
})
}
}
/// Does not access JSC; invoke on the authenticated listener thread.
pub(super) fn command(request: &Json) -> Result<Json, String> {
#[cfg(feature = "indexeddb")]
{
enabled::command(request)
}
#[cfg(not(feature = "indexeddb"))]
{
let _ = request;
Ok(Json::obj(vec![
("available", Json::Bool(false)),
("backend", Json::s("IndexedDB is not enabled in this build")),
("databases", Json::arr(Vec::new())),
]))
}
}