*Return error if hash is a directory since we don't want to download it all

This commit is contained in:
Derrick Hammer 2022-08-05 09:54:39 -04:00
parent 33fa36411b
commit 80c48b3a1c
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 10 additions and 5 deletions

View File

@ -9,3 +9,4 @@ export const ERR_NOT_READY = "NOT_READY";
export const ERR_INVALID_CHAIN = "INVALID_CHAIN"; export const ERR_INVALID_CHAIN = "INVALID_CHAIN";
export const ERR_ENDPOINT_INVALID = "ENDPOINT_INVALID"; export const ERR_ENDPOINT_INVALID = "ENDPOINT_INVALID";
export const ERR_METHOD_INVALID = "METHOD_INVALID"; export const ERR_METHOD_INVALID = "METHOD_INVALID";
export const ERR_HASH_IS_DIRECTORY = "HASH_IS_DIRECTORY";

View File

@ -16,8 +16,8 @@ import last from "it-last";
// @ts-ignore // @ts-ignore
import toStream from "it-to-stream"; import toStream from "it-to-stream";
import { addStream } from "../streams.js"; import { addStream } from "../streams.js";
import { ERR_INVALID_CHAIN } from "../error.js";
import { bases } from "multiformats/basics"; import { bases } from "multiformats/basics";
import { ERR_HASH_IS_DIRECTORY } from "../error.js";
let client: IPFS | Promise<any>; let client: IPFS | Promise<any>;
let resolver: typeof import("ipfs-http-response").resolver; let resolver: typeof import("ipfs-http-response").resolver;
@ -134,6 +134,10 @@ async function fetchFile(hash?: string, path?: string, fullPath?: string) {
return data; return data;
} }
if (data?.type === "directory") {
return rpcError(ERR_HASH_IS_DIRECTORY);
}
const streamId = addStream(data.content()); const streamId = addStream(data.content());
return { streamId }; return { streamId };
@ -228,14 +232,14 @@ async function resolveIpns(hash: string, path: string): Promise<string> {
const CHAIN = "ipfs"; const CHAIN = "ipfs";
export default { export default {
stat_ipfs: validateChain(CHAIN, async (args: any, context: object) => { stat_ipfs: validateChain(CHAIN, async (args: any) => {
try { try {
return await statFile(args?.hash, args?.path); return await statFile(args?.hash, args?.path);
} catch (e: any) { } catch (e: any) {
return rpcError((e as Error).message); return rpcError((e as Error).message);
} }
}), }),
stat_ipns: validateChain(CHAIN, async (args: any, context: object) => { stat_ipns: validateChain(CHAIN, async (args: any) => {
let ipfsPath; let ipfsPath;
try { try {
@ -247,7 +251,7 @@ export default {
} }
}), }),
fetch_ipfs: validateChain(CHAIN, async (args: any, context: object) => { fetch_ipfs: validateChain(CHAIN, async (args: any) => {
try { try {
const ret = await fetchFile(args?.hash, args?.path); const ret = await fetchFile(args?.hash, args?.path);
if (ret instanceof Error) { if (ret instanceof Error) {
@ -259,7 +263,7 @@ export default {
return rpcError((e as Error).message); return rpcError((e as Error).message);
} }
}), }),
fetch_ipns: validateChain(CHAIN, async (args: any, context: object) => { fetch_ipns: validateChain(CHAIN, async (args: any) => {
let ipfsPath; let ipfsPath;
try { try {
ipfsPath = await resolveIpns(args.hash, args.path); ipfsPath = await resolveIpns(args.hash, args.path);