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