From bf153306cbaeb039e3264d007be8b6b3c8dbc4ae Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 28 Jan 2016 18:08:05 +0000 Subject: [PATCH 1/8] object create --- package.json | 2 +- src/cli/commands/config.js | 2 +- src/ipfs-core/index.js | 35 ++++++++++++- tests/test-cli/test-config.js | 2 +- tests/test-core/browser.js | 6 +-- tests/test-core/test-block.js | 2 +- tests/test-core/test-config.js | 16 +++--- tests/test-core/test-object.js | 91 ++++++++++++++++++++++++++++++++++ 8 files changed, 140 insertions(+), 16 deletions(-) create mode 100644 tests/test-core/test-object.js diff --git a/package.json b/package.json index af777932e2..4908edf66d 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,7 @@ "debug": "^2.2.0", "hapi": "^12.0.0", "ipfs-blocks": "^0.1.0", - "ipfs-merkle-dag": "^0.1.1", + "ipfs-merkle-dag": "^0.2.0", "ipfs-repo": "^0.5.0", "lodash.get": "^4.0.0", "lodash.set": "^4.0.0", diff --git a/src/cli/commands/config.js b/src/cli/commands/config.js index 196eb0c388..22c6b788a3 100644 --- a/src/cli/commands/config.js +++ b/src/cli/commands/config.js @@ -24,7 +24,7 @@ module.exports = Command.extend({ run: (bool, json, key, value) => { if (!key) { - throw new Error('argument \'key\' is required') + throw new Error("argument 'key' is required") } var node = new IPFS() diff --git a/src/ipfs-core/index.js b/src/ipfs-core/index.js index 367bc04dae..408dcf0dde 100644 --- a/src/ipfs-core/index.js +++ b/src/ipfs-core/index.js @@ -2,10 +2,11 @@ const defaultRepo = require('./default-repo') // const bl = require('bl') -// const MerkleDAG = require('ipfs-merkle-dag') const blocks = require('ipfs-blocks') const BlockService = blocks.BlockService +const Block = blocks.Block // const Block = MerkleDAG.Block +const mDAG = require('ipfs-merkle-dag') exports = module.exports = IPFS @@ -153,4 +154,36 @@ function IPFS (repo) { }) } } + + this.object = { + // named `new` in go-ipfs + create: (template, callback) => { + if (!callback) { + callback = template + } + var node = new mDAG.DAGNode() + var block = new Block(node.marshal()) + bs.addBlock(block, function (err) { + if (err) { + return callback(err) + } + callback(null, { + Hash: block.key, + Size: node.size(), + Name: '' + }) + }) + }, + patch: (multihash, options, callback) => {}, + data: (multihash, callback) => {}, + links: (multihash, callback) => {}, + get: (multihash, options, callback) => { + if (typeof options === 'function') { + callback = options + options = {} + } + }, + put: (multihash, options, callback) => {}, + stat: (multihash, options, callback) => {} + } } diff --git a/tests/test-cli/test-config.js b/tests/test-cli/test-config.js index 9a3eeca710..7ee63bf5c8 100644 --- a/tests/test-cli/test-config.js +++ b/tests/test-cli/test-config.js @@ -77,7 +77,7 @@ describe('config', () => { it('call config with no arguments', done => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config']) .run((err, stdout, exitcode) => { - const expected = 'error argument \'key\' is required' + const expected = "error argument 'key' is required" expect(stdout[0]).to.equal(expected) expect(err).to.not.exist expect(exitcode).to.equal(1) diff --git a/tests/test-core/browser.js b/tests/test-core/browser.js index aff5aa7833..90f8b73e24 100644 --- a/tests/test-core/browser.js +++ b/tests/test-core/browser.js @@ -7,9 +7,9 @@ const _ = require('lodash') const repoContext = require.context('buffer!./../repo-example', true) const idb = window.indexedDB || - window.mozIndexedDB || - window.webkitIndexedDB || - window.msIndexedDB + window.mozIndexedDB || + window.webkitIndexedDB || + window.msIndexedDB idb.deleteDatabase('ipfs') idb.deleteDatabase('ipfs/blocks') diff --git a/tests/test-core/test-block.js b/tests/test-core/test-block.js index 74594c9397..da808c71be 100644 --- a/tests/test-core/test-block.js +++ b/tests/test-core/test-block.js @@ -61,7 +61,7 @@ describe('block', function () { it('stat', function (done) { const mh = new Buffer(base58 - .decode('QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe')) + .decode('QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe')) ipfs.block.stat(mh, (err, stats) => { expect(err).to.not.exist expect(stats.Key.equals(mh)).to.equal(true) diff --git a/tests/test-core/test-config.js b/tests/test-core/test-config.js index f4ec77f895..9a9ceafd22 100644 --- a/tests/test-core/test-config.js +++ b/tests/test-core/test-config.js @@ -90,12 +90,12 @@ describe('config', () => { }) }) - // cli only feature built with show and replace - // it.skip('edit', done => { - // const ipfs = new IPFS() - // ipfs.config((err, config) => { - // expect(err).to.not.exist - // done() - // }) - // }) +// cli only feature built with show and replace +// it.skip('edit', done => { +// const ipfs = new IPFS() +// ipfs.config((err, config) => { +// expect(err).to.not.exist +// done() +// }) +// }) }) diff --git a/tests/test-core/test-object.js b/tests/test-core/test-object.js new file mode 100644 index 0000000000..ec77117dc3 --- /dev/null +++ b/tests/test-core/test-object.js @@ -0,0 +1,91 @@ +/* globals describe, before, it */ + +'use strict' + +const expect = require('chai').expect +const IPFS = require('../../src/ipfs-core') +const bs58 = require('bs58') +// const Block = require('ipfs-merkle-dag').Block + +// TODO use arrow funtions again when https://github.com/webpack/webpack/issues/1944 is fixed +describe('object', function () { + var ipfs + + before(function (done) { + ipfs = new IPFS() + done() + }) + + it('create', function (done) { + ipfs.object.create(function (err, obj) { + expect(err).to.not.exist + expect(obj).to.have.property('Size') + expect(obj.Size).to.equal(0) + expect(obj).to.have.property('Name') + expect(obj.Name).to.equal('') + expect(obj).to.have.property('Hash') + expect(bs58.encode(obj.Hash).toString()) + .to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') + expect(obj.Size).to.equal(0) + done() + }) + }) + + it.skip('patch append-data', function (done) { + + }) + + it.skip('patch add-link', function (done) { + + }) + + it.skip('patch rm-link', function (done) { + + }) + + it.skip('patch set-data', function (done) { + + }) + + it.skip('patch append-data go-ipfs mDAG obj', function (done) { + + }) + + it.skip('patch add-link go-ipfs mDAG obj', function (done) { + + }) + + it.skip('patch rm-link go-ipfs mDAG obj', function (done) { + + }) + + it.skip('patch set-data go-ipfs mDAG obj', function (done) { + + }) + + it.skip('data', function (done) { + + }) + + it.skip('links', function (done) { + + }) + + it.skip('get', function (done) { + + }) + + it.skip('get cycle', function (done) { + // 1. get a go-ipfs marshaled DAG Node + // 2. store it and fetch it again + // 3. check if still matches (ipfs-merkle-dag should not mangle it) + }) + + it.skip('put', function (done) { + + }) + + it.skip('stat', function (done) { + + }) +}) From 6a3b889762e2f11ad16705da1be65b84c879dd89 Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 3 Feb 2016 07:31:35 +0000 Subject: [PATCH 2/8] get and put --- src/ipfs-core/index.js | 22 ++++++++++++++++++---- tests/test-core/test-object.js | 28 +++++++++++++++++----------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/ipfs-core/index.js b/src/ipfs-core/index.js index 408dcf0dde..9ef944f145 100644 --- a/src/ipfs-core/index.js +++ b/src/ipfs-core/index.js @@ -5,8 +5,9 @@ const defaultRepo = require('./default-repo') const blocks = require('ipfs-blocks') const BlockService = blocks.BlockService const Block = blocks.Block -// const Block = MerkleDAG.Block const mDAG = require('ipfs-merkle-dag') +const DAGNode = mDAG.DAGNode +const DAGService = mDAG.DAGService exports = module.exports = IPFS @@ -19,6 +20,7 @@ function IPFS (repo) { repo = defaultRepo() } const bs = new BlockService(repo) + const ds = new DAGService(bs) this.daemon = callback => { // 1. read repo to get peer data @@ -161,7 +163,7 @@ function IPFS (repo) { if (!callback) { callback = template } - var node = new mDAG.DAGNode() + var node = new DAGNode() var block = new Block(node.marshal()) bs.addBlock(block, function (err) { if (err) { @@ -182,8 +184,20 @@ function IPFS (repo) { callback = options options = {} } + ds.get(multihash, callback) }, - put: (multihash, options, callback) => {}, - stat: (multihash, options, callback) => {} + put: (dagNode, options, callback) => { + if (typeof options === 'function') { + callback = options + options = {} + } + ds.add(dagNode, callback) + }, + stat: (multihash, options, callback) => { + if (typeof options === 'function') { + callback = options + options = {} + } + } } } diff --git a/tests/test-core/test-object.js b/tests/test-core/test-object.js index ec77117dc3..5a62cf5ea3 100644 --- a/tests/test-core/test-object.js +++ b/tests/test-core/test-object.js @@ -5,7 +5,8 @@ const expect = require('chai').expect const IPFS = require('../../src/ipfs-core') const bs58 = require('bs58') -// const Block = require('ipfs-merkle-dag').Block +const mDAG = require('ipfs-merkle-dag') +const DAGNode = mDAG.DAGNode // TODO use arrow funtions again when https://github.com/webpack/webpack/issues/1944 is fixed describe('object', function () { @@ -71,18 +72,23 @@ describe('object', function () { }) - it.skip('get', function (done) { - - }) - - it.skip('get cycle', function (done) { - // 1. get a go-ipfs marshaled DAG Node - // 2. store it and fetch it again - // 3. check if still matches (ipfs-merkle-dag should not mangle it) + it('get', function (done) { + const mh = new Buffer(bs58.decode('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')) + ipfs.object.get(mh, function (err, obj) { + expect(err).to.not.exist + expect(obj.size()).to.equal(0) + expect(obj).to.have.property('data') + expect(obj).to.have.property('links') + done() + }) }) - it.skip('put', function (done) { - + it('put', function (done) { + const node = new DAGNode(new Buffer('Hello, is it me you are looking for')) + ipfs.object.put(node, function (err) { + expect(err).to.not.exist + done() + }) }) it.skip('stat', function (done) { From 8780bf9e7eee56af0806bb0bbe9dfc3369da8024 Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 3 Feb 2016 07:37:47 +0000 Subject: [PATCH 3/8] fix the tab thing https://github.com/ipfs/js-ipfs/pull/63#discussion-diff-51298905 --- tests/test-cli/test-config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test-cli/test-config.js b/tests/test-cli/test-config.js index 7ee63bf5c8..c704f2274a 100644 --- a/tests/test-cli/test-config.js +++ b/tests/test-cli/test-config.js @@ -66,7 +66,7 @@ describe('config', () => { it('set a config key with invalid json', done => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config', 'foo', '{"bar: 0}', '--json']) .run((err, stdout, exitcode) => { - const expected = 'error invalid JSON provided' + const expected = 'error\tinvalid JSON provided' expect(stdout[0]).to.equal(expected) expect(err).to.not.exist expect(exitcode).to.equal(1) @@ -77,7 +77,7 @@ describe('config', () => { it('call config with no arguments', done => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'config']) .run((err, stdout, exitcode) => { - const expected = "error argument 'key' is required" + const expected = "error\targument 'key' is required" expect(stdout[0]).to.equal(expected) expect(err).to.not.exist expect(exitcode).to.equal(1) From f28a0f342e9d0fd5ed9b2f5ef71aa61e5b546a89 Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 3 Feb 2016 08:20:16 +0000 Subject: [PATCH 4/8] links and stat --- src/ipfs-core/index.js | 26 +++- ...ad869e2ae1d9e41a2df4e086fe2e1403e8a5b.data | 3 + ...fcb1454f0dd7ae43df2dde01bef8a8bdae446.data | 3 + ...9acff7d5806c086068cad8d9e820dd2f4795a.data | Bin 0 -> 10765 bytes ...3edca90b68053c00b3004b7f0accbe1e8eedf.data | Bin 0 -> 309 bytes ...a45448e0cd0e21ec6a5dec62a51c54b4d51d1.data | Bin 0 -> 10849 bytes ...37cc9a3b80e182ac123bbd427563e8b167c96.data | Bin 0 -> 10807 bytes ...9e72dc99d23a391d3db5e1e42d00527241671.data | 115 ++++++++++++++++++ ...546ab06c601063224601337231b75401a31dd.data | Bin 0 -> 10765 bytes tests/repo-example/datastore/MANIFEST-000004 | Bin 0 -> 141 bytes tests/test-core/test-object.js | 24 +++- 11 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 tests/repo-example/blocks/12203aa9/12203aa913048b2ef582d4ee2bf6eccad869e2ae1d9e41a2df4e086fe2e1403e8a5b.data create mode 100644 tests/repo-example/blocks/122048b2/122048b220016f4051d7dd74315bf1ffcb1454f0dd7ae43df2dde01bef8a8bdae446.data create mode 100644 tests/repo-example/blocks/122077d2/122077d2a2b0fc907805a547ea83dc99acff7d5806c086068cad8d9e820dd2f4795a.data create mode 100644 tests/repo-example/blocks/12209d6c/12209d6c2be50f706953479ab9df2ce3edca90b68053c00b3004b7f0accbe1e8eedf.data create mode 100644 tests/repo-example/blocks/1220b0ab/1220b0abba9f487a9f38ed23337532da45448e0cd0e21ec6a5dec62a51c54b4d51d1.data create mode 100644 tests/repo-example/blocks/1220b0cb/1220b0cba7371f11461f77081b947d837cc9a3b80e182ac123bbd427563e8b167c96.data create mode 100644 tests/repo-example/blocks/1220e586/1220e586199640e1a4c63fa38c5434b9e72dc99d23a391d3db5e1e42d00527241671.data create mode 100644 tests/repo-example/blocks/1220f1c1/1220f1c121360796b1c031b0d8b58fd546ab06c601063224601337231b75401a31dd.data create mode 100644 tests/repo-example/datastore/MANIFEST-000004 diff --git a/src/ipfs-core/index.js b/src/ipfs-core/index.js index 9ef944f145..f983470f53 100644 --- a/src/ipfs-core/index.js +++ b/src/ipfs-core/index.js @@ -178,7 +178,12 @@ function IPFS (repo) { }, patch: (multihash, options, callback) => {}, data: (multihash, callback) => {}, - links: (multihash, callback) => {}, + links: (multihash, callback) => { + this.object.get(multihash, (err, obj) => { + if (err) { return callback(err) } + callback(null, obj.links) + }) + }, get: (multihash, options, callback) => { if (typeof options === 'function') { callback = options @@ -198,6 +203,25 @@ function IPFS (repo) { callback = options options = {} } + + // NumLinks: 367 + // BlockSize: 19394 + // LinksSize: 19392 + // DataSize: 2 + // CumulativeSize: 26375427 + this.object.get(multihash, (err, obj) => { + if (err) { return callback(err) } + var res = { + NumLinks: obj.links.length, + BlockSize: obj.marshal().length, + LinksSize: obj.links.reduce((prev, link) => { + return prev + link.size + }, 0), + DataSize: obj.data.length, + CumulativeSize: '' + } + callback(null, res) + }) } } } diff --git a/tests/repo-example/blocks/12203aa9/12203aa913048b2ef582d4ee2bf6eccad869e2ae1d9e41a2df4e086fe2e1403e8a5b.data b/tests/repo-example/blocks/12203aa9/12203aa913048b2ef582d4ee2bf6eccad869e2ae1d9e41a2df4e086fe2e1403e8a5b.data new file mode 100644 index 0000000000..ed8c782e45 --- /dev/null +++ b/tests/repo-example/blocks/12203aa9/12203aa913048b2ef582d4ee2bf6eccad869e2ae1d9e41a2df4e086fe2e1403e8a5b.data @@ -0,0 +1,3 @@ +/ +" wҢxGܙ}X yZdirectT2 +" ˧7Fw}|ɣ*#'V>| recursiveT \ No newline at end of file diff --git a/tests/repo-example/blocks/122048b2/122048b220016f4051d7dd74315bf1ffcb1454f0dd7ae43df2dde01bef8a8bdae446.data b/tests/repo-example/blocks/122048b2/122048b220016f4051d7dd74315bf1ffcb1454f0dd7ae43df2dde01bef8a8bdae446.data new file mode 100644 index 0000000000..4a26a4ac35 --- /dev/null +++ b/tests/repo-example/blocks/122048b2/122048b220016f4051d7dd74315bf1ffcb1454f0dd7ae43df2dde01bef8a8bdae446.data @@ -0,0 +1,3 @@ +/ +" !61صF2$`7#u@1directT2 +" Hz8#3u2ED ƥ*QKMQ recursiveT \ No newline at end of file diff --git a/tests/repo-example/blocks/122077d2/122077d2a2b0fc907805a547ea83dc99acff7d5806c086068cad8d9e820dd2f4795a.data b/tests/repo-example/blocks/122077d2/122077d2a2b0fc907805a547ea83dc99acff7d5806c086068cad8d9e820dd2f4795a.data new file mode 100644 index 0000000000000000000000000000000000000000..78421c81c29ed8c18921f686ea3292e3e7300668 GIT binary patch literal 10765 zcmeIzAqoOf6a~;x48ym`#~`RM41#V$vuVH`*sVtQAcCXSECW-t8{CI3!R7?FVL1Jz z%e(i)S;%Adsve7DGwPqdpJ%zbPo~%Tdwt$7FNbotEpDrr43g+lN<931^R@s0 literal 0 HcmV?d00001 diff --git a/tests/repo-example/blocks/12209d6c/12209d6c2be50f706953479ab9df2ce3edca90b68053c00b3004b7f0accbe1e8eedf.data b/tests/repo-example/blocks/12209d6c/12209d6c2be50f706953479ab9df2ce3edca90b68053c00b3004b7f0accbe1e8eedf.data new file mode 100644 index 0000000000000000000000000000000000000000..b799cf6b2236c91f8224b9fefe43aca16c773490 GIT binary patch literal 309 zcmWgA<5Ch*SgK>j#LTl(=mjz6J*Z#Z`mUeibke1%>*qt`A@ymo*6O-~wOC)CS z3K@XZPnu<5V|lBKJN>}4>2CTNRSd^0_H1qVw~ozKk4Ii%i@p$ha(-S(VseSZ2}U7Z zkd6rls>-Jxlr$AsHtpiyKm2l@?&p6zQA*^T_KfrP>FKF6gjh0Ca|$F5F$tM~ls|2g zoaXRw$uaxIJs~DLpX;8StGsyP<=b&`P8V3!Rm2K~xC=`&le2Y;OA?DpBo6Qj>4S6$ z@n?Nwypkud>C!{F>oTF2;tiY*sWLv16>@vN@a5$jZ-m&2QWH~hQzg!E2$_R41Rb*} zFSX3JZ#rfpXsYYQnaL-~-$SEZaY-MiqUv4K}orj%_y00+YJV}#XGok z)Gc&_%?H(zbc;FK34aUrCGW}p=j0S(a*4bcz{(GU&M5Dn1~4bcz{(GU&M5Dn1~4bcz{(GU&M5Dn1~ z4bcz{(GU&M5Dn4rZ!}yDE04r@ptViQt$NSPbgyf+T8kZmjdSx~?(hFos3IOo>d{>N G+6cZA+z2}W literal 0 HcmV?d00001 diff --git a/tests/repo-example/blocks/1220e586/1220e586199640e1a4c63fa38c5434b9e72dc99d23a391d3db5e1e42d00527241671.data b/tests/repo-example/blocks/1220e586/1220e586199640e1a4c63fa38c5434b9e72dc99d23a391d3db5e1e42d00527241671.data new file mode 100644 index 0000000000..f2bf4f8b8d --- /dev/null +++ b/tests/repo-example/blocks/1220e586/1220e586199640e1a4c63fa38c5434b9e72dc99d23a391d3db5e1e42d00527241671.data @@ -0,0 +1,115 @@ + +  # 0.1 - Quick Start + +This is a set of short examples with minimal explanation. It is meant as +a "quick start". Soon, we'll write a longer tour :-) + + +Add a file to ipfs: + + echo "hello world" >hello + ipfs add hello + + +View it: + + ipfs cat + + +Try a directory: + + mkdir foo + mkdir foo/bar + echo "baz" > foo/baz + echo "baz" > foo/bar/baz + ipfs add -r foo + + +View things: + + ipfs ls + ipfs ls /bar + ipfs cat /baz + ipfs cat /bar/baz + ipfs cat /bar + ipfs ls /baz + + +References: + + ipfs refs + ipfs refs -r + ipfs refs --help + + +Get: + + ipfs get -o foo2 + diff foo foo2 + + +Objects: + + ipfs object get + ipfs object get /foo2 + ipfs object --help + + +Pin + GC: + + ipfs pin add + ipfs repo gc + ipfs ls + ipfs pin rm + ipfs repo gc + + +Daemon: + + ipfs daemon (in another terminal) + ipfs id + + +Network: + + (must be online) + ipfs swarm peers + ipfs id + ipfs cat + + +Mount: + + (warning: fuse is finicky!) + ipfs mount + cd /ipfs/ + ls + + +Tool: + + ipfs version + ipfs update + ipfs commands + ipfs config --help + open http://localhost:5001/webui + + +Browse: + + webui: + + http://localhost:5001/webui + + video: + + http://localhost:8080/ipfs/QmVc6zuAneKJzicnJpfrqCH9gSy6bz54JhcypfJYhGUFQu/play#/ipfs/QmTKZgRNwDNZwHtJSjCp6r5FYefzpULfy37JvMt9DwvXse + + images: + + http://localhost:8080/ipfs/QmZpc3HvfjEXvLWGQPWbHk3AjD5j8NEN4gmFN8Jmrd5g83/cs + + markdown renderer app: + + http://localhost:8080/ipfs/QmX7M9CiYXjVeFnkfVGf3y5ixTZ2ACeSGyL1vBJY1HvQPp/mdown + \ No newline at end of file diff --git a/tests/repo-example/blocks/1220f1c1/1220f1c121360796b1c031b0d8b58fd546ab06c601063224601337231b75401a31dd.data b/tests/repo-example/blocks/1220f1c1/1220f1c121360796b1c031b0d8b58fd546ab06c601063224601337231b75401a31dd.data new file mode 100644 index 0000000000000000000000000000000000000000..3092e413d6a7255523fdab354055977430ccf7c4 GIT binary patch literal 10765 zcmeIzu?j(97zW@&l75T+D8(sBoin=wW|MLUcB|1nNS(uKmXZAo?t@FPF}MxGvwzdu z_r6ce!#Iwf*J@sThu!1n{U|s0Np_jN)~EgAd?*Lo`pgoS$2eSd>_jU&O@B%*w*d&cJk;Q9?f_ kKRGc+KP@vSwYVrhzl4#A0Sttp5(Sxg#f*#)G17Ga00?Cx+yDRo literal 0 HcmV?d00001 diff --git a/tests/test-core/test-object.js b/tests/test-core/test-object.js index 5a62cf5ea3..f604dd66aa 100644 --- a/tests/test-core/test-object.js +++ b/tests/test-core/test-object.js @@ -68,8 +68,13 @@ describe('object', function () { }) - it.skip('links', function (done) { - + it('links', function (done) { + const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) + ipfs.object.links(mh, function (err, links) { + expect(err).to.not.exist + expect(links.length).to.equal(6) + done() + }) }) it('get', function (done) { @@ -91,7 +96,20 @@ describe('object', function () { }) }) - it.skip('stat', function (done) { + it('stat', function (done) { + const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) + ipfs.object.stat(mh, function (err, stats) { + expect(err).to.not.exist + var expected = { + NumLinks: 6, + BlockSize: 309, + LinksSize: 6067, + DataSize: 2, + CumulativeSize: '' + } + expect(stats).to.deep.equal(expected) + done() + }) }) }) From 80d098f4126e86cd11825092933af0ee7155a78a Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 3 Feb 2016 08:23:49 +0000 Subject: [PATCH 5/8] data --- src/ipfs-core/index.js | 7 ++++++- tests/test-core/test-object.js | 9 +++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/ipfs-core/index.js b/src/ipfs-core/index.js index f983470f53..285a4ca630 100644 --- a/src/ipfs-core/index.js +++ b/src/ipfs-core/index.js @@ -177,7 +177,12 @@ function IPFS (repo) { }) }, patch: (multihash, options, callback) => {}, - data: (multihash, callback) => {}, + data: (multihash, callback) => { + this.object.get(multihash, (err, obj) => { + if (err) { return callback(err) } + callback(null, obj.data) + }) + }, links: (multihash, callback) => { this.object.get(multihash, (err, obj) => { if (err) { return callback(err) } diff --git a/tests/test-core/test-object.js b/tests/test-core/test-object.js index f604dd66aa..0926d347b9 100644 --- a/tests/test-core/test-object.js +++ b/tests/test-core/test-object.js @@ -64,8 +64,13 @@ describe('object', function () { }) - it.skip('data', function (done) { - + it('data', function (done) { + const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) + ipfs.object.data(mh, function (err, data) { + expect(err).to.not.exist + expect(data).to.deep.equal(new Buffer('\u0008\u0001')) + done() + }) }) it('links', function (done) { From 0f57083d68140a1da9e65d570be361fb7405a282 Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 3 Feb 2016 09:47:09 +0000 Subject: [PATCH 6/8] patch and all its methods --- package.json | 2 +- src/ipfs-core/index.js | 57 +++++++++++++++++++++++++++++----- tests/test-core/test-object.js | 54 +++++++++++++++++++------------- 3 files changed, 82 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 4908edf66d..e55b7963a2 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,7 @@ "debug": "^2.2.0", "hapi": "^12.0.0", "ipfs-blocks": "^0.1.0", - "ipfs-merkle-dag": "^0.2.0", + "ipfs-merkle-dag": "^0.2.1", "ipfs-repo": "^0.5.0", "lodash.get": "^4.0.0", "lodash.set": "^4.0.0", diff --git a/src/ipfs-core/index.js b/src/ipfs-core/index.js index 285a4ca630..dd6d3f5d5e 100644 --- a/src/ipfs-core/index.js +++ b/src/ipfs-core/index.js @@ -158,8 +158,7 @@ function IPFS (repo) { } this.object = { - // named `new` in go-ipfs - create: (template, callback) => { + new: (template, callback) => { if (!callback) { callback = template } @@ -176,7 +175,54 @@ function IPFS (repo) { }) }) }, - patch: (multihash, options, callback) => {}, + patch: { + appendData: (multihash, data, callback) => { + this.object.get(multihash, (err, obj) => { + if (err) { return callback(err) } + obj.data = Buffer.concat([obj.data, data]) + ds.add(obj, (err) => { + if (err) { return callback(err) } + callback(null, obj.multihash()) + }) + }) + }, + addLink: (multihash, link, callback) => { + this.object.get(multihash, (err, obj) => { + if (err) { return callback(err) } + obj.addRawLink(link) + ds.add(obj, (err) => { + if (err) { return callback(err) } + callback(null, obj.multihash()) + }) + }) + }, + rmLink: (multihash, multihashLink, callback) => { + this.object.get(multihash, (err, obj) => { + if (err) { return callback(err) } + obj.links = obj.links.filter((link) => { + if (link.hash.equals(multihashLink)) { + return false + } else { + return true + } + }) + ds.add(obj, (err) => { + if (err) { return callback(err) } + callback(null, obj.multihash()) + }) + }) + }, + setData: (multihash, data, callback) => { + this.object.get(multihash, (err, obj) => { + if (err) { return callback(err) } + obj.data = data + ds.add(obj, (err) => { + if (err) { return callback(err) } + callback(null, obj.multihash()) + }) + }) + } + }, data: (multihash, callback) => { this.object.get(multihash, (err, obj) => { if (err) { return callback(err) } @@ -209,11 +255,6 @@ function IPFS (repo) { options = {} } - // NumLinks: 367 - // BlockSize: 19394 - // LinksSize: 19392 - // DataSize: 2 - // CumulativeSize: 26375427 this.object.get(multihash, (err, obj) => { if (err) { return callback(err) } var res = { diff --git a/tests/test-core/test-object.js b/tests/test-core/test-object.js index 0926d347b9..cba178318e 100644 --- a/tests/test-core/test-object.js +++ b/tests/test-core/test-object.js @@ -7,6 +7,7 @@ const IPFS = require('../../src/ipfs-core') const bs58 = require('bs58') const mDAG = require('ipfs-merkle-dag') const DAGNode = mDAG.DAGNode +const DAGLink = mDAG.DAGLink // TODO use arrow funtions again when https://github.com/webpack/webpack/issues/1944 is fixed describe('object', function () { @@ -17,8 +18,8 @@ describe('object', function () { done() }) - it('create', function (done) { - ipfs.object.create(function (err, obj) { + it('new', function (done) { + ipfs.object.new(function (err, obj) { expect(err).to.not.exist expect(obj).to.have.property('Size') expect(obj.Size).to.equal(0) @@ -32,36 +33,45 @@ describe('object', function () { }) }) - it.skip('patch append-data', function (done) { - - }) - - it.skip('patch add-link', function (done) { - - }) - - it.skip('patch rm-link', function (done) { - - }) - - it.skip('patch set-data', function (done) { - - }) - - it.skip('patch append-data go-ipfs mDAG obj', function (done) { + it('patch append-data', function (done) { + const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) + ipfs.object.patch.appendData(mh, new Buffer('data data'), function (err, multihash) { + expect(err).to.not.exist + expect(mh).to.not.deep.equal(multihash) + done() + }) }) - it.skip('patch add-link go-ipfs mDAG obj', function (done) { + it('patch add-link', function (done) { + const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) + ipfs.object.patch.addLink(mh, new DAGLink('prev', 0, mh), function (err, multihash) { + expect(err).to.not.exist + expect(mh).to.not.deep.equal(multihash) + done() + }) }) - it.skip('patch rm-link go-ipfs mDAG obj', function (done) { + it('patch rm-link', function (done) { + const rmmh = new Buffer(bs58.decode('QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V')) + const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) + ipfs.object.patch.rmLink(mh, rmmh, function (err, multihash) { + expect(err).to.not.exist + expect(mh).to.not.deep.equal(multihash) + done() + }) }) - it.skip('patch set-data go-ipfs mDAG obj', function (done) { + it('patch set-data', function (done) { + const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) + ipfs.object.patch.setData(mh, new Buffer('data data data'), function (err, multihash) { + expect(err).to.not.exist + expect(mh).to.not.deep.equal(multihash) + done() + }) }) it('data', function (done) { From 3b4c53b22641e646222ee7015fc4401f481e2f70 Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 3 Feb 2016 11:01:27 +0000 Subject: [PATCH 7/8] use webpack 2 to solve buffer issues --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e55b7963a2..0ae51a9e15 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "rimraf": "^2.4.4", "standard": "^5.4.1", "transform-loader": "^0.2.3", - "webpack": "diasdavid/webpack#81f5994" + "webpack": "^2.0.5-beta" }, "dependencies": { "bl": "^1.0.0", From 8e4b058bbbe315f16c51aec9245a77966cf347cb Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 3 Feb 2016 18:23:41 +0000 Subject: [PATCH 8/8] apply CR comments --- src/ipfs-core/index.js | 57 +++++++++++++++++++++------------- tests/test-core/test-object.js | 50 ++++++++++++++--------------- 2 files changed, 59 insertions(+), 48 deletions(-) diff --git a/src/ipfs-core/index.js b/src/ipfs-core/index.js index dd6d3f5d5e..9d131a5850 100644 --- a/src/ipfs-core/index.js +++ b/src/ipfs-core/index.js @@ -19,8 +19,8 @@ function IPFS (repo) { if (!repo) { repo = defaultRepo() } - const bs = new BlockService(repo) - const ds = new DAGService(bs) + const blockS = new BlockService(repo) + const dagS = new DAGService(blockS) this.daemon = callback => { // 1. read repo to get peer data @@ -136,16 +136,16 @@ function IPFS (repo) { this.block = { get: (multihash, callback) => { - bs.getBlock(multihash, callback) + blockS.getBlock(multihash, callback) }, put: (block, callback) => { - bs.addBlock(block, callback) + blockS.addBlock(block, callback) }, del: (multihash, callback) => { - bs.deleteBlock(multihash, callback) + blockS.deleteBlock(multihash, callback) }, stat: (multihash, callback) => { - bs.getBlock(multihash, (err, block) => { + blockS.getBlock(multihash, (err, block) => { if (err) { return callback(err) } @@ -164,7 +164,7 @@ function IPFS (repo) { } var node = new DAGNode() var block = new Block(node.marshal()) - bs.addBlock(block, function (err) { + blockS.addBlock(block, function (err) { if (err) { return callback(err) } @@ -180,8 +180,10 @@ function IPFS (repo) { this.object.get(multihash, (err, obj) => { if (err) { return callback(err) } obj.data = Buffer.concat([obj.data, data]) - ds.add(obj, (err) => { - if (err) { return callback(err) } + dagS.add(obj, (err) => { + if (err) { + return callback(err) + } callback(null, obj.multihash()) }) }) @@ -190,8 +192,10 @@ function IPFS (repo) { this.object.get(multihash, (err, obj) => { if (err) { return callback(err) } obj.addRawLink(link) - ds.add(obj, (err) => { - if (err) { return callback(err) } + dagS.add(obj, (err) => { + if (err) { + return callback(err) + } callback(null, obj.multihash()) }) }) @@ -202,12 +206,13 @@ function IPFS (repo) { obj.links = obj.links.filter((link) => { if (link.hash.equals(multihashLink)) { return false - } else { - return true } + return true }) - ds.add(obj, (err) => { - if (err) { return callback(err) } + dagS.add(obj, (err) => { + if (err) { + return callback(err) + } callback(null, obj.multihash()) }) }) @@ -216,8 +221,10 @@ function IPFS (repo) { this.object.get(multihash, (err, obj) => { if (err) { return callback(err) } obj.data = data - ds.add(obj, (err) => { - if (err) { return callback(err) } + dagS.add(obj, (err) => { + if (err) { + return callback(err) + } callback(null, obj.multihash()) }) }) @@ -225,13 +232,17 @@ function IPFS (repo) { }, data: (multihash, callback) => { this.object.get(multihash, (err, obj) => { - if (err) { return callback(err) } + if (err) { + return callback(err) + } callback(null, obj.data) }) }, links: (multihash, callback) => { this.object.get(multihash, (err, obj) => { - if (err) { return callback(err) } + if (err) { + return callback(err) + } callback(null, obj.links) }) }, @@ -240,14 +251,14 @@ function IPFS (repo) { callback = options options = {} } - ds.get(multihash, callback) + dagS.get(multihash, callback) }, put: (dagNode, options, callback) => { if (typeof options === 'function') { callback = options options = {} } - ds.add(dagNode, callback) + dagS.add(dagNode, callback) }, stat: (multihash, options, callback) => { if (typeof options === 'function') { @@ -256,7 +267,9 @@ function IPFS (repo) { } this.object.get(multihash, (err, obj) => { - if (err) { return callback(err) } + if (err) { + return callback(err) + } var res = { NumLinks: obj.links.length, BlockSize: obj.marshal().length, diff --git a/tests/test-core/test-object.js b/tests/test-core/test-object.js index cba178318e..16dbd80202 100644 --- a/tests/test-core/test-object.js +++ b/tests/test-core/test-object.js @@ -10,21 +10,19 @@ const DAGNode = mDAG.DAGNode const DAGLink = mDAG.DAGLink // TODO use arrow funtions again when https://github.com/webpack/webpack/issues/1944 is fixed -describe('object', function () { +describe('object', () => { var ipfs - before(function (done) { + before((done) => { ipfs = new IPFS() done() }) - it('new', function (done) { - ipfs.object.new(function (err, obj) { + it('new', (done) => { + ipfs.object.new((err, obj) => { expect(err).to.not.exist - expect(obj).to.have.property('Size') - expect(obj.Size).to.equal(0) - expect(obj).to.have.property('Name') - expect(obj.Name).to.equal('') + expect(obj).to.have.property('Size', 0) + expect(obj).to.have.property('Name', '') expect(obj).to.have.property('Hash') expect(bs58.encode(obj.Hash).toString()) .to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') @@ -33,68 +31,68 @@ describe('object', function () { }) }) - it('patch append-data', function (done) { + it('patch append-data', (done) => { const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) - ipfs.object.patch.appendData(mh, new Buffer('data data'), function (err, multihash) { + ipfs.object.patch.appendData(mh, new Buffer('data data'), (err, multihash) => { expect(err).to.not.exist expect(mh).to.not.deep.equal(multihash) done() }) }) - it('patch add-link', function (done) { + it('patch add-link', (done) => { const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) - ipfs.object.patch.addLink(mh, new DAGLink('prev', 0, mh), function (err, multihash) { + ipfs.object.patch.addLink(mh, new DAGLink('prev', 0, mh), (err, multihash) => { expect(err).to.not.exist expect(mh).to.not.deep.equal(multihash) done() }) }) - it('patch rm-link', function (done) { + it('patch rm-link', (done) => { const rmmh = new Buffer(bs58.decode('QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V')) const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) - ipfs.object.patch.rmLink(mh, rmmh, function (err, multihash) { + ipfs.object.patch.rmLink(mh, rmmh, (err, multihash) => { expect(err).to.not.exist expect(mh).to.not.deep.equal(multihash) done() }) }) - it('patch set-data', function (done) { + it('patch set-data', (done) => { const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) - ipfs.object.patch.setData(mh, new Buffer('data data data'), function (err, multihash) { + ipfs.object.patch.setData(mh, new Buffer('data data data'), (err, multihash) => { expect(err).to.not.exist expect(mh).to.not.deep.equal(multihash) done() }) }) - it('data', function (done) { + it('data', (done) => { const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) - ipfs.object.data(mh, function (err, data) { + ipfs.object.data(mh, (err, data) => { expect(err).to.not.exist expect(data).to.deep.equal(new Buffer('\u0008\u0001')) done() }) }) - it('links', function (done) { + it('links', (done) => { const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) - ipfs.object.links(mh, function (err, links) { + ipfs.object.links(mh, (err, links) => { expect(err).to.not.exist expect(links.length).to.equal(6) done() }) }) - it('get', function (done) { + it('get', (done) => { const mh = new Buffer(bs58.decode('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')) - ipfs.object.get(mh, function (err, obj) { + ipfs.object.get(mh, (err, obj) => { expect(err).to.not.exist expect(obj.size()).to.equal(0) expect(obj).to.have.property('data') @@ -103,17 +101,17 @@ describe('object', function () { }) }) - it('put', function (done) { + it('put', (done) => { const node = new DAGNode(new Buffer('Hello, is it me you are looking for')) - ipfs.object.put(node, function (err) { + ipfs.object.put(node, (err) => { expect(err).to.not.exist done() }) }) - it('stat', function (done) { + it('stat', (done) => { const mh = new Buffer(bs58.decode('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG')) - ipfs.object.stat(mh, function (err, stats) { + ipfs.object.stat(mh, (err, stats) => { expect(err).to.not.exist var expected = {