Skip to content

Commit 2f683f3

Browse files
committed
Try fix content length error in github example (#39)
1 parent 37cc199 commit 2f683f3

4 files changed

Lines changed: 20 additions & 8 deletions

File tree

dist/spl-node.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/spl-web.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pre.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,30 @@ class XHR {
8282
}
8383

8484
size() {
85+
// Use a Range request to get the actual file size from Content-Range header.
86+
// This avoids issues with Content-Length returning compressed size on some CDNs.
8587
let retry = 0;
8688
let size = -1;
8789
this.xhr.onload = () => {
88-
size = +this.xhr.getResponseHeader('Content-Length');
90+
const range = this.xhr.getResponseHeader('Content-Range');
91+
if (range) {
92+
// Content-Range: bytes 0-99/1234567 -> extract total size after '/'
93+
const match = range.match(/\/(\d+)/);
94+
if (match) {
95+
size = +match[1];
96+
}
97+
}
98+
if (size < 0) {
99+
// Fallback to Content-Length if Content-Range is not available
100+
size = +this.xhr.getResponseHeader('Content-Length');
101+
}
89102
};
90103
do {
91104
retry += 1;
92-
this.xhr.open('HEAD', this.url, false);
93-
this.xhr.setRequestHeader('Accept-Encoding', 'identity')
105+
this.xhr.open('GET', this.url, false);
106+
this.xhr.setRequestHeader('Range', 'bytes=0-0');
94107
this.xhr.send(null);
95-
} while (retry < 3 && this.xhr.status != 200)
108+
} while (retry < 3 && this.xhr.status != 206 && this.xhr.status != 200);
96109
this._size = size;
97110
return size;
98111
}
@@ -121,7 +134,6 @@ class XHR {
121134
do {
122135
retry += 1;
123136
this.xhr.open('GET', this.url, false);
124-
this.xhr.setRequestHeader('Accept-Encoding', 'identity')
125137
this.xhr.setRequestHeader('Range', `bytes=${pos}-${Math.min(this._size - 1, pos + len - 1)}`);
126138
this.xhr.send(null);
127139
} while (retry < 3 && this.xhr.status != 206);

0 commit comments

Comments
 (0)