@@ -4,7 +4,14 @@ import parallelBatch from 'it-parallel-batch'
4
4
import * as rawCodec from 'multiformats/codecs/raw'
5
5
import { CustomProgressEvent } from 'progress-events'
6
6
import { persist } from '../utils/persist.js'
7
- import type { BufferImporter , File , InProgressImportResult , WritableStorage , SingleBlockImportResult , ImporterProgressEvents } from '../index.js'
7
+ import type {
8
+ BufferImporter ,
9
+ File ,
10
+ InProgressImportResult ,
11
+ WritableStorage ,
12
+ SingleBlockImportResult ,
13
+ ImporterProgressEvents
14
+ } from '../index.js'
8
15
import type { FileLayout , Reducer } from '../layout/index.js'
9
16
import type { CID , Version } from 'multiformats/cid'
10
17
import type { ProgressOptions , ProgressEvent } from 'progress-events'
@@ -14,11 +21,18 @@ interface BuildFileBatchOptions {
14
21
blockWriteConcurrency : number
15
22
}
16
23
17
- async function * buildFileBatch ( file : File , blockstore : WritableStorage , options : BuildFileBatchOptions ) : AsyncGenerator < InProgressImportResult > {
24
+ async function * buildFileBatch (
25
+ file : File ,
26
+ blockstore : WritableStorage ,
27
+ options : BuildFileBatchOptions
28
+ ) : AsyncGenerator < InProgressImportResult > {
18
29
let count = - 1
19
30
let previous : SingleBlockImportResult | undefined
20
31
21
- for await ( const entry of parallelBatch ( options . bufferImporter ( file , blockstore ) , options . blockWriteConcurrency ) ) {
32
+ for await ( const entry of parallelBatch (
33
+ options . bufferImporter ( file , blockstore ) ,
34
+ options . blockWriteConcurrency
35
+ ) ) {
22
36
count ++
23
37
24
38
if ( count === 0 ) {
@@ -29,7 +43,7 @@ async function * buildFileBatch (file: File, blockstore: WritableStorage, option
29
43
}
30
44
31
45
continue
32
- } else if ( count === 1 && ( previous != null ) ) {
46
+ } else if ( count === 1 && previous != null ) {
33
47
// we have the second block of a multiple block import so yield the first
34
48
yield {
35
49
...previous ,
@@ -63,8 +77,10 @@ export interface LayoutLeafProgress {
63
77
path ?: string
64
78
}
65
79
66
- export type ReducerProgressEvents =
67
- ProgressEvent < 'unixfs:importer:progress:file:layout' , LayoutLeafProgress >
80
+ export type ReducerProgressEvents = ProgressEvent <
81
+ 'unixfs:importer:progress:file:layout' ,
82
+ LayoutLeafProgress
83
+ >
68
84
69
85
interface ReduceOptions extends ProgressOptions < ImporterProgressEvents > {
70
86
reduceSingleLeafToSelf : boolean
@@ -76,13 +92,24 @@ function isSingleBlockImport (result: any): result is SingleBlockImportResult {
76
92
return result . single === true
77
93
}
78
94
79
- const reduce = ( file : File , blockstore : WritableStorage , options : ReduceOptions ) : Reducer => {
95
+ const reduce = (
96
+ file : File ,
97
+ blockstore : WritableStorage ,
98
+ options : ReduceOptions
99
+ ) : Reducer => {
80
100
const reducer : Reducer = async function ( leaves ) {
81
- if ( leaves . length === 1 && isSingleBlockImport ( leaves [ 0 ] ) && options . reduceSingleLeafToSelf ) {
101
+ if (
102
+ leaves . length === 1 &&
103
+ isSingleBlockImport ( leaves [ 0 ] ) &&
104
+ options . reduceSingleLeafToSelf
105
+ ) {
82
106
const leaf = leaves [ 0 ]
83
107
let node : Uint8Array | PBNode = leaf . block
84
108
85
- if ( isSingleBlockImport ( leaf ) && ( file . mtime !== undefined || file . mode !== undefined ) ) {
109
+ if (
110
+ isSingleBlockImport ( leaf ) &&
111
+ ( file . mtime !== undefined || file . mode !== undefined )
112
+ ) {
86
113
// only one leaf node which is a raw leaf - we have metadata so convert it into a
87
114
// UnixFS entry otherwise we'll have nowhere to store the metadata
88
115
leaf . unixfs = new UnixFS ( {
@@ -103,10 +130,15 @@ const reduce = (file: File, blockstore: WritableStorage, options: ReduceOptions)
103
130
leaf . size = BigInt ( leaf . block . length )
104
131
}
105
132
106
- options . onProgress ?.( new CustomProgressEvent < LayoutLeafProgress > ( 'unixfs:importer:progress:file:layout' , {
107
- cid : leaf . cid ,
108
- path : leaf . originalPath
109
- } ) )
133
+ options . onProgress ?.(
134
+ new CustomProgressEvent < LayoutLeafProgress > (
135
+ 'unixfs:importer:progress:file:layout' ,
136
+ {
137
+ cid : leaf . cid ,
138
+ path : leaf . originalPath
139
+ }
140
+ )
141
+ )
110
142
111
143
return {
112
144
cid : leaf . cid ,
@@ -125,12 +157,16 @@ const reduce = (file: File, blockstore: WritableStorage, options: ReduceOptions)
125
157
} )
126
158
127
159
const links : PBLink [ ] = leaves
128
- . filter ( leaf => {
160
+ . filter ( ( leaf ) => {
129
161
if ( leaf . cid . code === rawCodec . code && leaf . size > 0 ) {
130
162
return true
131
163
}
132
164
133
- if ( ( leaf . unixfs != null ) && ( leaf . unixfs . data == null ) && leaf . unixfs . fileSize ( ) > 0n ) {
165
+ if (
166
+ leaf . unixfs != null &&
167
+ leaf . unixfs . data == null &&
168
+ leaf . unixfs . fileSize ( ) > 0n
169
+ ) {
134
170
return true
135
171
}
136
172
@@ -148,7 +184,7 @@ const reduce = (file: File, blockstore: WritableStorage, options: ReduceOptions)
148
184
}
149
185
}
150
186
151
- if ( ( leaf . unixfs == null ) || ( leaf . unixfs . data == null ) ) {
187
+ if ( leaf . unixfs == null || leaf . unixfs . data == null ) {
152
188
// node is an intermediate node
153
189
f . addBlockSize ( leaf . unixfs ?. fileSize ( ) ?? 0n )
154
190
} else {
@@ -170,16 +206,24 @@ const reduce = (file: File, blockstore: WritableStorage, options: ReduceOptions)
170
206
const block = encode ( prepare ( node ) )
171
207
const cid = await persist ( block , blockstore , options )
172
208
173
- options . onProgress ?.( new CustomProgressEvent < LayoutLeafProgress > ( 'unixfs:importer:progress:file:layout' , {
174
- cid,
175
- path : file . originalPath
176
- } ) )
209
+ options . onProgress ?.(
210
+ new CustomProgressEvent < LayoutLeafProgress > (
211
+ 'unixfs:importer:progress:file:layout' ,
212
+ {
213
+ cid,
214
+ path : file . originalPath
215
+ }
216
+ )
217
+ )
177
218
178
219
return {
179
220
cid,
180
221
path : file . path ,
181
222
unixfs : f ,
182
- size : BigInt ( block . length + node . Links . reduce ( ( acc , curr ) => acc + ( curr . Tsize ?? 0 ) , 0 ) ) ,
223
+ size : BigInt (
224
+ block . length +
225
+ node . Links . reduce ( ( acc , curr ) => acc + ( curr . Tsize ?? 0 ) , 0 )
226
+ ) ,
183
227
originalPath : file . originalPath ,
184
228
block
185
229
}
@@ -188,10 +232,19 @@ const reduce = (file: File, blockstore: WritableStorage, options: ReduceOptions)
188
232
return reducer
189
233
}
190
234
191
- export interface FileBuilderOptions extends BuildFileBatchOptions , ReduceOptions {
235
+ export interface FileBuilderOptions
236
+ extends BuildFileBatchOptions ,
237
+ ReduceOptions {
192
238
layout : FileLayout
193
239
}
194
240
195
- export const fileBuilder = async ( file : File , block : WritableStorage , options : FileBuilderOptions ) : Promise < InProgressImportResult > => {
196
- return options . layout ( buildFileBatch ( file , block , options ) , reduce ( file , block , options ) )
197
- }
241
+ export const defaultFileBuilder = async (
242
+ file : File ,
243
+ block : WritableStorage ,
244
+ options : FileBuilderOptions
245
+ ) : Promise < InProgressImportResult > => {
246
+ return options . layout (
247
+ buildFileBatch ( file , block , options ) ,
248
+ reduce ( file , block , options )
249
+ ) ;
250
+ } ;
0 commit comments