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

Commit 0b32373

Browse files
committed
Merge pull request #220 from ipfs/fix/object-patch
Fix/object patch
2 parents 2938470 + a185c92 commit 0b32373

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1172
-437
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ node_js:
44
- '5'
55
- stable
66

7+
addons:
8+
firefox: 'latest'
9+
710
before_script:
811
- export DISPLAY=:99.0
912
- sh -e /etc/init.d/xvfb start

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ If you omit the host and port, the api will parse `window.host`, and use this in
4949
var ipfs = window.ipfsAPI()
5050
```
5151

52+
### Using Promises
53+
54+
If you do not pass in a callback all api functions will return a `Promise`, for example
55+
56+
```js
57+
ipfs.id()
58+
.then(function (id) {
59+
console.log('my id is: ', id)
60+
})
61+
```
62+
63+
This relies on a global `Promise` object. If you are in an environemnt where that is not
64+
yet available you need to bring your own polyfill.
65+
5266
#### Gotchas
5367

5468
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).

dist/ipfsapi.js

+464-236
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ipfsapi.min.js

+12-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const runSequence = require('run-sequence')
55

66
require('require-dir')('tasks')
77

8-
gulp.task('default', done => {
8+
gulp.task('default', (done) => {
99
runSequence(
1010
'lint',
1111
'test',

package.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ipfs-api",
3-
"version": "2.11.0",
3+
"version": "2.13.1",
44
"description": "A client library for the IPFS API",
55
"main": "src/index.js",
66
"dependencies": {
@@ -22,19 +22,21 @@
2222
},
2323
"devDependencies": {
2424
"babel-core": "^6.1.21",
25-
"babel-eslint": "^5.0.0-beta6",
25+
"babel-eslint": "^5.0.0-beta9",
2626
"babel-loader": "^6.2.0",
2727
"babel-plugin-transform-runtime": "^6.1.18",
2828
"babel-preset-es2015": "^6.0.15",
2929
"babel-runtime": "^6.3.19",
3030
"chai": "^3.4.1",
3131
"concurrently": "^1.0.0",
32-
"eslint-config-standard": "^4.4.0",
32+
"eslint": "^2.0.0-rc.0",
33+
"eslint-config-standard": "^5.1.0",
34+
"eslint-plugin-promise": "^1.0.8",
3335
"eslint-plugin-standard": "^1.3.1",
3436
"glob-stream": "5.3.1",
3537
"gulp": "^3.9.0",
3638
"gulp-bump": "^1.0.0",
37-
"gulp-eslint": "^1.0.0",
39+
"gulp-eslint": "^2.0.0-rc-3",
3840
"gulp-filter": "^3.0.1",
3941
"gulp-git": "^1.6.0",
4042
"gulp-load-plugins": "^1.0.0",
@@ -43,7 +45,7 @@
4345
"gulp-tag-version": "^1.3.0",
4446
"gulp-util": "^3.0.7",
4547
"https-browserify": "0.0.1",
46-
"ipfsd-ctl": "^0.8.0",
48+
"ipfsd-ctl": "^0.8.1",
4749
"json-loader": "^0.5.3",
4850
"karma": "^0.13.11",
4951
"karma-chrome-launcher": "^0.2.1",

src/api/add.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Wreck = require('wreck')
22

3-
module.exports = send => {
3+
module.exports = (send) => {
44
return function add (files, opts, cb) {
55
if (typeof (opts) === 'function' && cb === undefined) {
66
cb = opts

src/api/block.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return {
77
get: argCommand(send, 'block/get'),
88
stat: argCommand(send, 'block/stat'),

src/api/cat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return argCommand(send, 'cat')
77
}

src/api/commands.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return command(send, 'commands')
77
}

src/api/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return {
77
get: argCommand(send, 'config'),
88
set (key, value, opts, cb) {

src/api/dht.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return {
77
findprovs: argCommand(send, 'dht/findprovs'),
88
get (key, opts, cb) {
@@ -11,23 +11,38 @@ module.exports = send => {
1111
opts = null
1212
}
1313

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

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

2424
if (res.Type === 5) {
25-
cb(null, res.Extra)
25+
done(null, res.Extra)
2626
} else {
2727
let error = new Error('key was not found (type 6)')
28-
cb(error)
28+
done(error)
2929
}
30-
})
30+
}
31+
32+
if (typeof cb !== 'function' && typeof Promise !== 'undefined') {
33+
const done = (err, res) => {
34+
if (err) throw err
35+
return res
36+
}
37+
38+
return send('dht/get', key, opts)
39+
.then(
40+
(res) => handleResult(done, null, res),
41+
(err) => handleResult(done, err)
42+
)
43+
}
44+
45+
return send('dht/get', key, opts, null, handleResult.bind(null, cb))
3146
},
3247
put (key, value, opts, cb) {
3348
if (typeof (opts) === 'function' && !cb) {

src/api/diag.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return {
77
net: command(send, 'diag/net'),
88
sys: command(send, 'diag/sys')

src/api/files.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return {
77
cp: argCommand(send, 'files/cp'),
88
ls: argCommand(send, 'files/ls'),

src/api/id.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict'
22

3-
module.exports = send => {
4-
return function id (id, cb) {
5-
if (typeof id === 'function') {
6-
cb = id
7-
id = null
3+
module.exports = (send) => {
4+
return function id (idParam, cb) {
5+
if (typeof idParam === 'function') {
6+
cb = idParam
7+
idParam = null
88
}
9-
return send('id', id, null, null, cb)
9+
return send('id', idParam, null, null, cb)
1010
}
1111
}

src/api/log.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
const ndjson = require('ndjson')
44

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return {
77
tail (cb) {
8+
if (typeof cb !== 'function' && typeof Promise !== 'undefined') {
9+
return send('log/tail', null, {}, null, false)
10+
.then((res) => res.pipe(ndjson.parse()))
11+
}
12+
813
return send('log/tail', null, {}, null, false, (err, res) => {
914
if (err) return cb(err)
1015
cb(null, res.pipe(ndjson.parse()))

src/api/ls.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return argCommand(send, 'ls')
77
}

src/api/mount.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
module.exports = send => {
3+
module.exports = (send) => {
44
return function mount (ipfs, ipns, cb) {
55
if (typeof ipfs === 'function') {
66
cb = ipfs

src/api/name.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return {
77
publish: argCommand(send, 'name/publish'),
88
resolve: argCommand(send, 'name/resolve')

src/api/object.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return {
77
get: argCommand(send, 'object/get'),
88
put (file, encoding, cb) {
@@ -15,8 +15,19 @@ module.exports = send => {
1515
links: argCommand(send, 'object/links'),
1616
stat: argCommand(send, 'object/stat'),
1717
new: argCommand(send, 'object/new'),
18-
patch (file, opts, cb) {
19-
return send('object/patch', [file].concat(opts), null, null, cb)
18+
patch: {
19+
rmLink: (root, link, cb) => {
20+
return send('object/patch/rm-link', [root, link], null, null, cb)
21+
},
22+
setData: (root, data, cb) => {
23+
return send('object/patch/set-data', [root], null, data, cb)
24+
},
25+
appendData: (root, data, cb) => {
26+
return send('object/patch/append-data', [root], null, data, cb)
27+
},
28+
addLink: (root, name, ref, cb) => {
29+
return send('object/patch/add-link', [root, name, ref], null, null, cb)
30+
}
2031
}
2132
}
2233
}

src/api/pin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
module.exports = send => {
3+
module.exports = (send) => {
44
return {
55
add (hash, opts, cb) {
66
if (typeof opts === 'function') {

src/api/ping.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
'use strict'
22

3-
module.exports = send => {
3+
module.exports = (send) => {
44
return function ping (id, cb) {
5+
if (typeof cb !== 'function' && typeof Promise !== 'undefined') {
6+
return send('ping', id, {n: 1}, null)
7+
.then((res) => res[1])
8+
}
9+
510
return send('ping', id, { n: 1 }, null, function (err, res) {
611
if (err) return cb(err, null)
712
cb(null, res[1])

src/api/refs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
const refs = cmds.argCommand(send, 'refs')
77
refs.local = cmds.command(send, 'refs/local')
88

src/api/swarm.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return {
77
peers: cmds.command(send, 'swarm/peers'),
88
connect: cmds.argCommand(send, 'swarm/connect')

src/api/update.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return {
77
apply: command(send, 'update'),
88
check: command(send, 'update/check'),

src/api/version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

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

5-
module.exports = send => {
5+
module.exports = (send) => {
66
return command(send, 'version')
77
}

src/get-files-stream.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function loadPaths (opts, file) {
4646
follow: followSymlinks
4747
})
4848

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

7474
const mp = new Multipart()
7575

76-
flatmap(files, file => {
76+
flatmap(files, (file) => {
7777
if (typeof file === 'string') {
7878
if (!isNode) {
7979
throw new Error('Can not add paths in node')
@@ -91,7 +91,7 @@ function getFilesStream (files, opts) {
9191
dir: false,
9292
content: file
9393
}
94-
}).forEach(file => {
94+
}).forEach((file) => {
9595
mp.addPart({
9696
headers: headers(file),
9797
body: file.content

src/load-commands.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function loadCommands (send) {
2929
const files = requireCommands()
3030
const cmds = {}
3131

32-
Object.keys(files).forEach(file => {
32+
Object.keys(files).forEach((file) => {
3333
cmds[file] = files[file](send)
3434
})
3535

0 commit comments

Comments
 (0)