This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dag): rebase, use waterfall for put
- Loading branch information
Showing
5 changed files
with
142 additions
and
127 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict' | ||
|
||
const dagPB = require('ipld-dag-pb') | ||
const dagCBOR = require('ipld-dag-cbor') | ||
const promisify = require('promisify-es6') | ||
const CID = require('cids') | ||
const waterfall = require('async/waterfall') | ||
const block = require('../block') | ||
|
||
module.exports = (send) => { | ||
return promisify((cid, path, options, callback) => { | ||
if (typeof path === 'function') { | ||
callback = path | ||
path = undefined | ||
} | ||
|
||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
options = options || {} | ||
|
||
if (CID.isCID(cid)) { | ||
cid = cid.toBaseEncodedString() | ||
} | ||
|
||
if (typeof cid === 'string') { | ||
const split = cid.split('/') | ||
cid = split[0] | ||
split.shift() | ||
|
||
if (split.length > 0) { | ||
path = split.join('/') | ||
} else { | ||
path = '/' | ||
} | ||
} | ||
|
||
waterfall([ | ||
cb => { | ||
send({ | ||
path: 'dag/resolve', | ||
args: cid + '/' + path, | ||
qs: options | ||
}, cb) | ||
}, | ||
(resolved, cb) => { | ||
block(send).get(new CID(resolved['Cid']['/']), (err, blk) => cb(err, blk, resolved['RemPath'])) | ||
}, | ||
(blk, path, cb) => { | ||
if (blk.cid.codec === 'dag-cbor') { | ||
dagCBOR.resolver.resolve(blk, path, cb) | ||
} | ||
if (blk.cid.codec === 'dag-pb') { | ||
dagPB.resolver.resolve(blk, path, cb) | ||
} | ||
} | ||
], callback) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict' | ||
|
||
const moduleConfig = require('../utils/module-config') | ||
|
||
module.exports = (arg) => { | ||
const send = moduleConfig(arg) | ||
|
||
return { | ||
get: require('./get')(send), | ||
put: require('./put')(send) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict' | ||
|
||
const dagPB = require('ipld-dag-pb') | ||
const dagCBOR = require('ipld-dag-cbor') | ||
const promisify = require('promisify-es6') | ||
const CID = require('cids') | ||
const multihash = require('multihashes') | ||
|
||
function noop () {} | ||
|
||
module.exports = (send) => { | ||
return promisify((dagNode, options, callback) => { | ||
if (typeof options === 'function') { | ||
return setImmediate(() => callback(new Error('no options were passed'))) | ||
} | ||
|
||
callback = callback || noop | ||
|
||
let hashAlg = options.hash || 'sha2-256' | ||
let format | ||
let inputEnc | ||
|
||
if (options.cid && CID.isCID(options.cid)) { | ||
format = options.cid.codec | ||
hashAlg = multihash.decode(options.cid.multihash).name | ||
prepare() | ||
} else if (options.format) { | ||
format = options.format | ||
prepare() | ||
} else { | ||
callback(new Error('Invalid arguments')) | ||
} | ||
|
||
function prepare () { | ||
inputEnc = 'raw' | ||
|
||
if (format === 'dag-cbor') { | ||
dagCBOR.util.serialize(dagNode, finalize) | ||
} | ||
if (format === 'dag-pb') { | ||
dagPB.util.serialize(dagNode, finalize) | ||
} | ||
} | ||
|
||
function finalize (err, serialized) { | ||
if (err) { return callback(err) } | ||
|
||
send({ | ||
path: 'dag/put', | ||
qs: { | ||
hash: hashAlg, | ||
format: format, | ||
'input-enc': inputEnc | ||
}, | ||
files: serialized | ||
}, (err, result) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
if (result['Cid']) { | ||
return callback(null, new CID(result['Cid']['/'])) | ||
} else { | ||
return callback(result) | ||
} | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters