Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit c73bd2f

Browse files
author
Alan Shaw
authored
fix: allow null/undefined options (#1581)
Options should be optional! Our API should be flexible enough to allow passing null or undefined in place of an options object. This PR fixes cases where we assume an options object has been passed. fixes: #1574
1 parent 88c735a commit c73bd2f

File tree

15 files changed

+211
-9
lines changed

15 files changed

+211
-9
lines changed

src/core/components/block.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module.exports = function block (self) {
3939
options = {}
4040
}
4141

42+
options = options || {}
43+
4244
if (Array.isArray(block)) {
4345
return callback(new Error('Array is not supported'))
4446
}
@@ -93,6 +95,8 @@ module.exports = function block (self) {
9395
options = {}
9496
}
9597

98+
options = options || {}
99+
96100
try {
97101
cid = cleanCid(cid)
98102
} catch (err) {

src/core/components/dag.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ module.exports = function dag (self) {
1414
if (typeof options === 'function') {
1515
callback = options
1616
options = {}
17-
} else if (options && options.cid && (options.format || options.hashAlg)) {
17+
}
18+
19+
options = options || {}
20+
21+
if (options.cid && (options.format || options.hashAlg)) {
1822
return callback(new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hashAlg` options.'))
19-
} else if (options && ((options.format && !options.hashAlg) || (!options.format && options.hashAlg))) {
23+
} else if (((options.format && !options.hashAlg) || (!options.format && options.hashAlg))) {
2024
return callback(new Error('Can\'t put dag node. Please provide `format` AND `hashAlg` options.'))
2125
}
22-
options = options || {}
2326

2427
const optionDefaults = {
2528
format: 'dag-cbor',

src/core/components/dht.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ module.exports = (self) => {
2828
options = {}
2929
}
3030

31+
options = options || {}
32+
3133
self._libp2pNode.dht.get(key, options.timeout, callback)
3234
}),
3335

@@ -134,6 +136,8 @@ module.exports = (self) => {
134136
options = {}
135137
}
136138

139+
options = options || {}
140+
137141
// ensure blocks are actually local
138142
every(keys, (key, cb) => {
139143
self._repo.blocks.has(key, cb)

src/core/components/dns.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module.exports = () => {
1515
opts = {}
1616
}
1717

18+
opts = opts || {}
19+
1820
dns(domain, opts, callback)
1921
})
2022
}

src/core/components/files.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,14 @@ module.exports = function files (self) {
256256

257257
return {
258258
add: (() => {
259-
const add = promisify((data, options = {}, callback) => {
259+
const add = promisify((data, options, callback) => {
260260
if (typeof options === 'function') {
261261
callback = options
262262
options = {}
263-
} else if (!callback || typeof callback !== 'function') {
264-
callback = noop
265263
}
266264

265+
options = options || {}
266+
267267
const ok = Buffer.isBuffer(data) ||
268268
isStream.readable(data) ||
269269
Array.isArray(data) ||

src/core/components/key.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const promisify = require('promisify-es6')
77
module.exports = function key (self) {
88
return {
99
gen: promisify((name, opts, callback) => {
10+
opts = opts || {}
1011
self._keychain.createKey(name, opts.type, opts.size, callback)
1112
}),
1213

src/core/components/object.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ module.exports = function object (self) {
7070
options = {}
7171
}
7272

73+
options = options || {}
74+
7375
waterfall([
7476
(cb) => {
7577
self.object.get(multihash, options, cb)
@@ -151,6 +153,8 @@ module.exports = function object (self) {
151153
options = {}
152154
}
153155

156+
options = options || {}
157+
154158
const encoding = options.enc
155159
let node
156160

@@ -217,6 +221,8 @@ module.exports = function object (self) {
217221
options = {}
218222
}
219223

224+
options = options || {}
225+
220226
let mh, cid
221227

222228
try {

src/core/components/stats.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ module.exports = function stats (self) {
8181
opts = {}
8282
}
8383

84+
opts = opts || {}
85+
8486
bandwidthStats(self, opts)
8587
.then((stats) => callback(null, stats))
8688
.catch((err) => callback(err))

src/core/components/swarm.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module.exports = function swarm (self) {
1313
opts = {}
1414
}
1515

16+
opts = opts || {}
17+
1618
if (!self.isOnline()) {
1719
return callback(new Error(OFFLINE_ERROR))
1820
}

src/core/components/version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function version (self) {
1313

1414
self.repo.version((err, repoVersion) => {
1515
if (err) {
16-
callback(err)
16+
return callback(err)
1717
}
1818

1919
callback(null, {

0 commit comments

Comments
 (0)