This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
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
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,79 @@ | ||
'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) => { | ||
const api = { | ||
put: promisify((dagNode, options, callback) => { | ||
if (typeof options === 'function') { | ||
return setImmediate(() => callback(new Error('no options were passed'))) | ||
} | ||
|
||
callback = callback || noop | ||
|
||
let hashAlg = options.hashAlg || '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 () { | ||
if (format === 'dag-cbor') { | ||
// TODO change this once | ||
// https://github.com/ipfs/go-ipfs/issues/3771 is finished | ||
format = 'cbor' | ||
|
||
inputEnc = 'cbor' | ||
dagCBOR.util.serialize(dagNode, finalize) | ||
} | ||
if (format === 'dag-pb') { | ||
// TODO change this once | ||
// https://github.com/ipfs/go-ipfs/issues/3771 is finished | ||
format = 'protobuf' | ||
|
||
inputEnc = 'protobuf' | ||
dagPB.util.serialize(dagNode, finalize) | ||
} | ||
} | ||
|
||
function finalize (err, serialized) { | ||
if (err) { return callback(err) } | ||
|
||
send({ | ||
path: 'dag/put', | ||
qs: { | ||
hashAlg: hashAlg, // not implemented in go yet https://github.com/ipfs/go-ipfs/issues/3771 | ||
format: format, | ||
inputenc: inputEnc | ||
}, | ||
files: serialized | ||
}, (err, result) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
// TODO handle the result | ||
}) | ||
} | ||
}), | ||
get: promisify((cid, path, options, callback) => { | ||
// TODO | ||
}) | ||
} | ||
|
||
return api | ||
} |
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,34 @@ | ||
/* eslint-env mocha */ | ||
|
||
'use strict' | ||
|
||
const test = require('interface-ipfs-core') | ||
const parallel = require('async/parallel') | ||
|
||
const IPFSApi = require('../../src') | ||
|
||
const DaemonFactory = require('ipfsd-ctl') | ||
const df = DaemonFactory.create() | ||
|
||
const nodes = [] | ||
const common = { | ||
setup: function (callback) { | ||
callback(null, { | ||
spawnNode: (cb) => { | ||
df.spawn((err, _ipfsd) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
nodes.push(_ipfsd) | ||
cb(null, IPFSApi(_ipfsd.apiAddr)) | ||
}) | ||
} | ||
}) | ||
}, | ||
teardown: function (callback) { | ||
parallel(nodes.map((node) => (cb) => node.stop(cb)), callback) | ||
} | ||
} | ||
|
||
test.dag(common) |