0

我创建了这个测试用例来证明 cat 方法不适用于我使用 IPFS javascript 库。我究竟做错了什么 ?我的控制台输出没有从“node.files.cat”函数中提取任何内容,就好像根本没有调用 (err,filestream) 回调。我知道我的多重哈希有点工作,因为如果我改变它,我会得到一个致命的错误。但是现在它似乎只是在节点就绪后锁定和暂停。

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')
})
4

1 回答 1

0

这看起来像一个错误。解决它的一种快速方法是将 node.files.cat 放在 .on('ready') 的回调中。似乎 bitswap 在节点联机之前丢弃请求。

让我知道这个是否奏效。

于 2017-09-10T03:27:43.560 回答