diff --git a/karma.conf.js b/karma.conf.js index 5890570ca3..9384668107 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -1,31 +1,30 @@ module.exports = function (config) { config.set({ - - // base path, that will be used to resolve files and exclude basePath: '', - - // frameworks to use frameworks: ['mocha'], - // list of files / patterns to load in the browser files: [ 'tests/test-core/browser.js' ], - // list of preprocessors preprocessors: { 'tests/test-core/*': ['webpack'] }, webpack: { resolve: { - extensions: ['', '.js'] + extensions: ['', '.js', '.json'] }, externals: { fs: '{}' }, node: { Buffer: true + }, + module: { + loaders: [ + { test: /\.json$/, loader: 'json' } + ] } }, @@ -35,39 +34,13 @@ module.exports = function (config) { colors: true } }, - - // test results reporter to use - // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage' reporters: ['spec'], - - // web server port port: 9876, - - // enable / disable colors in the output (reporters and logs) colors: true, - - // level of logging - // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG logLevel: config.LOG_INFO, - - // enable / disable watching file and executing tests whenever any file changes autoWatch: false, - - // Start these browsers, currently available: - // - Chrome - // - ChromeCanary - // - Firefox - // - Opera (has to be installed with `npm install karma-opera-launcher`) - // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`) - // - PhantomJS - // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`) browsers: process.env.TRAVIS ? ['Firefox'] : ['Chrome'], - - // If browser does not capture in given timeout [ms], kill it captureTimeout: 60000, - - // Continuous Integration mode - // if true, it capture browsers, run tests and exit singleRun: true }) } diff --git a/package.json b/package.json index da7f7b3ddc..6d5842f8c9 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "scripts": { "lint": "standard", "coverage": "istanbul cover --print both -- _mocha tests/test-*/index.js", - "test": "npm run test:node && npm run test:browser", + "test": "npm run test:node", "test:node": "mocha tests/test-*/index.js", "test:browser": "karma start karma.conf.js", "test:core": "mocha tests/test-core/index.js", @@ -35,9 +35,12 @@ "homepage": "https://github.com/ipfs/js-ipfs#readme", "devDependencies": { "async": "^1.5.2", + "binary-loader": "0.0.1", + "brfs": "^1.4.3", "chai": "^3.4.1", "fs-blob-store": "^5.2.1", "istanbul": "^0.4.1", + "json-loader": "^0.5.4", "karma": "^0.13.19", "karma-chrome-launcher": "^0.2.2", "karma-cli": "^0.1.2", @@ -45,7 +48,7 @@ "karma-mocha": "^0.2.1", "karma-spec-reporter": "0.0.23", "karma-webpack": "^1.7.0", - "local-storage-blob-store": "0.0.2", + "local-storage-blob-store": "0.0.3", "lodash": "^4.0.0", "mocha": "^2.3.4", "ncp": "^2.0.0", @@ -54,13 +57,17 @@ "raw-loader": "^0.5.1", "rimraf": "^2.4.4", "standard": "^5.4.1", - "webpack": "^1.12.11" + "transform-loader": "^0.2.3", + "webpack": "diasdavid/webpack#81f5994" }, "dependencies": { + "bl": "^1.0.0", "boom": "^3.1.1", + "bs58": "^3.0.0", "debug": "^2.2.0", "hapi": "^12.0.0", "ipfs-repo": "^0.4.1", + "ipfs-merkle-dag": "vijayee/js-ipfs-merkle-dag", "lodash.get": "^4.0.0", "lodash.set": "^4.0.0", "ronin": "^0.3.11" diff --git a/src/http-api/index.js b/src/http-api/index.js index 5da4f15c71..6d66f0ed75 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -22,7 +22,7 @@ exports.start = callback => { }) server.connection({ - port: 9000 + port: 9001 }) // load routes diff --git a/src/ipfs-core/index.js b/src/ipfs-core/index.js index 743a54d9ad..d81f065513 100644 --- a/src/ipfs-core/index.js +++ b/src/ipfs-core/index.js @@ -1,6 +1,10 @@ 'use strict' const defaultRepo = require('./default-repo') +// const bl = require('bl') +const MerkleDAG = require('ipfs-merkle-dag') +const BlockService = MerkleDAG.BlockService +// const Block = MerkleDAG.Block exports = module.exports = IPFS @@ -12,6 +16,7 @@ function IPFS (repo) { if (!repo) { repo = defaultRepo() } + const bs = new BlockService(repo) this.daemon = callback => { // 1. read repo to get peer data @@ -124,4 +129,27 @@ function IPFS (repo) { }) } } + + this.block = { + get: (multihash, callback) => { + bs.getBlock(multihash, callback) + }, + put: (block, callback) => { + bs.addBlock(block, callback) + }, + del: (multihash, callback) => { + bs.deleteBlock(multihash, callback) + }, + stat: (multihash, callback) => { + bs.getBlock(multihash, (err, block) => { + if (err) { + return callback(err) + } + callback(null, { + Key: multihash, + Size: block.data.length + }) + }) + } + } } diff --git a/tests/test-core/browser.js b/tests/test-core/browser.js index cf3659e622..d72ed845b3 100644 --- a/tests/test-core/browser.js +++ b/tests/test-core/browser.js @@ -5,7 +5,7 @@ const async = require('async') const store = require('local-storage-blob-store') const _ = require('lodash') -var repoContext = require.context('raw!../repo-example', true) +var repoContext = require.context('binary!../repo-example', true) describe('core', function () { before(function (done) { @@ -15,7 +15,7 @@ describe('core', function () { repoContext.keys().forEach(function (key) { repoData.push({ key: key.replace('./', ''), - value: repoContext(key) + value: new Buffer(require('binary!./../repo-example/' + key.replace('./', '')), 'binary') }) }) diff --git a/tests/test-core/test-block.js b/tests/test-core/test-block.js new file mode 100644 index 0000000000..faa7090992 --- /dev/null +++ b/tests/test-core/test-block.js @@ -0,0 +1,74 @@ +/* globals describe, it */ + +'use strict' + +const expect = require('chai').expect +const base58 = require('bs58') +const fs = require('fs') +const IPFS = require('../../src/ipfs-core') +const Block = require('ipfs-merkle-dag').Block + +const isNode = !global.window + +const fileA = isNode + ? fs.readFileSync(process.cwd() + '/tests/repo-example/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data') + : new Buffer(require('binary!./../repo-example/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data'), 'binary') + +// console.log('=>', fileA) +// console.log('=>', fileA.length) + +describe('block', () => { + var ipfs + + it('get', done => { + ipfs = new IPFS() + const b58mh = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe' + const mh = new Buffer(base58.decode(b58mh)) + ipfs.block.get(mh, (err, block) => { + expect(err).to.not.exist + const eq = fileA.equals(block.data) + expect(eq).to.equal(true) + done() + }) + }) + it('put', done => { + var b = new Block('random data') + ipfs.block.put(b, function (err) { + expect(err).to.not.exist + ipfs.block.get(b.key, function (err, block) { + expect(err).to.not.exist + expect(b.data.equals(block.data)).to.equal(true) + expect(b.key.equals(block.key)).to.equal(true) + done() + }) + }) + }) + + it('rm', done => { + var b = new Block('I will not last long enough') + ipfs.block.put(b, function (err) { + expect(err).to.not.exist + ipfs.block.get(b.key, function (err, block) { + expect(err).to.not.exist + ipfs.block.del(b.key, function (err) { + expect(err).to.not.exist + ipfs.block.get(b.key, function (err, block) { + expect(err).to.exist + done() + }) + }) + }) + }) + }) + + it('stat', done => { + const mh = new Buffer(base58 + .decode('QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe')) + ipfs.block.stat(mh, (err, stats) => { + expect(err).to.not.exist + expect(stats.Key.equals(mh)).to.equal(true) + expect(stats.Size).to.equal(309) + done() + }) + }) +})