Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 71013ba

Browse files
committed
Merge pull request #52 from ipfs/feat/blocks
WIP: feat/blocks
2 parents 731c24c + b575bec commit 71013ba

File tree

6 files changed

+121
-39
lines changed

6 files changed

+121
-39
lines changed

karma.conf.js

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
module.exports = function (config) {
22
config.set({
3-
4-
// base path, that will be used to resolve files and exclude
53
basePath: '',
6-
7-
// frameworks to use
84
frameworks: ['mocha'],
95

10-
// list of files / patterns to load in the browser
116
files: [
127
'tests/test-core/browser.js'
138
],
149

15-
// list of preprocessors
1610
preprocessors: {
1711
'tests/test-core/*': ['webpack']
1812
},
1913

2014
webpack: {
2115
resolve: {
22-
extensions: ['', '.js']
16+
extensions: ['', '.js', '.json']
2317
},
2418
externals: {
2519
fs: '{}'
2620
},
2721
node: {
2822
Buffer: true
23+
},
24+
module: {
25+
loaders: [
26+
{ test: /\.json$/, loader: 'json' }
27+
]
2928
}
3029
},
3130

@@ -35,39 +34,13 @@ module.exports = function (config) {
3534
colors: true
3635
}
3736
},
38-
39-
// test results reporter to use
40-
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
4137
reporters: ['spec'],
42-
43-
// web server port
4438
port: 9876,
45-
46-
// enable / disable colors in the output (reporters and logs)
4739
colors: true,
48-
49-
// level of logging
50-
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
5140
logLevel: config.LOG_INFO,
52-
53-
// enable / disable watching file and executing tests whenever any file changes
5441
autoWatch: false,
55-
56-
// Start these browsers, currently available:
57-
// - Chrome
58-
// - ChromeCanary
59-
// - Firefox
60-
// - Opera (has to be installed with `npm install karma-opera-launcher`)
61-
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
62-
// - PhantomJS
63-
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
6442
browsers: process.env.TRAVIS ? ['Firefox'] : ['Chrome'],
65-
66-
// If browser does not capture in given timeout [ms], kill it
6743
captureTimeout: 60000,
68-
69-
// Continuous Integration mode
70-
// if true, it capture browsers, run tests and exit
7144
singleRun: true
7245
})
7346
}

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"scripts": {
1010
"lint": "standard",
1111
"coverage": "istanbul cover --print both -- _mocha tests/test-*/index.js",
12-
"test": "npm run test:node && npm run test:browser",
12+
"test": "npm run test:node",
1313
"test:node": "mocha tests/test-*/index.js",
1414
"test:browser": "karma start karma.conf.js",
1515
"test:core": "mocha tests/test-core/index.js",
@@ -35,17 +35,20 @@
3535
"homepage": "https://github.com/ipfs/js-ipfs#readme",
3636
"devDependencies": {
3737
"async": "^1.5.2",
38+
"binary-loader": "0.0.1",
39+
"brfs": "^1.4.3",
3840
"chai": "^3.4.1",
3941
"fs-blob-store": "^5.2.1",
4042
"istanbul": "^0.4.1",
43+
"json-loader": "^0.5.4",
4144
"karma": "^0.13.19",
4245
"karma-chrome-launcher": "^0.2.2",
4346
"karma-cli": "^0.1.2",
4447
"karma-firefox-launcher": "^0.1.7",
4548
"karma-mocha": "^0.2.1",
4649
"karma-spec-reporter": "0.0.23",
4750
"karma-webpack": "^1.7.0",
48-
"local-storage-blob-store": "0.0.2",
51+
"local-storage-blob-store": "0.0.3",
4952
"lodash": "^4.0.0",
5053
"mocha": "^2.3.4",
5154
"ncp": "^2.0.0",
@@ -54,13 +57,17 @@
5457
"raw-loader": "^0.5.1",
5558
"rimraf": "^2.4.4",
5659
"standard": "^5.4.1",
57-
"webpack": "^1.12.11"
60+
"transform-loader": "^0.2.3",
61+
"webpack": "diasdavid/webpack#81f5994"
5862
},
5963
"dependencies": {
64+
"bl": "^1.0.0",
6065
"boom": "^3.1.1",
66+
"bs58": "^3.0.0",
6167
"debug": "^2.2.0",
6268
"hapi": "^12.0.0",
6369
"ipfs-repo": "^0.4.1",
70+
"ipfs-merkle-dag": "vijayee/js-ipfs-merkle-dag",
6471
"lodash.get": "^4.0.0",
6572
"lodash.set": "^4.0.0",
6673
"ronin": "^0.3.11"

src/http-api/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ exports.start = callback => {
2222
})
2323

2424
server.connection({
25-
port: 9000
25+
port: 9001
2626
})
2727

2828
// load routes

src/ipfs-core/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict'
22

33
const defaultRepo = require('./default-repo')
4+
// const bl = require('bl')
5+
const MerkleDAG = require('ipfs-merkle-dag')
6+
const BlockService = MerkleDAG.BlockService
7+
// const Block = MerkleDAG.Block
48

59
exports = module.exports = IPFS
610

@@ -12,6 +16,7 @@ function IPFS (repo) {
1216
if (!repo) {
1317
repo = defaultRepo()
1418
}
19+
const bs = new BlockService(repo)
1520

1621
this.daemon = callback => {
1722
// 1. read repo to get peer data
@@ -124,4 +129,27 @@ function IPFS (repo) {
124129
})
125130
}
126131
}
132+
133+
this.block = {
134+
get: (multihash, callback) => {
135+
bs.getBlock(multihash, callback)
136+
},
137+
put: (block, callback) => {
138+
bs.addBlock(block, callback)
139+
},
140+
del: (multihash, callback) => {
141+
bs.deleteBlock(multihash, callback)
142+
},
143+
stat: (multihash, callback) => {
144+
bs.getBlock(multihash, (err, block) => {
145+
if (err) {
146+
return callback(err)
147+
}
148+
callback(null, {
149+
Key: multihash,
150+
Size: block.data.length
151+
})
152+
})
153+
}
154+
}
127155
}

tests/test-core/browser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const async = require('async')
55
const store = require('local-storage-blob-store')
66
const _ = require('lodash')
77

8-
var repoContext = require.context('raw!../repo-example', true)
8+
var repoContext = require.context('binary!../repo-example', true)
99

1010
describe('core', function () {
1111
before(function (done) {
@@ -15,7 +15,7 @@ describe('core', function () {
1515
repoContext.keys().forEach(function (key) {
1616
repoData.push({
1717
key: key.replace('./', ''),
18-
value: repoContext(key)
18+
value: new Buffer(require('binary!./../repo-example/' + key.replace('./', '')), 'binary')
1919
})
2020
})
2121

tests/test-core/test-block.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* globals describe, it */
2+
3+
'use strict'
4+
5+
const expect = require('chai').expect
6+
const base58 = require('bs58')
7+
const fs = require('fs')
8+
const IPFS = require('../../src/ipfs-core')
9+
const Block = require('ipfs-merkle-dag').Block
10+
11+
const isNode = !global.window
12+
13+
const fileA = isNode
14+
? fs.readFileSync(process.cwd() + '/tests/repo-example/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data')
15+
: new Buffer(require('binary!./../repo-example/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data'), 'binary')
16+
17+
// console.log('=>', fileA)
18+
// console.log('=>', fileA.length)
19+
20+
describe('block', () => {
21+
var ipfs
22+
23+
it('get', done => {
24+
ipfs = new IPFS()
25+
const b58mh = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe'
26+
const mh = new Buffer(base58.decode(b58mh))
27+
ipfs.block.get(mh, (err, block) => {
28+
expect(err).to.not.exist
29+
const eq = fileA.equals(block.data)
30+
expect(eq).to.equal(true)
31+
done()
32+
})
33+
})
34+
it('put', done => {
35+
var b = new Block('random data')
36+
ipfs.block.put(b, function (err) {
37+
expect(err).to.not.exist
38+
ipfs.block.get(b.key, function (err, block) {
39+
expect(err).to.not.exist
40+
expect(b.data.equals(block.data)).to.equal(true)
41+
expect(b.key.equals(block.key)).to.equal(true)
42+
done()
43+
})
44+
})
45+
})
46+
47+
it('rm', done => {
48+
var b = new Block('I will not last long enough')
49+
ipfs.block.put(b, function (err) {
50+
expect(err).to.not.exist
51+
ipfs.block.get(b.key, function (err, block) {
52+
expect(err).to.not.exist
53+
ipfs.block.del(b.key, function (err) {
54+
expect(err).to.not.exist
55+
ipfs.block.get(b.key, function (err, block) {
56+
expect(err).to.exist
57+
done()
58+
})
59+
})
60+
})
61+
})
62+
})
63+
64+
it('stat', done => {
65+
const mh = new Buffer(base58
66+
.decode('QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe'))
67+
ipfs.block.stat(mh, (err, stats) => {
68+
expect(err).to.not.exist
69+
expect(stats.Key.equals(mh)).to.equal(true)
70+
expect(stats.Size).to.equal(309)
71+
done()
72+
})
73+
})
74+
})

0 commit comments

Comments
 (0)