From 80c48b3a1cc0cb7a531747e6e77427b1f1495244 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 5 Aug 2022 09:54:39 -0400 Subject: [PATCH] *Return error if hash is a directory since we don't want to download it all --- src/error.ts | 1 + src/rpc/ipfs.ts | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/error.ts b/src/error.ts index 4fb86bb..cc310fc 100644 --- a/src/error.ts +++ b/src/error.ts @@ -9,3 +9,4 @@ export const ERR_NOT_READY = "NOT_READY"; export const ERR_INVALID_CHAIN = "INVALID_CHAIN"; export const ERR_ENDPOINT_INVALID = "ENDPOINT_INVALID"; export const ERR_METHOD_INVALID = "METHOD_INVALID"; +export const ERR_HASH_IS_DIRECTORY = "HASH_IS_DIRECTORY"; diff --git a/src/rpc/ipfs.ts b/src/rpc/ipfs.ts index 5a90457..c5573d3 100644 --- a/src/rpc/ipfs.ts +++ b/src/rpc/ipfs.ts @@ -16,8 +16,8 @@ import last from "it-last"; // @ts-ignore import toStream from "it-to-stream"; import { addStream } from "../streams.js"; -import { ERR_INVALID_CHAIN } from "../error.js"; import { bases } from "multiformats/basics"; +import { ERR_HASH_IS_DIRECTORY } from "../error.js"; let client: IPFS | Promise; let resolver: typeof import("ipfs-http-response").resolver; @@ -134,6 +134,10 @@ async function fetchFile(hash?: string, path?: string, fullPath?: string) { return data; } + if (data?.type === "directory") { + return rpcError(ERR_HASH_IS_DIRECTORY); + } + const streamId = addStream(data.content()); return { streamId }; @@ -228,14 +232,14 @@ async function resolveIpns(hash: string, path: string): Promise { const CHAIN = "ipfs"; export default { - stat_ipfs: validateChain(CHAIN, async (args: any, context: object) => { + stat_ipfs: validateChain(CHAIN, async (args: any) => { try { return await statFile(args?.hash, args?.path); } catch (e: any) { return rpcError((e as Error).message); } }), - stat_ipns: validateChain(CHAIN, async (args: any, context: object) => { + stat_ipns: validateChain(CHAIN, async (args: any) => { let ipfsPath; try { @@ -247,7 +251,7 @@ export default { } }), - fetch_ipfs: validateChain(CHAIN, async (args: any, context: object) => { + fetch_ipfs: validateChain(CHAIN, async (args: any) => { try { const ret = await fetchFile(args?.hash, args?.path); if (ret instanceof Error) { @@ -259,7 +263,7 @@ export default { return rpcError((e as Error).message); } }), - fetch_ipns: validateChain(CHAIN, async (args: any, context: object) => { + fetch_ipns: validateChain(CHAIN, async (args: any) => { let ipfsPath; try { ipfsPath = await resolveIpns(args.hash, args.path);