Skip to content

Commit 3c4c966

Browse files
authored
chore: replace toBaseEncodedString() with toString() (#135)
On CIDs `toString()` does the same as `toBaseEncodedString()`. The new js-multiformats based CID implementation only has `toString()`. This change should make future PRs easier to review.
1 parent 5ccac2f commit 3c4c966

12 files changed

+59
-59
lines changed

packages/ipfs-unixfs-exporter/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async function * walkPath (path, ipld, options = {}) {
7070
cid,
7171
toResolve
7272
} = cidAndRest(path)
73-
let name = cid.toBaseEncodedString()
73+
let name = cid.toString()
7474
let entryPath = name
7575
const startingDepth = toResolve.length
7676

packages/ipfs-unixfs-exporter/src/resolvers/dag-cbor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options
5050
subObject = subObject[prop]
5151
} else {
5252
// cannot resolve further
53-
throw errCode(new Error(`No property named ${prop} found in cbor node ${cid.toBaseEncodedString()}`), 'ERR_NO_PROP')
53+
throw errCode(new Error(`No property named ${prop} found in cbor node ${cid}`), 'ERR_NO_PROP')
5454
}
5555
}
5656

packages/ipfs-unixfs-exporter/src/resolvers/identity.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const rawContent = (node) => {
3434
*/
3535
const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options) => {
3636
if (toResolve.length) {
37-
throw errCode(new Error(`No link named ${path} found in raw node ${cid.toBaseEncodedString()}`), 'ERR_NOT_FOUND')
37+
throw errCode(new Error(`No link named ${path} found in raw node ${cid}`), 'ERR_NOT_FOUND')
3838
}
3939

4040
const buf = await mh.decode(cid.multihash)

packages/ipfs-unixfs-exporter/src/resolvers/raw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const rawContent = (node) => {
3232
*/
3333
const resolve = async (cid, name, path, toResolve, resolve, depth, ipld, options) => {
3434
if (toResolve.length) {
35-
throw errCode(new Error(`No link named ${path} found in raw node ${cid.toBaseEncodedString()}`), 'ERR_NOT_FOUND')
35+
throw errCode(new Error(`No link named ${path} found in raw node ${cid}`), 'ERR_NOT_FOUND')
3636
}
3737

3838
const buf = await ipld.get(cid, options)

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const unixFsResolver = async (cid, name, path, toResolve, resolve, depth, ipld,
5050
let next
5151

5252
if (!name) {
53-
name = cid.toBaseEncodedString()
53+
name = cid.toString()
5454
}
5555

5656
try {

packages/ipfs-unixfs-exporter/test/exporter-sharded.spec.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,59 +172,59 @@ describe('exporter sharded', function () {
172172

173173
it('exports one file from a sharded directory', async () => {
174174
const dirCid = await createShard(31)
175-
const exported = await exporter(`/ipfs/${dirCid.toBaseEncodedString()}/file-14`, ipld)
175+
const exported = await exporter(`/ipfs/${dirCid}/file-14`, ipld)
176176

177177
expect(exported).to.have.property('name', 'file-14')
178178
})
179179

180180
it('exports one file from a sharded directory sub shard', async () => {
181181
const dirCid = await createShard(31)
182-
const exported = await exporter(`/ipfs/${dirCid.toBaseEncodedString()}/file-30`, ipld)
182+
const exported = await exporter(`/ipfs/${dirCid}/file-30`, ipld)
183183

184184
expect(exported.name).to.deep.equal('file-30')
185185
})
186186

187187
it('exports one file from a shard inside a shard inside a shard', async () => {
188188
const dirCid = await createShard(2568)
189-
const exported = await exporter(`/ipfs/${dirCid.toBaseEncodedString()}/file-2567`, ipld)
189+
const exported = await exporter(`/ipfs/${dirCid}/file-2567`, ipld)
190190

191191
expect(exported.name).to.deep.equal('file-2567')
192192
})
193193

194194
it('extracts a deep folder from the sharded directory', async () => {
195195
const dirCid = await createShardWithFileNames(31, (index) => `/foo/bar/baz/file-${index}`)
196-
const exported = await exporter(`/ipfs/${dirCid.toBaseEncodedString()}/foo/bar/baz`, ipld)
196+
const exported = await exporter(`/ipfs/${dirCid}/foo/bar/baz`, ipld)
197197

198198
expect(exported.name).to.deep.equal('baz')
199199
})
200200

201201
it('extracts an intermediate folder from the sharded directory', async () => {
202202
const dirCid = await createShardWithFileNames(31, (index) => `/foo/bar/baz/file-${index}`)
203-
const exported = await exporter(`/ipfs/${dirCid.toBaseEncodedString()}/foo/bar`, ipld)
203+
const exported = await exporter(`/ipfs/${dirCid}/foo/bar`, ipld)
204204

205205
expect(exported.name).to.deep.equal('bar')
206206
})
207207

208208
it('uses .path to extract all intermediate entries from the sharded directory', async () => {
209209
const dirCid = await createShardWithFileNames(31, (index) => `/foo/bar/baz/file-${index}`)
210-
const exported = await all(walkPath(`/ipfs/${dirCid.toBaseEncodedString()}/foo/bar/baz/file-1`, ipld))
210+
const exported = await all(walkPath(`/ipfs/${dirCid}/foo/bar/baz/file-1`, ipld))
211211

212212
expect(exported.length).to.equal(5)
213213

214-
expect(exported[0].name).to.equal(dirCid.toBaseEncodedString())
214+
expect(exported[0].name).to.equal(dirCid.toString())
215215
expect(exported[1].name).to.equal('foo')
216-
expect(exported[1].path).to.equal(`${dirCid.toBaseEncodedString()}/foo`)
216+
expect(exported[1].path).to.equal(`${dirCid}/foo`)
217217
expect(exported[2].name).to.equal('bar')
218-
expect(exported[2].path).to.equal(`${dirCid.toBaseEncodedString()}/foo/bar`)
218+
expect(exported[2].path).to.equal(`${dirCid}/foo/bar`)
219219
expect(exported[3].name).to.equal('baz')
220-
expect(exported[3].path).to.equal(`${dirCid.toBaseEncodedString()}/foo/bar/baz`)
220+
expect(exported[3].path).to.equal(`${dirCid}/foo/bar/baz`)
221221
expect(exported[4].name).to.equal('file-1')
222-
expect(exported[4].path).to.equal(`${dirCid.toBaseEncodedString()}/foo/bar/baz/file-1`)
222+
expect(exported[4].path).to.equal(`${dirCid}/foo/bar/baz/file-1`)
223223
})
224224

225225
it('uses .path to extract all intermediate entries from the sharded directory as well as the contents', async () => {
226226
const dirCid = await createShardWithFileNames(31, (index) => `/foo/bar/baz/file-${index}`)
227-
const exported = await all(walkPath(`/ipfs/${dirCid.toBaseEncodedString()}/foo/bar/baz`, ipld))
227+
const exported = await all(walkPath(`/ipfs/${dirCid}/foo/bar/baz`, ipld))
228228

229229
expect(exported.length).to.equal(4)
230230

@@ -268,7 +268,7 @@ describe('exporter sharded', function () {
268268
hashAlg: mh.names['sha2-256']
269269
})
270270

271-
const exported = await exporter(`/ipfs/${shardNodeCid.toBaseEncodedString()}/normal-dir/shard/file-1`, ipld)
271+
const exported = await exporter(`/ipfs/${shardNodeCid}/normal-dir/shard/file-1`, ipld)
272272

273273
expect(exported.name).to.deep.equal('file-1')
274274
})

packages/ipfs-unixfs-exporter/test/exporter-subtree.spec.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ describe('exporter subtree', () => {
4444
throw new Error('Nothing imported')
4545
}
4646

47-
const exported = await exporter(`${imported.cid.toBaseEncodedString()}/level-1/200Bytes.txt`, ipld)
47+
const exported = await exporter(`${imported.cid}/level-1/200Bytes.txt`, ipld)
4848

4949
expect(exported).to.have.property('cid')
5050
expect(exported.name).to.equal('200Bytes.txt')
51-
expect(exported.path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/200Bytes.txt`)
51+
expect(exported.path).to.equal(`${imported.cid}/level-1/200Bytes.txt`)
5252

5353
if (exported.type !== 'file') {
5454
throw new Error('Unexpected type')
@@ -74,7 +74,7 @@ describe('exporter subtree', () => {
7474
throw new Error('Nothing imported')
7575
}
7676

77-
const exported = await exporter(`${imported.cid.toBaseEncodedString()}/level-1`, ipld)
77+
const exported = await exporter(`${imported.cid}/level-1`, ipld)
7878

7979
if (exported.type !== 'directory') {
8080
throw new Error('Unexpected type')
@@ -84,10 +84,10 @@ describe('exporter subtree', () => {
8484

8585
expect(files.length).to.equal(2)
8686
expect(files[0].name).to.equal('200Bytes.txt')
87-
expect(files[0].path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/200Bytes.txt`)
87+
expect(files[0].path).to.equal(`${imported.cid}/level-1/200Bytes.txt`)
8888

8989
expect(files[1].name).to.equal('level-2')
90-
expect(files[1].path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/level-2`)
90+
expect(files[1].path).to.equal(`${imported.cid}/level-1/level-2`)
9191

9292
if (files[0].type !== 'file') {
9393
throw new Error('Unexpected type')
@@ -108,7 +108,7 @@ describe('exporter subtree', () => {
108108
}
109109

110110
try {
111-
await exporter(`${imported.cid.toBaseEncodedString()}/doesnotexist`, ipld)
111+
await exporter(`${imported.cid}/doesnotexist`, ipld)
112112
} catch (err) {
113113
expect(err.code).to.equal('ERR_NOT_FOUND')
114114
}
@@ -134,16 +134,16 @@ describe('exporter subtree', () => {
134134
throw new Error('Nothing imported')
135135
}
136136

137-
const exported = await all(walkPath(`${imported.cid.toBaseEncodedString()}/level-1/level-2/200Bytes.txt`, ipld))
137+
const exported = await all(walkPath(`${imported.cid}/level-1/level-2/200Bytes.txt`, ipld))
138138

139139
expect(exported.length).to.equal(4)
140-
expect(exported[0].path).to.equal(imported.cid.toBaseEncodedString())
141-
expect(exported[0].name).to.equal(imported.cid.toBaseEncodedString())
142-
expect(exported[1].path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1`)
140+
expect(exported[0].path).to.equal(imported.cid.toString())
141+
expect(exported[0].name).to.equal(imported.cid.toString())
142+
expect(exported[1].path).to.equal(`${imported.cid}/level-1`)
143143
expect(exported[1].name).to.equal('level-1')
144-
expect(exported[2].path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/level-2`)
144+
expect(exported[2].path).to.equal(`${imported.cid}/level-1/level-2`)
145145
expect(exported[2].name).to.equal('level-2')
146-
expect(exported[3].path).to.equal(`${imported.cid.toBaseEncodedString()}/level-1/level-2/200Bytes.txt`)
146+
expect(exported[3].path).to.equal(`${imported.cid}/level-1/level-2/200Bytes.txt`)
147147
expect(exported[3].name).to.equal('200Bytes.txt')
148148
})
149149
})

packages/ipfs-unixfs-exporter/test/exporter.spec.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ describe('exporter', () => {
180180
const file = await exporter(result.cid, ipld)
181181

182182
expect(file).to.have.property('cid')
183-
expect(file).to.have.property('path', result.cid.toBaseEncodedString())
183+
expect(file).to.have.property('path', result.cid.toString())
184184

185185
if (file.type !== 'file') {
186186
throw new Error('Unexpected type')
@@ -199,11 +199,11 @@ describe('exporter', () => {
199199
content: asAsyncIterable(smallFile)
200200
}], block))
201201

202-
const path = `/ipfs/${files[1].cid.toBaseEncodedString()}/${fileName}`
202+
const path = `/ipfs/${files[1].cid}/${fileName}`
203203
const file = await exporter(path, ipld)
204204

205205
expect(file.name).to.equal(fileName)
206-
expect(file.path).to.equal(`${files[1].cid.toBaseEncodedString()}/${fileName}`)
206+
expect(file.path).to.equal(`${files[1].cid}/${fileName}`)
207207
})
208208

209209
it('small file in a directory with an square brackets in the title', async () => {
@@ -215,11 +215,11 @@ describe('exporter', () => {
215215
content: asAsyncIterable(smallFile)
216216
}], block))
217217

218-
const path = `/ipfs/${files[1].cid.toBaseEncodedString()}/${fileName}`
218+
const path = `/ipfs/${files[1].cid}/${fileName}`
219219
const file = await exporter(path, ipld)
220220

221221
expect(file.name).to.equal(fileName)
222-
expect(file.path).to.equal(`${files[1].cid.toBaseEncodedString()}/${fileName}`)
222+
expect(file.path).to.equal(`${files[1].cid}/${fileName}`)
223223
})
224224

225225
it('exports a chunk of a file with no links', async () => {
@@ -338,7 +338,7 @@ describe('exporter', () => {
338338
throw new Error('Unexpected type')
339339
}
340340

341-
expect(file).to.have.property('path', cid.toBaseEncodedString())
341+
expect(file).to.have.property('path', cid.toString())
342342
expect(file.unixfs.fileSize()).to.equal(ONE_MEG * 6)
343343
})
344344

@@ -354,7 +354,7 @@ describe('exporter', () => {
354354
})
355355

356356
const file = await exporter(cid, ipld)
357-
expect(file).to.have.property('path', cid.toBaseEncodedString())
357+
expect(file).to.have.property('path', cid.toString())
358358

359359
if (file.type !== 'file') {
360360
throw new Error('Unexpected type')
@@ -432,9 +432,9 @@ describe('exporter', () => {
432432
expect(
433433
files.map((file) => file.path)
434434
).to.be.eql([
435-
`${dir.cid.toBaseEncodedString()}/200Bytes.txt`,
436-
`${dir.cid.toBaseEncodedString()}/dir-another`,
437-
`${dir.cid.toBaseEncodedString()}/level-1`
435+
`${dir.cid}/200Bytes.txt`,
436+
`${dir.cid}/dir-another`,
437+
`${dir.cid}/level-1`
438438
])
439439

440440
files
@@ -480,9 +480,9 @@ describe('exporter', () => {
480480
expect(
481481
files.map((file) => file.path)
482482
).to.be.eql([
483-
`${importedDir.cid.toBaseEncodedString()}/200Bytes.txt`,
484-
`${importedDir.cid.toBaseEncodedString()}/dir-another`,
485-
`${importedDir.cid.toBaseEncodedString()}/level-1`
483+
`${importedDir.cid}/200Bytes.txt`,
484+
`${importedDir.cid}/dir-another`,
485+
`${importedDir.cid}/level-1`
486486
])
487487

488488
expect(
@@ -942,7 +942,7 @@ describe('exporter', () => {
942942
}, mc.DAG_CBOR)
943943

944944
try {
945-
await exporter(`${cborNodeCid.toBaseEncodedString()}/baz`, ipld)
945+
await exporter(`${cborNodeCid}/baz`, ipld)
946946
} catch (err) {
947947
expect(err.code).to.equal('ERR_NO_PROP')
948948
}
@@ -954,7 +954,7 @@ describe('exporter', () => {
954954
}
955955

956956
const cborNodeCid = await ipld.put(node, mc.DAG_CBOR)
957-
const exported = await exporter(`${cborNodeCid.toBaseEncodedString()}`, ipld)
957+
const exported = await exporter(`${cborNodeCid}`, ipld)
958958

959959
if (exported.type !== 'object') {
960960
throw new Error('Unexpected type')
@@ -967,7 +967,7 @@ describe('exporter', () => {
967967
const cid = new CID(1, 'git-raw', new CID('zdj7WkRPAX9o9nb9zPbXzwG7JEs78uyhwbUs8JSUayB98DWWY').multihash)
968968

969969
try {
970-
await exporter(`${cid.toBaseEncodedString()}`, ipld)
970+
await exporter(`${cid}`, ipld)
971971
} catch (err) {
972972
expect(err.code).to.equal('ERR_NO_RESOLVER')
973973
}
@@ -977,7 +977,7 @@ describe('exporter', () => {
977977
const cid = await ipld.put(Uint8Array.from([0, 1, 2, 3, 4]), mc.RAW)
978978

979979
try {
980-
await exporter(`${cid.toBaseEncodedString()}/lol`, ipld)
980+
await exporter(`${cid}/lol`, ipld)
981981
} catch (err) {
982982
expect(err.code).to.equal('ERR_NOT_FOUND')
983983
}
@@ -1048,7 +1048,7 @@ describe('exporter', () => {
10481048
}
10491049

10501050
const exported = await all(recursive(dir.cid, ipld))
1051-
const dirCid = dir.cid.toBaseEncodedString()
1051+
const dirCid = dir.cid.toString()
10521052

10531053
expect(exported[0].depth).to.equal(0)
10541054
expect(exported[0].name).to.equal(dirCid)

packages/ipfs-unixfs-exporter/test/import-export-dir-sharding.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ describe('builder: directory sharding', () => {
117117
throw new Error('Unexpected type')
118118
}
119119

120-
const expectedHash = nonShardedHash.toBaseEncodedString()
120+
const expectedHash = nonShardedHash.toString()
121121

122122
expect(dir.path).to.be.eql(expectedHash)
123-
expect(dir.cid.toBaseEncodedString()).to.be.eql(expectedHash)
123+
expect(dir.cid.toString()).to.be.eql(expectedHash)
124124
expect(files[0].path).to.be.eql(expectedHash + '/b')
125125
expect(files[0].unixfs.fileSize()).to.be.eql(content.length)
126126

@@ -154,10 +154,10 @@ describe('builder: directory sharding', () => {
154154
throw new Error('Unexpected type')
155155
}
156156

157-
const expectedHash = shardedHash.toBaseEncodedString()
157+
const expectedHash = shardedHash.toString()
158158

159159
expect(dir.path).to.be.eql(expectedHash)
160-
expect(dir.cid.toBaseEncodedString()).to.be.eql(expectedHash)
160+
expect(dir.cid.toString()).to.be.eql(expectedHash)
161161
expect(files[0].path).to.be.eql(expectedHash + '/b')
162162
expect(files[0].unixfs.fileSize()).to.be.eql(content.length)
163163

@@ -325,7 +325,7 @@ describe('builder: directory sharding', () => {
325325
if (!index) {
326326
// first dir
327327
if (depth === 1) {
328-
expect(path).to.equal(dir.cid.toBaseEncodedString())
328+
expect(path).to.equal(dir.cid.toString())
329329
}
330330

331331
const entry = entries[path]
@@ -363,7 +363,7 @@ describe('builder: directory sharding', () => {
363363
})
364364

365365
it('exports a big dir with subpath', async () => {
366-
const exportHash = rootHash.toBaseEncodedString() + '/big/big/2000'
366+
const exportHash = rootHash.toString() + '/big/big/2000'
367367

368368
const node = await exporter(exportHash, ipld)
369369
expect(node.path).to.equal(exportHash)

packages/ipfs-unixfs-exporter/test/import-export-nested-dir.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ async function recursiveExport (node, path, entries = []) {
126126
function normalizeNode (node) {
127127
return {
128128
path: node.path || '',
129-
multihash: node.cid.toBaseEncodedString()
129+
multihash: node.cid.toString()
130130
}
131131
}
132132

packages/ipfs-unixfs-exporter/test/importer.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function stringifyMh (files) {
4343
return files.map((file) => {
4444
return {
4545
...file,
46-
cid: file.cid.toBaseEncodedString()
46+
cid: file.cid.toString()
4747
}
4848
})
4949
}
@@ -341,7 +341,7 @@ strategies.forEach((strategy) => {
341341
const actualFile = actualFiles[i]
342342

343343
expect(actualFile.path).to.equal(expectedFile.path)
344-
expect(actualFile.cid.toBaseEncodedString('base58btc')).to.equal(expectedFile.cid)
344+
expect(actualFile.cid.toString('base58btc')).to.equal(expectedFile.cid)
345345

346346
if (actualFile.unixfs) {
347347
expect(actualFile.unixfs.type).to.equal(expectedFile.type)
@@ -422,7 +422,7 @@ strategies.forEach((strategy) => {
422422
expect(files.length).to.eql(1)
423423

424424
// always yield empty file node
425-
expect(files[0].cid.toBaseEncodedString()).to.eql('QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH')
425+
expect(files[0].cid.toString()).to.eql('QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH')
426426
})
427427

428428
it('supports more than one root', async () => {

0 commit comments

Comments
 (0)