Skip to content

Commit 04ee50a

Browse files
unify coding style to use class
1 parent 219cf5d commit 04ee50a

14 files changed

+614
-527
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,25 @@
3636
},
3737
"dependencies": {
3838
"bs58": "^3.0.0",
39-
"detect-node": "^2.0.3",
4039
"ipfs-block": "^0.3.0",
4140
"is-ipfs": "^0.2.0",
41+
"multihashes": "^0.2.2",
4242
"multihashing": "^0.2.0",
4343
"protocol-buffers": "^3.1.4",
4444
"stable": "^0.1.5"
4545
},
4646
"devDependencies": {
4747
"aegir": "^3.0.1",
48-
"async": "^1.5.2",
4948
"buffer-loader": "0.0.1",
5049
"chai": "^3.5.0",
5150
"fs-blob-store": "^5.2.1",
5251
"idb-plus-blob-store": "^1.0.0",
53-
"ipfs-block-service": "^0.3.0",
54-
"ipfs-repo": "^0.7.4",
52+
"ipfs-block-service": "^0.4.0",
53+
"ipfs-repo": "^0.8.0",
5554
"lodash": "^4.6.1",
5655
"ncp": "^2.0.0",
5756
"pre-commit": "^1.1.2",
58-
"rimraf": "^2.5.0"
57+
"rimraf": "^2.5.0",
58+
"run-series": "^1.1.4"
5959
}
60-
}
60+
}

src/dag-link.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict'
2+
3+
const mh = require('multihashes')
4+
5+
// Link represents an IPFS Merkle DAG Link between Nodes.
6+
module.exports = class DAGLink {
7+
constructor (name, size, hash) {
8+
this.name = name
9+
this.size = size
10+
11+
if (typeof hash === 'string') {
12+
this.hash = mh.fromB58String(hash)
13+
} else if (Buffer.isBuffer(hash)) {
14+
this.hash = hash
15+
}
16+
}
17+
18+
toJSON () {
19+
return {
20+
Name: this.name,
21+
Size: this.size,
22+
Hash: mh.toB58String(this.hash)
23+
}
24+
}
25+
26+
toString () {
27+
const hash = mh.toB58String(this.hash)
28+
return `DAGLink <${hash} - name: "${this.name}", size: ${this.size}>`
29+
}
30+
}

src/dag-node.js

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
'use strict'
22

3-
const util = require('./util')
43
const protobuf = require('protocol-buffers')
54
const stable = require('stable')
6-
const bs58 = require('bs58')
5+
const fs = require('fs')
6+
const path = require('path')
7+
const mh = require('multihashes')
78

8-
var schema = 'message PBLink {optional bytes Hash = 1; optional string Name = 2;optional uint64 Tsize = 3;} message PBNode {repeated PBLink Links = 2; optional bytes Data = 1;}'
9+
const util = require('./util')
10+
const DAGLink = require('./dag-link')
911

10-
var mdagpb = protobuf(schema)
12+
const proto = protobuf(fs.readFileSync(path.join(__dirname, 'merkledag.proto')))
1113

1214
function linkSort (a, b) {
1315
return (new Buffer(a.name, 'ascii').compare(new Buffer(b.name, 'ascii')))
1416
}
1517

1618
// Helper method to get a protobuf object equivalent
1719
function toProtoBuf (node) {
18-
var pbn = {}
20+
const pbn = {}
1921

2022
if (node.data && node.data.length > 0) {
2123
pbn.Data = node.data
@@ -24,24 +26,21 @@ function toProtoBuf (node) {
2426
}
2527

2628
if (node.links.length > 0) {
27-
pbn.Links = []
28-
29-
for (var i = 0; i < node.links.length; i++) {
30-
var link = node.links[i]
31-
pbn.Links.push({
29+
pbn.Links = node.links.map((link) => {
30+
return {
3231
Hash: link.hash,
3332
Name: link.name,
3433
Tsize: link.size
35-
})
36-
}
34+
}
35+
})
3736
} else {
3837
pbn.Links = null
3938
}
4039

4140
return pbn
4241
}
4342

44-
class DAGNode {
43+
module.exports = class DAGNode {
4544
constructor (data, links) {
4645
this._cached = null
4746
this._encoded = null
@@ -65,9 +64,9 @@ class DAGNode {
6564

6665
// copy - returns a clone of the DAGNode
6766
copy () {
68-
var clone = new DAGNode()
67+
const clone = new DAGNode()
6968
if (this.data && this.data.length > 0) {
70-
var buf = new Buffer(this.data.length)
69+
const buf = new Buffer(this.data.length)
7170
this.data.copy(buf)
7271
clone.data = buf
7372
}
@@ -84,7 +83,7 @@ class DAGNode {
8483
if (typeof name !== 'string') {
8584
return
8685
}
87-
var link = this.makeLink(node)
86+
const link = this.makeLink(node)
8887

8988
link.name = name
9089
this.addRawLink(link)
@@ -101,7 +100,7 @@ class DAGNode {
101100
// that. If a link of the same name existed, it is replaced.
102101
// TODO this would make more sense as an utility
103102
updateNodeLink (name, node) {
104-
var newnode = this.copy()
103+
const newnode = this.copy()
105104
newnode.removeNodeLink(name)
106105
newnode.addNodeLink(name, node)
107106
return newnode
@@ -134,9 +133,7 @@ class DAGNode {
134133
// makeLink returns a DAGLink node from a DAGNode
135134
// TODO: this would make more sense as an utility
136135
makeLink (node) {
137-
var size = node.size()
138-
var mh = node.multihash()
139-
return new DAGLink(null, size, mh)
136+
return new DAGLink(null, node.size(), node.multihash())
140137
}
141138

142139
// multihash - returns the multihash value of this DAGNode
@@ -148,15 +145,12 @@ class DAGNode {
148145
// Size returns the total size of the data addressed by node,
149146
// including the total sizes of references.
150147
size () {
151-
var buf = this.encoded()
148+
const buf = this.encoded()
152149
if (!buf) {
153150
return 0
154151
}
155-
var size = buf.length
156-
for (var i = 0; i < this.links.length; i++) {
157-
size += this.links[i].size
158-
}
159-
return size
152+
153+
return this.links.reduce((sum, l) => sum + l.size, buf.length)
160154
}
161155

162156
// Encoded returns the encoded raw data version of a Node instance.
@@ -174,21 +168,17 @@ class DAGNode {
174168

175169
// marshal - encodes the DAGNode into a probuf
176170
marshal () {
177-
var pbn = toProtoBuf(this)
178-
var data = mdagpb.PBNode.encode(pbn)
179-
return data
171+
return proto.PBNode.encode(toProtoBuf(this))
180172
}
181173

182174
// unMarshal - decodes a protobuf into a DAGNode
183175
// TODO: this would make more sense as an utility
184176
unMarshal (data) {
185-
var pbn = mdagpb.PBNode.decode(data)
186-
this.links = []
187-
for (var i = 0; i < pbn.Links.length; i++) {
188-
var link = pbn.Links[i]
189-
var lnk = new DAGLink(link.Name, link.Tsize, link.Hash)
190-
this.links.push(lnk)
191-
}
177+
const pbn = proto.PBNode.decode(data)
178+
this.links = pbn.Links.map((link) => {
179+
return new DAGLink(link.Name, link.Tsize, link.Hash)
180+
})
181+
192182
stable.inplace(this.links, linkSort)
193183
this.data = pbn.Data || new Buffer(0)
194184
return this
@@ -197,34 +187,14 @@ class DAGNode {
197187
toJSON () {
198188
return {
199189
Data: this.data,
200-
Links: this.links.map((l) => { return l.toJSON() }),
201-
Hash: bs58.encode(this.multihash()).toString(),
190+
Links: this.links.map((l) => l.toJSON()),
191+
Hash: mh.toB58String(this.multihash()),
202192
Size: this.size()
203193
}
204194
}
205-
}
206195

207-
// Link represents an IPFS Merkle DAG Link between Nodes.
208-
function DAGLink (name, size, hash) {
209-
if (typeof hash === 'string') {
210-
this.hash = new Buffer(bs58.decode(hash))
211-
} else {
212-
this.hash = hash
196+
toString () {
197+
const hash = mh.toB58String(this.multihash())
198+
return `DAGNode <${hash} - data: "${this.data.toString()}", links: ${this.links.length}, size: ${this.size()}>`
213199
}
214-
215-
this.name = name
216-
this.size = size
217-
218-
this.toJSON = () => {
219-
return {
220-
Name: this.name,
221-
Size: this.size,
222-
Hash: bs58.encode(this.hash).toString()
223-
}
224-
}
225-
}
226-
227-
exports = module.exports = {
228-
DAGLink: DAGLink,
229-
DAGNode: DAGNode
230200
}

src/dag-service-batch.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
11
'use strict'
22

3-
var Block = require('ipfs-block')
4-
5-
exports = module.exports = Batch
3+
const Block = require('ipfs-block')
64

75
// Batch is to defer writes
8-
function Batch (ds, max) {
9-
if (!ds) {
10-
throw Error('Invalid DAG Service')
6+
module.exports = class Batch {
7+
constructor (ds, max) {
8+
if (!ds) {
9+
throw Error('Invalid DAG Service')
10+
}
11+
12+
this.dagService = ds
13+
this.blocks = []
14+
this.size = 0
15+
this.maxSize = max || 0
1116
}
12-
this.dagService = ds
13-
this.blocks = []
14-
this.size = 0
15-
this.maxSize = max || 0
1617

17-
this.add = (node, cb) => {
18+
add (node, cb) {
1819
if (!node) {
1920
return cb('Node is invalid')
2021
}
2122

22-
var data = node.encoded()
23+
const data = node.encoded()
2324

2425
if (!data) {
2526
return cb('Node is unencoded')
2627
}
28+
2729
this.size += data.length
28-
var block = new Block(data)
30+
const block = new Block(data)
2931
this.blocks.push(block)
32+
3033
if (this.size > this.maxSize) {
3134
this.commit(cb, block.key)
3235
} else {
3336
cb(null, block.key)
3437
}
3538
}
36-
this.commit = (cb, key) => {
37-
var self = this
38-
this.dagService.blocks().addBlocks(this.blocks, function (err) {
39+
40+
commit (cb, key) {
41+
this.dagService.blocks().addBlocks(this.blocks, (err) => {
3942
if (err) {
4043
return cb(err)
4144
}
42-
self.blocks = []
43-
self.size = 0
45+
46+
this.blocks = []
47+
this.size = 0
4448
cb(null, key)
4549
})
4650
}

0 commit comments

Comments
 (0)