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.
feat: implement ipfs refs and refs local (#2004)
* feat: implement ipfs refs * feat: refs support in http api * feat: use ipld instead of unix-fs-exporter for refs * test: add basic refs test * feat: refs local * feat: add refs.localPullStream && refs.localReadableStream * chore: change Ref -> ref * feat: make object.links work with CBOR * feat: handle multiple refs. Better param handling * fix: print refs errors to stderr * chore: add comment to explain cli param parsing * refactor: use streaming for refs local * chore: update interface-ipfs-core package * refactor: cleaner refs param handling * fix: alias 'refs local' to 'refs-local' * refactor: move links retrieval from object to refs * chore: add missing packages * refactor: use streaming for cli refs and refs local * fix: add refs and refs local to command count * fix: refs in browser * fix: restore param parsing behaviour * chore: update interface-ipfs-core * chore: update http-client * chore: update interface-ipfs-core * fix: skip failing config.set test for now * fix: skip failing config.set test in http-api * chore: update interface-ipfs-core and ipfs-http-client * chore: fix ipfs-http-client version
- Loading branch information
Showing
21 changed files
with
692 additions
and
27 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 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 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,35 @@ | ||
'use strict' | ||
|
||
const aliases = { | ||
// We need to be able to show help text for both the `refs` command and the | ||
// `refs local` command, but with yargs `refs` cannot be both a command and | ||
// a command directory. So alias `refs local` to `refs-local` | ||
'refs-local': ['refs', 'local'] | ||
} | ||
|
||
// Replace multi-word command with alias | ||
// eg replace `refs local` with `refs-local` | ||
module.exports = function (args) { | ||
for (const [alias, original] of Object.entries(aliases)) { | ||
if (arrayMatch(args, original)) { | ||
return [alias, ...args.slice(original.length)] | ||
} | ||
} | ||
|
||
return args | ||
} | ||
|
||
// eg arrayMatch([1, 2, 3], [1, 2]) => true | ||
function arrayMatch (arr, sub) { | ||
if (sub.length > arr.length) { | ||
return false | ||
} | ||
|
||
for (let i = 0; i < sub.length; i++) { | ||
if (arr[i] !== sub[i]) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
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,30 @@ | ||
'use strict' | ||
|
||
const { print } = require('../utils') | ||
|
||
module.exports = { | ||
command: 'refs-local', | ||
|
||
describe: 'List all local references.', | ||
|
||
handler ({ getIpfs, resolve }) { | ||
resolve((async () => { | ||
const ipfs = await getIpfs() | ||
|
||
return new Promise((resolve, reject) => { | ||
const stream = ipfs.refs.localReadableStream() | ||
|
||
stream.on('error', reject) | ||
stream.on('end', resolve) | ||
|
||
stream.on('data', (ref) => { | ||
if (ref.err) { | ||
print(ref.err, true, true) | ||
} else { | ||
print(ref.ref) | ||
} | ||
}) | ||
}) | ||
})()) | ||
} | ||
} |
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,65 @@ | ||
'use strict' | ||
|
||
const { print } = require('../utils') | ||
|
||
module.exports = { | ||
command: 'refs <key> [keys..]', | ||
|
||
describe: 'List links (references) from an object', | ||
|
||
builder: { | ||
recursive: { | ||
alias: 'r', | ||
desc: 'Recursively list links of child nodes.', | ||
type: 'boolean', | ||
default: false | ||
}, | ||
format: { | ||
desc: 'Output edges with given format. Available tokens: <src> <dst> <linkname>.', | ||
type: 'string', | ||
default: '<dst>' | ||
}, | ||
edges: { | ||
alias: 'e', | ||
desc: 'Output edge format: `<from> -> <to>`', | ||
type: 'boolean', | ||
default: false | ||
}, | ||
unique: { | ||
alias: 'u', | ||
desc: 'Omit duplicate refs from output.', | ||
type: 'boolean', | ||
default: false | ||
}, | ||
'max-depth': { | ||
desc: 'Only for recursive refs, limits fetch and listing to the given depth.', | ||
type: 'number' | ||
} | ||
}, | ||
|
||
handler ({ getIpfs, key, keys, recursive, format, edges, unique, maxDepth, resolve }) { | ||
resolve((async () => { | ||
if (maxDepth === 0) { | ||
return | ||
} | ||
|
||
const ipfs = await getIpfs() | ||
const k = [key].concat(keys) | ||
|
||
return new Promise((resolve, reject) => { | ||
const stream = ipfs.refsReadableStream(k, { recursive, format, edges, unique, maxDepth }) | ||
|
||
stream.on('error', reject) | ||
stream.on('end', resolve) | ||
|
||
stream.on('data', (ref) => { | ||
if (ref.err) { | ||
print(ref.err, true, true) | ||
} else { | ||
print(ref.ref) | ||
} | ||
}) | ||
}) | ||
})()) | ||
} | ||
} |
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 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 |
---|---|---|
@@ -1,19 +1,28 @@ | ||
'use strict' | ||
|
||
module.exports = self => ({ | ||
add: require('./add')(self), | ||
addFromFs: require('./add-from-fs')(self), | ||
addFromStream: require('./add-from-stream')(self), | ||
addFromURL: require('./add-from-url')(self), | ||
addPullStream: require('./add-pull-stream')(self), | ||
addReadableStream: require('./add-readable-stream')(self), | ||
cat: require('./cat')(self), | ||
catPullStream: require('./cat-pull-stream')(self), | ||
catReadableStream: require('./cat-readable-stream')(self), | ||
get: require('./get')(self), | ||
getPullStream: require('./get-pull-stream')(self), | ||
getReadableStream: require('./get-readable-stream')(self), | ||
ls: require('./ls')(self), | ||
lsPullStream: require('./ls-pull-stream')(self), | ||
lsReadableStream: require('./ls-readable-stream')(self) | ||
}) | ||
module.exports = (self) => { | ||
const filesRegular = { | ||
add: require('./add')(self), | ||
addFromFs: require('./add-from-fs')(self), | ||
addFromStream: require('./add-from-stream')(self), | ||
addFromURL: require('./add-from-url')(self), | ||
addPullStream: require('./add-pull-stream')(self), | ||
addReadableStream: require('./add-readable-stream')(self), | ||
cat: require('./cat')(self), | ||
catPullStream: require('./cat-pull-stream')(self), | ||
catReadableStream: require('./cat-readable-stream')(self), | ||
get: require('./get')(self), | ||
getPullStream: require('./get-pull-stream')(self), | ||
getReadableStream: require('./get-readable-stream')(self), | ||
ls: require('./ls')(self), | ||
lsPullStream: require('./ls-pull-stream')(self), | ||
lsReadableStream: require('./ls-readable-stream')(self), | ||
refs: require('./refs')(self), | ||
refsReadableStream: require('./refs-readable-stream')(self), | ||
refsPullStream: require('./refs-pull-stream')(self) | ||
} | ||
filesRegular.refs.local = require('./refs-local')(self) | ||
filesRegular.refs.localReadableStream = require('./refs-local-readable-stream')(self) | ||
filesRegular.refs.localPullStream = require('./refs-local-pull-stream')(self) | ||
return filesRegular | ||
} |
34 changes: 34 additions & 0 deletions
34
src/core/components/files-regular/refs-local-pull-stream.js
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 CID = require('cids') | ||
const base32 = require('base32.js') | ||
const pull = require('pull-stream') | ||
const pullDefer = require('pull-defer') | ||
|
||
module.exports = function (self) { | ||
return () => { | ||
const deferred = pullDefer.source() | ||
|
||
self._repo.blocks.query({ keysOnly: true }, (err, blocks) => { | ||
if (err) { | ||
return deferred.resolve(pull.error(err)) | ||
} | ||
|
||
const refs = blocks.map(b => dsKeyToRef(b.key)) | ||
deferred.resolve(pull.values(refs)) | ||
}) | ||
|
||
return deferred | ||
} | ||
} | ||
|
||
function dsKeyToRef (key) { | ||
try { | ||
// Block key is of the form /<base32 encoded string> | ||
const decoder = new base32.Decoder() | ||
const buff = Buffer.from(decoder.write(key.toString().slice(1)).finalize()) | ||
return { ref: new CID(buff).toString() } | ||
} catch (err) { | ||
return { err: `Could not convert block with key '${key}' to CID: ${err.message}` } | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/core/components/files-regular/refs-local-readable-stream.js
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,9 @@ | ||
'use strict' | ||
|
||
const toStream = require('pull-stream-to-stream') | ||
|
||
module.exports = function (self) { | ||
return (ipfsPath, options) => { | ||
return toStream.source(self.refs.localPullStream()) | ||
} | ||
} |
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,18 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const pull = require('pull-stream') | ||
|
||
module.exports = function (self) { | ||
return promisify((callback) => { | ||
pull( | ||
self.refs.localPullStream(), | ||
pull.collect((err, values) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
callback(null, values) | ||
}) | ||
) | ||
}) | ||
} |
Oops, something went wrong.