feat(client): pipeline sends on the fetch's answer
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)
This commit is contained in:
1 parent
1177fc86b6
commit
da6c3f02ce
7 files changed
+197
-58
No files matched your search
+9
-7
@@ -4,8 +4,9 @@
|
||||
*/
|
||||
const HELP = `usage: counter.mjs HOST CANISTER_ID [VERB...]
|
||||
|
||||
Open a CapTP session to the canister, fetch "counter", and send each VERB
|
||||
(incr, decr, value; default: incr incr decr), printing each answer.
|
||||
Open a CapTP session to the canister, fetch "counter", and send it each
|
||||
VERB (incr, decr, value; default: incr incr decr) without waiting for the
|
||||
fetch, then print each answer.
|
||||
HOST is the replica, e.g. http://127.0.0.1:4959, or https://icp-api.io.
|
||||
A fresh Ed25519 identity makes each run its own caller, so its own session.`;
|
||||
|
||||
@@ -51,11 +52,12 @@ const main = async ([host, canisterId, ...verbs]) => {
|
||||
|
||||
const peer = await openPeer({ module, seed, carry }, canisterId);
|
||||
console.log('session open');
|
||||
await peer.fetch('counter');
|
||||
console.log('fetched counter');
|
||||
for (const verb of verbs.length ? verbs : ['incr', 'incr', 'decr']) {
|
||||
console.log(verb, await peer.send(verb));
|
||||
}
|
||||
// Pipelined: the sends target the fetch's answer before it settles, so
|
||||
// the fetch and every send cross in one update call.
|
||||
const counter = peer.fetch('counter');
|
||||
const answers = (verbs.length ? verbs : ['incr', 'incr', 'decr']).map(verb => [verb, peer.send(counter, verb)]);
|
||||
await peer.flush();
|
||||
for (const [verb, answer] of answers) console.log(verb, peer.valueOf(answer));
|
||||
};
|
||||
|
||||
main(process.argv.slice(2)).catch(err => {
|
||||
|
||||
Generated
+94
@@ -7,6 +7,9 @@
|
||||
"name": "icp-ocapn-counter-client",
|
||||
"dependencies": {
|
||||
"@icp-sdk/core": "6.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@icp-sdk/icp-cli": "1.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@dfinity/cbor": {
|
||||
@@ -29,6 +32,97 @@
|
||||
"asn1js": "^3.0.7"
|
||||
}
|
||||
},
|
||||
"node_modules/@icp-sdk/icp-cli": {
|
||||
"version": "1.8.0",
|
||||
"resolved": "https://registry.npmjs.org/@icp-sdk/icp-cli/-/icp-cli-1.8.0.tgz",
|
||||
"integrity": "sha512-5wdTxakBoYKqCVvApwlEgZZat9Y0qPxvrPVO31eAem2IO3+Xu3gn/k4EAHb9K9DTfCKyJ+Ll2O8Nm5GEPUldpQ==",
|
||||
"dev": true,
|
||||
"hasInstallScript": true,
|
||||
"license": "Apache-2.0",
|
||||
"bin": {
|
||||
"icp": "bin/icp.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"@icp-sdk/icp-cli-darwin-arm64": "1.8.0",
|
||||
"@icp-sdk/icp-cli-darwin-x64": "1.8.0",
|
||||
"@icp-sdk/icp-cli-linux-arm64": "1.8.0",
|
||||
"@icp-sdk/icp-cli-linux-x64": "1.8.0",
|
||||
"@icp-sdk/icp-cli-win32-x64": "1.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@icp-sdk/icp-cli-darwin-arm64": {
|
||||
"version": "1.8.0",
|
||||
"resolved": "https://registry.npmjs.org/@icp-sdk/icp-cli-darwin-arm64/-/icp-cli-darwin-arm64-1.8.0.tgz",
|
||||
"integrity": "sha512-4RO/r1p1h74wuePwat0vMPfYagzFO2CiYmAR33ZnrPfo22aGVcdwldpePZYBV8egjq7BMnI2LPY3UVLVHnSing==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
]
|
||||
},
|
||||
"node_modules/@icp-sdk/icp-cli-darwin-x64": {
|
||||
"version": "1.8.0",
|
||||
"resolved": "https://registry.npmjs.org/@icp-sdk/icp-cli-darwin-x64/-/icp-cli-darwin-x64-1.8.0.tgz",
|
||||
"integrity": "sha512-+/MDaDmleZ43kY14dvMHGxxSIW7Z+brccSqy6Vr+CwvRWqJcDG6QzwYXqk+owgLcmTrF11vufMhZbWN7n/yv0g==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
]
|
||||
},
|
||||
"node_modules/@icp-sdk/icp-cli-linux-arm64": {
|
||||
"version": "1.8.0",
|
||||
"resolved": "https://registry.npmjs.org/@icp-sdk/icp-cli-linux-arm64/-/icp-cli-linux-arm64-1.8.0.tgz",
|
||||
"integrity": "sha512-E/unBg0Kh8jfdf9IWVdbTP5FeMMtnQSDo1Cw+qb1WfX0lYYsFndS6bLW4KujKloETPKBwUPrH9gGc2AjDDhlKQ==",
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
]
|
||||
},
|
||||
"node_modules/@icp-sdk/icp-cli-linux-x64": {
|
||||
"version": "1.8.0",
|
||||
"resolved": "https://registry.npmjs.org/@icp-sdk/icp-cli-linux-x64/-/icp-cli-linux-x64-1.8.0.tgz",
|
||||
"integrity": "sha512-f6NSdFIrr2eSmNe0JjQWedaiRkJWqCTbaS6GjZFbPtRnd52Eb+Ichrom2zmx/4CZ/DW71BfU9iE8D+OFTDsyow==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
]
|
||||
},
|
||||
"node_modules/@icp-sdk/icp-cli-win32-x64": {
|
||||
"version": "1.8.0",
|
||||
"resolved": "https://registry.npmjs.org/@icp-sdk/icp-cli-win32-x64/-/icp-cli-win32-x64-1.8.0.tgz",
|
||||
"integrity": "sha512-4x3JZwLljFNK67c3ClYDbluE2Lw98q5pE+Jomn0wq8MqGV/UqtQHdKeWqB73NWdeaeuGar3Hl15i81gxJJXAGg==",
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"license": "Apache-2.0",
|
||||
"optional": true,
|
||||
"os": [
|
||||
"win32"
|
||||
]
|
||||
},
|
||||
"node_modules/@noble/curves": {
|
||||
"version": "2.4.0",
|
||||
"resolved": "https://registry.npmjs.org/@noble/curves/-/curves-2.4.0.tgz",
|
||||
|
||||
@@ -4,5 +4,8 @@
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@icp-sdk/core": "6.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@icp-sdk/icp-cli": "1.8.0"
|
||||
}
|
||||
}
|
||||
+30
-18
@@ -65,27 +65,39 @@ export const openPeer = async ({ module, seed, carry }, canisterId) => {
|
||||
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 () => {
|
||||
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();
|
||||
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;
|
||||
};
|
||||
/** 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({
|
||||
/** 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());
|
||||
},
|
||||
/** @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),
|
||||
});
|
||||
};
|
||||
Reference in new issue
Block a user