Skip to content

Commit

Permalink
Removing double-encoding of urls (#1254)
Browse files Browse the repository at this point in the history
* Removing double-encoding of urls, testing baseUrl

* testing file with space

* updating version
  • Loading branch information
tambien committed Jun 11, 2024
1 parent 73f158f commit de086f5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 40 deletions.
65 changes: 51 additions & 14 deletions Tone/core/context/ToneAudioBuffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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");
Expand Down
24 changes: 1 addition & 23 deletions Tone/core/context/ToneAudioBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,36 +372,14 @@ export class ToneAudioBuffer extends Tone {
* Loads a url using fetch and returns the AudioBuffer.
*/
static async load(url: string): Promise<AudioBuffer> {
// 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 === "" ||
ToneAudioBuffer.baseUrl.endsWith("/")
? 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}`);
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Binary file added test/audio/name with space.wav
Binary file not shown.

0 comments on commit de086f5

Please sign in to comment.