@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 34 lines 1.15 KB
Raw
1 import * as Data from "effect/Data";
2 import * as Effect from "effect/Effect";
3 import * as HttpClient from "effect/http/HttpClient";
4 import * as Schedule from "effect/Schedule";
5
6 export class UnfurlError extends Data.TaggedError("UnfurlError")<{
7 url: string;
8 cause: unknown;
9 }> {}
10
11 const decode = (text: string) =>
12 text
13 .replace(/&amp;/g, "&")
14 .replace(/&lt;/g, "<")
15 .replace(/&gt;/g, ">")
16 .replace(/&quot;/g, '"')
17 .replace(/&#39;/g, "'")
18 .trim();
19
20 /** Fetch a page and read its `<title>`, retrying transient failures. */
21 export const unfurl = Effect.fn("unfurl")(function* (url: string) {
22 const client = (yield* HttpClient.HttpClient).pipe(
23 HttpClient.filterStatusOk,
24 HttpClient.followRedirects(5),
25 );
26 const html = yield* client.get(url).pipe(
27 Effect.flatMap((response) => response.text),
28 Effect.timeout("5 seconds"),
29 Effect.retry({ schedule: Schedule.exponential("200 millis"), times: 3 }),
30 Effect.mapError((cause) => new UnfurlError({ url, cause })),
31 );
32 const title = html.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1];
33 return { title: title ? decode(title) : new URL(url).hostname, fetchedAt: Date.now() };
34 });