From e6d43b7ae3c9baacfeae37ef9637d1351cbdceec Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 16 Jan 2019 12:46:22 +0000 Subject: [PATCH] fix: upgrade for 0.34 JS IPFS v0.34 is coming out soon. It has breaking changes. This PR addresses those issues. I've tested this against v0.34.0-rc.1 in the [browser video streaming example](https://github.com/ipfs/js-ipfs/tree/master/examples/browser-video-streaming) and it all works. This PR also improves the code by actually streaming the file using `catReadableStream` over `cat` (which buffers content in memory). License: MIT Signed-off-by: Alan Shaw --- package.json | 3 +-- src/index.js | 46 ++++++++++++++++------------------------------ 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 1e84434..48137c6 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,6 @@ "homepage": "https://github.com/moshisushi/hlsjs-ipfs-loader#readme", "dependencies": { "aegir": "^11.0.2", - "gulp": "^3.9.1", - "lodash": "^4.17.4" + "gulp": "^3.9.1" } } diff --git a/src/index.js b/src/index.js index 5574951..c3fc872 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,5 @@ 'use strict' -const _ = require('lodash') -const Duplex = require('stream').Duplex - class HlsjsIPFSLoader { constructor(config) { this.ipfs = config.ipfs @@ -67,9 +64,9 @@ function getFile(ipfs, rootHash, filename, callback) { var hash = null var fileSize, fileName - _.each(res.links, function(link) { + res.links.forEach(function(link) { if (link.name === filename) { - hash = link.multihash + hash = link.cid.toString() fileSize = link.size fileName = link.name return false @@ -87,24 +84,20 @@ function getFile(ipfs, rootHash, filename, callback) { var bufView = new Uint8Array(resBuf) var offs = 0 - ipfs.files.cat(hash, function (err, stream) { - console.log("Received stream for file '" + rootHash + "/" + - fileName + "'") - if (err) return callback(err) - stream = buf2Stream(stream) - stream.on('data', function (chunk) { - console.log("Received " + chunk.length + " bytes for file '" + - rootHash + "/" + fileName + "'") - bufView.set(chunk, offs) - offs += chunk.length - }); - stream.on('error', function (err) { - callback(err, null) - }); - stream.on('end', function () { - callback(null, resBuf) - }); - }) + const stream = ipfs.catReadableStream(hash) + console.log("Received stream for file '" + rootHash + "/" + fileName + "'") + stream.on('data', function (chunk) { + console.log("Received " + chunk.length + " bytes for file '" + + rootHash + "/" + fileName + "'") + bufView.set(chunk, offs) + offs += chunk.length + }); + stream.on('error', function (err) { + callback(err, null) + }); + stream.on('end', function () { + callback(null, resBuf) + }); }); } @@ -112,11 +105,4 @@ function buf2str(buf) { return String.fromCharCode.apply(null, new Uint8Array(buf)) } -function buf2Stream(buffer) { - let stream = new Duplex() - stream.push(buffer) - stream.push(null) - return stream -} - exports = module.exports = HlsjsIPFSLoader