From 68d92b60267f8547f8c95289098c1f0a494e9e70 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Fri, 17 Mar 2017 23:20:43 +0100 Subject: [PATCH] feat: datastore, ipfs-block and all the deps that were updated --- .gitignore | 3 + examples/dag/eth.js | 2 +- gulpfile.js | 45 ++---- package.json | 42 ++--- src/cli/bin.js | 41 ++++- src/cli/commands/bitswap/stat.js | 27 ++-- src/cli/commands/bitswap/unwant.js | 10 +- src/cli/commands/bitswap/wantlist.js | 14 +- src/cli/commands/block/get.js | 20 +-- src/cli/commands/block/put.js | 49 +++--- src/cli/commands/block/rm.js | 23 +-- src/cli/commands/block/stat.js | 19 +-- src/cli/commands/bootstrap/add.js | 17 +- src/cli/commands/bootstrap/list.js | 18 +-- src/cli/commands/bootstrap/rm.js | 14 +- src/cli/commands/config.js | 80 +++++----- src/cli/commands/config/edit.js | 145 ++++++++---------- src/cli/commands/config/replace.js | 23 +-- src/cli/commands/config/show.js | 11 +- src/cli/commands/daemon.js | 9 +- src/cli/commands/dag/get.js | 74 ++++----- src/cli/commands/files/add.js | 47 +++--- src/cli/commands/files/cat.js | 11 +- src/cli/commands/files/get.js | 32 ++-- src/cli/commands/id.js | 14 +- src/cli/commands/init.js | 7 +- src/cli/commands/object/data.js | 17 +- src/cli/commands/object/get.js | 11 +- src/cli/commands/object/links.js | 29 ++-- src/cli/commands/object/new.js | 7 +- src/cli/commands/object/patch/add-link.js | 56 ++----- src/cli/commands/object/patch/append-data.js | 16 +- src/cli/commands/object/patch/rm-link.js | 10 +- src/cli/commands/object/patch/set-data.js | 18 +-- src/cli/commands/object/put.js | 20 +-- src/cli/commands/object/stat.js | 21 +-- src/cli/commands/pubsub/ls.js | 17 +- src/cli/commands/pubsub/peers.js | 17 +- src/cli/commands/pubsub/pub.js | 17 +- src/cli/commands/pubsub/sub.js | 21 +-- src/cli/commands/repo/version.js | 13 +- src/cli/commands/swarm/addrs.js | 25 +-- src/cli/commands/swarm/addrs/local.js | 20 +-- src/cli/commands/swarm/connect.js | 13 +- src/cli/commands/swarm/disconnect.js | 13 +- src/cli/commands/swarm/peers.js | 33 ++-- src/cli/commands/version.js | 14 +- src/cli/utils.js | 18 ++- src/core/boot.js | 98 ++++++++++++ src/core/components/block.js | 36 ++--- src/core/components/init.js | 41 +++-- src/core/components/libp2p.js | 7 +- src/core/components/pre-start.js | 10 +- src/core/components/repo.js | 17 +- src/core/components/start.js | 57 ++++--- src/core/components/stop.js | 45 ++++-- src/core/default-repo-browser.js | 5 +- src/core/default-repo.js | 3 +- src/core/index.js | 46 +----- src/core/state.js | 65 ++++++++ src/core/utils.js | 14 -- src/http-api/error-handler.js | 3 +- src/http-api/index.js | 79 +++++----- src/http-api/resources/block.js | 23 +-- src/http-api/resources/config.js | 4 +- src/http-api/resources/files.js | 4 +- src/http-api/resources/object.js | 4 +- src/http-api/resources/swarm.js | 4 +- test/cli/block.js | 5 +- test/cli/config.js | 2 +- test/cli/init.js | 11 +- test/core/bitswap.spec.js | 38 ++--- test/core/create-node.spec.js | 80 +++++----- ...HSCRTXCYHGIOBXKWUMKFR3UPAFHQ5WK5362FQ.data | 0 ...DDTF32OIR4FWLKT5YLL7MLDVIT7DC3NHOK5VA.data | 0 ...NLTXZM4PW4VEFWQBJ4ACZQAS37BLGL4HUO5XY.data | Bin ...B5FJNHZPTSVA7IB6OHXSQ2XSVEEKMKK6RT75I.data | Bin ...7E32LQAL6236OUKZTMHPQSFIXPWXNZHQOV7JQ.data | 0 ...SIM3AK7AD554D3BWZPAGEAQYQOWMFZQDUUAEI.data | 0 ...YVY4BA22FPHUIODJEXS4LCTQDWA275XAJDAPI.data | Bin ...LZHQZUR2R3GECRFV3WPKNL7PL2SKFIL2LXC4Y.data | 0 ...774EZOYCYNHPRVFD53ZSAU7237F67XDSQGCYQ.data | Bin ...RTG5JS3JCUJDQM2DRB5RVF33DCUUOFJNGVDUI.data | Bin ...YNRNMP2VDKWBWGAEDDEJDACM3SGG3VIANDDXI.data | Bin ...LJNXLLHZOPGSL2PBC65D4UIVWM6TI5F5TAFNI.data | 0 ...VXLWEU4FWPVGT24VJT7DUZPTNLF25N25IGGQA.data | 0 ...WJNESD7XHQSXA5EGJVNTPVHD7444C2KLKXHDI.data | Bin ...AJNF2GF5UHUAGGHC6LLAH6VYDEKLQMD4QLILY.data | 0 ...2DCW7R77FRIVHQ3V5OIPPS3XQBX34KRPNOIRQ.data | 0 ...TJNUP57QHR4SKHZ74OIITBBGLOMCO3ZOLWLGA.data | 0 ...RYYVBUXHTS3SM5EORZDU63LYPEFUAFE4SBM4I.data | 0 ...33ROGCKOFZLXJJ2MPKYZBTV4H3N7GYHXMAO6A.data | 0 ...HTQ7R247ZI7KJWP3QWPQYS43LFULQC5ANLQFI.data | Bin ...57JSEZN64SIJ5OIHSGJG4TJSSJLGI3PBJLQVI.data | 0 ...JOCHWXDRK5EXZQILBCKAPEDUJENZ5B5HJ5R3A.data | 0 ...LP4OS5EAVHFMCX2HD7FZUC2B3WUU3D4LGKS5A.data | 0 test/go-ipfs-repo/blocks/SHARDING | 1 + ...D6VA64TGWP67KYA3AIMBUMVWGZ5AQN2L2HSWQ.data | Bin ...ANT6IBNTFN7WR5RPD5F6GN6MBKUUO25DNOTWQ.data | Bin ...NH7VEGIQJRPL6J7FT2XYVKAXT4MQPXXPUYUNY.data | Bin ...XCX5XMZLMGTYVODWPEDIW7JYEG7YXBIA7IUWY.data | 0 ...LOQ45VUM2GUZCGAXLWCTOKKPGTUWPXHBNIVOY.data | 0 ...2BFAGLXEZL4UWFNWM4LFTLMXQBCERZ6CMLX3Y.data | 0 ...2FSMG76VV256I4PXBULZZ5ASNLK4FL4EG7XOI.data | Bin ...5KVN6ALXC6QRHK2X4R6EUFRMBB5OSFO2FUYDQ.data | 0 ...EBXFD5QN6MTI5YBYMCVQJDXPKCOVR6RMLHZFQ.data | Bin test/go-ipfs-repo/blocks/_README | 30 ++++ test/{test-data => }/go-ipfs-repo/config | 0 .../go-ipfs-repo/datastore/000002.ldb | Bin .../go-ipfs-repo/datastore/000005.ldb | Bin .../go-ipfs-repo/datastore/000010.ldb | Bin .../go-ipfs-repo/datastore/CURRENT | 0 .../go-ipfs-repo/datastore/LOCK | 0 .../go-ipfs-repo/datastore/LOG | 0 .../go-ipfs-repo/datastore/LOG.old | 0 .../go-ipfs-repo/datastore/MANIFEST-000014 | Bin test/go-ipfs-repo/version | 1 + test/http-api/index.js | 9 +- test/http-api/over-ipfs-api/block.js | 7 +- test/http-api/over-ipfs-api/object.js | 4 +- test/http-api/spec/config.js | 2 +- test/interop/daemons/go.js | 4 +- test/interop/daemons/js.js | 105 +++++-------- test/interop/index.js | 4 +- test/interop/repo.js | 15 +- test/test-data/go-ipfs-repo/version | 1 - test/utils/create-repo-browser.js | 14 +- test/utils/create-repo-node.js | 17 +- test/utils/ipfs-factory-daemon/index.js | 37 ++--- test/utils/ipfs-factory-instance/index.js | 7 +- test/utils/on-and-off.js | 2 +- 131 files changed, 984 insertions(+), 1200 deletions(-) create mode 100644 src/core/boot.js create mode 100644 src/core/state.js rename test/{test-data/go-ipfs-repo/blocks/CIQEU => go-ipfs-repo/blocks/2F}/CIQEUWUVLBXVFYSYCHHSCRTXCYHGIOBXKWUMKFR3UPAFHQ5WK5362FQ.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQFF => go-ipfs-repo/blocks/5V}/CIQFFRR4O52TS2Z7QLDDTF32OIR4FWLKT5YLL7MLDVIT7DC3NHOK5VA.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQJ2 => go-ipfs-repo/blocks/5X}/CIQJ23BL4UHXA2KTI6NLTXZM4PW4VEFWQBJ4ACZQAS37BLGL4HUO5XY.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQMB => go-ipfs-repo/blocks/75}/CIQMB7DLJFKD267QJ2B5FJNHZPTSVA7IB6OHXSQ2XSVEEKMKK6RT75I.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQKK => go-ipfs-repo/blocks/7J}/CIQKKLBWAIBQZOIS5X7E32LQAL6236OUKZTMHPQSFIXPWXNZHQOV7JQ.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQON => go-ipfs-repo/blocks/AE}/CIQONICFQZH7QVU6IPSIM3AK7AD554D3BWZPAGEAQYQOWMFZQDUUAEI.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQHA => go-ipfs-repo/blocks/AP}/CIQHAKDLTL5GMIFGN5YVY4BA22FPHUIODJEXS4LCTQDWA275XAJDAPI.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQDD => go-ipfs-repo/blocks/C4}/CIQDDZ5EDQK5AP7LRTLZHQZUR2R3GECRFV3WPKNL7PL2SKFIL2LXC4Y.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQDM => go-ipfs-repo/blocks/CY}/CIQDMKFEUGKSLXMEXO774EZOYCYNHPRVFD53ZSAU7237F67XDSQGCYQ.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQLB => go-ipfs-repo/blocks/DU}/CIQLBK52T5EHVHZY5URTG5JS3JCUJDQM2DRB5RVF33DCUUOFJNGVDUI.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQPD => go-ipfs-repo/blocks/DX}/CIQPDQJBGYDZNMOAGGYNRNMP2VDKWBWGAEDDEJDACM3SGG3VIANDDXI.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQIX => go-ipfs-repo/blocks/FN}/CIQIXBZMUTXFC5QIGMLJNXLLHZOPGSL2PBC65D4UIVWM6TI5F5TAFNI.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQH7 => go-ipfs-repo/blocks/GQ}/CIQH7OEYWXL34RWYL7VXLWEU4FWPVGT24VJT7DUZPTNLF25N25IGGQA.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQDD => go-ipfs-repo/blocks/HD}/CIQDDVW2EZIJF4NQH7WJNESD7XHQSXA5EGJVNTPVHD7444C2KLKXHDI.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQJF => go-ipfs-repo/blocks/IL}/CIQJFGRQHQ45VCQLM7AJNF2GF5UHUAGGHC6LLAH6VYDEKLQMD4QLILY.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQER => go-ipfs-repo/blocks/IR}/CIQERMRAAFXUAUOX3V2DCW7R77FRIVHQ3V5OIPPS3XQBX34KRPNOIRQ.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQJB => go-ipfs-repo/blocks/LG}/CIQJBQD2O6K4CGJVCCTJNUP57QHR4SKHZ74OIITBBGLOMCO3ZOLWLGA.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQOL => go-ipfs-repo/blocks/M4}/CIQOLBQZSZAODJGGH6RYYVBUXHTS3SM5EORZDU63LYPEFUAFE4SBM4I.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQOY => go-ipfs-repo/blocks/O6}/CIQOYW2THIZBRGI7IN33ROGCKOFZLXJJ2MPKYZBTV4H3N7GYHXMAO6A.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQGP => go-ipfs-repo/blocks/QF}/CIQGPALRQ24P6NS4OWHTQ7R247ZI7KJWP3QWPQYS43LFULQC5ANLQFI.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQOH => go-ipfs-repo/blocks/QV}/CIQOHMGEIKMPYHAUTL57JSEZN64SIJ5OIHSGJG4TJSSJLGI3PBJLQVI.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQBE => go-ipfs-repo/blocks/R3}/CIQBED3K6YA5I3QQWLJOCHWXDRK5EXZQILBCKAPEDUJENZ5B5HJ5R3A.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQHB => go-ipfs-repo/blocks/S5}/CIQHBGZNZRPWVEFNMTLP4OS5EAVHFMCX2HD7FZUC2B3WUU3D4LGKS5A.data (100%) create mode 100644 test/go-ipfs-repo/blocks/SHARDING rename test/{test-data/go-ipfs-repo/blocks/CIQHP => go-ipfs-repo/blocks/SW}/CIQHPUVCWD6JA6AFUVD6VA64TGWP67KYA3AIMBUMVWGZ5AQN2L2HSWQ.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQFE => go-ipfs-repo/blocks/TW}/CIQFEAGMNNXXTYKYQSANT6IBNTFN7WR5RPD5F6GN6MBKUUO25DNOTWQ.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQOM => go-ipfs-repo/blocks/UN}/CIQOMBKARLB7PAITVSNH7VEGIQJRPL6J7FT2XYVKAXT4MQPXXPUYUNY.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQDV => go-ipfs-repo/blocks/UW}/CIQDVKITASFS55MC2TXCX5XMZLMGTYVODWPEDIW7JYEG7YXBIA7IUWY.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQGF => go-ipfs-repo/blocks/VO}/CIQGFTQ7FSI2COUXWWLOQ45VUM2GUZCGAXLWCTOKKPGTUWPXHBNIVOY.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQFT => go-ipfs-repo/blocks/X3}/CIQFTFEEHEDF6KLBT32BFAGLXEZL4UWFNWM4LFTLMXQBCERZ6CMLX3Y.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQJG => go-ipfs-repo/blocks/XO}/CIQJGO2B2N75IUEM372FSMG76VV256I4PXBULZZ5ASNLK4FL4EG7XOI.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQEN => go-ipfs-repo/blocks/YD}/CIQENVCICS44LLYUDQ5KVN6ALXC6QRHK2X4R6EUFRMBB5OSFO2FUYDQ.data (100%) rename test/{test-data/go-ipfs-repo/blocks/CIQLB => go-ipfs-repo/blocks/ZF}/CIQLBS5HG4PRCRQ7O4EBXFD5QN6MTI5YBYMCVQJDXPKCOVR6RMLHZFQ.data (100%) create mode 100644 test/go-ipfs-repo/blocks/_README rename test/{test-data => }/go-ipfs-repo/config (100%) rename test/{test-data => }/go-ipfs-repo/datastore/000002.ldb (100%) rename test/{test-data => }/go-ipfs-repo/datastore/000005.ldb (100%) rename test/{test-data => }/go-ipfs-repo/datastore/000010.ldb (100%) rename test/{test-data => }/go-ipfs-repo/datastore/CURRENT (100%) rename test/{test-data => }/go-ipfs-repo/datastore/LOCK (100%) rename test/{test-data => }/go-ipfs-repo/datastore/LOG (100%) rename test/{test-data => }/go-ipfs-repo/datastore/LOG.old (100%) rename test/{test-data => }/go-ipfs-repo/datastore/MANIFEST-000014 (100%) create mode 100644 test/go-ipfs-repo/version delete mode 100644 test/test-data/go-ipfs-repo/version diff --git a/.gitignore b/.gitignore index 254988dc81..1f5aa33552 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,6 @@ node_modules lib dist +test/test-data/go-ipfs-repo/LOCK +test/test-data/go-ipfs-repo/LOG +test/test-data/go-ipfs-repo/LOG.old \ No newline at end of file diff --git a/examples/dag/eth.js b/examples/dag/eth.js index e1a19f3296..370037d6e0 100644 --- a/examples/dag/eth.js +++ b/examples/dag/eth.js @@ -30,7 +30,7 @@ createNode((err, ipfs) => { const cid = new CID(1, 'eth-block', multihash) // console.log(cid.toBaseEncodedString()) - ipfs.block.put(new Block(data), cid, cb) + ipfs.block.put(new Block(data, cid), cb) }) }, (err) => { if (err) { diff --git a/gulpfile.js b/gulpfile.js index 6172040832..1ef4ca0c9e 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -4,7 +4,6 @@ const gulp = require('gulp') const parallel = require('async/parallel') const series = require('async/series') const createTempRepo = require('./test/utils/create-repo-node.js') -const IPFS = require('./src/core') const HTTPAPI = require('./src/http-api') const leftPad = require('left-pad') @@ -16,38 +15,26 @@ let nodes = [] function spawnDaemon (num, callback) { num = leftPad(num, 3, 0) - const node = new IPFS({ - repo: createTempRepo(), - init: { - bits: 1024 + const config = { + Addresses: { + Swarm: [ + `/ip4/127.0.0.1/tcp/10${num}`, + `/ip4/127.0.0.1/tcp/20${num}/ws` + ], + API: `/ip4/127.0.0.1/tcp/31${num}`, + Gateway: `/ip4/127.0.0.1/tcp/32${num}` }, - start: false, - EXPERIMENTAL: { - pubsub: true - }, - config: { - Addresses: { - Swarm: [ - `/ip4/127.0.0.1/tcp/10${num}`, - `/ip4/127.0.0.1/tcp/20${num}/ws` - ], - API: `/ip4/127.0.0.1/tcp/31${num}`, - Gateway: `/ip4/127.0.0.1/tcp/32${num}` - }, - Bootstrap: [], - Discovery: { - MDNS: { - Enabled: false - } + Bootstrap: [], + Discovery: { + MDNS: { + Enabled: false } } - }) + } - node.on('init', () => { - const daemon = new HTTPAPI(node.repo.path()) - nodes.push(daemon) - setTimeout(() => daemon.start(callback), 400) - }) + const daemon = new HTTPAPI(createTempRepo(), config) + nodes.push(daemon) + daemon.start(true, callback) } gulp.task('libnode:start', (done) => { diff --git a/package.json b/package.json index 2808ba3a0d..997f605b88 100644 --- a/package.json +++ b/package.json @@ -63,19 +63,19 @@ }, "homepage": "https://github.com/ipfs/js-ipfs#readme", "devDependencies": { - "aegir": "^11.0.0", + "aegir": "^11.0.1", "buffer-loader": "0.0.1", "chai": "^3.5.0", - "delay": "^1.3.1", + "delay": "^2.0.0", "detect-node": "^2.0.3", "dir-compare": "^1.3.0", "dirty-chai": "^1.2.2", - "eslint-plugin-react": "^6.10.0", - "execa": "^0.6.1", + "eslint-plugin-react": "^6.10.3", + "execa": "^0.6.3", "expose-loader": "^0.7.3", "form-data": "^2.1.2", "gulp": "^3.9.1", - "interface-ipfs-core": "~0.25.0", + "interface-ipfs-core": "~0.26.0", "ipfsd-ctl": "~0.20.0", "left-pad": "^1.1.3", "lodash": "^4.17.4", @@ -93,36 +93,35 @@ "dependencies": { "async": "^2.1.5", "bl": "^1.2.0", - "boom": "^4.2.0", + "boom": "^4.3.0", "cids": "^0.4.2", "debug": "^2.6.3", - "fs-pull-blob-store": "~0.4.1", + "fsm-event": "^2.1.0", "glob": "^7.1.1", "hapi": "^16.1.0", "hapi-set-header": "^1.0.2", "hoek": "^4.1.0", - "idb-pull-blob-store": "~0.5.1", - "ipfs-api": "^12.1.7", - "ipfs-bitswap": "~0.9.5", - "ipfs-block": "~0.5.5", - "ipfs-block-service": "~0.8.3", + "ipfs-api": "^13.0.0", + "ipfs-bitswap": "~0.10.0", + "ipfs-block": "~0.6.0", + "ipfs-block-service": "~0.9.0", "ipfs-multipart": "~0.1.0", - "ipfs-repo": "~0.11.3", + "ipfs-repo": "~0.13.0", "ipfs-unixfs": "~0.1.11", - "ipfs-unixfs-engine": "~0.17.0", - "ipld-resolver": "~0.10.1", + "ipfs-unixfs-engine": "~0.18.0", + "ipld-resolver": "~0.11.0", "isstream": "^0.1.2", - "joi": "^10.2.2", - "libp2p-floodsub": "~0.7.4", - "libp2p-ipfs-browser": "~0.20.0", - "libp2p-ipfs-nodejs": "~0.20.0", + "joi": "^10.3.0", + "libp2p-floodsub": "~0.7.5", + "libp2p-ipfs-browser": "~0.20.1", + "libp2p-ipfs-nodejs": "~0.20.1", "lodash.flatmap": "^4.5.0", "lodash.get": "^4.4.2", "lodash.has": "^4.5.2", "lodash.set": "^4.3.2", "lodash.sortby": "^4.7.0", "lodash.values": "^4.3.0", - "mafmt": "^2.1.6", + "mafmt": "^2.1.7", "mkdirp": "^0.5.1", "multiaddr": "^2.2.2", "multihashes": "~0.4.4", @@ -141,12 +140,13 @@ "pull-zip": "^2.0.1", "read-pkg-up": "^2.0.0", "readable-stream": "1.1.14", + "safe-buffer": "^5.0.1", "stream-to-pull-stream": "^1.7.2", "tar-stream": "^1.5.2", "temp": "^0.8.3", "through2": "^2.0.3", "update-notifier": "^2.1.0", - "yargs": "^6.6.0" + "yargs": "7.0.2" }, "contributors": [ "Andrew de Andrade ", diff --git a/src/cli/bin.js b/src/cli/bin.js index d3d86b7c5d..8b99c13476 100755 --- a/src/cli/bin.js +++ b/src/cli/bin.js @@ -5,6 +5,7 @@ const yargs = require('yargs') const updateNotifier = require('update-notifier') const readPkgUp = require('read-pkg-up') +const utils = require('./utils') const pkg = readPkgUp.sync({cwd: __dirname}).pkg updateNotifier({ @@ -14,7 +15,7 @@ updateNotifier({ const cli = yargs .commandDir('commands') - .demand(1) + .demandCommand(1) // NOTE: This creates an alias of // `jsipfs files {add, get, cat}` to `jsipfs {add, get, cat}`. @@ -27,9 +28,35 @@ aliases.forEach((alias) => { cli.command(alias.command, alias.describe, alias.builder, alias.handler) }) -// finalize cli setup -cli // eslint-disable-line - .help() - .strict() - .completion() - .argv +const args = process.argv.slice(2) + +// Need to skip to avoid locking as these commands +// don't require a daemon +if (args[0] === 'daemon' || args[0] === 'init') { + return cli + .help() + .strict(false) + .completion() + .parse(args) +} + +utils.getIPFS((err, ipfs, cleanup) => { + if (err) { + throw err + } + + // finalize cli setup + cli // eslint-disable-line + .help() + .strict(false) + .completion() + .parse(args, { + ipfs: ipfs + }, (err, argv, output) => { + cleanup(() => { + if (err) { + throw err + } + }) + }) +}) diff --git a/src/cli/commands/bitswap/stat.js b/src/cli/commands/bitswap/stat.js index f9e4b8a879..6a404cdd40 100644 --- a/src/cli/commands/bitswap/stat.js +++ b/src/cli/commands/bitswap/stat.js @@ -1,6 +1,5 @@ 'use strict' -const utils = require('../../utils') const CID = require('cids') module.exports = { @@ -10,26 +9,21 @@ module.exports = { builder: {}, - handler () { - utils.getIPFS((err, ipfs) => { + handler (argv) { + argv.ipfs.bitswap.stat((err, stats) => { if (err) { throw err } - ipfs.bitswap.stat((err, stats) => { - if (err) { - throw err - } - - stats.Wantlist = stats.Wantlist || [] - stats.Wantlist = stats.Wantlist.map((entry) => { - const buf = new Buffer(entry.cid.hash.data) - const cid = new CID(entry.cid.version, entry.cid.codec, buf) - return cid.toBaseEncodedString() - }) - stats.Peers = stats.Peers || [] + stats.Wantlist = stats.Wantlist || [] + stats.Wantlist = stats.Wantlist.map((entry) => { + const buf = new Buffer(entry.cid.hash.data) + const cid = new CID(entry.cid.version, entry.cid.codec, buf) + return cid.toBaseEncodedString() + }) + stats.Peers = stats.Peers || [] - console.log(`bitswap status + console.log(`bitswap status blocks received: ${stats.BlocksReceived} dup blocks received: ${stats.DupBlksReceived} dup data received: ${stats.DupDataReceived}B @@ -37,7 +31,6 @@ module.exports = { ${stats.Wantlist.join('\n ')} partners [${stats.Peers.length}] ${stats.Peers.join('\n ')}`) - }) }) } } diff --git a/src/cli/commands/bitswap/unwant.js b/src/cli/commands/bitswap/unwant.js index 7a0feb533d..840276e9b2 100644 --- a/src/cli/commands/bitswap/unwant.js +++ b/src/cli/commands/bitswap/unwant.js @@ -1,19 +1,11 @@ 'use strict' -const utils = require('../../utils') - module.exports = { command: 'unwant ', describe: 'Remove a given block from your wantlist.', handler (argv) { - utils.getIPFS((err, ipfs) => { - if (err) { - throw err - } - - throw new Error('Not implemented yet') - }) + throw new Error('Not implemented yet') } } diff --git a/src/cli/commands/bitswap/wantlist.js b/src/cli/commands/bitswap/wantlist.js index 41e3dc5607..94a38e6dcf 100644 --- a/src/cli/commands/bitswap/wantlist.js +++ b/src/cli/commands/bitswap/wantlist.js @@ -1,7 +1,5 @@ 'use strict' -const utils = require('../../utils') - module.exports = { command: 'wantlist', @@ -17,18 +15,12 @@ module.exports = { handler (argv) { // TODO: handle argv.peer - utils.getIPFS((err, ipfs) => { + argv.ipfs.bitswap.wantlist((err, res) => { if (err) { throw err } - - ipfs.bitswap.wantlist((err, res) => { - if (err) { - throw err - } - res.Keys.forEach((cidStr) => { - console.log(cidStr) - }) + res.Keys.forEach((cidStr) => { + console.log(cidStr) }) }) } diff --git a/src/cli/commands/block/get.js b/src/cli/commands/block/get.js index e8a653be0f..09c7a5a98f 100644 --- a/src/cli/commands/block/get.js +++ b/src/cli/commands/block/get.js @@ -1,10 +1,6 @@ 'use strict' -const utils = require('../../utils') const CID = require('cids') -const debug = require('debug') -const log = debug('cli:block') -log.error = debug('cli:block:error') module.exports = { command: 'get ', @@ -14,21 +10,15 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { + const cid = new CID(argv.key) + + argv.ipfs.block.get(cid, (err, block) => { if (err) { throw err } - const cid = new CID(argv.key) - - ipfs.block.get(cid, (err, block) => { - if (err) { - throw err - } - - process.stdout.write(block.data) - process.stdout.write('\n') - }) + process.stdout.write(block.data) + process.stdout.write('\n') }) } } diff --git a/src/cli/commands/block/put.js b/src/cli/commands/block/put.js index 14895e96d7..73814f7c37 100644 --- a/src/cli/commands/block/put.js +++ b/src/cli/commands/block/put.js @@ -1,41 +1,32 @@ 'use strict' -const utils = require('../../utils') const CID = require('cids') const multihashing = require('multihashing-async') const bl = require('bl') const fs = require('fs') const Block = require('ipfs-block') const waterfall = require('async/waterfall') -const debug = require('debug') -const log = debug('cli:block') -log.error = debug('cli:block:error') function addBlock (data, opts) { - utils.getIPFS((err, ipfs) => { + const ipfs = opts.ipfs + let cid + + waterfall([ + (cb) => multihashing(data, opts.mhtype || 'sha2-256', cb), + (multihash, cb) => { + if (opts.format !== 'dag-pb' || opts.version !== 0) { + cid = new CID(1, opts.format || 'dag-pb', multihash) + } else { + cid = new CID(0, 'dag-pb', multihash) + } + + ipfs.block.put(new Block(data, cid), cb) + } + ], (err) => { if (err) { throw err } - - let cid - - waterfall([ - (cb) => multihashing(data, opts.mhtype || 'sha2-256', cb), - (multihash, cb) => { - if (!opts.version || opts.version !== 0) { - cid = new CID(1, opts.format || 'dag-pb', multihash) - } else { - cid = new CID(0, 'dag-pb', multihash) - } - cb(null, cid) - }, - (cid, cb) => ipfs.block.put(new Block(data), cid, cb) - ], (err) => { - if (err) { throw err } - - // console.log(cid) - console.log(cid.toBaseEncodedString()) - }) + console.log(cid.toBaseEncodedString()) }) } @@ -57,12 +48,18 @@ module.exports = { mhlen: { describe: 'multihash hash length', default: undefined + }, + version: { + describe: 'cid version', + type: 'number', + default: 0 } }, handler (argv) { if (argv.block) { - return addBlock(fs.readFileSync(argv.block), argv) + const buf = fs.readFileSync(argv.block) + return addBlock(buf, argv) } process.stdin.pipe(bl((err, input) => { diff --git a/src/cli/commands/block/rm.js b/src/cli/commands/block/rm.js index 87fe66891b..729a1fd6f9 100644 --- a/src/cli/commands/block/rm.js +++ b/src/cli/commands/block/rm.js @@ -2,9 +2,6 @@ const utils = require('../../utils') const mh = require('multihashes') -const debug = require('debug') -const log = debug('cli:block') -log.error = debug('cli:block:error') module.exports = { command: 'rm ', @@ -14,23 +11,17 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { + if (utils.isDaemonOn()) { + // TODO implement this once `js-ipfs-api` supports it + throw new Error('rm block with daemon running is not yet implemented') + } + + argv.ipfs.block.del(mh.fromB58String(argv.key), (err) => { if (err) { throw err } - if (utils.isDaemonOn()) { - // TODO implement this once `js-ipfs-api` supports it - throw new Error('rm block with daemon running is not yet implemented') - } - - ipfs.block.del(mh.fromB58String(argv.key), (err) => { - if (err) { - throw err - } - - console.log('removed', argv.key) - }) + console.log('removed', argv.key) }) } } diff --git a/src/cli/commands/block/stat.js b/src/cli/commands/block/stat.js index 73c82aff9e..c4df1e35d9 100644 --- a/src/cli/commands/block/stat.js +++ b/src/cli/commands/block/stat.js @@ -1,10 +1,6 @@ 'use strict' -const utils = require('../../utils') -const debug = require('debug') const CID = require('cids') -const log = debug('cli:block') -log.error = debug('cli:block:error') module.exports = { command: 'stat ', @@ -14,16 +10,13 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { - if (err) { throw err } - const cid = new CID(argv.key) + argv.ipfs.block.stat(new CID(argv.key), (err, stats) => { + if (err) { + throw err + } - ipfs.block.stat(cid, (err, stats) => { - if (err) { throw err } - - console.log('Key:', stats.key) - console.log('Size:', stats.size) - }) + console.log('Key:', stats.key) + console.log('Size:', stats.size) }) } } diff --git a/src/cli/commands/bootstrap/add.js b/src/cli/commands/bootstrap/add.js index 6331013555..da0ed43805 100644 --- a/src/cli/commands/bootstrap/add.js +++ b/src/cli/commands/bootstrap/add.js @@ -1,10 +1,5 @@ 'use strict' -const debug = require('debug') -const log = debug('cli:bootstrap') -const utils = require('../../utils') -log.error = debug('cli:bootstrap:error') - module.exports = { command: 'add []', @@ -19,18 +14,14 @@ module.exports = { }, handler (argv) { - utils.getIPFS((err, ipfs) => { + argv.ipfs.bootstrap.add(argv.peer, { + default: argv.default + }, (err, list) => { if (err) { throw err } - ipfs.bootstrap.add(argv.peer, {default: argv.default}, (err, list) => { - if (err) { - throw err - } - - list.Peers.forEach((l) => console.log(l)) - }) + list.Peers.forEach((l) => console.log(l)) }) } } diff --git a/src/cli/commands/bootstrap/list.js b/src/cli/commands/bootstrap/list.js index 95a88e45fb..7cf0bd5b29 100644 --- a/src/cli/commands/bootstrap/list.js +++ b/src/cli/commands/bootstrap/list.js @@ -1,10 +1,5 @@ 'use strict' -const utils = require('../../utils') -const debug = require('debug') -const log = debug('cli:bootstrap') -log.error = debug('cli:bootstrap:error') - module.exports = { command: 'list', @@ -12,19 +7,14 @@ module.exports = { builder: {}, - handler () { - utils.getIPFS((err, ipfs) => { + handler (argv) { + argv.ipfs.bootstrap.list((err, list) => { if (err) { throw err } - ipfs.bootstrap.list((err, list) => { - if (err) { - throw err - } - list.Peers.forEach((node) => { - console.log(node) - }) + list.Peers.forEach((node) => { + console.log(node) }) }) } diff --git a/src/cli/commands/bootstrap/rm.js b/src/cli/commands/bootstrap/rm.js index ec440528dc..1a2462a9ed 100644 --- a/src/cli/commands/bootstrap/rm.js +++ b/src/cli/commands/bootstrap/rm.js @@ -3,8 +3,6 @@ const debug = require('debug') const log = debug('cli:bootstrap') log.error = debug('cli:bootstrap:error') -const utils = require('../../utils') - module.exports = { command: 'rm []', @@ -19,18 +17,14 @@ module.exports = { }, handler (argv) { - utils.getIPFS((err, ipfs) => { + argv.ipfs.bootstrap.rm(argv.peer, { + all: argv.all + }, (err, list) => { if (err) { throw err } - ipfs.bootstrap.rm(argv.peer, {all: argv.all}, (err, list) => { - if (err) { - throw err - } - - list.Peers.forEach((l) => console.log(l)) - }) + list.Peers.forEach((l) => console.log(l)) }) } } diff --git a/src/cli/commands/config.js b/src/cli/commands/config.js index 4f9b0b993f..d2a3828f2f 100644 --- a/src/cli/commands/config.js +++ b/src/cli/commands/config.js @@ -1,10 +1,5 @@ 'use strict' -const debug = require('debug') -const log = debug('cli:config') -log.error = debug('cli:config:error') -const utils = require('../utils') - module.exports = { command: 'config [value]', @@ -16,17 +11,21 @@ module.exports = { .options({ bool: { type: 'boolean', - default: false + default: false, + global: false }, json: { type: 'boolean', - default: false + default: false, + global: false } }) }, handler (argv) { - if (argv._handled) return + if (argv._handled) { + return + } argv._handled = true const bool = argv.bool @@ -34,46 +33,37 @@ module.exports = { const key = argv.key let value = argv.value - utils.getIPFS((err, ipfs) => { - if (err) { - throw err - } - - if (!value) { - // Get the value of a given key - ipfs.config.get(key, (err, value) => { - if (err) { - log.error(err) - throw new Error('failed to read the config') - } - - if (typeof value === 'object') { - console.log(JSON.stringify(value, null, 2)) - } else { - console.log(value) - } - }) - } else { - // Set the new value of a given key + if (!value) { + // Get the value of a given key + argv.ipfs.config.get(key, (err, value) => { + if (err) { + throw new Error('failed to read the config') + } - if (bool) { - value = (value === 'true') - } else if (json) { - try { - value = JSON.parse(value) - } catch (err) { - log.error(err) - throw new Error('invalid JSON provided') - } + if (typeof value === 'object') { + console.log(JSON.stringify(value, null, 2)) + } else { + console.log(value) } + }) + } else { + // Set the new value of a given key - ipfs.config.set(key, value, (err) => { - if (err) { - log.error(err) - throw new Error('failed to read the config') - } - }) + if (bool) { + value = (value === 'true') + } else if (json) { + try { + value = JSON.parse(value) + } catch (err) { + throw new Error('invalid JSON provided') + } } - }) + + argv.ipfs.config.set(key, value, (err) => { + if (err) { + throw new Error('failed to read the config') + } + }) + } } } diff --git a/src/cli/commands/config/edit.js b/src/cli/commands/config/edit.js index 91900578ce..4d0c5ef865 100644 --- a/src/cli/commands/config/edit.js +++ b/src/cli/commands/config/edit.js @@ -4,9 +4,7 @@ const spawn = require('child_process').spawn const fs = require('fs') const temp = require('temp') const waterfall = require('async/waterfall') -const debug = require('debug') -const log = debug('cli:config') -log.error = debug('cli:config:error') + const utils = require('../../utils') module.exports = { @@ -20,101 +18,90 @@ module.exports = { if (argv._handled) return argv._handled = true - utils.getIPFS((err, ipfs) => { - if (err) { - throw err - } - const editor = process.env.EDITOR + const editor = process.env.EDITOR - if (!editor) { - throw new Error('ENV variable $EDITOR not set') - } + if (!editor) { + throw new Error('ENV variable $EDITOR not set') + } - function getConfig (next) { - ipfs.config.get((err, config) => { - if (err) { - log.error(err) - next(new Error('failed to get the config')) - } + function getConfig (next) { + argv.ipfs.config.get((err, config) => { + if (err) { + next(new Error('failed to get the config')) + } - next(null, config) - }) - } + next(null, config) + }) + } - function saveTempConfig (config, next) { - temp.open('ipfs-config', (err, info) => { + function saveTempConfig (config, next) { + temp.open('ipfs-config', (err, info) => { + if (err) { + next(new Error('failed to open the config')) + } + + fs.write(info.fd, JSON.stringify(config, null, 2)) + fs.close(info.fd, (err) => { if (err) { - log.error(err) next(new Error('failed to open the config')) } - - fs.write(info.fd, JSON.stringify(config, null, 2)) - fs.close(info.fd, (err) => { - if (err) { - log.error(err) - next(new Error('failed to open the config')) - } - }) - - next(null, info.path) }) - } - function openEditor (path, next) { - const child = spawn(editor, [path], { - stdio: 'inherit' - }) - - child.on('exit', (err, code) => { - if (err) { - throw new Error('error on the editor') - } + next(null, info.path) + }) + } - next(null, path) - }) - } + function openEditor (path, next) { + const child = spawn(editor, [path], { + stdio: 'inherit' + }) - function readTempConfig (path, next) { - fs.readFile(path, 'utf8', (err, data) => { - if (err) { - log.error(err) - next(new Error('failed to get the updated config')) - } + child.on('exit', (err, code) => { + if (err) { + throw new Error('error on the editor') + } - try { - next(null, JSON.parse(data)) - } catch (err) { - log.error(err) - next(new Error(`failed to parse the updated config "${err.message}"`)) - } - }) - } + next(null, path) + }) + } - function saveConfig (config, next) { - config = utils.isDaemonOn() - ? new Buffer(JSON.stringify(config)) : config + function readTempConfig (path, next) { + fs.readFile(path, 'utf8', (err, data) => { + if (err) { + next(new Error('failed to get the updated config')) + } - ipfs.config.replace(config, (err) => { - if (err) { - log.error(err) - next(new Error('failed to save the config')) - } + try { + next(null, JSON.parse(data)) + } catch (err) { + next(new Error(`failed to parse the updated config "${err.message}"`)) + } + }) + } - next() - }) - } + function saveConfig (config, next) { + config = utils.isDaemonOn() + ? new Buffer(JSON.stringify(config)) : config - waterfall([ - getConfig, - saveTempConfig, - openEditor, - readTempConfig, - saveConfig - ], (err) => { + argv.ipfs.config.replace(config, (err) => { if (err) { - throw err + next(new Error('failed to save the config')) } + + next() }) + } + + waterfall([ + getConfig, + saveTempConfig, + openEditor, + readTempConfig, + saveConfig + ], (err) => { + if (err) { + throw err + } }) } } diff --git a/src/cli/commands/config/replace.js b/src/cli/commands/config/replace.js index fd1a075c97..eef322aaf3 100644 --- a/src/cli/commands/config/replace.js +++ b/src/cli/commands/config/replace.js @@ -1,11 +1,8 @@ 'use strict' -const debug = require('debug') const path = require('path') -const log = debug('cli:config') -log.error = debug('cli:config:error') -const utils = require('../../utils') const fs = require('fs') +const utils = require('../../utils') module.exports = { command: 'replace ', @@ -18,21 +15,15 @@ module.exports = { if (argv._handled) return argv._handled = true - utils.getIPFS((err, ipfs) => { + const filePath = path.resolve(process.cwd(), argv.file) + + const config = utils.isDaemonOn() + ? filePath : JSON.parse(fs.readFileSync(filePath, 'utf8')) + + argv.ipfs.config.replace(config, (err) => { if (err) { throw err } - - const filePath = path.resolve(process.cwd(), argv.file) - - const config = utils.isDaemonOn() - ? filePath : JSON.parse(fs.readFileSync(filePath, 'utf8')) - - ipfs.config.replace(config, (err) => { - if (err) { - throw err - } - }) }) } } diff --git a/src/cli/commands/config/show.js b/src/cli/commands/config/show.js index 838f4ac6a2..45c9c615e2 100644 --- a/src/cli/commands/config/show.js +++ b/src/cli/commands/config/show.js @@ -3,8 +3,6 @@ const debug = require('debug') const log = debug('cli:config') log.error = debug('cli:config:error') -const utils = require('../../utils') - module.exports = { command: 'show', @@ -16,17 +14,12 @@ module.exports = { if (argv._handled) return argv._handled = true - utils.getIPFS((err, ipfs) => { + argv.ipfs.config.get((err, config) => { if (err) { throw err } - ipfs.config.get((err, config) => { - if (err) { - throw err - } - console.log(JSON.stringify(config, null, 4)) - }) + console.log(JSON.stringify(config, null, 4)) }) } } diff --git a/src/cli/commands/daemon.js b/src/cli/commands/daemon.js index e2a7d6aa67..076a6d9fbe 100644 --- a/src/cli/commands/daemon.js +++ b/src/cli/commands/daemon.js @@ -1,9 +1,7 @@ 'use strict' const HttpAPI = require('../../http-api') -const debug = require('debug') -const log = debug('cli:daemon') -log.error = debug('cli:daemon:error') +const utils = require('../utils') let httpAPI @@ -15,11 +13,12 @@ module.exports = { handler () { console.log('Initializing daemon...') - httpAPI = new HttpAPI(process.env.IPFS_PATH) + const repoPath = utils.getRepoPath() + httpAPI = new HttpAPI(repoPath) httpAPI.start((err) => { if (err && err.code === 'ENOENT') { - console.log('Error: no ipfs repo found in ' + process.env.IPFS_PATH) + console.log('Error: no ipfs repo found in ' + repoPath) console.log('please run: jsipfs init') process.exit(1) } diff --git a/src/cli/commands/dag/get.js b/src/cli/commands/dag/get.js index 500be7adb2..e4a65d6a83 100644 --- a/src/cli/commands/dag/get.js +++ b/src/cli/commands/dag/get.js @@ -1,10 +1,6 @@ 'use strict' -const utils = require('../../utils') const CID = require('cids') -const debug = require('debug') -const log = debug('cli:block') -log.error = debug('cli:block:error') module.exports = { command: 'get ', @@ -19,52 +15,46 @@ module.exports = { }, handler (argv) { - utils.getIPFS((err, ipfs) => { - if (err) { - throw err - } + const refParts = argv.cidpath.split('/') + const cidString = refParts[0] + const path = refParts.slice(1).join('/') + const cid = new CID(cidString) - const refParts = argv.cidpath.split('/') - const cidString = refParts[0] - const path = refParts.slice(1).join('/') - const cid = new CID(cidString) + const options = { + localResolve: argv.localResolve + } - const options = { - localResolve: argv.localResolve + argv.ipfs.dag.get(cid, path, options, (err, result) => { + if (err) { + return console.log('dag get failed:', err.message) } - ipfs.dag.get(cid, path, options, (err, result) => { - if (err) { - return console.log('dag get failed:', err.message) - } - - if (options.localResolve) { - console.log('resolving path within the node only') - console.log('remainder path:', result.remainderPath || 'n/a', '\n') - } + if (options.localResolve) { + console.log('resolving path within the node only') + console.log('remainder path:', result.remainderPath || 'n/a', '\n') + } - const node = result.value + const node = result.value - // TODO we need to find* a way to pretty print objects - // * reads as 'agree in' - if (node._json) { - delete node._json.multihash - node._json.data = '0x' + node._json.data.toString('hex') - console.log(node._json) - return - } + // TODO we need to find* a way to pretty print objects + // * reads as 'agree in' + if (node._json) { + delete node._json.multihash + node._json.data = '0x' + node._json.data.toString('hex') + console.log(node._json) + return + } - if (Buffer.isBuffer(node)) { - console.log('0x' + node.toString('hex')) - return - } + if (Buffer.isBuffer(node)) { + console.log('0x' + node.toString('hex')) + return + } - if (node.raw) { - console.log(node.raw) - } else { - console.log(node) - } - }) + if (node.raw) { + console.log(node.raw) + } else { + console.log(node) + } }) } } diff --git a/src/cli/commands/files/add.js b/src/cli/commands/files/add.js index e8afb525d2..e133056c16 100644 --- a/src/cli/commands/files/add.js +++ b/src/cli/commands/files/add.js @@ -1,9 +1,5 @@ 'use strict' -const utils = require('../../utils') -const debug = require('debug') -const log = debug('cli:version') -log.error = debug('cli:version:error') const fs = require('fs') const path = require('path') const glob = require('glob') @@ -69,40 +65,35 @@ module.exports = { const options = { strategy: argv.trickle ? 'trickle' : 'balanced' } + const ipfs = argv.ipfs - utils.getIPFS((err, ipfs) => { - if (err) { - throw err - } + // TODO: revist when interface-ipfs-core exposes pull-streams + let createAddStream = (cb) => { + ipfs.files.createAddStream(options, (err, stream) => { + cb(err, err ? null : toPull.transform(stream)) + }) + } - // TODO: revist when interface-ipfs-core exposes pull-streams - let createAddStream = (cb) => { - ipfs.files.createAddStream(options, (err, stream) => { - cb(err, err ? null : toPull.transform(stream)) - }) + if (typeof ipfs.files.createAddPullStream === 'function') { + createAddStream = (cb) => { + cb(null, ipfs.files.createAddPullStream(options)) } + } - if (typeof ipfs.files.createAddPullStream === 'function') { - createAddStream = (cb) => { - cb(null, ipfs.files.createAddPullStream(options)) - } + createAddStream((err, addStream) => { + if (err) { + throw err } - createAddStream((err, addStream) => { + glob(path.join(inPath, '/**/*'), (err, list) => { if (err) { throw err } + if (list.length === 0) { + list = [inPath] + } - glob(path.join(inPath, '/**/*'), (err, list) => { - if (err) { - throw err - } - if (list.length === 0) { - list = [inPath] - } - - addPipeline(index, addStream, list, argv.wrapWithDirectory) - }) + addPipeline(index, addStream, list, argv.wrapWithDirectory) }) }) } diff --git a/src/cli/commands/files/cat.js b/src/cli/commands/files/cat.js index 0fe9e9ed91..1294d79158 100644 --- a/src/cli/commands/files/cat.js +++ b/src/cli/commands/files/cat.js @@ -1,11 +1,5 @@ 'use strict' -const waterfall = require('async/waterfall') -const debug = require('debug') -const utils = require('../../utils') -const log = debug('cli:files') -log.error = debug('cli:files:error') - module.exports = { command: 'cat ', @@ -19,10 +13,7 @@ module.exports = { path = path.replace('/ipfs/', '') } - waterfall([ - (cb) => utils.getIPFS(cb), - (ipfs, cb) => ipfs.files.cat(path, cb) - ], (err, file) => { + argv.ipfs.files.cat(path, (err, file) => { if (err) { throw err } diff --git a/src/cli/commands/files/get.js b/src/cli/commands/files/get.js index 7bbc82f221..b8eccf4e2c 100644 --- a/src/cli/commands/files/get.js +++ b/src/cli/commands/files/get.js @@ -1,9 +1,5 @@ 'use strict' -const debug = require('debug') -const utils = require('../../utils') -const log = debug('cli:files') -log.error = debug('cli:files:error') var fs = require('fs') const path = require('path') const mkdirp = require('mkdirp') @@ -68,26 +64,20 @@ module.exports = { const ipfsPath = argv['ipfs-path'] const dir = checkArgs(ipfsPath, argv.output) - utils.getIPFS((err, ipfs) => { + argv.ipfs.files.get(ipfsPath, (err, stream) => { if (err) { throw err } - - ipfs.files.get(ipfsPath, (err, stream) => { - if (err) { - throw err - } - console.log(`Saving file(s) to ${ipfsPath}`) - pull( - toPull.source(stream), - pull.asyncMap(fileHandler(dir)), - pull.onEnd((err) => { - if (err) { - throw err - } - }) - ) - }) + console.log(`Saving file(s) to ${ipfsPath}`) + pull( + toPull.source(stream), + pull.asyncMap(fileHandler(dir)), + pull.onEnd((err) => { + if (err) { + throw err + } + }) + ) }) } } diff --git a/src/cli/commands/id.js b/src/cli/commands/id.js index 0ccd56af00..d1e63afa1f 100644 --- a/src/cli/commands/id.js +++ b/src/cli/commands/id.js @@ -1,10 +1,5 @@ 'use strict' -const debug = require('debug') -const utils = require('../utils') -const log = debug('cli') -log.error = debug('cli:error') - module.exports = { command: 'id', @@ -19,17 +14,12 @@ module.exports = { handler (argv) { // TODO: handle argv.format - utils.getIPFS((err, ipfs) => { + argv.ipfs.id((err, id) => { if (err) { throw err } - ipfs.id((err, id) => { - if (err) { - throw err - } - console.log(JSON.stringify(id, '', 2)) - }) + console.log(JSON.stringify(id, '', 2)) }) } } diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index 20550602a4..770ad6e6fc 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -2,7 +2,6 @@ const Repo = require('ipfs-repo') const IPFS = require('../../core') -const Store = require('fs-pull-blob-store') const utils = require('../utils') module.exports = { @@ -30,12 +29,8 @@ module.exports = { const log = utils.createLogger(true) log(`initializing ipfs node at ${path}`) - const repo = new Repo(path, { - stores: Store - }) - const node = new IPFS({ - repo: repo, + repo: new Repo(path), init: false, start: false }) diff --git a/src/cli/commands/object/data.js b/src/cli/commands/object/data.js index 7dfb371568..ec595c90ea 100644 --- a/src/cli/commands/object/data.js +++ b/src/cli/commands/object/data.js @@ -1,10 +1,5 @@ 'use strict' -const utils = require('../../utils') -const debug = require('debug') -const log = debug('cli:object') -log.error = debug('cli:object:error') - module.exports = { command: 'data ', @@ -13,18 +8,14 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { + argv.ipfs.object.data(argv.key, { + enc: 'base58' + }, (err, data) => { if (err) { throw err } - ipfs.object.data(argv.key, {enc: 'base58'}, (err, data) => { - if (err) { - throw err - } - - console.log(data.toString()) - }) + console.log(data.toString()) }) } } diff --git a/src/cli/commands/object/get.js b/src/cli/commands/object/get.js index ed1be4c596..c0e77e624e 100644 --- a/src/cli/commands/object/get.js +++ b/src/cli/commands/object/get.js @@ -1,11 +1,5 @@ 'use strict' -const waterfall = require('async/waterfall') -const utils = require('../../utils') -const debug = require('debug') -const log = debug('cli:object') -log.error = debug('cli:object:error') - module.exports = { command: 'get ', @@ -14,10 +8,7 @@ module.exports = { builder: {}, handler (argv) { - waterfall([ - (cb) => utils.getIPFS(cb), - (ipfs, cb) => ipfs.object.get(argv.key, {enc: 'base58'}, cb) - ], (err, node) => { + argv.ipfs.object.get(argv.key, {enc: 'base58'}, (err, node) => { if (err) { throw err } diff --git a/src/cli/commands/object/links.js b/src/cli/commands/object/links.js index f4c576f960..1c391cf502 100644 --- a/src/cli/commands/object/links.js +++ b/src/cli/commands/object/links.js @@ -1,10 +1,5 @@ 'use strict' -const utils = require('../../utils') -const debug = require('debug') -const log = debug('cli:object') -log.error = debug('cli:object:error') - module.exports = { command: 'links ', @@ -13,25 +8,21 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { + argv.ipfs.object.links(argv.key, { + enc: 'base58' + }, (err, links) => { if (err) { throw err } - ipfs.object.links(argv.key, {enc: 'base58'}, (err, links) => { - if (err) { - throw err - } - - links.forEach((link) => { - link = link.toJSON() + links.forEach((link) => { + link = link.toJSON() - console.log( - link.multihash, - link.size, - link.name - ) - }) + console.log( + link.multihash, + link.size, + link.name + ) }) }) } diff --git a/src/cli/commands/object/new.js b/src/cli/commands/object/new.js index 6a9d27b0df..b121989672 100644 --- a/src/cli/commands/object/new.js +++ b/src/cli/commands/object/new.js @@ -1,7 +1,5 @@ 'use strict' -const waterfall = require('async/waterfall') -const utils = require('../../utils') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') @@ -14,10 +12,7 @@ module.exports = { builder: {}, handler (argv) { - waterfall([ - (cb) => utils.getIPFS(cb), - (ipfs, cb) => ipfs.object.new(argv.template, cb) - ], (err, node) => { + argv.ipfs.object.new(argv.template, (err, node) => { if (err) { throw err } diff --git a/src/cli/commands/object/patch/add-link.js b/src/cli/commands/object/patch/add-link.js index 8929524831..382ecea44e 100644 --- a/src/cli/commands/object/patch/add-link.js +++ b/src/cli/commands/object/patch/add-link.js @@ -1,12 +1,7 @@ 'use strict' -const utils = require('../../../utils') -const debug = require('debug') -const log = debug('cli:object') const dagPB = require('ipld-dag-pb') const DAGLink = dagPB.DAGLink -const series = require('async/series') -log.error = debug('cli:object:error') module.exports = { command: 'add-link ', @@ -16,46 +11,25 @@ module.exports = { builder: {}, handler (argv) { - let ipfs - let nodeA - let nodeB - - series([ - (cb) => { - utils.getIPFS((err, _ipfs) => { - if (err) { - return cb(err) - } - ipfs = _ipfs - cb() - }) - }, - (cb) => { - ipfs.object.get(argv.ref, {enc: 'base58'}, (err, node) => { - if (err) { - return cb(err) - } - nodeA = node - cb() - }) - }, - (cb) => { - const link = new DAGLink(argv.name, nodeA.size, nodeA.multihash) - - ipfs.object.patch.addLink(argv.root, link, {enc: 'base58'}, (err, node) => { - if (err) { - return cb(err) - } - nodeB = node - cb() - }) - } - ], (err) => { + const ipfs = argv.ipfs + ipfs.object.get(argv.ref, { + enc: 'base58' + }, (err, nodeA) => { if (err) { throw err } - console.log(nodeB.toJSON().multihash) + const link = new DAGLink(argv.name, nodeA.size, nodeA.multihash) + + ipfs.object.patch.addLink(argv.root, link, { + enc: 'base58' + }, (err, nodeB) => { + if (err) { + throw err + } + + console.log(nodeB.toJSON().multihash) + }) }) } } diff --git a/src/cli/commands/object/patch/append-data.js b/src/cli/commands/object/patch/append-data.js index fefbd641ac..bf27b7c032 100644 --- a/src/cli/commands/object/patch/append-data.js +++ b/src/cli/commands/object/patch/append-data.js @@ -1,18 +1,15 @@ 'use strict' -const utils = require('../../../utils') const bl = require('bl') const fs = require('fs') -const waterfall = require('async/waterfall') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') -function appendData (key, data) { - waterfall([ - (cb) => utils.getIPFS(cb), - (ipfs, cb) => ipfs.object.patch.appendData(key, data, {enc: 'base58'}, cb) - ], (err, node) => { +function appendData (key, data, ipfs) { + ipfs.object.patch.appendData(key, data, { + enc: 'base58' + }, (err, node) => { if (err) { throw err } @@ -30,8 +27,9 @@ module.exports = { builder: {}, handler (argv) { + const ipfs = argv.ipfs if (argv.data) { - return appendData(argv.root, fs.readFileSync(argv.data)) + return appendData(argv.root, fs.readFileSync(argv.data), ipfs) } process.stdin.pipe(bl((err, input) => { @@ -39,7 +37,7 @@ module.exports = { throw err } - appendData(argv.root, input) + appendData(argv.root, input, ipfs) })) } } diff --git a/src/cli/commands/object/patch/rm-link.js b/src/cli/commands/object/patch/rm-link.js index df60803a5f..8b11b58e2f 100644 --- a/src/cli/commands/object/patch/rm-link.js +++ b/src/cli/commands/object/patch/rm-link.js @@ -1,8 +1,6 @@ 'use strict' const DAGLink = require('ipld-dag-pb').DAGLink -const waterfall = require('async/waterfall') -const utils = require('../../../utils') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') @@ -20,11 +18,9 @@ module.exports = { // since it expectes a DAGLink type, we have to pass some fake size and // hash. const link = new DAGLink(argv.link, 1, 'Qm') - - waterfall([ - (cb) => utils.getIPFS(cb), - (ipfs, cb) => ipfs.object.patch.rmLink(argv.root, link, {enc: 'base58'}, cb) - ], (err, node) => { + argv.ipfs.object.patch.rmLink(argv.root, link, { + enc: 'base58' + }, (err, node) => { if (err) { throw err } diff --git a/src/cli/commands/object/patch/set-data.js b/src/cli/commands/object/patch/set-data.js index ccf0cee023..72153c5dde 100644 --- a/src/cli/commands/object/patch/set-data.js +++ b/src/cli/commands/object/patch/set-data.js @@ -1,18 +1,15 @@ 'use strict' -const utils = require('../../../utils') -const bl = require('bl') const fs = require('fs') -const waterfall = require('async/waterfall') +const bl = require('bl') const debug = require('debug') const log = debug('cli:object') log.error = debug('cli:object:error') -function parseAndAddNode (key, data) { - waterfall([ - (cb) => utils.getIPFS(cb), - (ipfs, cb) => ipfs.object.patch.setData(key, data, {enc: 'base58'}, cb) - ], (err, node) => { +function parseAndAddNode (key, data, ipfs) { + ipfs.object.patch.setData(key, data, { + enc: 'base58' + }, (err, node) => { if (err) { throw err } @@ -30,8 +27,9 @@ module.exports = { builder: {}, handler (argv) { + const ipfs = argv.ipfs if (argv.data) { - return parseAndAddNode(argv.root, fs.readFileSync(argv.data)) + return parseAndAddNode(argv.root, fs.readFileSync(argv.data), ipfs) } process.stdin.pipe(bl((err, input) => { @@ -39,7 +37,7 @@ module.exports = { throw err } - parseAndAddNode(argv.root, input) + parseAndAddNode(argv.root, input, ipfs) })) } } diff --git a/src/cli/commands/object/put.js b/src/cli/commands/object/put.js index 1e6a6c38a1..60d94195f0 100644 --- a/src/cli/commands/object/put.js +++ b/src/cli/commands/object/put.js @@ -1,18 +1,10 @@ 'use strict' -const utils = require('../../utils') const bl = require('bl') const fs = require('fs') -const waterfall = require('async/waterfall') -const debug = require('debug') -const log = debug('cli:object') -log.error = debug('cli:object:error') - -function putNode (buf, enc) { - waterfall([ - (cb) => utils.getIPFS(cb), - (ipfs, cb) => ipfs.object.put(buf, {enc: enc}, cb) - ], (err, node) => { + +function putNode (buf, enc, ipfs) { + ipfs.object.put(buf, {enc: enc}, (err, node) => { if (err) { throw err } @@ -36,10 +28,10 @@ module.exports = { }, handler (argv) { + const ipfs = argv.ipfs if (argv.data) { const buf = fs.readFileSync(argv.data) - putNode(buf, argv.inputEnc) - return + return putNode(buf, argv.inputEnc, ipfs) } process.stdin.pipe(bl((err, input) => { @@ -47,7 +39,7 @@ module.exports = { throw err } - putNode(input, argv.inputEnc) + putNode(input, argv.inputEnc, ipfs) })) } } diff --git a/src/cli/commands/object/stat.js b/src/cli/commands/object/stat.js index ac1da4382a..e7a28d83ce 100644 --- a/src/cli/commands/object/stat.js +++ b/src/cli/commands/object/stat.js @@ -1,10 +1,5 @@ 'use strict' -const utils = require('../../utils') -const debug = require('debug') -const log = debug('cli:object') -log.error = debug('cli:object:error') - module.exports = { command: 'stat ', @@ -13,21 +8,17 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { + argv.ipfs.object.stat(argv.key, { + enc: 'base58' + }, (err, stats) => { if (err) { throw err } - ipfs.object.stat(argv.key, {enc: 'base58'}, (err, stats) => { - if (err) { - throw err - } - - delete stats.Hash // only for js-ipfs-api output + delete stats.Hash // only for js-ipfs-api output - Object.keys(stats).forEach((key) => { - console.log(`${key}: ${stats[key]}`) - }) + Object.keys(stats).forEach((key) => { + console.log(`${key}: ${stats[key]}`) }) }) } diff --git a/src/cli/commands/pubsub/ls.js b/src/cli/commands/pubsub/ls.js index 82ca7cbb54..95cb7751b9 100644 --- a/src/cli/commands/pubsub/ls.js +++ b/src/cli/commands/pubsub/ls.js @@ -1,10 +1,5 @@ 'use strict' -const utils = require('../../utils') -const debug = require('debug') -const log = debug('cli:pubsub') -log.error = debug('cli:pubsub:error') - module.exports = { command: 'ls', @@ -13,19 +8,13 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { + argv.ipfs.pubsub.ls((err, subscriptions) => { if (err) { throw err } - ipfs.pubsub.ls((err, subscriptions) => { - if (err) { - throw err - } - - subscriptions.forEach((sub) => { - console.log(sub) - }) + subscriptions.forEach((sub) => { + console.log(sub) }) }) } diff --git a/src/cli/commands/pubsub/peers.js b/src/cli/commands/pubsub/peers.js index 4519677b64..8dd6c5a0a3 100644 --- a/src/cli/commands/pubsub/peers.js +++ b/src/cli/commands/pubsub/peers.js @@ -1,10 +1,5 @@ 'use strict' -const utils = require('../../utils') -const debug = require('debug') -const log = debug('cli:pubsub') -log.error = debug('cli:pubsub:error') - module.exports = { command: 'peers ', @@ -13,19 +8,13 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { + argv.ipfs.pubsub.peers(argv.topic, (err, peers) => { if (err) { throw err } - ipfs.pubsub.peers(argv.topic, (err, peers) => { - if (err) { - throw err - } - - peers.forEach((peer) => { - console.log(peer) - }) + peers.forEach((peer) => { + console.log(peer) }) }) } diff --git a/src/cli/commands/pubsub/pub.js b/src/cli/commands/pubsub/pub.js index ef303d1c04..bed5232805 100644 --- a/src/cli/commands/pubsub/pub.js +++ b/src/cli/commands/pubsub/pub.js @@ -1,10 +1,5 @@ 'use strict' -const utils = require('../../utils') -const debug = require('debug') -const log = debug('cli:pubsub') -log.error = debug('cli:pubsub:error') - module.exports = { command: 'pub ', @@ -13,18 +8,12 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { + const data = new Buffer(String(argv.data)) + + argv.ipfs.pubsub.publish(argv.topic, data, (err) => { if (err) { throw err } - - const data = new Buffer(String(argv.data)) - - ipfs.pubsub.publish(argv.topic, data, (err) => { - if (err) { - throw err - } - }) }) } } diff --git a/src/cli/commands/pubsub/sub.js b/src/cli/commands/pubsub/sub.js index f88aeed727..d856be8b48 100644 --- a/src/cli/commands/pubsub/sub.js +++ b/src/cli/commands/pubsub/sub.js @@ -1,10 +1,5 @@ 'use strict' -const utils = require('../../utils') -const debug = require('debug') -const log = debug('cli:pubsub') -log.error = debug('cli:pubsub:error') - module.exports = { command: 'sub ', @@ -13,20 +8,14 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { + const handler = (msg) => { + console.log(msg.data.toString()) + } + + argv.ipfs.pubsub.subscribe(argv.topic, handler, (err) => { if (err) { throw err } - - const handler = (msg) => { - console.log(msg.data.toString()) - } - - ipfs.pubsub.subscribe(argv.topic, handler, (err) => { - if (err) { - throw err - } - }) }) } } diff --git a/src/cli/commands/repo/version.js b/src/cli/commands/repo/version.js index d1c6a57c96..2c4e388832 100644 --- a/src/cli/commands/repo/version.js +++ b/src/cli/commands/repo/version.js @@ -1,7 +1,5 @@ 'use strict' -const utils = require('../../utils') - module.exports = { command: 'version', @@ -10,16 +8,11 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { + argv.ipfs.repo.version(function (err, version) { if (err) { - throw err + return console.error(err) } - ipfs.repo.version(function (err, version) { - if (err) { - return console.error(err) - } - console.log(version) - }) + console.log(version) }) } } diff --git a/src/cli/commands/swarm/addrs.js b/src/cli/commands/swarm/addrs.js index d5ef9706c0..45f66b296c 100644 --- a/src/cli/commands/swarm/addrs.js +++ b/src/cli/commands/swarm/addrs.js @@ -1,10 +1,5 @@ 'use strict' -const utils = require('../../utils') -const debug = require('debug') -const log = debug('cli:object') -log.error = debug('cli:object:error') - module.exports = { command: 'addrs', @@ -16,23 +11,17 @@ module.exports = { }, handler (argv) { - utils.getIPFS((err, ipfs) => { + argv.ipfs.swarm.addrs((err, res) => { if (err) { throw err } - ipfs.swarm.addrs((err, res) => { - if (err) { - throw err - } - - res.forEach((peer) => { - const count = peer.multiaddrs.length - console.log(`${peer.id.toB58String()} (${count})`) - peer.multiaddrs.forEach((addr) => { - const res = addr.decapsulate('ipfs').toString() - console.log(`\t${res}`) - }) + res.forEach((peer) => { + const count = peer.multiaddrs.length + console.log(`${peer.id.toB58String()} (${count})`) + peer.multiaddrs.forEach((addr) => { + const res = addr.decapsulate('ipfs').toString() + console.log(`\t${res}`) }) }) }) diff --git a/src/cli/commands/swarm/addrs/local.js b/src/cli/commands/swarm/addrs/local.js index cb559fb564..c923986372 100644 --- a/src/cli/commands/swarm/addrs/local.js +++ b/src/cli/commands/swarm/addrs/local.js @@ -13,23 +13,17 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { + if (!utils.isDaemonOn()) { + throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.') + } + + argv.ipfs.swarm.localAddrs((err, res) => { if (err) { throw err } - if (!utils.isDaemonOn()) { - throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.') - } - - ipfs.swarm.localAddrs((err, res) => { - if (err) { - throw err - } - - res.forEach((addr) => { - console.log(addr.toString()) - }) + res.forEach((addr) => { + console.log(addr.toString()) }) }) } diff --git a/src/cli/commands/swarm/connect.js b/src/cli/commands/swarm/connect.js index 234bb2b69d..a75946f2bb 100644 --- a/src/cli/commands/swarm/connect.js +++ b/src/cli/commands/swarm/connect.js @@ -1,9 +1,6 @@ 'use strict' const utils = require('../../utils') -const debug = require('debug') -const log = debug('cli:swarm') -log.error = debug('cli:swarm:error') module.exports = { command: 'connect
', @@ -17,18 +14,12 @@ module.exports = { throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.') } - utils.getIPFS((err, ipfs) => { + argv.ipfs.swarm.connect(argv.address, (err, res) => { if (err) { throw err } - ipfs.swarm.connect(argv.address, (err, res) => { - if (err) { - throw err - } - - console.log(res.Strings[0]) - }) + console.log(res.Strings[0]) }) } } diff --git a/src/cli/commands/swarm/disconnect.js b/src/cli/commands/swarm/disconnect.js index 44df126b1d..2e58df9752 100644 --- a/src/cli/commands/swarm/disconnect.js +++ b/src/cli/commands/swarm/disconnect.js @@ -1,9 +1,6 @@ 'use strict' const utils = require('../../utils') -const debug = require('debug') -const log = debug('cli:object') -log.error = debug('cli:object:error') module.exports = { command: 'disconnect
', @@ -17,18 +14,12 @@ module.exports = { throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.') } - utils.getIPFS((err, ipfs) => { + argv.ipfs.swarm.disconnect(argv.address, (err, res) => { if (err) { throw err } - ipfs.swarm.disconnect(argv.address, (err, res) => { - if (err) { - throw err - } - - console.log(res.Strings[0]) - }) + console.log(res.Strings[0]) }) } } diff --git a/src/cli/commands/swarm/peers.js b/src/cli/commands/swarm/peers.js index 0d299c2c4a..15ab5261c8 100644 --- a/src/cli/commands/swarm/peers.js +++ b/src/cli/commands/swarm/peers.js @@ -1,11 +1,8 @@ 'use strict' -const utils = require('../../utils') -const debug = require('debug') const mafmt = require('mafmt') const multiaddr = require('multiaddr') -const log = debug('cli:object') -log.error = debug('cli:object:error') +const utils = require('../../utils') module.exports = { command: 'peers', @@ -15,28 +12,22 @@ module.exports = { builder: {}, handler (argv) { - utils.getIPFS((err, ipfs) => { + if (!utils.isDaemonOn()) { + throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.') + } + + argv.ipfs.swarm.peers((err, result) => { if (err) { throw err } - if (!utils.isDaemonOn()) { - throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.') - } - - ipfs.swarm.peers((err, result) => { - if (err) { - throw err + result.forEach((item) => { + let ma = multiaddr(item.addr.toString()) + if (!mafmt.IPFS.matches(ma)) { + ma = ma.encapsulate('/ipfs/' + item.peer.toB58String()) } - - result.forEach((item) => { - let ma = multiaddr(item.addr.toString()) - if (!mafmt.IPFS.matches(ma)) { - ma = ma.encapsulate('/ipfs/' + item.peer.toB58String()) - } - const addr = ma.toString() - console.log(addr) - }) + const addr = ma.toString() + console.log(addr) }) }) } diff --git a/src/cli/commands/version.js b/src/cli/commands/version.js index 9158bffc31..b55a744e3b 100644 --- a/src/cli/commands/version.js +++ b/src/cli/commands/version.js @@ -1,10 +1,5 @@ 'use strict' -const utils = require('../utils') -const debug = require('debug') -const log = debug('cli:version') -log.error = debug('cli:version:error') - module.exports = { command: 'version', @@ -28,17 +23,12 @@ module.exports = { handler (argv) { // TODO: handle argv.{repo|commit|number} - utils.getIPFS((err, ipfs) => { + argv.ipfs.version((err, version) => { if (err) { throw err } - ipfs.version((err, version) => { - if (err) { - throw err - } - console.log(`js-ipfs version: ${version.version}`) - }) + console.log(`js-ipfs version: ${version.version}`) }) } } diff --git a/src/cli/utils.js b/src/cli/utils.js index 6c8f32e190..6af62979c7 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -36,7 +36,7 @@ function getAPICtl () { exports.getIPFS = (callback) => { if (isDaemonOn()) { - return callback(null, getAPICtl()) + return callback(null, getAPICtl(), (cb) => cb()) } const node = new IPFS({ @@ -48,7 +48,21 @@ exports.getIPFS = (callback) => { } }) - node.preStart(() => callback(null, node)) + const cleanup = (cb) => { + if (node && node._repo && !node._repo.closed) { + node._repo.close(() => cb()) + } else { + cb() + } + } + + node.on('error', (err) => { + throw err + }) + + node.once('ready', () => { + callback(null, node, cleanup) + }) } exports.getRepoPath = () => { diff --git a/src/core/boot.js b/src/core/boot.js new file mode 100644 index 0000000000..a315f68839 --- /dev/null +++ b/src/core/boot.js @@ -0,0 +1,98 @@ +'use strict' + +const waterfall = require('async/waterfall') +const series = require('async/series') +const extend = require('deep-extend') + +// Boot an IPFS node depending on the options set +module.exports = (self) => { + self.log('booting') + const options = self._options + const doInit = options.init + const doStart = options.start + const config = options.config + const setConfig = config && typeof config === 'object' + const repoOpen = !self._repo.closed + + const customInitOptions = typeof options.init === 'object' ? options.init : {} + const initOptions = Object.assign({ bits: 2048 }, customInitOptions) + + const maybeOpenRepo = (cb) => { + waterfall([ + (cb) => self._repo.exists(cb), + (exists, cb) => { + self.log('boot:maybeOpen', exists, repoOpen) + if (exists && !repoOpen) { + return series([ + (cb) => self._repo.open(cb), + (cb) => self.preStart(cb), + (cb) => { + self.state.initialized() + cb() + } + ], cb) + } + cb() + } + ], cb) + } + + const done = (err) => { + if (err) { + return self.emit('error', err) + } + self.emit('ready') + self.log('boot:done', err) + } + + const tasks = [] + + if (doInit) { + self.log('boot:doInit') + tasks.push((cb) => self.init(initOptions, cb)) + next(null, true) + } else if (!repoOpen) { + self._repo.exists(next) + } + + function next (err, hasRepo) { + self.log('boot:next') + if (err) { + return done(err) + } + + if (hasRepo && !doInit) { + self.log('boot:maybeopenreop') + tasks.push(maybeOpenRepo) + } + + if (setConfig) { + self.log('boot:setConfig') + if (!hasRepo) { + console.log('WARNING, trying to set config on uninitialized repo, maybe forgot to set "init: true"') + } else { + tasks.push((cb) => { + waterfall([ + (cb) => self.config.get(cb), + (config, cb) => { + extend(config, options.config) + self.config.replace(config, cb) + } + ], cb) + }) + } + } + + if (doStart) { + self.log('boot:doStart') + if (!hasRepo) { + console.log('WARNING, trying to start ipfs node on uninitialized repo, maybe forgot to set "init: true"') + return done(new Error('Uninitalized repo')) + } else { + tasks.push((cb) => self.start(cb)) + } + } + + series(tasks, done) + } +} diff --git a/src/core/components/block.js b/src/core/components/block.js index 2ec08a5561..1d06e1220e 100644 --- a/src/core/components/block.js +++ b/src/core/components/block.js @@ -2,6 +2,7 @@ const Block = require('ipfs-block') const multihash = require('multihashes') +const multihashing = require('multihashing-async') const CID = require('cids') const waterfall = require('async/waterfall') @@ -11,43 +12,32 @@ module.exports = function block (self) { cid = cleanCid(cid) self._blockService.get(cid, callback) }, - put: (block, cid, callback) => { - if (typeof cid === 'function') { - // legacy (without CID) - callback = cid - cid = undefined - } - + put: (block, callback) => { if (Array.isArray(block)) { return callback(new Error('Array is not supported')) } - if (Buffer.isBuffer(block)) { - block = new Block(block) - } - waterfall([ (cb) => { - if (cid) { - return cb(null, cid) + if (Block.isBlock(block)) { + return cb(null, block) } - block.key('sha2-256', (err, key) => { + multihashing(block, 'sha2-256', (err, multihash) => { if (err) { return cb(err) } - cb(null, new CID(key)) + cb(null, new Block(block, new CID(multihash))) }) }, - (cid, cb) => self._blockService.put({block: block, cid: cid}, cb) - ], (err) => { - if (err) { - return callback(err) - } - - callback(null, block) - }) + (block, cb) => self._blockService.put(block, (err) => { + if (err) { + return cb(err) + } + cb(null, block) + }) + ], callback) }, rm: (cid, callback) => { cid = cleanCid(cid) diff --git a/src/core/components/init.js b/src/core/components/init.js index 34ebe29de8..02ff9d71fa 100644 --- a/src/core/components/init.js +++ b/src/core/components/init.js @@ -7,9 +7,6 @@ const isNode = require('detect-node') const addDefaultAssets = require('./init-assets') -// Current repo version -const VERSION = '3' - module.exports = function init (self) { return (opts, callback) => { if (typeof opts === 'function') { @@ -17,6 +14,24 @@ module.exports = function init (self) { opts = {} } + const done = (err, res) => { + if (err) { + self.emit('error', err) + return callback(err) + } + + self.state.initialized() + self.emit('init') + callback(null, res) + } + + if (self.state.state() !== 'uninitalized') { + return done(new Error('Not able to init from state: ' + self.state.state())) + } + + self.state.init() + self.log('init') + opts.emptyRepo = opts.emptyRepo || false opts.bits = Number(opts.bits) || 2048 opts.log = opts.log || function () {} @@ -29,15 +44,18 @@ module.exports = function init (self) { // Verify repo does not yet exist. (cb) => self._repo.exists(cb), (exists, cb) => { + self.log('repo exists?', exists) if (exists === true) { return cb(new Error('repo already exists')) } // Generate peer identity keypair + transform to desired format + add to config. opts.log(`generating ${opts.bits}-bit RSA keypair...`, false) + self.log('generating peer id: %s bits', opts.bits) peerId.create({bits: opts.bits}, cb) }, (keys, cb) => { + self.log('identity generated') config.Identity = { PeerID: keys.toB58String(), PrivKey: keys.privKey.bytes.toString('base64') @@ -45,10 +63,11 @@ module.exports = function init (self) { opts.log('done') opts.log('peer identity: ' + config.Identity.PeerID) - self._repo.version.set(VERSION, cb) + self._repo.init(config, cb) }, - (cb) => self._repo.config.set(config, cb), + (_, cb) => self._repo.open(cb), (cb) => { + self.log('repo opened') if (opts.emptyRepo) { return cb(null, true) } @@ -59,8 +78,6 @@ module.exports = function init (self) { ] if (typeof addDefaultAssets === 'function') { - console.log('init assets') - tasks.push((cb) => addDefaultAssets(self, opts.log, cb)) } @@ -72,14 +89,6 @@ module.exports = function init (self) { cb(null, true) }) } - ], (err, success) => { - if (err) { - return callback(err) - } - - self.emit('init') - - callback(null, success) - }) + ], done) } } diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index f02574655e..17a39bb9a3 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -2,6 +2,7 @@ const Node = require('libp2p-ipfs-nodejs') const promisify = require('promisify-es6') +const get = require('lodash.get') module.exports = function libp2p (self) { return { @@ -14,9 +15,9 @@ module.exports = function libp2p (self) { } const options = { - mdns: config.Discovery.MDNS.Enabled, - webRTCStar: config.Discovery.webRTCStar.Enabled, - bootstrap: config.Bootstrap + mdns: get(config, 'Discovery.MDNS.Enabled'), + webRTCStar: get(config, 'Discovery.webRTCStar.Enabled'), + bootstrap: get(config, 'Bootstrap') } self._libp2pNode = new Node(self._peerInfo, self._peerInfoBook, options) diff --git a/src/core/components/pre-start.js b/src/core/components/pre-start.js index 2c4dc90ea0..87dad9c911 100644 --- a/src/core/components/pre-start.js +++ b/src/core/components/pre-start.js @@ -6,15 +6,14 @@ const multiaddr = require('multiaddr') const waterfall = require('async/waterfall') const mafmt = require('mafmt') -const utils = require('../utils') - /* * Load stuff from Repo into memory */ module.exports = function preStart (self) { return (callback) => { + self.log('pre-start') + waterfall([ - (cb) => utils.ifRepoExists(self._repo, cb), (cb) => self._repo.config.get(cb), (config, cb) => { const privKey = config.Identity.PrivKey @@ -30,8 +29,9 @@ module.exports = function preStart (self) { let ma = multiaddr(addr) if (!mafmt.IPFS.matches(ma)) { - ma = ma.encapsulate('/ipfs/' + - self._peerInfo.id.toB58String()) + ma = ma.encapsulate( + '/ipfs/' + self._peerInfo.id.toB58String() + ) } self._peerInfo.multiaddr.add(ma) diff --git a/src/core/components/repo.js b/src/core/components/repo.js index ea635a5c9c..eb488346a9 100644 --- a/src/core/components/repo.js +++ b/src/core/components/repo.js @@ -1,26 +1,13 @@ 'use strict' -const utils = require('../utils') - module.exports = function repo (self) { return { init: (bits, empty, callback) => { // 1. check if repo already exists }, - version: (opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - - utils.ifRepoExists(self._repo, (err, res) => { - if (err) { - return callback(err) - } - - self._repo.version.get(callback) - }) + version: (callback) => { + self._repo.version.get(callback) }, gc: function () {}, diff --git a/src/core/components/start.js b/src/core/components/start.js index 3209f2fb14..f3791f8792 100644 --- a/src/core/components/start.js +++ b/src/core/components/start.js @@ -8,12 +8,37 @@ module.exports = (self) => { return (callback) => { callback = callback || function noop () {} + const done = (err) => { + if (err) { + self.emit('error', err) + return callback(err) + } + + self.state.started() + self.emit('start') + callback() + } + + if (self.state.state() !== 'stopped') { + return done(new Error('Not able to start from state: ' + self.state.state())) + } + + self.log('starting') + self.state.start() + series([ + (cb) => { + console.log('repo is:', self._repo.closed) + + self._repo.closed + ? self._repo.open(cb) + : cb() + }, (cb) => self.preStart(cb), (cb) => self.libp2p.start(cb) ], (err) => { if (err) { - return callback(err) + return done(err) } self._bitswap = new Bitswap( @@ -22,33 +47,15 @@ module.exports = (self) => { self._peerInfoBook ) - const pubsub = self._options.EXPERIMENTAL.pubsub + self._bitswap.start() + self._blockService.goOnline(self._bitswap) - if (pubsub) { + if (self._options.EXPERIMENTAL.pubsub) { self._pubsub = new FloodSub(self._libp2pNode) + self._pubsub.start(done) + } else { + done() } - - series([ - (cb) => { - self._bitswap.start() - cb() - }, - (cb) => { - self._blockService.goOnline(self._bitswap) - cb() - }, - (cb) => { - if (pubsub) { - self._pubsub.start(cb) - } else { - cb() - } - }, - (cb) => { - self.emit('start') - cb() - } - ], callback) }) } } diff --git a/src/core/components/stop.js b/src/core/components/stop.js index 0a38350056..33e0a4a3c1 100644 --- a/src/core/components/stop.js +++ b/src/core/components/stop.js @@ -1,29 +1,40 @@ 'use strict' +const series = require('async/series') + module.exports = (self) => { return (callback) => { callback = callback || function noop () {} + self.log('stop') - self._blockService.goOffline() - self._bitswap.stop() - - if (self._options.EXPERIMENTAL.pubsub) { - self._pubsub.stop(next) - } else { - next() - } - - function next (err) { + const done = (err) => { if (err) { + self.emit('error', err) return callback(err) } - self.libp2p.stop((err) => { - if (err) { - return callback(err) - } - self.emit('stop') - callback() - }) + self.state.stopped() + self.emit('stop') + callback() } + + if (self.state.state() !== 'running') { + return done(new Error('Not able to stop from state: ' + self.state.state())) + } + + self.state.stop() + self._blockService.goOffline() + self._bitswap.stop() + + series([ + (cb) => { + if (self._options.EXPERIMENTAL.pubsub) { + self._pubsub.stop(cb) + } else { + cb() + } + }, + (cb) => self.libp2p.stop(cb), + (cb) => self._repo.close(cb) + ], done) } } diff --git a/src/core/default-repo-browser.js b/src/core/default-repo-browser.js index 604127350e..bcfe6b8489 100644 --- a/src/core/default-repo-browser.js +++ b/src/core/default-repo-browser.js @@ -1,11 +1,8 @@ 'use strict' -const Store = require('idb-pull-blob-store') const IPFSRepo = require('ipfs-repo') module.exports = (dir) => { const repoPath = dir || 'ipfs' - return new IPFSRepo(repoPath, { - stores: Store - }) + return new IPFSRepo(repoPath) } diff --git a/src/core/default-repo.js b/src/core/default-repo.js index a65098bf78..751689cbac 100644 --- a/src/core/default-repo.js +++ b/src/core/default-repo.js @@ -1,12 +1,11 @@ 'use strict' const os = require('os') -const Store = require('fs-pull-blob-store') const IPFSRepo = require('ipfs-repo') const path = require('path') module.exports = (dir) => { const repoPath = dir || path.join(os.homedir(), '.jsipfs') - return new IPFSRepo(repoPath, { stores: Store }) + return new IPFSRepo(repoPath) } diff --git a/src/core/index.js b/src/core/index.js index 7513e59fcb..9a0edc16be 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -10,11 +10,10 @@ const PeerBook = require('peer-book') const CID = require('cids') const debug = require('debug') const extend = require('deep-extend') -const series = require('async/series') const EventEmitter = require('events') const defaultRepo = require('./default-repo') - +const boot = require('./boot') const components = require('./components') class IPFS extends EventEmitter { @@ -29,13 +28,11 @@ class IPFS extends EventEmitter { extend(this._options, options) - if (typeof options.init === 'boolean' && - options.init === false) { + if (options.init === false) { this._options.init = false } - if (!(typeof options.start === 'boolean' && - options.start === false)) { + if (!(options.start === false)) { this._options.start = true } @@ -50,6 +47,8 @@ class IPFS extends EventEmitter { this.log = debug('jsipfs') this.log.err = debug('jsipfs:err') + this.on('error', (err) => this.log(err)) + // IPFS types this.types = { Buffer: Buffer, @@ -96,40 +95,9 @@ class IPFS extends EventEmitter { if (this._options.EXPERIMENTAL.pubsub) { this.log('EXPERIMENTAL pubsub is enabled') } + this.state = require('./state')(this) - // Boot - series([ - (cb) => { - this._options.init - ? this.init({ bits: this._options.init.bits || 2048 }, cb) - : cb() - }, - (cb) => { - if (!(this._options.config && - typeof this._options.config === 'object' && - this._options.init)) { - return cb() - } - - this._repo.config.get((err, config) => { - if (err) { - return cb(err) - } - - extend(config, this._options.config) - this._repo.config.set(config, cb) - }) - }, - (cb) => { - this._options.start - ? this.start(cb) - : cb() - } - ], (err) => { - if (err) { - this.emit('error', err) - } - }) + boot(this) } } diff --git a/src/core/state.js b/src/core/state.js new file mode 100644 index 0000000000..c86a2539de --- /dev/null +++ b/src/core/state.js @@ -0,0 +1,65 @@ +'use strict' + +const debug = require('debug') +const log = debug('jsipfs:state') +log.error = debug('jsipfs:state:error') + +const fsm = require('fsm-event') + +module.exports = (self) => { + const s = fsm('uninitalized', { + uninitalized: { + init: 'initializing', + initialized: 'stopped' + }, + initializing: { + initialized: 'stopped' + }, + stopped: { + start: 'starting' + }, + starting: { + started: 'running' + }, + running: { + stop: 'stopping' + }, + stopping: { + stopped: 'stopped' + } + }) + + // log events + s.on('error', (err) => log.error(err)) + s.on('done', () => log('-> ' + s._state)) + + // -- Actions + + s.init = () => { + s('init') + } + + s.initialized = () => { + s('initialized') + } + + s.stop = () => { + s('stop') + } + + s.stopped = () => { + s('stopped') + } + + s.start = () => { + s('start') + } + + s.started = () => { + s('started') + } + + s.state = () => s._state + + return s +} diff --git a/src/core/utils.js b/src/core/utils.js index 6278eba9ed..9203aacb4e 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -1,17 +1,3 @@ 'use strict' -exports.ifRepoExists = (repo, cb) => { - repo.exists((err, exists) => { - if (err) { - return cb(err) - } - - if (exists === false) { - return cb(new Error('no ipfs repo found')) - } - - cb() - }) -} - exports.OFFLINE_ERROR = new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.') diff --git a/src/http-api/error-handler.js b/src/http-api/error-handler.js index 82062a492b..012a98f173 100644 --- a/src/http-api/error-handler.js +++ b/src/http-api/error-handler.js @@ -2,7 +2,7 @@ const Hoek = require('hoek') -module.exports = (server) => { +module.exports = (api, server) => { server.ext('onRequest', (request, reply) => { request.handleError = handleError reply.continue() @@ -31,6 +31,7 @@ module.exports = (server) => { response: res.output.payload } + api.log.error(res.stack) server.log('error', debug) reply({ diff --git a/src/http-api/index.js b/src/http-api/index.js index f465b8ae72..3fb36e73b2 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -3,17 +3,10 @@ const series = require('async/series') const Hapi = require('hapi') const debug = require('debug') -const fs = require('fs') -const path = require('path') -const IPFSRepo = require('ipfs-repo') const multiaddr = require('multiaddr') -const Store = require('fs-pull-blob-store') const setHeader = require('hapi-set-header') const once = require('once') -const log = debug('api') -log.error = debug('api:error') - const IPFS = require('../core') const errorHandler = require('./error-handler') @@ -22,56 +15,58 @@ function uriToMultiaddr (uri) { return `/ip4/${ipPort[0]}/tcp/${ipPort[1]}` } -function HttpApi (repo) { +function HttpApi (repo, config) { this.node = undefined this.server = undefined - this.start = (callback) => { - if (typeof repo === 'string') { - repo = new IPFSRepo(repo, {stores: Store}) - } + this.log = debug('jsipfs:http-api') + this.log.error = debug('jsipfs:http-api:error') - let apiFilePath + this.start = (init, callback) => { + if (typeof init === 'function') { + callback = init + init = false + } + this.log('starting') series([ (cb) => { - // start the daemon - this.node = new IPFS({ - repo: repo, - init: false, - start: true, - EXPERIMENTAL: { - pubsub: true - } - }) + // try-catch so that programmer errors are not swallowed during testing + try { + // start the daemon + this.node = new IPFS({ + repo: repo, + init: init, + start: true, + config: config, + EXPERIMENTAL: { + pubsub: true + } + }) + } catch (err) { + return cb(err) + } cb = once(cb) this.node.once('error', (err) => { + this.log('error starting core', err) err.code = 'ENOENT' cb(err) }) this.node.once('start', cb) }, (cb) => { - // start the http server - const repoPath = this.node.repo.path() - apiFilePath = path.join(repoPath, 'api') - - // make sure we are not using some other daemon repo - try { - fs.statSync(apiFilePath) - console.log('This repo is currently being used by another daemon') - process.exit(1) - } catch (err) {} - + this.log('fetching config') this.node._repo.config.get((err, config) => { if (err) { return callback(err) } // CORS is enabled by default - this.server = new Hapi.Server({ connections: { routes: { cors: true } } }) + this.server = new Hapi.Server({ + connections: { routes: { cors: true } } + }) this.server.app.ipfs = this.node const api = config.Addresses.API.split('/') @@ -91,7 +86,7 @@ function HttpApi (repo) { }) // Nicer errors - errorHandler(this.server) + errorHandler(this, this.server) // load routes require('./routes')(this.server) @@ -118,22 +113,22 @@ function HttpApi (repo) { console.log('Gateway (readonly) is listening on: %s', gateway.info.ma) // for the CLI to know the where abouts of the API - fs.writeFileSync(apiFilePath, api.info.ma) - - cb() + this.node._repo.setApiAddress(api.info.ma, cb) } - ], callback) + ], (err) => { + this.log('done', err) + callback(err) + }) } this.stop = (callback) => { - const repoPath = this.node.repo.path() - fs.unlinkSync(path.join(repoPath, 'api')) - + this.log('stopping') series([ (cb) => this.server.stop(cb), (cb) => this.node.stop(cb) ], (err) => { if (err) { + this.log.error(err) console.log('There were errors stopping') } callback() diff --git a/src/http-api/resources/block.js b/src/http-api/resources/block.js index b0ffc7a92c..77553cbcaf 100644 --- a/src/http-api/resources/block.js +++ b/src/http-api/resources/block.js @@ -1,13 +1,14 @@ 'use strict' -const mh = require('multihashes') const CID = require('cids') const multipart = require('ipfs-multipart') const Block = require('ipfs-block') const waterfall = require('async/waterfall') +const multihashing = require('multihashing-async') +const Buffer = require('safe-buffer').Buffer const debug = require('debug') -const log = debug('http-api:block') -log.error = debug('http-api:block:error') +const log = debug('jsipfs:http-api:block') +log.error = debug('jsipfs:http-api:block:error') exports = module.exports @@ -81,18 +82,18 @@ exports.put = { // main route handler which is called after the above `parseArgs`, but only if the args were valid handler: (request, reply) => { - const data = request.pre.args.data + const data = Buffer.from(request.pre.args.data) const ipfs = request.server.app.ipfs waterfall([ - (cb) => ipfs.block.put(new Block(data), cb), - (block, cb) => block.key('sha2-256', (err, key) => { + (cb) => multihashing(data, 'sha2-256', (err, multihash) => { if (err) { return cb(err) } - cb(null, [key, block]) - }) - ], (err, res) => { + cb(null, new Block(data, new CID(multihash))) + }), + (block, cb) => ipfs.block.put(block, cb) + ], (err, block) => { if (err) { log.error(err) return reply({ @@ -102,8 +103,8 @@ exports.put = { } return reply({ - Key: mh.toB58String(res[0]), - Size: res[1].data.length + Key: block.cid.toBaseEncodedString(), + Size: block.data.length }) }) } diff --git a/src/http-api/resources/config.js b/src/http-api/resources/config.js index 7344394a08..fec1fea76e 100644 --- a/src/http-api/resources/config.js +++ b/src/http-api/resources/config.js @@ -3,8 +3,8 @@ const debug = require('debug') const get = require('lodash.get') const set = require('lodash.set') -const log = debug('http-api:config') -log.error = debug('http-api:config:error') +const log = debug('jsipfs:http-api:config') +log.error = debug('jsipfs:http-api:config:error') const multipart = require('ipfs-multipart') exports = module.exports diff --git a/src/http-api/resources/files.js b/src/http-api/resources/files.js index ec28ff90a5..1a97bce1a7 100644 --- a/src/http-api/resources/files.js +++ b/src/http-api/resources/files.js @@ -4,8 +4,8 @@ const mh = require('multihashes') const multipart = require('ipfs-multipart') const debug = require('debug') const tar = require('tar-stream') -const log = debug('http-api:files') -log.error = debug('http-api:files:error') +const log = debug('jsipfs:http-api:files') +log.error = debug('jsipfs:http-api:files:error') const pull = require('pull-stream') const toPull = require('stream-to-pull-stream') const pushable = require('pull-pushable') diff --git a/src/http-api/resources/object.js b/src/http-api/resources/object.js index a68132cf83..4a366f4e60 100644 --- a/src/http-api/resources/object.js +++ b/src/http-api/resources/object.js @@ -8,8 +8,8 @@ const DAGNode = dagPB.DAGNode const waterfall = require('async/waterfall') const series = require('async/series') const debug = require('debug') -const log = debug('http-api:object') -log.error = debug('http-api:object:error') +const log = debug('jsipfs:http-api:object') +log.error = debug('jsipfs:http-api:object:error') exports = module.exports diff --git a/src/http-api/resources/swarm.js b/src/http-api/resources/swarm.js index dc4e5b04f3..8e81bfd337 100644 --- a/src/http-api/resources/swarm.js +++ b/src/http-api/resources/swarm.js @@ -1,8 +1,8 @@ 'use strict' const debug = require('debug') -const log = debug('http-api:block') -log.error = debug('http-api:block:error') +const log = debug('jsipfs:http-api:block') +log.error = debug('jsipfs:http-api:block:error') const multiaddr = require('multiaddr') exports = module.exports diff --git a/test/cli/block.js b/test/cli/block.js index 5869ed0a03..671f24b9b3 100644 --- a/test/cli/block.js +++ b/test/cli/block.js @@ -13,13 +13,14 @@ describe('block', () => runOnAndOff((thing) => { it('put', () => { return ipfs('block put test/test-data/hello').then((out) => { - expect(out).to.eql('zdj7Wgpi9yzsvjJerghrdhPFpe1p1jZFyB5GKLyXEzFQyaxVk') + expect(out).to.eql('QmZjTnYw2TFhn9Nn7tjmPSoTBoY7YRkwPzwSrSbabY24Kp') }) }) it('put with flags, format and mhtype', () => { return ipfs('block put --format eth-block --mhtype keccak-256 test/test-data/eth-block') - .then((out) => expect(out).to.eql('z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS')) + .then((out) => + expect(out).to.eql('z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS')) }) it('get', () => { diff --git a/test/cli/config.js b/test/cli/config.js index 8de6595978..9f13d81b25 100644 --- a/test/cli/config.js +++ b/test/cli/config.js @@ -19,7 +19,7 @@ describe('config', () => runOnAndOff((thing) => { before(() => { ipfs = thing.ipfs configPath = path.join(ipfs.repoPath, 'config') - originalConfigPath = path.join(__dirname, '../test-data/go-ipfs-repo/config') + originalConfigPath = path.join(__dirname, '../go-ipfs-repo/config') updatedConfig = () => JSON.parse(fs.readFileSync(configPath, 'utf8')) restoreConfig = () => fs.writeFileSync(configPath, fs.readFileSync(originalConfigPath, 'utf8'), 'utf8') }) diff --git a/test/cli/init.js b/test/cli/init.js index 50feb615b9..919a74f3af 100644 --- a/test/cli/init.js +++ b/test/cli/init.js @@ -14,6 +14,11 @@ describe('init', () => { const repoExistsSync = (p) => fs.existsSync(path.join(repoPath, p)) + const repoDirSync = (p) => { + return fs.readdirSync(path.join(repoPath, p)).filter((f) => { + return !f.startsWith('.') + }) + } beforeEach(() => { repoPath = os.tmpdir() + '/ipfs-' + Math.random().toString().substring(2, 8) ipfs = ipfsExec(repoPath) @@ -23,7 +28,7 @@ describe('init', () => { it('basic', () => { return ipfs('init').then(() => { - expect(repoExistsSync('blocks')).to.equal(true) + expect(repoDirSync('blocks')).to.have.length.above(2) expect(repoExistsSync('config')).to.equal(true) expect(repoExistsSync('version')).to.equal(true) }) @@ -31,7 +36,7 @@ describe('init', () => { it('bits', () => { return ipfs('init --bits 1024').then(() => { - expect(repoExistsSync('blocks')).to.equal(true) + expect(repoDirSync('blocks')).to.have.length.above(2) expect(repoExistsSync('config')).to.equal(true) expect(repoExistsSync('version')).to.equal(true) }) @@ -39,7 +44,7 @@ describe('init', () => { it('empty', () => { return ipfs('init --bits 1024 --empty-repo true').then(() => { - expect(repoExistsSync('blocks')).to.equal(false) + expect(repoDirSync('blocks')).to.have.length(2) expect(repoExistsSync('config')).to.equal(true) expect(repoExistsSync('version')).to.equal(true) }) diff --git a/test/core/bitswap.spec.js b/test/core/bitswap.spec.js index 8be6151714..cdfa3db9d9 100644 --- a/test/core/bitswap.spec.js +++ b/test/core/bitswap.spec.js @@ -10,14 +10,14 @@ const _ = require('lodash') const series = require('async/series') const waterfall = require('async/waterfall') const parallel = require('async/parallel') -const map = require('async/map') const leftPad = require('left-pad') const Block = require('ipfs-block') -const mh = require('multihashes') const bl = require('bl') const API = require('ipfs-api') const multiaddr = require('multiaddr') const isNode = require('detect-node') +const multihashing = require('multihashing-async') +const CID = require('cids') // This gets replaced by '../utils/create-repo-browser.js' in the browser const createTempRepo = require('../utils/create-repo-node.js') @@ -25,7 +25,13 @@ const createTempRepo = require('../utils/create-repo-node.js') const IPFS = require('../../src/core') function makeBlock (cb) { - return cb(null, new Block(`IPFS is awesome ${Math.random()}`)) + const d = new Buffer(`IPFS is awesome ${Math.random()}`) + multihashing(d, 'sha2-256', (err, multihash) => { + if (err) { + return cb(err) + } + cb(null, new Block(d, new CID(multihash))) + }) } describe('bitswap', () => { @@ -143,8 +149,7 @@ describe('bitswap', () => { cb() }, (cb) => remoteNode.block.put(block, cb), - (res, cb) => block.key('sha2-256', cb), - (key, cb) => inProcNode.block.get(key, cb), + (key, cb) => inProcNode.block.get(block.cid, cb), (b, cb) => { expect(b.data).to.be.eql(block.data) cb() @@ -156,18 +161,13 @@ describe('bitswap', () => { this.timeout(60 * 1000) let blocks - let keys const remoteNodes = [] series([ (cb) => parallel(_.range(6).map((i) => makeBlock), (err, _blocks) => { expect(err).to.not.exist() blocks = _blocks - map(blocks, (b, cb) => b.key('sha2-256', cb), (err, res) => { - expect(err).to.not.exist() - keys = res - cb() - }) + cb() }), (cb) => addNode(8, (err, _ipfs) => { remoteNodes.push(_ipfs) @@ -186,22 +186,18 @@ describe('bitswap', () => { (cb) => inProcNode.block.put(blocks[5], cb), // 3. Fetch blocks on all nodes (cb) => parallel(_.range(6).map((i) => (cbI) => { - const check = (n, k, callback) => { - n.block.get(k, (err, b) => { + const check = (n, cid, callback) => { + n.block.get(cid, (err, b) => { expect(err).to.not.exist() - expect( - (b.data || b).toString() - ).to.be.eql( - blocks[i].data.toString() - ) + expect(b).to.eql(blocks[i]) callback() }) } series([ - (cbJ) => check(remoteNodes[0], mh.toB58String(keys[i]), cbJ), - (cbJ) => check(remoteNodes[1], mh.toB58String(keys[i]), cbJ), - (cbJ) => check(inProcNode, keys[i], cbJ) + (cbJ) => check(remoteNodes[0], blocks[i].cid, cbJ), + (cbJ) => check(remoteNodes[1], blocks[i].cid, cbJ), + (cbJ) => check(inProcNode, blocks[i].cid, cbJ) ], cbI) }), cb) ], done) diff --git a/test/core/create-node.spec.js b/test/core/create-node.spec.js index 8e62448019..c8b3156865 100644 --- a/test/core/create-node.spec.js +++ b/test/core/create-node.spec.js @@ -6,6 +6,7 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) +const series = require('async/series') const isNode = require('detect-node') const IPFS = require('../../src/core') @@ -20,14 +21,14 @@ describe('create node', () => { repo: '/tmp/ipfs-repo-' + Math.random() }) - node.on('start', (err) => { + node.once('start', (err) => { expect(err).to.not.exist() node.config.get((err, config) => { expect(err).to.not.exist() expect(config.Identity).to.exist() - node.on('stop', done) + node.once('stop', done) node.stop() }) }) @@ -38,13 +39,13 @@ describe('create node', () => { repo: createTempRepo() }) - node.on('start', (err) => { + node.once('start', (err) => { expect(err).to.not.exist() node.config.get((err, config) => { expect(err).to.not.exist() expect(config.Identity).to.exist() - node.on('stop', done) + node.once('stop', done) node.stop() }) }) @@ -55,7 +56,7 @@ describe('create node', () => { repo: createTempRepo() }) - node.on('start', (err) => { + node.once('start', (err) => { expect(err).to.not.exist() node.config.get((err, config) => { expect(err).to.not.exist() @@ -64,7 +65,7 @@ describe('create node', () => { // note: key length doesn't map to buffer length expect(config.Identity.PrivKey.length).is.below(2048) - node.on('stop', done) + node.once('stop', done) node.stop() }) }) @@ -78,13 +79,13 @@ describe('create node', () => { } }) - node.on('start', (err) => { + node.once('start', (err) => { expect(err).to.not.exist() node.config.get((err, config) => { expect(err).to.not.exist() expect(config.Identity).to.exist() expect(config.Identity.PrivKey.length).is.below(1024) - node.on('stop', done) + node.once('stop', done) node.stop() }) }) @@ -95,7 +96,7 @@ describe('create node', () => { repo: createTempRepo(), init: false }) - node.on('error', (err) => { + node.once('error', (err) => { expect(err).to.exist() done() }) @@ -114,9 +115,9 @@ describe('create node', () => { happened = true } - node.on('error', shouldNotHappen) - node.on('start', shouldNotHappen) - node.on('stop', shouldNotHappen) + node.once('error', shouldNotHappen) + node.once('start', shouldNotHappen) + node.once('stop', shouldNotHappen) setTimeout(() => { expect(happened).to.equal(false) @@ -125,36 +126,36 @@ describe('create node', () => { }) it('init: true, start: false', (done) => { - // TODO investigate why this test likes to fail in the browser in travis - if (!isNode) { - return done() - } const node = new IPFS({ repo: createTempRepo(), init: true, - start: false + start: false, + config: { + Bootstrap: [] + } }) - node.on('init', () => { - node.on('stop', () => done()) - node.on('start', () => node.stop()) - node.start() - }) + node.once('error', done) + node.once('stop', done) + node.once('start', () => node.stop()) + + node.once('ready', () => node.start()) }) it('init: true, start: false, use callback', (done) => { - // TODO investigate why this test likes to fail in the browser in travis - if (!isNode) { - return done() - } - const node = new IPFS({ repo: createTempRepo(), init: true, - start: false + start: false, + config: { + Bootstrap: [] + } }) - node.on('init', () => node.start(() => node.stop(done))) + node.once('error', done) + node.once('ready', () => { + node.start(() => node.stop(done)) + }) }) it('overload config', (done) => { @@ -166,11 +167,12 @@ describe('create node', () => { config: { Addresses: { Swarm: ['/ip4/127.0.0.1/tcp/9977'] - } + }, + Bootstrap: [] } }) - node.on('start', (err) => { + node.once('start', (err) => { expect(err).to.not.exist() node.config.get((err, config) => { expect(err).to.not.exist() @@ -178,6 +180,9 @@ describe('create node', () => { expect(config.Addresses.Swarm).to.eql( ['/ip4/127.0.0.1/tcp/9977'] ) + + expect(config.Bootstrap).to.eql([]) + node.stop(done) }) }) @@ -191,12 +196,11 @@ describe('create node', () => { } }) - node.once('start', () => { - node.stop(() => { - node.start(() => { - node.stop(done) - }) - }) - }) + series([ + (cb) => node.once('start', cb), + (cb) => node.stop(cb), + (cb) => node.start(cb), + (cb) => node.stop(cb) + ], done) }) }) diff --git a/test/test-data/go-ipfs-repo/blocks/CIQEU/CIQEUWUVLBXVFYSYCHHSCRTXCYHGIOBXKWUMKFR3UPAFHQ5WK5362FQ.data b/test/go-ipfs-repo/blocks/2F/CIQEUWUVLBXVFYSYCHHSCRTXCYHGIOBXKWUMKFR3UPAFHQ5WK5362FQ.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQEU/CIQEUWUVLBXVFYSYCHHSCRTXCYHGIOBXKWUMKFR3UPAFHQ5WK5362FQ.data rename to test/go-ipfs-repo/blocks/2F/CIQEUWUVLBXVFYSYCHHSCRTXCYHGIOBXKWUMKFR3UPAFHQ5WK5362FQ.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQFF/CIQFFRR4O52TS2Z7QLDDTF32OIR4FWLKT5YLL7MLDVIT7DC3NHOK5VA.data b/test/go-ipfs-repo/blocks/5V/CIQFFRR4O52TS2Z7QLDDTF32OIR4FWLKT5YLL7MLDVIT7DC3NHOK5VA.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQFF/CIQFFRR4O52TS2Z7QLDDTF32OIR4FWLKT5YLL7MLDVIT7DC3NHOK5VA.data rename to test/go-ipfs-repo/blocks/5V/CIQFFRR4O52TS2Z7QLDDTF32OIR4FWLKT5YLL7MLDVIT7DC3NHOK5VA.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQJ2/CIQJ23BL4UHXA2KTI6NLTXZM4PW4VEFWQBJ4ACZQAS37BLGL4HUO5XY.data b/test/go-ipfs-repo/blocks/5X/CIQJ23BL4UHXA2KTI6NLTXZM4PW4VEFWQBJ4ACZQAS37BLGL4HUO5XY.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQJ2/CIQJ23BL4UHXA2KTI6NLTXZM4PW4VEFWQBJ4ACZQAS37BLGL4HUO5XY.data rename to test/go-ipfs-repo/blocks/5X/CIQJ23BL4UHXA2KTI6NLTXZM4PW4VEFWQBJ4ACZQAS37BLGL4HUO5XY.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQMB/CIQMB7DLJFKD267QJ2B5FJNHZPTSVA7IB6OHXSQ2XSVEEKMKK6RT75I.data b/test/go-ipfs-repo/blocks/75/CIQMB7DLJFKD267QJ2B5FJNHZPTSVA7IB6OHXSQ2XSVEEKMKK6RT75I.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQMB/CIQMB7DLJFKD267QJ2B5FJNHZPTSVA7IB6OHXSQ2XSVEEKMKK6RT75I.data rename to test/go-ipfs-repo/blocks/75/CIQMB7DLJFKD267QJ2B5FJNHZPTSVA7IB6OHXSQ2XSVEEKMKK6RT75I.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQKK/CIQKKLBWAIBQZOIS5X7E32LQAL6236OUKZTMHPQSFIXPWXNZHQOV7JQ.data b/test/go-ipfs-repo/blocks/7J/CIQKKLBWAIBQZOIS5X7E32LQAL6236OUKZTMHPQSFIXPWXNZHQOV7JQ.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQKK/CIQKKLBWAIBQZOIS5X7E32LQAL6236OUKZTMHPQSFIXPWXNZHQOV7JQ.data rename to test/go-ipfs-repo/blocks/7J/CIQKKLBWAIBQZOIS5X7E32LQAL6236OUKZTMHPQSFIXPWXNZHQOV7JQ.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQON/CIQONICFQZH7QVU6IPSIM3AK7AD554D3BWZPAGEAQYQOWMFZQDUUAEI.data b/test/go-ipfs-repo/blocks/AE/CIQONICFQZH7QVU6IPSIM3AK7AD554D3BWZPAGEAQYQOWMFZQDUUAEI.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQON/CIQONICFQZH7QVU6IPSIM3AK7AD554D3BWZPAGEAQYQOWMFZQDUUAEI.data rename to test/go-ipfs-repo/blocks/AE/CIQONICFQZH7QVU6IPSIM3AK7AD554D3BWZPAGEAQYQOWMFZQDUUAEI.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQHA/CIQHAKDLTL5GMIFGN5YVY4BA22FPHUIODJEXS4LCTQDWA275XAJDAPI.data b/test/go-ipfs-repo/blocks/AP/CIQHAKDLTL5GMIFGN5YVY4BA22FPHUIODJEXS4LCTQDWA275XAJDAPI.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQHA/CIQHAKDLTL5GMIFGN5YVY4BA22FPHUIODJEXS4LCTQDWA275XAJDAPI.data rename to test/go-ipfs-repo/blocks/AP/CIQHAKDLTL5GMIFGN5YVY4BA22FPHUIODJEXS4LCTQDWA275XAJDAPI.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQDD/CIQDDZ5EDQK5AP7LRTLZHQZUR2R3GECRFV3WPKNL7PL2SKFIL2LXC4Y.data b/test/go-ipfs-repo/blocks/C4/CIQDDZ5EDQK5AP7LRTLZHQZUR2R3GECRFV3WPKNL7PL2SKFIL2LXC4Y.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQDD/CIQDDZ5EDQK5AP7LRTLZHQZUR2R3GECRFV3WPKNL7PL2SKFIL2LXC4Y.data rename to test/go-ipfs-repo/blocks/C4/CIQDDZ5EDQK5AP7LRTLZHQZUR2R3GECRFV3WPKNL7PL2SKFIL2LXC4Y.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQDM/CIQDMKFEUGKSLXMEXO774EZOYCYNHPRVFD53ZSAU7237F67XDSQGCYQ.data b/test/go-ipfs-repo/blocks/CY/CIQDMKFEUGKSLXMEXO774EZOYCYNHPRVFD53ZSAU7237F67XDSQGCYQ.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQDM/CIQDMKFEUGKSLXMEXO774EZOYCYNHPRVFD53ZSAU7237F67XDSQGCYQ.data rename to test/go-ipfs-repo/blocks/CY/CIQDMKFEUGKSLXMEXO774EZOYCYNHPRVFD53ZSAU7237F67XDSQGCYQ.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQLB/CIQLBK52T5EHVHZY5URTG5JS3JCUJDQM2DRB5RVF33DCUUOFJNGVDUI.data b/test/go-ipfs-repo/blocks/DU/CIQLBK52T5EHVHZY5URTG5JS3JCUJDQM2DRB5RVF33DCUUOFJNGVDUI.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQLB/CIQLBK52T5EHVHZY5URTG5JS3JCUJDQM2DRB5RVF33DCUUOFJNGVDUI.data rename to test/go-ipfs-repo/blocks/DU/CIQLBK52T5EHVHZY5URTG5JS3JCUJDQM2DRB5RVF33DCUUOFJNGVDUI.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQPD/CIQPDQJBGYDZNMOAGGYNRNMP2VDKWBWGAEDDEJDACM3SGG3VIANDDXI.data b/test/go-ipfs-repo/blocks/DX/CIQPDQJBGYDZNMOAGGYNRNMP2VDKWBWGAEDDEJDACM3SGG3VIANDDXI.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQPD/CIQPDQJBGYDZNMOAGGYNRNMP2VDKWBWGAEDDEJDACM3SGG3VIANDDXI.data rename to test/go-ipfs-repo/blocks/DX/CIQPDQJBGYDZNMOAGGYNRNMP2VDKWBWGAEDDEJDACM3SGG3VIANDDXI.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQIX/CIQIXBZMUTXFC5QIGMLJNXLLHZOPGSL2PBC65D4UIVWM6TI5F5TAFNI.data b/test/go-ipfs-repo/blocks/FN/CIQIXBZMUTXFC5QIGMLJNXLLHZOPGSL2PBC65D4UIVWM6TI5F5TAFNI.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQIX/CIQIXBZMUTXFC5QIGMLJNXLLHZOPGSL2PBC65D4UIVWM6TI5F5TAFNI.data rename to test/go-ipfs-repo/blocks/FN/CIQIXBZMUTXFC5QIGMLJNXLLHZOPGSL2PBC65D4UIVWM6TI5F5TAFNI.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQH7/CIQH7OEYWXL34RWYL7VXLWEU4FWPVGT24VJT7DUZPTNLF25N25IGGQA.data b/test/go-ipfs-repo/blocks/GQ/CIQH7OEYWXL34RWYL7VXLWEU4FWPVGT24VJT7DUZPTNLF25N25IGGQA.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQH7/CIQH7OEYWXL34RWYL7VXLWEU4FWPVGT24VJT7DUZPTNLF25N25IGGQA.data rename to test/go-ipfs-repo/blocks/GQ/CIQH7OEYWXL34RWYL7VXLWEU4FWPVGT24VJT7DUZPTNLF25N25IGGQA.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQDD/CIQDDVW2EZIJF4NQH7WJNESD7XHQSXA5EGJVNTPVHD7444C2KLKXHDI.data b/test/go-ipfs-repo/blocks/HD/CIQDDVW2EZIJF4NQH7WJNESD7XHQSXA5EGJVNTPVHD7444C2KLKXHDI.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQDD/CIQDDVW2EZIJF4NQH7WJNESD7XHQSXA5EGJVNTPVHD7444C2KLKXHDI.data rename to test/go-ipfs-repo/blocks/HD/CIQDDVW2EZIJF4NQH7WJNESD7XHQSXA5EGJVNTPVHD7444C2KLKXHDI.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQJF/CIQJFGRQHQ45VCQLM7AJNF2GF5UHUAGGHC6LLAH6VYDEKLQMD4QLILY.data b/test/go-ipfs-repo/blocks/IL/CIQJFGRQHQ45VCQLM7AJNF2GF5UHUAGGHC6LLAH6VYDEKLQMD4QLILY.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQJF/CIQJFGRQHQ45VCQLM7AJNF2GF5UHUAGGHC6LLAH6VYDEKLQMD4QLILY.data rename to test/go-ipfs-repo/blocks/IL/CIQJFGRQHQ45VCQLM7AJNF2GF5UHUAGGHC6LLAH6VYDEKLQMD4QLILY.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQER/CIQERMRAAFXUAUOX3V2DCW7R77FRIVHQ3V5OIPPS3XQBX34KRPNOIRQ.data b/test/go-ipfs-repo/blocks/IR/CIQERMRAAFXUAUOX3V2DCW7R77FRIVHQ3V5OIPPS3XQBX34KRPNOIRQ.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQER/CIQERMRAAFXUAUOX3V2DCW7R77FRIVHQ3V5OIPPS3XQBX34KRPNOIRQ.data rename to test/go-ipfs-repo/blocks/IR/CIQERMRAAFXUAUOX3V2DCW7R77FRIVHQ3V5OIPPS3XQBX34KRPNOIRQ.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQJB/CIQJBQD2O6K4CGJVCCTJNUP57QHR4SKHZ74OIITBBGLOMCO3ZOLWLGA.data b/test/go-ipfs-repo/blocks/LG/CIQJBQD2O6K4CGJVCCTJNUP57QHR4SKHZ74OIITBBGLOMCO3ZOLWLGA.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQJB/CIQJBQD2O6K4CGJVCCTJNUP57QHR4SKHZ74OIITBBGLOMCO3ZOLWLGA.data rename to test/go-ipfs-repo/blocks/LG/CIQJBQD2O6K4CGJVCCTJNUP57QHR4SKHZ74OIITBBGLOMCO3ZOLWLGA.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQOL/CIQOLBQZSZAODJGGH6RYYVBUXHTS3SM5EORZDU63LYPEFUAFE4SBM4I.data b/test/go-ipfs-repo/blocks/M4/CIQOLBQZSZAODJGGH6RYYVBUXHTS3SM5EORZDU63LYPEFUAFE4SBM4I.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQOL/CIQOLBQZSZAODJGGH6RYYVBUXHTS3SM5EORZDU63LYPEFUAFE4SBM4I.data rename to test/go-ipfs-repo/blocks/M4/CIQOLBQZSZAODJGGH6RYYVBUXHTS3SM5EORZDU63LYPEFUAFE4SBM4I.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQOY/CIQOYW2THIZBRGI7IN33ROGCKOFZLXJJ2MPKYZBTV4H3N7GYHXMAO6A.data b/test/go-ipfs-repo/blocks/O6/CIQOYW2THIZBRGI7IN33ROGCKOFZLXJJ2MPKYZBTV4H3N7GYHXMAO6A.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQOY/CIQOYW2THIZBRGI7IN33ROGCKOFZLXJJ2MPKYZBTV4H3N7GYHXMAO6A.data rename to test/go-ipfs-repo/blocks/O6/CIQOYW2THIZBRGI7IN33ROGCKOFZLXJJ2MPKYZBTV4H3N7GYHXMAO6A.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQGP/CIQGPALRQ24P6NS4OWHTQ7R247ZI7KJWP3QWPQYS43LFULQC5ANLQFI.data b/test/go-ipfs-repo/blocks/QF/CIQGPALRQ24P6NS4OWHTQ7R247ZI7KJWP3QWPQYS43LFULQC5ANLQFI.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQGP/CIQGPALRQ24P6NS4OWHTQ7R247ZI7KJWP3QWPQYS43LFULQC5ANLQFI.data rename to test/go-ipfs-repo/blocks/QF/CIQGPALRQ24P6NS4OWHTQ7R247ZI7KJWP3QWPQYS43LFULQC5ANLQFI.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQOH/CIQOHMGEIKMPYHAUTL57JSEZN64SIJ5OIHSGJG4TJSSJLGI3PBJLQVI.data b/test/go-ipfs-repo/blocks/QV/CIQOHMGEIKMPYHAUTL57JSEZN64SIJ5OIHSGJG4TJSSJLGI3PBJLQVI.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQOH/CIQOHMGEIKMPYHAUTL57JSEZN64SIJ5OIHSGJG4TJSSJLGI3PBJLQVI.data rename to test/go-ipfs-repo/blocks/QV/CIQOHMGEIKMPYHAUTL57JSEZN64SIJ5OIHSGJG4TJSSJLGI3PBJLQVI.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQBE/CIQBED3K6YA5I3QQWLJOCHWXDRK5EXZQILBCKAPEDUJENZ5B5HJ5R3A.data b/test/go-ipfs-repo/blocks/R3/CIQBED3K6YA5I3QQWLJOCHWXDRK5EXZQILBCKAPEDUJENZ5B5HJ5R3A.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQBE/CIQBED3K6YA5I3QQWLJOCHWXDRK5EXZQILBCKAPEDUJENZ5B5HJ5R3A.data rename to test/go-ipfs-repo/blocks/R3/CIQBED3K6YA5I3QQWLJOCHWXDRK5EXZQILBCKAPEDUJENZ5B5HJ5R3A.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQHB/CIQHBGZNZRPWVEFNMTLP4OS5EAVHFMCX2HD7FZUC2B3WUU3D4LGKS5A.data b/test/go-ipfs-repo/blocks/S5/CIQHBGZNZRPWVEFNMTLP4OS5EAVHFMCX2HD7FZUC2B3WUU3D4LGKS5A.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQHB/CIQHBGZNZRPWVEFNMTLP4OS5EAVHFMCX2HD7FZUC2B3WUU3D4LGKS5A.data rename to test/go-ipfs-repo/blocks/S5/CIQHBGZNZRPWVEFNMTLP4OS5EAVHFMCX2HD7FZUC2B3WUU3D4LGKS5A.data diff --git a/test/go-ipfs-repo/blocks/SHARDING b/test/go-ipfs-repo/blocks/SHARDING new file mode 100644 index 0000000000..a153331dac --- /dev/null +++ b/test/go-ipfs-repo/blocks/SHARDING @@ -0,0 +1 @@ +/repo/flatfs/shard/v1/next-to-last/2 diff --git a/test/test-data/go-ipfs-repo/blocks/CIQHP/CIQHPUVCWD6JA6AFUVD6VA64TGWP67KYA3AIMBUMVWGZ5AQN2L2HSWQ.data b/test/go-ipfs-repo/blocks/SW/CIQHPUVCWD6JA6AFUVD6VA64TGWP67KYA3AIMBUMVWGZ5AQN2L2HSWQ.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQHP/CIQHPUVCWD6JA6AFUVD6VA64TGWP67KYA3AIMBUMVWGZ5AQN2L2HSWQ.data rename to test/go-ipfs-repo/blocks/SW/CIQHPUVCWD6JA6AFUVD6VA64TGWP67KYA3AIMBUMVWGZ5AQN2L2HSWQ.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQFE/CIQFEAGMNNXXTYKYQSANT6IBNTFN7WR5RPD5F6GN6MBKUUO25DNOTWQ.data b/test/go-ipfs-repo/blocks/TW/CIQFEAGMNNXXTYKYQSANT6IBNTFN7WR5RPD5F6GN6MBKUUO25DNOTWQ.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQFE/CIQFEAGMNNXXTYKYQSANT6IBNTFN7WR5RPD5F6GN6MBKUUO25DNOTWQ.data rename to test/go-ipfs-repo/blocks/TW/CIQFEAGMNNXXTYKYQSANT6IBNTFN7WR5RPD5F6GN6MBKUUO25DNOTWQ.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQOM/CIQOMBKARLB7PAITVSNH7VEGIQJRPL6J7FT2XYVKAXT4MQPXXPUYUNY.data b/test/go-ipfs-repo/blocks/UN/CIQOMBKARLB7PAITVSNH7VEGIQJRPL6J7FT2XYVKAXT4MQPXXPUYUNY.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQOM/CIQOMBKARLB7PAITVSNH7VEGIQJRPL6J7FT2XYVKAXT4MQPXXPUYUNY.data rename to test/go-ipfs-repo/blocks/UN/CIQOMBKARLB7PAITVSNH7VEGIQJRPL6J7FT2XYVKAXT4MQPXXPUYUNY.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQDV/CIQDVKITASFS55MC2TXCX5XMZLMGTYVODWPEDIW7JYEG7YXBIA7IUWY.data b/test/go-ipfs-repo/blocks/UW/CIQDVKITASFS55MC2TXCX5XMZLMGTYVODWPEDIW7JYEG7YXBIA7IUWY.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQDV/CIQDVKITASFS55MC2TXCX5XMZLMGTYVODWPEDIW7JYEG7YXBIA7IUWY.data rename to test/go-ipfs-repo/blocks/UW/CIQDVKITASFS55MC2TXCX5XMZLMGTYVODWPEDIW7JYEG7YXBIA7IUWY.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQGF/CIQGFTQ7FSI2COUXWWLOQ45VUM2GUZCGAXLWCTOKKPGTUWPXHBNIVOY.data b/test/go-ipfs-repo/blocks/VO/CIQGFTQ7FSI2COUXWWLOQ45VUM2GUZCGAXLWCTOKKPGTUWPXHBNIVOY.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQGF/CIQGFTQ7FSI2COUXWWLOQ45VUM2GUZCGAXLWCTOKKPGTUWPXHBNIVOY.data rename to test/go-ipfs-repo/blocks/VO/CIQGFTQ7FSI2COUXWWLOQ45VUM2GUZCGAXLWCTOKKPGTUWPXHBNIVOY.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQFT/CIQFTFEEHEDF6KLBT32BFAGLXEZL4UWFNWM4LFTLMXQBCERZ6CMLX3Y.data b/test/go-ipfs-repo/blocks/X3/CIQFTFEEHEDF6KLBT32BFAGLXEZL4UWFNWM4LFTLMXQBCERZ6CMLX3Y.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQFT/CIQFTFEEHEDF6KLBT32BFAGLXEZL4UWFNWM4LFTLMXQBCERZ6CMLX3Y.data rename to test/go-ipfs-repo/blocks/X3/CIQFTFEEHEDF6KLBT32BFAGLXEZL4UWFNWM4LFTLMXQBCERZ6CMLX3Y.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQJG/CIQJGO2B2N75IUEM372FSMG76VV256I4PXBULZZ5ASNLK4FL4EG7XOI.data b/test/go-ipfs-repo/blocks/XO/CIQJGO2B2N75IUEM372FSMG76VV256I4PXBULZZ5ASNLK4FL4EG7XOI.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQJG/CIQJGO2B2N75IUEM372FSMG76VV256I4PXBULZZ5ASNLK4FL4EG7XOI.data rename to test/go-ipfs-repo/blocks/XO/CIQJGO2B2N75IUEM372FSMG76VV256I4PXBULZZ5ASNLK4FL4EG7XOI.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQEN/CIQENVCICS44LLYUDQ5KVN6ALXC6QRHK2X4R6EUFRMBB5OSFO2FUYDQ.data b/test/go-ipfs-repo/blocks/YD/CIQENVCICS44LLYUDQ5KVN6ALXC6QRHK2X4R6EUFRMBB5OSFO2FUYDQ.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQEN/CIQENVCICS44LLYUDQ5KVN6ALXC6QRHK2X4R6EUFRMBB5OSFO2FUYDQ.data rename to test/go-ipfs-repo/blocks/YD/CIQENVCICS44LLYUDQ5KVN6ALXC6QRHK2X4R6EUFRMBB5OSFO2FUYDQ.data diff --git a/test/test-data/go-ipfs-repo/blocks/CIQLB/CIQLBS5HG4PRCRQ7O4EBXFD5QN6MTI5YBYMCVQJDXPKCOVR6RMLHZFQ.data b/test/go-ipfs-repo/blocks/ZF/CIQLBS5HG4PRCRQ7O4EBXFD5QN6MTI5YBYMCVQJDXPKCOVR6RMLHZFQ.data similarity index 100% rename from test/test-data/go-ipfs-repo/blocks/CIQLB/CIQLBS5HG4PRCRQ7O4EBXFD5QN6MTI5YBYMCVQJDXPKCOVR6RMLHZFQ.data rename to test/go-ipfs-repo/blocks/ZF/CIQLBS5HG4PRCRQ7O4EBXFD5QN6MTI5YBYMCVQJDXPKCOVR6RMLHZFQ.data diff --git a/test/go-ipfs-repo/blocks/_README b/test/go-ipfs-repo/blocks/_README new file mode 100644 index 0000000000..23cb090956 --- /dev/null +++ b/test/go-ipfs-repo/blocks/_README @@ -0,0 +1,30 @@ +This is a repository of IPLD objects. Each IPLD object is in a single file, +named .data. Where is the +"base32" encoding of the CID (as specified in +https://github.com/multiformats/multibase) without the 'B' prefix. +All the object files are placed in a tree of directories, based on a +function of the CID. This is a form of sharding similar to +the objects directory in git repositories. Previously, we used +prefixes, we now use the next-to-last two charters. + + func NextToLast(base32cid string) { + nextToLastLen := 2 + offset := len(base32cid) - nextToLastLen - 1 + return str[offset : offset+nextToLastLen] + } + +For example, an object with a base58 CIDv1 of + + zb2rhYSxw4ZjuzgCnWSt19Q94ERaeFhu9uSqRgjSdx9bsgM6f + +has a base32 CIDv1 of + + BAFKREIA22FLID5AJ2KU7URG47MDLROZIH6YF2KALU2PWEFPVI37YLKRSCA + +and will be placed at + + SC/AFKREIA22FLID5AJ2KU7URG47MDLROZIH6YF2KALU2PWEFPVI37YLKRSCA.data + +with 'SC' being the last-to-next two characters and the 'B' at the +beginning of the CIDv1 string is the multibase prefix that is not +stored in the filename. diff --git a/test/test-data/go-ipfs-repo/config b/test/go-ipfs-repo/config similarity index 100% rename from test/test-data/go-ipfs-repo/config rename to test/go-ipfs-repo/config diff --git a/test/test-data/go-ipfs-repo/datastore/000002.ldb b/test/go-ipfs-repo/datastore/000002.ldb similarity index 100% rename from test/test-data/go-ipfs-repo/datastore/000002.ldb rename to test/go-ipfs-repo/datastore/000002.ldb diff --git a/test/test-data/go-ipfs-repo/datastore/000005.ldb b/test/go-ipfs-repo/datastore/000005.ldb similarity index 100% rename from test/test-data/go-ipfs-repo/datastore/000005.ldb rename to test/go-ipfs-repo/datastore/000005.ldb diff --git a/test/test-data/go-ipfs-repo/datastore/000010.ldb b/test/go-ipfs-repo/datastore/000010.ldb similarity index 100% rename from test/test-data/go-ipfs-repo/datastore/000010.ldb rename to test/go-ipfs-repo/datastore/000010.ldb diff --git a/test/test-data/go-ipfs-repo/datastore/CURRENT b/test/go-ipfs-repo/datastore/CURRENT similarity index 100% rename from test/test-data/go-ipfs-repo/datastore/CURRENT rename to test/go-ipfs-repo/datastore/CURRENT diff --git a/test/test-data/go-ipfs-repo/datastore/LOCK b/test/go-ipfs-repo/datastore/LOCK similarity index 100% rename from test/test-data/go-ipfs-repo/datastore/LOCK rename to test/go-ipfs-repo/datastore/LOCK diff --git a/test/test-data/go-ipfs-repo/datastore/LOG b/test/go-ipfs-repo/datastore/LOG similarity index 100% rename from test/test-data/go-ipfs-repo/datastore/LOG rename to test/go-ipfs-repo/datastore/LOG diff --git a/test/test-data/go-ipfs-repo/datastore/LOG.old b/test/go-ipfs-repo/datastore/LOG.old similarity index 100% rename from test/test-data/go-ipfs-repo/datastore/LOG.old rename to test/go-ipfs-repo/datastore/LOG.old diff --git a/test/test-data/go-ipfs-repo/datastore/MANIFEST-000014 b/test/go-ipfs-repo/datastore/MANIFEST-000014 similarity index 100% rename from test/test-data/go-ipfs-repo/datastore/MANIFEST-000014 rename to test/go-ipfs-repo/datastore/MANIFEST-000014 diff --git a/test/go-ipfs-repo/version b/test/go-ipfs-repo/version new file mode 100644 index 0000000000..7ed6ff82de --- /dev/null +++ b/test/go-ipfs-repo/version @@ -0,0 +1 @@ +5 diff --git a/test/http-api/index.js b/test/http-api/index.js index acfcf2f3a3..ad647e92df 100644 --- a/test/http-api/index.js +++ b/test/http-api/index.js @@ -13,7 +13,7 @@ const path = require('path') const clean = require('../utils/clean') describe('HTTP API', () => { - const repoExample = path.join(__dirname, '../test-data/go-ipfs-repo') + const repoExample = path.join(__dirname, '../go-ipfs-repo') const repoTests = path.join(__dirname, '../repo-tests-run') let http = {} @@ -21,13 +21,10 @@ describe('HTTP API', () => { before((done) => { http.api = new API(repoTests) - clean(repoTests) ncp(repoExample, repoTests, (err) => { - if (err) { - return done(err) - } + expect(err).to.not.exist() - http.api.start(done) + http.api.start(false, done) }) }) diff --git a/test/http-api/over-ipfs-api/block.js b/test/http-api/over-ipfs-api/block.js index ee35da1933..2555386587 100644 --- a/test/http-api/over-ipfs-api/block.js +++ b/test/http-api/over-ipfs-api/block.js @@ -21,9 +21,10 @@ module.exports = (ctl) => { waterfall([ (cb) => ctl.block.put(data, cb), - (block, cb) => block.key(cb), - (key, cb) => { - expect(key).to.eql(multihash.fromB58String(expectedResult.key)) + (block, cb) => { + expect(block.cid.multihash).to.eql( + multihash.fromB58String(expectedResult.key) + ) cb() } ], done) diff --git a/test/http-api/over-ipfs-api/object.js b/test/http-api/over-ipfs-api/object.js index 5664c57c8f..105b25bdd3 100644 --- a/test/http-api/over-ipfs-api/object.js +++ b/test/http-api/over-ipfs-api/object.js @@ -191,7 +191,7 @@ module.exports = (ctl) => { const key = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n' const filePath = 'test/test-data/badnode.json' const expectedResult = { - data: fs.readFileSync(filePath).toString(), + data: fs.readFileSync(filePath), multihash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', links: [], size: 19 @@ -226,7 +226,7 @@ module.exports = (ctl) => { const key = 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6' const filePath = 'test/test-data/badnode.json' const expectedResult = { - data: fs.readFileSync(filePath).toString(), + data: fs.readFileSync(filePath), multihash: 'QmfY37rjbPCZRnhvvJuQ46htW3VCAWziVB991P79h6WSv6', links: [], size: 19 diff --git a/test/http-api/spec/config.js b/test/http-api/spec/config.js index 06e0755e08..43cc49409b 100644 --- a/test/http-api/spec/config.js +++ b/test/http-api/spec/config.js @@ -10,7 +10,7 @@ const path = require('path') module.exports = (http) => { describe('/config', () => { const configPath = path.join(__dirname, '../../repo-tests-run/config') - const originalConfigPath = path.join(__dirname, '../../test-data/go-ipfs-repo/config') + const originalConfigPath = path.join(__dirname, '../../go-ipfs-repo/config') let updatedConfig let api diff --git a/test/interop/daemons/go.js b/test/interop/daemons/go.js index e1719b7024..e8a8b214af 100644 --- a/test/interop/daemons/go.js +++ b/test/interop/daemons/go.js @@ -3,8 +3,6 @@ const ctl = require('ipfsd-ctl') const waterfall = require('async/waterfall') -const flags = ['--enable-mplex-experiment'] - class GoDaemon { constructor (opts) { opts = opts || { @@ -39,7 +37,7 @@ class GoDaemon { this.node = node this.node.setConfig('Bootstrap', '[]', cb) }, - (res, cb) => this.node.startDaemon(flags, cb), + (res, cb) => this.node.startDaemon(cb), (api, cb) => { this.api = api diff --git a/test/interop/daemons/js.js b/test/interop/daemons/js.js index a0d0376f32..66b81975ff 100644 --- a/test/interop/daemons/js.js +++ b/test/interop/daemons/js.js @@ -1,39 +1,29 @@ 'use strict' +const EventEmitter = require('events').EventEmitter const IPFSAPI = require('ipfs-api') const series = require('async/series') const rimraf = require('rimraf') -const IPFSRepo = require('ipfs-repo') const tmpDir = require('../util').tmpDir -const IPFS = require('../../../src/core') -const HTTPAPI = require('../../../src/http-api') +const HttpApi = require('../../../src/http-api') -function setPorts (ipfs, port, callback) { - series([ - (cb) => ipfs.config.set( - 'Addresses.Gateway', - '/ip4/127.0.0.1/tcp/' + (9090 + port), - cb - ), - (cb) => ipfs.config.set( - 'Addresses.API', - '/ip4/127.0.0.1/tcp/' + (5002 + port), - cb - ), - (cb) => ipfs.config.set( - 'Addresses.Swarm', - [ - '/ip4/0.0.0.0/tcp/' + (4003 + port), - '/ip4/0.0.0.0/tcp/' + (4004 + port) + '/ws' - ], - cb - ) - ], callback) +function portConfig (port) { + port = port + 5 + + return { + Gateway: '/ip4/127.0.0.1/tcp/' + (9090 + port), + API: '/ip4/127.0.0.1/tcp/' + (5002 + port), + Swarm: [ + '/ip4/127.0.0.1/tcp/' + (4003 + port), + '/ip4/127.0.0.1/tcp/' + (4104 + port) + '/ws' + ] + } } -class JsDaemon { +class JsDaemon extends EventEmitter { constructor (opts) { + super() opts = Object.assign({}, { disposable: true, init: true @@ -42,61 +32,42 @@ class JsDaemon { this.path = opts.path this.disposable = opts.disposable this.init = opts.init - this.port = opts.port + this.port = opts.port || 1 this.path = opts.path || tmpDir() - let repo + this._started = false + if (this.init) { - repo = this.path - } else { - repo = new IPFSRepo(this.path, { - stores: require('fs-pull-blob-store') + const p = portConfig(this.port) + this.node = new HttpApi(this.path, { + Bootstrap: [], + Addresses: p }) + } else { + this.node = new HttpApi(this.path) } - this.ipfs = new IPFS({ - repo: repo, - init: false, - start: false, - EXPERIMENTAL: { - pubsub: true + + this.node.start(this.init, (err) => { + if (err) { + throw err } - }) + this._started = true + this.api = new IPFSAPI(this.node.apiMultiaddr) - this.node = null - this.api = null + this.emit('start') + }) } start (callback) { - console.log('starting js', this.path) - series([ - (cb) => { - if (this.init) { - this.ipfs.init(cb) - } else { - cb() - } - }, - (cb) => this.ipfs.config.set('Bootstrap', [], cb), - (cb) => { - if (this.port) { - console.log('setting to port', this.port) - setPorts(this.ipfs, this.port, cb) - } else { - cb() - } - }, - (cb) => { - this.node = new HTTPAPI(this.ipfs._repo) - this.node.start(cb) - }, - (cb) => { - this.api = new IPFSAPI(this.node.apiMultiaddr) - cb() - } - ], (err) => callback(err)) + if (!this._started) { + return this.once('start', callback) + } + + callback() } stop (callback) { + this._started = false series([ (cb) => this.node.stop(cb), (cb) => { diff --git a/test/interop/index.js b/test/interop/index.js index 01385da125..010bb4a471 100644 --- a/test/interop/index.js +++ b/test/interop/index.js @@ -49,8 +49,8 @@ describe('basic', () => { before((done) => { goDaemon = new GoDaemon() - jsDaemon = new JsDaemon() - js2Daemon = new JsDaemon({port: 1}) + jsDaemon = new JsDaemon({port: 1}) + js2Daemon = new JsDaemon({port: 2}) parallel([ (cb) => goDaemon.start(cb), diff --git a/test/interop/repo.js b/test/interop/repo.js index b892c937fc..f7200ddd37 100644 --- a/test/interop/repo.js +++ b/test/interop/repo.js @@ -25,13 +25,16 @@ function catAndCheck (daemon, hash, data, callback) { }) } -// TODO: these are not working, will need to figure out why -describe.skip('repo', () => { +describe('repo', () => { it('read repo: go -> js', (done) => { const dir = os.tmpdir() + '/' + Math.ceil(Math.random() * 10000) const data = crypto.randomBytes(1024 * 5) - const goDaemon = new GoDaemon({init: true, disposable: false, path: dir}) + const goDaemon = new GoDaemon({ + init: true, + disposable: false, + path: dir + }) let jsDaemon let hash @@ -44,7 +47,11 @@ describe.skip('repo', () => { }, (cb) => goDaemon.stop(cb), (cb) => { - jsDaemon = new JsDaemon({init: false, disposable: false, path: dir}) + jsDaemon = new JsDaemon({ + init: false, + disposable: false, + path: dir + }) jsDaemon.start(cb) }, (cb) => catAndCheck(goDaemon, hash, data, cb), diff --git a/test/test-data/go-ipfs-repo/version b/test/test-data/go-ipfs-repo/version deleted file mode 100644 index b8626c4cff..0000000000 --- a/test/test-data/go-ipfs-repo/version +++ /dev/null @@ -1 +0,0 @@ -4 diff --git a/test/utils/create-repo-browser.js b/test/utils/create-repo-browser.js index d280acf896..e72fec18c8 100644 --- a/test/utils/create-repo-browser.js +++ b/test/utils/create-repo-browser.js @@ -2,7 +2,6 @@ 'use strict' const IPFSRepo = require('ipfs-repo') -const Store = require('idb-pull-blob-store') const idb = self.indexedDB || self.mozIndexedDB || @@ -12,15 +11,14 @@ const idb = self.indexedDB || function createTempRepo (repoPath) { repoPath = repoPath || '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) - const repo = new IPFSRepo(repoPath, { - bits: 1024, - stores: Store - }) + const repo = new IPFSRepo(repoPath) repo.teardown = (done) => { - idb.deleteDatabase(repoPath) - idb.deleteDatabase(repoPath + '/blocks') - done() + repo.close(() => { + idb.deleteDatabase(repoPath) + idb.deleteDatabase(repoPath + '/blocks') + done() + }) } return repo diff --git a/test/utils/create-repo-node.js b/test/utils/create-repo-node.js index b1a17512e1..629e31c7ff 100644 --- a/test/utils/create-repo-node.js +++ b/test/utils/create-repo-node.js @@ -1,20 +1,23 @@ 'use strict' const IPFSRepo = require('ipfs-repo') -const Store = require('fs-pull-blob-store') const clean = require('./clean') +const series = require('async/series') function createTempRepo (repoPath) { repoPath = repoPath || '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) - const repo = new IPFSRepo(repoPath, { - bits: 1024, - stores: Store - }) + const repo = new IPFSRepo(repoPath) repo.teardown = (done) => { - clean(repoPath) - setImmediate(() => done()) + series([ + // ignore err, might have been closed already + (cb) => repo.close(() => cb()), + (cb) => { + clean(repoPath) + cb() + } + ], done) } return repo diff --git a/test/utils/ipfs-factory-daemon/index.js b/test/utils/ipfs-factory-daemon/index.js index cd9f1d9ca4..f9606a7414 100644 --- a/test/utils/ipfs-factory-daemon/index.js +++ b/test/utils/ipfs-factory-daemon/index.js @@ -2,10 +2,10 @@ const PeerId = require('peer-id') const IPFSAPI = require('ipfs-api') -const IPFS = require('../../../src/core') const clean = require('../clean') -const HTTPAPI = require('../../../src/http-api') +const HttpApi = require('../../../src/http-api') const series = require('async/series') +const eachSeries = require('async/eachSeries') const defaultConfig = require('./default-config.json') const os = require('os') @@ -28,8 +28,7 @@ class Factory { repoPath = repoPath || os.tmpdir() + '/ipfs-' + Math.random().toString().substring(2, 8) + - '-' + - new Date().toString() + '-' + Date.now() let daemon let ctl @@ -55,25 +54,11 @@ class Factory { }) }, (cb) => { - // create the node - IPFS.createNode({ - repo: repoPath, - start: false, - config: config, - EXPERIMENTAL: { - pubsub: true - } - }) - - setTimeout(cb, 1000) - }, - (cb) => { - // create the daemon - daemon = new HTTPAPI(repoPath) + daemon = new HttpApi(repoPath, config) daemon.repoPath = repoPath this.daemonsSpawned.push(daemon) - daemon.start(cb) + daemon.start(true, cb) }, (cb) => { ctl = IPFSAPI(daemon.apiMultiaddr) @@ -85,17 +70,15 @@ class Factory { } dismantle (callback) { - const tasks = this.daemonsSpawned.map((daemon) => (cb) => { - daemon.stop((err) => { + eachSeries(this.daemonsSpawned, (d, cb) => { + d.stop((err) => { if (err) { - return cb(err) + console.error('error stopping', err) } - clean(daemon.repoPath) + clean(d.repoPath) cb() }) - }) - - series(tasks, callback) + }, callback) } } diff --git a/test/utils/ipfs-factory-instance/index.js b/test/utils/ipfs-factory-instance/index.js index 04b117a4cf..ec1f3d410d 100644 --- a/test/utils/ipfs-factory-instance/index.js +++ b/test/utils/ipfs-factory-instance/index.js @@ -2,6 +2,7 @@ const PeerId = require('peer-id') const series = require('async/series') +const each = require('async/each') const defaultConfig = require('./default-config.json') const IPFS = require('../../../src/core') @@ -48,7 +49,7 @@ function Factory () { } }) - node.on('start', () => { + node.once('start', () => { nodes.push({ repo: repo, ipfs: node }) callback(null, node) }) @@ -76,8 +77,8 @@ function Factory () { this.dismantle = function (callback) { series([ - (cb) => series(nodes.map((el) => el.ipfs.stop), cb), - (cb) => series(nodes.map((el) => el.repo.teardown), cb) + (cb) => each(nodes, (el, cb) => el.ipfs.stop(cb), cb), + (cb) => each(nodes, (el, cb) => el.repo.teardown(cb), cb) ], callback) } } diff --git a/test/utils/on-and-off.js b/test/utils/on-and-off.js index 94e37a89b8..6bc44b7a71 100644 --- a/test/utils/on-and-off.js +++ b/test/utils/on-and-off.js @@ -17,7 +17,7 @@ function off (tests) { let repoPath before(() => { - repoPath = os.tmpdir() + '/ipfs-' + Math.random().toString().substring(2, 8) + repoPath = os.tmpdir() + '/ipfs-' + Math.random().toString().substring(2, 16) thing.ipfs = ipfsExec(repoPath) thing.ipfs.repoPath = repoPath return thing.ipfs('init')