From 86542429939c84086c9b1743995cae68510aa6c1 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Sat, 7 Nov 2015 00:35:26 +0100 Subject: [PATCH 1/4] docs: Auto generate API.md using mocha --- API.md | 689 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- tasks/test.js | 19 +- 3 files changed, 708 insertions(+), 2 deletions(-) create mode 100644 API.md diff --git a/API.md b/API.md new file mode 100644 index 000000000..4297888fc --- /dev/null +++ b/API.md @@ -0,0 +1,689 @@ + - running against version 0.3.9 +# TOC + - [IPFS Node.js API wrapper tests](#ipfs-nodejs-api-wrapper-tests) + - [.send](#ipfs-nodejs-api-wrapper-tests-send) + - [.add](#ipfs-nodejs-api-wrapper-tests-add) + - [.cat](#ipfs-nodejs-api-wrapper-tests-cat) + - [.ls](#ipfs-nodejs-api-wrapper-tests-ls) + - [.config](#ipfs-nodejs-api-wrapper-tests-config) + - [.update (currently disabled, wait for IPFS 0.4.0 release](#ipfs-nodejs-api-wrapper-tests-update-currently-disabled-wait-for-ipfs-040-release) + - [.version](#ipfs-nodejs-api-wrapper-tests-version) + - [.commands](#ipfs-nodejs-api-wrapper-tests-commands) + - [.mount](#ipfs-nodejs-api-wrapper-tests-mount) + - [.diag](#ipfs-nodejs-api-wrapper-tests-diag) + - [.block](#ipfs-nodejs-api-wrapper-tests-block) + - [.object](#ipfs-nodejs-api-wrapper-tests-object) + - [.swarm](#ipfs-nodejs-api-wrapper-tests-swarm) + - [.ping](#ipfs-nodejs-api-wrapper-tests-ping) + - [.id](#ipfs-nodejs-api-wrapper-tests-id) + - [.pin](#ipfs-nodejs-api-wrapper-tests-pin) + - [.log](#ipfs-nodejs-api-wrapper-tests-log) + - [.name](#ipfs-nodejs-api-wrapper-tests-name) + - [.refs](#ipfs-nodejs-api-wrapper-tests-refs) + - [.dht](#ipfs-nodejs-api-wrapper-tests-dht) + + + +# IPFS Node.js API wrapper tests +connect Node a to b and c. + +```js +this.timeout(5000) +var addrs = {} +var counter = 0 +collectAddr('b', finish) +collectAddr('c', finish) +function finish () { + counter++ + if (counter === 2) { + dial() + } +} +function collectAddr (key, cb) { + apiClients[key].id(function (err, id) { + if (err) { + throw err + } + // note to self: HTTP API port !== Node port + addrs[key] = id.Addresses[0] + cb() + }) +} +function dial () { + apiClients['a'].swarm.connect(addrs['b'], function (err, res) { + if (err) { + throw err + } + apiClients['a'].swarm.connect(addrs['c'], function (err) { + if (err) { + throw err + } + done() + }) + }) +} +``` + +has the api object. + +```js +assert(apiClients['a']) +assert(apiClients['a'].id) +``` + + +## .send + +## .add +add file. + +```js +if (!isNode) { + return done() +} +this.timeout(10000) +var file = new File({ + cwd: path.dirname(testfilePath), + base: path.dirname(testfilePath), + path: testfilePath, + contents: new Buffer(testfile) +}) +apiClients['a'].add(file, function (err, res) { + if (err) throw err + var added = res[0] != null ? res[0] : res + assert.equal(added.Hash, 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') + assert.equal(added.Name, path.basename(testfilePath)) + done() +}) +``` + +add buffer. + +```js +this.timeout(10000) +var buf = new Buffer(testfile) +apiClients['a'].add(buf, function (err, res) { + if (err) throw err + // assert.equal(res.length, 1) + var added = res[0] != null ? res[0] : res + assert.equal(added.Hash, 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') + done() +}) +``` + +add path. + +```js +if (!isNode) { + return done() +} +this.timeout(10000) +apiClients['a'].add(testfilePath, function (err, res) { + if (err) throw err + var added = res[0] != null ? res[0] : res + assert.equal(added.Hash, 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') + done() +}) +``` + +add a nested dir. + +```js +this.timeout(10000) +apiClients['a'].add(__dirname + '/test-folder', { recursive: true }, function (err, res) { + if (isNode) { + if (err) throw err + var added = res[res.length - 1] + assert.equal(added.Hash, 'QmZdsefMGMeG6bL719gX44XSVQrL6psEgRZdw1SGadFaK2') + done() + } else { + assert.equal(err.message, 'Recursive uploads are not supported in the browser') + done() + } +}) +``` + + +## .cat +cat. + +```js +this.timeout(10000) +apiClients['a'].cat('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', function (err, res) { + if (err) { + throw err + } + if (typeof res === 'string') { + // Just a string + assert.equal(res, testfile) + done() + return + } + var buf = '' + res + .on('error', function (err) { throw err }) + .on('data', function (data) { buf += data }) + .on('end', function () { + assert.equal(buf, testfile) + done() + }) +}) +``` + + +## .ls +ls. + +```js +if (!isNode) { + return done() +} +this.timeout(100000) +apiClients['a'].ls(folder, function (err, res) { + if (err) { + throw err + } + var objs = { + Hash: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q', + Links: [{ + Name: 'add.js', + Hash: 'QmaeuuKLHzirbVoTjb3659fyyV381amjaGrU2pecHEWPrN', + Size: 481, + Type: 2 + }, { + Name: 'cat.js', + Hash: 'QmTQhTtDWeaaP9pttDd1CuoVTLQm1w51ABfjgmGUbCUF6i', + Size: 364, + Type: 2 + }, { + Name: 'files', + Hash: 'QmTYFLz5vsdMpq4XXw1a1pSxujJc9Z5V3Aw1Qg64d849Zy', + Size: 132, + Type: 1 + }, { + Name: 'ipfs-add.js', + Hash: 'QmTjXxUemcuMAZ2KNN3iJGWHwrkMsW8SWEwkYVSBi1nFD9', + Size: 315, + Type: 2 + }, { + Name: 'ls.js', + Hash: 'QmXYUXDFNNh1wgwtX5QDG7MsuhAAcE9NzDYnz8SjnhvQrK', + Size: 428, + Type: 2 + }, { + Name: 'version.js', + Hash: 'QmUmDmH4hZgN5THnVP1VjJ1YWh5kWuhLGUihch8nFiD9iy', + Size: 153, + Type: 2 }] + } + assert.deepEqual(res.Objects[0], objs) + done() +}) +``` + + +## .config +.config.{set, get}. + +```js +this.timeout(10000) +var confKey = 'arbitraryKey' +var confVal = 'arbitraryVal' +apiClients['a'].config.set(confKey, confVal, function (err, res) { + if (err) throw err + apiClients['a'].config.get(confKey, function (err, res) { + if (err) throw err + assert.equal(res.Value, confVal) + done() + }) +}) +``` + +.config.show. + +```js +this.timeout(10000) +apiClients['c'].config.show(function (err, res) { + if (err) { + throw err + } + assert(res) + done() +}) +``` + +.config.replace. + +```js +this.timeout(10000) +if (!isNode) { + return done() +} +apiClients['c'].config.replace(__dirname + '/r-config.json', function (err, res) { + if (err) { + throw err + } + assert.equal(res, '') + done() +}) +``` + + +## .update (currently disabled, wait for IPFS 0.4.0 release + +## .version +checks the version. + +```js +this.timeout(10000) +apiClients['a'].version(function (err, res) { + if (err) { + throw err + } + assert(res) + assert(res.Version) + console.log(' - running against version', res.Version) + done() +}) +``` + + +## .commands +lists commands. + +```js +this.timeout(10000) +apiClients['a'].commands(function (err, res) { + if (err) { + throw err + } + assert(res) + done() +}) +``` + + +## .diag +.diag.net. + +```js +this.timeout(1000000) +apiClients['a'].diag.net(function (err, res) { + if (err) { + throw err + } + assert(res) + done() +}) +``` + + +## .block +block.put. + +```js +this.timeout(10000) +apiClients['a'].block.put(blorb, function (err, res) { + if (err) throw err + var store = res.Key + assert.equal(store, 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ') + done() +}) +``` + +block.get. + +```js +this.timeout(10000) +apiClients['a'].block.get(blorbKey, function (err, res) { + if (err) throw err + if (typeof res === 'string') { + // Just a string + assert.equal(res, 'blorb') + done() + return + } + var buf = '' + res + .on('data', function (data) { buf += data }) + .on('end', function () { + assert.equal(buf, 'blorb') + done() + }) +}) +``` + + +## .object +object.put. + +```js +apiClients['a'].object.put(testObject, 'json', function (err, res) { + if (err) throw err + var obj = res + assert.equal(obj.Hash, testObjectHash) + assert.equal(obj.Links.length, 0) + done() +}) +``` + +object.get. + +```js +apiClients['a'].object.get(testObjectHash, function (err, res) { + if (err) { + throw err + } + var obj = res + assert.equal(obj.Data, 'testdata') + assert.equal(obj.Links.length, 0) + done() +}) +``` + +object.data. + +```js +this.timeout(10000) +apiClients['a'].object.data(testObjectHash, function (err, res) { + if (err) throw err + if (typeof res === 'string') { + // Just a string + assert.equal(res, 'testdata') + done() + return + } + var buf = '' + res + .on('error', function (err) { throw err }) + .on('data', function (data) { buf += data }) + .on('end', function () { + assert.equal(buf, 'testdata') + done() + }) +}) +``` + +object.stat. + +```js +this.timeout(10000) +apiClients['a'].object.stat(testObjectHash, function (err, res) { + if (err) { + throw err + } + assert.deepEqual(res, { + Hash: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD', + NumLinks: 0, + BlockSize: 10, + LinksSize: 2, + DataSize: 8, + CumulativeSize: 10 + }) + done() +}) +``` + +object.links. + +```js +this.timeout(10000) +apiClients['a'].object.links(testObjectHash, function (err, res) { + if (err) { + throw err + } + assert.deepEqual(res, { + Hash: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD', + Links: [] + }) + done() +}) +``` + +object.patch. + +```js +this.timeout(10000) +apiClients['a'].object.put(testPatchObject, 'json', function (err, res) { + if (err) { + throw err + } + apiClients['a'].object.patch(testObjectHash, ['add-link', 'next', testPatchObjectHash], function (err, res) { + if (err) { + throw err + } + var o = JSON.parse(res) + assert.deepEqual(o, { + Hash: 'QmZFdJ3CQsY4kkyQtjoUo8oAzsEs5BNguxBhp8sjQMpgkd', + Links: null + }) + apiClients['a'].object.get(o.Hash, function (err, res) { + if (err) { + throw err + } + assert.deepEqual(JSON.parse(res), { + Data: 'testdata', + Links: [{ + Name: 'next', + Hash: 'QmWJDtdQWQSajQPx1UVAGWKaSGrHVWdjnrNhbooHP7LuF2', + Size: 15 + }] + }) + done() + }) + }) +}) +``` + + +## .swarm +.swarm.peers. + +```js +this.timeout(5000) +apiClients['a'].swarm.peers(function (err, res) { + if (err) { + throw err + } + assert(res.Strings.length >= 2) + done() +}) +``` + +.swarm.connect. + +```js +// Done in the 'before' segment +done() +``` + + +## .ping +ping another peer. + +```js +apiClients['b'].id(function (err, id) { + if (err) { + throw err + } + apiClients['a'].ping(id.ID, function (err, res) { + if (err) { + throw err + } + assert(res) + assert(res.Success) + done() + }) +}) +``` + + +## .id +id. + +```js +this.timeout(10000) +apiClients['a'].id(function (err, res) { + if (err) throw err + var id = res + assert(id.ID) + assert(id.PublicKey) + done() +}) +``` + + +## .pin +.pin.add. + +```js +this.timeout(5000) +apiClients['b'].pin.add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}, function (err, res) { + if (err) { + throw err + } + assert.equal(res.Pinned[0], 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') + done() +}) +``` + +.pin.list. + +```js +this.timeout(5000) +apiClients['b'].pin.list(function (err, res) { + if (err) { + throw err + } + assert(res) + done() +}) +``` + +.pin.remove. + +```js +this.timeout(5000) +apiClients['b'].pin.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}, function (err, res) { + if (err) { + throw err + } + assert(res) + apiClients['b'].pin.list('direct', function (err, res) { + if (err) { + throw err + } + assert(res) + assert.equal(Object.keys(res.Keys).length, 0) + done() + }) +}) +``` + + +## .log + +## .name +.name.publish. + +```js +apiClients['a'].name.publish('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', function (err, res) { + if (err) { + throw err + } + assert(res) + name = res + done() +}) +``` + +.name.resolve. + +```js +apiClients['a'].name.resolve(name.Name, function (err, res) { + if (err) { + throw err + } + assert(res) + assert.deepEqual(res, { Path: '/ipfs/' + name.Value }) + done() +}) +``` + + +## .refs +refs. + +```js +if (!isNode) { + return done() +} +this.timeout(10000) +apiClients['a'].refs(folder, {'format': ' '}, function (err, objs) { + if (err) { + throw err + } + var result = [{ + Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmaeuuKLHzirbVoTjb3659fyyV381amjaGrU2pecHEWPrN add.js', + Err: '' }, + { Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmTQhTtDWeaaP9pttDd1CuoVTLQm1w51ABfjgmGUbCUF6i cat.js', + Err: '' }, + { Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmTYFLz5vsdMpq4XXw1a1pSxujJc9Z5V3Aw1Qg64d849Zy files', + Err: '' }, + { Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmTjXxUemcuMAZ2KNN3iJGWHwrkMsW8SWEwkYVSBi1nFD9 ipfs-add.js', + Err: '' }, + { Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmXYUXDFNNh1wgwtX5QDG7MsuhAAcE9NzDYnz8SjnhvQrK ls.js', + Err: '' }, + { Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmUmDmH4hZgN5THnVP1VjJ1YWh5kWuhLGUihch8nFiD9iy version.js', + Err: '' } ] + assert.deepEqual(objs, result) + done() +}) +``` + + +## .dht +returns an error when getting a non-existent key from the DHT. + +```js +this.timeout(20000) +apiClients['a'].dht.get('non-existent', {timeout: '100ms'}, function (err, value) { + assert(err) + done() +}) +``` + +puts and gets a key value pair in the DHT. + +```js +this.timeout(20000) +apiClients['a'].dht.put('scope', 'interplanetary', function (err, res) { + if (err) { + throw err + } + return done() + // non ipns or pk hashes fail to fetch, known bug + // bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234 + // apiClients['a'].dht.get('scope', function (err, value) { + // console.log('->>', err, value) + // if (err) { + // throw err + // } + // assert.equal(value, 'interplanetary') + // done() + // }) +}) +``` + +.dht.findprovs. + +```js +apiClients['a'].dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', function (err, res) { + if (err) { + throw err + } + assert(res) + done() +}) +``` + diff --git a/package.json b/package.json index 51bba7808..9808e4703 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,8 @@ "gulp": "^3.9.0", "gulp-eslint": "^1.0.0", "gulp-load-plugins": "^1.0.0", - "gulp-mocha": "^2.1.3", "gulp-size": "^2.0.0", + "gulp-spawn-mocha": "^2.2.1", "gulp-util": "^3.0.7", "https-browserify": "0.0.1", "ipfsd-ctl": "^0.6.1", diff --git a/tasks/test.js b/tasks/test.js index 1e4ac8b07..e1024355f 100644 --- a/tasks/test.js +++ b/tasks/test.js @@ -24,6 +24,15 @@ gulp.task('test:node', done => { ) }) +gulp.task('docs', done => { + runSequence( + 'daemons:start', + 'mocha:docs', + 'daemons:stop', + done + ) +}) + gulp.task('test:browser', done => { runSequence( 'daemons:start', @@ -35,7 +44,15 @@ gulp.task('test:browser', done => { gulp.task('mocha', () => { return gulp.src('test/tests.js') - .pipe($.mocha()) + .pipe($.spawnMocha()) +}) + +gulp.task('mocha:docs', function () { + return gulp.src('test/tests.js') + .pipe($.spawnMocha({ + reporter: 'markdown', + output: 'API.md' + })) }) gulp.task('karma', done => { From f79c70a0d5ee643c5aa058c4f3a3f43ea548945b Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Wed, 18 Nov 2015 16:28:27 +0100 Subject: [PATCH 2/4] Update api --- API.md | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/API.md b/API.md index 4297888fc..2cd07c1a1 100644 --- a/API.md +++ b/API.md @@ -143,6 +143,34 @@ apiClients['a'].add(__dirname + '/test-folder', { recursive: true }, function (e }) ``` +add stream. + +```js +this.timeout(10000) +var stream = new Readable() +stream.push('Hello world') +stream.push(null) +apiClients['a'].add(stream, function (err, res) { + if (err) throw err + var added = res[0] != null ? res[0] : res + assert.equal(added.Hash, 'QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve') + done() +}) +``` + +add url. + +```js +this.timeout(10000) +var url = 'https://raw.githubusercontent.com/ipfs/js-ipfs-api/2a9cc63d7427353f2145af6b1a768a69e67c0588/README.md' +apiClients['a'].add(url, function (err, res) { + if (err) throw err + var added = res[0] != null ? res[0] : res + assert.equal(added.Hash, 'QmZmHgEX9baxUn3qMjsEXQzG6DyNcrVnwieQQTrpDdrFvt') + done() +}) +``` + ## .cat cat. @@ -452,16 +480,15 @@ apiClients['a'].object.put(testPatchObject, 'json', function (err, res) { if (err) { throw err } - var o = JSON.parse(res) - assert.deepEqual(o, { + assert.deepEqual(res, { Hash: 'QmZFdJ3CQsY4kkyQtjoUo8oAzsEs5BNguxBhp8sjQMpgkd', Links: null }) - apiClients['a'].object.get(o.Hash, function (err, res) { + apiClients['a'].object.get(res.Hash, function (err, res2) { if (err) { throw err } - assert.deepEqual(JSON.parse(res), { + assert.deepEqual(res2, { Data: 'testdata', Links: [{ Name: 'next', From 10f3764bf8e8bd05edc3527e75a87b8769acdd05 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Thu, 19 Nov 2015 13:33:32 +0100 Subject: [PATCH 3/4] chore: build docs --- API.md | 218 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 116 insertions(+), 102 deletions(-) diff --git a/API.md b/API.md index 2cd07c1a1..4c670beb9 100644 --- a/API.md +++ b/API.md @@ -29,8 +29,8 @@ connect Node a to b and c. ```js this.timeout(5000) -var addrs = {} -var counter = 0 +const addrs = {} +let counter = 0 collectAddr('b', finish) collectAddr('c', finish) function finish () { @@ -40,7 +40,7 @@ function finish () { } } function collectAddr (key, cb) { - apiClients[key].id(function (err, id) { + apiClients[key].id((err, id) => { if (err) { throw err } @@ -50,11 +50,11 @@ function collectAddr (key, cb) { }) } function dial () { - apiClients['a'].swarm.connect(addrs['b'], function (err, res) { + apiClients['a'].swarm.connect(addrs['b'], (err, res) => { if (err) { throw err } - apiClients['a'].swarm.connect(addrs['c'], function (err) { + apiClients['a'].swarm.connect(addrs['c'], err => { if (err) { throw err } @@ -82,15 +82,15 @@ if (!isNode) { return done() } this.timeout(10000) -var file = new File({ +const file = new File({ cwd: path.dirname(testfilePath), base: path.dirname(testfilePath), path: testfilePath, contents: new Buffer(testfile) }) -apiClients['a'].add(file, function (err, res) { +apiClients['a'].add(file, (err, res) => { if (err) throw err - var added = res[0] != null ? res[0] : res + const added = res[0] != null ? res[0] : res assert.equal(added.Hash, 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') assert.equal(added.Name, path.basename(testfilePath)) done() @@ -101,11 +101,11 @@ add buffer. ```js this.timeout(10000) -var buf = new Buffer(testfile) -apiClients['a'].add(buf, function (err, res) { +let buf = new Buffer(testfile) +apiClients['a'].add(buf, (err, res) => { if (err) throw err // assert.equal(res.length, 1) - var added = res[0] != null ? res[0] : res + const added = res[0] != null ? res[0] : res assert.equal(added.Hash, 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') done() }) @@ -118,9 +118,9 @@ if (!isNode) { return done() } this.timeout(10000) -apiClients['a'].add(testfilePath, function (err, res) { +apiClients['a'].add(testfilePath, (err, res) => { if (err) throw err - var added = res[0] != null ? res[0] : res + const added = res[0] != null ? res[0] : res assert.equal(added.Hash, 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') done() }) @@ -130,11 +130,11 @@ add a nested dir. ```js this.timeout(10000) -apiClients['a'].add(__dirname + '/test-folder', { recursive: true }, function (err, res) { +apiClients['a'].add(__dirname + '/test-folder', { recursive: true }, (err, res) => { if (isNode) { if (err) throw err - var added = res[res.length - 1] - assert.equal(added.Hash, 'QmZdsefMGMeG6bL719gX44XSVQrL6psEgRZdw1SGadFaK2') + const added = res[res.length - 1] + assert.equal(added.Hash, 'QmSzLpCVbWnEm3XoTWnv6DT6Ju5BsVoLhzvxKXZeQ2cmdg') done() } else { assert.equal(err.message, 'Recursive uploads are not supported in the browser') @@ -147,12 +147,12 @@ add stream. ```js this.timeout(10000) -var stream = new Readable() +const stream = new Readable() stream.push('Hello world') stream.push(null) -apiClients['a'].add(stream, function (err, res) { +apiClients['a'].add(stream, (err, res) => { if (err) throw err - var added = res[0] != null ? res[0] : res + const added = res[0] != null ? res[0] : res assert.equal(added.Hash, 'QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve') done() }) @@ -162,10 +162,10 @@ add url. ```js this.timeout(10000) -var url = 'https://raw.githubusercontent.com/ipfs/js-ipfs-api/2a9cc63d7427353f2145af6b1a768a69e67c0588/README.md' -apiClients['a'].add(url, function (err, res) { +const url = 'https://raw.githubusercontent.com/ipfs/js-ipfs-api/2a9cc63d7427353f2145af6b1a768a69e67c0588/README.md' +apiClients['a'].add(url, (err, res) => { if (err) throw err - var added = res[0] != null ? res[0] : res + const added = res[0] != null ? res[0] : res assert.equal(added.Hash, 'QmZmHgEX9baxUn3qMjsEXQzG6DyNcrVnwieQQTrpDdrFvt') done() }) @@ -177,21 +177,21 @@ cat. ```js this.timeout(10000) -apiClients['a'].cat('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', function (err, res) { +apiClients['a'].cat('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => { if (err) { throw err } - if (typeof res === 'string') { + if (!res.on) { // Just a string - assert.equal(res, testfile) + assert.equal(res.toString(), testfile) done() return } - var buf = '' + let buf = '' res - .on('error', function (err) { throw err }) - .on('data', function (data) { buf += data }) - .on('end', function () { + .on('error', err => { throw err }) + .on('data', data => buf += data) + .on('end', () => { assert.equal(buf, testfile) done() }) @@ -207,21 +207,21 @@ if (!isNode) { return done() } this.timeout(100000) -apiClients['a'].ls(folder, function (err, res) { +apiClients['a'].ls(folder, (err, res) => { if (err) { throw err } - var objs = { - Hash: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q', + const objs = { + Hash: 'QmSzLpCVbWnEm3XoTWnv6DT6Ju5BsVoLhzvxKXZeQ2cmdg', Links: [{ Name: 'add.js', - Hash: 'QmaeuuKLHzirbVoTjb3659fyyV381amjaGrU2pecHEWPrN', - Size: 481, + Hash: 'QmcUYKmQxmTcFom4R4UZP7FWeQzgJkwcFn51XrvsMy7PE9', + Size: 487, Type: 2 }, { Name: 'cat.js', - Hash: 'QmTQhTtDWeaaP9pttDd1CuoVTLQm1w51ABfjgmGUbCUF6i', - Size: 364, + Hash: 'QmNeHxDfQfjVFyYj2iruvysLH9zpp78v3cu1s3BZq1j5hY', + Size: 368, Type: 2 }, { Name: 'files', @@ -230,18 +230,23 @@ apiClients['a'].ls(folder, function (err, res) { Type: 1 }, { Name: 'ipfs-add.js', - Hash: 'QmTjXxUemcuMAZ2KNN3iJGWHwrkMsW8SWEwkYVSBi1nFD9', - Size: 315, + Hash: 'QmU7wetVaAqc3Meurif9hcYBHGvQmL5QdpPJYBoZizyTNL', + Size: 333, Type: 2 }, { Name: 'ls.js', - Hash: 'QmXYUXDFNNh1wgwtX5QDG7MsuhAAcE9NzDYnz8SjnhvQrK', - Size: 428, + Hash: 'QmctZfSuegbi2TMFY2y3VQjxsH5JbRBu7XmiLfHNvshhio', + Size: 432, Type: 2 + }, { + Hash: 'QmTDH2RXGn8XyDAo9YyfbZAUXwL1FCr44YJCN9HBZmL9Gj', + Name: 'test-folder', + Size: 2212, + Type: 1 }, { Name: 'version.js', - Hash: 'QmUmDmH4hZgN5THnVP1VjJ1YWh5kWuhLGUihch8nFiD9iy', - Size: 153, + Hash: 'QmbkMNB6rwfYAxRvnG9CWJ6cKKHEdq2ZKTozyF5FQ7H8Rs', + Size: 155, Type: 2 }] } assert.deepEqual(res.Objects[0], objs) @@ -255,11 +260,11 @@ apiClients['a'].ls(folder, function (err, res) { ```js this.timeout(10000) -var confKey = 'arbitraryKey' -var confVal = 'arbitraryVal' -apiClients['a'].config.set(confKey, confVal, function (err, res) { +const confKey = 'arbitraryKey' +const confVal = 'arbitraryVal' +apiClients['a'].config.set(confKey, confVal, (err, res) => { if (err) throw err - apiClients['a'].config.get(confKey, function (err, res) { + apiClients['a'].config.get(confKey, (err, res) => { if (err) throw err assert.equal(res.Value, confVal) done() @@ -271,7 +276,7 @@ apiClients['a'].config.set(confKey, confVal, function (err, res) { ```js this.timeout(10000) -apiClients['c'].config.show(function (err, res) { +apiClients['c'].config.show((err, res) => { if (err) { throw err } @@ -287,7 +292,7 @@ this.timeout(10000) if (!isNode) { return done() } -apiClients['c'].config.replace(__dirname + '/r-config.json', function (err, res) { +apiClients['c'].config.replace(__dirname + '/r-config.json', (err, res) => { if (err) { throw err } @@ -304,7 +309,7 @@ checks the version. ```js this.timeout(10000) -apiClients['a'].version(function (err, res) { +apiClients['a'].version((err, res) => { if (err) { throw err } @@ -321,7 +326,7 @@ lists commands. ```js this.timeout(10000) -apiClients['a'].commands(function (err, res) { +apiClients['a'].commands((err, res) => { if (err) { throw err } @@ -336,7 +341,7 @@ apiClients['a'].commands(function (err, res) { ```js this.timeout(1000000) -apiClients['a'].diag.net(function (err, res) { +apiClients['a'].diag.net((err, res) => { if (err) { throw err } @@ -351,9 +356,9 @@ block.put. ```js this.timeout(10000) -apiClients['a'].block.put(blorb, function (err, res) { +apiClients['a'].block.put(blorb, (err, res) => { if (err) throw err - var store = res.Key + const store = res.Key assert.equal(store, 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ') done() }) @@ -363,15 +368,15 @@ block.get. ```js this.timeout(10000) -apiClients['a'].block.get(blorbKey, function (err, res) { +apiClients['a'].block.get(blorbKey, (err, res) => { if (err) throw err - if (typeof res === 'string') { + if (!res.on) { // Just a string - assert.equal(res, 'blorb') + assert.equal(res.toString(), 'blorb') done() return } - var buf = '' + let buf = '' res .on('data', function (data) { buf += data }) .on('end', function () { @@ -386,9 +391,9 @@ apiClients['a'].block.get(blorbKey, function (err, res) { object.put. ```js -apiClients['a'].object.put(testObject, 'json', function (err, res) { +apiClients['a'].object.put(testObject, 'json', (err, res) => { if (err) throw err - var obj = res + const obj = res assert.equal(obj.Hash, testObjectHash) assert.equal(obj.Links.length, 0) done() @@ -398,11 +403,11 @@ apiClients['a'].object.put(testObject, 'json', function (err, res) { object.get. ```js -apiClients['a'].object.get(testObjectHash, function (err, res) { +apiClients['a'].object.get(testObjectHash, (err, res) => { if (err) { throw err } - var obj = res + const obj = res assert.equal(obj.Data, 'testdata') assert.equal(obj.Links.length, 0) done() @@ -413,19 +418,19 @@ object.data. ```js this.timeout(10000) -apiClients['a'].object.data(testObjectHash, function (err, res) { +apiClients['a'].object.data(testObjectHash, (err, res) => { if (err) throw err - if (typeof res === 'string') { + if (!res.on) { // Just a string - assert.equal(res, 'testdata') + assert.equal(res.toString(), 'testdata') done() return } - var buf = '' + let buf = '' res - .on('error', function (err) { throw err }) - .on('data', function (data) { buf += data }) - .on('end', function () { + .on('error', err => { throw err }) + .on('data', data => buf += data) + .on('end', () => { assert.equal(buf, 'testdata') done() }) @@ -436,7 +441,7 @@ object.stat. ```js this.timeout(10000) -apiClients['a'].object.stat(testObjectHash, function (err, res) { +apiClients['a'].object.stat(testObjectHash, (err, res) => { if (err) { throw err } @@ -456,7 +461,7 @@ object.links. ```js this.timeout(10000) -apiClients['a'].object.links(testObjectHash, function (err, res) { +apiClients['a'].object.links(testObjectHash, (err, res) => { if (err) { throw err } @@ -472,11 +477,11 @@ object.patch. ```js this.timeout(10000) -apiClients['a'].object.put(testPatchObject, 'json', function (err, res) { +apiClients['a'].object.put(testPatchObject, 'json', (err, res) => { if (err) { throw err } - apiClients['a'].object.patch(testObjectHash, ['add-link', 'next', testPatchObjectHash], function (err, res) { + apiClients['a'].object.patch(testObjectHash, ['add-link', 'next', testPatchObjectHash], (err, res) => { if (err) { throw err } @@ -484,7 +489,7 @@ apiClients['a'].object.put(testPatchObject, 'json', function (err, res) { Hash: 'QmZFdJ3CQsY4kkyQtjoUo8oAzsEs5BNguxBhp8sjQMpgkd', Links: null }) - apiClients['a'].object.get(res.Hash, function (err, res2) { + apiClients['a'].object.get(res.Hash, (err, res2) => { if (err) { throw err } @@ -508,7 +513,7 @@ apiClients['a'].object.put(testPatchObject, 'json', function (err, res) { ```js this.timeout(5000) -apiClients['a'].swarm.peers(function (err, res) { +apiClients['a'].swarm.peers((err, res) => { if (err) { throw err } @@ -529,11 +534,11 @@ done() ping another peer. ```js -apiClients['b'].id(function (err, id) { +apiClients['b'].id((err, id) => { if (err) { throw err } - apiClients['a'].ping(id.ID, function (err, res) { + apiClients['a'].ping(id.ID, (err, res) => { if (err) { throw err } @@ -550,9 +555,9 @@ id. ```js this.timeout(10000) -apiClients['a'].id(function (err, res) { +apiClients['a'].id((err, res) => { if (err) throw err - var id = res + const id = res assert(id.ID) assert(id.PublicKey) done() @@ -565,7 +570,7 @@ apiClients['a'].id(function (err, res) { ```js this.timeout(5000) -apiClients['b'].pin.add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}, function (err, res) { +apiClients['b'].pin.add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}, (err, res) => { if (err) { throw err } @@ -578,7 +583,7 @@ apiClients['b'].pin.add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recur ```js this.timeout(5000) -apiClients['b'].pin.list(function (err, res) { +apiClients['b'].pin.list((err, res) => { if (err) { throw err } @@ -591,12 +596,12 @@ apiClients['b'].pin.list(function (err, res) { ```js this.timeout(5000) -apiClients['b'].pin.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}, function (err, res) { +apiClients['b'].pin.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}, (err, res) => { if (err) { throw err } assert(res) - apiClients['b'].pin.list('direct', function (err, res) { + apiClients['b'].pin.list('direct', (err, res) => { if (err) { throw err } @@ -614,7 +619,7 @@ apiClients['b'].pin.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {re .name.publish. ```js -apiClients['a'].name.publish('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', function (err, res) { +apiClients['a'].name.publish('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => { if (err) { throw err } @@ -627,7 +632,7 @@ apiClients['a'].name.publish('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', f .name.resolve. ```js -apiClients['a'].name.resolve(name.Name, function (err, res) { +apiClients['a'].name.resolve(name.Name, (err, res) => { if (err) { throw err } @@ -646,23 +651,32 @@ if (!isNode) { return done() } this.timeout(10000) -apiClients['a'].refs(folder, {'format': ' '}, function (err, objs) { +apiClients['a'].refs(folder, {'format': ' '}, (err, objs) => { if (err) { throw err } - var result = [{ - Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmaeuuKLHzirbVoTjb3659fyyV381amjaGrU2pecHEWPrN add.js', - Err: '' }, - { Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmTQhTtDWeaaP9pttDd1CuoVTLQm1w51ABfjgmGUbCUF6i cat.js', - Err: '' }, - { Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmTYFLz5vsdMpq4XXw1a1pSxujJc9Z5V3Aw1Qg64d849Zy files', - Err: '' }, - { Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmTjXxUemcuMAZ2KNN3iJGWHwrkMsW8SWEwkYVSBi1nFD9 ipfs-add.js', - Err: '' }, - { Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmXYUXDFNNh1wgwtX5QDG7MsuhAAcE9NzDYnz8SjnhvQrK ls.js', - Err: '' }, - { Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmUmDmH4hZgN5THnVP1VjJ1YWh5kWuhLGUihch8nFiD9iy version.js', - Err: '' } ] + const result = [{ + Ref: 'QmSzLpCVbWnEm3XoTWnv6DT6Ju5BsVoLhzvxKXZeQ2cmdg QmcUYKmQxmTcFom4R4UZP7FWeQzgJkwcFn51XrvsMy7PE9 add.js', + Err: '' + }, { + Ref: 'QmSzLpCVbWnEm3XoTWnv6DT6Ju5BsVoLhzvxKXZeQ2cmdg QmNeHxDfQfjVFyYj2iruvysLH9zpp78v3cu1s3BZq1j5hY cat.js', + Err: '' + }, { + Ref: 'QmSzLpCVbWnEm3XoTWnv6DT6Ju5BsVoLhzvxKXZeQ2cmdg QmTYFLz5vsdMpq4XXw1a1pSxujJc9Z5V3Aw1Qg64d849Zy files', + Err: '' + }, { + Ref: 'QmSzLpCVbWnEm3XoTWnv6DT6Ju5BsVoLhzvxKXZeQ2cmdg QmU7wetVaAqc3Meurif9hcYBHGvQmL5QdpPJYBoZizyTNL ipfs-add.js', + Err: '' + }, { + Ref: 'QmSzLpCVbWnEm3XoTWnv6DT6Ju5BsVoLhzvxKXZeQ2cmdg QmctZfSuegbi2TMFY2y3VQjxsH5JbRBu7XmiLfHNvshhio ls.js', + Err: '' + }, { + Ref: 'QmSzLpCVbWnEm3XoTWnv6DT6Ju5BsVoLhzvxKXZeQ2cmdg QmTDH2RXGn8XyDAo9YyfbZAUXwL1FCr44YJCN9HBZmL9Gj test-folder', + Err: '' + }, { + Ref: 'QmSzLpCVbWnEm3XoTWnv6DT6Ju5BsVoLhzvxKXZeQ2cmdg QmbkMNB6rwfYAxRvnG9CWJ6cKKHEdq2ZKTozyF5FQ7H8Rs version.js', + Err: '' + }] assert.deepEqual(objs, result) done() }) @@ -674,7 +688,7 @@ returns an error when getting a non-existent key from the DHT. ```js this.timeout(20000) -apiClients['a'].dht.get('non-existent', {timeout: '100ms'}, function (err, value) { +apiClients['a'].dht.get('non-existent', {timeout: '100ms'}, (err, value) => { assert(err) done() }) @@ -684,14 +698,14 @@ puts and gets a key value pair in the DHT. ```js this.timeout(20000) -apiClients['a'].dht.put('scope', 'interplanetary', function (err, res) { +apiClients['a'].dht.put('scope', 'interplanetary', (err, res) => { if (err) { throw err } return done() // non ipns or pk hashes fail to fetch, known bug // bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234 - // apiClients['a'].dht.get('scope', function (err, value) { + // apiClients['a'].dht.get('scope', (err, value) => { // console.log('->>', err, value) // if (err) { // throw err @@ -705,7 +719,7 @@ apiClients['a'].dht.put('scope', 'interplanetary', function (err, res) { .dht.findprovs. ```js -apiClients['a'].dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', function (err, res) { +apiClients['a'].dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => { if (err) { throw err } From 59c2c5ff703258305a6395d962f8d9a6f33937dd Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Sat, 21 Nov 2015 17:29:24 +0100 Subject: [PATCH 4/4] chore: build docs --- API.md | 91 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 71 insertions(+), 20 deletions(-) diff --git a/API.md b/API.md index 4c670beb9..f0a8f410e 100644 --- a/API.md +++ b/API.md @@ -105,12 +105,28 @@ let buf = new Buffer(testfile) apiClients['a'].add(buf, (err, res) => { if (err) throw err // assert.equal(res.length, 1) - const added = res[0] != null ? res[0] : res + const added = res[0] !== null ? res[0] : res assert.equal(added.Hash, 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') done() }) ``` +add BIG buffer. + +```js +if (!isNode) { + return done() +} +this.timeout(10000) +apiClients['a'].add(testfileBig, (err, res) => { + if (err) throw err + // assert.equal(res.length, 1) + const added = res[0] !== null ? res[0] : res + assert.equal(added.Hash, 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq') + done() +}) +``` + add path. ```js @@ -181,12 +197,6 @@ apiClients['a'].cat('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) if (err) { throw err } - if (!res.on) { - // Just a string - assert.equal(res.toString(), testfile) - done() - return - } let buf = '' res .on('error', err => { throw err }) @@ -198,6 +208,27 @@ apiClients['a'].cat('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) }) ``` +cat BIG file. + +```js +if (!isNode) { + return done() +} +this.timeout(1000000) +apiClients['a'].cat('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq', (err, res) => { + if (err) { + throw err + } + testfileBig = require('fs').createReadStream(__dirname + '/15mb.random', { bufferSize: 128 }) + // Do not blow out the memory of nodejs :) + streamEqual(res, testfileBig, (err, equal) => { + if (err) throw err + assert(equal) + done() + }) +}) +``` + ## .ls ls. @@ -296,7 +327,7 @@ apiClients['c'].config.replace(__dirname + '/r-config.json', (err, res) => { if (err) { throw err } - assert.equal(res, '') + assert.equal(res, null) done() }) ``` @@ -350,6 +381,20 @@ apiClients['a'].diag.net((err, res) => { }) ``` +.diag.sys. + +```js +apiClients['a'].diag.sys((err, res) => { + if (err) { + throw err + } + assert(res) + assert(res.memory) + assert(res.diskinfo) + done() +}) +``` + ## .block block.put. @@ -370,12 +415,6 @@ block.get. this.timeout(10000) apiClients['a'].block.get(blorbKey, (err, res) => { if (err) throw err - if (!res.on) { - // Just a string - assert.equal(res.toString(), 'blorb') - done() - return - } let buf = '' res .on('data', function (data) { buf += data }) @@ -420,12 +459,6 @@ object.data. this.timeout(10000) apiClients['a'].object.data(testObjectHash, (err, res) => { if (err) throw err - if (!res.on) { - // Just a string - assert.equal(res.toString(), 'testdata') - done() - return - } let buf = '' res .on('error', err => { throw err }) @@ -614,6 +647,22 @@ apiClients['b'].pin.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {re ## .log +.log.tail. + +```js +this.timeout(20000) +apiClients['a'].log.tail((err, res) => { + if (err) { + throw err + } + res.once('data', obj => { + assert(obj) + assert.equal(typeof obj, 'object') + done() + }) +}) +``` + ## .name .name.publish. @@ -702,6 +751,7 @@ apiClients['a'].dht.put('scope', 'interplanetary', (err, res) => { if (err) { throw err } + assert.equal(typeof res, 'object') return done() // non ipns or pk hashes fail to fetch, known bug // bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234 @@ -723,6 +773,7 @@ apiClients['a'].dht.findprovs('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', if (err) { throw err } + assert.equal(typeof res, 'object') assert(res) done() })