mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-22 10:23:56 +00:00
feat: adding import download source
This commit is contained in:
13
src/main/workers/index.ts
Normal file
13
src/main/workers/index.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import path from "node:path";
|
||||
import steamGamesWorkerPath from "./steam-games.worker?modulePath";
|
||||
|
||||
import Piscina from "piscina";
|
||||
|
||||
import { seedsPath } from "@main/constants";
|
||||
|
||||
export const steamGamesWorker = new Piscina({
|
||||
filename: steamGamesWorkerPath,
|
||||
workerData: {
|
||||
steamGamesPath: path.join(seedsPath, "steam-games.json"),
|
||||
},
|
||||
});
|
||||
38
src/main/workers/steam-games.worker.ts
Normal file
38
src/main/workers/steam-games.worker.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { SteamGame } from "@types";
|
||||
import { orderBy, slice } from "lodash-es";
|
||||
import flexSearch from "flexsearch";
|
||||
import fs from "node:fs";
|
||||
|
||||
import { formatName } from "@shared";
|
||||
import { workerData } from "node:worker_threads";
|
||||
|
||||
const steamGamesIndex = new flexSearch.Index({
|
||||
tokenize: "forward",
|
||||
});
|
||||
|
||||
const { steamGamesPath } = workerData;
|
||||
|
||||
const data = fs.readFileSync(steamGamesPath, "utf-8");
|
||||
|
||||
const steamGames = JSON.parse(data) as SteamGame[];
|
||||
|
||||
for (let i = 0; i < steamGames.length; i++) {
|
||||
const steamGame = steamGames[i];
|
||||
|
||||
const formattedName = formatName(steamGame.name);
|
||||
|
||||
steamGamesIndex.add(i, formattedName);
|
||||
}
|
||||
|
||||
export const search = (options: flexSearch.SearchOptions) => {
|
||||
const results = steamGamesIndex.search(options.query ?? "", options);
|
||||
const games = results.map((index) => steamGames[index]);
|
||||
|
||||
return orderBy(games, ["name"], ["asc"]);
|
||||
};
|
||||
|
||||
export const getById = (id: number) =>
|
||||
steamGames.find((game) => game.id === id);
|
||||
|
||||
export const list = ({ limit, offset }: { limit: number; offset: number }) =>
|
||||
slice(steamGames, offset, offset + limit);
|
||||
@@ -1,14 +0,0 @@
|
||||
import { parentPort } from "worker_threads";
|
||||
import parseTorrent from "parse-torrent";
|
||||
|
||||
const port = parentPort;
|
||||
if (!port) throw new Error("IllegalState");
|
||||
|
||||
port.on("message", async (buffer: Buffer) => {
|
||||
try {
|
||||
const torrent = await parseTorrent(buffer);
|
||||
port.postMessage(torrent);
|
||||
} catch (err) {
|
||||
port.postMessage(null);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user