This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor: convert dag API to async/await #2664
Merged
alanshaw
merged 2 commits into
refactor/block-and-pin-async-await
from
refactor/dag-async-await
Dec 16, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -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() | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -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 | ||
] | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.