| 2905 | this.getter = getter; |
| 2906 | } |
| 2907 | cacheLength() { |
| 2908 | // Find length |
| 2909 | var xhr = new XMLHttpRequest(); |
| 2910 | xhr.open('HEAD', url, false); |
| 2911 | xhr.send(null); |
| 2912 | if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) abort("Couldn't load " + url + ". Status: " + xhr.status); |
| 2913 | var datalength = Number(xhr.getResponseHeader("Content-length")); |
| 2914 | var header; |
| 2915 | var hasByteServing = (header = xhr.getResponseHeader("Accept-Ranges")) && header === "bytes"; |
| 2916 | var usesGzip = (header = xhr.getResponseHeader("Content-Encoding")) && header === "gzip"; |
| 2917 | |
| 2918 | var chunkSize = 1024*1024; // Chunk size in bytes |
| 2919 | |
| 2920 | if (!hasByteServing) chunkSize = datalength; |
| 2921 | |
| 2922 | // Function to get a range from the remote URL. |
| 2923 | var doXHR = (from, to) => { |
| 2924 | if (from > to) abort("invalid range (" + from + ", " + to + ") or no bytes requested!"); |
| 2925 | if (to > datalength-1) abort("only " + datalength + " bytes available! programmer error!"); |
| 2926 | |
| 2927 | // TODO: Use mozResponseArrayBuffer, responseStream, etc. if available. |
| 2928 | var xhr = new XMLHttpRequest(); |
| 2929 | xhr.open('GET', url, false); |
| 2930 | if (datalength !== chunkSize) xhr.setRequestHeader("Range", "bytes=" + from + "-" + to); |
| 2931 | |
| 2932 | // Some hints to the browser that we want binary data. |
| 2933 | xhr.responseType = 'arraybuffer'; |
| 2934 | if (xhr.overrideMimeType) { |
| 2935 | xhr.overrideMimeType('text/plain; charset=x-user-defined'); |
| 2936 | } |
| 2937 | |
| 2938 | xhr.send(null); |
| 2939 | if (!(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304)) abort("Couldn't load " + url + ". Status: " + xhr.status); |
| 2940 | if (xhr.response !== undefined) { |
| 2941 | return new Uint8Array(/** @type{Array<number>} */(xhr.response || [])); |
| 2942 | } |
| 2943 | return intArrayFromString(xhr.responseText || '', true); |
| 2944 | }; |
| 2945 | var lazyArray = this; |
| 2946 | lazyArray.setDataGetter((chunkNum) => { |
| 2947 | var start = chunkNum * chunkSize; |
| 2948 | var end = (chunkNum+1) * chunkSize - 1; // including this byte |
| 2949 | end = Math.min(end, datalength-1); // if datalength-1 is selected, this is the last block |
| 2950 | if (typeof lazyArray.chunks[chunkNum] == 'undefined') { |
| 2951 | lazyArray.chunks[chunkNum] = doXHR(start, end); |
| 2952 | } |
| 2953 | if (typeof lazyArray.chunks[chunkNum] == 'undefined') abort('doXHR failed!'); |
| 2954 | return lazyArray.chunks[chunkNum]; |
| 2955 | }); |
| 2956 | |
| 2957 | if (usesGzip || !datalength) { |
| 2958 | // if the server uses gzip or doesn't supply the length, we have to download the whole file to get the (uncompressed) length |
| 2959 | chunkSize = datalength = 1; // this will force getter(0)/doXHR do download the whole file |
| 2960 | datalength = this.getter(0).length; |
| 2961 | chunkSize = datalength; |
| 2962 | out("LazyFiles on gzip forces download of the whole file when length is accessed"); |
| 2963 | } |
| 2964 | |