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

Commit 8ecdcf2

Browse files
authored
fix: only output unixfs things (#49)
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.
1 parent 2e5de45 commit 8ecdcf2

File tree

6 files changed

+22
-5
lines changed

6 files changed

+22
-5
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"ipld-in-memory": "^3.0.0",
4949
"it-buffer-stream": "^1.0.0",
5050
"it-last": "^1.0.0",
51-
"multihashes": "^0.4.14",
5251
"nyc": "^15.0.0",
5352
"sinon": "^8.0.4"
5453
},

src/dag-builder/file/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
} = require('ipld-dag-pb')
1010
const all = require('it-all')
1111
const parallelBatch = require('it-parallel-batch')
12+
const mc = require('multicodec')
1213

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

56+
if (leaf.cid.codec === 'raw') {
57+
// only one leaf node which is a buffer
58+
const buffer = await ipld.get(leaf.cid)
59+
60+
leaf.unixfs = new UnixFS({
61+
type: 'file',
62+
mtime: file.mtime,
63+
mode: file.mode,
64+
data: buffer
65+
})
66+
67+
const node = new DAGNode(leaf.unixfs.marshal())
68+
69+
leaf.cid = await ipld.put(node, mc.DAG_PB, options)
70+
leaf.size = node.size
71+
}
72+
5573
return {
5674
cid: leaf.cid,
5775
path: file.path,

src/utils/persist.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const mh = require('multihashes')
3+
const mh = require('multihashing-async').multihash
44
const mc = require('multicodec')
55

66
const persist = (node, ipld, options) => {

test/builder.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const chai = require('chai')
55
chai.use(require('dirty-chai'))
66
const expect = chai.expect
7-
const mh = require('multihashes')
7+
const mh = require('multihashing-async').multihash
88
const IPLD = require('ipld')
99
const inMemory = require('ipld-in-memory')
1010
const UnixFS = require('ipfs-unixfs')

test/chunker-custom.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ describe('custom chunker', function () {
6969
cid: await inmem.put(Buffer.from('hello world'), mc.RAW)
7070
}
7171
}
72-
it('works with single part', fromPartsTest(single, 11))
72+
it('works with single part', fromPartsTest(single, 19))
7373
})

test/importer.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ strategies.forEach((strategy) => {
260260
type: 'directory'
261261
},
262262
'200Bytes.txt with raw leaves': extend({}, baseFiles['200Bytes.txt'], {
263-
cid: 'zb2rhXrz1gkCv8p4nUDZRohY6MzBE9C3HVTVDP72g6Du3SD9Q',
263+
cid: 'QmQmZQxSKQppbsWfVzBvg59Cn3DKtsNVQ94bjAxg2h3Lb8',
264264
size: 200
265265
})
266266
}, strategyOverrides[strategy])

0 commit comments

Comments
 (0)