Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use RPC getters to retrieve IPFS content #250

Open
rmlearney-digicatapult opened this issue Nov 13, 2024 · 5 comments
Open

Unable to use RPC getters to retrieve IPFS content #250

rmlearney-digicatapult opened this issue Nov 13, 2024 · 5 comments
Labels
dif/trivial Can be confidently tackled by newcomers good first issue Good issue for new contributors help wanted Seeking public contribution on this issue kind/enhancement A net-new feature or improvement to an existing feature kind/support This is a question or a request for support P2 Medium: Good to have, but can wait until someone steps up status/ready Ready to be worked topic/docs Improvements or additions to documentation

Comments

@rmlearney-digicatapult
Copy link

Have looked for examples and code snippets but can't figure out how to programmatically retrieve data from IPFS using .get methods. Have had to revert to HTTP fetch with node-fetch or axios instead.

E.g.

import { create } from 'kubo-rpc-client'
var { cid } = await client.add('Hello world!')
// undefined
await client.get( cid  )
// Object [AsyncGenerator] {}

These methods would seem to be in the RPC but am I just using them incorrectly?

@SgtPooki SgtPooki added the kind/support This is a question or a request for support label Nov 14, 2024
@SgtPooki
Copy link
Member

SgtPooki commented Nov 14, 2024

If you are creating a kubo-rpc-client and have a background Kubo running via a separate process, you should be able to do it like this:

import { create } from 'kubo-rpc-client'

const client = create()

const result = await client.add('hello world', { cidVersion: 1 })
console.log(result)

/**
{
  path: 'bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e',
  cid: CID(bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e),
  size: 11
}
 */

const textDecoder = new TextDecoder()
for await (const content of client.cat(result.cid)) {
  console.log(textDecoder.decode(content)) // 'hello world'
}

If you do not have a background Kubo process running, you will get something like this:

TypeError: fetch failed
    at node:internal/deps/undici/undici:12345:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Client.fetch (file:///Users/sgtpooki/code/work/ipshipyard/ipfs/service-worker-gateway/node_modules/kubo-rpc-client/dist/src/lib/http.js:67:30)
    at async addAll (file:///Users/sgtpooki/code/work/ipshipyard/ipfs/service-worker-gateway/node_modules/kubo-rpc-client/dist/src/add-all.js:21:25)
    at async file:///Users/sgtpooki/code/work/ipshipyard/ipfs/service-worker-gateway/node_modules/it-last/dist/src/index.js:40:30
    at async KuboRPCClient.add (file:///Users/sgtpooki/code/work/ipshipyard/ipfs/service-worker-gateway/node_modules/kubo-rpc-client/dist/src/add.js:9:24) {
  cause: Error: connect ECONNREFUSED 127.0.0.1:5001
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
    errno: -61,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '127.0.0.1',
    port: 5001
  }
}

If you need to manage your kubo instance with javascript, check out https://github.com/ipfs/js-ipfsd-ctl

@SgtPooki SgtPooki added need/author-input Needs input from the original author and removed kind/support This is a question or a request for support labels Nov 14, 2024
@rmlearney-digicatapult
Copy link
Author

rmlearney-digicatapult commented Nov 14, 2024

Thanks @SgtPooki but your example is for posting content and recieving the CID.

How do I then programmatically retrieve that original content given the CID when using the RPC client?

@SgtPooki
Copy link
Member

@rmlearney-digicatapult there is an example of getting the content there. Maybe you looked before my edit?

Basically, you get the content with .cat or .get. Both return an async iterable.

If your question still isn't answered, I'm not sure I understand what you're trying to do.

@rmlearney-digicatapult
Copy link
Author

rmlearney-digicatapult commented Nov 14, 2024

My apologies @SgtPooki I must have commented before your edit.

Thanks so much!

Would it be possible to add this simple example to the docs for future users?

@SgtPooki
Copy link
Member

Would it be possible to add this simple example to the docs for future users?

Sure. I'll mark this as a doc enhancement issue

@SgtPooki SgtPooki added P2 Medium: Good to have, but can wait until someone steps up dif/trivial Can be confidently tackled by newcomers good first issue Good issue for new contributors help wanted Seeking public contribution on this issue kind/enhancement A net-new feature or improvement to an existing feature kind/support This is a question or a request for support status/ready Ready to be worked topic/docs Improvements or additions to documentation and removed need/author-input Needs input from the original author labels Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dif/trivial Can be confidently tackled by newcomers good first issue Good issue for new contributors help wanted Seeking public contribution on this issue kind/enhancement A net-new feature or improvement to an existing feature kind/support This is a question or a request for support P2 Medium: Good to have, but can wait until someone steps up status/ready Ready to be worked topic/docs Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants