Skip to content

Commit

Permalink
[HUBS] [Upcoming] FileLoader: HTTP Range requests support (#68)
Browse files Browse the repository at this point in the history
[HUBS]
This change is needed for glTF LOD progressive loading.
This change is not merged to the official Three.js PR yet
but likely it can get in soon.

mrdoob#24580

We can remove this commit if the PR is merged to
the official Three.js and we upgrade our Three.js fork to
the one including the change.
  • Loading branch information
takahirox committed Dec 7, 2022
1 parent f282aec commit 98e13ff
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/loaders/FileLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class FileLoader extends Loader {

url = this.manager.resolveURL( url );

const cached = Cache.get( url );
const isRangeRequest = this.requestHeader.Range !== undefined;
const key = url + ( isRangeRequest ? `:${this.requestHeader.Range}` : '' );

const cached = Cache.get( key );

if ( cached !== undefined ) {

Expand All @@ -50,9 +53,9 @@ class FileLoader extends Loader {

// Check if request is duplicate

if ( loading[ url ] !== undefined ) {
if ( loading[ key ] !== undefined ) {

loading[ url ].push( {
loading[ key ].push( {

onLoad: onLoad,
onProgress: onProgress,
Expand All @@ -65,9 +68,9 @@ class FileLoader extends Loader {
}

// Initialise array for duplicate requests
loading[ url ] = [];
loading[ key ] = [];

loading[ url ].push( {
loading[ key ].push( {
onLoad: onLoad,
onProgress: onProgress,
onError: onError,
Expand All @@ -88,7 +91,7 @@ class FileLoader extends Loader {
fetch( req )
.then( response => {

if ( response.status === 200 || response.status === 0 ) {
if ( response.status === 200 || response.status === 206 || response.status === 0 ) {

// Some browsers return HTTP Status 0 when using non-http protocol
// e.g. 'file://' or 'data://'. Handle as success.
Expand All @@ -99,6 +102,12 @@ class FileLoader extends Loader {

}

if ( isRangeRequest && response.status === 200 ) {

throw new HttpError( `range request fetch for "${response.url}" responded with ${response.status}: ${response.statusText}`, response );

}

// Workaround: Checking if response.body === undefined for Alipay browser #23548

if ( typeof ReadableStream === 'undefined' || response.body === undefined || response.body.getReader === undefined ) {
Expand All @@ -107,7 +116,7 @@ class FileLoader extends Loader {

}

const callbacks = loading[ url ];
const callbacks = loading[ key ];
const reader = response.body.getReader();

// Nginx needs X-File-Size check
Expand Down Expand Up @@ -215,10 +224,10 @@ class FileLoader extends Loader {

// Add to cache only on HTTP success, so that we do not cache
// error response bodies as proper responses to requests.
Cache.add( url, data );
Cache.add( key, data );

const callbacks = loading[ url ];
delete loading[ url ];
const callbacks = loading[ key ];
delete loading[ key ];

for ( let i = 0, il = callbacks.length; i < il; i ++ ) {

Expand All @@ -232,7 +241,7 @@ class FileLoader extends Loader {

// Abort errors and other errors are handled the same

const callbacks = loading[ url ];
const callbacks = loading[ key ];

if ( callbacks === undefined ) {

Expand All @@ -242,7 +251,7 @@ class FileLoader extends Loader {

}

delete loading[ url ];
delete loading[ key ];

for ( let i = 0, il = callbacks.length; i < il; i ++ ) {

Expand Down

0 comments on commit 98e13ff

Please sign in to comment.