@@ -2,26 +2,46 @@ import { UnixFS } from 'ipfs-unixfs'
2
2
import { persist , PersistOptions } from '../utils/persist.js'
3
3
import * as dagPb from '@ipld/dag-pb'
4
4
import * as raw from 'multiformats/codecs/raw'
5
- import type { BufferImporter , ProgressHandler } from '../index.js'
5
+ import type { BufferImporter } from '../index.js'
6
6
import type { Version } from 'multiformats/cid'
7
+ import { CustomProgressEvent } from 'progress-events'
8
+ import type { ProgressOptions , ProgressEvent } from 'progress-events'
7
9
8
- export interface BufferImporterOptions {
10
+ /**
11
+ * Passed to the onProgress callback while importing files
12
+ */
13
+ export interface ImportProgressData {
14
+ /**
15
+ * The size of the current chunk
16
+ */
17
+ bytes : number
18
+
19
+ /**
20
+ * The path of the file being imported, if one was specified
21
+ */
22
+ path ?: string
23
+ }
24
+
25
+ export type BufferImportProgressEvents =
26
+ ProgressEvent < 'unixfs:importer:progress' , ImportProgressData >
27
+
28
+ export interface BufferImporterOptions extends ProgressOptions < BufferImportProgressEvents > {
9
29
cidVersion : Version
10
30
rawLeaves : boolean
11
31
leafType : 'file' | 'raw'
12
- progress ?: ProgressHandler
13
32
}
14
33
15
34
export function defaultBufferImporter ( options : BufferImporterOptions ) : BufferImporter {
16
35
return async function * bufferImporter ( file , block ) {
17
36
for await ( let buffer of file . content ) {
18
37
yield async ( ) => {
19
- options . progress ?.( buffer . length , file . path )
38
+ options . onProgress ?.( new CustomProgressEvent < ImportProgressData > ( 'unixfs:importer:progress' , { bytes : buffer . length , path : file . path } ) )
20
39
let unixfs
21
40
22
41
const opts : PersistOptions = {
23
42
codec : dagPb ,
24
- cidVersion : options . cidVersion
43
+ cidVersion : options . cidVersion ,
44
+ onProgress : options . onProgress
25
45
}
26
46
27
47
if ( options . rawLeaves ) {
0 commit comments