From 17065c382b21a688c642fe9cc1571c9f05bad70c Mon Sep 17 00:00:00 2001 From: Carlo Piovesan Date: Wed, 5 Jun 2024 07:12:59 +0200 Subject: [PATCH] PROTOTYPE: Avoid HEAD requests that might produce unreliable output --- .../src/bindings/runtime_browser.ts | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/duckdb-wasm/src/bindings/runtime_browser.ts b/packages/duckdb-wasm/src/bindings/runtime_browser.ts index 5371dd33e..b3375e57a 100644 --- a/packages/duckdb-wasm/src/bindings/runtime_browser.ts +++ b/packages/duckdb-wasm/src/bindings/runtime_browser.ts @@ -159,6 +159,7 @@ export const BROWSER_RUNTIME: DuckDBRuntime & { // Supports ranges? let contentLength = null; let error: any | null = null; + if (!file.allowFullHttpReads) { try { // Send a dummy HEAD request with range protocol // -> good IFF status is 206 and contentLenght is present @@ -185,6 +186,7 @@ export const BROWSER_RUNTIME: DuckDBRuntime & { error = e; console.warn(`HEAD request with range header failed: ${e}`); } + } // Try to fallback to full read? if (file.allowFullHttpReads) { @@ -209,8 +211,24 @@ export const BROWSER_RUNTIME: DuckDBRuntime & { let presumedLength = null; if (contentRange !== undefined) { presumedLength = contentRange; - } else if (contentLength !== null && +contentLength > 1) { - presumedLength = contentLength; + } else { + // Send a dummy HEAD request with range protocol + // -> good IFF status is 206 and contentLenght is present + const head = new XMLHttpRequest(); + if (file.dataProtocol == DuckDBDataProtocol.S3) { + head.open('HEAD', getHTTPUrl(file.s3Config, file.dataUrl!), false); + addS3Headers(head, file.s3Config, file.dataUrl!, 'HEAD'); + } else { + head.open('HEAD', file.dataUrl!, false); + } + head.setRequestHeader('Range', `bytes=0-`); + head.send(null); + + // Supports range requests + contentLength = head.getResponseHeader('Content-Length'); + if (contentLength !== null && +contentLength > 1) { + presumedLength = contentLength; + } } if (xhr.status == 206 && contentLength2 !== null && +contentLength2 == 1 && presumedLength !== null) {