@@ -39,21 +39,19 @@ addToLibrary({
39
39
offset ??= 0 ;
40
40
len ??= chunkSize ;
41
41
// In which chunk does the seeked range start? E.g., 5-14 with chunksize 8 will start in chunk 0.
42
- var firstChunk = ( offset / chunkSize ) | 0 ;
43
- // In which chunk does the seeked range end? E.g., 5-14 with chunksize 8 will end in chunk 1, as will 5-16 (since byte 16 isn't requested).
44
- // This will always give us a chunk >= firstChunk since len > 0.
45
- var lastChunk = ( ( offset + len - 1 ) / chunkSize ) | 0 ;
46
42
if ( ! ( file in wasmFS$JSMemoryRanges ) ) {
47
43
var fileInfo = await fetch ( url , { method :'HEAD' , headers :{ 'Range' : 'bytes=0-' } } ) ;
48
44
if ( fileInfo . ok &&
49
45
fileInfo . headers . has ( 'Content-Length' ) &&
50
46
fileInfo . headers . get ( 'Accept-Ranges' ) == 'bytes' &&
51
47
( parseInt ( fileInfo . headers . get ( 'Content-Length' ) , 10 ) > chunkSize * 2 ) ) {
48
+ var size = parseInt ( fileInfo . headers . get ( 'Content-Length' ) , 10 ) ;
52
49
wasmFS$JSMemoryRanges [ file ] = {
53
- size : parseInt ( fileInfo . headers . get ( 'Content-Length' ) , 10 ) ,
50
+ size,
54
51
chunks : [ ] ,
55
52
chunkSize : chunkSize
56
53
} ;
54
+ len = Math . min ( len , size - offset + 1 ) | 0 ;
57
55
} else {
58
56
// may as well/forced to download the whole file
59
57
var wholeFileReq = await fetch ( url ) ;
@@ -70,6 +68,10 @@ addToLibrary({
70
68
return Promise . resolve ( ) ;
71
69
}
72
70
}
71
+ var firstChunk = ( offset / chunkSize ) | 0 ;
72
+ // In which chunk does the seeked range end? E.g., 5-14 with chunksize 8 will end in chunk 1, as will 5-16 (since byte 16 isn't requested).
73
+ // This will always give us a chunk >= firstChunk since len > 0.
74
+ var lastChunk = ( ( offset + len - 1 ) / chunkSize ) | 0 ;
73
75
var allPresent = true ;
74
76
var i ;
75
77
// Do we have all the chunks already? If so, we don't need to do any fetches.
@@ -120,7 +122,10 @@ addToLibrary({
120
122
121
123
// read/getSize fetch the data, then forward to the parent class.
122
124
read : async ( file , buffer , length , offset ) => {
123
- if ( length == 0 ) {
125
+ // This function assumes that offset is non-negative and length is positive.
126
+ // C read() doesn't take an offset and so doesn't have to deal with the former situation,
127
+ // and if the length is 0 or the offset is negative there's no reasonable read we can make.
128
+ if ( offset < 0 || length <= 0 ) {
124
129
return 0 ;
125
130
}
126
131
try {
@@ -129,6 +134,11 @@ addToLibrary({
129
134
return failedResponse . status === 404 ? - { { { cDefs . ENOENT } } } : - { { { cDefs. EBADF } } } ;
130
135
}
131
136
var fileInfo = wasmFS$JSMemoryRanges [ file ] ;
137
+ length = Math . min ( length , fileInfo . size - offset + 1 ) | 0 ;
138
+ // As above, we check the length just in case offset was beyond size and length is now negative.
139
+ if ( length <= 0 ) {
140
+ return 0 ;
141
+ }
132
142
var chunks = fileInfo . chunks ;
133
143
var chunkSize = fileInfo . chunkSize ;
134
144
var firstChunk = ( offset / chunkSize ) | 0 ;
0 commit comments