From 83952d1449a6d2a0f6bfadf04a52c43f75ebc33b Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Tue, 24 Feb 2026 23:40:14 -0800 Subject: [PATCH] Refactor to simplify and flatten conditionals --- index.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 4a382a6..e3d485f 100644 --- a/index.js +++ b/index.js @@ -219,19 +219,17 @@ function calculateCID(metadata) { * @returns {Promise} `Promise` that resolves to a `Response` object. */ function fetchCID(cid, ipfs) { - let response; - if (cid[0] === 'Q') { // v0 - response = fetch(`${ipfs.origin}/ipfs/${cid}${ipfs.search}`); + // CID v0 + if (cid[0] === 'Q') { + return fetch(`${ipfs.origin}/ipfs/${cid}${ipfs.search}`); } - else { // v1 - if (ipfs.host.split('.')[0] === 'ipfs') { - response = fetch(`${ipfs.protocol}//${cid}.${ipfs.host}${ipfs.pathname}${ipfs.search}`); - } - else { - response = fetch(`${ipfs.protocol}//${cid}.ipfs.${ipfs.host}${ipfs.pathname}${ipfs.search}`); - } + + // CID v1 + if (ipfs.host.split('.')[0] === 'ipfs') { // Hostname already includes ipfs subdomain + return fetch(`${ipfs.protocol}//${cid}.${ipfs.host}${ipfs.pathname}${ipfs.search}`); } - return response; + + return fetch(`${ipfs.protocol}//${cid}.ipfs.${ipfs.host}${ipfs.pathname}${ipfs.search}`); } export { fetchBytecode, detectLanguage, extractCBOR, decodeCBOR, calculateCID, fetchCID }; -- 2.47.3