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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//! Checked, bounded request snapshots. No request slot is retained by this module.
use crate::format::{IrError, IslandEntry, MAX_ISLAND_STATE_BYTES};
use crate::parser::IrModule;
use crate::schema::{PropField, PropType};
use crate::slot::{SlotData, SlotValue};
const PREFIX: &str = "{\"v\":1,\"s\":[],\"i\":[";
const SUFFIX: &str = "]}";
static NULL: SlotValue = SlotValue::Null;
/// A complete, checked island-state envelope for one rendered page.
///
/// The encoded bytes own each occurrence's declared inputs as they were at its
/// ISLAND_START. They contain no references to mutable slots. Construction is
/// private, and the complete escaped UTF-8 envelope is capped at one MiB.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IslandState {
json: String,
count: usize,
}
impl IslandState {
/// Compact JSON safe as the text of an application/json script: every `<`
/// is escaped. The server places it once in the head and charges its bytes
/// to the final response budget.
pub fn as_json(&self) -> &str {
&self.json
}
/// Whether the walk executed no island occurrences.
pub fn is_empty(&self) -> bool {
self.count == 0
}
/// Number of executed island occurrences, including repeated list rows.
pub fn len(&self) -> usize {
self.count
}
}
pub(crate) struct StateBuilder {
encoded: String,
count: usize,
}
impl StateBuilder {
pub(crate) fn new() -> Self {
Self {
encoded: PREFIX.to_owned(),
count: 0,
}
}
/// Encode directly into capped owned storage, transactionally. The suffix
/// is reserved on every append; a failed entry is discarded in its entirety.
pub(crate) fn snapshot(
&mut self,
module: &IrModule,
entry: &IslandEntry,
slots: &SlotData,
) -> Result<(usize, String), IrError> {
let before = self.encoded.len();
let occurrence = self.count;
let hash = entry
.digest
.iter()
.map(|byte| format!("{byte:02x}"))
.collect::<String>();
let result = (|| {
if entry.digest.len() != 8 {
return Err(invalid(entry.id, &[], "identity digest is not eight bytes"));
}
let fields = module
.schema
.objects
.get(entry.schema_object as usize)
.ok_or_else(|| invalid(entry.id, &[], "missing declared island object"))?;
if fields.len() != entry.slot_ids.len() {
return Err(invalid(
entry.id,
&[],
"ordered input count differs from declared fields",
));
}
if occurrence > 0 {
self.push(",")?;
}
self.push("{\"h\":\"")?;
self.push(&hash)?;
self.push("\",\"p\":")?;
self.project(
module,
entry.id,
Task::Object {
fields,
values: ObjectValues::Inputs(&entry.slot_ids, slots),
index: 0,
emitted: false,
},
)?;
self.push("}")?;
Ok(())
})();
if let Err(error) = result {
self.encoded.truncate(before);
return Err(error);
}
self.count += 1;
Ok((occurrence, hash))
}
pub(crate) fn finish(mut self) -> Result<IslandState, IrError> {
let charged = self
.encoded
.len()
.checked_add(SUFFIX.len())
.ok_or_else(limit)?;
if charged > MAX_ISLAND_STATE_BYTES {
return Err(limit());
}
self.encoded.push_str(SUFFIX);
// Finalization reads only owned bytes, never the module or slots.
if self.encoded.len() != charged {
return Err(limit());
}
Ok(IslandState {
json: self.encoded,
count: self.count,
})
}
fn push(&mut self, text: &str) -> Result<(), IrError> {
let next = self
.encoded
.len()
.checked_add(text.len())
.and_then(|n| n.checked_add(SUFFIX.len()))
.ok_or_else(limit)?;
if next > MAX_ISLAND_STATE_BYTES {
return Err(limit());
}
self.encoded.push_str(text);
Ok(())
}
fn string(&mut self, text: &str) -> Result<(), IrError> {
self.push("\"")?;
for ch in text.chars() {
match ch {
'"' => self.push("\\\"")?,
'\\' => self.push("\\\\")?,
'\n' => self.push("\\n")?,
'\r' => self.push("\\r")?,
'\t' => self.push("\\t")?,
'\u{08}' => self.push("\\b")?,
'\u{0c}' => self.push("\\f")?,
'<' => self.push("\\u003c")?,
'\0'..='\u{1f}' => {
self.push(&format!("\\u{:04x}", ch as u8))?;
}
_ => {
let mut bytes = [0; 4];
self.push(ch.encode_utf8(&mut bytes))?;
}
}
}
self.push("\"")
}
fn project<'a>(
&mut self,
module: &'a IrModule,
island: u16,
first: Task<'a>,
) -> Result<(), IrError> {
let mut tasks = vec![first];
let mut path = Vec::new();
// Array and object frames schedule one child at a time. The work stack
// is bounded by the already validated acyclic schema, not request size.
while let Some(task) = tasks.pop() {
match task {
Task::PathEnd => {
path.pop();
}
Task::Value(kind, value) => match (kind, value) {
(PropType::Optional(_), SlotValue::Null) => self.push("null")?,
(PropType::Optional(inner), _) => tasks.push(Task::Value(inner, value)),
(PropType::Boolean, SlotValue::Bool(value)) => {
self.push(if *value { "true" } else { "false" })?
}
(PropType::String, SlotValue::Text(value)) => self.string(value)?,
(PropType::Number, SlotValue::Number(number)) if number.is_finite() => {
self.push(&value.to_text())?
}
(PropType::Date, SlotValue::Number(number))
if number.is_finite()
&& number.fract() == 0.0
&& number.abs() <= 8_640_000_000_000_000.0 =>
{
self.push(&value.to_text())?
}
(PropType::Enum(members), SlotValue::Text(value))
if members.iter().any(|id| {
module.strings.get(*id).is_ok_and(|member| member == value)
}) =>
{
self.string(value)?
}
(PropType::Array(inner), SlotValue::Array(values)) => tasks.push(Task::Array {
kind: inner,
values,
index: 0,
}),
(PropType::Object(id), SlotValue::Object(values)) => {
let fields = module
.schema
.objects
.get(*id as usize)
.ok_or_else(|| invalid(island, &path, "missing declared object"))?;
tasks.push(Task::Object {
fields,
values: ObjectValues::Named(values),
index: 0,
emitted: false,
});
}
_ => return Err(invalid(island, &path, "missing or invalid declared value")),
},
Task::Array {
kind,
values,
index,
} => {
if index == 0 {
self.push("[")?;
}
if let Some(value) = values.get(index) {
if index > 0 {
self.push(",")?;
}
tasks.push(Task::Array {
kind,
values,
index: index + 1,
});
path.push(Path::Index(index));
tasks.push(Task::PathEnd);
tasks.push(Task::Value(kind, value));
} else {
self.push("]")?;
}
}
Task::Object {
fields,
values,
index,
emitted,
} => {
if index == 0 {
self.push("{")?;
}
if let Some(field) = fields.get(index) {
let name = module.strings.get(field.name)?;
let value = match values {
ObjectValues::Inputs(ids, slots) => slots.get(ids[index]),
ObjectValues::Named(entries) => {
let mut matches = entries.iter().filter(|(key, _)| key == name);
let value = matches.next().map_or(&NULL, |(_, value)| value);
if matches.next().is_some() {
path.push(Path::Field(name));
return Err(invalid(
island,
&path,
"duplicate declared input field",
));
}
value
}
};
if matches!(field.kind, PropType::Optional(_))
&& matches!(value, SlotValue::Null)
{
tasks.push(Task::Object {
fields,
values,
index: index + 1,
emitted,
});
continue;
}
if emitted {
self.push(",")?;
}
self.string(name)?;
self.push(":")?;
tasks.push(Task::Object {
fields,
values,
index: index + 1,
emitted: true,
});
path.push(Path::Field(name));
tasks.push(Task::PathEnd);
tasks.push(Task::Value(&field.kind, value));
} else {
self.push("}")?;
}
}
}
}
Ok(())
}
}
#[derive(Clone, Copy)]
enum ObjectValues<'a> {
Inputs(&'a [u16], &'a SlotData),
Named(&'a [(String, SlotValue)]),
}
enum Task<'a> {
Value(&'a PropType, &'a SlotValue),
Array {
kind: &'a PropType,
values: &'a [SlotValue],
index: usize,
},
Object {
fields: &'a [PropField],
values: ObjectValues<'a>,
index: usize,
emitted: bool,
},
PathEnd,
}
enum Path<'a> {
Field(&'a str),
Index(usize),
}
fn invalid(island: u16, path: &[Path<'_>], reason: &'static str) -> IrError {
use std::fmt::Write;
let mut name = String::from("$");
for segment in path {
match segment {
Path::Field(field) => {
let _ = write!(name, "[{field:?}]");
}
Path::Index(index) => {
let _ = write!(name, "[{index}]");
}
}
}
IrError::InvalidIslandState(Box::new(crate::format::IslandStateViolation {
island,
path: name,
reason,
}))
}
fn limit() -> IrError {
IrError::IslandStateLimitExceeded {
limit: MAX_ISLAND_STATE_BYTES,
}
}