// Where Bluesky is: the public AppView (the read API, no login), the web app // (bsky.app, the page a post capture screenshots) and the CDN (a post's // images). One place, so a test can put all three on a fake. // // TEST-ONLY: E2E_BLUESKY_ORIGIN_FILE names a file holding ONE loopback origin // (`http://127.0.0.1:`). While that file exists, all three hosts are // that origin — the editor e2e suite's fake Bluesky // (common/testing/social/fakeBluesky.mjs), which a spec starts and names in // the file. The file is read per call, so a spec can start its fake after the // test server booted. Anything but a loopback origin in it is ignored. Unset // everywhere but the editor's e2e server. Unit tests pass endpoints directly. import { readFileSync } from "node:fs"; export type BlueskyEndpoints = { // The public AppView: com.atproto.identity.resolveHandle, // app.bsky.feed.getPosts, getAuthorFeed, getProfile. appview: string; // The web app: /profile//post/. web: string; // The image CDN: a post's `embed.images[].fullsize`. cdn: string; }; export const BLUESKY_ENDPOINTS: Readonly = Object.freeze({ appview: "https://public.api.bsky.app", web: "https://bsky.app", cdn: "https://cdn.bsky.app", }); const LOOPBACK_ORIGIN = /^http:\/\/(?:127\.0\.0\.1|localhost)(?::\d{1,5})?$/; export function blueskyEndpoints( env: Readonly> = process.env, ): BlueskyEndpoints { const file = env.E2E_BLUESKY_ORIGIN_FILE; if (file) { let origin = ""; try { origin = readFileSync(file, "utf8").trim().replace(/\/$/, ""); } catch { origin = ""; } if (LOOPBACK_ORIGIN.test(origin)) return { appview: origin, web: origin, cdn: origin }; } return { ...BLUESKY_ENDPOINTS }; } // A Bluesky URL (one of the three real hosts) on `endpoints`: the AppView's // image URLs name cdn.bsky.app, which a fake must serve. Any other URL is // returned as it is. export function onBlueskyEndpoints(url: string, endpoints: BlueskyEndpoints): string { for (const key of ["appview", "web", "cdn"] as const) { const real = BLUESKY_ENDPOINTS[key]; if (url === real || url.startsWith(`${real}/`)) return endpoints[key] + url.slice(real.length); } return url; }