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
feat: add mssing dag put
and dag resolve
cli commands
#2521
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 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 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,124 @@ | ||
'use strict' | ||
|
||
const mh = require('multihashes') | ||
const multibase = require('multibase') | ||
const dagCBOR = require('ipld-dag-cbor') | ||
const dagPB = require('ipld-dag-pb') | ||
const { cidToString } = require('../../../utils/cid') | ||
|
||
const inputDecoders = { | ||
json: (buf) => JSON.parse(buf.toString()), | ||
cbor: (buf) => dagCBOR.util.deserialize(buf), | ||
protobuf: (buf) => dagPB.util.deserialize(buf), | ||
raw: (buf) => buf | ||
} | ||
|
||
const formats = { | ||
cbor: 'dag-cbor', | ||
raw: 'raw', | ||
protobuf: 'dag-pb', | ||
'dag-cbor': 'dag-cbor', | ||
'dag-pb': 'dag-pb' | ||
} | ||
|
||
module.exports = { | ||
command: 'put [data]', | ||
|
||
describe: 'accepts input from a file or stdin and parses it into an object of the specified format', | ||
|
||
builder: { | ||
data: { | ||
type: 'string' | ||
}, | ||
format: { | ||
type: 'string', | ||
alias: 'f', | ||
default: 'cbor', | ||
describe: 'Format that the object will be added as', | ||
choices: ['dag-cbor', 'dag-pb', 'raw', 'cbor', 'protobuf'] | ||
}, | ||
'input-encoding': { | ||
type: 'string', | ||
alias: 'input-enc', | ||
default: 'json', | ||
describe: 'Format that the input object will be', | ||
choices: ['json', 'cbor', 'raw', 'protobuf'] | ||
}, | ||
pin: { | ||
type: 'boolean', | ||
default: true, | ||
describe: 'Pin this object when adding' | ||
}, | ||
'hash-alg': { | ||
type: 'string', | ||
alias: 'hash', | ||
default: 'sha2-256', | ||
describe: 'Hash function to use', | ||
choices: Object.keys(mh.names) | ||
}, | ||
'cid-version': { | ||
type: 'integer', | ||
describe: 'CID version. Defaults to 0 unless an option that depends on CIDv1 is passed', | ||
default: 0 | ||
}, | ||
'cid-base': { | ||
describe: 'Number base to display CIDs in.', | ||
type: 'string', | ||
choices: multibase.names | ||
}, | ||
preload: { | ||
type: 'boolean', | ||
default: true, | ||
describe: 'Preload this object when adding' | ||
}, | ||
'only-hash': { | ||
type: 'boolean', | ||
default: false, | ||
describe: 'Only hash the content, do not write to the underlying block store' | ||
} | ||
}, | ||
|
||
handler ({ data, format, inputEncoding, pin, hashAlg, cidVersion, cidBase, preload, onlyHash, getIpfs, print, resolve }) { | ||
resolve((async () => { | ||
const ipfs = await getIpfs() | ||
|
||
if (inputEncoding === 'cbor') { | ||
format = 'dag-cbor' | ||
} else if (inputEncoding === 'protobuf') { | ||
format = 'dag-pb' | ||
} | ||
|
||
format = formats[format] | ||
|
||
if (format !== 'dag-pb') { | ||
cidVersion = 1 | ||
} | ||
|
||
let source = data | ||
|
||
if (!source) { | ||
// pipe from stdin | ||
source = Buffer.alloc(0) | ||
|
||
for await (const buf of process.stdin) { | ||
source = Buffer.concat([source, buf]) | ||
} | ||
} else { | ||
source = Buffer.from(source) | ||
} | ||
|
||
source = inputDecoders[inputEncoding](source) | ||
|
||
const cid = await ipfs.dag.put(source, { | ||
format, | ||
hashAlg, | ||
version: cidVersion, | ||
onlyHash, | ||
preload, | ||
pin | ||
}) | ||
|
||
print(cidToString(cid, { base: cidBase })) | ||
})()) | ||
} | ||
} |
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,45 @@ | ||
'use strict' | ||
|
||
const CID = require('cids') | ||
|
||
module.exports = { | ||
command: 'resolve <ref>', | ||
|
||
describe: 'fetches a dag node from ipfs, prints its address and remaining path', | ||
|
||
builder: { | ||
ref: { | ||
type: 'string' | ||
} | ||
}, | ||
|
||
handler ({ ref, getIpfs, print, resolve }) { | ||
resolve((async () => { | ||
const ipfs = await getIpfs() | ||
const options = {} | ||
|
||
try { | ||
const result = await ipfs.dag.resolve(ref, options) | ||
let lastCid | ||
|
||
for (const res of result) { | ||
if (CID.isCID(res.value)) { | ||
lastCid = res.value | ||
} | ||
} | ||
|
||
if (!lastCid) { | ||
if (ref.startsWith('/ipfs/')) { | ||
ref = ref.substring(6) | ||
} | ||
|
||
lastCid = ref.split('/').shift() | ||
} | ||
|
||
print(lastCid.toString()) | ||
} catch (err) { | ||
return print(`dag get resolve: ${err}`) | ||
} | ||
})()) | ||
} | ||
} |
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
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 need to take the lock around the
ipld.put
andpin.add
like we do inipfs.add
https://github.com/ipfs/js-ipfs/blob/master/src/core/components/files-regular/add-async-iterator.js#L58-L64Note we'll need to call
pin.add
withlock: false
in the same way we do with add.