Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 029174d

Browse files
committed
feat: remove DAGNode.create()
BREAKING CHANGE: DAGNode.create() is removed Instead of `DAGNode.create()`, please use `new DAGNode()` instead. It takes the same parameters and is compatible to `create()`. Example: Prior to this change: const node = DAGNode.create('some data', links) Now: const node = new DAGNode('some data', links) Closes #132.
1 parent d5e1135 commit 029174d

File tree

11 files changed

+159
-138
lines changed

11 files changed

+159
-138
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
- [Add and remove a Link](#add-and-remove-a-link)
2828
- [API](#api)
2929
- [DAGNode functions](#dagnode-functions)
30-
- [DAGNode.create(data, links)](#dagnodecreatedata-links)
30+
- [DAGNode constructor](#dagnode-constructor)
3131
- [addLink(node, link)](#addlinknode-link)
3232
- [rmLink(node, nameOrCid)](#rmlinknode-nameorcid)
3333
- [DAGNode instance methods and properties](#dagnode-instance-methods-and-properties)
@@ -65,7 +65,6 @@
6565
```JavaScript
6666
const dagPB = require('ipld-dag-pb')
6767

68-
dagPB.DAGNode.create // create a DAGNode
6968
dagPB.DAGNode.addLink // add a Link to a DAGNode, creating a new one
7069
dagPB.DAGNode.rmLink // remove a Link to a DAGNode, creating a new one
7170

@@ -79,10 +78,10 @@ dagPB.util
7978
#### Create a DAGNode
8079

8180
```JavaScript
82-
const node1 = DAGNode.create(Buffer.from('some data'))
81+
const node1 = new DAGNode(Buffer.from('some data'))
8382

8483
// node2 will have the same data as node1
85-
const node2 = DAGNode.create('some data')
84+
const node2 = new DAGNode('some data')
8685
```
8786

8887
#### Add and remove a Link
@@ -114,15 +113,16 @@ const dagPB = require('ipld-dag-pb')
114113
const DAGNode = dagPB.DAGNode
115114
```
116115

117-
#### DAGNode.create(data, links)
116+
#### DAGNode constructor
118117

119118
- `data` - type: Buffer
120-
- `links`- type: Array of DAGLink instances or Array of DAGLink instances in its json format (link.toJSON)
119+
- `links`- (optional) type: Array of DAGLink instances or Array of DAGLink instances in its json format (link.toJSON)
120+
- `serializedSize`- (optional) type: Number of bytes the serialized node has. If none is given, it will automatically be calculated.
121121

122122
Create a DAGNode.
123123

124124
```JavaScript
125-
const dagNode = DAGNode.create('data', links)
125+
const dagNode = new DAGNode('data', links)
126126
```
127127

128128
links can be a single or an array of DAGLinks instances or objects with the following pattern

src/dag-node/addLink.js

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

3-
const sort = require('stable')
4-
const { linkSort, toDAGLink } = require('./util')
3+
const sortLinks = require('./sortLinks')
4+
const toDAGLink = require('./toDagLink')
55
const DAGLink = require('../dag-link')
66
const DAGNode = require('./index')
77

@@ -27,7 +27,7 @@ const asDAGLink = async (link) => {
2727
const addLink = async (node, link) => {
2828
const dagLink = await asDAGLink(link)
2929
node._links.push(dagLink)
30-
node._links = sort(node._links, linkSort)
30+
node._links = sortLinks(node._links)
3131
}
3232

3333
module.exports = addLink

src/dag-node/create.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/dag-node/dagNode.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
'use strict'
22

3-
const assert = require('assert')
43
const withIs = require('class-is')
4+
const sortLinks = require('./sortLinks')
55
const visibility = require('../visibility')
6+
const DAGLink = require('../dag-link/dagLink')
7+
const { serializeDAGNode } = require('../serialize.js')
68

79
class DAGNode {
8-
constructor (data, links, serializedSize) {
9-
if (serializedSize !== 0) {
10-
assert(serializedSize, 'A DAGNode requires it\'s serialized size')
10+
constructor (data, links = [], serializedSize = 0) {
11+
if (!data) {
12+
data = Buffer.alloc(0)
13+
}
14+
if (typeof data === 'string') {
15+
data = Buffer.from(data)
16+
}
17+
if (!Buffer.isBuffer(data)) {
18+
throw new Error('Passed \'data\' is not a buffer or a string!')
19+
}
20+
21+
links = links.map((link) => {
22+
return DAGLink.isDAGLink(link)
23+
? link
24+
: DAGLink.util.createDagLinkFromB58EncodedHash(link)
25+
})
26+
links = sortLinks(links)
27+
28+
if (serializedSize === 0) {
29+
serializedSize = serializeDAGNode({
30+
Data: data,
31+
Links: links
32+
}).length
1133
}
1234

13-
this._data = data || Buffer.alloc(0)
35+
this._data = data
1436
this._links = links
1537
this._serializedSize = serializedSize
1638

src/dag-node/index.js

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

33
exports = module.exports = require('./dagNode')
4-
exports.create = require('./create')
54
exports.addLink = require('./addLink')
65
exports.rmLink = require('./rmLink')

src/dag-node/sortLinks.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
const sort = require('stable')
4+
5+
const linkSort = (a, b) => {
6+
return Buffer.compare(a.nameAsBuffer, b.nameAsBuffer)
7+
}
8+
9+
const sortLinks = (links) => {
10+
return sort(links, linkSort)
11+
}
12+
13+
module.exports = sortLinks
Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
'use strict'
22

33
const DAGLink = require('./../dag-link/dagLink')
4-
const {
5-
cid,
6-
serialize
7-
} = require('../util')
8-
9-
exports = module.exports
10-
11-
function linkSort (a, b) {
12-
return Buffer.compare(a.nameAsBuffer, b.nameAsBuffer)
13-
}
4+
const { cid, serialize } = require('../util')
145

156
/*
167
* toDAGLink converts a DAGNode to a DAGLink
@@ -21,5 +12,4 @@ const toDAGLink = async (node, options = {}) => {
2112
return new DAGLink(options.name || '', serialized.length, nodeCid)
2213
}
2314

24-
exports.linkSort = linkSort
25-
exports.toDAGLink = toDAGLink
15+
module.exports = toDAGLink

src/serialize.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict'
2+
3+
const protons = require('protons')
4+
const proto = protons(require('./dag.proto.js'))
5+
const DAGLink = require('./dag-link/dagLink')
6+
7+
exports = module.exports
8+
9+
const toProtoBuf = (node) => {
10+
const pbn = {}
11+
12+
if (node.Data && node.Data.length > 0) {
13+
pbn.Data = node.Data
14+
} else {
15+
// NOTE: this has to be null in order to match go-ipfs serialization
16+
// `null !== new Buffer(0)`
17+
pbn.Data = null
18+
}
19+
20+
if (node.Links && node.Links.length > 0) {
21+
pbn.Links = node.Links
22+
.map((link) => ({
23+
Hash: link.Hash.buffer,
24+
Name: link.Name,
25+
Tsize: link.Tsize
26+
}))
27+
} else {
28+
pbn.Links = null
29+
}
30+
31+
return pbn
32+
}
33+
34+
/**
35+
* Serialize internal representation into a binary PB block.
36+
*
37+
* @param {Object} node - Internal representation of a CBOR block
38+
* @returns {Buffer} - The encoded binary representation
39+
*/
40+
const serializeDAGNode = (node) => {
41+
const data = node.Data
42+
const links = node.Links || []
43+
44+
const serialized = proto.PBNode.encode(toProtoBuf({
45+
Data: data,
46+
Links: links
47+
}))
48+
49+
return serialized
50+
}
51+
52+
// Serialize an object where the `Links` might not be a `DAGLink` instance yet
53+
const serializeDAGNodeLike = (data, links = []) => {
54+
const node = { Data: data }
55+
node.Links = links.map((link) => {
56+
return DAGLink.isDAGLink(link)
57+
? link
58+
: DAGLink.util.createDagLinkFromB58EncodedHash(link)
59+
})
60+
return serializeDAGNode(node)
61+
}
62+
63+
exports.serializeDAGNode = serializeDAGNode
64+
exports.serializeDAGNodeLike = serializeDAGNodeLike

src/util.js

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const DAGLink = require('./dag-link/dagLink')
77
const DAGNode = require('./dag-node/dagNode')
88
const multicodec = require('multicodec')
99
const multihashing = require('multihashing-async')
10+
const { serializeDAGNode, serializeDAGNodeLike } = require('./serialize')
1011

1112
exports = module.exports
1213

@@ -40,22 +41,11 @@ const cid = async (binaryBlob, userOptions) => {
4041
* @returns {Buffer} - The encoded binary representation
4142
*/
4243
const serialize = (node) => {
43-
const data = node.Data
44-
let links = node.Links || []
45-
46-
// If the node is not an instance of a DAGNode, the link.hash might be a Base58 encoded string; decode it
47-
if (!DAGNode.isDAGNode(node) && links) {
48-
links = links.map((link) => {
49-
return DAGLink.isDAGLink(link) ? link : DAGLink.util.createDagLinkFromB58EncodedHash(link)
50-
})
44+
if (DAGNode.isDAGNode(node)) {
45+
return serializeDAGNode(node)
46+
} else {
47+
return serializeDAGNodeLike(node.Data, node.Links)
5148
}
52-
53-
const serialized = proto.PBNode.encode(toProtoBuf({
54-
Data: data,
55-
Links: links
56-
}))
57-
58-
return serialized
5949
}
6050

6151
/**
@@ -76,30 +66,6 @@ const deserialize = (buffer) => {
7666
return new DAGNode(data, links, buffer.length)
7767
}
7868

79-
function toProtoBuf (node) {
80-
const pbn = {}
81-
82-
if (node.Data && node.Data.length > 0) {
83-
pbn.Data = node.Data
84-
} else {
85-
// NOTE: this has to be null in order to match go-ipfs serialization `null !== new Buffer(0)`
86-
pbn.Data = null
87-
}
88-
89-
if (node.Links && node.Links.length > 0) {
90-
pbn.Links = node.Links
91-
.map((link) => ({
92-
Hash: link.Hash.buffer,
93-
Name: link.Name,
94-
Tsize: link.Tsize
95-
}))
96-
} else {
97-
pbn.Links = null
98-
}
99-
100-
return pbn
101-
}
102-
10369
exports.serialize = serialize
10470
exports.deserialize = deserialize
10571
exports.cid = cid

0 commit comments

Comments
 (0)