Skip to content

Commit caaad5b

Browse files
authored
Merge pull request #5 from olizilla/base32-and-cidv1
fix: base32 for ipfs:// urls and cidv1 everywhere
2 parents 631127a + 7c2ba68 commit caaad5b

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

index.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ module.exports = function makeIPFSFetch ({ ipfs }) {
9898
path,
9999
content: body
100100
}, {
101+
cidVersion: 1,
101102
wrapWithDirectory: true
102103
})
103-
const cidHash = cidToString(cid)
104+
const cidHash = cid.toString()
104105
const addedURL = `ipfs://${cidHash}${path}`
105106
return {
106107
statusCode: 200,
@@ -186,7 +187,7 @@ module.exports = function makeIPFSFetch ({ ipfs }) {
186187
}
187188

188189
const { name } = await ipfs.name.publish(value, { name: keyName, signal })
189-
const nameHash = cidToString(new CID(name))
190+
const nameHash = new CID(name).toV1().toString('base36')
190191

191192
const nameURL = `ipns://${nameHash}/`
192193
return {
@@ -250,7 +251,3 @@ function getMimeType (path) {
250251
if (mimeType.startsWith('text/')) mimeType = `${mimeType}; charset=utf-8`
251252
return mimeType
252253
}
253-
254-
function cidToString (cid) {
255-
return cid.toV1().toString('base36')
256-
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"devDependencies": {
3333
"browser-run": "^8.0.0",
3434
"browserify": "^17.0.0",
35-
"ipfs": "^0.50.2",
35+
"ipfs-core": "^0.6.1",
3636
"standard": "^14.3.4",
3737
"tape": "^5.0.1"
3838
}

test.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
global.Buffer = Buffer
22

33
const test = require('tape')
4-
const IPFS = require('ipfs')
4+
const IPFS = require('ipfs-core')
55
const makeIPFSFetch = require('./')
66

77
const TEST_DATA = 'Hello World!'
@@ -20,7 +20,7 @@ test('Load a file via fetch', async (t) => {
2020

2121
t.pass('Able to make create fetch instance')
2222

23-
const { cid } = await ipfs.add(TEST_DATA)
23+
const { cid } = await ipfs.add(TEST_DATA, { cidVersion: 1 })
2424

2525
const response = await fetch(`ipfs://${cid}`)
2626

@@ -55,7 +55,7 @@ test('Load a range from a file', async (t) => {
5555

5656
t.pass('Able to make create fetch instance')
5757

58-
const { cid } = await ipfs.add(TEST_DATA)
58+
const { cid } = await ipfs.add(TEST_DATA, { cidVersion: 1 })
5959

6060
const response = await fetch(`ipfs://${cid}`, { headers: { Range: 'bytes=0-4' } })
6161

@@ -87,7 +87,7 @@ test('Get expected headers from HEAD', async (t) => {
8787

8888
t.pass('Able to make create fetch instance')
8989

90-
const { cid } = await ipfs.add(TEST_DATA)
90+
const { cid } = await ipfs.add(TEST_DATA, { cidVersion: 1 })
9191

9292
const response = await fetch(`ipfs://${cid}`, { method: 'head' })
9393

@@ -171,7 +171,7 @@ test('Resolve index.html from a directory', async (t) => {
171171
const results = await collect(ipfs.addAll([
172172
{ path: '/index.html', content: TEST_DATA },
173173
{ path: 'example/index.html', content: TEST_DATA }
174-
], { wrapWithDirectory: true }))
174+
], { wrapWithDirectory: true, cidVersion: 1 }))
175175

176176
// The last element should be the directory itself
177177
const { cid } = results[results.length - 1]
@@ -236,16 +236,23 @@ test('POST a file into IPFS', async (t) => {
236236
t.ok(response, 'Got a response object')
237237
t.equal(response.status, 200, 'Got OK in response')
238238

239-
const cid = await response.text()
240-
241-
t.match(cid, /ipfs:\/\/\w+\/example.txt/, 'returned IPFS url with CID')
239+
const ipfsUri = await response.text()
240+
t.match(ipfsUri, /ipfs:\/\/\w+\/example.txt/, 'returned IPFS url with CID')
242241

243-
const fileResponse = await fetch(cid)
242+
const fileResponse = await fetch(ipfsUri)
244243
t.equal(fileResponse.status, 200, 'Got OK in response')
245244

246245
const text = await fileResponse.text()
247-
248246
t.equal(text, TEST_DATA, 'Able to load POSTed file')
247+
248+
const { cid } = await ipfs.add({
249+
path: 'example.txt',
250+
content: TEST_DATA
251+
}, {
252+
cidVersion: 1,
253+
wrapWithDirectory: true
254+
})
255+
t.equal(ipfsUri.match(/ipfs:\/\/([^/]+)/)[1], cid.toString('base32'), 'Matches cid from ipfs.add')
249256
} catch (e) {
250257
t.fail(e.message)
251258
} finally {
@@ -277,7 +284,8 @@ test('Publish and resolve IPNS', async (t) => {
277284

278285
const ipnsURI = await publishResponse.text()
279286

280-
t.ok(ipnsURI, 'Got resulting IPNS url')
287+
// base36 prefix is k https://github.com/multiformats/js-multibase/blob/ddd99e6d0d089d5d1209094f2e7a2a07d87729fb/src/constants.js#L43
288+
t.ok(ipnsURI.startsWith('ipns://k'), 'Got base36 encoded IPNS url')
281289

282290
const resolvedResponse = await fetch(ipnsURI, {
283291
headers: {

0 commit comments

Comments
 (0)