Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
refactor: convert dag API to async/await (#2664)
Browse files Browse the repository at this point in the history
* refactor: convert dag API to async/await

* refactor: pull out repeated name to codec conversion code
  • Loading branch information
Alan Shaw authored Dec 16, 2019
1 parent 070236c commit a482044
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 177 deletions.
3 changes: 1 addition & 2 deletions src/cli/commands/dag/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ module.exports = {
const options = {}

try {
const result = await ipfs.dag.resolve(ref, options)
let lastCid

for (const res of result) {
for await (const res of ipfs.dag.resolve(ref, options)) {
if (CID.isCID(res.value)) {
lastCid = res.value
}
Expand Down
170 changes: 0 additions & 170 deletions src/core/components/dag.js

This file was deleted.

34 changes: 34 additions & 0 deletions src/core/components/dag/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const { parseArgs } = require('./utils')

module.exports = ({ ipld, preload }) => {
return async function get (cid, path, options) {
[cid, path, options] = parseArgs(cid, path, options)

if (options.preload !== false) {
preload(cid)
}

if (path == null || path === '/') {
const value = await ipld.get(cid)

return {
value,
remainderPath: ''
}
} else {
let result

for await (const entry of ipld.resolve(cid, path)) {
if (options.localResolve) {
return entry
}

result = entry
}

return result
}
}
}
70 changes: 70 additions & 0 deletions src/core/components/dag/put.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict'

const multicodec = require('multicodec')
const nameToCodec = name => multicodec[name.toUpperCase().replace(/-/g, '_')]

module.exports = ({ ipld, pin, gcLock, preload }) => {
return async function put (dagNode, options) {
options = options || {}

if (options.cid && (options.format || options.hashAlg)) {
throw new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hashAlg` options.')
} else if (((options.format && !options.hashAlg) || (!options.format && options.hashAlg))) {
throw new Error('Can\'t put dag node. Please provide `format` AND `hashAlg` options.')
}

const optionDefaults = {
format: multicodec.DAG_CBOR,
hashAlg: multicodec.SHA2_256
}

// The IPLD expects the format and hashAlg as constants
if (options.format && typeof options.format === 'string') {
options.format = nameToCodec(options.format)
}
if (options.hashAlg && typeof options.hashAlg === 'string') {
options.hashAlg = nameToCodec(options.hashAlg)
}

options = options.cid ? options : Object.assign({}, optionDefaults, options)

// js-ipld defaults to verion 1 CIDs. Hence set version 0 explicitly for
// dag-pb nodes
if (options.version === undefined) {
if (options.format === multicodec.DAG_PB && options.hashAlg === multicodec.SHA2_256) {
options.version = 0
} else {
options.version = 1
}
}

let release

if (options.pin) {
release = await gcLock.readLock()
}

try {
const cid = await ipld.put(dagNode, options.format, {
hashAlg: options.hashAlg,
cidVersion: options.version
})

if (options.pin) {
await pin.add(cid, {
lock: false
})
}

if (options.preload !== false) {
preload(cid)
}

return cid
} finally {
if (release) {
release()
}
}
}
}
15 changes: 15 additions & 0 deletions src/core/components/dag/resolve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const { parseArgs } = require('./utils')

module.exports = ({ ipld, preload }) => {
return async function * resolve (cid, path, options) { // eslint-disable-line require-await
[cid, path, options] = parseArgs(cid, path, options)

if (options.preload !== false) {
preload(cid)
}

yield * ipld.resolve(cid, path)
}
}
15 changes: 15 additions & 0 deletions src/core/components/dag/tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const { parseArgs } = require('./utils')

module.exports = ({ ipld, preload }) => {
return async function * tree (cid, path, options) { // eslint-disable-line require-await
[cid, path, options] = parseArgs(cid, path, options)

if (options.preload !== false) {
preload(cid)
}

yield * ipld.tree(cid, path, options)
}
}
48 changes: 48 additions & 0 deletions src/core/components/dag/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict'

const CID = require('cids')
const errCode = require('err-code')

exports.parseArgs = (cid, path, options) => {
options = options || {}

// Allow options in path position
if (path !== undefined && typeof path !== 'string') {
options = path
path = undefined
}

if (typeof cid === 'string') {
if (cid.startsWith('/ipfs/')) {
cid = cid.substring(6)
}

const split = cid.split('/')

try {
cid = new CID(split[0])
} catch (err) {
throw errCode(err, 'ERR_INVALID_CID')
}

split.shift()

if (split.length > 0) {
path = split.join('/')
} else {
path = path || '/'
}
} else if (Buffer.isBuffer(cid)) {
try {
cid = new CID(cid)
} catch (err) {
throw errCode(err, 'ERR_INVALID_CID')
}
}

return [
cid,
path,
options
]
}
7 changes: 6 additions & 1 deletion src/core/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ exports.block = {
stat: require('./block/stat')
}
exports.config = require('./config')
exports.dag = {
get: require('./dag/get'),
put: require('./dag/put'),
resolve: require('./dag/resolve'),
tree: require('./dag/tree')
}
exports.init = require('./init')
exports.pin = {
add: require('./pin/add'),
Expand All @@ -18,7 +24,6 @@ exports.start = require('./start')
exports.stop = require('./stop')

exports.legacy = { // TODO: these will be removed as the new API is completed
dag: require('./dag'),
libp2p: require('./libp2p'),
object: require('./object')
}
Loading

0 comments on commit a482044

Please sign in to comment.