Add workaround for Chromium range request caching bug

This commit is contained in:
Vanilagy
2025-08-11 18:14:02 +02:00
parent 6d447660c2
commit 1492fd5c6f
5 changed files with 40 additions and 10 deletions
+10
View File
@@ -8,6 +8,15 @@
document.body.append(fileInput); document.body.append(fileInput);
fileInput.addEventListener('change', async () => { fileInput.addEventListener('change', async () => {
const videoUrl = "https://upload.wikimedia.org/wikipedia/commons/5/53/1941._%D0%9A%D0%BE%D0%BD%D1%91%D0%BA-%D0%B3%D0%BE%D1%80%D0%B1%D1%83%D0%BD%D0%BE%D0%BA.webm"
const source = new Mediabunny.UrlSource(videoUrl)
const input = new Mediabunny.Input({ formats: Mediabunny.ALL_FORMATS, source });
const videoTrack = await input.getPrimaryVideoTrack();
console.log(videoTrack);
/*
const file = fileInput.files[0]; const file = fileInput.files[0];
const source = new Mediabunny.BlobSource(file); const source = new Mediabunny.BlobSource(file);
@@ -20,6 +29,7 @@
const sink = new Mediabunny.EncodedPacketSink(audioTrack); const sink = new Mediabunny.EncodedPacketSink(audioTrack);
console.log(await sink.getPacket(100)) console.log(await sink.getPacket(100))
*/
/* /*
for await (const packet of sink.packets()) { for await (const packet of sink.packets()) {
+6 -6
View File
@@ -1,12 +1,12 @@
{ {
"name": "mediabunny", "name": "mediabunny",
"version": "1.7.0", "version": "1.7.1",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "mediabunny", "name": "mediabunny",
"version": "1.7.0", "version": "1.7.1",
"license": "MPL-2.0", "license": "MPL-2.0",
"workspaces": [ "workspaces": [
"packages/*" "packages/*"
@@ -5900,9 +5900,9 @@
} }
}, },
"node_modules/mediabunny": { "node_modules/mediabunny": {
"version": "1.6.2", "version": "1.7.0",
"resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.6.2.tgz", "resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.7.0.tgz",
"integrity": "sha512-c0z6lfeYHYk5M6V9kLaXD/7HqW8UjjsDhoZUwLConxHCrL/pPMnZwX5tXjXz/T8DIeuXYYbjYR9X43LnEmWacA==", "integrity": "sha512-QcTdptOtvjAHb4KQpgWWWHRS0+DgGwTXelTwPWaZwQu85iYy11bzsLS4e29rfH4nLL/eq8K/dBsjj1LuApc2Qw==",
"license": "MPL-2.0", "license": "MPL-2.0",
"peer": true, "peer": true,
"workspaces": [ "workspaces": [
@@ -9017,7 +9017,7 @@
}, },
"packages/mp3-encoder": { "packages/mp3-encoder": {
"name": "@mediabunny/mp3-encoder", "name": "@mediabunny/mp3-encoder",
"version": "1.7.0", "version": "1.7.1",
"license": "MPL-2.0", "license": "MPL-2.0",
"devDependencies": { "devDependencies": {
"@types/emscripten": "^1.40.1" "@types/emscripten": "^1.40.1"
+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"name": "mediabunny", "name": "mediabunny",
"author": "Vanilagy", "author": "Vanilagy",
"version": "1.7.0", "version": "1.7.1",
"description": "Pure TypeScript media toolkit for reading, writing, and converting media files, directly in the browser.", "description": "Pure TypeScript media toolkit for reading, writing, and converting media files, directly in the browser.",
"type": "module", "type": "module",
"workspaces": [ "workspaces": [
+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"name": "@mediabunny/mp3-encoder", "name": "@mediabunny/mp3-encoder",
"author": "Vanilagy", "author": "Vanilagy",
"version": "1.7.0", "version": "1.7.1",
"description": "MP3 encoder extension for Mediabunny, based on LAME.", "description": "MP3 encoder extension for Mediabunny, based on LAME.",
"main": "./dist/bundles/mediabunny-mp3-encoder.mjs", "main": "./dist/bundles/mediabunny-mp3-encoder.mjs",
"module": "./dist/bundles/mediabunny-mp3-encoder.mjs", "module": "./dist/bundles/mediabunny-mp3-encoder.mjs",
+22 -2
View File
@@ -164,11 +164,13 @@ export type UrlSourceOptions = {
*/ */
export class UrlSource extends Source { export class UrlSource extends Source {
/** @internal */ /** @internal */
private _url: string | URL; private _url: URL;
/** @internal */ /** @internal */
private _options: UrlSourceOptions; private _options: UrlSourceOptions;
/** @internal */ /** @internal */
private _fullData: ArrayBuffer | null = null; private _fullData: ArrayBuffer | null = null;
/** @internal */
private _nextUrlVersion: number | null = null;
constructor( constructor(
url: string | URL, url: string | URL,
@@ -189,7 +191,7 @@ export class UrlSource extends Source {
super(); super();
this._url = url; this._url = url instanceof URL ? url : new URL(url);
this._options = options; this._options = options;
} }
@@ -203,6 +205,11 @@ export class UrlSource extends Source {
headers['Range'] = `bytes=${range.start}-${range.end - 1}`; headers['Range'] = `bytes=${range.start}-${range.end - 1}`;
} }
if (this._nextUrlVersion !== null) {
this._url.searchParams.set('mediabunny_version', this._nextUrlVersion.toString());
this._nextUrlVersion++;
}
const response = await retriedFetch( const response = await retriedFetch(
this._url, this._url,
mergeObjectsDeeply(this._options.requestInit ?? {}, { mergeObjectsDeeply(this._options.requestInit ?? {}, {
@@ -218,6 +225,19 @@ export class UrlSource extends Source {
const buffer = await response.arrayBuffer(); const buffer = await response.arrayBuffer();
if (
response.status === 206
&& range
&& buffer.byteLength !== range.end - range.start
&& this._nextUrlVersion === null
) {
// We did a range request but it resolved with the wrong range; in Chromium, this can be due to a caching
// bug (https://issues.chromium.org/issues/436025873). Let's circumvent the cache for the rest of the
// session by appending a version to the URL.
this._nextUrlVersion = 1;
return this._makeRequest(range);
}
if (response.status === 200) { if (response.status === 200) {
// The server didn't return 206 Partial Content, so it's not a range response // The server didn't return 206 Partial Content, so it's not a range response
this._fullData = buffer; this._fullData = buffer;