diff --git a/packages/interface-ipfs-core/src/refs-local.js b/packages/interface-ipfs-core/src/refs-local.js index fa8b9bb826..89a1230fc3 100644 --- a/packages/interface-ipfs-core/src/refs-local.js +++ b/packages/interface-ipfs-core/src/refs-local.js @@ -2,8 +2,11 @@ 'use strict' const { fixtures } = require('./utils') +const createFile = require('./utils/create-file') const { getDescribe, getIt, expect } = require('./utils/mocha') const all = require('it-all') +const UnixFS = require('ipfs-unixfs') +const { DAGLink, DAGNode } = require('ipld-dag-pb') /** @typedef { import("ipfsd-ctl/src/factory") } Factory */ /** @@ -26,17 +29,14 @@ module.exports = (common, options) => { after(() => common.clean()) it('should get local refs', async function () { - const content = (name) => ({ - path: `test-folder/${name}`, - content: fixtures.directory.files[name] - }) - - const dirs = [ - content('pp.txt'), - content('holmes.txt') - ] - - await all(ipfs.add(dirs)) + const pp = await createFile(ipfs, fixtures.directory.files['pp.txt']) + const holmes = await createFile(ipfs, fixtures.directory.files['holmes.txt']) + const directory = new UnixFS({ type: 'directory' }) + const serialized = new DAGNode(directory.marshal(), [ + new DAGLink('pp.txt', pp.cumulativeSize, pp.cid), + new DAGLink('holmes.txt', holmes.cumulativeSize, holmes.cid) + ]).serialize() + await ipfs.block.put(serialized) const refs = await all(ipfs.refs.local()) diff --git a/packages/interface-ipfs-core/src/utils/create-file.js b/packages/interface-ipfs-core/src/utils/create-file.js new file mode 100644 index 0000000000..05b1ce30fb --- /dev/null +++ b/packages/interface-ipfs-core/src/utils/create-file.js @@ -0,0 +1,45 @@ +'use strict' + +const UnixFS = require('ipfs-unixfs') +const { DAGLink, DAGNode } = require('ipld-dag-pb') + +// This function creates blocks from lower-level primitives +// to simulate an `ipfs add` without explicitly using `ipfs add` +// for conformance testing clients that haven't implemented UnixFS yet +module.exports = async (ipfs, data, chunkSize = 262144) => { + const chunks = [] + + for (let i = 0; i < data.length; i += chunkSize) { + const unixfs = new UnixFS({ + type: 'file', + data: data.slice(i, i + chunkSize) + }) + const dagNode = new DAGNode(unixfs.marshal()) + const block = await ipfs.block.put(dagNode.serialize()) + + chunks.push({ + unixfs, + size: block.data.length, + cid: block.cid + }) + } + + if (chunks.length === 1) { + return { + cid: chunks[0].cid, + cumulativeSize: chunks[0].size + } + } + + const unixfs = new UnixFS({ + type: 'file', + blockSizes: chunks.map(chunk => chunk.unixfs.fileSize()) + }) + const dagNode = new DAGNode(unixfs.marshal(), chunks.map(chunk => new DAGLink('', chunk.size, chunk.cid))) + const block = await ipfs.block.put(dagNode.serialize()) + + return { + cid: block.cid, + cumulativeSize: chunks.reduce((acc, curr) => acc + curr.size, 0) + block.data.length + } +}