durable-actors 0.4.0

Standalone regional durable-actors control plane, host, and durability runtime
Documentation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use std::collections::HashSet;

use anyhow::{Context, Result, ensure};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::admin::validate_component;

#[derive(Clone, Debug)]
pub(crate) struct PublicActorContract {
    document: Value,
    hash: String,
}

impl PublicActorContract {
    pub(crate) fn new(document: Value) -> Result<Self> {
        let parsed: ContractDocument =
            serde_json::from_value(document.clone()).context("invalid public actor contract")?;
        parsed.validate()?;
        let document = canonical_json(document);
        let bytes = serde_json::to_vec(&document)?;
        ensure!(
            bytes.len() <= 4 * 1024 * 1024,
            "public actor contract exceeds 4 MiB"
        );
        let digest = aws_lc_rs::digest::digest(&aws_lc_rs::digest::SHA256, &bytes);
        let hash = format!(
            "sha256:{}",
            digest
                .as_ref()
                .iter()
                .map(|byte| format!("{byte:02x}"))
                .collect::<String>()
        );
        Ok(Self { document, hash })
    }

    pub(crate) fn document(&self) -> &Value {
        &self.document
    }
    pub(crate) fn hash(&self) -> &str {
        &self.hash
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct PublishedContract {
    pub contract_hash: String,
    pub contract: Value,
}

impl PublishedContract {
    pub(crate) fn new(contract: &PublicActorContract) -> Self {
        Self {
            contract_hash: contract.hash().into(),
            contract: contract.document().clone(),
        }
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ContractDocument {
    version: u32,
    actors: Vec<ActorApi>,
}

impl ContractDocument {
    fn validate(&self) -> Result<()> {
        ensure!(
            self.version == 1,
            "unsupported public actor contract version {}",
            self.version
        );
        let mut names = HashSet::new();
        for actor in &self.actors {
            validate_actor_name(&actor.actor_name)?;
            ensure!(
                names.insert(&actor.actor_name),
                "duplicate actor name {}",
                actor.actor_name
            );
            ensure!(
                actor.socket.version == 1 && actor.socket.actor_name == actor.actor_name,
                "actor and socket contract must match"
            );
            validate_schema(&actor.socket.schema)?;
            for kind in ["Metadata", "Incoming", "Outgoing", "State"] {
                ensure!(
                    actor.socket.schema["definitions"].get(kind).is_some(),
                    "missing socket type {kind}"
                );
            }
            let mut fields = HashSet::new();
            for field in &actor.socket.emittable {
                ensure!(fields.insert(field), "duplicate emittable field {field}");
                ensure!(
                    actor.socket.schema["definitions"]["State"]["properties"]
                        .get(field)
                        .is_some(),
                    "emittable field {field} is not public state"
                );
            }
            actor.rpc.validate()?;
        }
        Ok(())
    }
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct ActorApi {
    actor_name: String,
    socket: SocketContract,
    rpc: RpcContract,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct SocketContract {
    version: u32,
    actor_name: String,
    schema: Value,
    emittable: Vec<String>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RpcContract {
    schema: Value,
    methods: Vec<RpcMethod>,
}

impl RpcContract {
    fn validate(&self) -> Result<()> {
        validate_schema(&self.schema)?;
        let mut names = HashSet::new();
        for method in &self.methods {
            validate_component("actor method", &method.name, 255)?;
            ensure!(
                names.insert(&method.name),
                "duplicate actor method {}",
                method.name
            );
            ensure!(
                ![
                    "constructor",
                    "then",
                    "connect",
                    "broadcast",
                    "onConnect",
                    "onMessage",
                    "onDisconnect"
                ]
                .contains(&method.name.as_str()),
                "reserved actor method {}",
                method.name
            );
            let mut optional_seen = false;
            for (index, parameter) in method.parameters.iter().enumerate() {
                ensure!(
                    !parameter.name.is_empty(),
                    "RPC parameter name must not be empty"
                );
                let kind = parameter.kind.validate(&self.schema)?;
                ensure!(
                    !parameter.rest
                        || (!parameter.optional
                            && index + 1 == method.parameters.len()
                            && kind["type"] == "array"
                            && !kind["items"].is_array()),
                    "rest parameter must be a final, required array parameter"
                );
                ensure!(
                    !optional_seen || parameter.optional || parameter.rest,
                    "required parameter follows optional parameter"
                );
                optional_seen |= parameter.optional;
            }
            if let RpcResult::Value { kind } = &method.result {
                kind.validate(&self.schema)?;
            }
        }
        Ok(())
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RpcMethod {
    name: String,
    parameters: Vec<RpcParameter>,
    result: RpcResult,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RpcParameter {
    name: String,
    optional: bool,
    rest: bool,
    #[serde(rename = "type")]
    kind: TypeReference,
}

#[derive(Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase", deny_unknown_fields)]
enum RpcResult {
    Void,
    Value {
        #[serde(rename = "type")]
        kind: TypeReference,
    },
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct TypeReference {
    #[serde(rename = "$ref")]
    reference: String,
}

impl TypeReference {
    fn validate<'a>(&self, schema: &'a Value) -> Result<&'a Value> {
        validate_reference(&self.reference, schema)
    }
}

fn validate_actor_name(name: &str) -> Result<()> {
    validate_component("actor name", name, 255)?;
    ensure!(
        name.bytes()
            .enumerate()
            .all(|(index, byte)| byte.is_ascii_alphabetic()
                || byte == b'_'
                || (index > 0 && byte.is_ascii_digit()))
            && !is_typescript_keyword(name),
        "actor name {name} cannot be emitted as a TypeScript identifier"
    );
    Ok(())
}

fn is_typescript_keyword(name: &str) -> bool {
    // Match the SDK's TypeScript scanner, including contextual keywords.
    [
        "abstract",
        "accessor",
        "any",
        "as",
        "asserts",
        "assert",
        "bigint",
        "boolean",
        "break",
        "case",
        "catch",
        "class",
        "continue",
        "const",
        "constructor",
        "debugger",
        "declare",
        "default",
        "defer",
        "delete",
        "do",
        "else",
        "enum",
        "export",
        "extends",
        "false",
        "finally",
        "for",
        "from",
        "function",
        "get",
        "if",
        "implements",
        "import",
        "in",
        "infer",
        "instanceof",
        "interface",
        "intrinsic",
        "is",
        "keyof",
        "let",
        "module",
        "namespace",
        "never",
        "new",
        "null",
        "number",
        "object",
        "package",
        "private",
        "protected",
        "public",
        "override",
        "out",
        "readonly",
        "require",
        "global",
        "return",
        "satisfies",
        "set",
        "static",
        "string",
        "super",
        "switch",
        "symbol",
        "this",
        "throw",
        "true",
        "try",
        "type",
        "typeof",
        "undefined",
        "unique",
        "unknown",
        "using",
        "var",
        "void",
        "while",
        "with",
        "yield",
        "async",
        "await",
        "of",
    ]
    .contains(&name)
}

fn validate_schema(schema: &Value) -> Result<()> {
    ensure!(
        schema.is_object() && schema["definitions"].is_object(),
        "contract schema must contain definitions"
    );
    validate_schema_node(schema, schema)
}

fn validate_schema_node(node: &Value, root: &Value) -> Result<()> {
    if node.is_boolean() {
        return Ok(());
    }
    let object = node
        .as_object()
        .context("contract schema must be an object or boolean")?;
    ensure!(
        !object.contains_key("$id") && !object.contains_key("id") && !object.contains_key("tsType"),
        "contract schemas cannot override type resolution"
    );
    if let Some(reference) = object.get("$ref") {
        validate_reference(
            reference.as_str().context("schema $ref must be a string")?,
            root,
        )?;
    }
    for key in ["definitions", "properties", "patternProperties"] {
        if let Some(children) = object.get(key) {
            for child in children
                .as_object()
                .context("schema properties must be an object")?
                .values()
            {
                validate_schema_node(child, root)?;
            }
        }
    }
    for key in [
        "items",
        "additionalItems",
        "additionalProperties",
        "contains",
        "propertyNames",
        "not",
        "if",
        "then",
        "else",
        "allOf",
        "anyOf",
        "oneOf",
    ] {
        if let Some(child) = object.get(key) {
            if let Some(children) = child.as_array() {
                for child in children {
                    validate_schema_node(child, root)?;
                }
            } else {
                validate_schema_node(child, root)?;
            }
        }
    }
    if let Some(dependencies) = object.get("dependencies") {
        for child in dependencies
            .as_object()
            .context("schema dependencies must be an object")?
            .values()
        {
            if !child.is_array() {
                validate_schema_node(child, root)?;
            }
        }
    }
    Ok(())
}

fn validate_reference<'a>(reference: &str, root: &'a Value) -> Result<&'a Value> {
    ensure!(
        reference.starts_with("#/definitions/"),
        "contract type references must be local definitions"
    );
    root.pointer(&reference[1..])
        .filter(|target| target.is_object() || target.is_boolean())
        .with_context(|| format!("contract type reference is missing: {reference}"))
}

fn canonical_json(value: Value) -> Value {
    match value {
        Value::Object(object) => {
            let mut entries: Vec<_> = object.into_iter().collect();
            entries.sort_by(|left, right| left.0.cmp(&right.0));
            Value::Object(
                entries
                    .into_iter()
                    .map(|(key, value)| (key, canonical_json(value)))
                    .collect(),
            )
        }
        Value::Array(values) => Value::Array(values.into_iter().map(canonical_json).collect()),
        other => other,
    }
}

#[cfg(test)]
#[path = "../../tests/unit/control_plane/contract_tests.rs"]
mod tests;