@@ -10,13 +10,15 @@ const callbackify = require('callbackify')
10
10
const PassThrough = require ( 'stream' ) . PassThrough
11
11
const pull = require ( 'pull-stream/pull' )
12
12
const map = require ( 'pull-stream/throughs/map' )
13
+ const isIpfs = require ( 'is-ipfs' )
14
+ const { cidToString } = require ( '../../utils/cid' )
13
15
14
16
const mapLsFile = ( options = { } ) => {
15
17
const long = options . long || options . l
16
18
17
19
return ( file ) => {
18
20
return {
19
- hash : long ? file . cid . toBaseEncodedString ( options . cidBase ) : '' ,
21
+ hash : long ? cidToString ( file . cid , { base : options . cidBase } ) : '' ,
20
22
name : file . name ,
21
23
type : long ? file . type : 0 ,
22
24
size : long ? file . size || 0 : 0
@@ -32,15 +34,37 @@ module.exports = self => {
32
34
repoOwner : self . _options . repoOwner
33
35
} )
34
36
37
+ const withPreload = fn => ( ...args ) => {
38
+ const cids = args . reduce ( ( cids , p ) => {
39
+ if ( isIpfs . ipfsPath ( p ) ) {
40
+ cids . push ( p . split ( '/' ) [ 2 ] )
41
+ } else if ( isIpfs . cid ( p ) ) {
42
+ cids . push ( p )
43
+ } else if ( isIpfs . cidPath ( p ) ) {
44
+ cids . push ( p . split ( '/' ) [ 0 ] )
45
+ }
46
+ return cids
47
+ } , [ ] )
48
+
49
+ if ( cids . length ) {
50
+ const options = args [ args . length - 1 ]
51
+ if ( options . preload !== false ) {
52
+ cids . forEach ( cid => self . _preload ( cid ) )
53
+ }
54
+ }
55
+
56
+ return fn ( ...args )
57
+ }
58
+
35
59
return {
36
- cp : callbackify . variadic ( methods . cp ) ,
60
+ cp : callbackify . variadic ( withPreload ( methods . cp ) ) ,
37
61
flush : callbackify . variadic ( methods . flush ) ,
38
- ls : callbackify . variadic ( async ( path , options = { } ) => {
62
+ ls : callbackify . variadic ( withPreload ( async ( path , options = { } ) => {
39
63
const files = await all ( methods . ls ( path , options ) )
40
64
41
65
return files . map ( mapLsFile ( options ) )
42
- } ) ,
43
- lsReadableStream : ( path , options = { } ) => {
66
+ } ) ) ,
67
+ lsReadableStream : withPreload ( ( path , options = { } ) => {
44
68
const stream = toReadableStream . obj ( methods . ls ( path , options ) )
45
69
const through = new PassThrough ( {
46
70
objectMode : true
@@ -60,33 +84,33 @@ module.exports = self => {
60
84
} )
61
85
62
86
return through
63
- } ,
64
- lsPullStream : ( path , options = { } ) => {
87
+ } ) ,
88
+ lsPullStream : withPreload ( ( path , options = { } ) => {
65
89
return pull (
66
90
toPullStream . source ( methods . ls ( path , options ) ) ,
67
91
map ( mapLsFile ( options ) )
68
92
)
69
- } ,
93
+ } ) ,
70
94
mkdir : callbackify . variadic ( methods . mkdir ) ,
71
- mv : callbackify . variadic ( methods . mv ) ,
72
- read : callbackify ( async ( path , options = { } ) => {
95
+ mv : callbackify . variadic ( withPreload ( methods . mv ) ) ,
96
+ read : callbackify ( withPreload ( async ( path , options = { } ) => {
73
97
return Buffer . concat ( await all ( methods . read ( path , options ) ) )
74
- } ) ,
75
- readPullStream : ( path , options = { } ) => {
98
+ } ) ) ,
99
+ readPullStream : withPreload ( ( path , options = { } ) => {
76
100
return toPullStream . source ( methods . read ( path , options ) )
77
- } ,
78
- readReadableStream : ( path , options = { } ) => {
101
+ } ) ,
102
+ readReadableStream : withPreload ( ( path , options = { } ) => {
79
103
return toReadableStream ( methods . read ( path , options ) )
80
- } ,
104
+ } ) ,
81
105
rm : callbackify . variadic ( methods . rm ) ,
82
- stat : callbackify ( async ( path , options = { } ) => {
106
+ stat : callbackify ( withPreload ( async ( path , options = { } ) => {
83
107
const stats = await methods . stat ( path , options )
84
108
85
- stats . hash = stats . cid . toBaseEncodedString ( options && options . cidBase )
109
+ stats . hash = cidToString ( stats . cid , { base : options . cidBase } )
86
110
delete stats . cid
87
111
88
112
return stats
89
- } ) ,
113
+ } ) ) ,
90
114
write : callbackify . variadic ( async ( path , content , options = { } ) => {
91
115
if ( isPullStream . isSource ( content ) ) {
92
116
content = pullStreamToAsyncIterator ( content )
0 commit comments