| 142 | chunk.status === ChunkStatus.QUEUED || chunk.status === ChunkStatus.ERROR; |
| 143 | |
| 144 | const processChunk = chunk => { |
| 145 | // processing is paused, wait here |
| 146 | if (state.aborted) return; |
| 147 | |
| 148 | // get next chunk to process |
| 149 | chunk = chunk || chunks.find(canProcessChunk); |
| 150 | |
| 151 | // no more chunks to process |
| 152 | if (!chunk) { |
| 153 | // all done? |
| 154 | if (chunks.every(chunk => chunk.status === ChunkStatus.COMPLETE)) { |
| 155 | completeProcessingChunks(); |
| 156 | } |
| 157 | |
| 158 | // no chunk to handle |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | // now processing this chunk |
| 163 | chunk.status = ChunkStatus.PROCESSING; |
| 164 | chunk.progress = null; |
| 165 | |
| 166 | // allow parsing of formdata |
| 167 | const ondata = chunkServer.ondata || (fd => fd); |
| 168 | const onerror = chunkServer.onerror || (res => null); |
| 169 | const onload = chunkServer.onload || (() => {}); |
| 170 | |
| 171 | // send request object |
| 172 | const requestUrl = buildURL(apiUrl, chunkServer.url, state.serverId); |
| 173 | |
| 174 | const headers = |
| 175 | typeof chunkServer.headers === 'function' |
| 176 | ? chunkServer.headers(chunk) |
| 177 | : { |
| 178 | ...chunkServer.headers, |
| 179 | 'Content-Type': 'application/offset+octet-stream', |
| 180 | 'Upload-Offset': chunk.offset, |
| 181 | 'Upload-Length': file.size, |
| 182 | 'Upload-Name': file.name, |
| 183 | }; |
| 184 | |
| 185 | const request = (chunk.request = sendRequest(ondata(chunk.data), requestUrl, { |
| 186 | ...chunkServer, |
| 187 | headers, |
| 188 | })); |
| 189 | |
| 190 | request.onload = xhr => { |
| 191 | // allow hooking into request result |
| 192 | onload(xhr, chunk.index, chunks.length); |
| 193 | |
| 194 | // done! |
| 195 | chunk.status = ChunkStatus.COMPLETE; |
| 196 | |
| 197 | // remove request reference |
| 198 | chunk.request = null; |
| 199 | |
| 200 | // start processing more chunks |
| 201 | processChunks(); |
no test coverage detected