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

fix: ipfs get with raw blocks #3683

Merged
merged 6 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/interface-ipfs-core/src/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ module.exports = (common, options) => {
expect(uint8ArrayConcat(await all(output[0].content))).to.eql(input)
})

it('should get a file added as CIDv1 with rawLeaves', async () => {
const input = uint8ArrayFromString(`TEST${Math.random()}`)

const res = await all(importer([{ content: input }], ipfs.block, { cidVersion: 1, rawLeaves: true }))

const cidv1 = res[0].cid
expect(cidv1.version).to.equal(1)

const output = await all(ipfs.get(cidv1))
expect(output[0].type).to.eql('file')
expect(uint8ArrayConcat(await all(output[0].content))).to.eql(input)
})

it('should get a BIG file', async () => {
for await (const file of ipfs.get(fixtures.bigFile.cid)) {
expect(file.path).to.equal(fixtures.bigFile.cid)
Expand Down
31 changes: 20 additions & 11 deletions packages/ipfs-core/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,36 +96,45 @@ const resolvePath = async function (ipld, ipfsPath, options = {}) {
* @param {boolean} [options.includeContent]
*/
const mapFile = (file, options = {}) => {
if (file.type !== 'file' && file.type !== 'directory' && file.type !== 'raw') {
// file.type === object | identity not supported yet
throw new Error(`Unknown node type '${file.type}'`)
}

/** @type {import('ipfs-core-types/src/root').IPFSEntry} */
const output = {
cid: file.cid,
path: file.path,
name: file.name,
depth: file.path.split('/').length,
size: 0,
size: file.size,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it legit to pass through the raw size for a raw node?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it'll be the size of the block

type: 'file'
}

if (file.type === 'file' || file.type === 'directory') {
if (file.type === 'directory') {
// @ts-ignore - TS type can't be changed from File to Directory
output.type = file.type === 'directory' ? 'dir' : 'file'

if (file.type === 'file') {
output.size = file.unixfs.fileSize()
output.type = 'dir'
}

if (options.includeContent) {
// @ts-expect-error - content is readonly
output.content = file.content()
}
}
if (file.type === 'file') {
output.size = file.unixfs.fileSize()
}

if (file.type === 'file' || file.type === 'directory') {
output.mode = file.unixfs.mode

if (file.unixfs.mtime !== undefined) {
output.mtime = file.unixfs.mtime
}
}

if (options.includeContent) {
if (file.type === 'file' || file.type === 'raw') {
// @ts-expect-error - content is readonly
output.content = file.content()
}
}

return output
}

Expand Down