browser_ipfs_service.ts
1 import { createNode } from "./helia_node" 2 import { unixfs } from '@helia/unixfs' 3 import { CID } from 'multiformats/cid'; 4 import { createVerifiedFetch, verifiedFetch } from '@helia/verified-fetch' 5 6 class BrowserIPFSService { 7 8 node() { 9 return createNode() 10 } 11 12 async getContentIPNS(name: string) { 13 const response = await verifiedFetch('ipns://' + name) 14 return response 15 16 } 17 18 async getContent(cid: string) { 19 const response = await verifiedFetch('ipfs://' + cid) 20 console.log('got res') 21 return response 22 } 23 24 // async getContent(cidStr: string) { 25 // const node = await this.node(); 26 // const fs = unixfs(node) 27 28 // const cid = CID.parse(cidStr); 29 30 // return new Promise(async (res, rej) => { 31 // const chunks = []; 32 // console.log("waiting for chunks") 33 // for await (const chunk of fs.cat(cid)) { 34 // chunks.push(chunk); 35 // } 36 37 // console.log({ chunks }) 38 39 // // 5. Combine the chunks into a single Uint8Array 40 // const combinedChunks = new Uint8Array(chunks.reduce((acc, chunk) => acc.concat(Array.from(chunk)), [])); 41 42 // // 6. Decode the data into a string (assuming it's a text file) 43 // const content = new TextDecoder().decode(combinedChunks); 44 // res(content) 45 // }) 46 // } 47 } 48 49 export { BrowserIPFSService }