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

Commit

Permalink
fix: block.put options (#844)
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
Alan Shaw authored Sep 20, 2018
1 parent e505098 commit e290a38
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"eslint-plugin-react": "^7.10.0",
"go-ipfs-dep": "~0.4.17",
"gulp": "^3.9.1",
"interface-ipfs-core": "~0.75.1",
"interface-ipfs-core": "~0.78.0",
"ipfsd-ctl": "~0.39.0",
"pull-stream": "^3.6.8",
"socket.io": "^2.1.1",
Expand Down
61 changes: 49 additions & 12 deletions src/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,72 @@
const promisify = require('promisify-es6')
const Block = require('ipfs-block')
const CID = require('cids')
const once = require('once')
const multihash = require('multihashes')
const SendOneFile = require('../utils/send-one-file')

module.exports = (send) => {
const sendOneFile = SendOneFile(send, 'block/put')

return promisify((block, cid, _callback) => {
// TODO this needs to be adjusted with the new go-ipfs http-api
if (typeof cid === 'function') {
_callback = cid
cid = {}
return promisify((block, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}

const callback = once(_callback)
options = options || {}

if (Array.isArray(block)) {
return callback(new Error('block.put accepts only one block'))
}

if (typeof block === 'object' && block.data) {
block = block.data
if (Buffer.isBuffer(block)) {
block = { data: block }
}

sendOneFile(block, {}, (err, result) => {
if (!block || !block.data) {
return callback(new Error('invalid block arg'))
}

const qs = {}

if (block.cid || options.cid) {
let cid

try {
cid = new CID(block.cid || options.cid)
} catch (err) {
return callback(err)
}

const { name, length } = multihash.decode(cid.multihash)

qs.format = cid.codec
qs.mhtype = name
qs.mhlen = length
qs.version = cid.version
} else {
if (options.format) qs.format = options.format
if (options.mhtype) qs.mhtype = options.mhtype
if (options.mhlen) qs.mhlen = options.mhlen
if (options.version != null) qs.version = options.version
}

sendOneFile(block.data, { qs }, (err, result) => {
if (err) {
return callback(err) // early
// Retry with "protobuf" format for go-ipfs
// TODO: remove when https://github.com/ipfs/go-cid/issues/75 resolved
if (qs.format === 'dag-pb') {
qs.format = 'protobuf'
return sendOneFile(block.data, { qs }, (err, result) => {
if (err) return callback(err)
callback(null, new Block(block.data, new CID(result.Key)))
})
}

return callback(err)
}

callback(null, new Block(block, new CID(result.Key)))
callback(null, new Block(block.data, new CID(result.Key)))
})
})
}
5 changes: 5 additions & 0 deletions test/interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ describe('interface-ipfs-core tests', () => {
isNode ? null : {
name: 'should get a directory',
reason: 'FIXME https://github.com/ipfs/js-ipfs-api/issues/339'
},
// files.write
{
name: 'should write to deeply nested non existent file with create and parents flags',
reason: 'TODO remove when 0.4.18 is released https://github.com/ipfs/go-ipfs/pull/5359'
}
]
})
Expand Down

0 comments on commit e290a38

Please sign in to comment.