Skip to content

fix: base32 for ipfs:// urls and cidv1 everywhere #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 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
9 changes: 3 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ module.exports = function makeIPFSFetch ({ ipfs }) {
path,
content: body
}, {
cidVersion: 1,
wrapWithDirectory: true
})
const cidHash = cidToString(cid)
const cidHash = cid.toString()
const addedURL = `ipfs://${cidHash}${path}`
return {
statusCode: 200,
Expand Down Expand Up @@ -186,7 +187,7 @@ module.exports = function makeIPFSFetch ({ ipfs }) {
}

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

const nameURL = `ipns://${nameHash}/`
return {
Expand Down Expand Up @@ -250,7 +251,3 @@ function getMimeType (path) {
if (mimeType.startsWith('text/')) mimeType = `${mimeType}; charset=utf-8`
return mimeType
}

function cidToString (cid) {
return cid.toV1().toString('base36')
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"devDependencies": {
"browser-run": "^8.0.0",
"browserify": "^17.0.0",
"ipfs": "^0.50.2",
"ipfs-core": "^0.6.1",
"standard": "^14.3.4",
"tape": "^5.0.1"
}
Expand Down
30 changes: 19 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
global.Buffer = Buffer

const test = require('tape')
const IPFS = require('ipfs')
const IPFS = require('ipfs-core')
const makeIPFSFetch = require('./')

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

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

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

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

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

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

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

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

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

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

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

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

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

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

const cid = await response.text()

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

const fileResponse = await fetch(cid)
const fileResponse = await fetch(ipfsUri)
t.equal(fileResponse.status, 200, 'Got OK in response')

const text = await fileResponse.text()

t.equal(text, TEST_DATA, 'Able to load POSTed file')

const { cid } = await ipfs.add({
path: 'example.txt',
content: TEST_DATA
}, {
cidVersion: 1,
wrapWithDirectory: true
})
t.equal(ipfsUri.match(/ipfs:\/\/([^/]+)/)[1], cid.toString('base32'), 'Matches cid from ipfs.add')
} catch (e) {
t.fail(e.message)
} finally {
Expand Down Expand Up @@ -277,7 +284,8 @@ test('Publish and resolve IPNS', async (t) => {

const ipnsURI = await publishResponse.text()

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

const resolvedResponse = await fetch(ipnsURI, {
headers: {
Expand Down