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

Commit

Permalink
chore: upgrade human-to-milliseconds (#2213)
Browse files Browse the repository at this point in the history
* chore: upgrade human to milliseconds

* chore: add clearInterval if stream ended
  • Loading branch information
vasco-santos authored and Alan Shaw committed Jul 4, 2019
1 parent f596b01 commit a421856
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"glob": "^7.1.3",
"hapi-pino": "^6.0.0",
"hashlru": "^2.3.0",
"human-to-milliseconds": "^1.0.0",
"human-to-milliseconds": "^2.0.0",
"interface-datastore": "~0.6.0",
"ipfs-bitswap": "~0.24.1",
"ipfs-block": "~0.8.1",
Expand Down
25 changes: 14 additions & 11 deletions src/core/components/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,20 @@ module.exports = function name (self) {
return callback(err)
}

let pubLifetime
try {
pubLifetime = human(lifetime)

// Calculate lifetime with nanoseconds precision
pubLifetime = pubLifetime.toFixed(6)
} catch (err) {
log.error(err)
return callback(err)
}

// TODO: ttl human for cache

parallel([
(cb) => human(lifetime, cb),
// (cb) => ttl ? human(ttl, cb) : cb(),
(cb) => keyLookup(self, key, cb),
// verify if the path exists, if not, an error will stop the execution
(cb) => resolve.toString() === 'true' ? path.resolvePath(self, value, cb) : cb()
Expand All @@ -121,16 +132,8 @@ module.exports = function name (self) {
return callback(err)
}

// Calculate lifetime with nanoseconds precision
const pubLifetime = results[0].toFixed(6)
const privateKey = results[1]

// TODO IMPROVEMENT - Handle ttl for cache
// const ttl = results[1]
// const privateKey = results[2]

// Start publishing process
self._ipns.publish(privateKey, value, pubLifetime, callback)
self._ipns.publish(results[0], value, pubLifetime, callback)
})
}),

Expand Down
29 changes: 19 additions & 10 deletions src/core/components/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,26 @@ module.exports = function stats (self) {
})

if (opts.poll) {
human(opts.interval || '1s', (err, value) => {
if (err) {
return stream.end(errCode(err, 'ERR_INVALID_POLL_INTERVAL'))
}
let value
try {
value = human(opts.interval || '1s')
} catch (err) {
// Pull stream expects async work, so we need to simulate it.
process.nextTick(() => {
stream.end(errCode(err, 'ERR_INVALID_POLL_INTERVAL'))
})
}

interval = setInterval(() => {
bandwidthStats(self, opts)
.then((stats) => stream.push(stats))
.catch((err) => stream.end(err))
}, value)
})
interval = setInterval(() => {
bandwidthStats(self, opts)
.then((stats) => stream.push(stats))
.catch((err) => {
if (interval) {
clearInterval(interval)
}
stream.end(err)
})
}, value)
} else {
bandwidthStats(self, opts)
.then((stats) => {
Expand Down

0 comments on commit a421856

Please sign in to comment.