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

Commit 91c5999

Browse files
committed
refactor: only create new CID if not already stringy
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent 2b52ff2 commit 91c5999

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/core/components/files.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function preloadFile (self, opts, file, cb) {
9898

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

101-
self._preload(new CID(file.hash), (err) => {
101+
self._preload(file.hash, (err) => {
102102
// Preload error is not fatal
103103
if (err) console.error(err)
104104
cb(null, file)

src/core/preload.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const setImmediate = require('async/setImmediate')
44
const each = require('async/each')
55
const toUri = require('multiaddr-to-uri')
66
const debug = require('debug')
7+
const CID = require('cids')
78
const preload = require('./runtime/preload-nodejs')
89

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

47-
const url = `${gatewayUri}/ipfs/${cid.toBaseEncodedString()}#${redirectOptOutHint}`
48+
if (typeof cid !== 'string') {
49+
try {
50+
cid = new CID(cid).toBaseEncodedString()
51+
} catch (err) {
52+
return cb(err)
53+
}
54+
}
55+
56+
const url = `${gatewayUri}/ipfs/${cid}#${redirectOptOutHint}`
4857
preload(url, cb)
4958
}, callback)
5059
}

0 commit comments

Comments
 (0)