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
Merged
Implement ipfs refs #2004
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
8b36a48
feat: implement ipfs refs
dirkmc 71ec3f1
feat: refs support in http api
dirkmc 89f9b8c
feat: use ipld instead of unix-fs-exporter for refs
dirkmc 67f3a84
test: add basic refs test
dirkmc 8c09c8c
feat: refs local
dirkmc 12b4b60
feat: add refs.localPullStream && refs.localReadableStream
dirkmc b0564a1
chore: change Ref -> ref
dirkmc 00b38c6
feat: make object.links work with CBOR
dirkmc 425447a
feat: handle multiple refs. Better param handling
dirkmc 1129e4c
fix: print refs errors to stderr
dirkmc 348b1e2
chore: add comment to explain cli param parsing
dirkmc 1e9aff9
refactor: use streaming for refs local
dirkmc 1d81be8
chore: update interface-ipfs-core package
dirkmc bf9fdff
refactor: cleaner refs param handling
dirkmc e8b76b4
fix: alias 'refs local' to 'refs-local'
dirkmc 762aff0
refactor: move links retrieval from object to refs
dirkmc 3416564
chore: add missing packages
dirkmc f991388
refactor: use streaming for cli refs and refs local
dirkmc 5e5cd59
fix: add refs and refs local to command count
dirkmc 07e4267
fix: refs in browser
dirkmc b55cfde
fix: restore param parsing behaviour
dirkmc e663075
chore: update interface-ipfs-core
dirkmc c731ddf
chore: update http-client
dirkmc 6ac01e5
chore: update interface-ipfs-core
dirkmc 6b002ac
fix: skip failing config.set test for now
dirkmc 0fc81e5
fix: skip failing config.set test in http-api
dirkmc a6bc527
chore: update interface-ipfs-core and ipfs-http-client
dirkmc 384c5de
chore: fix ipfs-http-client version
dirkmc 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 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 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,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 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,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 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,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 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 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 |
---|---|---|
@@ -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 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 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 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,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 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,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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be able to use this as
ipfs refs local
i.e. without the hyphenThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alanshaw it seems like it will be tricky to make the parser work with both
ipfs refs <path>
ipfs refs local
I can probably hack it to make it work, but before I do that do you think it would make more sense to the user to give the commands different names instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets keep compat with go-ipfs - it's not too hacky to detect when
path === 'local'
and do the right thing in therefs
handler.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That part is easy, it's making the command line parser / help text auto-formatter understand it that will be a little trickier, I'll see if I can make it work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, please take a look but if it's going to be a big time sink or over complicated then lets forget it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a command aliaser at the beginning of parsing (unfortunately it's not possible to do with yargs middleware): https://github.com/ipfs/js-ipfs/pull/2004/files#diff-71bd1bde77e38a68f76195b8f85ce19c