Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init ipfs-api or js-ipfs #320

Merged
merged 20 commits into from
Dec 12, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix stream error. Disable mimeType check
  • Loading branch information
olizilla committed Nov 30, 2017
commit e0e17d88f7e47c30b75e1eca11d726c7bc5075ed
21 changes: 16 additions & 5 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ try {
onclick: copyAddressAtPublicGw
})
} catch (err) {
console.log('[ipfs-companion] Error creating contextMenus', err)
console.debug('[ipfs-companion] Error creating contextMenus')
}

function inFirefox () {
Expand Down Expand Up @@ -255,7 +255,7 @@ async function addFromURL (info) {
const reader = new FileReader()
reader.onloadend = () => {
const buffer = Buffer.from(reader.result)
ipfs.add(buffer, uploadResultHandler)
ipfs.files.add(buffer, uploadResultHandler)
}
reader.readAsArrayBuffer(await response.blob())
} else {
Expand All @@ -275,6 +275,17 @@ async function addFromURL (info) {
}
}

// TODO: feature detect and push to client type specific modules.
function getIpfsPathAndLocalAddress (hash) {
const path = `/ipfs/${hash}`
if (state.ipfsNodeType === 'embedded' && chrome.protocol.registerStringProtocol) {
return {path, localAddress: `ipfs://${hash}`}
} else {
const url = new URL(path, state.gwURLString).toString()
return {path, localAddress: url}
}
}

function uploadResultHandler (err, result) {
if (err || !result) {
console.error('[ipfs-companion] ipfs add error', err, result)
Expand All @@ -283,9 +294,9 @@ function uploadResultHandler (err, result) {
}
result.forEach(function (file) {
if (file && file.hash) {
const path = `/ipfs/${file.hash}`
const {path, localAddress} = getIpfsPathAndLocalAddress(file.hash)
browser.tabs.create({
'url': new URL(state.gwURLString + path).toString()
'url': localAddress
})
console.info('[ipfs-companion] successfully stored', path)
if (state.preloadAtPublicGateway) {
Expand Down Expand Up @@ -383,7 +394,7 @@ async function updateContextMenus (changedTabId) {
}
}
} catch (err) {
// console.debug('[ipfs-companion] Error updating context menus', err)
console.debug('[ipfs-companion] Error updating context menus')
}
}

Expand Down
59 changes: 23 additions & 36 deletions add-on/src/lib/ipfs-protocol.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const identifyBuffer = require('buffer-signature').identify
const bl = require('bl')
const identifyStream = require('buffer-signature').identifyStream

exports.createIpfsUrlProtocolHandler = (getIpfs) => {
return async (request, reply) => {
Expand All @@ -7,47 +8,33 @@ exports.createIpfsUrlProtocolHandler = (getIpfs) => {

const path = request.url.split('ipfs://')[1]
const ipfs = getIpfs()
const { mimeType, data, charset } = await catFromIpfs(ipfs, path)

console.log(`[ipfs-companion] returning ${path} as ${mimeType} ${charset}`)
console.timeEnd('[ipfs-companion] IpfsUrlProtocolHandler')
reply({ mimeType, data, charset })
}
}
try {
// TODO: disable mime type detection for now
const mimeType = 'text/plain'
const {data} = await getDataAndGuessMimeType(ipfs, path)
console.log(`[ipfs-companion] returning ${path} as ${mimeType}`)
reply({mimeType, data})
} catch (err) {
reply({mimeType: 'text/html', data: `Error ${err.message}`})
}

async function catFromIpfs (ipfs, path) {
try {
const stream = await ipfs.files.cat(path)
console.debug(`[ipfs-companion] ipfs.files.cat returned a stream for ${path}`)

const res = await getMimeTypeAndData(stream)
console.debug(`[ipfs-companion] mimeType guessed as ${res.mimeType} for ${path}`)

return res
} catch (err) {
console.log('Error', err)
return { mimeType: 'text/html', data: `Error ${err.message}` }
console.timeEnd('[ipfs-companion] IpfsUrlProtocolHandler')
}
}

async function getMimeTypeAndData (stream) {
let buffs = []

stream.on('data', (buff) => {
console.debug(`[ipfs-companion] data event from stream`)
buffs.push(buff)
})

function getDataAndGuessMimeType (ipfs, path) {
return new Promise((resolve, reject) => {
stream.on('error', (err) => reject(err))

stream.on('end', () => {
console.debug(`[ipfs-companion] end event from stream`)
const data = Buffer.concat(buffs)
const mimeType = identifyBuffer(data).mimeType
resolve({mimeType, data: data.toString('utf8'), charset: 'utf8'})
ipfs.files.cat(path, (err, stream) => {
if (err) return reject(err)

let mimeType = null
stream
.pipe(identifyStream(info => { mimeType = info.mimeType }))
.pipe(bl((err, data) => {
if (err) return reject(err)
resolve({mimeType, data: data.toString('utf8')})
}))
})
})
}

exports.getMimeTypeAndData = getMimeTypeAndData
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"web-ext": "2.2.2"
},
"dependencies": {
"bl": "1.2.1",
"buffer-signature": "1.0.0",
"choo": "6.6.0",
"ipfs": "0.26.0",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ bip66@^1.1.3:
dependencies:
safe-buffer "^5.0.1"

bl@^1.0.0, bl@^1.2.1:
bl@1.2.1, bl@^1.0.0, bl@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/bl/-/bl-1.2.1.tgz#cac328f7bee45730d404b692203fcb590e172d5e"
dependencies:
Expand Down