The WebAssembly port: the same engine, compiled for the browser

Because the ddbsh engine depends on neither a terminal nor the AWS SDK, it
compiles to WebAssembly with Emscripten. The wasm-release CMake preset setsDDBSH_BUILD_AWS=OFF and DDBSH_BUILD_WEB=ON, so the C++ SDK never enters the build. That leaves two jobs the SDK used to do: turning a planned request into the bytes DynamoDB expects, and carrying those bytes over the network.

ddbsh-wire: a codec held to the SDK’s bytes

Natively, the planner hands an SDK-free request struct to the ddbsh::aws adapter, and the AWS SDK for C++ serializes it. In the WASM build the engine
does that itself through ddbsh-wire, an SDK-free AWS-JSON-1.0 codec that
serializes requests and parses responses (including __type error documents)
back into the engine’s own structs.

The codec is held to a differential test, wire_serialize_diff_test. For a
corpus covering the data-plane and control-plane operations and every
AttributeValue type, it asserts that the codec’s body equals, byte for byte,
what the C++ SDK’s SerializePayload() produces. The browser therefore sends
the same request bodies the native binary sends.

The transport seam

WireClient implements IDDBClient as serialize, send, parse. The send step
goes through one small interface:

class Transport {
public:
virtual ~Transport() = default;
virtual Result<std::string> send(std::string_view operation,
std::string_view body) = 0;
virtual Result<void> on_connect(const std::string& region,
const std::string& endpoint); // default: no-op
virtual std::string descriptor() const; // default: a generic label
};

operation is the X-Amz-Target suffix ("Query", "PutItem"). The return
value is only the response body, which is all the codec needs to decide success
or error. on_connect lets a transport re-target when the user runs CONNECT.
descriptor feeds SHOW CONNECTION.

Crossing into JavaScript with Asyncify

The engine is synchronous; fetch is not. The browser transport,
JsSdkTransport, bridges the two with an Emscripten EM_ASYNC_JS hook, linked
with -sASYNCIFY:

EM_ASYNC_JS(char*, ddbsh_js_send, (const char* op, const char* body), {
// ...
const resp = await Module.ddbshSend(UTF8ToString(op), UTF8ToString(body));
return dup("0" + resp); // '1' + message on a transport failure
});

Asyncify suspends the WASM stack while the promise is pending and resumes it
with the result. A one-character prefix carries success or failure in a single
string. On the JavaScript side, Module.ddbshSend belongs to the SDK bridge
described in the next post.

Retries moved to JavaScript. The SDK makes up to 11 attempts per call in
standard mode, so the browser’s WireClient subclass cuts its own budget to one
attempt; sustained throttling costs 11 attempts, not 11 × 11.

One driver, two entry points

The embind surface is a single class, DdbshSession. It owns the transport, the
WireClient, and a Session, and it runs input through the same terminal-free
driver the CLI uses (cli/repl.hpp), buffering output instead of writing to file
descriptors:

const Module = await createDdbsh();
installHooks(Module, bridge);
const session = new Module.DdbshSession("us-east-1");
const result = await session.runScript(text, abortOnError);
for (const seg of result.segments) { /* {stream: "stdout"|"stderr", text} */ }

run(text) is the page’s path, one submission at a time; runScript() matches
the CLI’s -c/-f handling. The golden runner calls a small Node front end,
web/ddbsh-node.mjs, in place of the native binary. It loads the same files
the page loads, and all 710 golden tests pass through it against real DynamoDB.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.