The canister is freestanding wasm32 importing only ic0. Each caller principal gets a CapTP session: start carries op:start-session, deliver carries later frames, and each reply carries the canister's frames. The client peer is wasm too, driven from Node by @icp-sdk/core. It holds answer releases so they ride ahead of the next call instead of costing an update call of their own: handshake, fetch, and each send are one update call apiece. Generated-by: Claude Code (claude-opus-5-5)
92 lines
3.0 KiB
JavaScript
92 lines
3.0 KiB
JavaScript
/**
|
|
* @file The client peer (`zig-out/bin/client.wasm`) driven over a canister's
|
|
* `start` and `deliver` update calls.
|
|
*/
|
|
|
|
const { freeze } = Object;
|
|
|
|
const decoder = new TextDecoder();
|
|
const encoder = new TextEncoder();
|
|
|
|
/**
|
|
* @typedef {(method: 'start' | 'deliver', frames: Uint8Array) => Promise<Uint8Array>} Carry
|
|
* one update call: the frames out, the frames in the reply
|
|
*/
|
|
|
|
/**
|
|
* Instantiate the client peer and open a session to `canisterId`.
|
|
*
|
|
* @param {object} io
|
|
* @param {WebAssembly.Module} io.module the compiled client.wasm
|
|
* @param {Uint8Array} io.seed 64 bytes of fresh entropy
|
|
* @param {Carry} io.carry
|
|
* @param {string} canisterId the canister's principal, as text
|
|
*/
|
|
export const openPeer = async ({ module, seed, carry }, canisterId) => {
|
|
const { exports: wasm } = await WebAssembly.instantiate(module, {
|
|
env: { report: (ptr, len) => console.error(readText(ptr, len)) },
|
|
});
|
|
const memory = () => new Uint8Array(wasm.memory.buffer);
|
|
const readText = (ptr, len) => decoder.decode(memory().slice(ptr, ptr + len));
|
|
|
|
/** Copy `bytes` into wasm memory for the length of `fn`. */
|
|
const withBytes = (bytes, fn) => {
|
|
const ptr = wasm.client_alloc(bytes.length || 1);
|
|
memory().set(bytes, ptr);
|
|
try {
|
|
return fn(ptr, bytes.length);
|
|
} finally {
|
|
wasm.client_free(ptr, bytes.length || 1);
|
|
}
|
|
};
|
|
const staged = () => memory().slice(wasm.client_out_ptr(), wasm.client_out_ptr() + wasm.client_out_len());
|
|
const check = (status, what) => {
|
|
if (status !== 0) throw Error(`${what}: ${decoder.decode(staged())}`);
|
|
};
|
|
|
|
/**
|
|
* Carry staged frames until the canister has nothing more to say. The
|
|
* first call of a session is `start`.
|
|
*/
|
|
let method = 'start';
|
|
const pump = async () => {
|
|
for (;;) {
|
|
const frames = staged();
|
|
if (frames.length === 0) return;
|
|
const reply = await carry(method, frames);
|
|
method = 'deliver';
|
|
check(withBytes(reply, wasm.client_feed), 'feed');
|
|
check(wasm.client_outbound(), 'outbound');
|
|
}
|
|
};
|
|
|
|
check(withBytes(seed, wasm.client_init), 'init');
|
|
check(withBytes(encoder.encode(canisterId), wasm.client_dial), 'dial');
|
|
await pump();
|
|
if (!wasm.client_session_open()) throw Error('session did not open');
|
|
|
|
/** Wait for the pending call's answer, carrying frames as needed. */
|
|
const settle = async () => {
|
|
await pump();
|
|
const kind = wasm.client_result_kind();
|
|
if (kind === 0) throw Error('no answer after the canister went quiet');
|
|
if (kind === 3) throw Error('broken');
|
|
return kind;
|
|
};
|
|
|
|
return freeze({
|
|
/** Fetch the capability published as `name` and make it the target. */
|
|
async fetch(name) {
|
|
check(withBytes(encoder.encode(name), wasm.client_fetch), 'fetch');
|
|
await settle();
|
|
check(wasm.client_adopt(), 'adopt');
|
|
},
|
|
/** Send `verb` to the target; resolve to its integer answer. */
|
|
async send(verb) {
|
|
check(withBytes(encoder.encode(verb), wasm.client_send), 'send');
|
|
await settle();
|
|
return Number(wasm.client_result_integer());
|
|
},
|
|
});
|
|
};
|