This repository has been 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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert dag API to async/await (#2664)
* 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
Showing
12 changed files
with
215 additions
and
177 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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 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 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 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 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 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.