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

Commit

Permalink
Merge pull request #220 from ipfs/fix/object-patch
Browse files Browse the repository at this point in the history
Fix/object patch
  • Loading branch information
daviddias committed Feb 26, 2016
2 parents 2938470 + a185c92 commit 0b32373
Show file tree
Hide file tree
Showing 52 changed files with 1,172 additions and 437 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ node_js:
- '5'
- stable

addons:
firefox: 'latest'

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ If you omit the host and port, the api will parse `window.host`, and use this in
var ipfs = window.ipfsAPI()
```

### Using Promises

If you do not pass in a callback all api functions will return a `Promise`, for example

```js
ipfs.id()
.then(function (id) {
console.log('my id is: ', id)
})
```

This relies on a global `Promise` object. If you are in an environemnt where that is not
yet available you need to bring your own polyfill.

#### Gotchas

When using the api from script tag for things that require buffers (`ipfs.add`, for example), you will have to use either the exposed `ipfs.Buffer`, that works just like a node buffer, or use this [browser buffer](https://github.com/feross/buffer).
Expand Down
700 changes: 464 additions & 236 deletions dist/ipfsapi.js

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions dist/ipfsapi.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const runSequence = require('run-sequence')

require('require-dir')('tasks')

gulp.task('default', done => {
gulp.task('default', (done) => {
runSequence(
'lint',
'test',
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ipfs-api",
"version": "2.11.0",
"version": "2.13.1",
"description": "A client library for the IPFS API",
"main": "src/index.js",
"dependencies": {
Expand All @@ -22,19 +22,21 @@
},
"devDependencies": {
"babel-core": "^6.1.21",
"babel-eslint": "^5.0.0-beta6",
"babel-eslint": "^5.0.0-beta9",
"babel-loader": "^6.2.0",
"babel-plugin-transform-runtime": "^6.1.18",
"babel-preset-es2015": "^6.0.15",
"babel-runtime": "^6.3.19",
"chai": "^3.4.1",
"concurrently": "^1.0.0",
"eslint-config-standard": "^4.4.0",
"eslint": "^2.0.0-rc.0",
"eslint-config-standard": "^5.1.0",
"eslint-plugin-promise": "^1.0.8",
"eslint-plugin-standard": "^1.3.1",
"glob-stream": "5.3.1",
"gulp": "^3.9.0",
"gulp-bump": "^1.0.0",
"gulp-eslint": "^1.0.0",
"gulp-eslint": "^2.0.0-rc-3",
"gulp-filter": "^3.0.1",
"gulp-git": "^1.6.0",
"gulp-load-plugins": "^1.0.0",
Expand All @@ -43,7 +45,7 @@
"gulp-tag-version": "^1.3.0",
"gulp-util": "^3.0.7",
"https-browserify": "0.0.1",
"ipfsd-ctl": "^0.8.0",
"ipfsd-ctl": "^0.8.1",
"json-loader": "^0.5.3",
"karma": "^0.13.11",
"karma-chrome-launcher": "^0.2.1",
Expand Down
2 changes: 1 addition & 1 deletion src/api/add.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Wreck = require('wreck')

module.exports = send => {
module.exports = (send) => {
return function add (files, opts, cb) {
if (typeof (opts) === 'function' && cb === undefined) {
cb = opts
Expand Down
2 changes: 1 addition & 1 deletion src/api/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const argCommand = require('../cmd-helpers').argCommand

module.exports = send => {
module.exports = (send) => {
return {
get: argCommand(send, 'block/get'),
stat: argCommand(send, 'block/stat'),
Expand Down
2 changes: 1 addition & 1 deletion src/api/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

const argCommand = require('../cmd-helpers').argCommand

module.exports = send => {
module.exports = (send) => {
return argCommand(send, 'cat')
}
2 changes: 1 addition & 1 deletion src/api/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

const command = require('../cmd-helpers').command

module.exports = send => {
module.exports = (send) => {
return command(send, 'commands')
}
2 changes: 1 addition & 1 deletion src/api/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const argCommand = require('../cmd-helpers').argCommand

module.exports = send => {
module.exports = (send) => {
return {
get: argCommand(send, 'config'),
set (key, value, opts, cb) {
Expand Down
31 changes: 23 additions & 8 deletions src/api/dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const argCommand = require('../cmd-helpers').argCommand

module.exports = send => {
module.exports = (send) => {
return {
findprovs: argCommand(send, 'dht/findprovs'),
get (key, opts, cb) {
Expand All @@ -11,23 +11,38 @@ module.exports = send => {
opts = null
}

return send('dht/get', key, opts, null, (err, res) => {
if (err) return cb(err)
if (!res) return cb(new Error('empty response'))
if (res.length === 0) return cb(new Error('no value returned for key'))
const handleResult = (done, err, res) => {
if (err) return done(err)
if (!res) return done(new Error('empty response'))
if (res.length === 0) return done(new Error('no value returned for key'))

// Inconsistent return values in the browser vs node
if (Array.isArray(res)) {
res = res[0]
}

if (res.Type === 5) {
cb(null, res.Extra)
done(null, res.Extra)
} else {
let error = new Error('key was not found (type 6)')
cb(error)
done(error)
}
})
}

if (typeof cb !== 'function' && typeof Promise !== 'undefined') {
const done = (err, res) => {
if (err) throw err
return res
}

return send('dht/get', key, opts)
.then(
(res) => handleResult(done, null, res),
(err) => handleResult(done, err)
)
}

return send('dht/get', key, opts, null, handleResult.bind(null, cb))
},
put (key, value, opts, cb) {
if (typeof (opts) === 'function' && !cb) {
Expand Down
2 changes: 1 addition & 1 deletion src/api/diag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const command = require('../cmd-helpers').command

module.exports = send => {
module.exports = (send) => {
return {
net: command(send, 'diag/net'),
sys: command(send, 'diag/sys')
Expand Down
2 changes: 1 addition & 1 deletion src/api/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const argCommand = require('../cmd-helpers').argCommand

module.exports = send => {
module.exports = (send) => {
return {
cp: argCommand(send, 'files/cp'),
ls: argCommand(send, 'files/ls'),
Expand Down
12 changes: 6 additions & 6 deletions src/api/id.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

module.exports = send => {
return function id (id, cb) {
if (typeof id === 'function') {
cb = id
id = null
module.exports = (send) => {
return function id (idParam, cb) {
if (typeof idParam === 'function') {
cb = idParam
idParam = null
}
return send('id', id, null, null, cb)
return send('id', idParam, null, null, cb)
}
}
7 changes: 6 additions & 1 deletion src/api/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

const ndjson = require('ndjson')

module.exports = send => {
module.exports = (send) => {
return {
tail (cb) {
if (typeof cb !== 'function' && typeof Promise !== 'undefined') {
return send('log/tail', null, {}, null, false)
.then((res) => res.pipe(ndjson.parse()))
}

return send('log/tail', null, {}, null, false, (err, res) => {
if (err) return cb(err)
cb(null, res.pipe(ndjson.parse()))
Expand Down
2 changes: 1 addition & 1 deletion src/api/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

const argCommand = require('../cmd-helpers').argCommand

module.exports = send => {
module.exports = (send) => {
return argCommand(send, 'ls')
}
2 changes: 1 addition & 1 deletion src/api/mount.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

module.exports = send => {
module.exports = (send) => {
return function mount (ipfs, ipns, cb) {
if (typeof ipfs === 'function') {
cb = ipfs
Expand Down
2 changes: 1 addition & 1 deletion src/api/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const argCommand = require('../cmd-helpers').argCommand

module.exports = send => {
module.exports = (send) => {
return {
publish: argCommand(send, 'name/publish'),
resolve: argCommand(send, 'name/resolve')
Expand Down
17 changes: 14 additions & 3 deletions src/api/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const argCommand = require('../cmd-helpers').argCommand

module.exports = send => {
module.exports = (send) => {
return {
get: argCommand(send, 'object/get'),
put (file, encoding, cb) {
Expand All @@ -15,8 +15,19 @@ module.exports = send => {
links: argCommand(send, 'object/links'),
stat: argCommand(send, 'object/stat'),
new: argCommand(send, 'object/new'),
patch (file, opts, cb) {
return send('object/patch', [file].concat(opts), null, null, cb)
patch: {
rmLink: (root, link, cb) => {
return send('object/patch/rm-link', [root, link], null, null, cb)
},
setData: (root, data, cb) => {
return send('object/patch/set-data', [root], null, data, cb)
},
appendData: (root, data, cb) => {
return send('object/patch/append-data', [root], null, data, cb)
},
addLink: (root, name, ref, cb) => {
return send('object/patch/add-link', [root, name, ref], null, null, cb)
}
}
}
}
2 changes: 1 addition & 1 deletion src/api/pin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

module.exports = send => {
module.exports = (send) => {
return {
add (hash, opts, cb) {
if (typeof opts === 'function') {
Expand Down
7 changes: 6 additions & 1 deletion src/api/ping.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use strict'

module.exports = send => {
module.exports = (send) => {
return function ping (id, cb) {
if (typeof cb !== 'function' && typeof Promise !== 'undefined') {
return send('ping', id, {n: 1}, null)
.then((res) => res[1])
}

return send('ping', id, { n: 1 }, null, function (err, res) {
if (err) return cb(err, null)
cb(null, res[1])
Expand Down
2 changes: 1 addition & 1 deletion src/api/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const cmds = require('../cmd-helpers')

module.exports = send => {
module.exports = (send) => {
const refs = cmds.argCommand(send, 'refs')
refs.local = cmds.command(send, 'refs/local')

Expand Down
2 changes: 1 addition & 1 deletion src/api/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const cmds = require('../cmd-helpers')

module.exports = send => {
module.exports = (send) => {
return {
peers: cmds.command(send, 'swarm/peers'),
connect: cmds.argCommand(send, 'swarm/connect')
Expand Down
2 changes: 1 addition & 1 deletion src/api/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const command = require('../cmd-helpers').command

module.exports = send => {
module.exports = (send) => {
return {
apply: command(send, 'update'),
check: command(send, 'update/check'),
Expand Down
2 changes: 1 addition & 1 deletion src/api/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

const command = require('../cmd-helpers').command

module.exports = send => {
module.exports = (send) => {
return command(send, 'version')
}
6 changes: 3 additions & 3 deletions src/get-files-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function loadPaths (opts, file) {
follow: followSymlinks
})

return mg.found.map(name => {
return mg.found.map((name) => {
if (mg.cache[name] === 'FILE') {
return {
path: strip(name, file),
Expand All @@ -73,7 +73,7 @@ function getFilesStream (files, opts) {

const mp = new Multipart()

flatmap(files, file => {
flatmap(files, (file) => {
if (typeof file === 'string') {
if (!isNode) {
throw new Error('Can not add paths in node')
Expand All @@ -91,7 +91,7 @@ function getFilesStream (files, opts) {
dir: false,
content: file
}
}).forEach(file => {
}).forEach((file) => {
mp.addPart({
headers: headers(file),
body: file.content
Expand Down
2 changes: 1 addition & 1 deletion src/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function loadCommands (send) {
const files = requireCommands()
const cmds = {}

Object.keys(files).forEach(file => {
Object.keys(files).forEach((file) => {
cmds[file] = files[file](send)
})

Expand Down
Loading

0 comments on commit 0b32373

Please sign in to comment.