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

Commit 39cfef1

Browse files
achingbrainvmx
authored andcommitted
BREAKING CHANGE: Remove .cid, .multihash and .serialized properties (#99)
* feat: remove .cid, .multihash and .serialized properties Follows on from ipld/js-ipld#173 (comment) BREAKING CHANGE: These properties are removed from the DAGNode class. * `.multihash` is removed because they aren't multihashes any more * `.cid` is removed to bring dag-pb in line with other ipld types * `.serialized` is removed because storing data buffers and the serialized form uses too much memory - we can use the utils.serialize method to create the serialized form when we need it, which in this module is just during the tests `.multihash` has also changed to `.cid` in the output of `DAGLink.toJSON` and `DAGNode.toJSON` because since CIDv1 they are not just multihashes any more; the multihash is contained within the CID.
1 parent dafad86 commit 39cfef1

File tree

14 files changed

+182
-242
lines changed

14 files changed

+182
-242
lines changed

README.md

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,22 @@
2626
- [Add and remove a Link](#add-and-remove-a-link)
2727
- [API](#api)
2828
- [DAGNode functions](#dagnode-functions)
29-
- [DAGNode.create(data, links, hashAlg, callback)](#dagnodecreatedata-links-hashalg-callback)
29+
- [DAGNode.create(data, links, callback)](#dagnodecreatedata-links-callback)
3030
- [addLink(node, link, callback)](#addlinknode-link-callback)
31-
- [rmLink(node, nameOrMultihash, callback)](#rmlinknode-nameormultihash-callback)
31+
- [rmLink(node, nameOrCid, callback)](#rmlinknode-nameorcid-callback)
3232
- [clone(node, callback)](#clonenode-callback)
3333
- [DAGNode instance methods and properties](#dagnode-instance-methods-and-properties)
3434
- [`node.data`](#nodedata)
3535
- [`node.links`](#nodelinks)
3636
- [`node.size`](#nodesize)
37-
- [`node.multihash`](#nodemultihash)
38-
- [`node.serialized`](#nodeserialized)
3937
- [`node.toJSON()`](#nodetojson)
4038
- [`node.toString()`](#nodetostring)
4139
- [DAGLink functions](#daglink-functions)
42-
- [DAGLink.create(name, size, multihash, callback)](#daglinkcreatename-size-multihash-callback)
40+
- [DAGLink.create(name, size, cid, callback)](#daglinkcreatename-size-cid-callback)
4341
- [DAGLink instance methods and properties](#daglink-instance-methods-and-properties)
4442
- [`link.name`](#linkname)
4543
- [`link.size`](#linksize)
46-
- [`link.multihash`](#linkmultihash)
44+
- [`link.cid`](#linkcid)
4745
- [`link.toJSON()`](#linktojson)
4846
- [`link.toString()`](#linktostring)
4947
- [[IPLD Format Specifics](https://github.com/ipld/interface-ipld-format) - Local (node/block scope) resolver](#ipld-format-specificshttpsgithubcomipldinterface-ipld-format---local-nodeblock-scope-resolver)
@@ -101,7 +99,7 @@ DAGNode.create('some data', (err, node2) => {
10199
```JavaScript
102100
const link = {
103101
name: 'I am a link',
104-
multihash: 'QmHash..',
102+
cid: 'QmHash..',
105103
size: 42
106104
}
107105

@@ -136,11 +134,10 @@ const dagPB = require('ipld-dag-pb')
136134
const DAGNode = dagPB.DAGNode
137135
```
138136

139-
#### DAGNode.create(data, links, hashAlg, callback)
137+
#### DAGNode.create(data, links, callback)
140138

141139
- `data` - type: Buffer
142140
- `links`- type: Array of DAGLink instances or Array of DAGLink instances in its json format (link.toJSON)
143-
- `hashAlg` - type: String
144141
- `callback` - type: function with signature `function (err, node) {}`
145142

146143
Create a DAGNode.
@@ -156,7 +153,7 @@ links can be a single or an array of DAGLinks instances or objects with the foll
156153
```JavaScript
157154
{
158155
name: '<some name>',
159-
hash: '<some multihash>', // can also be `multihash: <some multihash>`
156+
cid: '<some cid>',
160157
size: <sizeInBytes>
161158
}
162159
```
@@ -167,7 +164,7 @@ links can be a single or an array of DAGLinks instances or objects with the foll
167164
- `link` - type: DAGLink or DAGLink in its json format
168165
- `callback` - type: function with signature `function (err, node) {}`
169166

170-
Creates a link on node A to node B by using node B to get its multihash. Returns a *new* instance of DAGNode without modifying the old one.
167+
Creates a link on node A to node B by using node B to get its CID. Returns a *new* instance of DAGNode without modifying the old one.
171168

172169
Creates a new DAGNode instance with the union of node.links plus the new link.
173170

@@ -180,21 +177,20 @@ Creates a new DAGNode instance with the union of node.links plus the new link.
180177
{
181178
name: '<some string>', // optional
182179
size: <size in bytes>,
183-
multihash: <multihash> // can be a String multihash or multihash buffer
180+
cid: <cid> // can be a String CID, CID buffer or CID object
184181
}
185182
```
186183

187-
188-
#### rmLink(node, nameOrMultihash, callback)
184+
#### rmLink(node, nameOrCid, callback)
189185

190186
- `node` - type: DAGNode
191-
- `nameOrMultihash` - type: String or multihash buffer
187+
- `nameOrCid` - type: String, CID object or CID buffer
192188
- `callback` - type: function with signature `function (err, node) {}`
193189

194190
Removes a link from the node by name. Returns a *new* instance of DAGNode without modifying the old one.
195191

196192
```JavaScript
197-
DAGNode.rmLink(node, 'Link1' (err, dagNode) => ...)
193+
DAGNode.rmLink(node, 'Link1' (err, dagNode) => ...)
198194
```
199195

200196
#### clone(node, callback)
@@ -222,18 +218,14 @@ An array of `DAGLinks`
222218

223219
Size of the node, in bytes
224220

225-
#### `node.multihash`
226-
227-
#### `node.serialized`
228-
229221
#### `node.toJSON()`
230222

231223
#### `node.toString()`
232224

233225

234226
### DAGLink functions
235227

236-
Following the same pattern as [`DAGNode functions`]() above, DAGLink also offers a function for its creation.
228+
Following the same pattern as [`DAGNode functions`]() above, DAGLink also offers a function for its creation.
237229

238230
You can incude it in your project with:
239231

@@ -242,13 +234,13 @@ const dagPB = require('ipld-dag-pb')
242234
const DAGLink = dagPB.DAGLink
243235
```
244236

245-
#### DAGLink.create(name, size, multihash, callback)
237+
#### DAGLink.create(name, size, cid, callback)
246238

247239
```JavaScript
248240
DAGLink.create(
249241
'link-to-file', // name of the link (can be empty)
250242
10, // size in bytes
251-
'QmSomeHash...', // can be multihash buffer or string
243+
'QmSomeHash...', // can be CID object, CID buffer or string
252244
(err, link) => {
253245
if (err) {
254246
throw err
@@ -260,7 +252,7 @@ DAGLink.create(
260252
Note: DAGLinks are simpler objects and can be instantiated directly:
261253

262254
```JavaScript
263-
const link = new DAGLink(name, size, multihash)
255+
const link = new DAGLink(name, size, cid)
264256
```
265257

266258
### DAGLink instance methods and properties
@@ -269,7 +261,7 @@ const link = new DAGLink(name, size, multihash)
269261

270262
#### `link.size`
271263

272-
#### `link.multihash`
264+
#### `link.cid`
273265

274266
#### `link.toJSON()`
275267

src/dag-link/create.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
const DAGLink = require('./index.js')
44

5-
function create (name, size, multihash, callback) {
6-
const link = new DAGLink(name, size, multihash)
5+
function create (name, size, cid, callback) {
6+
const link = new DAGLink(name, size, cid)
77
callback(null, link)
88
}
99

src/dag-link/index.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ const withIs = require('class-is')
66

77
// Link represents an IPFS Merkle DAG Link between Nodes.
88
class DAGLink {
9-
constructor (name, size, multihash) {
10-
assert(multihash, 'A link requires a multihash to point to')
9+
constructor (name, size, cid) {
10+
assert(cid, 'A link requires a cid to point to')
1111
// assert(size, 'A link requires a size')
1212
// note - links should include size, but this assert is disabled
1313
// for now to maintain consistency with go-ipfs pinset
1414

1515
this._name = name || ''
1616
this._size = size
17-
this._cid = new CID(multihash)
17+
this._cid = new CID(cid)
1818
}
1919

2020
toString () {
@@ -26,7 +26,7 @@ class DAGLink {
2626
this._json = Object.freeze({
2727
name: this.name,
2828
size: this.size,
29-
multihash: this._cid.toBaseEncodedString()
29+
cid: this._cid.toBaseEncodedString()
3030
})
3131
}
3232

@@ -49,14 +49,6 @@ class DAGLink {
4949
throw new Error("Can't set property: 'size' is immutable")
5050
}
5151

52-
get multihash () {
53-
return this._cid.buffer
54-
}
55-
56-
set multihash (multihash) {
57-
throw new Error("Can't set property: 'multihash' is immutable")
58-
}
59-
6052
get cid () {
6153
return this._cid
6254
}

src/dag-link/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function createDagLinkFromB58EncodedHash (link) {
66
return new DAGLink(
77
link.name ? link.name : link.Name,
88
link.size ? link.size : link.Size,
9-
link.hash || link.Hash || link.multihash
9+
link.hash || link.Hash || link.multihash || link.cid
1010
)
1111
}
1212

src/dag-node/addLink.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,40 @@ const DAGLink = require('../dag-link')
88
const DAGNode = require('./index')
99
const create = require('./create')
1010

11-
function addLink (node, link, callback) {
12-
const links = cloneLinks(node)
13-
const data = cloneData(node)
14-
11+
function asDAGLink (link, callback) {
1512
if (DAGLink.isDAGLink(link)) {
1613
// It's a DAGLink instance
1714
// no need to do anything
18-
} else if (DAGNode.isDAGNode(link)) {
15+
16+
return callback(null, link)
17+
}
18+
19+
if (DAGNode.isDAGNode(link)) {
1920
// It's a DAGNode instance
2021
// convert to link
21-
link = toDAGLink(link)
22-
} else {
23-
// It's a Object with name, multihash/link and size
24-
try {
25-
link = new DAGLink(link.name, link.size, link.multihash || link.hash)
26-
} catch (err) {
27-
return callback(err)
28-
}
22+
return toDAGLink(link, {}, callback)
2923
}
3024

31-
links.push(link)
32-
create(data, links, callback)
25+
// It's a Object with name, multihash/hash/cid and size
26+
try {
27+
callback(null, new DAGLink(link.name, link.size, link.multihash || link.hash || link.cid))
28+
} catch (err) {
29+
return callback(err)
30+
}
31+
}
32+
33+
function addLink (node, link, callback) {
34+
const links = cloneLinks(node)
35+
const data = cloneData(node)
36+
37+
asDAGLink(link, (error, link) => {
38+
if (error) {
39+
return callback(error)
40+
}
41+
42+
links.push(link)
43+
create(data, links, callback)
44+
})
3345
}
3446

3547
module.exports = addLink

src/dag-node/create.js

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,43 @@
11
'use strict'
22

3-
const multihashing = require('multihashing-async')
43
const sort = require('stable')
5-
const dagPBUtil = require('../util.js')
6-
const serialize = dagPBUtil.serialize
4+
const {
5+
serialize
6+
} = require('../util.js')
77
const dagNodeUtil = require('./util.js')
88
const linkSort = dagNodeUtil.linkSort
99
const DAGNode = require('./index.js')
1010
const DAGLink = require('../dag-link')
1111

12-
function create (data, dagLinks, hashAlg, callback) {
12+
function create (data, links, callback) {
1313
if (typeof data === 'function') {
1414
callback = data
1515
data = undefined
1616
} else if (typeof data === 'string') {
1717
data = Buffer.from(data)
1818
}
19-
if (typeof dagLinks === 'function') {
20-
callback = dagLinks
21-
dagLinks = []
22-
}
23-
if (typeof hashAlg === 'function') {
24-
callback = hashAlg
25-
hashAlg = undefined
19+
if (typeof links === 'function') {
20+
callback = links
21+
links = []
2622
}
2723

2824
if (!Buffer.isBuffer(data)) {
2925
return callback(new Error('Passed \'data\' is not a buffer or a string!'))
3026
}
3127

32-
if (!hashAlg) {
33-
hashAlg = 'sha2-256'
34-
}
35-
36-
const links = dagLinks.map((link) => {
28+
links = links.map((link) => {
3729
return DAGLink.isDAGLink(link) ? link : DAGLink.util.createDagLinkFromB58EncodedHash(link)
3830
})
39-
const sortedLinks = sort(links, linkSort)
31+
links = sort(links, linkSort)
4032

4133
serialize({
42-
data: data,
43-
links: sortedLinks
44-
}, (err, serialized) => {
34+
data, links
35+
}, (err, buffer) => {
4536
if (err) {
4637
return callback(err)
4738
}
48-
multihashing(serialized, hashAlg, (err, multihash) => {
49-
if (err) {
50-
return callback(err)
51-
}
52-
const dagNode = new DAGNode(data, sortedLinks, serialized, multihash)
53-
callback(null, dagNode)
54-
})
39+
40+
callback(null, new DAGNode(data, links, buffer.length))
5541
})
5642
}
5743

0 commit comments

Comments
 (0)