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

chore: change from callbacks to promises #38

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ const zlib = require('zlib')

// `gitObject` is a Buffer containing a git object
inflatedObject = zlib.inflateSync(gitObject)
IpldGit.util.deserialize(inflatedObject, (err, dagNode) => {
if (err) throw err
IpldGit.util.deserialize(inflatedObject).then(dagNode => {
console.log(dagNode)
}, error => {
console.error(error)
})

```
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
},
"homepage": "https://github.com/ipld/js-ipld-git",
"dependencies": {
"async": "^2.6.0",
"cids": "~0.5.2",
"multicodec": "~0.4.0",
"multihashes": "~0.4.12",
Expand Down
195 changes: 86 additions & 109 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,139 +15,116 @@ const personInfoPaths = [
'date'
]

exports.resolve = (binaryBlob, path, callback) => {
if (typeof path === 'function') {
callback = path
path = undefined
}

util.deserialize(binaryBlob, (err, node) => {
if (err) {
return callback(err)
}
exports.resolve = async (binaryBlob, path) => {
let node = await util.deserialize(binaryBlob)

if (!path || path === '/') {
return callback(null, {
value: node,
remainderPath: ''
})
if (!path || path === '/') {
return {
value: node,
remainderPath: ''
}
}

if (Buffer.isBuffer(node)) { // git blob
return callback(null, {
value: node,
remainderPath: path
})
if (Buffer.isBuffer(node)) { // git blob
return {
value: node,
remainderPath: path
}
}

const parts = path.split('/')
const val = traverse(node).get(parts)
const parts = path.split('/')
const val = traverse(node).get(parts)

if (val) {
return callback(null, {
value: val,
remainderPath: ''
})
if (val) {
return {
value: val,
remainderPath: ''
}
}

let value
let len = parts.length
let value
let len = parts.length

for (let i = 0; i < len; i++) {
const partialPath = parts.shift()
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]
if (Array.isArray(node)) {
value = node[Number(partialPath)]
} if (node[partialPath]) {
value = node[partialPath]
} else {
// can't traverse more
if (!value) {
throw new Error('path not available at root')
} 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('/')
})
parts.unshift(partialPath)
return {
value: value,
remainderPath: parts.join('/')
}
}
node = value
}
})
}

exports.tree = (binaryBlob, options, callback) => {
if (typeof options === 'function') {
callback = options
options = undefined
node = value
}
}

exports.tree = async (binaryBlob, options) => {
options = options || {}

util.deserialize(binaryBlob, (err, node) => {
if (err) {
return callback(err)
}
const node = await util.deserialize(binaryBlob)

if (Buffer.isBuffer(node)) { // git blob
return callback(null, [])
}
if (Buffer.isBuffer(node)) { // git blob
return []
}

let paths = []
switch (node.gitType) {
case 'commit':
paths = [
'message',
'tree'
]
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))
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))
}
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)
})
break
default: // tree
Object.keys(node).forEach(dir => {
paths.push(dir)
paths.push(dir + '/hash')
paths.push(dir + '/mode')
})
}
return paths
}

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'))
}
exports.isLink = async (binaryBlob, path) => {
const result = await exports.resolve(binaryBlob, path)
if (result.remainderPath.length > 0) {
throw new Error('path out of scope')
}

if (typeof result.value === 'object' && result.value['/']) {
callback(null, result.value)
} else {
callback(null, false)
}
})
if (typeof result.value === 'object' && result.value['/']) {
return result.value
} else {
return false
}
}
63 changes: 21 additions & 42 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const setImmediate = require('async/setImmediate')
const waterfall = require('async/waterfall')
const multihashing = require('multihashing-async')
const CID = require('cids')

Expand All @@ -14,87 +12,68 @@ const tree = require('./util/tree')

exports = module.exports

exports.serialize = (dagNode, callback) => {
exports.serialize = async (dagNode) => {
if (dagNode === null) {
setImmediate(() => callback(new Error('dagNode passed to serialize was null'), null))
return
throw new Error('dagNode passed to serialize was null')
}

if (Buffer.isBuffer(dagNode)) {
if (dagNode.slice(0, 4).toString() === 'blob') {
setImmediate(() => callback(null, dagNode))
return dagNode
} else {
setImmediate(() => callback(new Error('unexpected dagNode passed to serialize'), null))
throw new Error('unexpected dagNode passed to serialize')
}
return
}

switch (dagNode.gitType) {
case 'commit':
commit.serialize(dagNode, callback)
break
return commit.serialize(dagNode)
case 'tag':
tag.serialize(dagNode, callback)
break
return tag.serialize(dagNode)
default:
// assume tree as a file named 'type' is legal
tree.serialize(dagNode, callback)
return tree.serialize(dagNode)
}
}

exports.deserialize = (data, callback) => {
exports.deserialize = async (data) => {
let headLen = gitUtil.find(data, 0)
let head = data.slice(0, headLen).toString()
let typeLen = head.match(/([^ ]+) (\d+)/)
if (!typeLen) {
setImmediate(() => callback(new Error('invalid object header'), null))
return
throw new Error('invalid object header')
}

switch (typeLen[1]) {
case 'blob':
callback(null, data)
break
return data
case 'commit':
commit.deserialize(data.slice(headLen + 1), callback)
break
return commit.deserialize(data.slice(headLen + 1))
case 'tag':
tag.deserialize(data.slice(headLen + 1), callback)
break
return tag.deserialize(data.slice(headLen + 1))
case 'tree':
tree.deserialize(data.slice(headLen + 1), callback)
break
return tree.deserialize(data.slice(headLen + 1))
default:
setImmediate(() => callback(new Error('unknown object type ' + typeLen[1]), null))
throw new Error('unknown object type ' + typeLen[1])
}
}

/**
* @callback CidCallback
* @param {?Error} error - Error if getting the CID failed
* @param {?CID} cid - CID if call was successful
*/
/**
* Get the CID of the DAG-Node.
*
* @param {Object} dagNode - Internal representation
* @param {Object} [options] - Options to create the CID
* @param {number} [options.version=1] - CID version number
* @param {string} [options.hashAlg='sha1'] - Hashing algorithm
* @param {CidCallback} callback - Callback that handles the return value
* @returns {void}
* @returns {Promise} that resolves the CID
*/
exports.cid = (dagNode, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
exports.cid = async (dagNode, options) => {
options = options || {}
const hashAlg = options.hashAlg || resolver.defaultHashAlg
const version = typeof options.version === 'undefined' ? 1 : options.version
waterfall([
(cb) => exports.serialize(dagNode, cb),
(serialized, cb) => multihashing(serialized, hashAlg, cb),
(mh, cb) => cb(null, new CID(version, resolver.multicodec, mh))
], callback)
const serialized = await exports.serialize(dagNode)
const mh = await new Promise((resolve, reject) => {
multihashing(serialized, hashAlg, (err, data) => err ? reject(err) : resolve(data))
})
return new CID(version, resolver.multicodec, mh)
}
Loading