Skip to content

Commit 3ba1be1

Browse files
committed
chore: make empty files cause the same cid as go
1 parent 271988b commit 3ba1be1

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,23 @@ function Data (type, data) {
7373
}
7474
let fileSize = this.fileSize()
7575

76-
if (!fileSize) {
77-
fileSize = undefined
76+
let data = this.data
77+
78+
if (!this.data || !this.data.length) {
79+
data = undefined
80+
}
81+
82+
let blockSizes = this.blockSizes
83+
84+
if (!this.blockSizes || !this.blockSizes.length) {
85+
blockSizes = undefined
7886
}
7987

8088
return unixfsData.encode({
8189
Type: type,
82-
Data: this.data,
90+
Data: data,
8391
filesize: fileSize,
84-
blocksizes: this.blockSizes.length > 0 ? this.blockSizes : undefined,
92+
blocksizes: blockSizes,
8593
hashType: this.hashType,
8694
fanout: this.fanout
8795
})

test/unixfs-format.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,11 @@ describe('unixfs-format', () => {
135135
// expect(unmarshalled.marshal()).to.deep.equal(symlink)
136136
})
137137
})
138+
139+
it('empty', () => {
140+
const data = new UnixFS('file')
141+
const marshalled = data.marshal()
142+
143+
expect(marshalled).to.deep.equal(Buffer.from([0x08, 0x02, 0x18, 0x00]))
144+
})
138145
})

0 commit comments

Comments
 (0)