From f6a58ad4b6a98efb94702f93db5de37baf63a85e Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 6 Aug 2022 10:10:02 -0400 Subject: [PATCH] *heavy simplification to not use in memory stores and use default on-disk repo --- src/rpc/ipfs.ts | 47 ++++++++++++++--------------------------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/src/rpc/ipfs.ts b/src/rpc/ipfs.ts index 191c1b2..77a37a0 100644 --- a/src/rpc/ipfs.ts +++ b/src/rpc/ipfs.ts @@ -1,23 +1,14 @@ import { rpcError, RpcMethodList, validateChain } from "./index.js"; import type { IPFS } from "ipfs-core"; -import type { UnixFSEntry } from "ipfs-core/dist/src/utils"; import { dynImport } from "../util.js"; -import { exporter } from "ipfs-unixfs-exporter"; -// @ts-ignore import { CID } from "multiformats/cid"; -import { MemoryDatastore } from "datastore-core"; -import { MemoryBlockstore } from "blockstore-core"; -import { createRepo } from "ipfs-repo"; -// @ts-ignore -import { MemoryLock } from "ipfs-repo/locks/memory"; -// @ts-ignore -import * as rawCodec from "multiformats/codecs/raw"; import last from "it-last"; // @ts-ignore import toStream from "it-to-stream"; import { addStream } from "../streams.js"; import { bases } from "multiformats/basics"; import { ERR_HASH_IS_DIRECTORY } from "../error.js"; +import type { StatResult } from "ipfs-core/dist/src/components/files/stat"; let client: IPFS | Promise; let resolver: typeof import("ipfs-http-response").resolver; @@ -25,19 +16,6 @@ let utils: typeof import("ipfs-http-response").utils; let detectContentType: typeof import("ipfs-http-response").utils.detectContentType; let normalizeCidPath: typeof import("ipfs-core/dist/src/utils.js").normalizeCidPath; -const repo = createRepo( - "", - async () => rawCodec, - { - blocks: new MemoryBlockstore(), - datastore: new MemoryDatastore(), - keys: new MemoryDatastore(), - pins: new MemoryDatastore(), - root: new MemoryDatastore(), - }, - { autoMigrate: false, repoLock: MemoryLock, repoOwner: true } -); - interface StatFileResponse { exists: boolean; contentType: string | null; @@ -76,7 +54,6 @@ async function initIpfs() { client = IPFS.create({ // relay: { hop: { enabled: false } }, silent: true, - repo, }); client = await client; } @@ -109,7 +86,9 @@ async function fetchFile(hash?: string, path?: string, fullPath?: string) { return rpcError(ERR_HASH_IS_DIRECTORY); } - const streamId = addStream(data.content()); + client = client as IPFS; + + const streamId = addStream(client.cat(data.cid)); return { streamId }; } @@ -143,12 +122,12 @@ async function statFile(hash?: string, path?: string, fullPath?: string) { if (exists?.type === "directory") { stats.directory = true; - stats.files = exists.node.Links.map((item) => { - return { - name: item.Name, - size: item.Tsize, - } as StatFileSubfile; - }); + for await (const item of client.ls(exists.cid)) { + stats.files.push({ + name: item.name, + size: item.size, + } as StatFileSubfile); + } return stats; } @@ -168,14 +147,16 @@ async function fileExists( hash?: string, path?: string, fullPath?: string -): Promise { +): Promise { await initIpfs(); client = client as IPFS; let ipfsPath = normalizePath(hash, path, fullPath); try { const controller = new AbortController(); setTimeout(() => controller.abort(), 5000); - return await exporter(ipfsPath, repo.blocks, { signal: controller.signal }); + return client.files.stat(`/ipfs/${ipfsPath}`, { + signal: controller.signal, + }); } catch (err: any) { return err; }