Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
39 changes: 6 additions & 33 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -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' }
]
}
},

Expand All @@ -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
})
}
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -35,17 +35,20 @@
"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",
"karma-firefox-launcher": "^0.1.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",
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/http-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exports.start = callback => {
})

server.connection({
port: 9000
port: 9001
})

// load routes
Expand Down
28 changes: 28 additions & 0 deletions src/ipfs-core/index.js
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
})
})
}
}
}
4 changes: 2 additions & 2 deletions tests/test-core/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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')
})
})

Expand Down
74 changes: 74 additions & 0 deletions tests/test-core/test-block.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})