@@ -7,7 +7,7 @@ const pull = require('pull-stream')
7
7
const paramap = require ( 'pull-paramap' )
8
8
9
9
// Logic to export a single (possibly chunked) unixfs file.
10
- module . exports = ( node , name , path , pathRest , resolve , size , dag , parent , depth , begin , end ) => {
10
+ module . exports = ( node , name , path , pathRest , resolve , size , dag , parent , depth , offset , length ) => {
11
11
const accepts = pathRest [ 0 ]
12
12
13
13
if ( accepts !== undefined && accepts !== path ) {
@@ -16,46 +16,50 @@ module.exports = (node, name, path, pathRest, resolve, size, dag, parent, depth,
16
16
17
17
const file = UnixFS . unmarshal ( node . data )
18
18
const fileSize = size || file . fileSize ( )
19
- const content = streamBytes ( dag , node , fileSize , findByteRange ( fileSize , begin , end ) )
20
19
21
- return pull . values ( [ {
22
- depth : depth ,
23
- content : content ,
24
- name : name ,
25
- path : path ,
26
- hash : node . multihash ,
27
- size : fileSize ,
28
- type : 'file'
29
- } ] )
30
- }
20
+ if ( offset < 0 ) {
21
+ return pull . error ( new Error ( 'Offset must be greater than 0' ) )
22
+ }
31
23
32
- function findByteRange ( fileSize , begin , end ) {
33
- if ( ! begin ) {
34
- begin = 0
24
+ if ( offset > fileSize ) {
25
+ return pull . error ( new Error ( 'Offset must be less than the file size' ) )
35
26
}
36
27
37
- if ( ! end || end > fileSize ) {
38
- end = fileSize
28
+ if ( length < 0 ) {
29
+ return pull . error ( new Error ( 'Length must be greater than or equal to 0' ) )
39
30
}
40
31
41
- if ( begin < 0 ) {
42
- begin = fileSize + begin
32
+ if ( length === 0 ) {
33
+ return pull . empty ( )
43
34
}
44
35
45
- if ( end < 0 ) {
46
- end = fileSize + end
36
+ if ( ! offset ) {
37
+ offset = 0
47
38
}
48
39
49
- return {
50
- begin , end
40
+ if ( ! length || ( offset + length > fileSize ) ) {
41
+ length = fileSize - offset
51
42
}
43
+
44
+ const content = streamBytes ( dag , node , fileSize , offset , length )
45
+
46
+ return pull . values ( [ {
47
+ depth : depth ,
48
+ content : content ,
49
+ name : name ,
50
+ path : path ,
51
+ hash : node . multihash ,
52
+ size : fileSize ,
53
+ type : 'file'
54
+ } ] )
52
55
}
53
56
54
- function streamBytes ( dag , node , fileSize , { begin , end } ) {
55
- if ( begin === end ) {
57
+ function streamBytes ( dag , node , fileSize , offset , length ) {
58
+ if ( offset === fileSize || length === 0 ) {
56
59
return pull . empty ( )
57
60
}
58
61
62
+ const end = offset + length
59
63
let streamPosition = 0
60
64
61
65
function getData ( { node, start } ) {
@@ -70,7 +74,7 @@ function streamBytes (dag, node, fileSize, { begin, end }) {
70
74
return
71
75
}
72
76
73
- const block = extractDataFromBlock ( file . data , start , begin , end )
77
+ const block = extractDataFromBlock ( file . data , start , offset , end )
74
78
75
79
return block
76
80
} catch ( err ) {
@@ -95,9 +99,9 @@ function streamBytes (dag, node, fileSize, { begin, end }) {
95
99
return child
96
100
} )
97
101
. filter ( ( child , index ) => {
98
- return ( begin >= child . start && begin < child . end ) || // child has begin byte
102
+ return ( offset >= child . start && offset < child . end ) || // child has offset byte
99
103
( end > child . start && end <= child . end ) || // child has end byte
100
- ( begin < child . start && end > child . end ) // child is between begin and end bytes
104
+ ( offset < child . start && end > child . end ) // child is between offset and end bytes
101
105
} )
102
106
103
107
if ( filteredLinks . length ) {
0 commit comments