@@ -13,16 +13,23 @@ const MAX_CHUNK_SIZE = 2 * TARGET_CHUNK_SIZE;
13
13
const XORB_SIZE = 64 * 1024 * 1024 ;
14
14
const MAX_XORB_CHUNKS = 8 * 1024 ;
15
15
16
- export async function * createXorbs ( fileSources : AsyncGenerator < Blob > ) : AsyncGenerator <
16
+ export async function * createXorbs (
17
+ fileSources : AsyncGenerator < { content : Blob ; path : string ; sha256 : string } >
18
+ ) : AsyncGenerator <
17
19
| {
18
20
type : "xorb" ;
19
21
xorb : Uint8Array ;
20
22
hash : string ;
21
23
id : number ;
22
24
chunks : Array < { hash : string ; length : number ; offset : number } > ;
25
+ files : Array < {
26
+ path : string ;
27
+ progress : number ;
28
+ } > ;
23
29
}
24
30
| {
25
31
type : "file" ;
32
+ path : string ;
26
33
hash : string ;
27
34
sha256 : string ;
28
35
representation : Array < {
@@ -38,7 +45,6 @@ export async function* createXorbs(fileSources: AsyncGenerator<Blob>): AsyncGene
38
45
undefined
39
46
> {
40
47
const chunkModule = await import ( "../vendor/xet-chunk/chunker_wasm" ) ;
41
- const sha256Module = await import ( "../vendor/hash-wasm/sha256-wrapper" ) ;
42
48
let xorbId = 0 ;
43
49
44
50
await chunkModule . init ( ) ;
@@ -47,13 +53,15 @@ export async function* createXorbs(fileSources: AsyncGenerator<Blob>): AsyncGene
47
53
let xorb = new Uint8Array ( XORB_SIZE ) ;
48
54
let xorbOffset = 0 ;
49
55
let xorbChunks = Array < { hash : string ; length : number ; offset : number } > ( ) ;
56
+ let xorbFiles : Record < string , number > = { } ;
50
57
51
58
try {
52
59
for await ( const fileSource of fileSources ) {
53
60
const initialXorbOffset = xorbOffset ;
54
61
const sourceChunks : Array < Uint8Array > = [ ] ;
55
62
56
- const reader = fileSource . stream ( ) . getReader ( ) ;
63
+ const reader = fileSource . content . stream ( ) . getReader ( ) ;
64
+ let processedBytes = 0 ;
57
65
const fileChunks : Array < { hash : string ; length : number } > = [ ] ;
58
66
let currentChunkRangeBeginning = 0 ;
59
67
const fileRepresentation : Array < {
@@ -64,9 +72,6 @@ export async function* createXorbs(fileSources: AsyncGenerator<Blob>): AsyncGene
64
72
rangeHash : string ;
65
73
} > = [ ] ;
66
74
67
- const sha256 = await sha256Module . createSHA256 ( ) ;
68
- sha256 . init ( ) ;
69
-
70
75
const addChunks = function * ( chunks : Array < { hash : string ; length : number } > ) {
71
76
for ( const chunk of chunks ) {
72
77
let chunkOffset = xorbOffset ;
@@ -98,11 +103,13 @@ export async function* createXorbs(fileSources: AsyncGenerator<Blob>): AsyncGene
98
103
hash : chunkModule . compute_xorb_hash ( xorbChunks ) ,
99
104
chunks : [ ...xorbChunks ] ,
100
105
id : xorbId ,
106
+ files : Object . entries ( xorbFiles ) . map ( ( [ path , progress ] ) => ( { path, progress } ) ) ,
101
107
} ;
102
108
xorbId ++ ;
103
109
xorb = new Uint8Array ( XORB_SIZE ) ;
104
110
chunkOffset = 0 ;
105
111
xorbOffset = writeChunk ( xorb , 0 , chunkToCopy ) ;
112
+ xorbFiles = { } ;
106
113
107
114
if ( xorbOffset === 0 ) {
108
115
throw new Error ( "Failed to write chunk into xorb" ) ;
@@ -138,17 +145,20 @@ export async function* createXorbs(fileSources: AsyncGenerator<Blob>): AsyncGene
138
145
}
139
146
}
140
147
xorbChunks . push ( { hash : chunk . hash , length : chunk . length , offset : chunkOffset } ) ;
148
+ xorbFiles [ fileSource . path ] = processedBytes / fileSource . content . size ;
141
149
if ( xorbChunks . length >= MAX_XORB_CHUNKS ) {
142
150
yield {
143
151
type : "xorb" as const ,
144
152
xorb : xorb . subarray ( 0 , xorbOffset ) ,
145
153
hash : chunkModule . compute_xorb_hash ( xorbChunks ) ,
146
154
chunks : [ ...xorbChunks ] ,
147
155
id : xorbId ,
156
+ files : Object . entries ( xorbFiles ) . map ( ( [ path , progress ] ) => ( { path, progress } ) ) ,
148
157
} ;
149
158
xorbId ++ ;
150
159
xorbOffset = 0 ;
151
160
xorbChunks = [ ] ;
161
+ xorbFiles = { } ;
152
162
xorb = new Uint8Array ( XORB_SIZE ) ;
153
163
}
154
164
}
@@ -160,8 +170,8 @@ export async function* createXorbs(fileSources: AsyncGenerator<Blob>): AsyncGene
160
170
yield * addChunks ( chunker . finish ( ) ) ;
161
171
break ;
162
172
}
173
+ processedBytes += value . length ;
163
174
sourceChunks . push ( value ) ;
164
- sha256 . update ( value ) ;
165
175
yield * addChunks ( chunker . add_data ( value ) ) ;
166
176
}
167
177
@@ -174,9 +184,10 @@ export async function* createXorbs(fileSources: AsyncGenerator<Blob>): AsyncGene
174
184
175
185
yield {
176
186
type : "file" as const ,
187
+ path : fileSource . path ,
177
188
hash : chunkModule . compute_file_hash ( fileChunks ) ,
178
- sha256 : sha256 . digest ( "hex" ) ,
179
189
representation : fileRepresentation ,
190
+ sha256 : fileSource . sha256 ,
180
191
} ;
181
192
}
182
193
} finally {
0 commit comments