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

Commit

Permalink
refactor: only create new CID if not already stringy
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
  • Loading branch information
alanshaw committed Jul 25, 2018
1 parent 25a8c4f commit d040f26
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function preloadFile (self, opts, file, cb) {

if (!shouldPreload) return cb(null, file)

self._preload(new CID(file.hash), (err) => {
self._preload(file.hash, (err) => {
// Preload error is not fatal
if (err) console.error(err)
cb(null, file)
Expand Down
11 changes: 10 additions & 1 deletion src/core/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const setImmediate = require('async/setImmediate')
const each = require('async/each')
const toUri = require('multiaddr-to-uri')
const debug = require('debug')
const CID = require('cids')
const preload = require('./runtime/preload-nodejs')

const log = debug('jsipfs:preload')
Expand Down Expand Up @@ -44,7 +45,15 @@ module.exports = (options) => {
return cb(err)
}

const url = `${gatewayUri}/ipfs/${cid.toBaseEncodedString()}#${redirectOptOutHint}`
if (typeof cid !== 'string') {
try {
cid = new CID(cid).toBaseEncodedString()
} catch (err) {
return cb(err)
}
}

const url = `${gatewayUri}/ipfs/${cid}#${redirectOptOutHint}`
preload(url, cb)
}, callback)
}
Expand Down

0 comments on commit d040f26

Please sign in to comment.