Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 21e62cb

Browse files
author
Alan Shaw
committed
refactor: return CID instance from ipfs.add
BREAKING CHANGE: `ipfs.add` results now contain a `cid` property (a [CID instance](https://github.com./multiformats/js-cid)) instead of a string `hash` property. refs ipfs-inactive/interface-js-ipfs-core#394
1 parent cbdf9e6 commit 21e62cb

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

src/cli/commands/add.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ module.exports = {
156156
? globSource(argv.file, { recursive: argv.recursive })
157157
: process.stdin // Pipe directly to ipfs.add
158158

159-
let finalHash
159+
let finalCid
160160

161161
try {
162162
for await (const file of ipfs.add(source, options)) {
@@ -165,11 +165,11 @@ module.exports = {
165165
}
166166

167167
if (argv.quieter) {
168-
finalHash = file.hash
168+
finalCid = file.cid
169169
continue
170170
}
171171

172-
const cid = cidToString(file.hash, { base: argv.cidBase })
172+
const cid = cidToString(file.cid, { base: argv.cidBase })
173173
let message = cid
174174

175175
if (!argv.quiet) {
@@ -197,7 +197,7 @@ module.exports = {
197197
}
198198

199199
if (argv.quieter) {
200-
log(cidToString(finalHash, { base: argv.cidBase }))
200+
log(cidToString(finalCid, { base: argv.cidBase }))
201201
}
202202
})())
203203
}

src/core/components/add/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,10 @@ module.exports = ({ ipld, dag, gcLock, preload, pin, constructorOptions }) => {
5959
function transformFile (dag, opts) {
6060
return async function * (source) {
6161
for await (const { cid, path, unixfs } of source) {
62-
const hash = cid.toString()
63-
6462
if (opts.onlyHash) {
6563
yield {
66-
hash,
67-
path: path || hash,
64+
cid,
65+
path: path || cid.toString(),
6866
size: unixfs.fileSize()
6967
}
7068

@@ -74,8 +72,8 @@ function transformFile (dag, opts) {
7472
const { value: node } = await dag.get(cid, { ...opts, preload: false })
7573

7674
yield {
77-
hash,
78-
path: path || hash,
75+
cid,
76+
path: path || cid.toString(),
7977
size: Buffer.isBuffer(node) ? node.length : node.size
8078
}
8179
}

src/http/api/resources/files-regular.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ exports.add = {
225225
for await (const file of source) {
226226
output.write(JSON.stringify({
227227
Name: file.path,
228-
Hash: cidToString(file.hash, { base: request.query['cid-base'] }),
228+
Hash: cidToString(file.cid, { base: request.query['cid-base'] }),
229229
Size: file.size
230230
}) + '\n')
231231
}

0 commit comments

Comments
 (0)