This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
11 changed files
with
159 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
'use strict' | ||
|
||
exports = module.exports = require('./dagNode') | ||
exports.create = require('./create') | ||
exports.addLink = require('./addLink') | ||
exports.rmLink = require('./rmLink') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict' | ||
|
||
const sort = require('stable') | ||
|
||
const linkSort = (a, b) => { | ||
return Buffer.compare(a.nameAsBuffer, b.nameAsBuffer) | ||
} | ||
|
||
const sortLinks = (links) => { | ||
return sort(links, linkSort) | ||
} | ||
|
||
module.exports = sortLinks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict' | ||
|
||
const protons = require('protons') | ||
const proto = protons(require('./dag.proto.js')) | ||
const DAGLink = require('./dag-link/dagLink') | ||
|
||
exports = module.exports | ||
|
||
const toProtoBuf = (node) => { | ||
const pbn = {} | ||
|
||
if (node.Data && node.Data.length > 0) { | ||
pbn.Data = node.Data | ||
} else { | ||
// NOTE: this has to be null in order to match go-ipfs serialization | ||
// `null !== new Buffer(0)` | ||
pbn.Data = null | ||
} | ||
|
||
if (node.Links && node.Links.length > 0) { | ||
pbn.Links = node.Links | ||
.map((link) => ({ | ||
Hash: link.Hash.buffer, | ||
Name: link.Name, | ||
Tsize: link.Tsize | ||
})) | ||
} else { | ||
pbn.Links = null | ||
} | ||
|
||
return pbn | ||
} | ||
|
||
/** | ||
* Serialize internal representation into a binary PB block. | ||
* | ||
* @param {Object} node - Internal representation of a CBOR block | ||
* @returns {Buffer} - The encoded binary representation | ||
*/ | ||
const serializeDAGNode = (node) => { | ||
const data = node.Data | ||
const links = node.Links || [] | ||
|
||
const serialized = proto.PBNode.encode(toProtoBuf({ | ||
Data: data, | ||
Links: links | ||
})) | ||
|
||
return serialized | ||
} | ||
|
||
// Serialize an object where the `Links` might not be a `DAGLink` instance yet | ||
const serializeDAGNodeLike = (data, links = []) => { | ||
const node = { Data: data } | ||
node.Links = links.map((link) => { | ||
return DAGLink.isDAGLink(link) | ||
? link | ||
: DAGLink.util.createDagLinkFromB58EncodedHash(link) | ||
}) | ||
return serializeDAGNode(node) | ||
} | ||
|
||
exports.serializeDAGNode = serializeDAGNode | ||
exports.serializeDAGNodeLike = serializeDAGNodeLike |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.