feat: add cat api

This commit is contained in:
Derrick Hammer 2023-11-17 08:57:15 -05:00
parent 95128891e1
commit 024cb507d1
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 19 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import {
} from "@lumeweb/kernel-swarm-client";
import Protomux from "@lumeweb/kernel-protomux-client";
import {
CID,
CID_HASH_TYPES,
createKeyPair,
createNode,
@ -37,6 +38,7 @@ addHandler("setRegistryEntry", handleSetRegistryEntry);
addHandler("registrySubscription", handleRegistrySubscription, {
receiveUpdates: true,
});
addHandler("cat", handleCat);
async function handlePresentKey(aq: ActiveQuery) {
handlePresentKeyModule({
@ -178,3 +180,20 @@ async function handleRegistrySubscription(aq: ActiveQuery) {
await wait.promise;
aq.respond();
}
async function handleCat(aq: ActiveQuery) {
if (!("cid" in aq.callerInput)) {
aq.reject("cid required");
return;
}
try {
const ret = await node.downloadBytesByHash(
CID.decode(aq.callerInput.cid as string).hash,
);
aq.respond(ret);
} catch (e) {
aq.reject(e);
}
}