From 0cfcaf65998bdc2af0cc29ac48229bb3bc35c5b8 Mon Sep 17 00:00:00 2001 From: RangerMauve Date: Wed, 11 Jan 2023 12:24:07 -0500 Subject: [PATCH] fix: allow reading rawLeaves in MFS (#4282) If you write to an MFS directory with `rawLeaves: true`, it'll error out when doing a read saying `Error: /example-0/example.txt was not a file`. This accounts for that case by handling the `raw` type the same as a file. Co-authored-by: Mauve Signweaver Co-authored-by: Alex Potsides --- packages/interface-ipfs-core/src/files/read.js | 9 +++++++++ packages/ipfs-core/src/components/files/read.js | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/interface-ipfs-core/src/files/read.js b/packages/interface-ipfs-core/src/files/read.js index 1212cc4165..32ea03e468 100644 --- a/packages/interface-ipfs-core/src/files/read.js +++ b/packages/interface-ipfs-core/src/files/read.js @@ -112,6 +112,15 @@ export function testRead (factory, options) { expect(testFileData).to.eql(fixtures.smallFile.data) }) + it('should be able to read rawLeaves files', async () => { + const { cid } = await ipfs.add(fixtures.smallFile.data, { + rawLeaves: true + }) + await ipfs.files.cp(`/ipfs/${cid}`, '/raw-leaves.txt') + const testFileData = uint8ArrayConcat(await all(ipfs.files.read('/raw-leaves.txt'))) + expect(testFileData).to.eql(fixtures.smallFile.data) + }) + describe('with sharding', () => { /** @type {import('ipfs-core-types').IPFS} */ let ipfs diff --git a/packages/ipfs-core/src/components/files/read.js b/packages/ipfs-core/src/components/files/read.js index a66ba0d78c..28b132f12c 100644 --- a/packages/ipfs-core/src/components/files/read.js +++ b/packages/ipfs-core/src/components/files/read.js @@ -39,8 +39,8 @@ export function createRead (context) { const mfsPath = await toMfsPath(context, path, options) const result = await exporter(mfsPath.mfsPath, context.repo.blocks) - if (result.type !== 'file') { - throw errCode(new Error(`${path} was not a file`), 'ERR_NOT_FILE') + if (result.type !== 'file' && result.type !== 'raw') { + throw errCode(new Error(`${path} was not a file or raw bytes`), 'ERR_NOT_FILE') } if (!result.content) {