feat: optimize and use new object

This commit is contained in:
2025-05-29 17:17:11 +02:00
parent 3ef50464aa
commit 8ef1e62e76
4 changed files with 35 additions and 44 deletions

View File

@@ -79,7 +79,7 @@
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
*/
class SIMDMersenneTwister {
export class SIMDMersenneTwister {
/* SFMT period parameters for SFMT19937 */
private readonly MEXP = 19937;
private readonly N = Math.floor(this.MEXP / 128) + 1; // 156

View File

@@ -1,17 +1,28 @@
import type { Song } from '@/types';
import { getChillhopStation } from './chillhop';
import { getSleepStationSongs } from './sleep';
const customStations = {
const customStations: Record<number, () => Promise<Song[]>> = {
50000: getSleepStationSongs,
};
const chillhopStations = Object.fromEntries(
Array.from({ length: 100 }, (_, i) => {
const stationId = 10000 + i;
return [stationId, getChillhopStation.bind(null, stationId)];
})
);
export const stations = {
...customStations,
...chillhopStations,
};
export const stations: Record<number, () => Promise<Song[]>> = new Proxy(customStations, {
get(target, prop) {
const stationId = Number(prop);
if (target[stationId]) {
return target[stationId];
}
if (stationId >= 10000 && stationId < 20000) {
return () => getChillhopStation(stationId);
}
return undefined;
},
has(target, prop) {
const stationId = Number(prop);
return target[stationId] !== undefined || (stationId >= 10000 && stationId < 20000);
}
});

View File

@@ -1,4 +1,5 @@
import type { Song } from "@/types";
import { SIMDMersenneTwister } from "@/mersenne";
export async function getSleepStationSongs(): Promise<Song[]> {
const m = new SIMDMersenneTwister();

View File

@@ -1,4 +1,4 @@
import type { Song } from '@/types.js';
import { stations } from '@/stations/index.js';
export async function GET(event) {
const { stationId } = event.params;
@@ -7,36 +7,15 @@ export async function GET(event) {
return new Response('Invalid station ID', { status: 400 });
}
if (stationId.startsWith('50')) {
// custom stations
if (stationIdInt === 50000) {
const data = await fetch('https://lofi-cdn.srizan.dev/sleep/list.json').then(async (res) => await res.json() as string[]);
const randomData = data.sort(() => 0.5 - Math.random()).slice(0, 5).map(song => {
const noOpus = song.replace('.opus', '');
const [artist, title] = noOpus.split(' - ');
return {
id: parseInt((Math.random() * 1000000).toFixed(0)),
fileId: noOpus,
endpoint: `https://lofi-cdn.srizan.dev/sleep/${song}`,
artists: artist,
title: title,
image: `https://lofi-cdn.srizan.dev/sleep/thumbs/${noOpus}.webp`,
} as Song;
});
return new Response(JSON.stringify(randomData), {
const stationFunction = stations[stationIdInt];
if (!stationFunction) {
return new Response('Station not found', { status: 404 });
}
const songs = await stationFunction();
return new Response(JSON.stringify(songs), {
headers: {
'Content-Type': 'application/json'
}
});
}
} else {
// chillhop stations
const res = await fetch(`https://stream.chillhop.com/live/${stationId}`);
const data = await res.json();
return new Response(JSON.stringify(data), {
headers: {
'Content-Type': 'application/json'
}
});
}
}