Skip to content

Commit

Permalink
Switch to bare client for omnibox, remove libcurl as a dep
Browse files Browse the repository at this point in the history
  • Loading branch information
MotorTruck1221 committed Nov 8, 2024
1 parent 32a7cc3 commit 0d1d6e5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 44 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"fastify": "^5.1.0",
"form-data": "^4.0.1",
"gradient-string": "^3.0.0",
"libcurl.js-new": "npm:libcurl.js@^0.6.19",
"nanostores": "^0.10.3",
"ora": "^8.1.1",
"pg": "^8.13.1",
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/components/settings/Loader.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script>
import { settings, Settings } from "@utils/settings/index";
import { loadProxyScripts, setTransport, initSw, setSWStuff } from "@utils/registerSW";
// This loads the settings in a nice way
settings.tabSettings.cloakTab(localStorage.getItem(Settings.TabSettings.tabCloak) as string || "default");
settings.proxySettings.changeProxy(localStorage.getItem(Settings.ProxySettings.proxy) as string || "automatic");
Expand All @@ -12,4 +13,13 @@
settings.tabSettings.cloakTab(localStorage.getItem(Settings.TabSettings.tabCloak) as string || "default");
//settings.marketPlaceSettings.changeTheme(false);
});
document.addEventListener("DOMContentLoaded", async () => {
const conn = await loadProxyScripts();
await setTransport(
conn,
localStorage.getItem(Settings.ProxySettings.transport) as string
);
const sw = await initSw();
await setSWStuff({sw, conn});
});
</script>
19 changes: 8 additions & 11 deletions src/pages/[lang]/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import { VERSION } from "astro:env/client";
</div>
</Layout>
<script>
import { initSw, setTransport, loadProxyScripts } from "@utils/registerSW.ts"; //../../utils/registerSW.ts
import { setTransport, getSWStuff } from "@utils/registerSW"; //../../utils/registerSW.ts
import { pageLoad } from "@utils/events";
import { SupportedSites } from "@utils/siteSupport";
import {
Expand All @@ -66,8 +66,8 @@ import { VERSION } from "astro:env/client";
cloak,
settings,
} from "@utils/settings/index";
import { search } from "@utils/search.ts"; //../../utils/search.ts
import { client as libcurlClient } from "@utils/libcurl";
import { search } from "@utils/search"; //../../utils/search.ts
import { BareClient } from "@mercuryworkshop/bare-mux";
type Suggestion = {
phrase: string;
};
Expand Down Expand Up @@ -97,12 +97,11 @@ import { VERSION } from "astro:env/client";
}
}
async function uv(iframe: HTMLIFrameElement, term: string) {
const conn = await loadProxyScripts();
const { sw, conn } = getSWStuff();
await setTransport(
conn,
localStorage.getItem(Settings.ProxySettings.transport) as string
);
const sw = await initSw();
await settings.marketPlaceSettings.handlePlugins(sw);
iframe.classList.remove("hidden");
const url = await proxy(term);
Expand Down Expand Up @@ -148,12 +147,10 @@ import { VERSION } from "astro:env/client";
omnibox.classList.add("hidden");
}
if (value.length >= 3) {
await libcurlClient.initLibcurl();
const data = (await libcurlClient.fetchFromLibcurl(
`https://api.duckduckgo.com/ac?q=${encodeURIComponent(value)}&format=json`,
"json"
)) as [];
const filteredData = data.slice(0, 8); //Trim to only about 8 results. Any more and our omnibox dies
const client = new BareClient();
const data = await client.fetch(`https://api.duckduckgo.com/ac?q=${encodeURIComponent(value)}&format=json`);
const dataRes = await data.json();
const filteredData = dataRes.slice(0, 8); //Trim to only about 8 results. Any more and our omnibox dies
if (filteredData) {
omnibox.innerHTML = "";
filteredData.map((results: Suggestion) => {
Expand Down
28 changes: 0 additions & 28 deletions src/utils/libcurl.ts

This file was deleted.

27 changes: 26 additions & 1 deletion src/utils/registerSW.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { BareMuxConnection } from "@mercuryworkshop/bare-mux";
import { Settings, WispServerURLS } from "./settings/index";

let baremuxConn: BareMuxConnection;
let swReg: ServiceWorkerRegistration;

function loadProxyScripts() {
//wrap everything in a promise to avoid race conditions
return new Promise<BareMuxConnection>((resolve) => {
Expand Down Expand Up @@ -57,4 +61,25 @@ function initSw() {
});
}

export { initSw, setTransport, loadProxyScripts };
interface SWStuff {
sw: ServiceWorkerRegistration;
conn: BareMuxConnection;
}

function setSWStuff(stuff: SWStuff): Promise<void> {
return new Promise<void>((resolve) => {
swReg = stuff.sw;
baremuxConn = stuff.conn;
resolve();
});
}

function getSWStuff(): SWStuff {
const stuff: SWStuff = {
sw: swReg,
conn: baremuxConn
}
return stuff;
}

export { initSw, setTransport, loadProxyScripts, setSWStuff, getSWStuff };

0 comments on commit 0d1d6e5

Please sign in to comment.