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

Backwards compatible pure data model API #184

Merged
merged 8 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,11 @@
"class-is": "^1.1.0",
"multicodec": "^1.0.1",
"multihashing-async": "~0.8.1",
"protons": "^1.0.2",
"protons": "^1.2.1",
"stable": "^0.1.8"
},
"devDependencies": {
"aegir": "^22.0.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"fs-extra": "^9.0.0",
"ipfs-block-service": "~0.17.0",
"ipfs-repo": "^3.0.0",
Expand All @@ -84,4 +82,4 @@
"multibase": "^0.7.0",
"multihashes": "~0.4.19"
}
}
}
38 changes: 8 additions & 30 deletions src/dag-link/dagLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ class DAGLink {
// note - links should include size, but this assert is disabled
// for now to maintain consistency with go-ipfs pinset

this._name = name || ''
this._nameBuf = null
this._size = size
this._cid = new CID(cid)
Object.defineProperties(this, {
Name: { value: name || '', writable: false, enumerable: true },
Tsize: { value: size, writable: false, enumerable: true },
Hash: { value: new CID(cid), writable: false, enumerable: true },
_nameBuf: { value: null, writable: true, enumerable: false }
})
}

toString () {
return `DAGLink <${this._cid.toBaseEncodedString()} - name: "${this.Name}", size: ${this.Tsize}>`
return `DAGLink <${this.Hash.toBaseEncodedString()} - name: "${this.Name}", size: ${this.Tsize}>`
}

toJSON () {
Expand All @@ -37,10 +39,6 @@ class DAGLink {
return Object.assign({}, this._json)
}

get Name () {
return this._name
}

// Memoize the Buffer representation of name
// We need this to sort the links, otherwise
// we will reallocate new buffers every time
Expand All @@ -49,29 +47,9 @@ class DAGLink {
return this._nameBuf
}

this._nameBuf = Buffer.from(this._name)
this._nameBuf = Buffer.from(this.Name)
return this._nameBuf
}

set Name (name) {
throw new Error("Can't set property: 'name' is immutable")
}

get Tsize () {
return this._size
}

set Tsize (size) {
throw new Error("Can't set property: 'size' is immutable")
}

get Hash () {
return this._cid
}

set Hash (cid) {
throw new Error("Can't set property: 'cid' is immutable")
}
}

exports = module.exports = withIs(DAGLink, { className: 'DAGLink', symbolName: '@ipld/js-ipld-dag-pb/daglink' })
4 changes: 2 additions & 2 deletions src/dag-node/addLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const asDAGLink = (link) => {

const addLink = (node, link) => {
const dagLink = asDAGLink(link)
node._links.push(dagLink)
node._links = sortLinks(node._links)
node.Links.push(dagLink)
sortLinks(node.Links)
}

module.exports = addLink
44 changes: 10 additions & 34 deletions src/dag-node/dagNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,21 @@ class DAGNode {
? link
: DAGLink.util.createDagLinkFromB58EncodedHash(link)
})
links = sortLinks(links)
sortLinks(links)

this._data = data
this._links = links
this._serializedSize = serializedSize
this._size = null
Object.defineProperties(this, {
Data: { value: data, writable: false, enumerable: true },
Links: { value: links, writable: false, enumerable: true },
_serializedSize: { value: serializedSize, writable: true, enumerable: false },
_size: { value: null, writable: true, enumerable: false }
})
}

toJSON () {
if (!this._json) {
this._json = Object.freeze({
data: this.Data,
links: this._links.map((l) => l.toJSON()),
links: this.Links.map((l) => l.toJSON()),
size: this.size
})
}
Expand Down Expand Up @@ -75,18 +77,15 @@ class DAGNode {
}

serialize () {
return serializeDAGNode({
Data: this._data,
Links: this._links
})
return serializeDAGNode(this)
}

get size () {
if (this._size === null) {
if (this._serializedSize === null) {
this._serializedSize = this.serialize().length
}
this._size = this._links.reduce((sum, l) => sum + l.Tsize, this._serializedSize)
this._size = this.Links.reduce((sum, l) => sum + l.Tsize, this._serializedSize)
}

return this._size
Expand All @@ -95,29 +94,6 @@ class DAGNode {
set size (size) {
throw new Error("Can't set property: 'size' is immutable")
}

// Getters for backwards compatible path resolving
get Data () {
return this._data
}

set Data (_) {
throw new Error("Can't set property: 'Data' is immutable")
}

get Links () {
return this._links.map((link) => {
return {
Name: link.Name,
Tsize: link.Tsize,
Hash: link.Hash
}
})
}

set Links (_) {
throw new Error("Can't set property: 'Links' is immutable")
}
}

exports = module.exports = withIs(DAGNode, { className: 'DAGNode', symbolName: '@ipld/js-ipld-dag-pb/dagnode' })
20 changes: 17 additions & 3 deletions src/dag-node/rmLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ const CID = require('cids')
const { Buffer } = require('buffer')

const rmLink = (dagNode, nameOrCid) => {
let predicate = null

// It's a name
if (typeof nameOrCid === 'string') {
dagNode._links = dagNode._links.filter((link) => link.Name !== nameOrCid)
predicate = (link) => link.Name === nameOrCid
} else if (Buffer.isBuffer(nameOrCid) || CID.isCID(nameOrCid)) {
dagNode._links = dagNode._links.filter(
(link) => !link.Hash.equals(nameOrCid))
predicate = (link) => link.Hash.equals(nameOrCid)
}

if (predicate) {
const links = dagNode.Links
let index = 0
while (index < links.length) {
const link = links[index]
if (predicate(link)) {
links.splice(index, 1)
} else {
index++
}
}
} else {
throw new Error('second arg needs to be a name or CID')
}
Expand Down
6 changes: 3 additions & 3 deletions src/dag-node/sortLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const linkSort = (a, b) => {
}

/**
*
* Sorts links in place (mutating given array)
* @param {Array} links
* @returns {Array}
* @returns {void}
*/
const sortLinks = (links) => {
return sort(links, linkSort)
sort.inplace(links, linkSort)
}

module.exports = sortLinks
2 changes: 1 addition & 1 deletion src/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports = module.exports
const toProtoBuf = (node) => {
const pbn = {}

if (node.Data && node.Data.length > 0) {
if (node.Data && node.Data.byteLength > 0) {
pbn.Data = node.Data
} else {
// NOTE: this has to be null in order to match go-ipfs serialization
Expand Down
6 changes: 2 additions & 4 deletions test/dag-link-test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const chai = require('aegir/utils/chai')
const { Buffer } = require('buffer')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const CID = require('cids')
const DAGLink = require('../src').DAGLink

Expand Down Expand Up @@ -63,7 +61,7 @@ module.exports = (repo) => {

it('has an immutable CID', () => {
const link = new DAGLink('hello', 3, 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U')
expect(() => { link.Hash = 'foo' }).to.throw(/property/)
expect(() => { link.Hash = 'foo' }).to.throw(/read.only/)
})
})
}
6 changes: 2 additions & 4 deletions test/dag-node-test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const chai = require('aegir/utils/chai')
const { Buffer } = require('buffer')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const dagPB = require('../src')
const DAGLink = dagPB.DAGLink
Expand Down Expand Up @@ -80,7 +78,7 @@ module.exports = (repo) => {
})

const node2 = new DAGNode(someData, l2)
expect(node2.Links).to.eql([l1[1], l1[0]])
expect(node2.Links).to.containSubset([l1[1], l1[0]])
Gozala marked this conversation as resolved.
Show resolved Hide resolved
expect(node1.toJSON()).to.eql(node2.toJSON())

// check sorting
Expand Down
2 changes: 1 addition & 1 deletion test/mod.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const chai = require('aegir/utils/chai')
const expect = chai.expect
const multicodec = require('multicodec')

Expand Down
Loading