diff --git a/runtime/console-api/Cargo.toml b/runtime/console-api/Cargo.toml index d142858..ebd08ac 100644 --- a/runtime/console-api/Cargo.toml +++ b/runtime/console-api/Cargo.toml @@ -6,3 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +capnp = "0.16" + +[build-dependencies] +capnpc = "0.16" diff --git a/runtime/console-api/build.rs b/runtime/console-api/build.rs new file mode 100644 index 0000000..5590bba --- /dev/null +++ b/runtime/console-api/build.rs @@ -0,0 +1,21 @@ +fn generate_api() { + println!("cargo:rerun-if-changed=schema"); + let mut compile_command = ::capnpc::CompilerCommand::new(); + compile_command + .src_prefix("schema") + .default_parent_module(vec!["schema".to_string()]) + .file("schema/async_ops.capnp") + .file("schema/common.capnp") + .file("schema/instrument.capnp") + .file("schema/resources.capnp") + .file("schema/streaming.capnp") + .file("schema/stream.capnp") + .file("schema/tasks.capnp") + .file("schema/trace.capnp") + .run() + .expect("Failed to generate API code"); +} + +fn main() { + generate_api() +} diff --git a/runtime/console-api/schema/instrument.capnp b/runtime/console-api/schema/instrument.capnp index 6a004fe..543e8ba 100644 --- a/runtime/console-api/schema/instrument.capnp +++ b/runtime/console-api/schema/instrument.capnp @@ -1,7 +1,7 @@ @0xd53fadcd6c8f437f; using Common = import "common.capnp"; -using Stream = import "stream.capnp"; +using Stream = import "streaming.capnp"; using Tasks = import "tasks.capnp"; using Resources = import "resources.capnp"; using AsyncOps = import "async_ops.capnp"; diff --git a/runtime/console-api/schema/resources.capnp b/runtime/console-api/schema/resources.capnp index 82e7f9f..12a0441 100644 --- a/runtime/console-api/schema/resources.capnp +++ b/runtime/console-api/schema/resources.capnp @@ -39,7 +39,7 @@ struct Resource { # The kind of this resource struct Kind { union { - wellKnown @0 :WellKnown; + known @0 :WellKnown; other @1 :Text; } diff --git a/runtime/console-api/schema/stream.capnp b/runtime/console-api/schema/stream.capnp index 68f5dd3..bfcb72b 100644 --- a/runtime/console-api/schema/stream.capnp +++ b/runtime/console-api/schema/stream.capnp @@ -1,12 +1,50 @@ -@0x85894bd060182ed3; +# Copyright (c) 2019 Cloudflare, Inc. and contributors +# Licensed under the MIT License: +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. -interface Receiver(T) { - send @0 (chunk :T) -> stream; - done @1 (); -} +@0x86c366a91393f3f8; +# Defines placeholder types used to provide backwards-compatibility while introducing streaming +# to the language. The goal is that old code generators that don't know about streaming can still +# generate code that functions, leaving it up to the application to implement flow control +# manually. -interface Sender { - pause @0 (); - resume @1 (); - stop @2 (); +$import "/capnp/c++.capnp".namespace("capnp"); + +struct StreamResult @0x995f9a3377c0b16e { + # Empty struct that serves as the return type for "streaming" methods. + # + # Defining a method like: + # + # write @0 (bytes :Data) -> stream; + # + # Is equivalent to: + # + # write @0 (bytes :Data) -> import "/capnp/stream.capnp".StreamResult; + # + # However, implementations that recognize streaming will elide the reference to StreamResult + # and instead give write() a different signature appropriate for streaming. + # + # Streaming methods do not return a result -- that is, they return Promise. This promise + # resolves not to indicate that the call was actually delivered, but instead to provide + # backpressure. When the previous call's promise resolves, it is time to make another call. On + # the client side, the RPC system will resolve promises immediately until an appropriate number + # of requests are in-flight, and then will delay promise resolution to apply back-pressure. + # On the server side, the RPC system will deliver one call at a time. } diff --git a/runtime/console-api/schema/streaming.capnp b/runtime/console-api/schema/streaming.capnp new file mode 100644 index 0000000..324adcb --- /dev/null +++ b/runtime/console-api/schema/streaming.capnp @@ -0,0 +1,12 @@ +@0x85894bd060182ed3; + +interface Receiver(T) { + send @0 (chunk :T) -> import "stream.capnp".StreamResult; + done @1 (); +} + +interface Sender { + pause @0 (); + resume @1 (); + stop @2 (); +} diff --git a/runtime/console-api/schema/trace.capnp b/runtime/console-api/schema/trace.capnp index aaf1a7a..6c509e4 100644 --- a/runtime/console-api/schema/trace.capnp +++ b/runtime/console-api/schema/trace.capnp @@ -1,7 +1,7 @@ @0xb021b19fd5986342; using Common = import "common.capnp"; -using Stream = import "stream.capnp"; +using Stream = import "streaming.capnp"; interface Trace { watch @0 (request :WatchRequest, receiver :Stream.Receiver(TraceEvent)) -> (stream :Stream.Sender); diff --git a/runtime/console-api/src/lib.rs b/runtime/console-api/src/lib.rs index 7d12d9a..12857ca 100644 --- a/runtime/console-api/src/lib.rs +++ b/runtime/console-api/src/lib.rs @@ -1,14 +1,11 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right -} +mod schema; -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} +pub use schema::{ + async_ops_capnp as async_ops, + common_capnp as common, + instrument_capnp as instrument, + resources_capnp as resource, + streaming_capnp as streaming, + tasks_capnp as tasks, + trace_capnp as trace, +}; diff --git a/runtime/console-api/src/schema.rs b/runtime/console-api/src/schema.rs new file mode 100644 index 0000000..19889c0 --- /dev/null +++ b/runtime/console-api/src/schema.rs @@ -0,0 +1,31 @@ +pub mod async_ops_capnp { + include!(concat!(env!("OUT_DIR"), "/async_ops_capnp.rs")); +} + +pub mod common_capnp { + include!(concat!(env!("OUT_DIR"), "/common_capnp.rs")); +} + +pub mod instrument_capnp { + include!(concat!(env!("OUT_DIR"), "/instrument_capnp.rs")); +} + +pub mod resources_capnp { + include!(concat!(env!("OUT_DIR"), "/resources_capnp.rs")); +} + +pub mod streaming_capnp { + include!(concat!(env!("OUT_DIR"), "/streaming_capnp.rs")); +} + +pub mod stream_capnp { + include!(concat!(env!("OUT_DIR"), "/stream_capnp.rs")); +} + +pub mod tasks_capnp { + include!(concat!(env!("OUT_DIR"), "/tasks_capnp.rs")); +} + +pub mod trace_capnp { + include!(concat!(env!("OUT_DIR"), "/trace_capnp.rs")); +}