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

Commit 8d1bddd

Browse files
authored
fix: make exporter report file sizes without protobuf overhead (#20)
Restores compatibility with go. Fixes ipfs/js-ipfs#1934
1 parent c470c67 commit 8d1bddd

File tree

7 files changed

+19
-13
lines changed

7 files changed

+19
-13
lines changed

src/dir-flat.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ const cat = require('pull-cat')
99
// Logic to export a unixfs directory.
1010
module.exports = dirExporter
1111

12-
function dirExporter (cid, node, name, path, pathRest, resolve, size, dag, parent, depth, options) {
12+
function dirExporter (cid, node, name, path, pathRest, resolve, dag, parent, depth, options) {
1313
const accepts = pathRest[0]
1414

1515
const dir = {
1616
name: name,
1717
depth: depth,
1818
path: path,
1919
cid,
20-
size: node.size,
20+
size: 0,
2121
type: 'dir'
2222
}
2323

@@ -32,7 +32,7 @@ function dirExporter (cid, node, name, path, pathRest, resolve, size, dag, paren
3232
filter((item) => accepts === undefined || item.name === accepts),
3333
map((link) => ({
3434
depth: depth + 1,
35-
size: link.size,
35+
size: 0,
3636
name: link.name,
3737
path: path + '/' + link.name,
3838
cid: link.cid,

src/dir-hamt-sharded.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ const waterfall = require('async/waterfall')
1414
// Logic to export a unixfs directory.
1515
module.exports = shardedDirExporter
1616

17-
function shardedDirExporter (cid, node, name, path, pathRest, resolve, size, dag, parent, depth, options) {
17+
function shardedDirExporter (cid, node, name, path, pathRest, resolve, dag, parent, depth, options) {
1818
let dir
1919
if (!parent || (parent.path !== path)) {
2020
dir = {
2121
name: name,
2222
depth: depth,
2323
path: path,
2424
cid,
25-
size: node.size,
25+
size: 0,
2626
type: 'dir'
2727
}
2828
}

src/file.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const paramap = require('pull-paramap')
1414
const extractDataFromBlock = require('./extract-data-from-block')
1515

1616
// Logic to export a single (possibly chunked) unixfs file.
17-
module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, depth, options) => {
17+
module.exports = (cid, node, name, path, pathRest, resolve, dag, parent, depth, options) => {
1818
const accepts = pathRest[0]
1919

2020
if (accepts !== undefined && accepts !== path) {
@@ -29,7 +29,7 @@ module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, d
2929
return error(err)
3030
}
3131

32-
const fileSize = size || file.fileSize()
32+
const fileSize = file.fileSize()
3333

3434
let offset = options.offset
3535
let length = options.length

src/object.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const pull = require('pull-stream/pull')
55
const values = require('pull-stream/sources/values')
66
const error = require('pull-stream/sources/error')
77

8-
module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, depth) => {
8+
module.exports = (cid, node, name, path, pathRest, resolve, dag, parent, depth) => {
99
let newNode
1010
if (pathRest.length) {
1111
const pathElem = pathRest[0]

src/raw.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ const empty = require('pull-stream/sources/empty')
66
const extractDataFromBlock = require('./extract-data-from-block')
77

88
// Logic to export a single raw block
9-
module.exports = (cid, node, name, path, pathRest, resolve, size, dag, parent, depth, options) => {
9+
module.exports = (cid, node, name, path, pathRest, resolve, dag, parent, depth, options) => {
1010
const accepts = pathRest[0]
1111

1212
if (accepts !== undefined && accepts !== path) {
1313
return empty()
1414
}
1515

16-
size = size || node.length
16+
const size = node.length
1717

1818
let offset = options.offset
1919
let length = options.length

src/resolve.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,14 @@ function createResolver (dag, options, depth, parent) {
5858
name: item.name,
5959
path: item.path,
6060
pathRest: item.pathRest,
61-
size: item.size,
6261
dag,
6362
parentNode: item.parent || parent,
6463
depth: item.depth,
6564
options
6665
})
6766
}
6867

69-
function resolve ({ cid, node, name, path, pathRest, size, dag, parentNode, depth, options }) {
68+
function resolve ({ cid, node, name, path, pathRest, dag, parentNode, depth, options }) {
7069
let type
7170

7271
try {
@@ -83,7 +82,7 @@ function createResolver (dag, options, depth, parent) {
8382

8483
const resolveDeep = createResolver(dag, options, depth, node)
8584

86-
return nodeResolver(cid, node, name, path, pathRest, resolveDeep, size, dag, parentNode, depth, options)
85+
return nodeResolver(cid, node, name, path, pathRest, resolveDeep, dag, parentNode, depth, options)
8786
}
8887
}
8988

test/exporter.spec.js

+7
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ describe('exporter', () => {
382382
),
383383
({ cid, files: [ file ] }, cb) => {
384384
expect(file).to.have.property('path', cid.toBaseEncodedString())
385+
expect(file).to.have.property('size', ONE_MEG * 6)
385386
fileEql(file, null, cb)
386387
}
387388
], done)
@@ -500,6 +501,12 @@ describe('exporter', () => {
500501
`${cid.toBaseEncodedString()}/level-1/level-2`
501502
])
502503

504+
files
505+
.filter(file => file.type === 'dir')
506+
.forEach(dir => {
507+
expect(dir).to.has.property('size', 0)
508+
})
509+
503510
pull(
504511
pull.values(files),
505512
pull.map((file) => Boolean(file.content)),

0 commit comments

Comments
 (0)