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

Commit a482044

Browse files
author
Alan Shaw
authored
refactor: convert dag API to async/await (#2664)
* refactor: convert dag API to async/await * refactor: pull out repeated name to codec conversion code
1 parent 070236c commit a482044

File tree

12 files changed

+215
-177
lines changed

12 files changed

+215
-177
lines changed

src/cli/commands/dag/resolve.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ module.exports = {
1919
const options = {}
2020

2121
try {
22-
const result = await ipfs.dag.resolve(ref, options)
2322
let lastCid
2423

25-
for (const res of result) {
24+
for await (const res of ipfs.dag.resolve(ref, options)) {
2625
if (CID.isCID(res.value)) {
2726
lastCid = res.value
2827
}

src/core/components/dag.js

Lines changed: 0 additions & 170 deletions
This file was deleted.

src/core/components/dag/get.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict'
2+
3+
const { parseArgs } = require('./utils')
4+
5+
module.exports = ({ ipld, preload }) => {
6+
return async function get (cid, path, options) {
7+
[cid, path, options] = parseArgs(cid, path, options)
8+
9+
if (options.preload !== false) {
10+
preload(cid)
11+
}
12+
13+
if (path == null || path === '/') {
14+
const value = await ipld.get(cid)
15+
16+
return {
17+
value,
18+
remainderPath: ''
19+
}
20+
} else {
21+
let result
22+
23+
for await (const entry of ipld.resolve(cid, path)) {
24+
if (options.localResolve) {
25+
return entry
26+
}
27+
28+
result = entry
29+
}
30+
31+
return result
32+
}
33+
}
34+
}

src/core/components/dag/put.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict'
2+
3+
const multicodec = require('multicodec')
4+
const nameToCodec = name => multicodec[name.toUpperCase().replace(/-/g, '_')]
5+
6+
module.exports = ({ ipld, pin, gcLock, preload }) => {
7+
return async function put (dagNode, options) {
8+
options = options || {}
9+
10+
if (options.cid && (options.format || options.hashAlg)) {
11+
throw new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hashAlg` options.')
12+
} else if (((options.format && !options.hashAlg) || (!options.format && options.hashAlg))) {
13+
throw new Error('Can\'t put dag node. Please provide `format` AND `hashAlg` options.')
14+
}
15+
16+
const optionDefaults = {
17+
format: multicodec.DAG_CBOR,
18+
hashAlg: multicodec.SHA2_256
19+
}
20+
21+
// The IPLD expects the format and hashAlg as constants
22+
if (options.format && typeof options.format === 'string') {
23+
options.format = nameToCodec(options.format)
24+
}
25+
if (options.hashAlg && typeof options.hashAlg === 'string') {
26+
options.hashAlg = nameToCodec(options.hashAlg)
27+
}
28+
29+
options = options.cid ? options : Object.assign({}, optionDefaults, options)
30+
31+
// js-ipld defaults to verion 1 CIDs. Hence set version 0 explicitly for
32+
// dag-pb nodes
33+
if (options.version === undefined) {
34+
if (options.format === multicodec.DAG_PB && options.hashAlg === multicodec.SHA2_256) {
35+
options.version = 0
36+
} else {
37+
options.version = 1
38+
}
39+
}
40+
41+
let release
42+
43+
if (options.pin) {
44+
release = await gcLock.readLock()
45+
}
46+
47+
try {
48+
const cid = await ipld.put(dagNode, options.format, {
49+
hashAlg: options.hashAlg,
50+
cidVersion: options.version
51+
})
52+
53+
if (options.pin) {
54+
await pin.add(cid, {
55+
lock: false
56+
})
57+
}
58+
59+
if (options.preload !== false) {
60+
preload(cid)
61+
}
62+
63+
return cid
64+
} finally {
65+
if (release) {
66+
release()
67+
}
68+
}
69+
}
70+
}

src/core/components/dag/resolve.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const { parseArgs } = require('./utils')
4+
5+
module.exports = ({ ipld, preload }) => {
6+
return async function * resolve (cid, path, options) { // eslint-disable-line require-await
7+
[cid, path, options] = parseArgs(cid, path, options)
8+
9+
if (options.preload !== false) {
10+
preload(cid)
11+
}
12+
13+
yield * ipld.resolve(cid, path)
14+
}
15+
}

src/core/components/dag/tree.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const { parseArgs } = require('./utils')
4+
5+
module.exports = ({ ipld, preload }) => {
6+
return async function * tree (cid, path, options) { // eslint-disable-line require-await
7+
[cid, path, options] = parseArgs(cid, path, options)
8+
9+
if (options.preload !== false) {
10+
preload(cid)
11+
}
12+
13+
yield * ipld.tree(cid, path, options)
14+
}
15+
}

src/core/components/dag/utils.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
3+
const CID = require('cids')
4+
const errCode = require('err-code')
5+
6+
exports.parseArgs = (cid, path, options) => {
7+
options = options || {}
8+
9+
// Allow options in path position
10+
if (path !== undefined && typeof path !== 'string') {
11+
options = path
12+
path = undefined
13+
}
14+
15+
if (typeof cid === 'string') {
16+
if (cid.startsWith('/ipfs/')) {
17+
cid = cid.substring(6)
18+
}
19+
20+
const split = cid.split('/')
21+
22+
try {
23+
cid = new CID(split[0])
24+
} catch (err) {
25+
throw errCode(err, 'ERR_INVALID_CID')
26+
}
27+
28+
split.shift()
29+
30+
if (split.length > 0) {
31+
path = split.join('/')
32+
} else {
33+
path = path || '/'
34+
}
35+
} else if (Buffer.isBuffer(cid)) {
36+
try {
37+
cid = new CID(cid)
38+
} catch (err) {
39+
throw errCode(err, 'ERR_INVALID_CID')
40+
}
41+
}
42+
43+
return [
44+
cid,
45+
path,
46+
options
47+
]
48+
}

src/core/components/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ exports.block = {
88
stat: require('./block/stat')
99
}
1010
exports.config = require('./config')
11+
exports.dag = {
12+
get: require('./dag/get'),
13+
put: require('./dag/put'),
14+
resolve: require('./dag/resolve'),
15+
tree: require('./dag/tree')
16+
}
1117
exports.init = require('./init')
1218
exports.pin = {
1319
add: require('./pin/add'),
@@ -18,7 +24,6 @@ exports.start = require('./start')
1824
exports.stop = require('./stop')
1925

2026
exports.legacy = { // TODO: these will be removed as the new API is completed
21-
dag: require('./dag'),
2227
libp2p: require('./libp2p'),
2328
object: require('./object')
2429
}

0 commit comments

Comments
 (0)