Sends target an answer before it settles (Host.vowSend), so the fetch and every send cross in one update call. Add icp.yaml for icp-cli, the successor to dfx. Generated-by: Claude Code (claude-opus-5-5)
104 lines
3.5 KiB
JavaScript
104 lines
3.5 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');
|
|
|
|
const slotOf = (status, what) => {
|
|
if (status < 0) throw Error(`${what}: ${decoder.decode(staged())}`);
|
|
return status;
|
|
};
|
|
/** Stage what the Host queued, then carry it until the canister is quiet. */
|
|
const flush = async () => {
|
|
check(wasm.client_outbound(), 'outbound');
|
|
await pump();
|
|
};
|
|
/** Read a settled answer, or throw. */
|
|
const valueOf = slot => {
|
|
const kind = wasm.client_result_kind(slot);
|
|
if (kind === 0) throw Error(`answer ${slot} still pending after the canister went quiet`);
|
|
if (kind === 3) throw Error(`answer ${slot} broke`);
|
|
return kind === 2 ? Number(wasm.client_result_integer(slot)) : { slot };
|
|
};
|
|
|
|
/**
|
|
* An answer: what `fetch` or `send` will settle to. Sends can target one
|
|
* before it settles; nothing crosses the wire until `flush`.
|
|
*
|
|
* @typedef {{ slot: number }} Answer
|
|
*/
|
|
|
|
return freeze({
|
|
/** @returns {Answer} the capability published as `name` */
|
|
fetch: name => ({ slot: slotOf(withBytes(encoder.encode(name), (p, n) => wasm.client_fetch(p, n)), 'fetch') }),
|
|
/** @returns {Answer} `target`'s answer to `verb` */
|
|
send: (target, verb) => ({
|
|
slot: slotOf(withBytes(encoder.encode(verb), (p, n) => wasm.client_send(target.slot, p, n)), 'send'),
|
|
}),
|
|
flush,
|
|
/** @param {Answer} answer */
|
|
valueOf: answer => valueOf(answer.slot),
|
|
});
|
|
};
|