Skip to content

Commit

Permalink
[HUBS] FileLoader: HTTP Range requests support (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirox committed Nov 1, 2022
1 parent c72f142 commit fc241c8
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/loaders/FileLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { Loader } from './Loader.js';

const loading = {};

class HttpError extends Error {

constructor( message, response ) {

super( message );
this.response = response;

}

}

class FileLoader extends Loader {

constructor( manager ) {
Expand All @@ -19,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 @@ -39,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 @@ -54,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 @@ -77,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 @@ -88,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 @@ -96,7 +116,7 @@ class FileLoader extends Loader {

}

const callbacks = loading[ url ];
const callbacks = loading[ key ];
const reader = response.body.getReader();
const contentLength = response.headers.get( 'Content-Length' );
const total = contentLength ? parseInt( contentLength ) : 0;
Expand Down Expand Up @@ -201,10 +221,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 @@ -218,7 +238,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 @@ -228,7 +248,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 fc241c8

Please sign in to comment.