Skip to content
This repository has been archived by the owner on Apr 29, 2020. It is now read-only.

Commit

Permalink
fix: only output unixfs things (#49)
Browse files Browse the repository at this point in the history
This is a `unixfs-importer` but it's possible to output CIDs that
resolve to `dag-raw` nodes if your data is small and you set
`rawLeaves` and `reduceSingleLeafToSelf` to true. In that case
you'll get a `dag-raw` node as the output.

This makes it impossible to set metadata on small files with
`rawLeaves` set to true.

BREAKING CHANGE:

If your data is below the chunk size, and you have `rawLeaves` and
`reduceSingleLeafToSelf` set to true, you'll get a CID that resolves
to a bona fide UnixFS file back with metadata and all that good
stuff instead of a `dag-raw` node.
  • Loading branch information
achingbrain authored Feb 4, 2020
1 parent 2e5de45 commit 8ecdcf2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"ipld-in-memory": "^3.0.0",
"it-buffer-stream": "^1.0.0",
"it-last": "^1.0.0",
"multihashes": "^0.4.14",
"nyc": "^15.0.0",
"sinon": "^8.0.4"
},
Expand Down
18 changes: 18 additions & 0 deletions src/dag-builder/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
} = require('ipld-dag-pb')
const all = require('it-all')
const parallelBatch = require('it-parallel-batch')
const mc = require('multicodec')

const dagBuilders = {
flat: require('./flat'),
Expand Down Expand Up @@ -52,6 +53,23 @@ const reduce = (file, ipld, options) => {
if (leaves.length === 1 && leaves[0].single && options.reduceSingleLeafToSelf) {
const leaf = leaves[0]

if (leaf.cid.codec === 'raw') {
// only one leaf node which is a buffer
const buffer = await ipld.get(leaf.cid)

leaf.unixfs = new UnixFS({
type: 'file',
mtime: file.mtime,
mode: file.mode,
data: buffer
})

const node = new DAGNode(leaf.unixfs.marshal())

leaf.cid = await ipld.put(node, mc.DAG_PB, options)
leaf.size = node.size
}

return {
cid: leaf.cid,
path: file.path,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/persist.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const mh = require('multihashes')
const mh = require('multihashing-async').multihash
const mc = require('multicodec')

const persist = (node, ipld, options) => {
Expand Down
2 changes: 1 addition & 1 deletion test/builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const mh = require('multihashes')
const mh = require('multihashing-async').multihash
const IPLD = require('ipld')
const inMemory = require('ipld-in-memory')
const UnixFS = require('ipfs-unixfs')
Expand Down
2 changes: 1 addition & 1 deletion test/chunker-custom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ describe('custom chunker', function () {
cid: await inmem.put(Buffer.from('hello world'), mc.RAW)
}
}
it('works with single part', fromPartsTest(single, 11))
it('works with single part', fromPartsTest(single, 19))
})
2 changes: 1 addition & 1 deletion test/importer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ strategies.forEach((strategy) => {
type: 'directory'
},
'200Bytes.txt with raw leaves': extend({}, baseFiles['200Bytes.txt'], {
cid: 'zb2rhXrz1gkCv8p4nUDZRohY6MzBE9C3HVTVDP72g6Du3SD9Q',
cid: 'QmQmZQxSKQppbsWfVzBvg59Cn3DKtsNVQ94bjAxg2h3Lb8',
size: 200
})
}, strategyOverrides[strategy])
Expand Down

0 comments on commit 8ecdcf2

Please sign in to comment.