From 28235b02558c513e1119dfd3d12b622d67546eca Mon Sep 17 00:00:00 2001 From: Oli Evans Date: Tue, 11 May 2021 19:11:51 +0100 Subject: [PATCH] fix: ipfs get with raw blocks (#3683) - tweak ipfs-cli get to handle `raw` - tweak ipfs-core mapFile to handle `raw` - mapFile doesn't handle all mapping object or identity types to an IPFSEntry, so throw if it finds one. This makes the cli behaviour match go-ipfs for ipfs get - add interface-core test for get with rawLeaves: true - remove cli test for "raw" as core normalises the type to "file" so it was not testing a valid conditon fixes #3682 License: MIT Signed-off-by: Oli Evans --- packages/interface-ipfs-core/src/get.js | 13 +++++++++++ packages/ipfs-core/src/utils.js | 31 ++++++++++++++++--------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/packages/interface-ipfs-core/src/get.js b/packages/interface-ipfs-core/src/get.js index 4f345954d1..c1f6e7671c 100644 --- a/packages/interface-ipfs-core/src/get.js +++ b/packages/interface-ipfs-core/src/get.js @@ -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) diff --git a/packages/ipfs-core/src/utils.js b/packages/ipfs-core/src/utils.js index 39494dc222..8dbb131694 100644 --- a/packages/ipfs-core/src/utils.js +++ b/packages/ipfs-core/src/utils.js @@ -96,29 +96,31 @@ 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, 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) { @@ -126,6 +128,13 @@ const mapFile = (file, options = {}) => { } } + if (options.includeContent) { + if (file.type === 'file' || file.type === 'raw') { + // @ts-expect-error - content is readonly + output.content = file.content() + } + } + return output }