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

fix: upgrade for 0.34 #7

Merged
merged 1 commit into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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