{"action":"created","comment":{"url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls/comments/88211258","pull_request_review_id":8790101,"id":88211258,"diff_hunk":"@@ -0,0 +1,210 @@\n+'use strict'\n+\n+const multihashing = require('multihashing-async')\n+const CID = require('cids')\n+const stable = require('stable')\n+const sort = stable.inplace\n+const protobuf = require('protocol-buffers')\n+const proto = protobuf(require('./dag.proto'))\n+const DAGNode = require('./dag-node')\n+const DAGLink = require('./dag-link')\n+\n+const hash = (type, data, cb) => multihashing(data, type, cb)\n+\n+const linkSort = (a, b) => {\n+  return (new Buffer(a.name || '', 'ascii').compare(new Buffer(b.name || '', 'ascii')))\n+}\n+\n+function cid (node, callback) {\n+  callback(null, new CID(node.multihash))\n+}\n+\n+function create (data, dagLinks, hashAlg, callback) {\n+  if (typeof data === 'function') {\n+    // empty obj\n+    callback = data\n+    data = undefined\n+  }\n+  if (typeof dagLinks === 'function') {\n+    // empty obj\n+    callback = dagLinks\n+    dagLinks = []\n+  }\n+  if (typeof hashAlg === 'function') {\n+    // empty obj\n+    callback = hashAlg\n+    hashAlg = undefined\n+  }\n+\n+  if (!hashAlg) {\n+    hashAlg = 'sha2-256'\n+  }\n+\n+  const links = dagLinks.map((l) => {\n+    if (!l.constructor && l.constructor.name !== 'DAGLink') {\n+      return l\n+    }\n+\n+    // haadcode: are the .name vs .Name for backwards compatibility?\n+    const link = new DAGLink(l.name || l.Name,\n+                             l.size || l.Size,\n+                             l.hash || l.Hash || l.multihash)\n+    return link\n+  })\n+\n+  // Sort the links (in-place)\n+  sort(links, linkSort)\n+\n+  serialize({\n+    data: data,\n+    links: links\n+  }, (err, serialized) => {\n+    if (err) {\n+      callback(err)\n+    }\n+    hash(hashAlg, serialized, (err, multihash) => {\n+      if (err) {\n+        callback(err)\n+      }\n+      const dagNode = new DAGNode(data, links, serialized, multihash)\n+      callback(null, dagNode)\n+    })\n+  })\n+}\n+\n+function addLink (dagNode, nameOrLink, nodeOrMultihash, callback) {\n+  const links = _cloneLinks(dagNode)\n+  const data = _cloneData(dagNode)\n+  let newLink = null\n+\n+  if ((nameOrLink.constructor &&\n+       nameOrLink.constructor.name === 'DAGLink')) {\n+    // It's a link\n+    newLink = nameOrLink\n+  } else if (typeof nameOrLink === 'string') {\n+    // It's a name\n+    if ((nodeOrMultihash.constructor &&\n+       nodeOrMultihash.constructor.name === 'DAGNode')) {\n+      // It's a node\n+      // haadcode: not sure what ^ means, so the line below might not be correct\n+      newLink = toDAGLink(nodeOrMultihash)\n+    } else {\n+      // It's a multihash\n+      // haadcode: not sure what ^ means, so the line below might not be correct\n+      newLink = new DAGLink(null, dagNode.size, nodeOrMultihash)\n+    }\n+  }\n+\n+  if (newLink) {\n+    links.push(newLink)\n+    sort(links, linkSort)\n+  } else {\n+    throw new Error('Link given as the argument is invalid')\n+  }\n+\n+  create(data, links, callback)\n+}\n+\n+function removeLink (dagNode, nameOrMultihash, callback) {\n+  let data = _cloneData(dagNode)\n+  let links = _cloneLinks(dagNode)\n+\n+  if (typeof nameOrMultihash === 'string') {\n+    links = dagNode.links.filter((link) => link.name !== nameOrMultihash)\n+  } else if (Buffer.isBuffer(nameOrMultihash)) {\n+    links = dagNode.links.filter((link) => !link.hash.equals(nameOrMultihash))\n+  } else {\n+    throw new Error('second arg needs to be a name or multihash')\n+  }\n+\n+  create(data, links, callback)\n+}\n+\n+/*\n+ * toDAGLink converts a DAGNode to a DAGLink\n+ */\n+// was: makeLink(node, callback)\n+function toDAGLink (dagNode) {\n+  return new DAGLink(null, dagNode.size, dagNode.multihash)\n+}\n+\n+function clone (dagNode, callback) {\n+  const data = _cloneData(dagNode)\n+  const links = _cloneLinks(dagNode)\n+  create(data, links, callback)\n+}\n+\n+function serialize (node, callback) {\n+  let serialized\n+\n+  try {\n+    const pb = toProtoBuf(node)\n+    serialized = proto.PBNode.encode(pb)\n+  } catch (err) {\n+    return callback(err)\n+  }\n+\n+  callback(null, serialized)\n+}\n+\n+function deserialize (data, callback) {\n+  const pbn = proto.PBNode.decode(data)\n+\n+  const links = pbn.Links.map((link) => {\n+    return new DAGLink(link.Name, link.Tsize, link.Hash)\n+  })\n+\n+  sort(links, linkSort)\n+\n+  const buf = pbn.Data || new Buffer(0)\n+\n+  create(buf, links, callback)\n+}\n+\n+function toProtoBuf (node) {\n+  const pbn = {}\n+\n+  if (node.data && node.data.length > 0) {\n+    pbn.Data = node.data\n+  } else {\n+    pbn.Data = null // new Buffer(0)\n+  }\n+\n+  if (node.links.length > 0) {\n+    pbn.Links = node.links.map((link) => {\n+      return {\n+        Hash: link.hash,\n+        Name: link.name,\n+        Tsize: link.size\n+      }\n+    })\n+  } else {\n+    pbn.Links = null\n+  }\n+\n+  return pbn\n+}\n+\n+function _cloneData (dagNode) {\n+  let data = new Buffer(0)\n+  if (dagNode.data && dagNode.data.length > 0) {\n+    data = new Buffer(dagNode.data.length)\n+    dagNode.data.copy(data)\n+  }\n+  return data\n+}\n+\n+function _cloneLinks (dagNode) {\n+  return dagNode.links.length > 0 ? dagNode.links.slice() : []\n+}\n+\n+exports.create = create\n+exports.clone = clone","path":"src/dag-node-utils.js","position":202,"original_position":202,"commit_id":"d253d7344b207d4178f411e7bb938eab9e1d31ad","original_commit_id":"0f160c8b22d30db8cf15ac8254e251d8cab24f8f","user":{"login":"diasdavid","id":1211152,"avatar_url":"https://avatars.githubusercontent.com/u/1211152?v=3","gravatar_id":"","url":"https://api.github.com/users/diasdavid","html_url":"https://github.com/diasdavid","followers_url":"https://api.github.com/users/diasdavid/followers","following_url":"https://api.github.com/users/diasdavid/following{/other_user}","gists_url":"https://api.github.com/users/diasdavid/gists{/gist_id}","starred_url":"https://api.github.com/users/diasdavid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/diasdavid/subscriptions","organizations_url":"https://api.github.com/users/diasdavid/orgs","repos_url":"https://api.github.com/users/diasdavid/repos","events_url":"https://api.github.com/users/diasdavid/events{/privacy}","received_events_url":"https://api.github.com/users/diasdavid/received_events","type":"User","site_admin":false},"body":"why did clone get separated from the class node? `const copy = node.clone()` was nice","created_at":"2016-11-16T10:46:22Z","updated_at":"2016-11-16T10:46:22Z","html_url":"https://github.com/ipld/js-ipld-dag-pb/pull/7#discussion_r88211258","pull_request_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls/7","_links":{"self":{"href":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls/comments/88211258"},"html":{"href":"https://github.com/ipld/js-ipld-dag-pb/pull/7#discussion_r88211258"},"pull_request":{"href":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls/7"}}},"pull_request":{"url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls/7","id":93586366,"html_url":"https://github.com/ipld/js-ipld-dag-pb/pull/7","diff_url":"https://github.com/ipld/js-ipld-dag-pb/pull/7.diff","patch_url":"https://github.com/ipld/js-ipld-dag-pb/pull/7.patch","issue_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/issues/7","number":7,"state":"open","locked":false,"title":"Refactor feat/immutable PR","user":{"login":"haadcode","id":7499694,"avatar_url":"https://avatars.githubusercontent.com/u/7499694?v=3","gravatar_id":"","url":"https://api.github.com/users/haadcode","html_url":"https://github.com/haadcode","followers_url":"https://api.github.com/users/haadcode/followers","following_url":"https://api.github.com/users/haadcode/following{/other_user}","gists_url":"https://api.github.com/users/haadcode/gists{/gist_id}","starred_url":"https://api.github.com/users/haadcode/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/haadcode/subscriptions","organizations_url":"https://api.github.com/users/haadcode/orgs","repos_url":"https://api.github.com/users/haadcode/repos","events_url":"https://api.github.com/users/haadcode/events{/privacy}","received_events_url":"https://api.github.com/users/haadcode/received_events","type":"User","site_admin":false},"body":"Refactored https://github.com/ipld/js-ipld-dag-pb/pull/6","created_at":"2016-11-14T15:24:51Z","updated_at":"2016-11-16T10:46:22Z","closed_at":null,"merged_at":null,"merge_commit_sha":"fb85938896439e66d84cb7f6c9ef80d57bac299b","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls/7/commits","review_comments_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls/7/comments","review_comment_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/issues/7/comments","statuses_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/statuses/d253d7344b207d4178f411e7bb938eab9e1d31ad","head":{"label":"ipld:feat/immutable-haad","ref":"feat/immutable-haad","sha":"d253d7344b207d4178f411e7bb938eab9e1d31ad","user":{"login":"ipld","id":19803583,"avatar_url":"https://avatars.githubusercontent.com/u/19803583?v=3","gravatar_id":"","url":"https://api.github.com/users/ipld","html_url":"https://github.com/ipld","followers_url":"https://api.github.com/users/ipld/followers","following_url":"https://api.github.com/users/ipld/following{/other_user}","gists_url":"https://api.github.com/users/ipld/gists{/gist_id}","starred_url":"https://api.github.com/users/ipld/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ipld/subscriptions","organizations_url":"https://api.github.com/users/ipld/orgs","repos_url":"https://api.github.com/users/ipld/repos","events_url":"https://api.github.com/users/ipld/events{/privacy}","received_events_url":"https://api.github.com/users/ipld/received_events","type":"Organization","site_admin":false},"repo":{"id":69641599,"name":"js-ipld-dag-pb","full_name":"ipld/js-ipld-dag-pb","owner":{"login":"ipld","id":19803583,"avatar_url":"https://avatars.githubusercontent.com/u/19803583?v=3","gravatar_id":"","url":"https://api.github.com/users/ipld","html_url":"https://github.com/ipld","followers_url":"https://api.github.com/users/ipld/followers","following_url":"https://api.github.com/users/ipld/following{/other_user}","gists_url":"https://api.github.com/users/ipld/gists{/gist_id}","starred_url":"https://api.github.com/users/ipld/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ipld/subscriptions","organizations_url":"https://api.github.com/users/ipld/orgs","repos_url":"https://api.github.com/users/ipld/repos","events_url":"https://api.github.com/users/ipld/events{/privacy}","received_events_url":"https://api.github.com/users/ipld/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ipld/js-ipld-dag-pb","description":"JavaScript Implementation of the MerkleDAG Nodes with Protobuf.","fork":false,"url":"https://api.github.com/repos/ipld/js-ipld-dag-pb","forks_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/forks","keys_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/teams","hooks_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/hooks","issue_events_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/issues/events{/number}","events_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/events","assignees_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/assignees{/user}","branches_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/branches{/branch}","tags_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/tags","blobs_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/git/refs{/sha}","trees_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/statuses/{sha}","languages_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/languages","stargazers_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/stargazers","contributors_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/contributors","subscribers_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/subscribers","subscription_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/subscription","commits_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/commits{/sha}","git_commits_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/git/commits{/sha}","comments_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/comments{/number}","issue_comment_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/issues/comments{/number}","contents_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/contents/{+path}","compare_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/merges","archive_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/downloads","issues_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/issues{/number}","pulls_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls{/number}","milestones_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/milestones{/number}","notifications_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/labels{/name}","releases_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/releases{/id}","deployments_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/deployments","created_at":"2016-09-30T06:51:36Z","updated_at":"2016-11-16T02:16:03Z","pushed_at":"2016-11-16T10:46:12Z","git_url":"git://github.com/ipld/js-ipld-dag-pb.git","ssh_url":"git@github.com:ipld/js-ipld-dag-pb.git","clone_url":"https://github.com/ipld/js-ipld-dag-pb.git","svn_url":"https://github.com/ipld/js-ipld-dag-pb","homepage":null,"size":86538,"stargazers_count":2,"watchers_count":2,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":5,"forks":1,"open_issues":5,"watchers":2,"default_branch":"master"}},"base":{"label":"ipld:feat/immutable","ref":"feat/immutable","sha":"4bdb48bd80a2d5a459204f08f69f4e4e4649e027","user":{"login":"ipld","id":19803583,"avatar_url":"https://avatars.githubusercontent.com/u/19803583?v=3","gravatar_id":"","url":"https://api.github.com/users/ipld","html_url":"https://github.com/ipld","followers_url":"https://api.github.com/users/ipld/followers","following_url":"https://api.github.com/users/ipld/following{/other_user}","gists_url":"https://api.github.com/users/ipld/gists{/gist_id}","starred_url":"https://api.github.com/users/ipld/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ipld/subscriptions","organizations_url":"https://api.github.com/users/ipld/orgs","repos_url":"https://api.github.com/users/ipld/repos","events_url":"https://api.github.com/users/ipld/events{/privacy}","received_events_url":"https://api.github.com/users/ipld/received_events","type":"Organization","site_admin":false},"repo":{"id":69641599,"name":"js-ipld-dag-pb","full_name":"ipld/js-ipld-dag-pb","owner":{"login":"ipld","id":19803583,"avatar_url":"https://avatars.githubusercontent.com/u/19803583?v=3","gravatar_id":"","url":"https://api.github.com/users/ipld","html_url":"https://github.com/ipld","followers_url":"https://api.github.com/users/ipld/followers","following_url":"https://api.github.com/users/ipld/following{/other_user}","gists_url":"https://api.github.com/users/ipld/gists{/gist_id}","starred_url":"https://api.github.com/users/ipld/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ipld/subscriptions","organizations_url":"https://api.github.com/users/ipld/orgs","repos_url":"https://api.github.com/users/ipld/repos","events_url":"https://api.github.com/users/ipld/events{/privacy}","received_events_url":"https://api.github.com/users/ipld/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ipld/js-ipld-dag-pb","description":"JavaScript Implementation of the MerkleDAG Nodes with Protobuf.","fork":false,"url":"https://api.github.com/repos/ipld/js-ipld-dag-pb","forks_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/forks","keys_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/teams","hooks_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/hooks","issue_events_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/issues/events{/number}","events_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/events","assignees_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/assignees{/user}","branches_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/branches{/branch}","tags_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/tags","blobs_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/git/refs{/sha}","trees_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/statuses/{sha}","languages_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/languages","stargazers_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/stargazers","contributors_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/contributors","subscribers_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/subscribers","subscription_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/subscription","commits_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/commits{/sha}","git_commits_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/git/commits{/sha}","comments_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/comments{/number}","issue_comment_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/issues/comments{/number}","contents_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/contents/{+path}","compare_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/merges","archive_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/downloads","issues_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/issues{/number}","pulls_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls{/number}","milestones_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/milestones{/number}","notifications_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/labels{/name}","releases_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/releases{/id}","deployments_url":"https://api.github.com/repos/ipld/js-ipld-dag-pb/deployments","created_at":"2016-09-30T06:51:36Z","updated_at":"2016-11-16T02:16:03Z","pushed_at":"2016-11-16T10:46:12Z","git_url":"git://github.com/ipld/js-ipld-dag-pb.git","ssh_url":"git@github.com:ipld/js-ipld-dag-pb.git","clone_url":"https://github.com/ipld/js-ipld-dag-pb.git","svn_url":"https://github.com/ipld/js-ipld-dag-pb","homepage":null,"size":86538,"stargazers_count":2,"watchers_count":2,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":5,"forks":1,"open_issues":5,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls/7"},"html":{"href":"https://github.com/ipld/js-ipld-dag-pb/pull/7"},"issue":{"href":"https://api.github.com/repos/ipld/js-ipld-dag-pb/issues/7"},"comments":{"href":"https://api.github.com/repos/ipld/js-ipld-dag-pb/issues/7/comments"},"review_comments":{"href":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls/7/comments"},"review_comment":{"href":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ipld/js-ipld-dag-pb/pulls/7/commits"},"statuses":{"href":"https://api.github.com/repos/ipld/js-ipld-dag-pb/statuses/d253d7344b207d4178f411e7bb938eab9e1d31ad"}}}}
{"actor":{"display_login":"diasdavid"}}