Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/i18n/resources/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@
}
}
},
"better-fullscreen": {
"description": "Enhances the fullscreen experience with lyrics, background effects, and more",
"name": "Better Fullscreen"
},
"blur-nav-bar": {
"description": "Makes navigation bar transparent and blurry",
"name": "Blur Navigation Bar"
Expand Down Expand Up @@ -588,6 +592,20 @@
},
"name": "In-App Menu"
},
"lyrics-provider": {
"description": "Provides lyrics fetching functionality for other plugins",
"menu": {
"preferred-provider": {
"label": "Preferred Provider",
"tooltip": "Choose the default provider to use",
"none": {
"label": "None",
"tooltip": "No preferred provider"
}
}
},
"name": "Lyrics Provider"
},
"lumiastream": {
"description": "Adds Lumia Stream support",
"name": "Lumia Stream [Beta]"
Expand Down Expand Up @@ -830,14 +848,6 @@
"not-found": "⚠️ No lyrics found for this song."
},
"menu": {
"preferred-provider": {
"label": "Preferred Provider",
"tooltip": "Choose the default provider to use",
"none": {
"label": "None",
"tooltip": "No preferred provider"
}
},
"default-text-string": {
"label": "Default character between lyrics",
"tooltip": "Choose the default character to use for the gap between lyrics"
Expand Down
44 changes: 41 additions & 3 deletions src/loader/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,52 @@ export const forceLoadMainPlugin = async (
}
};

const topologicalSort = (plugins: Record<string, PluginDef<unknown, unknown, unknown>>) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Replace plugins:·Record<string,·PluginDef<unknown,·unknown,·unknown>> with ⏎··plugins:·Record<string,·PluginDef<unknown,·unknown,·unknown>>,⏎

Suggested change
const topologicalSort = (plugins: Record<string, PluginDef<unknown, unknown, unknown>>) => {
const topologicalSort = (
plugins: Record<string, PluginDef<unknown, unknown, unknown>>,
) => {

const visited = new Set<string>();
const visiting = new Set<string>();
const order: string[] = [];

const visit = (id: string) => {
if (visited.has(id)) return;
if (visiting.has(id)) {
console.warn(`Circular dependency detected involving plugin: ${id}`);
return;
}

visiting.add(id);
const plugin = plugins[id];
if (plugin?.dependencies) {
for (const dep of plugin.dependencies) {
if (plugins[dep]) {
visit(dep);
} else {
console.warn(`Plugin ${id} depends on ${dep} which is not found`);
}
}
}
visiting.delete(id);
visited.add(id);
order.push(id);
};

for (const id of Object.keys(plugins)) {
visit(id);
}

return order;
};

export const loadAllMainPlugins = async (win: BrowserWindow) => {
console.log(LoggerPrefix, t('common.console.plugins.load-all'));
const pluginConfigs = config.plugins.getPlugins();
const allPluginsMap = await mainPlugins();
const sortedPluginIds = topologicalSort(allPluginsMap);
const queue: Promise<void>[] = [];

for (const [plugin, pluginDef] of Object.entries(await mainPlugins())) {
const config = deepmerge(pluginDef.config, pluginConfigs[plugin] ?? {});
if (config.enabled) {
for (const plugin of sortedPluginIds) {
const pluginDef = allPluginsMap[plugin];
const pluginConfig = deepmerge(pluginDef.config, pluginConfigs[plugin] ?? {});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Replace pluginDef.config,·pluginConfigs[plugin]·??·{} with ⏎······pluginDef.config,⏎······pluginConfigs[plugin]·??·{},⏎····

Suggested change
const pluginConfig = deepmerge(pluginDef.config, pluginConfigs[plugin] ?? {});
const pluginConfig = deepmerge(
pluginDef.config,
pluginConfigs[plugin] ?? {},
);

if (pluginConfig.enabled) {
queue.push(forceLoadMainPlugin(plugin, win));
} else if (loadedPluginMap[plugin]) {
queue.push(forceUnloadMainPlugin(plugin, win));
Expand Down
44 changes: 41 additions & 3 deletions src/loader/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,54 @@ export const forceLoadPreloadPlugin = async (id: string) => {
}
};

const topologicalSort = (plugins: Record<string, PluginDef<unknown, unknown, unknown>>) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Replace plugins:·Record<string,·PluginDef<unknown,·unknown,·unknown>> with ⏎··plugins:·Record<string,·PluginDef<unknown,·unknown,·unknown>>,⏎

Suggested change
const topologicalSort = (plugins: Record<string, PluginDef<unknown, unknown, unknown>>) => {
const topologicalSort = (
plugins: Record<string, PluginDef<unknown, unknown, unknown>>,
) => {

const visited = new Set<string>();
const visiting = new Set<string>();
const order: string[] = [];

const visit = (id: string) => {
if (visited.has(id)) return;
if (visiting.has(id)) {
console.warn(`Circular dependency detected involving plugin: ${id}`);
return;
}

visiting.add(id);
const plugin = plugins[id];
if (plugin?.dependencies) {
for (const dep of plugin.dependencies) {
if (plugins[dep]) {
visit(dep);
} else {
console.warn(`Plugin ${id} depends on ${dep} which is not found`);
}
}
}
visiting.delete(id);
visited.add(id);
order.push(id);
};

for (const id of Object.keys(plugins)) {
visit(id);
}

return order;
};

export const loadAllPreloadPlugins = async () => {
const pluginConfigs = config.plugins.getPlugins();
const allPluginsMap = await preloadPlugins();
const sortedPluginIds = topologicalSort(allPluginsMap);

for (const [pluginId, pluginDef] of Object.entries(await preloadPlugins())) {
const config = deepmerge(
for (const pluginId of sortedPluginIds) {
const pluginDef = allPluginsMap[pluginId];
const pluginConfig = deepmerge(
pluginDef.config ?? { enable: false },
pluginConfigs[pluginId] ?? {},
);

if (config.enabled) {
if (pluginConfig.enabled) {
forceLoadPreloadPlugin(pluginId);
} else {
if (loadedPluginMap[pluginId]) {
Expand Down
40 changes: 39 additions & 1 deletion src/loader/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,48 @@ export const forceLoadRendererPlugin = async (id: string) => {
}
};

const topologicalSort = (plugins: Record<string, PluginDef<unknown, unknown, unknown>>) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Replace plugins:·Record<string,·PluginDef<unknown,·unknown,·unknown>> with ⏎··plugins:·Record<string,·PluginDef<unknown,·unknown,·unknown>>,⏎

Suggested change
const topologicalSort = (plugins: Record<string, PluginDef<unknown, unknown, unknown>>) => {
const topologicalSort = (
plugins: Record<string, PluginDef<unknown, unknown, unknown>>,
) => {

const visited = new Set<string>();
const visiting = new Set<string>();
const order: string[] = [];

const visit = (id: string) => {
if (visited.has(id)) return;
if (visiting.has(id)) {
console.warn(`Circular dependency detected involving plugin: ${id}`);
return;
}

visiting.add(id);
const plugin = plugins[id];
if (plugin?.dependencies) {
for (const dep of plugin.dependencies) {
if (plugins[dep]) {
visit(dep);
} else {
console.warn(`Plugin ${id} depends on ${dep} which is not found`);
}
}
}
visiting.delete(id);
visited.add(id);
order.push(id);
};

for (const id of Object.keys(plugins)) {
visit(id);
}

return order;
};

export const loadAllRendererPlugins = async () => {
const pluginConfigs = window.mainConfig.plugins.getPlugins();
const allPluginsMap = await rendererPlugins();
const sortedPluginIds = topologicalSort(allPluginsMap);

for (const [pluginId, pluginDef] of Object.entries(await rendererPlugins())) {
for (const pluginId of sortedPluginIds) {
const pluginDef = allPluginsMap[pluginId];
const config = deepmerge(pluginDef.config, pluginConfigs[pluginId] ?? {});

if (config.enabled) {
Expand Down
30 changes: 30 additions & 0 deletions src/plugins/lyrics-provider/backend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { net } from 'electron';

import { createBackend } from '@/utils';

const handlers = {
// Note: This will only be used for Forbidden headers, e.g. User-Agent, Authority, Cookie, etc.
// See: https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_request_header
async fetch(
url: string,
init: RequestInit,
): Promise<[number, string, Record<string, string>]> {
const res = await net.fetch(url, init);
return [
res.status,
await res.text(),
Object.fromEntries(res.headers.entries()),
];
},
};

export const backend = createBackend({
start(ctx) {
ctx.ipc.handle('lyrics-provider:fetch', (url: string, init: RequestInit) =>
handlers.fetch(url, init),
);
},
stop(ctx) {
ctx.ipc.removeHandler('lyrics-provider:fetch');
},
});
20 changes: 20 additions & 0 deletions src/plugins/lyrics-provider/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createPlugin } from '@/utils';
import { t } from '@/i18n';

import { backend } from './backend';
import { renderer } from './renderer';
import { menu } from './menu';

export default createPlugin({
name: () => t('plugins.lyrics-provider.name'),
description: () => t('plugins.lyrics-provider.description'),
restartNeeded: false,
config: {
enabled: true,
preferredProvider: null as string | null,
},

backend,
renderer,
menu,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Insert

Suggested change
});
});

44 changes: 44 additions & 0 deletions src/plugins/lyrics-provider/menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { t } from '@/i18n';

import { providerNames } from './providers';

import type { MenuItemConstructorOptions } from 'electron';
import type { MenuContext } from '@/types/contexts';

export const menu = async (
ctx: MenuContext,
): Promise<MenuItemConstructorOptions[]> => {
const config = await ctx.getConfig();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <@typescript-eslint/no-unsafe-assignment> reported by reviewdog 🐶
Unsafe assignment of an error typed value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <@typescript-eslint/no-unsafe-call> reported by reviewdog 🐶
Unsafe call of a type that could not be resolved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <@typescript-eslint/no-unsafe-member-access> reported by reviewdog 🐶
Unsafe member access .getConfig on a type that cannot be resolved.


return [
{
label: t('plugins.lyrics-provider.menu.preferred-provider.label'),
toolTip: t('plugins.lyrics-provider.menu.preferred-provider.tooltip'),
type: 'submenu',
submenu: [
{
label: t('plugins.lyrics-provider.menu.preferred-provider.none.label'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Replace 'plugins.lyrics-provider.menu.preferred-provider.none.label' with ⏎············'plugins.lyrics-provider.menu.preferred-provider.none.label',⏎··········

Suggested change
label: t('plugins.lyrics-provider.menu.preferred-provider.none.label'),
label: t(
'plugins.lyrics-provider.menu.preferred-provider.none.label',
),

toolTip: t(
'plugins.lyrics-provider.menu.preferred-provider.none.tooltip',
),
type: 'radio',
checked: config.preferredProvider === null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <@typescript-eslint/no-unsafe-member-access> reported by reviewdog 🐶
Unsafe member access .preferredProvider on a type that cannot be resolved.

click() {
ctx.setConfig({ preferredProvider: null });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <@typescript-eslint/no-unsafe-call> reported by reviewdog 🐶
Unsafe call of a type that could not be resolved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <@typescript-eslint/no-unsafe-member-access> reported by reviewdog 🐶
Unsafe member access .setConfig on a type that cannot be resolved.

},
},
...providerNames.map(
(provider) =>
({
label: provider,
type: 'radio',
checked: config.preferredProvider === provider,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <@typescript-eslint/no-unsafe-member-access> reported by reviewdog 🐶
Unsafe member access .preferredProvider on a type that cannot be resolved.

click() {
ctx.setConfig({ preferredProvider: provider });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <@typescript-eslint/no-unsafe-call> reported by reviewdog 🐶
Unsafe call of a type that could not be resolved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <@typescript-eslint/no-unsafe-member-access> reported by reviewdog 🐶
Unsafe member access .setConfig on a type that cannot be resolved.

},
}) as const,
),
],
},
];
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [eslint] <prettier/prettier> reported by reviewdog 🐶
Insert

Suggested change
};
};

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { jaroWinkler } from '@skyra/jaro-winkler';

import { config } from '../renderer/renderer';
import { netFetch } from '../renderer';
import { LRC } from '../parsers/lrc';

import type { LyricProvider, LyricResult, SearchSongInfo } from '../types';
Expand Down Expand Up @@ -28,13 +29,13 @@ export class LRCLib implements LyricProvider {
}

let url = `${this.baseUrl}/api/search?${query.toString()}`;
let response = await fetch(url);
let [status, text] = await netFetch(url);

if (!response.ok) {
throw new Error(`bad HTTPStatus(${response.statusText})`);
if (status < 200 || status >= 300) {
throw new Error(`bad HTTPStatus(${status})`);
}

let data = (await response.json()) as LRCLIBSearchResponse;
let data = JSON.parse(text) as LRCLIBSearchResponse;
if (!data || !Array.isArray(data)) {
throw new Error(`Expected an array, instead got ${typeof data}`);
}
Expand All @@ -49,12 +50,12 @@ export class LRCLib implements LyricProvider {
query = new URLSearchParams({ q: `${trackName}` });
url = `${this.baseUrl}/api/search?${query.toString()}`;

response = await fetch(url);
if (!response.ok) {
throw new Error(`bad HTTPStatus(${response.statusText})`);
[status, text] = await netFetch(url);
if (status < 200 || status >= 300) {
throw new Error(`bad HTTPStatus(${status})`);
}

data = (await response.json()) as LRCLIBSearchResponse;
data = JSON.parse(text) as LRCLIBSearchResponse;
if (!Array.isArray(data)) {
throw new Error(`Expected an array, instead got ${typeof data}`);
}
Expand All @@ -64,12 +65,12 @@ export class LRCLib implements LyricProvider {
query = new URLSearchParams({ q: title });
url = `${this.baseUrl}/api/search?${query.toString()}`;

response = await fetch(url);
if (!response.ok) {
throw new Error(`bad HTTPStatus(${response.statusText})`);
[status, text] = await netFetch(url);
if (status < 200 || status >= 300) {
throw new Error(`bad HTTPStatus(${status})`);
}

data = (await response.json()) as LRCLIBSearchResponse;
data = JSON.parse(text) as LRCLIBSearchResponse;
if (!Array.isArray(data)) {
throw new Error(`Expected an array, instead got ${typeof data}`);
}
Expand Down
Loading
Loading