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

Commit ebbb12d

Browse files
authored
fix: remove use of instanceof for CID class (#3847)
When we use ipjs to publish esm modules as esm and cjs, consuming code can end up loading classes from different locations based on whether the consuming code is esm or cjs so `instanceof` becomes unreliable. Remove its use with the CID class since we dual publish that one.
1 parent 7e02a57 commit ebbb12d

File tree

33 files changed

+80
-72
lines changed

33 files changed

+80
-72
lines changed

packages/interface-ipfs-core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"@types/pako": "^1.0.2",
4646
"@types/readable-stream": "^2.3.11",
4747
"abort-controller": "^3.0.0",
48-
"aegir": "^35.0.2",
48+
"aegir": "^35.0.3",
4949
"delay": "^5.0.0",
5050
"err-code": "^3.0.1",
5151
"interface-blockstore": "^1.0.0",

packages/ipfs-cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"devDependencies": {
6969
"@types/progress": "^2.0.3",
7070
"@types/yargs": "^16.0.0",
71-
"aegir": "^35.0.2",
71+
"aegir": "^35.0.3",
7272
"it-to-buffer": "^2.0.0",
7373
"nanoid": "^3.1.12",
7474
"ncp": "^2.0.0",

packages/ipfs-cli/src/utils.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,10 @@ const escapeControlCharacters = (str) => {
338338
* @returns {any}
339339
*/
340340
const makeEntriesPrintable = (obj, cidBase) => {
341-
if (obj instanceof CID) {
342-
return { '/': obj.toString(cidBase.encoder) }
341+
const cid = CID.asCID(obj)
342+
343+
if (cid) {
344+
return { '/': cid.toString(cidBase.encoder) }
343345
}
344346

345347
if (typeof obj === 'string') {

packages/ipfs-client/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"merge-options": "^3.0.4"
3838
},
3939
"devDependencies": {
40-
"aegir": "^35.0.2",
40+
"aegir": "^35.0.3",
4141
"rimraf": "^3.0.2"
4242
}
4343
}

packages/ipfs-core-types/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"multiformats": "^9.4.1"
4848
},
4949
"devDependencies": {
50-
"aegir": "^35.0.2",
50+
"aegir": "^35.0.3",
5151
"rimraf": "^3.0.2"
5252
},
5353
"contributors": [

packages/ipfs-core-utils/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
},
6161
"devDependencies": {
6262
"@web-std/file": "^1.1.2",
63-
"aegir": "^35.0.2",
63+
"aegir": "^35.0.3",
6464
"rimraf": "^3.0.2"
6565
}
6666
}

packages/ipfs-core-utils/src/pins/normalise-input.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ async function * normaliseInput (input) {
5353
}
5454

5555
// CID
56-
if (input instanceof CID) {
57-
yield toPin({ cid: input })
56+
const cid = CID.asCID(input)
57+
58+
if (cid) {
59+
yield toPin({ cid })
5860
return
5961
}
6062

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

8082
// Iterable<CID|String>
81-
if (first.value instanceof CID || first.value instanceof String || typeof first.value === 'string') {
83+
if (CID.asCID(first.value) || first.value instanceof String || typeof first.value === 'string') {
8284
yield toPin({ cid: first.value })
8385
for (const cid of iterator) {
8486
yield toPin({ cid })
@@ -106,7 +108,7 @@ async function * normaliseInput (input) {
106108
if (first.done) return iterator
107109

108110
// AsyncIterable<CID|String>
109-
if (first.value instanceof CID || first.value instanceof String || typeof first.value === 'string') {
111+
if (CID.asCID(first.value) || first.value instanceof String || typeof first.value === 'string') {
110112
yield toPin({ cid: first.value })
111113
for await (const cid of iterator) {
112114
yield toPin({ cid })

packages/ipfs-core-utils/src/to-cid-and-path.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@ const toCidAndPath = (string) => {
1818
}
1919
}
2020

21-
if (string instanceof CID) {
21+
let cid = CID.asCID(string)
22+
23+
if (cid) {
2224
return {
23-
cid: string,
25+
cid,
2426
path: undefined
2527
}
2628
}
2729

30+
string = string.toString()
31+
2832
if (string.startsWith(IPFS_PREFIX)) {
2933
string = string.substring(IPFS_PREFIX.length)
3034
}
3135

3236
const parts = string.split('/')
33-
let cid
3437
let path
3538

3639
try {

packages/ipfs-core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
"@types/dlv": "^1.1.2",
135135
"@types/pako": "^1.0.2",
136136
"@types/rimraf": "^3.0.1",
137-
"aegir": "^35.0.2",
137+
"aegir": "^35.0.3",
138138
"delay": "^5.0.0",
139139
"go-ipfs": "0.9.1",
140140
"interface-blockstore-tests": "^1.0.0",
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
'use strict'
22

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

65
/**
76
* @param {string|Uint8Array|CID} cid
87
*/
98
exports.cleanCid = cid => {
10-
if (cid instanceof CID) {
11-
return cid
12-
}
13-
14-
if (typeof cid === 'string') {
15-
return CID.parse(cid)
16-
}
17-
189
if (cid instanceof Uint8Array) {
1910
return CID.decode(cid)
2011
}
2112

22-
throw errCode(new Error('Invalid CID'), 'ERR_INVALID_CID')
13+
return CID.parse(cid.toString())
2314
}

packages/ipfs-core/src/components/files/utils/to-mfs-path.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ const toMfsPath = async (context, path, options) => {
9999

100100
let ipfsPath = ''
101101

102-
if (path instanceof CID) {
102+
if (CID.asCID(path)) {
103103
ipfsPath = `/ipfs/${path}`
104104
} else {
105-
ipfsPath = path
105+
ipfsPath = path.toString()
106106
}
107107

108108
ipfsPath = ipfsPath.trim()

packages/ipfs-core/src/components/object/links.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ function findLinks (node, links = []) {
3232
}
3333
}
3434

35-
if (val instanceof CID) {
35+
const cid = CID.asCID(val)
36+
37+
if (cid) {
3638
links.push({
3739
Name: '',
3840
Tsize: 0,
39-
Hash: val
41+
Hash: cid
4042
})
4143
continue
4244
}

packages/ipfs-core/src/components/pin/add.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ module.exports = ({ addAll }) =>
1414
(path, options = {}) => {
1515
let iter
1616

17-
if (path instanceof CID) {
17+
const cid = CID.asCID(path)
18+
19+
if (cid) {
1820
iter = addAll([{
19-
cid: path,
21+
cid,
2022
...options
2123
}], options)
2224
} else {

packages/ipfs-core/src/components/resolve.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module.exports = ({ repo, codecs, bases, name }) => {
4848
let remainderPath = path
4949

5050
for await (const result of results) {
51-
if (result.value instanceof CID) {
51+
if (CID.asCID(result.value)) {
5252
value = result.value
5353
remainderPath = result.remainderPath
5454
}

packages/ipfs-core/src/utils.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ const ERR_BAD_PATH = 'ERR_BAD_PATH'
2525
* @throws on an invalid @param pathStr
2626
*/
2727
const normalizePath = (pathStr) => {
28-
if (pathStr instanceof CID) {
28+
const cid = CID.asCID(pathStr)
29+
30+
if (cid) {
2931
return `/ipfs/${pathStr}`
3032
}
3133

34+
const str = pathStr.toString()
35+
3236
try {
33-
CID.parse(pathStr)
34-
pathStr = `/ipfs/${pathStr}`
37+
return `/ipfs/${CID.parse(str)}`
3538
} catch {}
3639

37-
if (isIpfs.path(pathStr)) {
38-
return pathStr
40+
if (isIpfs.path(str)) {
41+
return str
3942
} else {
4043
throw errCode(new Error(`invalid path: ${pathStr}`), ERR_BAD_PATH)
4144
}
@@ -45,21 +48,22 @@ const normalizePath = (pathStr) => {
4548
// TODO: don't forget ipfs-core-utils/src/to-cid-and-path
4649
/**
4750
* @param {Uint8Array|CID|string} path
48-
* @returns {string}
4951
*/
5052
const normalizeCidPath = (path) => {
5153
if (path instanceof Uint8Array) {
5254
return CID.decode(path).toString()
5355
}
54-
if (path instanceof CID) {
55-
return path.toString()
56-
}
56+
57+
path = path.toString()
58+
5759
if (path.indexOf('/ipfs/') === 0) {
5860
path = path.substring('/ipfs/'.length)
5961
}
62+
6063
if (path.charAt(path.length - 1) === '/') {
6164
path = path.substring(0, path.length - 1)
6265
}
66+
6367
return path
6468
}
6569

@@ -95,7 +99,7 @@ const resolvePath = async function (repo, codecs, ipfsPath, options = {}) {
9599
for await (const { value, remainderPath } of resolve(cid, options.path, codecs, repo, {
96100
signal: options.signal
97101
})) {
98-
if (!(value instanceof CID)) {
102+
if (!CID.asCID(value)) {
99103
break
100104
}
101105

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

238-
if (value instanceof CID) {
242+
if (CID.asCID(value)) {
239243
lastCid = value
240244
value = await load(value)
241245
}

packages/ipfs-daemon/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"libp2p-webrtc-star": "^0.23.0"
4444
},
4545
"devDependencies": {
46-
"aegir": "^35.0.2",
46+
"aegir": "^35.0.3",
4747
"node-fetch": "npm:@achingbrain/node-fetch@^2.6.4",
4848
"ws": "^7.3.1"
4949
},

packages/ipfs-grpc-client/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"ws": "^7.3.1"
5151
},
5252
"devDependencies": {
53-
"aegir": "^35.0.2",
53+
"aegir": "^35.0.3",
5454
"it-all": "^1.0.4",
5555
"rimraf": "^3.0.2",
5656
"sinon": "^11.1.1"

packages/ipfs-grpc-server/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
},
4949
"devDependencies": {
5050
"@types/ws": "^7.4.0",
51-
"aegir": "^35.0.2",
51+
"aegir": "^35.0.3",
5252
"ipfs-core": "^0.10.4",
5353
"it-all": "^1.0.4",
5454
"it-drain": "^1.0.3",

packages/ipfs-http-client/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"uint8arrays": "^3.0.0"
6868
},
6969
"devDependencies": {
70-
"aegir": "^35.0.2",
70+
"aegir": "^35.0.3",
7171
"delay": "^5.0.0",
7272
"go-ipfs": "0.9.1",
7373
"ipfsd-ctl": "^10.0.3",

packages/ipfs-http-client/src/files/cp.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = configure(api => {
2020
const res = await api.post('files/cp', {
2121
signal: options.signal,
2222
searchParams: toUrlSearchParams({
23-
arg: sourceArr.concat(destination).map(src => src instanceof CID ? `/ipfs/${src}` : src),
23+
arg: sourceArr.concat(destination).map(src => CID.asCID(src) ? `/ipfs/${src}` : src),
2424
...options
2525
}),
2626
headers: options.headers

packages/ipfs-http-client/src/files/ls.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = configure(api => {
2121
const res = await api.post('files/ls', {
2222
signal: options.signal,
2323
searchParams: toUrlSearchParams({
24-
arg: path instanceof CID ? `/ipfs/${path}` : path,
24+
arg: CID.asCID(path) ? `/ipfs/${path}` : path,
2525
// default long to true, diverges from go-ipfs where its false by default
2626
long: true,
2727
...options,

packages/ipfs-http-client/src/files/stat.js

-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ module.exports = configure(api => {
1515
* @type {FilesAPI["stat"]}
1616
*/
1717
async function stat (path, options = {}) {
18-
if (path && !(path instanceof CID) && typeof path !== 'string') {
19-
options = path || {}
20-
path = '/'
21-
}
22-
23-
options = options || {}
24-
2518
const res = await api.post('files/stat', {
2619
signal: options.signal,
2720
searchParams: toUrlSearchParams({

packages/ipfs-http-client/src/lib/resolve.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ const resolve = async function * (cid, path, codecs, getBlock, options) {
5757
throw errCode(new Error(`no link named "${key}" under ${lastCid}`), 'ERR_NO_LINK')
5858
}
5959

60-
if (value instanceof CID) {
61-
lastCid = value
60+
const cid = CID.asCID(value)
61+
62+
if (cid) {
63+
lastCid = cid
6264
value = await load(value)
6365
}
6466
}

packages/ipfs-http-client/src/pin/remote/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const encodeService = (service) => {
119119
* @returns {string}
120120
*/
121121
const encodeCID = (cid) => {
122-
if (cid instanceof CID) {
122+
if (CID.asCID(cid)) {
123123
return cid.toString()
124124
} else {
125125
throw new TypeError(`CID instance expected instead of ${typeof cid}`)

packages/ipfs-http-gateway/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"devDependencies": {
6060
"@types/hapi-pino": "^8.0.1",
6161
"@types/hapi__hapi": "^20.0.5",
62-
"aegir": "^35.0.2",
62+
"aegir": "^35.0.3",
6363
"file-type": "^16.0.0",
6464
"rimraf": "^3.0.2",
6565
"sinon": "^11.1.1"

packages/ipfs-http-server/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"devDependencies": {
6868
"@types/hapi-pino": "^8.0.1",
6969
"@types/hapi__hapi": "^20.0.5",
70-
"aegir": "^35.0.2",
70+
"aegir": "^35.0.3",
7171
"form-data": "^4.0.0",
7272
"ipfs-http-client": "^52.0.2",
7373
"iso-random-stream": "^2.0.0",

packages/ipfs-message-port-client/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"multiformats": "^9.4.1"
4141
},
4242
"devDependencies": {
43-
"aegir": "^35.0.2",
43+
"aegir": "^35.0.3",
4444
"interface-ipfs-core": "^0.150.1",
4545
"ipfs-core": "^0.10.4",
4646
"ipfs-message-port-server": "^0.9.1",

0 commit comments

Comments
 (0)