From de086f54c613b7013ca6f0289c3ce28d01278626 Mon Sep 17 00:00:00 2001 From: Yotam Mann Date: Tue, 11 Jun 2024 16:09:27 -0400 Subject: [PATCH] Removing double-encoding of urls (#1254) * Removing double-encoding of urls, testing baseUrl * testing file with space * updating version --- Tone/core/context/ToneAudioBuffer.test.ts | 65 +++++++++++++++++----- Tone/core/context/ToneAudioBuffer.ts | 24 +------- package-lock.json | 4 +- package.json | 2 +- test/audio/name with space.wav | Bin 0 -> 264646 bytes 5 files changed, 55 insertions(+), 40 deletions(-) create mode 100644 test/audio/name with space.wav diff --git a/Tone/core/context/ToneAudioBuffer.test.ts b/Tone/core/context/ToneAudioBuffer.test.ts index 3258ab32..7af757ff 100644 --- a/Tone/core/context/ToneAudioBuffer.test.ts +++ b/Tone/core/context/ToneAudioBuffer.test.ts @@ -115,6 +115,57 @@ describe("ToneAudioBuffer", () => { }, }); }); + + it("can load an audio file with a space in the name", async () => { + const buffer = new ToneAudioBuffer( + "./test/audio/name with space.wav" + ); + expect(buffer.loaded).to.be.false; + await ToneAudioBuffer.loaded(); + expect(buffer.loaded).to.be.true; + }); + + it("can load an encoded audio file with a space in the name", async () => { + const buffer = new ToneAudioBuffer( + "./test/audio/" + encodeURIComponent("name with space.wav") + ); + expect(buffer.loaded).to.be.false; + await ToneAudioBuffer.loaded(); + expect(buffer.loaded).to.be.true; + }); + }); + + context("baseUrl", () => { + afterEach(() => { + // reset baseUrl + ToneAudioBuffer.baseUrl = ""; + }); + + it("can resolve a url without a baseUrl", async () => { + const buffer = new ToneAudioBuffer("./test/audio/sine.wav"); + expect(buffer.loaded).to.be.false; + await ToneAudioBuffer.loaded(); + expect(buffer.loaded).to.be.true; + expect(buffer.duration).to.be.closeTo(3, 0.01); + }); + + it("can resolve a url with a baseUrl", async () => { + ToneAudioBuffer.baseUrl = "./test/audio"; + const buffer = new ToneAudioBuffer("sine.wav"); + expect(buffer.loaded).to.be.false; + await ToneAudioBuffer.loaded(); + expect(buffer.loaded).to.be.true; + expect(buffer.duration).to.be.closeTo(3, 0.01); + }); + + it("can resolve a url with a baseUrl that has a trailing slash", async () => { + ToneAudioBuffer.baseUrl = "./test/audio/"; + const buffer = new ToneAudioBuffer("sine.wav"); + expect(buffer.loaded).to.be.false; + await ToneAudioBuffer.loaded(); + expect(buffer.loaded).to.be.true; + expect(buffer.duration).to.be.closeTo(3, 0.01); + }); }); context("loading", () => { @@ -141,20 +192,6 @@ describe("ToneAudioBuffer", () => { expect(hadError).to.equal(true); }); - it("can load a file with fallback extensions", async () => { - const buffer = await ToneAudioBuffer.load( - "./test/audio/sine.[nope|nada|wav]" - ); - expect(buffer).to.exist; - }); - - it("takes the first supported format when multiple extensions are provided", async () => { - const buffer = await ToneAudioBuffer.load( - "./test/audio/sine.[wav|nope]" - ); - expect(buffer).to.exist; - }); - it("instance .load method returns Promise", (done) => { const promise = new ToneAudioBuffer().load(testFile); expect(promise).to.have.property("then"); diff --git a/Tone/core/context/ToneAudioBuffer.ts b/Tone/core/context/ToneAudioBuffer.ts index be983737..d9584f8f 100644 --- a/Tone/core/context/ToneAudioBuffer.ts +++ b/Tone/core/context/ToneAudioBuffer.ts @@ -372,20 +372,6 @@ export class ToneAudioBuffer extends Tone { * Loads a url using fetch and returns the AudioBuffer. */ static async load(url: string): Promise { - // test if the url contains multiple extensions - const matches = url.match(/\[([^\]\[]+\|.+)\]$/); - if (matches) { - const extensions = matches[1].split("|"); - let extension = extensions[0]; - for (const ext of extensions) { - if (ToneAudioBuffer.supportsType(ext)) { - extension = ext; - break; - } - } - url = url.replace(matches[0], extension); - } - // make sure there is a slash between the baseUrl and the url const baseUrl = ToneAudioBuffer.baseUrl === "" || @@ -393,15 +379,7 @@ export class ToneAudioBuffer extends Tone { ? ToneAudioBuffer.baseUrl : ToneAudioBuffer.baseUrl + "/"; - // encode special characters in file path - const location = document.createElement("a"); - location.href = baseUrl + url; - location.pathname = (location.pathname + location.hash) - .split("/") - .map(encodeURIComponent) - .join("/"); - - const response = await fetch(location.href); + const response = await fetch(baseUrl + url); if (!response.ok) { throw new Error(`could not load url: ${url}`); } diff --git a/package-lock.json b/package-lock.json index 7a52ded5..73c191fd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "tone", - "version": "15.0.0", + "version": "15.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "tone", - "version": "15.0.0", + "version": "15.1.0", "license": "MIT", "dependencies": { "standardized-audio-context": "^25.3.70", diff --git a/package.json b/package.json index 48635227..f6c48107 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tone", - "version": "15.0.0", + "version": "15.1.0", "description": "A Web Audio framework for making interactive music in the browser.", "type": "module", "main": "build/esm/index.js", diff --git a/test/audio/name with space.wav b/test/audio/name with space.wav new file mode 100644 index 0000000000000000000000000000000000000000..a66b5c18c665b3dca446ae74de699aedc3d88233 GIT binary patch literal 264646 zcmeI*=T{W>+Xiqzq<1MQ9aO4F??rm=%kIu-{GqWVQDbb0MvcZ2dyFlyBxsDYjivYA zi&W_XBE3kF4oZFaGoBaM*|+;<&YUyfna{cJ>k9Mr^2!w#7GSv2Jr5o|r6ngIARxH* z^WX~z++Yd{2nomu?B9E8@3Xc42ng`khf0phomL)Fb7?cOHL3a$#3qY2QTGTNAhLGvYIqi zG?vh7)xM^tr1HD`tYnA?CGce-Y&Lb=XgH;BSEqKHOw-o4M=OU*Ulu;ft;qCBlSo!g zI2XH}Z^^sQ@nX|h+0;eqA4-|^k)_Oe&OH;&joBVAn53I}FT*Xzw%}+<$D6A)yBqes zyVvod$A9SU*r933FR3g0ge4@{vQ|p5n+ zABF!F;lX&yd`#Gq7s~877 zyQA7uh4V+2H?AuX+b1)#;l!q94b?4<1{Nku<_~Np929m=yWjKf^ZOLY4cQQG5HU;- zXYMCd$)CtGqz3sdv5PrPw~WvVPY+oQocDX}E#_gcQ{0hnv(X~NgfOt%GOKZalcM5f znI18_b$6G0=Ut~_Mz;@^c3*hUXxUYNr+T9NPVv|I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