This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: The API is now async/await based There are numerous changes, the most significant one is that the API is no longer callback based, but it using async/await. For the full new API please see the [IPLD Formats spec]. [IPLD Formats spec]: https://github.com/ipld/interface-ipld-format
- Loading branch information
Showing
13 changed files
with
305 additions
and
525 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,21 +36,18 @@ | |
}, | ||
"homepage": "https://github.com/ipld/js-ipld-git", | ||
"dependencies": { | ||
"async": "^2.6.2", | ||
"cids": "~0.6.0", | ||
"multicodec": "~0.5.0", | ||
"multihashes": "~0.4.14", | ||
"multihashing-async": "~0.6.0", | ||
"multihashing-async": "~0.7.0", | ||
"smart-buffer": "^4.0.2", | ||
"traverse": "~0.6.6", | ||
"strftime": "~0.10.0" | ||
}, | ||
"devDependencies": { | ||
"aegir": "^18.2.1", | ||
"chai": "^4.2.0", | ||
"deep-freeze": "0.0.1", | ||
"dirty-chai": "^2.0.1", | ||
"garbage": "0.0.0" | ||
"chai-as-promised": "^7.1.1", | ||
"dirty-chai": "^2.0.1" | ||
}, | ||
"contributors": [ | ||
"Alan Shaw <[email protected]>", | ||
|
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,153 +1,70 @@ | ||
'use strict' | ||
|
||
const util = require('./util') | ||
const traverse = require('traverse') | ||
|
||
exports = module.exports | ||
|
||
exports.multicodec = 'git-raw' | ||
exports.defaultHashAlg = 'sha1' | ||
const CID = require('cids') | ||
|
||
const personInfoPaths = [ | ||
'original', | ||
'name', | ||
'email', | ||
'date' | ||
] | ||
|
||
exports.resolve = (binaryBlob, path, callback) => { | ||
if (typeof path === 'function') { | ||
callback = path | ||
path = undefined | ||
} | ||
|
||
util.deserialize(binaryBlob, (err, node) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
const util = require('./util') | ||
|
||
if (!path || path === '/') { | ||
return callback(null, { | ||
value: node, | ||
remainderPath: '' | ||
}) | ||
/** | ||
* Resolves a path within a Git block. | ||
* | ||
* Returns the value or a link and the partial mising path. This way the | ||
* IPLD Resolver can fetch the link and continue to resolve. | ||
* | ||
* @param {Buffer} binaryBlob - Binary representation of a Git block | ||
* @param {string} [path='/'] - Path that should be resolved | ||
* @returns {Object} result - Result of the path it it was resolved successfully | ||
* @returns {*} result.value - Value the path resolves to | ||
* @returns {string} result.remainderPath - If the path resolves half-way to a | ||
* link, then the `remainderPath` is the part after the link that can be used | ||
* for further resolving | ||
*/ | ||
exports.resolve = (binaryBlob, path) => { | ||
let node = util.deserialize(binaryBlob) | ||
|
||
const parts = path.split('/').filter(Boolean) | ||
while (parts.length) { | ||
const key = parts.shift() | ||
if (node[key] === undefined) { | ||
throw new Error(`Object has no property '${key}'`) | ||
} | ||
|
||
if (Buffer.isBuffer(node)) { // git blob | ||
return callback(null, { | ||
node = node[key] | ||
if (CID.isCID(node)) { | ||
return { | ||
value: node, | ||
remainderPath: path | ||
}) | ||
} | ||
|
||
const parts = path.split('/') | ||
const val = traverse(node).get(parts) | ||
|
||
if (val) { | ||
return callback(null, { | ||
value: val, | ||
remainderPath: '' | ||
}) | ||
} | ||
|
||
let value | ||
let len = parts.length | ||
|
||
for (let i = 0; i < len; i++) { | ||
const partialPath = parts.shift() | ||
|
||
if (Array.isArray(node)) { | ||
value = node[Number(partialPath)] | ||
} if (node[partialPath]) { | ||
value = node[partialPath] | ||
} else { | ||
// can't traverse more | ||
if (!value) { | ||
return callback(new Error('path not available at root')) | ||
} else { | ||
parts.unshift(partialPath) | ||
return callback(null, { | ||
value: value, | ||
remainderPath: parts.join('/') | ||
}) | ||
} | ||
remainderPath: parts.join('/') | ||
} | ||
node = value | ||
} | ||
}) | ||
} | ||
|
||
exports.tree = (binaryBlob, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = undefined | ||
} | ||
|
||
options = options || {} | ||
|
||
util.deserialize(binaryBlob, (err, node) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
if (Buffer.isBuffer(node)) { // git blob | ||
return callback(null, []) | ||
} | ||
|
||
let paths = [] | ||
switch (node.gitType) { | ||
case 'commit': | ||
paths = [ | ||
'message', | ||
'tree' | ||
] | ||
|
||
paths = paths.concat(personInfoPaths.map((e) => 'author/' + e)) | ||
paths = paths.concat(personInfoPaths.map((e) => 'committer/' + e)) | ||
paths = paths.concat(node.parents.map((_, e) => 'parents/' + e)) | ||
|
||
if (node.encoding) { | ||
paths.push('encoding') | ||
} | ||
break | ||
case 'tag': | ||
paths = [ | ||
'object', | ||
'type', | ||
'tag', | ||
'message' | ||
] | ||
|
||
if (node.tagger) { | ||
paths = paths.concat(personInfoPaths.map((e) => 'tagger/' + e)) | ||
} | ||
|
||
break | ||
default: // tree | ||
Object.keys(node).forEach(dir => { | ||
paths.push(dir) | ||
paths.push(dir + '/hash') | ||
paths.push(dir + '/mode') | ||
}) | ||
} | ||
callback(null, paths) | ||
}) | ||
return { | ||
value: node, | ||
remainderPath: '' | ||
} | ||
} | ||
|
||
exports.isLink = (binaryBlob, path, callback) => { | ||
exports.resolve(binaryBlob, path, (err, result) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
if (result.remainderPath.length > 0) { | ||
return callback(new Error('path out of scope')) | ||
} | ||
const traverse = function * (node, path) { | ||
// Traverse only objects and arrays | ||
if (Buffer.isBuffer(node) || CID.isCID(node) || typeof node === 'string' || | ||
node === null) { | ||
return | ||
} | ||
for (const item of Object.keys(node)) { | ||
const nextpath = path === undefined ? item : path + '/' + item | ||
yield nextpath | ||
yield * traverse(node[item], nextpath) | ||
} | ||
} | ||
|
||
if (typeof result.value === 'object' && result.value['/']) { | ||
callback(null, result.value) | ||
} else { | ||
callback(null, false) | ||
} | ||
}) | ||
/** | ||
* Return all available paths of a block. | ||
* | ||
* @generator | ||
* @param {Buffer} binaryBlob - Binary representation of a Bitcoin block | ||
* @yields {string} - A single path | ||
*/ | ||
exports.tree = function * (binaryBlob) { | ||
const node = util.deserialize(binaryBlob) | ||
|
||
yield * traverse(node) | ||
} |
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.