Remove base url from sample server and properly handle url schemes

This commit is contained in:
Aria 2025-11-20 12:46:29 -06:00 committed by W-A-James
parent 9fdba6374c
commit d70b758065
No known key found for this signature in database
GPG key ID: 2CE8DBC07DABE749
4 changed files with 16 additions and 6 deletions

View file

@ -55,7 +55,6 @@ async function getBanks(directory, flat = false) {
banks[bank].push(subDir); banks[bank].push(subDir);
return subDir; return subDir;
}); });
banks._base = `http://localhost:${PORT}`;
return { banks, files }; return { banks, files };
} }

View file

@ -1,4 +1,4 @@
import { getCommonSampleInfo } from './util.mjs'; import { getBaseURL, getCommonSampleInfo } from './util.mjs';
import { registerSound, registerWaveTable } from './index.mjs'; import { registerSound, registerWaveTable } from './index.mjs';
import { getAudioContext } from './audioContext.mjs'; import { getAudioContext } from './audioContext.mjs';
import { getADSRValues, getParamADSR, getPitchEnvelope, getVibratoOscillator } from './helpers.mjs'; import { getADSRValues, getParamADSR, getPitchEnvelope, getVibratoOscillator } from './helpers.mjs';
@ -211,7 +211,7 @@ export async function fetchSampleMap(url) {
// not a browser // not a browser
return; return;
} }
const base = url.split('/').slice(0, -1).join('/'); const base = getBaseURL(url);
if (typeof fetch === 'undefined') { if (typeof fetch === 'undefined') {
// skip fetch when in node / testing // skip fetch when in node / testing
return; return;

View file

@ -113,4 +113,14 @@ export function getCommonSampleInfo(hapValue, bank) {
/** Selects entries from `source` and renames them via `map` */ /** Selects entries from `source` and renames them via `map` */
export const pickAndRename = (source, map) => { export const pickAndRename = (source, map) => {
return Object.fromEntries(Object.entries(map).map(([newKey, oldKey]) => [newKey, source[oldKey]])); return Object.fromEntries(Object.entries(map).map(([newKey, oldKey]) => [newKey, source[oldKey]]));
}
export const getBaseURL = (url) => {
try {
// For real URLs
return new URL('.', new URL(url)).href.replace(/\/$/, ''); // removes trailing slash
} catch {
// For pseudo URLS
return url.split('/').slice(0, -1).join('/');
}
}; };

View file

@ -1,5 +1,5 @@
import { getAudioContext, registerSound } from './index.mjs'; import { getAudioContext, registerSound } from './index.mjs';
import { getCommonSampleInfo } from './util.mjs'; import { getBaseURL, getCommonSampleInfo } from './util.mjs';
import { import {
applyFM, applyFM,
applyParameterModulators, applyParameterModulators,
@ -134,7 +134,7 @@ function githubPath(base, subpath = '') {
return `https://raw.githubusercontent.com/${path}/${subpath}`; return `https://raw.githubusercontent.com/${path}/${subpath}`;
} }
const _processTables = (json, baseUrl, frameLen, options = {}) => { const _processTables = (json, baseUrl, frameLen) => {
baseUrl = json._base || baseUrl; baseUrl = json._base || baseUrl;
return Object.entries(json).forEach(([key, tables]) => { return Object.entries(json).forEach(([key, tables]) => {
if (key === '_base') return false; if (key === '_base') return false;
@ -190,6 +190,7 @@ export const tables = async (url, frameLen, json, options = {}) => {
if (url.startsWith('local:')) { if (url.startsWith('local:')) {
url = `http://localhost:5432`; url = `http://localhost:5432`;
} }
const base = getBaseURL(url);
if (typeof fetch !== 'function') { if (typeof fetch !== 'function') {
// not a browser // not a browser
return; return;
@ -200,7 +201,7 @@ export const tables = async (url, frameLen, json, options = {}) => {
} }
return fetch(url) return fetch(url)
.then((res) => res.json()) .then((res) => res.json())
.then((json) => _processTables(json, url, frameLen, options)) .then((json) => _processTables(json, base, frameLen, options))
.catch((error) => { .catch((error) => {
console.error(error); console.error(error);
throw new Error(`error loading "${url}"`); throw new Error(`error loading "${url}"`);