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

fix: remove use of instanceof for CID class #3847

Merged
merged 5 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/interface-ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@types/pako": "^1.0.2",
"@types/readable-stream": "^2.3.11",
"abort-controller": "^3.0.0",
"aegir": "^35.0.2",
"aegir": "^35.0.3",
"delay": "^5.0.0",
"err-code": "^3.0.1",
"interface-blockstore": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"devDependencies": {
"@types/progress": "^2.0.3",
"@types/yargs": "^16.0.0",
"aegir": "^35.0.2",
"aegir": "^35.0.3",
"it-to-buffer": "^2.0.0",
"nanoid": "^3.1.12",
"ncp": "^2.0.0",
Expand Down
6 changes: 4 additions & 2 deletions packages/ipfs-cli/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,10 @@ const escapeControlCharacters = (str) => {
* @returns {any}
*/
const makeEntriesPrintable = (obj, cidBase) => {
if (obj instanceof CID) {
return { '/': obj.toString(cidBase.encoder) }
const cid = CID.asCID(obj)

if (cid) {
return { '/': cid.toString(cidBase.encoder) }
}

if (typeof obj === 'string') {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"merge-options": "^3.0.4"
},
"devDependencies": {
"aegir": "^35.0.2",
"aegir": "^35.0.3",
"rimraf": "^3.0.2"
}
}
2 changes: 1 addition & 1 deletion packages/ipfs-core-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"multiformats": "^9.4.1"
},
"devDependencies": {
"aegir": "^35.0.2",
"aegir": "^35.0.3",
"rimraf": "^3.0.2"
},
"contributors": [
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-core-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
},
"devDependencies": {
"@web-std/file": "^1.1.2",
"aegir": "^35.0.2",
"aegir": "^35.0.3",
"rimraf": "^3.0.2"
}
}
10 changes: 6 additions & 4 deletions packages/ipfs-core-utils/src/pins/normalise-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ async function * normaliseInput (input) {
}

// CID
if (input instanceof CID) {
yield toPin({ cid: input })
const cid = CID.asCID(input)

if (cid) {
yield toPin({ cid })
return
}

Expand All @@ -78,7 +80,7 @@ async function * normaliseInput (input) {
if (first.done) return iterator

// Iterable<CID|String>
if (first.value instanceof CID || first.value instanceof String || typeof first.value === 'string') {
if (CID.asCID(first.value) || first.value instanceof String || typeof first.value === 'string') {
yield toPin({ cid: first.value })
for (const cid of iterator) {
yield toPin({ cid })
Expand Down Expand Up @@ -106,7 +108,7 @@ async function * normaliseInput (input) {
if (first.done) return iterator

// AsyncIterable<CID|String>
if (first.value instanceof CID || first.value instanceof String || typeof first.value === 'string') {
if (CID.asCID(first.value) || first.value instanceof String || typeof first.value === 'string') {
yield toPin({ cid: first.value })
for await (const cid of iterator) {
yield toPin({ cid })
Expand Down
9 changes: 6 additions & 3 deletions packages/ipfs-core-utils/src/to-cid-and-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ const toCidAndPath = (string) => {
}
}

if (string instanceof CID) {
let cid = CID.asCID(string)

if (cid) {
return {
cid: string,
cid,
path: undefined
}
}

string = string.toString()

if (string.startsWith(IPFS_PREFIX)) {
string = string.substring(IPFS_PREFIX.length)
}

const parts = string.split('/')
let cid
let path

try {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"@types/dlv": "^1.1.2",
"@types/pako": "^1.0.2",
"@types/rimraf": "^3.0.1",
"aegir": "^35.0.2",
"aegir": "^35.0.3",
"delay": "^5.0.0",
"go-ipfs": "0.9.1",
"interface-blockstore-tests": "^1.0.0",
Expand Down
11 changes: 1 addition & 10 deletions packages/ipfs-core/src/components/block/utils.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
'use strict'

const { CID } = require('multiformats/cid')
const errCode = require('err-code')

/**
* @param {string|Uint8Array|CID} cid
*/
exports.cleanCid = cid => {
if (cid instanceof CID) {
return cid
}

if (typeof cid === 'string') {
return CID.parse(cid)
}

if (cid instanceof Uint8Array) {
return CID.decode(cid)
}

throw errCode(new Error('Invalid CID'), 'ERR_INVALID_CID')
return CID.parse(cid.toString())
}
4 changes: 2 additions & 2 deletions packages/ipfs-core/src/components/files/utils/to-mfs-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ const toMfsPath = async (context, path, options) => {

let ipfsPath = ''

if (path instanceof CID) {
if (CID.asCID(path)) {
ipfsPath = `/ipfs/${path}`
} else {
ipfsPath = path
ipfsPath = path.toString()
}

ipfsPath = ipfsPath.trim()
Expand Down
6 changes: 4 additions & 2 deletions packages/ipfs-core/src/components/object/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ function findLinks (node, links = []) {
}
}

if (val instanceof CID) {
const cid = CID.asCID(val)

if (cid) {
links.push({
Name: '',
Tsize: 0,
Hash: val
Hash: cid
})
continue
}
Expand Down
6 changes: 4 additions & 2 deletions packages/ipfs-core/src/components/pin/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ module.exports = ({ addAll }) =>
(path, options = {}) => {
let iter

if (path instanceof CID) {
const cid = CID.asCID(path)

if (cid) {
iter = addAll([{
cid: path,
cid,
...options
}], options)
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-core/src/components/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = ({ repo, codecs, bases, name }) => {
let remainderPath = path

for await (const result of results) {
if (result.value instanceof CID) {
if (CID.asCID(result.value)) {
value = result.value
remainderPath = result.remainderPath
}
Expand Down
26 changes: 15 additions & 11 deletions packages/ipfs-core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ const ERR_BAD_PATH = 'ERR_BAD_PATH'
* @throws on an invalid @param pathStr
*/
const normalizePath = (pathStr) => {
if (pathStr instanceof CID) {
const cid = CID.asCID(pathStr)

if (cid) {
return `/ipfs/${pathStr}`
}

const str = pathStr.toString()

try {
CID.parse(pathStr)
pathStr = `/ipfs/${pathStr}`
return `/ipfs/${CID.parse(str)}`
} catch {}

if (isIpfs.path(pathStr)) {
return pathStr
if (isIpfs.path(str)) {
return str
} else {
throw errCode(new Error(`invalid path: ${pathStr}`), ERR_BAD_PATH)
}
Expand All @@ -45,21 +48,22 @@ const normalizePath = (pathStr) => {
// TODO: don't forget ipfs-core-utils/src/to-cid-and-path
/**
* @param {Uint8Array|CID|string} path
* @returns {string}
*/
const normalizeCidPath = (path) => {
if (path instanceof Uint8Array) {
return CID.decode(path).toString()
}
if (path instanceof CID) {
return path.toString()
}

path = path.toString()

if (path.indexOf('/ipfs/') === 0) {
path = path.substring('/ipfs/'.length)
}

if (path.charAt(path.length - 1) === '/') {
path = path.substring(0, path.length - 1)
}

return path
}

Expand Down Expand Up @@ -95,7 +99,7 @@ const resolvePath = async function (repo, codecs, ipfsPath, options = {}) {
for await (const { value, remainderPath } of resolve(cid, options.path, codecs, repo, {
signal: options.signal
})) {
if (!(value instanceof CID)) {
if (!CID.asCID(value)) {
break
}

Expand Down Expand Up @@ -235,7 +239,7 @@ const resolve = async function * (cid, path, codecs, repo, options) {
throw errCode(new Error(`no link named "${key}" under ${lastCid}`), 'ERR_NO_LINK')
}

if (value instanceof CID) {
if (CID.asCID(value)) {
lastCid = value
value = await load(value)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-daemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"libp2p-webrtc-star": "^0.23.0"
},
"devDependencies": {
"aegir": "^35.0.2",
"aegir": "^35.0.3",
"node-fetch": "npm:@achingbrain/node-fetch@^2.6.4",
"ws": "^7.3.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-grpc-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"ws": "^7.3.1"
},
"devDependencies": {
"aegir": "^35.0.2",
"aegir": "^35.0.3",
"it-all": "^1.0.4",
"rimraf": "^3.0.2",
"sinon": "^11.1.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-grpc-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"devDependencies": {
"@types/ws": "^7.4.0",
"aegir": "^35.0.2",
"aegir": "^35.0.3",
"ipfs-core": "^0.10.4",
"it-all": "^1.0.4",
"it-drain": "^1.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"aegir": "^35.0.2",
"aegir": "^35.0.3",
"delay": "^5.0.0",
"go-ipfs": "0.9.1",
"ipfsd-ctl": "^10.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/src/files/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = configure(api => {
const res = await api.post('files/cp', {
signal: options.signal,
searchParams: toUrlSearchParams({
arg: sourceArr.concat(destination).map(src => src instanceof CID ? `/ipfs/${src}` : src),
arg: sourceArr.concat(destination).map(src => CID.asCID(src) ? `/ipfs/${src}` : src),
...options
}),
headers: options.headers
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/src/files/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = configure(api => {
const res = await api.post('files/ls', {
signal: options.signal,
searchParams: toUrlSearchParams({
arg: path instanceof CID ? `/ipfs/${path}` : path,
arg: CID.asCID(path) ? `/ipfs/${path}` : path,
// default long to true, diverges from go-ipfs where its false by default
long: true,
...options,
Expand Down
7 changes: 0 additions & 7 deletions packages/ipfs-http-client/src/files/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ module.exports = configure(api => {
* @type {FilesAPI["stat"]}
*/
async function stat (path, options = {}) {
if (path && !(path instanceof CID) && typeof path !== 'string') {
options = path || {}
path = '/'
}

options = options || {}

const res = await api.post('files/stat', {
signal: options.signal,
searchParams: toUrlSearchParams({
Expand Down
6 changes: 4 additions & 2 deletions packages/ipfs-http-client/src/lib/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ const resolve = async function * (cid, path, codecs, getBlock, options) {
throw errCode(new Error(`no link named "${key}" under ${lastCid}`), 'ERR_NO_LINK')
}

if (value instanceof CID) {
lastCid = value
const cid = CID.asCID(value)

if (cid) {
lastCid = cid
value = await load(value)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-client/src/pin/remote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const encodeService = (service) => {
* @returns {string}
*/
const encodeCID = (cid) => {
if (cid instanceof CID) {
if (CID.asCID(cid)) {
return cid.toString()
} else {
throw new TypeError(`CID instance expected instead of ${typeof cid}`)
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"devDependencies": {
"@types/hapi-pino": "^8.0.1",
"@types/hapi__hapi": "^20.0.5",
"aegir": "^35.0.2",
"aegir": "^35.0.3",
"file-type": "^16.0.0",
"rimraf": "^3.0.2",
"sinon": "^11.1.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-http-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"devDependencies": {
"@types/hapi-pino": "^8.0.1",
"@types/hapi__hapi": "^20.0.5",
"aegir": "^35.0.2",
"aegir": "^35.0.3",
"form-data": "^4.0.0",
"ipfs-http-client": "^52.0.2",
"iso-random-stream": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-message-port-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"multiformats": "^9.4.1"
},
"devDependencies": {
"aegir": "^35.0.2",
"aegir": "^35.0.3",
"interface-ipfs-core": "^0.150.1",
"ipfs-core": "^0.10.4",
"ipfs-message-port-server": "^0.9.1",
Expand Down
Loading