From 024cb507d14ca9cf722dbebc428e56b5ea63cbac Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 17 Nov 2023 08:57:15 -0500 Subject: [PATCH] feat: add cat api --- src/index.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/index.ts b/src/index.ts index 8c26354..f391d15 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); + } +}