Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from alanshaw/fix/upgrade-for-0.34
Browse files Browse the repository at this point in the history
fix: upgrade for 0.34
  • Loading branch information
Henry Rodrick authored Jan 18, 2019
2 parents f0df3ff + e6d43b7 commit 4a9f4d5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 32 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
46 changes: 16 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict'

const _ = require('lodash')
const Duplex = require('stream').Duplex

class HlsjsIPFSLoader {
constructor(config) {
this.ipfs = config.ipfs
Expand Down Expand Up @@ -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
Expand All @@ -87,36 +84,25 @@ 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)
});
});
}

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

0 comments on commit 4a9f4d5

Please sign in to comment.