@@ -5,7 +5,6 @@ const dagCBOR = require('ipld-dag-cbor')
5
5
const promisify = require ( 'promisify-es6' )
6
6
const CID = require ( 'cids' )
7
7
const multihash = require ( 'multihashes' )
8
- const setImmediate = require ( 'async/setImmediate' )
9
8
const SendOneFile = require ( '../utils/send-one-file' )
10
9
11
10
function noop ( ) { }
@@ -15,29 +14,35 @@ module.exports = (send) => {
15
14
16
15
return promisify ( ( dagNode , options , callback ) => {
17
16
if ( typeof options === 'function' ) {
18
- return setImmediate ( ( ) => callback ( new Error ( 'no options were passed' ) ) )
17
+ callback = options
18
+ } else if ( options . cid && ( options . format || options . hash ) ) {
19
+ return callback ( new Error ( 'Can\'t put dag node. Please provide either `cid` OR `format` and `hash` options.' ) )
20
+ } else if ( ( options . format && ! options . hash ) || ( ! options . format && options . hash ) ) {
21
+ return callback ( new Error ( 'Can\'t put dag node. Please provide `format` AND `hash` options.' ) )
19
22
}
20
23
21
24
callback = callback || noop
22
25
23
- let hashAlg = options . hash || 'sha2-256'
24
- let format
25
- let inputEnc
26
+ const optionDefaults = {
27
+ format : 'dag-cbor' ,
28
+ hash : 'sha2-255' ,
29
+ inputEnc : 'raw'
30
+ }
31
+
32
+ let hashAlg = options . hash || optionDefaults . hash
33
+ let format = optionDefaults . format
34
+ let inputEnc = optionDefaults . inputEnc
26
35
27
36
if ( options . cid && CID . isCID ( options . cid ) ) {
28
37
format = options . cid . codec
29
38
hashAlg = multihash . decode ( options . cid . multihash ) . name
30
39
prepare ( )
31
- } else if ( options . format ) {
40
+ } else {
32
41
format = options . format
33
42
prepare ( )
34
- } else {
35
- callback ( new Error ( 'Invalid arguments' ) )
36
43
}
37
44
38
45
function prepare ( ) {
39
- inputEnc = 'raw'
40
-
41
46
if ( format === 'dag-cbor' ) {
42
47
dagCBOR . util . serialize ( dagNode , finalize )
43
48
}
0 commit comments