Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions packages/duckdb-wasm/src/bindings/runtime_browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,17 @@ export const BROWSER_RUNTIME: DuckDBRuntime & {
xhr.responseType = 'arraybuffer';
xhr.setRequestHeader('Range', `bytes=0-0`);
xhr.send(null);
let actualContentLength = null;
try { actualContentLength = xhr.getResponseHeader('Content-Length'); } catch (e: any) {console.warn(`Failed to get Content-Length on request`);}
const contentRange = actualContentLength?.split('/')[1];
const contentLength2 = actualContentLength;

let contentLength = null;
try { contentLength = xhr.getResponseHeader('Content-Length'); } catch (e: any) {console.warn(`Failed to get Content-Length on request`);}

let contentRange = null;
try { contentRange = xhr.getResponseHeader('Content-Range'); } catch (e: any) {console.warn(`Failed to get Content-Range on request`);}
const fullLengthFromRange = contentRange?.split('/')[1] || null;

let presumedLength = null;
if (contentRange !== undefined) {
presumedLength = contentRange;
if (fullLengthFromRange !== null) {
presumedLength = fullLengthFromRange;
} else if (!file.reliableHeadRequests) {
// Send a dummy HEAD request with range protocol
// -> good IFF status is 206 and contentLenght is present
Expand All @@ -327,8 +330,8 @@ export const BROWSER_RUNTIME: DuckDBRuntime & {

if (
xhr.status == 206 &&
contentLength2 !== null &&
+contentLength2 == 1 &&
fullLengthFromRange !== null &&
+fullLengthFromRange == 1 &&
presumedLength !== null
) {
const result = mod._malloc(3 * 8);
Expand All @@ -341,9 +344,9 @@ export const BROWSER_RUNTIME: DuckDBRuntime & {
}
if (
xhr.status == 200 &&
contentLength2 !== null &&
fullLengthFromRange !== null &&
contentLength !== null &&
+contentLength2 == +contentLength
+fullLengthFromRange == +contentLength
) {
console.warn(`fall back to full HTTP read for: ${file.dataUrl}`);
const data = mod._malloc(xhr.response.byteLength);
Expand Down