aboutsummaryrefslogtreecommitdiff
path: root/castle-search/src/search.ts
diff options
context:
space:
mode:
authorIgor Soarez <igor@soarez.org>2026-11-18 04:55:45 +0000
committerIgor Soarez <igor@soarez.org>2026-11-18 04:55:45 +0000
commitb03415aafeea985561f3980f8b484173530a05f8 (patch)
tree3748f62ae52a5c4d7906ec3268bb8b2863a5114a /castle-search/src/search.ts
parent4c1f9b4ea943d03e9610f13344c372e630db5b9d (diff)
castle-search: search X over HTTP, through the browser's signed-in session
POST /x-search reads x.com/search in a background tab: posts with their link, author, time, text, counts and quoted post, scrolling for up to 20. GET /openapi.json describes it for Open WebUI's tool servers. Signed out, an X error page or a stuck page are errors, never an empty list.
Diffstat (limited to 'castle-search/src/search.ts')
-rw-r--r--castle-search/src/search.ts24
1 files changed, 24 insertions, 0 deletions
diff --git a/castle-search/src/search.ts b/castle-search/src/search.ts
index b5effb8..0178349 100644
--- a/castle-search/src/search.ts
+++ b/castle-search/src/search.ts
@@ -393,3 +393,27 @@ export const resolveLink = async (url: string, signal: AbortSignal): Promise<Res
};
const isGoogleHost = (url: string): boolean => /^https?:\/\/([a-z0-9-]+\.)*google\.(?:com?\.)?[a-z]{2,3}\//.test(url);
+
+/**
+ * Runs `fn` on a fresh background tab of the shared browser, under the same
+ * concurrency cap and connection as a search, and closes the tab on every path.
+ * For page work that is not an engine search, such as x-search.ts.
+ */
+export const withPage = async <T>(
+ signal: AbortSignal,
+ fn: (page: PageSession, endpoint: string) => Promise<T>,
+): Promise<T> => {
+ const release = await acquire(signal);
+ try {
+ const conn = await ensureConnection(signal);
+ const endpoint = describeCdpTarget(conn.target);
+ const page = await PageSession.open(conn, signal);
+ try {
+ return await fn(page, endpoint);
+ } finally {
+ await page.close();
+ }
+ } finally {
+ release();
+ }
+};