The best way to control Claude Code, Codex and more from any computer or phone
| 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(/&/g, "&") |
| 14 | .replace(/</g, "<") |
| 15 | .replace(/>/g, ">") |
| 16 | .replace(/"/g, '"') |
| 17 | .replace(/'/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 | }); |