IPFS in Javascript 'cat' function doesn't work

758 views Asked by At

I created this testcase to prove that the cat method is not working for me using the IPFS javascript library. What am I doing wrong ? My console output does not draw anything from within the 'node.files.cat' function, its as if that (err,filestream) callback is not being called at all. I I know my multihash is somewhat working because if I change it I get a fatal error. However right now it is seemingly just locking up and pausing after NODE READY.

const IPFS = require('ipfs')
const path = require('path')
const os = require('os')
const fs = require('fs')

console.log('ipfs test ')


var mhash = "Qmc5LfkMVAhvzip2u2RjRBRhgVthtSokHsz4Y5bgaBCW2R";


// Create the IPFS node instance
const node = new IPFS()

node.on('ready', () => {

  // Your node is now ready to use \o/


    console.log('NODE READY')



/*
THIS WORKS
    var test_rstream = fs.createReadStream( path.join(__dirname, '.', '/public/sample_land_file.json') )
    var wstream =  fs.createWriteStream(os.tmpdir() + '/lobc_cache/'+'Qmc5LfkMVAhvzip2u2RjRBRhgVthtSokHsz4Y5bgaBCW2R');


      wstream.on('finish', function() {
       console.log('Written ' + wstream.bytesWritten + ' ' + wstream.path);
         test_rstream.close()
      });

       test_rstream.pipe(wstream);

*/


    node.files.cat("Qmc5LfkMVAhvzip2u2RjRBRhgVthtSokHsz4Y5bgaBCW2R", function (err, filestream) {

          console.log('WHY ISNT THIS FIRING ') // i never see this logged
        console.log(filestream)

        console.log(os.tmpdir())


        if (!fs.existsSync(os.tmpdir() + '/lobc_cache')){
          fs.mkdirSync(os.tmpdir() + '/lobc_cache');
        }

        var wstream =  fs.createWriteStream(os.tmpdir() + '/lobc_cache/'+'Qmc5LfkMVAhvzip2u2RjRBRhgVthtSokHsz4Y5bgaBCW2R');



        result   = '';


          wstream.on('finish', function() {
           console.log('Written ' + wstream.bytesWritten + ' ' + wstream.path);
             filestream.close()
          });


                filestream.pipe(wstream);

              //  wstream.end();
  // file will be a stream containing the data of the file requested
    })





  // stopping a node
  node.stop(() => {
    // node is now 'offline'
  })
})


node.on('start', () => {

  console.log('NODE START')
})
1

There are 1 answers

0
David Dias On

This looks like a bug. A quick way to solve it is just to put the node.files.cat inside the callback for .on('ready'). Seems that bitswap is dropping requests before the node is online.

Let me know if this works.