Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

WIP cat interface core #280

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"flatmap": "0.0.3",
"glob": "^7.0.3",
"ipfs-merkle-dag": "^0.6.0",
"is-ipfs": "^0.2.0",
"multiaddr": "^2.0.0",
"multipart-stream": "^2.0.1",
"ndjson": "^1.4.3",
Expand Down Expand Up @@ -90,4 +91,4 @@
"url": "https://github.com/ipfs/js-ipfs-api/issues"
},
"homepage": "https://github.com/ipfs/js-ipfs-api"
}
}
29 changes: 27 additions & 2 deletions src/api/cat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
'use strict'

const argCommand = require('../cmd-helpers').argCommand
const bs58 = require('bs58')
const isIPFS = require('is-ipfs')
const promisify = require('promisify-es6')

module.exports = (send) => {
return argCommand(send, 'cat')
const cat = promisify((multihash, callback) => {
try {
multihash = cleanMultihash(multihash)
} catch (err) {
return callback(err)
}
send('cat', multihash, null, null, function (err, result) {
if (err) {
return callback(err)
}
return callback(null, result)
})
})
return cat
}

function cleanMultihash (multihash) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is defined in src/api/object.js too. Could you toss it into a module in src/?

if (!isIPFS.multihash(multihash)) {
throw new Error('not valid multihash')
}
if (Buffer.isBuffer(multihash)) {
Copy link
Contributor

@hackergrrl hackergrrl May 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't input allowed to be a Buffer containing a base58-encoded multihash? If so, this would double b58-encode it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input is allowed to be a buffer for js-ipfs core, but the go-ipfs and js-ipfs-api could only take bs58 encoded strings of multihashes so i use this check to make sure that if a buffer multihash is supplied, then it is converted to a format everyone can use, bs58 encoded string.

It shouldn't double multihash

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linked to doc says "the raw Buffer of the multihash (or of and encoded version)". Did I misunderstand what "of the encoded version" means? It means "base58 encoded", right?

Underyling assumption: this cat method is supposed to satisfy interface-ipfs-core's cat method. Is this correct?

Copy link
Contributor Author

@nginnever nginnever May 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linked to doc says "the raw Buffer of the multihash (or of and encoded version)". Did I misunderstand what "of the encoded version" means? It means "base58 encoded", right?

I don't think the encoding mention on raw Buffer is base58, this is borrowed from the object.get definition of a multihash input. The string mentioned after it is the base58 encoded multihash

Underyling assumption: this cat method is supposed to satisfy interface-ipfs-core's cat method. Is this correct?

Right, this upgrade to cat will now take a buffer input so that it is interop with interface-core

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I've misunderstood? In "the raw Buffer of the multihash (or of encoded version)" -- what encoding does "the raw Buffer" have vs "encoded version"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the readme in interface-core, hopefully that simplifies the statement. A buffer is just a node.js default utf-8 buffer of a multihash, and a Qm... string is base58 encoded.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YES SO AWESOME

return bs58.encode(multihash)
}
return multihash
}
17 changes: 17 additions & 0 deletions test/api/file.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-env mocha */
/* globals apiClients */

'use strict'

const test = require('interface-ipfs-core')

const common = {
setup: function (cb) {
cb(null, apiClients.a)
},
teardown: function (cb) {
cb()
}
}

test.files(common)