mirror of
https://github.com/Tonejs/Tone.js
synced 2024-12-25 02:53:06 +00:00
Removing double-encoding of urls (#1254)
* Removing double-encoding of urls, testing baseUrl * testing file with space * updating version
This commit is contained in:
parent
73f158f686
commit
de086f54c6
5 changed files with 55 additions and 40 deletions
|
@ -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", () => {
|
context("loading", () => {
|
||||||
|
@ -141,20 +192,6 @@ describe("ToneAudioBuffer", () => {
|
||||||
expect(hadError).to.equal(true);
|
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) => {
|
it("instance .load method returns Promise", (done) => {
|
||||||
const promise = new ToneAudioBuffer().load(testFile);
|
const promise = new ToneAudioBuffer().load(testFile);
|
||||||
expect(promise).to.have.property("then");
|
expect(promise).to.have.property("then");
|
||||||
|
|
|
@ -372,20 +372,6 @@ export class ToneAudioBuffer extends Tone {
|
||||||
* Loads a url using fetch and returns the AudioBuffer.
|
* Loads a url using fetch and returns the AudioBuffer.
|
||||||
*/
|
*/
|
||||||
static async load(url: string): Promise<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
|
// make sure there is a slash between the baseUrl and the url
|
||||||
const baseUrl =
|
const baseUrl =
|
||||||
ToneAudioBuffer.baseUrl === "" ||
|
ToneAudioBuffer.baseUrl === "" ||
|
||||||
|
@ -393,15 +379,7 @@ export class ToneAudioBuffer extends Tone {
|
||||||
? ToneAudioBuffer.baseUrl
|
? ToneAudioBuffer.baseUrl
|
||||||
: ToneAudioBuffer.baseUrl + "/";
|
: ToneAudioBuffer.baseUrl + "/";
|
||||||
|
|
||||||
// encode special characters in file path
|
const response = await fetch(baseUrl + url);
|
||||||
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);
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`could not load url: ${url}`);
|
throw new Error(`could not load url: ${url}`);
|
||||||
}
|
}
|
||||||
|
|
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "tone",
|
"name": "tone",
|
||||||
"version": "15.0.0",
|
"version": "15.1.0",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "tone",
|
"name": "tone",
|
||||||
"version": "15.0.0",
|
"version": "15.1.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"standardized-audio-context": "^25.3.70",
|
"standardized-audio-context": "^25.3.70",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "tone",
|
"name": "tone",
|
||||||
"version": "15.0.0",
|
"version": "15.1.0",
|
||||||
"description": "A Web Audio framework for making interactive music in the browser.",
|
"description": "A Web Audio framework for making interactive music in the browser.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "build/esm/index.js",
|
"main": "build/esm/index.js",
|
||||||
|
|
BIN
test/audio/name with space.wav
Normal file
BIN
test/audio/name with space.wav
Normal file
Binary file not shown.
Loading…
Reference in a new issue