@@ -2,26 +2,46 @@ import { UnixFS } from 'ipfs-unixfs'
22import { persist , PersistOptions } from '../utils/persist.js'
33import * as dagPb from '@ipld/dag-pb'
44import * as raw from 'multiformats/codecs/raw'
5- import type { BufferImporter , ProgressHandler } from '../index.js'
5+ import type { BufferImporter } from '../index.js'
66import type { Version } from 'multiformats/cid'
7+ import { CustomProgressEvent } from 'progress-events'
8+ import type { ProgressOptions , ProgressEvent } from 'progress-events'
79
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 > {
929 cidVersion : Version
1030 rawLeaves : boolean
1131 leafType : 'file' | 'raw'
12- progress ?: ProgressHandler
1332}
1433
1534export function defaultBufferImporter ( options : BufferImporterOptions ) : BufferImporter {
1635 return async function * bufferImporter ( file , block ) {
1736 for await ( let buffer of file . content ) {
1837 yield async ( ) => {
19- options . progress ?.( buffer . length , file . path )
38+ options . onProgress ?.( new CustomProgressEvent < ImportProgressData > ( 'unixfs:importer:progress' , { bytes : buffer . length , path : file . path } ) )
2039 let unixfs
2140
2241 const opts : PersistOptions = {
2342 codec : dagPb ,
24- cidVersion : options . cidVersion
43+ cidVersion : options . cidVersion ,
44+ onProgress : options . onProgress
2545 }
2646
2747 if ( options . rawLeaves ) {
0 commit comments