@@ -4,14 +4,7 @@ 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 {
8
- BufferImporter ,
9
- File ,
10
- InProgressImportResult ,
11
- WritableStorage ,
12
- SingleBlockImportResult ,
13
- ImporterProgressEvents
14
- } from '../index.js'
7
+ import type { BufferImporter , File , InProgressImportResult , WritableStorage , SingleBlockImportResult , ImporterProgressEvents } from '../index.js'
15
8
import type { FileLayout , Reducer } from '../layout/index.js'
16
9
import type { PBLink , PBNode } from '@ipld/dag-pb'
17
10
import type { CID , Version } from 'multiformats/cid'
@@ -22,18 +15,11 @@ interface BuildFileBatchOptions {
22
15
blockWriteConcurrency : number
23
16
}
24
17
25
- async function * buildFileBatch (
26
- file : File ,
27
- blockstore : WritableStorage ,
28
- options : BuildFileBatchOptions
29
- ) : AsyncGenerator < InProgressImportResult > {
18
+ async function * buildFileBatch ( file : File , blockstore : WritableStorage , options : BuildFileBatchOptions ) : AsyncGenerator < InProgressImportResult > {
30
19
let count = - 1
31
20
let previous : SingleBlockImportResult | undefined
32
21
33
- for await ( const entry of parallelBatch (
34
- options . bufferImporter ( file , blockstore ) ,
35
- options . blockWriteConcurrency
36
- ) ) {
22
+ for await ( const entry of parallelBatch ( options . bufferImporter ( file , blockstore ) , options . blockWriteConcurrency ) ) {
37
23
count ++
38
24
39
25
if ( count === 0 ) {
@@ -78,10 +64,8 @@ export interface LayoutLeafProgress {
78
64
path ?: string
79
65
}
80
66
81
- export type ReducerProgressEvents = ProgressEvent <
82
- 'unixfs:importer:progress:file:layout' ,
83
- LayoutLeafProgress
84
- >
67
+ export type ReducerProgressEvents =
68
+ ProgressEvent < 'unixfs:importer:progress:file:layout' , LayoutLeafProgress >
85
69
86
70
interface ReduceOptions extends ProgressOptions < ImporterProgressEvents > {
87
71
reduceSingleLeafToSelf : boolean
@@ -93,24 +77,13 @@ function isSingleBlockImport (result: any): result is SingleBlockImportResult {
93
77
return result . single === true
94
78
}
95
79
96
- const reduce = (
97
- file : File ,
98
- blockstore : WritableStorage ,
99
- options : ReduceOptions
100
- ) : Reducer => {
80
+ const reduce = ( file : File , blockstore : WritableStorage , options : ReduceOptions ) : Reducer => {
101
81
const reducer : Reducer = async function ( leaves ) {
102
- if (
103
- leaves . length === 1 &&
104
- isSingleBlockImport ( leaves [ 0 ] ) &&
105
- options . reduceSingleLeafToSelf
106
- ) {
82
+ if ( leaves . length === 1 && isSingleBlockImport ( leaves [ 0 ] ) && options . reduceSingleLeafToSelf ) {
107
83
const leaf = leaves [ 0 ]
108
84
let node : Uint8Array | PBNode = leaf . block
109
85
110
- if (
111
- isSingleBlockImport ( leaf ) &&
112
- ( file . mtime !== undefined || file . mode !== undefined )
113
- ) {
86
+ if ( isSingleBlockImport ( leaf ) && ( file . mtime !== undefined || file . mode !== undefined ) ) {
114
87
// only one leaf node which is a raw leaf - we have metadata so convert it into a
115
88
// UnixFS entry otherwise we'll have nowhere to store the metadata
116
89
leaf . unixfs = new UnixFS ( {
@@ -132,13 +105,10 @@ const reduce = (
132
105
}
133
106
134
107
options . onProgress ?.(
135
- new CustomProgressEvent < LayoutLeafProgress > (
136
- 'unixfs:importer:progress:file:layout' ,
137
- {
138
- cid : leaf . cid ,
139
- path : leaf . originalPath
140
- }
141
- )
108
+ new CustomProgressEvent < LayoutLeafProgress > ( 'unixfs:importer:progress:file:layout' , {
109
+ cid : leaf . cid ,
110
+ path : leaf . originalPath
111
+ } )
142
112
)
143
113
144
114
return {
@@ -163,11 +133,7 @@ const reduce = (
163
133
return true
164
134
}
165
135
166
- if (
167
- leaf . unixfs != null &&
168
- leaf . unixfs . data == null &&
169
- leaf . unixfs . fileSize ( ) > 0n
170
- ) {
136
+ if ( leaf . unixfs != null && leaf . unixfs . data == null && leaf . unixfs . fileSize ( ) > 0n ) {
171
137
return true
172
138
}
173
139
@@ -208,13 +174,10 @@ const reduce = (
208
174
const cid = await persist ( block , blockstore , options )
209
175
210
176
options . onProgress ?.(
211
- new CustomProgressEvent < LayoutLeafProgress > (
212
- 'unixfs:importer:progress:file:layout' ,
213
- {
214
- cid,
215
- path : file . originalPath
216
- }
217
- )
177
+ new CustomProgressEvent < LayoutLeafProgress > ( 'unixfs:importer:progress:file:layout' , {
178
+ cid,
179
+ path : file . originalPath
180
+ } )
218
181
)
219
182
220
183
return {
@@ -234,24 +197,14 @@ const reduce = (
234
197
}
235
198
236
199
export interface FileBuilder {
237
- (
238
- file : File ,
239
- blockstore : WritableStorage ,
240
- options : FileBuilderOptions
241
- ) : Promise < InProgressImportResult >
200
+ ( file : File , blockstore : WritableStorage , options : FileBuilderOptions ) : Promise < InProgressImportResult >
242
201
}
243
202
244
- export interface FileBuilderOptions
245
- extends BuildFileBatchOptions ,
246
- ReduceOptions {
203
+ export interface FileBuilderOptions extends BuildFileBatchOptions , ReduceOptions {
247
204
layout : FileLayout
248
205
}
249
206
250
- export const defaultFileBuilder = async (
251
- file : File ,
252
- block : WritableStorage ,
253
- options : FileBuilderOptions
254
- ) : Promise < InProgressImportResult > => {
207
+ export const defaultFileBuilder : FileBuilder = async ( file : File , block : WritableStorage , options : FileBuilderOptions ) : Promise < InProgressImportResult > => {
255
208
return options . layout (
256
209
buildFileBatch ( file , block , options ) ,
257
210
reduce ( file , block , options )
0 commit comments