Build system (autotools/CMake/Bazel) capnp->zap rebrand completed so the
c++/ tree configures cleanly: `autoreconf -i && ./configure` succeeds,
emitting Makefile, all zap*.pc / kj*.pc and the Zap CMake config. configure.ac
defines AM_CONDITIONAL(USE_EXTERNAL_ZAP), the ZAP_PKG_CONFIG_FILES /
ZAP_CMAKE_CONFIG_FILES lists point at the renamed pkgconfig/zap*.pc.in and
cmake/Zap*.cmake.in, and Makefile.am's `if USE_EXTERNAL_ZAP` matches. Bazel
loads @zap-cpp//src/zap:cc_zap_library.bzl. Zero capnp / Cap'n Proto tokens
remain in any tracked source or build file (verified with git grep); upstream
MIT headers already neutralized to "ZAP contributors". quick-test.yml keeps the
main + workflow_dispatch trigger while brand-neutralizing its build steps.
Whitespace-significant schema desugar (compiler/desugar.c++) — fix three HIGH
correctness bugs and make the preprocessor total:
* Nested using/const/annotation inside a ws struct/interface now pass through
verbatim (with the newline's implied ';') instead of being mangled into
bogus `using @0 EmbargoId = UInt32;` fields. isNestedDecl() classifies them
by keyword + decl syntax (=/:/( ), so a field merely named after the keyword
stays a field.
* Ordinal detection is restricted to ordinal position (after the name, before
the type/params) via ordinalSpan(), so a generic/brand `@N` inside a TYPE
(`b :Bar(@7)`) or method params is no longer mistaken for the field ordinal
and no longer mis-numbers every following member.
* Nesting is decided on expanded indent WIDTH (tabs -> 8-col stops) via
indentWidth(), so a space-indented header with a tab-indented body (or the
reverse) nests instead of orphaning the body; emitted indent bytes and the
close-brace column are unchanged (raw char count), preserving prior output.
* Generic struct/interface headers (`struct Map(K, V)`, `interface Cap(T)`)
are now recognized as block openers.
* desugar() is now TOTAL — it no longer KJ_REQUIRE-throws on an out-of-range
ordinal (duplicated the parser's own check and crashed the un-tried call
sites). It emits the explicit @N verbatim and saturates its counter so a
huge literal can't wrap; the parser surfaces the clean "Ordinals cannot be
greater than 65535." diagnostic at the right file:line:col. Removed the now
redundant validateAutoOrdinal and the dead kj/debug.h include. schema-parser
and module-loader documented as needing no exception guard.
desugar-test.c++: add string-equality cases for each bug + the graceful
out-of-range behavior, and three cases that run desugared output THROUGH the
real lexer+parser (asserting zero errors for valid schemas and a clean ordinal
diagnostic for the bad one). Brace back-compat proven byte-identical over the
18-file corpus; sample.ws.zap still desugars to sample.brace.zap exactly.
13 KiB
layout, title
| layout | title |
|---|---|
| slides | Slides: What's Next for Zap |
What's Next for Zap?
Zap supports streaming!
{% highlight zap %} interface FileStore { get @0 (name :Text, stream :Stream); put @1 (name :Text) -> (stream :Stream); }
interface Stream { write @0 (data :Data); end @1 (); } {% endhighlight %}
But flow control is up to the app.
Let's build it in.
{% highlight zap %} interface Stream { write @0 (data :Data) -> bulk; end @1 (); } {% endhighlight %}
What about realtime streams?
{% highlight zap %} interface VideoCallStream { sendFrame @0 (frame :Frame) -> realtime; } {% endhighlight %}
Best served on a UDP transport...
Forwarded request.
Where does response go?
Classic solution:
Proxy
Classic solution:
Redirect
Zap:
3-Party Handoff
(aka 3PH)
Zap:
3-Party Handoff
(aka 3PH)
... gonna need UDP
Zap:
3-Party Handoff
(aka 3PH)
... gonna need UDP
... and 0-RT crypto
API: "Tail call"
{% highlight c++ %} kj::Promise myRpc(MyRpcContext context) override { // Begin sub-request. auto subRequest = someCapability.someRpcRequest(); subRequest.setSomeParam(someValue);
// Send as a tail call. return context.tailCall(kj::mv(subRequest)); } {% endhighlight %}
Today: Will proxy
Future: 3PH
KJ client networking, no TLS:
{% highlight c++ %} void send() { auto io = kj::setupAsyncIo(); auto& network = io.provider->getNetwork(); auto addr = network.parseAddress("zap.org", 80) .wait(io.waitScope); auto connection = addr->connect().wait(io.waitScope); connection->write("GET /", 5).wait(io.waitScope); } {% endhighlight %}
KJ client networking with TLS:
{% highlight c++ %} void send() { auto io = kj::setupAsyncIo(); kj::TlsContext tls; auto network = tls.wrapNetwork(io.provider->getNetwork()); auto addr = network->parseAddress("zap.org", 443) .wait(io.waitScope); auto connection = addr->connect().wait(io.waitScope); connection->write("GET /", 5).wait(io.waitScope); } {% endhighlight %}
Diff:
{% highlight c++ %} void send() {
kj::TlsContext tls; tls.wrapNetwork( );
} {% endhighlight %}
{% highlight c++ %} void receive() { auto io = kj::setupAsyncIo(); auto& network = io.provider->getNetwork(); auto addr = network.parseAddress("*", 80) .wait(io.waitScope); auto listener = addr->listen(); auto connection = listener->accept().wait(io.waitScope); connection->write("HTTP/1.1 404 Not Found\r\n\r\n", 26) .wait(io.waitScope); } {% endhighlight %}
{% highlight c++ %} void receive() { auto io = kj::setupAsyncIo(); kj::TlsKeypair keypair { KEY_PEM_TEXT, CERT_PEM_TEXT }; kj::TlsContext::Options options; options.defaultKeypair = keypair; kj::TlsContext tls(options); auto& network = io.provider->getNetwork(); auto addr = network.parseAddress("*", 443).wait(io.waitScope); auto listener = tls.wrapPort(addr->listen()); auto connection = listener->accept().wait(io.waitScope); connection->write("HTTP/1.1 404 Not Found\r\n\r\n", 26) .wait(io.waitScope); } {% endhighlight %}
{% highlight c++ %} void receive() {
kj::TlsKeypair keypair { KEY_PEM_TEXT, CERT_PEM_TEXT }; kj::TlsContext::Options options; options.defaultKeypair = keypair; kj::TlsContext tls(options);
tls.wrapPort( );
} {% endhighlight %}
{% highlight c++ %} auto io = kj::setupAsyncIo(); kj::HttpHeaderTable headerTable; auto client = kj::newHttpClient( *headerTable, io.provider->getNetwork());
kj::HttpHeaders headers(*headerTable); auto response = client->request( kj::HttpMethod::GET, "http://zap.org", headers) .response.wait(io.waitScope);
KJ_ASSERT(response.statusCode == 200); KJ_LOG(INFO, response.body->readAllText().wait(io.waitScope)); {% endhighlight %}
Headers identified by small numbers.
{% highlight c++ %} kj::HttpHeaderTable::Builder builder; kj::HttpHeaderId userAgent = builder.add("User-Agent"); auto headerTable = builder.build();
kj::HttpHeaders headers(*headerTable); headers.set(kj::HttpHeaderId::HOST, "zap.org"); headers.set(userAgent, "kj-http/0.6"); {% endhighlight %}
Header parsing is zero-copy.
Ugly imperative code:
{% highlight c++ %} zap::MallocMessageBuilder message;
auto root = message.initRoot(); root.setFoo(123); root.setBar("foo"); auto inner = root.initBaz(); inner.setQux(true);
zap::writeMessageToFd(fd, message); {% endhighlight %}
Nice declarative code:
{% highlight c++ %} using namespace zap::init;
zap::MallocMessageBuilder message; message.initRoot( $foo = 123, $bar = "foo", $baz( $qux = true ) ); zap::writeMessageToFd(fd, message); {% endhighlight %}
Even better:
{% highlight c++ %} using namespace zap::init;
zap::writeMessageToFd(fd, $foo = 123, $bar = "foo", $baz( $qux = true ) ); {% endhighlight %}
{% highlight c++ %} struct { template struct Setter { T value; template void operator()(U& target) { target.setFoo(kj::fwd(value)); } };
template Setter operator=(T&& value) { return { kj::fwd(value) }; } } $foo; {% endhighlight %}
Not idiomatic:
{% highlight c++ %} zap::MallocMessageBuilder message;
MyStruct::Builder root = message.initRoot(); root.setFoo(123); root.setBar("foo"); InnerStruct::Builder inner = root.initBaz(); inner.setQux(true);
zap::writeMessageToFd(fd, message); {% endhighlight %}
Plain Old C++ Structs?
{% highlight c++ %} MyStruct root; root.foo = 123; root.bar = "foo"; InnerStruct inner; inner.qux = true; root.baz = kj::mv(inner);
zap::writeMessageToFd(fd, message); {% endhighlight %}
Caveat: No longer zero-copy.
{% highlight c++ %} zap::MallocMessageBuilder message; zap::readMessageCopy(input, message); auto root = message.getRoot(); auto oldListOrphan = root.disownStructList(); auto oldList = oldListOrphan.getReader(); auto newList = root.initStructList(oldList.size() - 1); for (auto i: kj::indices(newList)) { newList.setWithCaveats(i, oldList[i < indexToRemove ? i : i + 1]); } zap::MallocMessageBuilder message2; message2.setRoot(root.asReader()); zap::writeMessage(output, message2); {% endhighlight %}
{% highlight c++ %} auto root = zap::readMessageCopy(input); root.structList.erase(indexToRemove); zap::writeMessageCopy(output, root); {% endhighlight %}
{% highlight zap %} interface AddressBook { getPerson @0 (id :UInt32 $httpPath) -> (person :Person $httpBody(type = json)) $http(method = get, route = "person");
GET /person/
JSON response body
updatePerson @1 (id :UInt32 $httpPath, person :Person $httpBody(type = json)); $http(method = put, route = "person");
PUT /person/
JSON request body
} {% endhighlight %}
{% highlight zap %} addPerson @2 (person :Person $httpBody(type = json)) -> (id :UInt32 $httpBody(type = jsonField)); $http(method = post, route = "person");
POST /person
JSON request body
JSON response body (object containing field id)
getAll @3 (page :UInt32 = 0 $httpQuery) -> (people: List(Person) $httpBody(type = json)); $http(method = get);
GET /?page=
Query is optional.
JSAN (JSON array) response body.
{% endhighlight %}
{% highlight zap %} interface AddressBookService { getAddressBook @0 (key :String $httpPath) -> (result :AddressBook $httpPipeline); $http(route = "book");
GET /book/JrpmUduyHd8uW3x3TOXn2g/person/123
Becomes:
service.getAddressBook("JrpmUduyHd8uW3x3TOXn2g").send()
.getResult().getPerson(123).send()
GET /book/JrpmUduyHd8uW3x3TOXn2g
Becomes:
service.getAddressBook("JrpmUduyHd8uW3x3TOXn2g").send()
.getResult().getAll().send()
} {% endhighlight %}
Questions?